Converting ER diagrams to relational schema in DBMS Theory - Time & Space Complexity
When converting an ER diagram to a relational schema, we want to understand how the work grows as the diagram gets bigger.
We ask: How does the time to create tables and relationships change with more entities and connections?
Analyze the time complexity of the following process.
-- For each entity in the ER diagram
FOR EACH entity IN ER_diagram.entities LOOP
CREATE TABLE for entity;
FOR EACH attribute IN entity.attributes LOOP
ADD attribute as column to table;
END LOOP;
END LOOP;
-- For each relationship in the ER diagram
FOR EACH relationship IN ER_diagram.relationships LOOP
CREATE foreign keys or join tables as needed;
END LOOP;
This process creates tables for entities and sets up relationships by adding keys or join tables.
Look at what repeats in this process.
- Primary operation: Looping over entities and their attributes, then looping over relationships.
- How many times: Once for each entity and its attributes, and once for each relationship.
As the number of entities and relationships grows, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 entities, 15 relationships | About 10 loops for entities plus their attributes, plus 15 loops for relationships |
| 100 entities, 150 relationships | About 100 loops for entities plus their attributes, plus 150 loops for relationships |
| 1000 entities, 1500 relationships | About 1000 loops for entities plus their attributes, plus 1500 loops for relationships |
Pattern observation: The total work grows roughly in direct proportion to the number of entities and relationships.
Time Complexity: O(n + m + a)
This means the time to convert grows linearly with the number of entities (n), relationships (m), and attributes (a).
[X] Wrong: "The time grows much faster because each relationship connects multiple entities, so it's exponential."
[OK] Correct: Each relationship is handled once, so the work adds up linearly, not exponentially.
Understanding how the conversion scales helps you explain database design steps clearly and shows you can think about efficiency in real projects.
"What if we had to handle many-to-many relationships by creating extra join tables? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand one-to-many relationship
In a one-to-many relationship, one entity instance relates to many instances of another entity.Step 2: Represent relationship in relational schema
The foreign key is placed in the table representing the 'many' side, pointing to the 'one' side's primary key.Final Answer:
Add a foreign key in the table on the 'many' side referencing the 'one' side -> Option AQuick Check:
One-to-many = foreign key on many side [OK]
- Placing foreign key on the 'one' side
- Creating unnecessary new tables for one-to-many
- Merging entities incorrectly
Solution
Step 1: Understand many-to-many relationship
Many-to-many means multiple instances of one entity relate to multiple instances of another.Step 2: Represent many-to-many in relational schema
Create a new table (junction table) with foreign keys referencing the primary keys of both entities.Final Answer:
Create a new table with foreign keys referencing both entities -> Option CQuick Check:
Many-to-many = new junction table [OK]
- Adding foreign keys directly in entity tables
- Merging entities incorrectly
- Ignoring the need for a junction table
Student(SID, Name) and Course(CID, Title) connected by a many-to-many relationship Enrolls, which relational schema correctly represents this?Solution
Step 1: Identify entities and relationship type
Student and Course are entities; Enrolls is many-to-many between them.Step 2: Convert entities and relationship to tables
Student and Course become tables with their attributes. Enrolls becomes a new table with foreign keys SID and CID.Final Answer:
Student(SID, Name), Course(CID, Title), Enrolls(SID, CID) -> Option BQuick Check:
Many-to-many needs separate table [OK]
- Adding foreign key in Course for Student
- Adding foreign key in Student for Course
- Omitting the Enrolls table
Person and Passport by adding a foreign key PersonID in Passport. However, the database allows multiple passports for one person. What is the likely mistake?Solution
Step 1: Understand one-to-one relationship enforcement
One-to-one means each person has at most one passport and vice versa.Step 2: Check foreign key constraints
Foreign key alone allows many passports per person unless a UNIQUE constraint is added to enforce one-to-one.Final Answer:
Foreign key in Passport lacks a UNIQUE constraint -> Option AQuick Check:
One-to-one needs UNIQUE foreign key [OK]
- Assuming foreign key alone enforces one-to-one
- Placing foreign key in wrong table
- Merging tables unnecessarily
Author(AID, Name), Book(BID, Title), and a many-to-many relationship Writes with an attribute RoyaltyPercentage. How should you convert this to a relational schema?Solution
Step 1: Identify entities and relationship with attribute
Author and Book are entities; Writes is many-to-many with attribute RoyaltyPercentage.Step 2: Convert entities and relationship with attribute
Entities become tables with their attributes. The many-to-many relationship becomes a new table including foreign keys and the attribute RoyaltyPercentage.Final Answer:
Create Author(AID, Name), Book(BID, Title), Writes(AID, BID, RoyaltyPercentage) -> Option DQuick Check:
Relationship attributes go in junction table [OK]
- Adding relationship attribute to entity tables
- Omitting the junction table
- Placing attribute in wrong table
Ready to prove it?
Pass the test and this pattern moves unknown
