Keys (primary, candidate, foreign, super) in DBMS Theory - Time & Space Complexity
When working with database keys, it's important to understand how the time to check or enforce these keys grows as the data grows.
We want to know how the cost of operations like searching or validating keys changes with more records.
Analyze the time complexity of checking a primary key constraint during data insertion.
-- Assume a table with a primary key column
INSERT INTO Employees (EmployeeID, Name) VALUES (123, 'Alice');
-- The DBMS checks if EmployeeID 123 already exists
SELECT EmployeeID FROM Employees WHERE EmployeeID = 123;
-- If exists, reject insert; else, insert the new record
This snippet shows the DBMS verifying uniqueness of a primary key before inserting a new record.
Look at what the DBMS does repeatedly when inserting data.
- Primary operation: Searching the existing records for the key value.
- How many times: Once per insert, but the search scans the data or index.
As the number of records grows, the search to check key uniqueness takes more time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: Without an index, the search grows linearly with the number of records.
Time Complexity: O(n)
This means the time to check key uniqueness grows directly with the number of records in the table.
[X] Wrong: "Checking a primary key is always instant no matter how big the table is."
[OK] Correct: Without an index, the database must scan records one by one, so more data means more time.
Understanding how key checks scale helps you explain database performance and design better data models.
What if the primary key column has an index? How would the time complexity of checking uniqueness change?
Practice
Solution
Step 1: Understand the role of Primary Key
The primary key uniquely identifies each record and must not be NULL.Step 2: Compare with other keys
Foreign keys link tables, candidate keys are possible unique keys, super keys may have extra columns.Final Answer:
Primary Key -> Option AQuick Check:
Unique and not NULL = Primary Key [OK]
- Confusing foreign key with primary key
- Thinking candidate key is always primary
- Assuming super key must be minimal
id in SQL?Solution
Step 1: Recall SQL syntax for primary key
Primary key is declared usingPRIMARY KEYafter the column definition.Step 2: Check other options
Foreign key requires reference clause, candidate and super key are not SQL keywords.Final Answer:
CREATE TABLE t (id INT PRIMARY KEY); -> Option DQuick Check:
Primary key syntax = column INT PRIMARY KEY [OK]
- Using FOREIGN KEY without REFERENCES
- Trying to declare candidate or super key directly
- Missing PRIMARY KEY keyword
Orders with columns OrderID, CustomerID, and ProductID. If OrderID is the primary key and CustomerID is a foreign key referencing Customers table, what does this imply?Solution
Step 1: Understand primary key role
OrderID uniquely identifies each order record.Step 2: Understand foreign key role
CustomerID links Orders to Customers table to maintain data consistency.Final Answer:
OrderID uniquely identifies each order; CustomerID links to Customers table -> Option AQuick Check:
Primary key = unique ID, foreign key = link [OK]
- Mixing primary and foreign key roles
- Assuming foreign key is unique
- Confusing candidate key with foreign key
EmpID, Email, and Phone. The EmpID is set as primary key, but duplicate emails exist. What is the problem and how to fix it?Solution
Step 1: Identify uniqueness requirements
Primary key ensures EmpID is unique, but Email duplicates violate candidate key uniqueness if Email should be unique.Step 2: Fix by adding UNIQUE constraint
To prevent duplicate emails, add UNIQUE constraint on Email column.Final Answer:
Duplicate emails violate candidate key uniqueness; add UNIQUE constraint on Email -> Option BQuick Check:
Unique candidate keys need UNIQUE constraint [OK]
- Thinking primary key allows duplicates
- Changing primary key without reason
- Ignoring uniqueness of candidate keys
Students with columns StudentID, Email, and PhoneNumber, which of the following sets can be a super key?Solution
Step 1: Define super key
A super key is any set of columns that uniquely identifies a record, even if it has extra columns.Step 2: Analyze each option
StudentID alone uniquely identifies records; Email and PhoneNumber together can also uniquely identify; adding Email to StudentID still uniquely identifies (extra columns allowed).Final Answer:
All of the above -> Option CQuick Check:
Super key = unique set, extra columns allowed [OK]
- Thinking super key must be minimal
- Confusing super key with candidate key
- Assuming single column only
Ready to prove it?
Pass the test and this pattern moves unknown
