Sub topic 4
Weak Entities and Identifying Relationships
This chapter assumes you can read basic ER diagrams (entities, attributes, relationships) and know the cardinality and participation notation from the previous chapter. You should also know what a primary key and a foreign key are.
What a weak entity is
A strong entity type has a key of its own. Its attributes alone identify each entity. A weak entity type does not. Its entities can only be told apart by combining its own attributes with the key of another entity, called the owner (or identifying) entity.
Typical examples:
- A
DEPENDENTof anEMPLOYEE. Two employees can each have a dependent named "Asha", so the name alone is not unique. - A
ROOMin aBUILDING. Room 101 exists in many buildings. - A
LOAN_PAYMENTof aLOAN. Payment number 1 exists for every loan.
A weak entity cannot exist without its owner. If the owner is deleted, the weak entities go with it.
Notation
| Concept | Symbol in the diagram |
|---|---|
| Weak entity type | Double rectangle |
| Identifying relationship | Double diamond |
| Partial key (discriminator) | Dashed underline |
| Participation of the weak entity | Double line (total) |
The partial key, also called the discriminator, is the set of attributes that distinguishes weak entities belonging to the same owner. dependent_name is a partial key because it is unique only among one employee's dependents. It is not unique across the whole table.
The identifying relationship connects the weak entity to its owner. It has these properties:
- The weak entity participates totally, because every dependent must belong to some employee.
- The cardinality is usually 1:N from owner to weak entity. One employee has many dependents, and each dependent has exactly one owner.
- The key of a weak entity is the owner's key plus the partial key.
Mermaid cannot draw the double rectangle, so read DEPENDENT as the weak entity. Its primary key has two parts, and emp_id comes from the owner.
Mapping a weak entity to a table
The ER-to-relational rules for a weak entity are:
- Create one table for the weak entity.
- Include all of its simple attributes. Flatten any composite attribute into its components.
- Add the owner's primary key as a foreign key.
- Make the primary key the owner's key plus the partial key.
- Declare the foreign key with
ON DELETE CASCADE, so removing an owner removes its weak entities.
The identifying relationship does not get its own table. The foreign key already represents it. If the relationship has descriptive attributes, they are stored in the weak entity's table.
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL
);
CREATE TABLE dependent (
emp_id INT,
dependent_name VARCHAR(50),
relationship VARCHAR(20),
PRIMARY KEY (emp_id, dependent_name),
FOREIGN KEY (emp_id) REFERENCES employee(emp_id)
ON DELETE CASCADE
);Three things in this table are worth checking:
emp_idis both part of the primary key and a foreign key. Entity integrity therefore forbids it from being NULL, so a dependent without an owner cannot be inserted.- The composite primary key allows two employees to have a dependent with the same name, but not one employee to have two dependents with the same name.
ON DELETE CASCADEmatches the existence dependency.ON DELETE SET NULLwould fail here, because the key column cannot be NULL.
Edge cases
Several owners. A weak entity can depend on more than one owner. Its key is then the union of all owners' keys plus the partial key, and each owner contributes its own foreign key.
Chains of weak entities. Suppose DEPARTMENT owns PROJECT_PHASE, which owns TASK. The key of TASK includes the key of PROJECT_PHASE, which in turn includes the key of DEPARTMENT. Keys grow along the chain.
Many-to-many identifying relationship. This is rare but allowed. The weak entity then needs a separate table for the relationship, as with any M:N relationship.
A strong entity with a foreign key is not weak. An EMPLOYEE table that has a dept_id foreign key is still strong, because emp_id identifies a row without help. Weakness is about identification, not about having a foreign key.
Surrogate keys. In practice a designer may give dependent its own dependent_id column and treat it as strong. The ER model still calls the original design weak, and exam questions assume that design.
Common errors
- Making the partial key the entire primary key. The owner's key must be part of it.
- Creating a separate table for the identifying relationship, which gives an extra table that the rules do not call for.
- Marking the weak entity's participation as partial. It is always total in the identifying relationship.
- Forgetting the cascade, so that deleting an owner leaves orphaned weak entities or is blocked.
Counting tables in GATE-style questions
GATE questions often ask for the minimum number of tables for an ER diagram. A weak entity adds one table, and its identifying relationship adds none. Suppose a diagram has EMPLOYEE, DEPENDENT (weak) and DEPARTMENT, with an N:1 WORKS_FOR relationship and the identifying relationship COVERS. It needs three tables. WORKS_FOR is folded into EMPLOYEE as a dept_id foreign key, and COVERS is folded into DEPENDENT.
Exercise
A BUILDING(building_id, address) entity owns a weak entity ROOM with partial key room_no and attribute capacity. Write the relational schema for ROOM, state its primary key, and say what happens to the rooms when a building row is deleted.
Show answer
ROOM(buildingid, roomno, capacity). The primary key is (buildingid, roomno), and building_id is a foreign key to BUILDING with ON DELETE CASCADE. Deleting a building also deletes all of its rooms, because a room cannot exist without its owner.
Exercise
Why can ON DELETE SET NULL not be used on the foreign key of a weak entity's table?
Show answer
The foreign key column is part of the primary key, and entity integrity forbids NULL in primary key columns. Setting it to NULL would violate that rule, so the delete would be rejected.