Database design best practices in SQL - Time & Space Complexity
When designing a database, it is important to understand how the structure affects the speed of queries.
We want to know how the design choices impact the time it takes to get data as the database grows.
Analyze the time complexity of this simple query on a well-designed table.
SELECT customer_name, order_date
FROM orders
WHERE customer_id = 12345;
This query fetches orders for one customer using an indexed column.
Look for repeated steps in the query process.
- Primary operation: Searching the index for matching customer_id.
- How many times: Depends on the number of matching orders for that customer.
As the total number of orders grows, the index helps keep search fast.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3-4 steps to find data |
| 100 | About 5-7 steps |
| 1000 | About 7-10 steps |
Pattern observation: The search steps grow slowly even if the data grows a lot, thanks to indexing.
Time Complexity: O(log n)
This means the time to find data grows slowly as the database gets bigger, making queries efficient.
[X] Wrong: "Adding more data always makes queries take much longer."
[OK] Correct: Proper design with indexes keeps search time growing slowly, not linearly.
Understanding how design affects query speed shows you know how to build databases that work well as they grow.
"What if we removed the index on customer_id? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of primary keys
Primary keys uniquely identify each row in a table, preventing duplicates.Step 2: Compare options with primary key purpose
Only To uniquely identify each record in the table describes uniqueness, which is the main purpose of primary keys.Final Answer:
To uniquely identify each record in the table -> Option DQuick Check:
Primary key = unique record ID [OK]
- Thinking primary keys store large text
- Confusing primary keys with performance hardware
- Believing primary keys allow duplicates
id column?Solution
Step 1: Review correct primary key syntax
The primary key is declared asid INT PRIMARY KEYinside the column definition.Step 2: Check each option's syntax
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); uses correct syntax; A has invalid syntax; B places PRIMARY KEY on wrong column; D uses UNIQUE but not PRIMARY KEY.Final Answer:
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); -> Option AQuick Check:
Primary key syntax = column_name TYPE PRIMARY KEY [OK]
- Placing PRIMARY KEY on wrong column
- Using UNIQUE instead of PRIMARY KEY
- Incorrect syntax for primary key declaration
CREATE TABLE authors (author_id INT PRIMARY KEY, name VARCHAR(100));CREATE TABLE books (book_id INT PRIMARY KEY, title VARCHAR(100), author_id INT, FOREIGN KEY (author_id) REFERENCES authors(author_id));What will happen if you try to insert a book with
author_id = 5, but no author with author_id = 5 exists?Solution
Step 1: Understand foreign key constraints
Foreign keys require the referenced value to exist in the parent table to maintain data integrity.Step 2: Apply constraint to the insert operation
Inserting a book with author_id = 5 fails if no author with id 5 exists, causing a constraint violation.Final Answer:
The insert will fail due to foreign key constraint violation -> Option BQuick Check:
Foreign key enforces existing parent record [OK]
- Assuming foreign keys auto-create missing parents
- Thinking foreign keys allow NULL by default
- Ignoring constraint errors on insert
orders with columns order_id (primary key), customer_id, and order_date. You want to add a foreign key constraint on customer_id referencing customers(customer_id). Which of these statements has an error?Solution
Step 1: Review correct syntax for adding foreign keys
The correct syntax requires parentheses around the column name after FOREIGN KEY.Step 2: Identify the incorrect statement
ALTER TABLE orders ADD FOREIGN KEY customer_id REFERENCES customers(customer_id); misses parentheses aroundcustomer_id, causing syntax error.Final Answer:
ALTER TABLE orders ADD FOREIGN KEY customer_id REFERENCES customers(customer_id); -> Option CQuick Check:
Foreign key column must be in parentheses [OK]
- Omitting parentheses around column names
- Forgetting to name the constraint (optional but recommended)
- Misplacing ON DELETE options
Solution
Step 1: Analyze normalization and relationships
Best practice is to separate entities into tables and link them with keys to avoid duplication.Step 2: Evaluate each option's design
Separate tables for books, authors, and categories with primary keys and foreign keys linking them uses separate tables with keys, which supports clear organization and data consistency.Final Answer:
Separate tables for books, authors, and categories with primary keys and foreign keys linking them -> Option AQuick Check:
Separate tables + keys = best design [OK]
- Putting all data in one table causing duplicates
- Mixing unrelated data in same table
- Storing related entity names as plain text
Ready to prove it?
Pass the test and this pattern moves unknown
