PRIMARY KEY constraint in SQL - Time & Space Complexity
When we use a PRIMARY KEY constraint in a database table, it helps keep data unique and easy to find.
We want to understand how the time to check or find a primary key changes as the table grows.
Analyze the time complexity of inserting and searching rows with a PRIMARY KEY constraint.
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100),
Department VARCHAR(50)
);
INSERT INTO Employees (EmployeeID, Name, Department) VALUES (1, 'Alice', 'HR');
SELECT * FROM Employees WHERE EmployeeID = 1;
This code creates a table with a PRIMARY KEY on EmployeeID, inserts a row, and searches by that key.
When inserting or searching, the database checks the PRIMARY KEY to keep values unique and find rows fast.
- Primary operation: Searching or checking the PRIMARY KEY index.
- How many times: Depends on the number of rows, but the index helps avoid scanning all rows.
As the table grows, the database uses the PRIMARY KEY index to quickly find or check keys without looking at every row.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3-4 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: The steps grow slowly even as the table gets much bigger, thanks to the index.
Time Complexity: O(log n)
This means finding or checking a primary key takes a small number of steps that grow slowly as the table gets bigger.
[X] Wrong: "Searching by PRIMARY KEY scans every row, so it takes longer as the table grows linearly."
[OK] Correct: The PRIMARY KEY uses an index (like a tree) that lets the database jump quickly to the right spot without checking every row.
Understanding how PRIMARY KEY constraints speed up data access shows you know how databases keep things efficient as data grows.
"What if the PRIMARY KEY was not indexed? How would the time complexity for searching change?"
Practice
PRIMARY KEY in a database table?Solution
Step 1: Understand the role of PRIMARY KEY
A PRIMARY KEY uniquely identifies each row and does not allow nulls, ensuring data integrity.Step 2: Compare options with PRIMARY KEY purpose
Options A, B, and C do not describe uniqueness or null restriction, which are key features of PRIMARY KEY.Final Answer:
To uniquely identify each row and prevent null values -> Option BQuick Check:
PRIMARY KEY = unique + not null [OK]
- Thinking PRIMARY KEY allows duplicates
- Confusing PRIMARY KEY with foreign key
- Assuming PRIMARY KEY stores large text
id column during table creation?Solution
Step 1: Review correct PRIMARY KEY syntax inline
PRIMARY KEY is declared after the column type, likeid INT PRIMARY KEY.Step 2: Check each option's syntax
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); correctly places PRIMARY KEY afterid INT. Options B, C, and D have syntax errors or misplaced keywords.Final Answer:
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); -> Option AQuick Check:
PRIMARY KEY follows column type inline [OK]
- Placing PRIMARY KEY after wrong column
- Using PRIMARY KEY without parentheses for multiple columns
- Syntax errors with misplaced keywords
CREATE TABLE orders (
order_id INT,
product_id INT,
PRIMARY KEY (order_id, product_id)
);
What will happen if you try to insert two rows with the same
order_id but different product_id?Solution
Step 1: Understand composite PRIMARY KEY behavior
The PRIMARY KEY is on bothorder_idandproduct_idtogether, so uniqueness is checked on the pair.Step 2: Analyze insertion of rows with same order_id but different product_id
Sinceproduct_iddiffers, the pair is unique, so both rows can be inserted without error.Final Answer:
Both rows will be inserted successfully -> Option DQuick Check:
Composite PRIMARY KEY = unique pairs [OK]
- Assuming order_id alone must be unique
- Thinking composite key allows duplicates in all columns
- Confusing syntax error with constraint violation
CREATE TABLE employees (
emp_id INT,
email VARCHAR(100),
PRIMARY KEY emp_id
);
What is wrong with this statement?
Solution
Step 1: Check syntax for table-level PRIMARY KEY
When declaring PRIMARY KEY separately, column names must be inside parentheses, likePRIMARY KEY (emp_id).Step 2: Identify error in given statement
The statement misses parentheses aroundemp_id, causing syntax error.Final Answer:
PRIMARY KEY must be declared with parentheses around column name -> Option AQuick Check:
Table-level PRIMARY KEY needs parentheses [OK]
- Omitting parentheses in table-level PRIMARY KEY
- Thinking PRIMARY KEY can't be on emp_id
- Confusing column-level and table-level syntax
students where each student is uniquely identified by a combination of student_id and campus_id. Which of the following statements correctly enforces this using a PRIMARY KEY?Solution
Step 1: Understand composite PRIMARY KEY requirement
To uniquely identify rows by two columns, a composite PRIMARY KEY on both columns is needed.Step 2: Evaluate each option's correctness
CREATE TABLE students (student_id INT, campus_id INT, PRIMARY KEY (student_id, campus_id)); correctly defines a composite PRIMARY KEY. CREATE TABLE students (student_id INT PRIMARY KEY, campus_id INT PRIMARY KEY); tries to assign two PRIMARY KEYs, which is invalid. CREATE TABLE students (student_id INT, campus_id INT, PRIMARY KEY student_id, campus_id); has syntax errors. CREATE TABLE students (student_id INT, campus_id INT, UNIQUE (student_id, campus_id)); uses UNIQUE but not PRIMARY KEY.Final Answer:
CREATE TABLE students (student_id INT, campus_id INT, PRIMARY KEY (student_id, campus_id)); -> Option CQuick Check:
Composite PRIMARY KEY uses parentheses around columns [OK]
- Trying multiple PRIMARY KEYs on separate columns
- Missing parentheses in PRIMARY KEY declaration
- Using UNIQUE instead of PRIMARY KEY for uniqueness
Ready to prove it?
Pass the test and this pattern moves unknown
