DBMS/Lesson 2 of 9/5 min read
Entity-Relationship Model
Entities, attributes and their types, relationships, cardinality, participation, weak entities, generalization, specialization, aggregation, and converting ER diagrams to tables.
On this page
ER Model
The Entity-Relationship (ER) model, proposed by Peter Chen in 1976, is a high-level conceptual model used to design a database before creating tables. It is drawn as an ER diagram.
Standard notation (Chen):
| Shape | Meaning |
|---|---|
| Rectangle | Entity set |
| Double rectangle | Weak entity set |
| Ellipse | Attribute |
| Underlined ellipse | Key attribute |
| Double ellipse | Multivalued attribute |
| Dashed ellipse | Derived attribute |
| Diamond | Relationship set |
| Double diamond | Identifying relationship (for weak entities) |
| Double line | Total participation |
Entity
An entity is a real-world object distinguishable from others: a particular student, a particular course.
Entity Set
An entity set is a collection of entities of the same type: all students. In tables, an entity set becomes a table, and each entity a row.
Attribute
An attribute is a property of an entity: name, roll_no, dob. Each attribute has a domain (set of allowed values).
Types:
- Simple: cannot be divided (
age). - Key attribute: uniquely identifies an entity (
roll_no). - Single-valued: one value per entity (
dob).
Composite Attribute
Can be divided into sub-parts: name = first_name, middle_name, last_name; address = street, city, pin.
Multivalued Attribute
Can have many values for one entity: phone_numbers, skills. Mapped to a separate table.
Derived Attribute
Computed from other attributes, not stored: age from dob, total_price from items.
A NULL value means unknown, missing or not applicable.
Relationship
A relationship is an association between entities: student enrolls in course. A relationship set is a set of similar relationships. Relationships can have attributes too (grade on enrolls in).
Degree of a relationship = number of participating entity sets:
- Unary (recursive): employee manages employee.
- Binary: student enrolls in course (most common).
- Ternary: supplier supplies part to project.
Cardinality
Cardinality (mapping cardinality) is the number of entities one entity can be associated with through a relationship.
| Type | Example | Table mapping |
|---|---|---|
| One-to-one (1:1) | Person has one passport | Foreign key on either side (prefer the total participation side) |
| One-to-many (1:N) | Department has many employees | Foreign key on the "many" side |
| Many-to-one (N:1) | Many employees work in one department | Foreign key on the "many" side |
| Many-to-many (M:N) | Students enroll in many courses | Separate junction table with both keys |
Participation
- Total participation: every entity must take part in the relationship (every loan must belong to a customer). Drawn with a double line.
- Partial participation: some entities may not participate (not every customer has a loan).
Min-max notation (min, max) on each side expresses both cardinality and participation precisely.
Weak Entity
A weak entity cannot be uniquely identified by its own attributes; it depends on a strong (owner) entity.
- Has a partial key (discriminator), drawn with a dashed underline.
- Primary key = owner's key + partial key.
- Always has total participation in an identifying relationship (double diamond).
Example: Dependent(name, age) of an Employee. Two employees can each have a dependent named "Asha"; the key is (emp_id, dependent_name).
Extended ER features
Generalization
Generalization is bottom-up: combine lower-level entity sets that share features into a higher-level one. Car and Truck generalize to Vehicle.
Specialization
Specialization is top-down: split a higher-level entity into sub-groups with distinct attributes. Employee specializes into Engineer and Manager. Represented with an ISA triangle.
Constraints on specialization:
- Disjoint vs overlapping: can an entity belong to more than one subclass?
- Total vs partial: must every superclass entity belong to some subclass?
Subclasses inherit all attributes and relationships of the superclass, the same idea as inheritance in OOP.
Aggregation
Aggregation treats a relationship set as an entity so it can take part in another relationship. Example: employee works on project as a unit, which is then monitored by a manager.
Converting ER to tables
- Strong entity: table with all simple attributes; composite attributes flattened; key attribute becomes the primary key.
- Weak entity: table with its attributes plus the owner's primary key as a foreign key; primary key = owner key + partial key.
- Multivalued attribute: new table
(entity_key, value). - 1:N relationship: add the "one" side's key as a foreign key in the "many" side's table.
- 1:1 relationship: foreign key in either table (prefer the side with total participation), or merge the tables.
- M:N relationship: new table with both keys (composite primary key) plus relationship attributes.
- Specialization: one table per subclass with the superclass key, or one table for everything with a type column, or a table per subclass only (if total and disjoint).
Example M:N:
CREATE TABLE enrollment (
student_id INT REFERENCES student(id),
course_id INT REFERENCES course(id),
grade CHAR(2),
PRIMARY KEY (student_id, course_id)
);Quick revision
- Entities become tables, attributes columns, M:N relationships junction tables.
- Weak entity key = owner key + partial key, with total participation.
- Generalization is bottom-up, specialization is top-down.
- Aggregation = relationship treated as an entity.