One-to-one relationship design in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with one-to-one relationships in databases, it's important to understand how the time to get data grows as the data grows.
We want to know how the cost of joining two tables changes when the number of rows increases.
Analyze the time complexity of the following SQL query joining two tables with a one-to-one relationship.
SELECT a.id, a.name, b.details
FROM TableA a
JOIN TableB b ON a.id = b.a_id
WHERE a.status = 'active';
This query fetches active records from TableA and their matching details from TableB using a one-to-one link.
Look for repeated actions that affect performance.
- Primary operation: Scanning and matching rows from TableA and TableB.
- How many times: Once for each row in TableA that meets the condition.
As the number of active rows in TableA grows, the work to find matching rows in TableB grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 row matches |
| 100 | About 100 row matches |
| 1000 | About 1000 row matches |
Pattern observation: The number of operations grows roughly in direct proportion to the number of rows.
Time Complexity: O(n)
This means the time to run the query grows linearly with the number of rows in TableA.
[X] Wrong: "Joining two tables always doubles the work, so it's O(n²)."
[OK] Correct: Because each row in TableA matches exactly one row in TableB, the join only processes each row once, so the work grows linearly, not squared.
Understanding how joins behave with one-to-one relationships helps you explain query performance clearly and confidently.
"What if TableB had multiple matching rows per TableA row (one-to-many)? How would the time complexity change?"
Practice
one-to-one relationship in database design?Solution
Step 1: Understand one-to-one relationship meaning
A one-to-one relationship means each record in one table corresponds to exactly one record in another table.Step 2: Compare options to definition
Each row in one table matches exactly one row in another table matches this definition perfectly, while others describe different relationships or no relationship.Final Answer:
Each row in one table matches exactly one row in another table -> Option AQuick Check:
One-to-one = single matching row [OK]
- Confusing one-to-one with one-to-many
- Thinking tables have no relation
- Assuming one table holds all data
Solution
Step 1: Identify constraint enforcing uniqueness
To ensure one-to-one, the foreign key must be unique so no duplicates link to the same row.Step 2: Match constraints to this need
UNIQUE constraint on the foreign key column enforces this, while FOREIGN KEY alone does not guarantee uniqueness.Final Answer:
UNIQUE constraint on the foreign key column -> Option CQuick Check:
Unique foreign key = one-to-one [OK]
- Using FOREIGN KEY without UNIQUE allows many-to-one
- Confusing CHECK with uniqueness
- Assuming NOT NULL enforces one-to-one
CREATE TABLE Person ( PersonID INT PRIMARY KEY, Name VARCHAR(50) ); CREATE TABLE Passport ( PassportID INT PRIMARY KEY, PersonID INT UNIQUE, Number VARCHAR(20), FOREIGN KEY (PersonID) REFERENCES Person(PersonID) );
What does the
UNIQUE constraint on PersonID in Passport ensure?Solution
Step 1: Understand UNIQUE on PersonID in Passport
The UNIQUE constraint means no two rows in Passport can have the same PersonID, so one person links to at most one passport.Step 2: Analyze relationship enforced
Since Passport has a foreign key to Person and PersonID is unique, each passport belongs to one person, and each person can have only one passport.Final Answer:
Each passport belongs to exactly one person, and each person has at most one passport -> Option BQuick Check:
Unique foreign key = one-to-one link [OK]
- Thinking one person can have many passports
- Ignoring UNIQUE constraint effect
- Assuming null allowed without checking
CREATE TABLE Employee ( EmployeeID INT PRIMARY KEY, Name VARCHAR(50) ); CREATE TABLE EmployeeDetails ( DetailID INT PRIMARY KEY, EmployeeID INT, Address VARCHAR(100), FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID) );
What is missing to enforce a one-to-one relationship between
Employee and EmployeeDetails?Solution
Step 1: Identify current constraints
EmployeeDetails has a foreign key to Employee but no uniqueness on EmployeeID, so multiple details can link to one employee.Step 2: Determine what enforces one-to-one
Adding UNIQUE on EmployeeID ensures each employee links to at most one detail, enforcing one-to-one.Final Answer:
Add UNIQUE constraint on EmployeeID in EmployeeDetails -> Option DQuick Check:
Unique foreign key needed for one-to-one [OK]
- Assuming NOT NULL enforces one-to-one
- Removing foreign key breaks relationship
- Confusing primary key with foreign key uniqueness
User and UserProfile. Each user has exactly one profile. Which design best enforces this one-to-one relationship?Solution
Step 1: Understand one-to-one enforcement methods
One way is to share the same primary key in both tables, ensuring exactly one matching row.Step 2: Compare options
User and UserProfile share the same primary key column uses the same primary key in both tables, which is a strong one-to-one design. UserProfile has a foreign key to User with UNIQUE constraint on that foreign key is valid but less strict. Options B and C do not enforce one-to-one properly.Final Answer:
User and UserProfile share the same primary key column -> Option AQuick Check:
Shared primary key = strict one-to-one [OK]
- Ignoring uniqueness on foreign key
- Assuming foreign key alone enforces one-to-one
- Not linking tables properly
