G27
Self-checkedTier 1 9 min readdbms.er-to-relational

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 EiE_i and relationship sets RjR_j:

  1. Strong entity set EE with simple attributes a1,…,ana_1, \ldots, a_n and key kk: a relation E(a1,…,an)E(a_1, \ldots, a_n) with primary key kk. A composite attribute is replaced by its simple components; a derived attribute is not stored.
  2. Weak entity set WW with owner EE and partial key pp: a relation W(key of E, attributes of W)W(\text{key of } E, \text{ attributes of } W) with primary key (key of E,p)(\text{key of } E, p) and a foreign key (key of EE) referencing EE. The identifying relationship needs no relation of its own.
  3. Multivalued attribute mm of EE: a relation E_m(key of E,m)E\_m(\text{key of } E, m) with primary key (key of EE, mm) and a foreign key referencing EE.
  4. Binary one-to-many relationship between AA (the "one" side) and BB (the "many" side): add the key of AA to the relation of BB as a foreign key (together with any attributes of the relationship). No new relation.
  5. 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.
  6. 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.
  7. nn-ary relationship (n≥3n \ge 3): 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).
  8. 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
Student(sid, sname)0 tuples
sidsname
(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.

0 / 6
computed by script: relational operators are evaluated by scripts/db_lib.py (and cross-checked against SQLite), one step at a time. python3 scripts/gen-db1-traces.py

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

number of relations=#entity sets+#multivalued attributes+#M:N (and n-ary) relationship sets\text{number of relations} = \#\text{entity sets} + \#\text{multivalued attributes} + \#\text{M:N (and $n$-ary) relationship sets}

(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 πiid,cid(Course)\pi_{\text{iid}, \text{cid}}(\text{Course}) and Has is πeid,dname(Dependent)\pi_{\text{eid}, \text{dname}}(\text{Dependent})). 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:

mappingrelationswhen it suits
one relation per classPerson(pid, name), Student(pid, gpa), Employee(pid, salary), with pid in each subclass a primary key and a foreign key to Personalways valid; the full tuple of a student needs a join
subclass relations only, with all attributesStudent(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 hierarchyPerson(pid, name, gpa, salary, type) with NULLsfew 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

Easy

An ER diagram has 44 entity sets (no weak ones), one multivalued attribute, two M:N relationship sets and three 1:N relationship sets. Tables: 4+1+2=74 + 1 + 2 = 7. The three 1:N relationships only add foreign keys.

EX 2Key of the relation of a relationship set

Easy

Enrolls 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

Medium

Person (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

Medium

Employee(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:" M+NM + N. (b) "An ER diagram with entity sets E1E_1, E2E_2 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 nn-ary relationship sets.
  • Specialisation: relation per class (always valid), subclass relations only (disjoint, total), single relation with NULLs.

Traps and GATE patterns

Connections

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

GATE 2017 · Set 2 · MCQ · 1 markQ5 · page 384 of the book
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
GATE 2015 · Set 2 · NAT · 2 marksQ6 · page 385 of the book
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________________.
GATE 2008 · MCQQ8 · page 385 of the book
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
GATE 2008 · MCQ · 2 marksQ9 · page 385 of the book
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
GATE 2005 · MCQ · 2 marksQ10 · page 385 of the book
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 33 entity sets, one M:N relationship set, one 1:N relationship set and one multivalued attribute?

Show answer

3+1+1=53 + 1 + 1 = 5.

Question 2

Where is the foreign key of a one-to-many relationship placed?

Show answer

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 answer

The owner's key together with the partial key.

Question 4

Why does a many-to-many relationship need its own relation?

Show answer

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)