Primary keys and uniqueness in SQL - Time & Space Complexity
When we use primary keys and uniqueness constraints in a database, the system must check if new data breaks these rules.
We want to know how the time to check these rules grows as the data grows.
Analyze the time complexity of inserting a new row with a primary key constraint.
INSERT INTO users (user_id, name) VALUES (123, 'Alice');
-- user_id is the primary key
This code adds a new user, ensuring the user_id is unique as required by the primary key.
What does the database do to keep the primary key unique?
- Primary operation: Searching the index for the user_id to check uniqueness.
- How many times: Once per insert, but the search depends on the number of rows.
As the table grows, the time to check if the user_id exists changes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4-5 steps to find or confirm uniqueness |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: The number of steps grows slowly, not directly with n, but with how deep the index tree is.
Time Complexity: O(log n)
This means checking uniqueness takes a little more time as data grows, but it grows slowly and efficiently.
[X] Wrong: "Checking uniqueness takes the same time no matter how big the table is."
[OK] Correct: The database uses an index, which grows in a balanced way, so checking takes more steps as data grows, but not one step per row.
Understanding how primary keys affect performance shows you know how databases keep data safe and fast, a key skill for real projects.
"What if the primary key was not indexed? How would the time complexity change?"
Practice
PRIMARY KEY in a SQL table?Solution
Step 1: Understand the role of PRIMARY KEY
A PRIMARY KEY uniquely identifies each row in a table and does not allow NULL values to ensure every row is distinct.Step 2: Compare options with PRIMARY KEY properties
It uniquely identifies each row and cannot contain NULL values. correctly states uniqueness and no NULLs. Other options contradict these rules.Final Answer:
It uniquely identifies each row and cannot contain NULL values. -> Option BQuick Check:
PRIMARY KEY = unique + no NULL [OK]
- Thinking primary keys allow NULLs
- Confusing primary key with unique constraint
- Assuming primary key allows duplicates
id as a primary key in a table?Solution
Step 1: Identify correct PRIMARY KEY syntax
The correct syntax to define a primary key inline iscolumn_name data_type PRIMARY KEY.Step 2: Check each option
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); uses correct syntax. CREATE TABLE users (id INT UNIQUE, name VARCHAR(50)); uses UNIQUE, not PRIMARY KEY. CREATE TABLE users (id INT NOT NULL, name VARCHAR(50)); only uses NOT NULL. CREATE TABLE users (id INT, name VARCHAR(50) PRIMARY KEY); incorrectly assigns PRIMARY KEY to the wrong column.Final Answer:
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50)); -> Option CQuick Check:
PRIMARY KEY syntax = column_name data_type PRIMARY KEY [OK]
- Using UNIQUE instead of PRIMARY KEY
- Placing PRIMARY KEY on wrong column
- Omitting PRIMARY KEY keyword
employees with id as PRIMARY KEY, what happens if you run this SQL?INSERT INTO employees (id, name) VALUES (1, 'Alice'); INSERT INTO employees (id, name) VALUES (1, 'Bob');
Solution
Step 1: Understand PRIMARY KEY uniqueness constraint
PRIMARY KEY enforces unique values. The first insert adds id=1 successfully.Step 2: Analyze second insert with duplicate id
The second insert tries to add id=1 again, violating uniqueness, causing an error.Final Answer:
The second insert fails due to duplicate primary key. -> Option AQuick Check:
Duplicate PRIMARY KEY value causes insert failure [OK]
- Assuming duplicates are allowed in primary key
- Thinking second insert overwrites first
- Believing primary keys cannot be integers
CREATE TABLE products ( product_id INT UNIQUE, name VARCHAR(100) );
Which problem will occur if you try to insert two rows with
product_id as NULL?Solution
Step 1: Understand UNIQUE constraint with NULLs
UNIQUE allows only one NULL in some databases, but standard SQL allows multiple NULLs because NULL means unknown.Step 2: Apply to given inserts
In most SQL systems, inserting multiple rows with NULL in a UNIQUE column succeeds because NULLs are not considered equal.Final Answer:
Both rows insert successfully because UNIQUE allows multiple NULLs. -> Option AQuick Check:
UNIQUE allows multiple NULLs in most SQL [OK]
- Assuming UNIQUE disallows multiple NULLs
- Confusing UNIQUE with PRIMARY KEY NULL rules
- Expecting syntax error on NULL in UNIQUE
orders where each order has a unique order_id and a unique tracking_number that can be NULL. Which SQL definition is correct?Solution
Step 1: Define order_id as PRIMARY KEY
order_id must be unique and not NULL, so PRIMARY KEY is appropriate.Step 2: Define tracking_number as UNIQUE and nullable
tracking_number should be unique but can be NULL, so UNIQUE constraint without NOT NULL is correct.Step 3: Evaluate options
CREATE TABLE orders (order_id INT PRIMARY KEY, tracking_number VARCHAR(20) UNIQUE); matches requirements. CREATE TABLE orders (order_id INT UNIQUE, tracking_number VARCHAR(20) PRIMARY KEY); wrongly assigns PRIMARY KEY to tracking_number. CREATE TABLE orders (order_id INT PRIMARY KEY UNIQUE, tracking_number VARCHAR(20) NOT NULL UNIQUE); makes tracking_number NOT NULL, disallowing NULLs. CREATE TABLE orders (order_id INT, tracking_number VARCHAR(20) UNIQUE PRIMARY KEY); has invalid syntax combining UNIQUE and PRIMARY KEY on tracking_number.Final Answer:
CREATE TABLE orders (order_id INT PRIMARY KEY, tracking_number VARCHAR(20) UNIQUE); -> Option DQuick Check:
PRIMARY KEY unique + NOT NULL, UNIQUE allows NULL [OK]
- Making UNIQUE column NOT NULL when NULLs allowed
- Assigning PRIMARY KEY to wrong column
- Combining UNIQUE and PRIMARY KEY on same column incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
