Sub topic 1
Entities, Entity Sets, and Relationships
Prerequisites
This chapter assumes you know basic set notation, what a table, row, and column are, and how to write CREATE TABLE and SELECT in SQL.
Entities and Entity Sets
An entity is a distinct object you want to store, such as one employee. An entity set is the group of entities of the same kind, such as all employees. In an ER (entity-relationship) diagram, an entity set is drawn as a rectangle.
Attribute Types in an ER Diagram
Attributes describe an entity. The common types are:
- Simple: one atomic value, such as
age. - Composite: made of parts, such as
address(street, city, zip). - Multivalued: several values, such as phone numbers.
- Derived: computed from other data, such as
agecomputed fromdate_of_birth.
Cardinality Ratios and Participation Constraints
A cardinality ratio states how many entities can relate to each other: 1:1, 1:N, or M:N. A participation constraint states whether every entity must take part. Total participation means each entity must join the relationship. Partial participation means it may skip it.
Weak Entities and Identifying Relationships
A weak entity has no key of its own. It is identified through an owner entity using an identifying relationship. A dependent, for example, is identified by its employee plus its partial key, dependent_name.
Specialization, Generalization, and Aggregation
Specialization splits an entity set into subclasses, such as Employee into Engineer and Manager. Generalization is the reverse: it combines subclasses into one superclass. Aggregation treats a relationship as a single entity so that it can take part in another relationship.
The Relational Model in Brief
A relation is a table. Each row is a tuple, each column is an attribute, and each attribute draws its values from a domain.
Superkeys, Candidate Keys, and Primary Keys
A superkey is any set of attributes that identifies each row uniquely. A candidate key is a minimal superkey, meaning no attribute can be removed without losing uniqueness. The primary key is the candidate key chosen for the table. The remaining candidate keys are alternate keys.
Converting an ER Diagram to Tables
Apply these rules:
- A strong entity becomes a table with its key as the primary key.
- A multivalued attribute becomes a separate table.
- An M:N relationship becomes a table whose primary key combines both foreign keys.
- For 1:N, place the foreign key on the N side.
- A weak entity becomes a table whose primary key combines the owner's key and its partial key.
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
);Exercise
A student can enroll in many courses, and a course has many students. Which table design do you use, and what is its primary key?
Show answer
Use a separate table for the M:N relationship, such as enrollment(studentid, courseid). Its primary key is the pair (studentid, courseid).