Composite primary keys in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using composite primary keys in a database, it's important to understand how the time to find or insert data changes as the table grows.
We want to know how the database handles searching and maintaining these keys as more rows are added.
Analyze the time complexity of this table definition and a query using a composite primary key.
CREATE TABLE Orders (
OrderID INT,
ProductID INT,
Quantity INT,
PRIMARY KEY (OrderID, ProductID)
);
SELECT * FROM Orders WHERE OrderID = 101 AND ProductID = 202;
This code creates a table with a composite primary key made of two columns and queries a row using both keys.
Look at what the database does repeatedly when searching or inserting.
- Primary operation: Searching the index built on both OrderID and ProductID.
- How many times: The database traverses the index tree nodes, which depends on the number of rows.
As the number of rows grows, the database index grows too, but it stays balanced.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3-4 steps to find a row |
| 100 | About 5-6 steps |
| 1000 | About 7-8 steps |
Pattern observation: The number of steps grows slowly, even if the table gets much bigger.
Time Complexity: O(log n)
This means the time to find or insert a row grows slowly and predictably as the table gets bigger.
[X] Wrong: "Using two columns as a primary key doubles the search time compared to one column."
[OK] Correct: The database uses a balanced index tree that handles multiple columns efficiently, so search time grows with the total number of rows, not the number of key columns.
Understanding how composite keys affect search time shows you know how databases keep data organized and efficient, a useful skill in many real projects.
What if we added a third column to the composite primary key? How would the time complexity change?
Practice
Solution
Step 1: Understand primary key basics
A primary key uniquely identifies each row in a table.Step 2: Define composite primary key
A composite primary key uses two or more columns together to ensure uniqueness.Final Answer:
A primary key made up of two or more columns combined to uniquely identify a row. -> Option BQuick Check:
Composite primary key = multiple columns [OK]
- Thinking a primary key can have duplicates
- Confusing composite key with foreign key
- Assuming composite key uses only one column
order_id and product_id in SQL?Solution
Step 1: Recall SQL syntax for composite keys
Composite keys are defined by listing columns inside parentheses separated by commas.Step 2: Match correct syntax
PRIMARY KEY (order_id, product_id) uses parentheses and comma correctly: PRIMARY KEY (order_id, product_id).Final Answer:
PRIMARY KEY (order_id, product_id) -> Option AQuick Check:
Composite key syntax uses parentheses and commas [OK]
- Omitting parentheses around columns
- Using symbols like & or + incorrectly
- Listing columns without commas
OrderDetails with composite primary key (order_id, product_id), what will this query return?SELECT * FROM OrderDetails WHERE order_id = 101;
Solution
Step 1: Understand composite key usage in queries
Composite keys uniquely identify rows, but queries can filter by any column(s).Step 2: Analyze the query filter
The query filters only by order_id = 101, so it returns all rows with that order_id regardless of product_id.Final Answer:
All rows where order_id is 101, regardless of product_id. -> Option DQuick Check:
Filtering by part of composite key returns matching rows [OK]
- Assuming missing key column causes error
- Thinking only full composite key filters work
- Expecting only one row when multiple match
CREATE TABLE Enrollment (
student_id INT,
course_id INT,
PRIMARY KEY student_id, course_id
);
What is the problem?
Solution
Step 1: Check syntax for composite primary key
Composite keys require parentheses around the column list in PRIMARY KEY declaration.Step 2: Identify error in given SQL
The statement uses PRIMARY KEY student_id, course_id without parentheses, causing syntax error.Final Answer:
Missing parentheses around the composite key columns. -> Option AQuick Check:
Composite keys need parentheses [OK]
- Omitting parentheses in PRIMARY KEY clause
- Confusing primary key with unique constraint
- Placing PRIMARY KEY before column definitions
Attendance with columns student_id, class_date, and session. You want to ensure each student can only have one attendance record per class date and session. Which composite primary key should you define?Solution
Step 1: Understand uniqueness requirement
Each student must have only one record per class date and session, so all three columns combined must be unique.Step 2: Choose composite key covering all uniqueness factors
Composite key must include student_id, class_date, and session to enforce this rule.Final Answer:
(student_id, class_date, session) -> Option CQuick Check:
Composite key covers all uniqueness columns [OK]
- Leaving out session or class_date from key
- Using only two columns when three needed
- Confusing foreign keys with primary keys
