Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
ER Diagram to Table Mapping
📖 Scenario: You are designing a simple database for a library. The library keeps track of books and authors. Each book has a title and a publication year. Each author has a name and a country. A book can have one author, and an author can write many books.
🎯 Goal: Create SQL tables that represent the entities and relationships from the ER diagram of the library system.
📋 What You'll Learn
Create a table called Authors with columns AuthorID (primary key), Name, and Country.
Create a table called Books with columns BookID (primary key), Title, PublicationYear, and AuthorID (foreign key referencing Authors.AuthorID).
Use appropriate data types for each column.
Set AuthorID in Books as a foreign key to enforce the relationship.
💡 Why This Matters
🌍 Real World
Database designers often convert ER diagrams into SQL tables to build real databases for applications like libraries, stores, or social networks.
💼 Career
Understanding how to map ER diagrams to tables is essential for roles like database developer, data analyst, and backend engineer.
Progress0 / 4 steps
1
Create the Authors table
Write a SQL statement to create a table called Authors with columns AuthorID as an integer primary key, Name as text, and Country as text.
SQL
Hint
Use CREATE TABLE Authors and define AuthorID as INTEGER PRIMARY KEY.
2
Create the Books table
Write a SQL statement to create a table called Books with columns BookID as an integer primary key, Title as text, PublicationYear as integer, and AuthorID as integer.
SQL
Hint
Define BookID as INTEGER PRIMARY KEY and include AuthorID as an integer column.
3
Add foreign key constraint
Modify the Books table creation to add a foreign key constraint on AuthorID referencing Authors(AuthorID).
SQL
Hint
Use FOREIGN KEY (AuthorID) REFERENCES Authors(AuthorID) inside the Books table definition.
4
Complete the table definitions
Ensure both Authors and Books tables are created with all columns and the foreign key constraint as specified.
SQL
Hint
Check that both tables have all columns and the foreign key constraint.
Practice
(1/5)
1. In an ER diagram, what does an entity typically become when converting to a database schema?
easy
A. A table with columns for each attribute
B. A single column in a table
C. A database index
D. A stored procedure
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 A
Quick Check:
Entity = Table [OK]
Hint: Entities become tables with columns for attributes [OK]
Common Mistakes:
Confusing entities with indexes
Thinking entities become single columns
Assuming entities become procedures
2. Which of the following is the correct way to represent a one-to-many relationship in tables derived from an ER diagram?
easy
A. Create a new table with only primary keys from both tables
B. Add a foreign key column in the 'many' side table referencing the 'one' side
C. Add a foreign key column in the 'one' side table referencing the 'many' side
D. Use a trigger to link the two tables
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 B
Quick Check:
Foreign key on 'many' side = Add a foreign key column in the 'many' side table referencing the 'one' side [OK]
Hint: Foreign key goes in the 'many' side table [OK]
Common Mistakes:
Placing foreign key on the 'one' side
Creating unnecessary tables for one-to-many
Using triggers instead of foreign keys
3. Given two entities 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';
medium
A. Syntax error due to join condition
B. List of all authors and their books
C. List of books with no authors
D. List of all books written by 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 D
Quick Check:
Join + filter by author name = books by Alice [OK]
Hint: JOIN on foreign key filters books by author [OK]
Common Mistakes:
Confusing join condition causing no results
Ignoring WHERE clause filtering
Thinking it lists all authors
4. You have two tables from an ER diagram: 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?
medium
A. ALTER TABLE Enrollment ADD FOREIGN KEY (student_id) REFERENCES Student(id);
B. ALTER TABLE Student ADD FOREIGN KEY (id) REFERENCES Enrollment(student_id);
C. CREATE FOREIGN KEY Enrollment.student_id REFERENCES Student.id;
D. ALTER Enrollment ADD CONSTRAINT FOREIGN KEY student_id Student(id);
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 A
Quick Check:
ALTER TABLE + ADD FOREIGN KEY + REFERENCES [OK]
Hint: Foreign key added on referencing table with ALTER TABLE [OK]
Common Mistakes:
Adding foreign key on referenced table
Wrong ALTER TABLE syntax
Using CREATE FOREIGN KEY instead of ALTER TABLE
5. Consider an ER diagram with entities 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?
hard
A. Add proj_id as a foreign key column in Employee table
B. Add emp_id as a foreign key column in Project table
C. Create a new table WorksOn with columns emp_id and proj_id as foreign keys
D. Merge Employee and Project tables into one
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 C
Quick Check:
Many-to-many = junction table with two foreign keys [OK]
Hint: Many-to-many needs a new table with two foreign keys [OK]