Sub topic 5
Specialization, Generalization, and Aggregation
This chapter covers the extended ER (EER) features: specialization, generalization, and aggregation. It assumes you know entities, entity sets, attributes, keys, and cardinality and participation constraints from the earlier chapters. You should also know basic SQL CREATE TABLE with PRIMARY KEY and FOREIGN KEY. These features appear in GATE CS mostly as short conceptual questions and as table-counting questions about mapping to relational tables.
Why the basic ER model is not enough
Sometimes entities of one type fall into subgroups. Some attributes apply to every subgroup, and others apply to one subgroup only. An EMPLOYEE entity set might include engineers, secretaries, and managers. All employees have an emp_id and a name. Only engineers have specialty, and only secretaries have typing_speed.
If you put all attributes in one entity set, most rows would have NULLs in the attributes that do not apply to them. The EER model solves this by letting you model subgroups explicitly.
Specialization
Specialization is a top-down process. You start with one entity set and define subsets of it, based on some distinguishing characteristic. The original entity set is the superclass (or higher-level entity set). Each subset is a subclass (or lower-level entity set).
Key properties:
- A subclass entity is also a member of its superclass. Every engineer is an employee.
- A subclass inherits all attributes of the superclass, including its key. It also inherits the relationships the superclass takes part in.
- A subclass can have its own local attributes and relationships. For example,
specialtybelongs only toENGINEER. - The relationship between a subclass and its superclass is called an ISA relationship ("is a"). It is drawn as a triangle in Silberschatz-style diagrams, or as a circle with a
Uin Elmasri-Navathe style.
Generalization
Generalization is the reverse, bottom-up process. You start with several entity sets that share common attributes and combine them into a higher-level entity set. For example, CAR and TRUCK both have reg_no, owner, and price. You can generalize them into a VEHICLE entity set that holds the shared attributes. CAR and TRUCK keep only their own attributes.
Specialization and generalization describe the same final structure. They differ only in the direction in which the designer thinks. In both cases the result is a superclass with one or more subclasses.
Constraints on specialization
Two independent constraints describe how subclasses relate to the superclass and to each other.
| Constraint | Options | Meaning |
|---|---|---|
| Disjointness | Disjoint or overlapping | Can one entity belong to more than one subclass? |
| Completeness | Total or partial | Must every superclass entity belong to at least one subclass? |
The two constraints combine into four possible cases:
- Disjoint and total: every employee is exactly one of engineer, secretary, or manager.
- Disjoint and partial: an employee may be an engineer or a secretary, but some employees belong to neither.
- Overlapping and total: every person is a student, a teacher, or both.
- Overlapping and partial: a person may be a student, a teacher, both, or neither of the two.
Do not confuse total completeness with total participation in a relationship. Participation concerns relationship sets. Completeness concerns whether every superclass entity must appear in a subclass.
A diagram shows a typical hierarchy. The caption and alt text describe it.
Attribute-defined and user-defined subclasses
A specialization can be attribute-defined or user-defined.
- In an attribute-defined specialization, a condition on one superclass attribute decides membership. For example,
job_type = 'Engineer'places an employee inENGINEER. - In a user-defined specialization, the database user assigns each entity to a subclass when inserting it. No rule on an attribute decides it.
Mapping specialization to tables
There are several standard ways to map a superclass EMPLOYEE(emp_id, name) with subclasses ENGINEER(specialty) and SECRETARY(typing_speed).
Option 1: one table per entity set
Create one table for the superclass and one for each subclass. Each subclass table holds the superclass key plus its local attributes. The superclass key is the primary key of the subclass table and also a foreign key to the superclass.
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE engineer (
emp_id INT PRIMARY KEY,
specialty VARCHAR(50),
FOREIGN KEY (emp_id) REFERENCES employee(emp_id)
ON DELETE CASCADE
);
CREATE TABLE secretary (
emp_id INT PRIMARY KEY,
typing_speed INT,
FOREIGN KEY (emp_id) REFERENCES employee(emp_id)
ON DELETE CASCADE
);This works for every combination of constraints: disjoint, overlapping, total, and partial. Reading all attributes of an engineer needs a join.
Option 2: tables for subclasses only
Create no table for the superclass. Each subclass table holds all superclass attributes plus its local attributes. For example, ENGINEER(emp_id, name, specialty) and SECRETARY(emp_id, name, typing_speed).
This needs the specialization to be total. If it is partial, entities that belong to no subclass have nowhere to be stored. If it is overlapping, an entity in two subclasses is stored twice, which causes redundancy.
Option 3: one table with a type attribute
Create a single table with all attributes of the superclass and of every subclass, plus a type attribute such as job_type. Attributes that do not apply to a row are NULL.
This suits disjoint specialization with few local attributes. It needs no joins but can produce many NULLs.
Option 4: one table with Boolean flags
Create a single table with all attributes and one Boolean flag per subclass, such as is_engineer and is_secretary. This suits overlapping specialization, because several flags can be true for one row.
| Option | Tables | Works for | Main drawback |
|---|---|---|---|
| 1. Superclass plus subclass tables | 1 + number of subclasses | All cases | Joins needed |
| 2. Subclass tables only | Number of subclasses | Total, disjoint | Redundancy if overlapping |
| 3. Single table with type attribute | 1 | Disjoint | NULLs |
| 4. Single table with flags | 1 | Overlapping | NULLs |
For GATE questions that ask for "the minimum number of tables", check which option is allowed and count accordingly. With one superclass and two subclasses, Option 1 gives 3 tables, Option 2 gives 2 tables, and Options 3 and 4 give 1 table.
Aggregation
The basic ER model cannot express a relationship between a relationship and an entity. Aggregation fixes this. It treats a relationship set, together with its participating entity sets, as a single higher-level entity set. That abstract entity can then take part in another relationship.
Consider this scenario. Employees work on projects through a relationship WORKS_ON. Some of these (employee, project) pairs have a manager who monitors them. A manager monitors the assignment of an employee to a project. It does not monitor the employee alone or the project alone.
If you add MANAGER as a third participant of WORKS_ON, the model says that every assignment has a manager. It also mixes two different facts. Instead, aggregate WORKS_ON into one unit and relate MANAGER to that unit through a new relationship MONITORS.
Mapping aggregation to tables
Map the inner relationship first. WORKS_ON becomes a table whose primary key is the combination of the employee key and the project key. The outer relationship MONITORS becomes a table whose key includes the manager key and the primary key of the WORKS_ON table. The foreign key in MONITORS points to the WORKS_ON table.
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE project (
proj_id INT PRIMARY KEY,
title VARCHAR(50) NOT NULL
);
CREATE TABLE manager (
mgr_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE works_on (
emp_id INT,
proj_id INT,
PRIMARY KEY (emp_id, proj_id),
FOREIGN KEY (emp_id) REFERENCES employee(emp_id),
FOREIGN KEY (proj_id) REFERENCES project(proj_id)
);
CREATE TABLE monitors (
mgr_id INT,
emp_id INT,
proj_id INT,
PRIMARY KEY (mgr_id, emp_id, proj_id),
FOREIGN KEY (mgr_id) REFERENCES manager(mgr_id),
FOREIGN KEY (emp_id, proj_id) REFERENCES works_on(emp_id, proj_id)
);The composite foreign key in monitors guarantees that a manager can monitor only an assignment that already exists in works_on. That is the integrity rule aggregation expresses.
Common errors
- Treating specialization as a relationship set with its own cardinality. ISA is not a relationship set. It is a structural link between a superclass and its subclasses.
- Giving a subclass its own key in Option 1. The subclass uses the superclass key.
- Using Option 2 for a partial or overlapping specialization.
- Confusing aggregation with a ternary relationship. A ternary relationship ties three entity sets together as equals. Aggregation first groups two of them and then relates the group to the third.
- Forgetting that a subclass inherits the superclass relationships. If
EMPLOYEEworks for a department,ENGINEERdoes too, without redrawing the line.
Exercise
A specialization of PERSON into STUDENT and TEACHER is overlapping and partial. Which mapping options (1 to 4) can store it correctly without losing entities that belong to neither subclass, and how many tables does each of those options need?
Show answer
Option 1 needs 3 tables (PERSON, STUDENT, TEACHER) and works because PERSON stores everyone, including people in neither subclass. Option 4 needs 1 table with flags isstudent and isteacher, and works because both flags can be true or both false. Option 2 fails because people in neither subclass have no table, and people in both are stored twice. Option 3 fails because a single type value cannot record membership in both subclasses.
Exercise
Why is a manager who monitors an employee's assignment to a project better modelled with aggregation than with a ternary relationship among MANAGER, EMPLOYEE, and PROJECT?
Show answer
A ternary relationship would require a manager for every (employee, project) pair and would mix assignment and monitoring into one fact. With aggregation, WORKSON exists on its own, and only some assignments take part in MONITORS. The monitors table's foreign key (empid, projid) references workson, so a manager can monitor only an existing assignment.
Exercise
A disjoint, total specialization of VEHICLE into CAR and TRUCK is mapped with Option 2. Write the two table schemas and say what would go wrong if the specialization were partial.
Show answer
CAR(regno, owner, price, doors) and TRUCK(regno, owner, price, loadcapacity), with regno as the primary key of each. If the specialization were partial, a vehicle that is neither a car nor a truck would have no table to hold it, so it could not be stored.