CMD Guide
HomeDatabases

Relational Model

Step 5 in the Databases path · 7 concepts · 0 problems

0 / 7 complete

📘 Learn Relational Model from zero

The relational model (Codd, 1970) is a precise, set-based way to store facts in tables. Think of a well-organized spreadsheet workbook: each sheet is a relation (table), each column is an attribute with a fixed type (its domain), and each row is a tuple, one fact about one thing.

Three rules make it more than a spreadsheet. First, every row must be uniquely identifiable by a primary key (entity integrity: no part of it may be NULL, and the combination is never duplicated). Second, to link sheets, a column in one table holds a key value from another, a foreign key (referential integrity: it must reference a row that actually exists, or be NULL). Third, a relation is a set of tuples, so the order of rows and columns carries no meaning and there are no duplicate rows; only the values matter.

Worked example. Model students and the courses they take. Two strong entities become two tables:

A student can take many courses and a course has many students, an M:N relationship. You cannot store this with a foreign key on either entity table, so you create a junction table: Enrollment(student_id FK, course_id FK, grade) whose primary key is the composite (student_id, course_id). Row (1, 'CS101', 'A') says Ada took Databases. Both foreign keys must reference existing rows, so you can never enroll a phantom student in a phantom course.

Key insight: the relational model represents everything, entities and the relationships between them, as relations governed by keys, and constraints declared in the schema (not application code) guarantee the data stays valid.

✨ Added by the guide to build intuition — not from the source course.

🎯 Guided practice

  1. Easy, identify the keys. Given Employee(emp_id, email, ssn, dept_id, name) where emp_id, email, and ssn are each individually unique.

    Step 1: List the candidate keys, the minimal attribute sets that uniquely identify a row. Here each of emp_id, email, and ssn alone uniquely identifies an employee, so there are three candidate keys. (Note: the set {emp_id, email} is a superkey but not a candidate key, because it is not minimal.)

    Step 2: Pick one as the primary key. Prefer the stable, simple, non-changing one: emp_id. (Emails change; SSNs are sensitive and not universal.) The others remain alternate keys, enforced as UNIQUE (and ideally NOT NULL).

    Step 3: Spot the foreign key. dept_id identifies a department, not an employee, so it is a foreign key referencing Department(dept_id); referential integrity requires every dept_id value to match an existing department (or be NULL if the column is nullable). Pattern: uniqueness within the table = candidate/primary key; a reference to another table's key = foreign key.

  2. Medium, map an ER diagram to relations. ER: entity Author (with a multivalued attribute phone), entity Book, and an M:N relationship Writes between them carrying an attribute royalty_pct.

    Step 1, strong entities → tables. Each becomes a table keyed by its identifier: Author(author_id PK, name) and Book(isbn PK, title).

    Step 2, multivalued attribute → its own table. A single column cannot hold many phone numbers atomically (storing a list violates first normal form). Create AuthorPhone(author_id FK, phone) with primary key (author_id, phone), where author_id references Author.

    Step 3, M:N relationship → junction table. Writes cannot live as a foreign key on either entity, so create Writes(author_id FK, isbn FK, royalty_pct) with composite primary key (author_id, isbn). The relationship's own attribute, royalty_pct, naturally lives on this junction table. (A 1:N relationship, by contrast, needs no new table: you put a foreign key on the "many" side.)

    Step 4, verify constraints. Every foreign key, in Writes and in AuthorPhone, must reference an existing parent row (referential integrity), and no attribute of any primary key may be NULL (entity integrity). Pattern to remember: strong entity → table; multivalued attribute → separate table; 1:N → foreign key on the "many" side; M:N → junction table whose primary key is the pair of foreign keys, and that junction table is where relationship attributes go.

✨ Added by the guide — work these before the full problem set.

Lessons in this topic

🧠 Review & recall

Active recall is what moves a topic into long-term memory. Flip each card before revealing, then test yourself — your results are saved on this device.

Flashcard
In the relational model, define relation, tuple, and attribute, and what is special about the set of tuples in a relation?
tap to reveal →
A relation is a table representing an entity (e.g., Student); a tuple is a single row representing one record/instance; an attribute is a column representing a property with a specific data type. Each tuple in a relation is unique, so there are no duplicate rows.
💡 Table = Relation, Row = Tuple, Column = Attribute (R-T-A); rows are a SET, so no dupes.
Flashcard
Distinguish a relation's arity (degree) from its cardinality.
tap to reveal →
Arity (degree) is the number of attributes/columns a relation has; cardinality is the number of tuples/rows it contains. A Student table with 3 columns and 50 rows has arity 3 and cardinality 50.
💡 Arity = Attributes (across); Cardinality = Count of rows (down).
Flashcard
What is the difference between a super key, a candidate key, an alternate key, and a primary key?
tap to reveal →
A super key is any attribute set that uniquely identifies a row (may include extra attributes). A candidate key is a minimal super key. The primary key is the one candidate key chosen as the main identifier; the remaining candidate keys are alternate keys. E.g., if Roll Number and Email both uniquely identify a student and Roll Number is chosen as PK, Email is an alternate key.
💡 Super ⊇ Candidate (minimal) → one becomes Primary, the rest are Alternate.
Flashcard
Name the three core key-related integrity constraints and what each guarantees.
tap to reveal →
Entity integrity: the primary key must be unique and cannot be null. Referential integrity: a foreign key value must match an existing primary key in the referenced table (no orphan records). Key/uniqueness constraint: certain attributes must have unique values (applied to primary and candidate keys).
💡 Entity = PK not null; Referential = FK must point to a real parent; Key = no duplicates.
Flashcard
When converting an ER diagram to relations, how do you handle a 1:1, a 1:M, and an M:N relationship?
tap to reveal →
1:1: add the primary key of one entity as a foreign key in the other table. 1:M: add the primary key of the 'one' side as a foreign key in the 'many' side table. M:N: create a new junction table holding both entities' primary keys as foreign keys, plus any relationship attributes (e.g., Enrollment with Student_ID, Course_ID, Enrollment_Date).
💡 1:1 & 1:M → FK (on the many side for 1:M); M:N → junction table.
Flashcard
How are composite, multivalued, and derived attributes mapped from an ER diagram into a relational model?
tap to reveal →
Composite: split into separate columns (Full_Name → First_Name, Middle_Initial, Last_Name). Multivalued: create a separate table with the attribute plus a foreign key to the original entity (Employee_Contact with Employee_ID FK, Contact_Number). Derived: avoid storing it and compute dynamically (Age from Birthdate), storing only if the computation is complex/expensive/frequent.
💡 Composite = split columns; Multivalued = new table + FK; Derived = compute, don't store.
Q1. A Student relation has columns Roll Number, Name, and CGPA, and currently holds 50 rows. What are its arity and cardinality?
Q2. In the Student table, both Roll Number and Email can each uniquely identify a row, and Roll Number is chosen as the primary key. What is Email?
Q3. A Student table has a Course ID foreign key referencing Course ID in the Course table. Inserting a student with Course ID 'C109' fails because no such course exists. Which constraint enforced this?
Q4. You are converting an M:N relationship 'Student takes Course' that carries an Enrollment_Date attribute. What is the correct relational mapping?
Q5. An Employee entity has a multivalued attribute Contact_Number and a derived attribute Age (from Birthdate). What is the recommended relational treatment?