Sub topic 3
Weak Entities and Identifying Relationships
This chapter assumes you know entities, relationships, primary keys, and foreign keys. It focuses on entities that cannot be uniquely identified by their own attributes.
What Is a Weak Entity?
A weak entity is an entity whose instances do not have a complete key of their own. Its identity depends on an owner entity.
For example, an employee may have several dependents. A dependent might have attributes such as dependent_name and relationship. However, dependent_name may be unique only for one employee. Two different employees can each have a dependent named Sam.
The owner entity, Employee, has a primary key such as emp_id. The weak entity, Dependent, uses:
- The owner's key, called the owner key.
- Its own partial key, which distinguishes dependents belonging to the same owner.
Together, these attributes form the weak entity's primary key:
(emp_id, dependent_name)
The weak entity has total participation in its identifying relationship. Every dependent must belong to an employee. An employee, however, might have no dependents.
Identifying Relationships
An identifying relationship connects a weak entity to its owner. The relationship supplies the part of the weak entity's identity that the weak entity cannot provide itself.
Consider this model:
The crow's-foot notation means that one employee can identify zero or more dependents. The single bar near EMPLOYEE means each dependent is identified by exactly one employee.
The weak entity's partial key is often shown with a dashed underline in a traditional ER diagram. The identifying relationship may be shown with a double diamond, and the weak entity with a double rectangle.
Mapping a Weak Entity to Tables
When converting the ER model to relations, place the owner's primary key in the weak entity's table. Make it both:
- A foreign key referencing the owner.
- Part of the weak entity's composite primary key.
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL
);
CREATE TABLE dependent (
emp_id INT NOT NULL,
dependent_name VARCHAR(50) NOT NULL,
relationship VARCHAR(20) NOT NULL,
PRIMARY KEY (emp_id, dependent_name),
FOREIGN KEY (emp_id) REFERENCES employee(emp_id)
ON DELETE CASCADE
);ON DELETE CASCADE reflects the usual semantics of a weak entity. If an employee is deleted, that employee's dependents should also be deleted. Without the cascade, the database would reject the deletion or require the application to remove dependents first.
The NOT NULL constraint on emp_id enforces total participation in the relational schema. The composite primary key prevents two dependents with the same name from belonging to one employee, while allowing that name under another employee.
Exercise
Why is dependent_name alone usually not a suitable primary key for the Dependent table?
Show answer
It is only a partial key. The same name can occur for dependents of different employees, so the primary key must combine emp_id and dependent_name.