Module 10 · 15 min read · Free chapter
ER model and relational model
The ER model draws entities, attributes and relationships, with cardinality (1:1, 1:N, M:N) and participation. The relational model stores them as tables of tuples. A candidate key is a minimal superkey, and one key of k attributes in n gives 2ⁿ⁻ᵏ superkeys. An M:N relationship needs its own table.
Start from zero: from a register to a database
Think of a railway reservation counter before computers. The clerk keeps a thick register, and every booking line has the passenger's name, age, address, train number, train name, date and berth. If Ramesh books five journeys, his address is written five times, and the train name is written again for every passenger on that train.
Now suppose Ramesh moves house. The clerk must find and correct all five lines, and if he misses one, the register now says two different things about the same man. Two clerks writing in the same register at the same moment can also give one berth to two people, and a fire in the office destroys everything.
A database solves these problems. It is an organised collection of related data, and a database management system (DBMS) is the software that stores it, answers questions on it and protects it. A DBMS keeps each fact in one place, lets many users work at once without clashing, and can recover the data after a crash.
Before building tables, designers first draw a picture of the real world: what things exist and how they are linked. That picture is the ER model, and the tables that follow from it form the relational model. This chapter teaches both.
Why it matters. Every railway ticket, bank account and exam result in India sits in a database. A bad design makes data repeat and disagree, while a good design keeps it correct for years, and that is why design questions open the database part of every exam.
Entities and the kinds of attributes
An entity is a real-world thing that can be told apart from others, such as one student, one train or one bank account. A collection of similar entities, such as all students of a college, is an entity set. Each entity is described by attributes, such as roll number, name and date of birth.
Attributes come in pairs of types, and exams test each pair:
- A simple attribute cannot be split further, such as roll number, while a composite attribute has parts, such as Name made of first name and last name.
- A single-valued attribute has one value per entity, such as date of birth, while a multivalued attribute can have several, such as phone numbers.
- A stored attribute is kept in the database, while a derived attribute is calculated from others, such as Age from date of birth.
- A key attribute has a different value for every entity, such as roll number, so it identifies the entity.
In an ER diagram, an entity set is drawn as a rectangle and an attribute as an ellipse joined to it. A multivalued attribute has a double ellipse, a derived attribute a dashed ellipse, and a key attribute is underlined. A composite attribute is an ellipse with smaller ellipses for its parts.
Sometimes a value is missing, for example when a student has no email address or we do not yet know it. Such a value is marked NULL, which means "unknown or not applicable", and it is not the same as zero or an empty word.
Why it matters. Each attribute type maps to tables in its own way. A derived attribute is never stored, a composite one is broken into parts, and a multivalued one needs a whole new table, so getting the type right decides the table design.
Relationships, cardinality and participation
Entities do not live alone. A student enrols in a course and an employee works in a department. Such an association is a relationship, and a set of similar relationships is a relationship set, drawn as a diamond joined to the rectangles it links.
The degree of a relationship set is the number of entity sets in it. A binary relationship links two entity sets and is by far the most common. A ternary relationship links three, such as Supplier supplies Part to Project. A unary or recursive relationship links an entity set to itself, such as Employee manages Employee, where the two ends play different roles.
A relationship can carry its own attribute. In "Student enrols in Course", the grade belongs neither to the student alone nor to the course alone; it belongs to the pair, so it is an attribute of Enrols.
The cardinality ratio of a binary relationship says how many entities on one side one entity on the other side can be linked to, at most:
| Ratio | Meaning | Example |
|---|---|---|
| 1:1 | Each A with at most one B, and each B with at most one A | Department has one Head |
| 1:N | One A with many B, but each B with at most one A | Department has many Employees |
| M:N | Each A with many B, and each B with many A | Students and Courses |
Participation answers a different question: must every entity take part? If every employee must work in some department, Employee has total participation, drawn with a double line. If a department may exist with no project yet, its participation in "runs project" is partial, drawn with a single line.
Some books write both facts together as a (min, max) pair on each line. For example, (1, 1) on the Employee side of Works_in means each employee works in at least one and at most one department. A minimum of 0 means partial participation, and a minimum of 1 or more means total participation.
Why it matters. Cardinality and participation decide where foreign keys go and whether they may be NULL. Most "minimum number of tables" questions in GATE are really questions about these two properties.
Weak entity sets
Some entities cannot be identified on their own. A company keeps the dependents of each employee for insurance: a dependent's name is "Asha", but two different employees may both have a daughter called Asha. The name alone is not a key; only "employee E101's dependent Asha" is unique.
Such an entity set is a weak entity set. It depends on an owner (also called identifying or strong) entity set through an identifying relationship. The attribute that tells apart the weak entities of the same owner, here the dependent's name, is the partial key or discriminator.
The ER notation marks all of this clearly. The weak entity set is a double rectangle, the identifying relationship is a double diamond, and the partial key is underlined with a dashed line. The weak entity always has total participation in the identifying relationship, because a dependent cannot exist without an employee.
The key of a weak entity is formed by joining the owner's primary key with the partial key. So Dependent gets the key (EmpID, DepName). The identifying relationship is normally 1:N from owner to weak entity, since one employee may have many dependents but each dependent belongs to one employee.
Why it matters. Weak entities show up wherever data is "owned": the rooms of a hotel building, the installments of a loan, the lines of a bill. If you forget the owner's key, two different records collide and the database cannot tell them apart.
The relational model: relations, tuples and domains
The ER diagram is a drawing; the computer stores relations. A relation is simply a table with a name, such as Student(RollNo, Name, City, DOB). Each column is an attribute, each row is a tuple, and the set of values an attribute may take is its domain, such as "10-digit numbers" for a phone column.
Four words describe a relation's size and shape:
| Term | Meaning | In Student with 4 columns and 60 rows |
|---|---|---|
| Degree (arity) | Number of attributes | 4 |
| Cardinality | Number of tuples | 60 |
| Schema | Name and attribute list, the design | Student(RollNo, Name, City, DOB) |
| Instance | The tuples present at one moment | The 60 rows today |
A relation is a set of tuples, and this gives it three properties. No two tuples can be exactly the same, the order of rows carries no meaning, and the order of columns does not matter when values are read by attribute name. Each value is also atomic, meaning a single value from the domain, not a list.
The schema changes rarely, perhaps once in years, while the instance changes every time a row is added or deleted. Exams use "relation" for the instance and "relation schema" for the design, so read the question carefully.
Why it matters. Because a relation is a set, the database can rearrange rows freely for speed, and queries can be written in terms of sets. Atomic values are also the first condition of good design, which the normal forms build on.
Keys: super, candidate, primary and foreign
How does a database find exactly one tuple? It uses a key. A superkey is any set of attributes whose values never repeat across tuples. In Student(RollNo, Name, City, DOB), {RollNo} is a superkey, and so are {RollNo, Name} and {RollNo, Name, City}, because adding attributes to a unique set keeps it unique.
A candidate key is a minimal superkey: remove any attribute and it stops being unique. {RollNo, Name} is a superkey but not a candidate key, since RollNo alone already works. If Aadhaar number is also stored, {Aadhaar} is a second candidate key.
The designer picks one candidate key as the primary key, and its values may never be NULL. The other candidate keys are alternate keys. Any attribute that belongs to at least one candidate key is called a prime attribute, and the rest are non-prime.
Worked example 1: counting superkeys. Take R(A, B, C, D) with the only candidate key A. Every superkey must contain A, and each of B, C, D may be in or out, so there are superkeys. In general, attributes and one candidate key of attributes give superkeys.
Worked example 2: two candidate keys. Let R(A, B, C, D) have candidate keys A and B. Sets containing A number , sets containing B also number 8, and sets containing both number . By inclusion and exclusion, the answer is superkeys.
A foreign key links two relations. In Enrolment(RollNo, CourseID, Grade), RollNo refers to Student. Referential integrity says every foreign-key value must match the primary key of some existing tuple in the referenced relation, or else be NULL. A foreign key may even point to its own relation, as in Employee(EmpID, Name, ManagerID), where ManagerID refers to EmpID.
Why it matters. Keys stop duplicate records, and foreign keys stop "orphan" records such as a marks entry for a student who does not exist. Counting superkeys and spotting candidate keys are among the most frequent one-mark GATE questions.
Turning an ER diagram into tables
Once the ER diagram is ready, a fixed set of rules turns it into tables. Start with the entity sets:
- Each strong entity set becomes a table with its simple attributes, and its key becomes the primary key.
- A composite attribute is flattened, so Name becomes two columns, FirstName and LastName.
- A derived attribute such as Age is not stored at all.
- Each multivalued attribute gets its own table, such as StudentPhone(RollNo, Phone), whose key is both columns together.
- A weak entity set becomes a table that includes the owner's key, such as Dependent(EmpID, DepName, Age), with primary key (EmpID, DepName).
Then map the relationships, and here the cardinality decides everything:
| Relationship | How it is stored | Tables for two entity sets |
|---|---|---|
| M:N | A new table holding both keys (plus relationship attributes), key is the pair | 3 |
| 1:N | Foreign key added on the N side | 2 |
| 1:1 | Foreign key on either side, preferably the total side | 2 |
| 1:1, total on both sides | Both entity sets merged into one table | 1 |
Worked example. Student(RollNo, Name) and Course(CourseID, Title) are linked by an M:N relationship Enrols with attribute Grade, and Student has a multivalued attribute Phone. The tables are Student(RollNo, Name), Course(CourseID, Title), Enrols(RollNo, CourseID, Grade) with key (RollNo, CourseID), and StudentPhone(RollNo, Phone). So the minimum number of tables is 4.
Why put the foreign key on the N side in 1:N? Each employee works in one department, so the Employee row has room for exactly one DeptID. Putting EmpID inside Department would need a list of employees in one cell, which breaks atomic values.
A ternary relationship, or a relationship of degree more than two, always becomes a table of its own holding the keys of all its entity sets. A recursive 1:N relationship, such as "manages", becomes a foreign key in the same table, ManagerID inside Employee.
Why it matters. GATE often draws a small ER diagram and asks for the minimum number of tables. Count the entity sets, add one for each M:N relationship and each multivalued attribute, and merge only when a 1:1 relationship has total participation on both sides.
Descriptive lens: how written papers ask this
Written papers for IT officer posts, state Assistant Programmer exams and university tests ask about data modelling in four ways:
- Draw an ER diagram for a given situation, such as a hospital, a library or a railway reservation system, marking keys, cardinality and participation.
- Map a given ER diagram to relations and state each primary and foreign key.
- Define and contrast pairs of terms, such as weak and strong entity, superkey and candidate key, or total and partial participation.
- Justify a design choice, such as why a multivalued attribute needs a separate table.
Practice questions (our own, not previous-year questions).
- Draw an ER diagram for a college library with members, books, copies of books and loans. Mark the weak entity, if any, and all cardinalities. (15 marks)
- Explain superkey, candidate key, primary key and foreign key with one relation of your own. How many superkeys does R(A, B, C, D, E) have if A is its only candidate key? (10 marks)
- Map an ER diagram with entity sets Doctor, Patient and Ward to tables, where a doctor treats many patients and each patient stays in one ward. (10 marks)
Answer plan for question 1.
- Identify the entity sets Member, Book and Copy, and explain that Copy is weak because copy number 3 repeats across titles.
- Give Book the key ISBN and give Copy the partial key CopyNo, so that its full key is (ISBN, CopyNo).
- Model Loan as an M:N relationship between Member and Copy over time, with attributes IssueDate and ReturnDate.
- Mark Copy's participation in the identifying relationship as total, and Member's participation in Loan as partial.
- Show the resulting tables and point out the foreign keys, so that the examiner sees the design is complete.
- Close with one line on why the design avoids repeating book details for every copy.
Quick revision
- A DBMS stores related data once, shares it safely among many users and recovers it after failures.
- An entity set is a rectangle, an attribute an ellipse, a multivalued attribute a double ellipse, a derived one a dashed ellipse, and a key is underlined.
- Cardinality (1:1, 1:N, M:N) gives the maximum partners, and participation (total or partial) gives the minimum.
- A weak entity has a double rectangle, a double diamond, total participation and a key made of the owner key plus the partial key.
- Degree is the number of columns, and cardinality is the number of rows.
- A candidate key is a minimal superkey, and the primary key is the chosen candidate key that cannot be NULL.
- One candidate key of attributes among attributes gives superkeys.
- A foreign key must match an existing primary key value or be NULL.
- M:N needs a separate table, 1:N puts the foreign key on the N side, and 1:1 with total participation on both sides can be merged.
- Each multivalued attribute adds one table, and a derived attribute adds none.