ER diagram to table mapping in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we convert an ER diagram into tables, we want to know how the time to create or query these tables changes as the data grows.
We ask: How does the work increase when we add more entities or relationships?
Analyze the time complexity of creating tables from an ER diagram with entities and relationships.
-- Create table for Entity: Student
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
Name VARCHAR(100)
);
-- Create table for Entity: Course
CREATE TABLE Course (
CourseID INT PRIMARY KEY,
Title VARCHAR(100)
);
-- Create table for Relationship: Enrollment
CREATE TABLE Enrollment (
StudentID INT,
CourseID INT,
PRIMARY KEY (StudentID, CourseID),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
);
This code creates tables for two entities and a many-to-many relationship between them.
Look for repeated actions that affect time.
- Primary operation: Inserting or querying rows in tables representing entities and relationships.
- How many times: Once per row added; the relationship table grows with pairs of related entities.
As you add more students and courses, the number of rows in each table grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 students, 10 courses | ~10 inserts in Student, 10 in Course, up to 100 in Enrollment |
| 100 students, 100 courses | ~100 inserts in Student, 100 in Course, up to 10,000 in Enrollment |
| 1000 students, 1000 courses | ~1000 inserts in Student, 1000 in Course, up to 1,000,000 in Enrollment |
Pattern observation: The relationship table can grow much faster, roughly multiplying the sizes of the two entity tables.
Time Complexity: O(n * m)
This means the work to handle the relationship table grows with the product of the sizes of the two entity tables.
[X] Wrong: "The relationship table grows linearly with the number of entities."
[OK] Correct: The relationship table can grow much faster because it stores pairs, so its size depends on the combination of entities, not just one side.
Understanding how tables grow from ER diagrams helps you design efficient databases and predict query performance in real projects.
"What if the relationship was one-to-many instead of many-to-many? How would the time complexity change?"
Practice
Solution
Step 1: Understand what an entity represents
An entity in an ER diagram represents a real-world object or concept with attributes.Step 2: Map entity to database structure
Each entity is converted into a table, where each attribute becomes a column in that table.Final Answer:
A table with columns for each attribute -> Option AQuick Check:
Entity = Table [OK]
- Confusing entities with indexes
- Thinking entities become single columns
- Assuming entities become procedures
Solution
Step 1: Understand one-to-many relationship
One-to-many means one record in the first table relates to many records in the second table.Step 2: Map relationship to tables
The 'many' side table gets a foreign key column referencing the 'one' side table's primary key.Final Answer:
Add a foreign key column in the 'many' side table referencing the 'one' side -> Option BQuick Check:
Foreign key on 'many' side = Add a foreign key column in the 'many' side table referencing the 'one' side [OK]
- Placing foreign key on the 'one' side
- Creating unnecessary tables for one-to-many
- Using triggers instead of foreign keys
Author(id, name) and Book(id, title, author_id) with a one-to-many relationship from Author to Book, what will be the result of this SQL query?SELECT a.name, b.title FROM Author a JOIN Book b ON a.id = b.author_id WHERE a.name = 'Alice';Solution
Step 1: Analyze the JOIN condition
The query joins Author and Book on matching author IDs, linking books to their authors.Step 2: Apply the WHERE filter
It filters authors with name 'Alice', so only books by Alice are selected.Final Answer:
List of all books written by Alice -> Option DQuick Check:
Join + filter by author name = books by Alice [OK]
- Confusing join condition causing no results
- Ignoring WHERE clause filtering
- Thinking it lists all authors
Student(id, name) and Enrollment(student_id, course_id). You want to add a foreign key constraint to Enrollment.student_id. Which SQL statement is correct?Solution
Step 1: Identify the correct syntax for adding foreign key
The standard syntax is ALTER TABLE [table] ADD FOREIGN KEY (column) REFERENCES [other_table](column).Step 2: Apply to given tables
Enrollment.student_id references Student.id, so the statement must alter Enrollment table.Final Answer:
ALTER TABLE Enrollment ADD FOREIGN KEY (student_id) REFERENCES Student(id); -> Option AQuick Check:
ALTER TABLE + ADD FOREIGN KEY + REFERENCES [OK]
- Adding foreign key on referenced table
- Wrong ALTER TABLE syntax
- Using CREATE FOREIGN KEY instead of ALTER TABLE
Employee(emp_id, name), Project(proj_id, title), and a many-to-many relationship WorksOn between them. How should you map this relationship into tables?Solution
Step 1: Understand many-to-many relationships
Many-to-many means multiple employees can work on multiple projects and vice versa.Step 2: Map many-to-many to tables
This requires a new table (junction table) that holds foreign keys from both Employee and Project tables.Final Answer:
Create a new table WorksOn with columns emp_id and proj_id as foreign keys -> Option CQuick Check:
Many-to-many = junction table with two foreign keys [OK]
- Adding foreign key to only one table
- Merging unrelated tables
- Ignoring the need for a junction table
