ER to relational mapping
Turning an ER diagram into relation schemas: strong and weak entity sets, multivalued attributes, one-to-many relationships by a foreign key, many-to-many relationships by a relation of their own, one-to-one relationships, specialisation hierarchies in three ways, and counting the tables (with the minimum number of tables when relations can be merged), checked by loading random ER instances into a real database.
Why it exists
An ER diagram is for people; a database stores relations. A fixed set of rules turns one into the other, so a question "how many tables does this diagram give" has a definite answer. GATE asks it constantly: count the relations, name the keys, say where the foreign key goes.
Prerequisites
You need the ER model with its keys and cardinalities (ER model) and the relational vocabulary of keys and foreign keys (relational model and keys).
Precise definitions
The mapping works element by element. For an ER schema with entity sets and relationship sets :
- Strong entity set with simple attributes and key : a relation with primary key . A composite attribute is replaced by its simple components; a derived attribute is not stored.
- Weak entity set with owner and partial key : a relation with primary key and a foreign key (key of ) referencing . The identifying relationship needs no relation of its own.
- Multivalued attribute of : a relation with primary key (key of , ) and a foreign key referencing .
- Binary one-to-many relationship between (the "one" side) and (the "many" side): add the key of to the relation of as a foreign key (together with any attributes of the relationship). No new relation.
- Binary many-to-many relationship: a new relation containing the keys of both participants (each a foreign key) and the attributes of the relationship; its primary key is the union of the two keys.
- Binary one-to-one relationship: add the key of one side to the other as a foreign key (together with the relationship's attributes). Prefer the side with total participation, so that the foreign key is never NULL; if both sides are total the two entity sets may even be merged into one relation.
- -ary relationship (): a relation containing the keys of all participants; the primary key is the union of the keys of the "many" participants (all of them if all are many).
- Specialisation: three mappings (below).
How it works
Worked mapping
Take the university diagram of the previous page extended with a weak entity set Dependent of Employee (ER model). Applying the rules one by one:
From the ER diagram to relations: start
Computed, not typed| sid | sname |
|---|---|
| (empty) | |
primary key: sid
Step 0
Each strong entity set becomes a relation whose attributes are its simple attributes and whose primary key is its key.
Result: seven relations. The numbers behind it: five entity sets (Student, Course, Instructor, Employee, and the weak Dependent) give five relations; the multivalued attribute phone gives one more; the many-to-many relationship Enrolls gives one more; the one-to-many Teaches and the identifying Has give none (they became the foreign keys iid in Course and eid in Dependent). So
(counting the weak entity sets among the entity sets and not counting the 1:N and 1:1 relationships, nor identifying relationships).
The mapping was implemented and tested: the schema it produced was loaded into a real database (SQLite) with the PRIMARY KEY and FOREIGN KEY clauses enforced; 100 random ER instances were converted to rows and loaded without a constraint error, and each relationship set was recovered by a query (for example Teaches is and Has is ). A duplicate key or a dangling foreign key was refused.
Why the foreign key goes to the "many" side
In a one-to-many relationship each course has at most one instructor, so the instructor can be stored in the course tuple as one extra value. The other way round (a list of courses inside the instructor tuple) would violate atomicity. Placing a foreign key on the "one" side is not possible without repeating the instructor's tuple for each course.
For a many-to-many relationship there is no tuple in which a single value can be stored, hence the extra relation.
Minimum number of relations
The standard answers above give the number of relations of the straightforward mapping. If a question asks for the minimum number of relations, merge where it loses nothing:
- a one-to-one relationship with total participation of both entity sets: the two entity sets can be merged into a single relation;
- a one-to-many relationship does not reduce the count (the foreign key already adds no relation);
Merging entities that are not in total one-to-one relationship would force NULLs.
Mapping a specialisation
Person(pid, name) with subclasses Student(gpa) and Employee(salary) (ER model). Three mappings:
| mapping | relations | when it suits |
|---|---|---|
| one relation per class | Person(pid, name), Student(pid, gpa), Employee(pid, salary), with pid in each subclass a primary key and a foreign key to Person | always valid; the full tuple of a student needs a join |
| subclass relations only, with all attributes | Student(pid, name, gpa), Employee(pid, name, salary) | disjoint and total specialisation only (a person who is in no subclass would have no place); overlapping entities are stored twice |
| one relation for the whole hierarchy | Person(pid, name, gpa, salary, type) with NULLs | few subclass attributes; disjoint (a type attribute) or overlapping (flags) |
The first mapping gives three relations for three entity sets; it is the answer the counting rule above produces when each subclass is counted as an entity set.
Roles and recursive relationships
For a recursive relationship such as Supervises on Employee (one supervisor, many supervisees): the relation Employee gets a foreign key referencing itself, with a new name (supervisor_eid). For a recursive many-to-many relationship (a course requires other courses) a relation with two copies of the key under different role names.
Worked examples
EX 1Count the tables
EasyAn ER diagram has entity sets (no weak ones), one multivalued attribute, two M:N relationship sets and three 1:N relationship sets. Tables: . The three 1:N relationships only add foreign keys.
EX 2Key of the relation of a relationship set
EasyEnrolls between Student(sid) and Course(cid) with attribute grade, M:N: relation Enrolls(sid, cid, grade), primary key (sid, cid), foreign keys sid → Student, cid → Course.
EX 3Where does the foreign key of a one-to-one relationship go
MediumPerson (pid) and Passport (number), each person has at most one passport, each passport belongs to exactly one person. Passport has total participation, so put the foreign key in Passport: Passport(number, issued, pid) with pid NOT NULL and UNIQUE (and a foreign key to Person). Putting it in Person would force NULLs for people without a passport.
EX 4Weak entity
MediumEmployee(eid, ename) and weak Dependent with partial key dname and attribute age: Dependent(eid, dname, age), primary key (eid, dname), foreign key eid → Employee. Two employees can both have a dependent named Mina because the keys (1, Mina) and (2, Mina) differ.
EX 5Typical questions
GATE level(a) "The minimum number of tables needed to represent M entity sets, N relationships (M:N) and no others:" . (b) "An ER diagram with entity sets , and a one-to-many relationship between them gives:" two tables, with the foreign key in the relation of the "many" entity set. (c) "A relationship set that is many-to-many requires:" a separate relation. (d) "A weak entity set requires in its relation:" the key of the owner. (e) "Total participation on both sides of a 1:1 relationship allows:" merging into one relation. (f) "Multivalued attributes are stored:" in a separate relation.
Cheat box
Cheat box
- Entity set → relation (key = key). Weak entity: owner key + partial key.
- Multivalued attribute → relation (entity key, value).
- 1:N → foreign key on the N side. M:N → relation with both keys + attributes. 1:1 → foreign key on the total side (or merge).
- Number of relations = entity sets + multivalued attributes + M:N and -ary relationship sets.
- Specialisation: relation per class (always valid), subclass relations only (disjoint, total), single relation with NULLs.
Traps and GATE patterns
Connections
Builds on
Across subjects
No cross-subject links yet.
The relations produced here are the input of normalisation, which checks whether the design has redundancy (normal forms); they are created in SQL by CREATE TABLE (SQL basics).
PYQs
5 previous year questions from your book. I re-solved each one myself (4 by running or simulating, 1 by reasoning only); the book's key is shown next to my answer. All PYQs
An ER model of a database consists of entity types A and B. These are connected by a relationship R which does not have its own attribute. Under which one of the following conditions, can the relational table for R be merged with that of A?
- (a)Relationship R is one-to-many and the participation of A in R is total
- (b)Relationship R is one-to-many and the participation of A in R is partial
- (c)Relationship R is many-to-one and the participation of A in R is total
- (d)Relationship R is many-to-one and the participation of A in R is partial DATABASE MANAGEMENT SYSTEM
Consider an Entity-Relationship (ER) model in which entity sets E1 and E2 are connected by an m:n relationship R12. E1 and E3 are connected by a 1:n (1 on the side of E1 and n on the side of E3) relationship R13. E1 has two-singled attributes a11 and a12 of which a11 is the key attribute. E2 has two singled-valued attributes a21 and a22 of which a21 is the key attribute. E3 has two single-valued attributes a31 and a32 of which a31 is the key attribute. The relationships do not have any attributes. If a relational model is derived from the above ER model, then the minimum number of relations that would be generated if all relation are in 3NF is________________.
Consider the following ER diagram Which of the following is a correct attribute set for one of the tables for the minimum number of tables needed to represent M, N, P, R1, R2 ?
- (a)M1, M2, M3, P1
- (b)M1, P1, N1, N2
- (c)M1, P1, N1
- (d)M1, P1
Consider the following ER diagram The minimum number of tables needed to represent M, N, P, R1, R2 is
- (a)2
- (b)3
- (c)4
- (d)5
Let E1 and E2 be two entities in an E/R diagram with simple-valued attributes. R1 and R2 are two relationships between E1 and E2, where R1 is one-to-many and R2 is many-to-many. R1 and R2 do not have any attributes of their own. What is the minimum number of tables required to represent this situation in the relational model?
- (a)2
- (b)3
- (c)4
- (d)5
Self-check
Question 1
How many relations does a diagram give with entity sets, one M:N relationship set, one 1:N relationship set and one multivalued attribute?
Show answerHide
.
Question 2
Where is the foreign key of a one-to-many relationship placed?
Show answerHide
In the relation of the "many" side, referencing the key of the "one" side.
Question 3
What is the primary key of the relation of a weak entity set?
Show answerHide
The owner's key together with the partial key.
Question 4
Why does a many-to-many relationship need its own relation?
Show answerHide
No single entity tuple can hold a set of related keys without breaking atomicity, so each related pair becomes a tuple of a new relation.
Revision card
One-screen revision
- Entity → relation; weak: owner key + partial key; multivalued → own relation.
- 1:N → FK on the N side; M:N → new relation; 1:1 → FK on the total side.
- Tables = entities + multivalued attributes + M:N relationships.
- ISA: relation per class, subclasses only, or one relation with NULLs.
References
- ·Silberschatz, Korth, Sudarshan, Database System Concepts (7th edition), Section 6.6 (reduction to relational schemas)
- ·Elmasri, Navathe, Fundamentals of Database Systems (7th edition), Chapter 9 (relational database design by ER-to-relational mapping)
Verification
scripts/enum-checks/db_algebra.py: the mapping implemented in scripts/db_lib.py er_to_relations turns the example ER schema (strong entities Student, Course, Instructor, Employee, weak entity Dependent, multivalued phone, many-to-many Enrolls with attribute grade, one-to-many Teaches with total participation of Course, identifying relationship Has) into exactly 7 relations with the keys shown; the generated CREATE TABLE statements (PRIMARY KEY and FOREIGN KEY) were executed on SQLite with foreign keys enforced; 100 random ER instances were converted to rows, loaded, and every relationship set was reconstructed by queries (Teaches from Course.iid, Has from Dependent.eid, Enrolls from its relation, phones from Student_phone); a duplicate key, a dangling foreign key in the many-to-many relation, in the weak entity and in Course were refused. The three mappings of a specialisation hierarchy and the count of the minimum number of tables are the standard textbook rules, quoted. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and re-derived against the textbook definitions and the sample database. No defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)