16.2The Entity-Relationship and Relational Models
Covers IGNOU MCS-201 §16.2
A data model describes how data is structured, related and constrained. Designers usually start with an entity-relationship (ER) diagram to capture the real world, then turn it into tables in the relational model, which MySQL implements.
Data models
| Model | Organises data as | Status |
|---|---|---|
| Flat file | One table in one file, no relationships | Simple data only |
| Hierarchical | A tree of parent and child records | Older systems |
| Network | Records linked in a graph | Older systems |
| Relational | Tables related through common columns | The standard model (MySQL, Oracle, PostgreSQL) |
The entity-relationship model
| Concept | Meaning | Example | ER diagram symbol |
|---|---|---|---|
| Entity | A real-world object that can be identified | An employee, a department | Rectangle |
| Attribute | A property of an entity | emp_name, age | Ellipse |
| Relationship | An association between entities | Employee works in department | Diamond |
| Cardinality | How many of one entity relate to the other | One-to-one, one-to-many, many-to-one, many-to-many | Labels on the lines |
(emp_id) (emp_name) (emp_addr) (dept_id) (dept_name)
\ | / \ /
[ Employee ]------< works in >------[ Department ]
N 1Many employees work in one department, and each employee works in one department at a time.
The relational model
E. F. Codd proposed the relational model in 1970. Data is stored in relations (tables).
| Formal term | Common term | Meaning |
|---|---|---|
| Relation | Table | A set of rows with the same columns |
| Tuple | Row, record | One entity instance, e.g. one employee |
| Attribute | Column, field | One property, e.g. salary |
| Domain | Data type and allowed values | E.g. salary: positive numbers |
| Degree | Number of columns | |
| Cardinality | Number of rows |
Keys
| Key | Definition | Example in employee(emp_code, emp_name, email, dept_id) |
|---|---|---|
| Super key | Any set of columns that uniquely identifies a row | (emp_code, emp_name) |
| Candidate key | A minimal super key (no column can be removed) | emp_code; email |
| Primary key | The candidate key chosen to identify rows; unique and NOT NULL; one per table | emp_code |
| Alternate key | A candidate key not chosen as primary key | email |
| Composite key | A key made of two or more columns | (order_id, product_id) in an order-lines table |
| Foreign key | A column that refers to the primary (or another unique) key of another table, linking the two | dept_id referring to department(dept_id) |
Key takeaways
- ER model: entities (rectangles), attributes (ellipses), relationships (diamonds), cardinality.
- Relational model: relation = table, tuple = row, attribute = column.
- Keys: candidate, primary (unique, not null, one per table), alternate, composite, foreign.
- Foreign keys implement relationships and referential integrity.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
A doctor can treat many patients and a patient can be treated by many doctors. How should this many-to-many relationship be stored in relational tables?
Show worked answer
Use three tables: doctor(doctor_id, name, ...) with primary key doctor_id, patient(patient_id, name, ...) with primary key patient_id, and a linking table treatment(doctor_id, patient_id, date). In treatment, doctor_id and patient_id are foreign keys referring to the two tables, and together (possibly with date) they form a composite primary key. Each row records one doctor treating one patient.