Sub topic 1
The Relational Model: Relations, Tuples and Schemas
Prerequisites
This chapter assumes you know basic set theory (union, intersection, difference, Cartesian product) and can read a simple table. No prior SQL is needed.
Relations, tuples and schemas
A relation is a named table. Each row is a tuple, and each column is an attribute. A schema lists the relation name and its attributes, for example Student(sid, sname, age). Each attribute draws its values from a domain, such as integers or strings.
A relation is a set. It has no duplicate tuples and no row order. This property affects every operator in this chapter.
Keys and integrity constraints
A superkey is a set of attributes that uniquely identifies each tuple. A candidate key is a minimal superkey. One candidate key is chosen as the primary key. A foreign key in one relation references the primary key of another. Referential integrity requires every non-null foreign key value to match an existing key value.
Inserting (99, 10) into Enrolled(sid, cid) violates referential integrity when no student has sid 99.
Selection and projection
Selection, written σ, keeps the tuples that satisfy a condition. Projection, written π, keeps only the listed attributes and removes duplicate tuples.
- σ_age>20(Student) keeps the students older than 20.
- π_sname(Student) lists the distinct names.
Union, intersection and difference
Union (∪), intersection (∩) and difference (−) require union-compatible relations. Both relations must have the same number of attributes, with matching domains in the same order.
Cartesian product and rename
R × S pairs every tuple of R with every tuple of S. If R has m tuples and S has n tuples, the product has m × n tuples. Rename, written ρ, gives a relation or attribute a new name. Renaming prevents clashes in products and self-joins.
Joins
- A theta join R ⋈θ S equals σθ(R × S) for any condition θ.
- An equi-join is a theta join where θ uses only equality.
- A natural join matches on all common attribute names and keeps one copy of each shared attribute.
- A left outer join keeps every tuple of R. It fills missing S attributes with NULL.
Exercise
R(A, B) = {(1, 2), (1, 3)} and S(B, C) = {(2, 5)}. What is R natural join S?
Show answer
{(1, 2, 5)}. Only the tuple with B = 2 matches. The tuple (1, 3) has no partner in S, so it is dropped.