Relational Model and Relational Algebra
7 pages · ~5 min✓ Reviewed
Builds on SQL Basics: DDL, DML and Joins. Next up: ER Model and Normalization.
Relational Model and Keys
1. Relation and Domain
- Relation = set of tuples (rows) over a schema of attributes (columns)
- It is a set: no duplicate tuples, row order irrelevant
- Domain = allowed atomic values, e.g. age: integers 0-150
- Degree = number of attributes; cardinality = number of tuples
2. Kinds of Keys
- Super keyuniquely identifies
- Candidate keyminimal super key
- Primary keychosen, no NULLs
| Key | Meaning | Student(id, email, name) |
|---|---|---|
| Super key | any unique attribute set | {id, name} |
| Candidate key | drop any attribute, uniqueness lost | id, email |
| Primary key | one chosen candidate | id |
| Alternate key | remaining candidates | email |
3. Foreign Key
- Foreign key references a candidate/primary key, in another or the same relation
- Value must match an existing key or be NULL (referential integrity)
sql
CREATE TABLE Employee ( id INT PRIMARY KEY, email TEXT UNIQUE, boss_id INT REFERENCES Employee(id) );
boss_id points back into the same relation
Remember
super key ⊇ candidate key ⊇ primary key. A foreign key points to a key, never to an arbitrary column.
Selection, Projection and Renaming
1. Selection and Projection
| σ_cond(R) | π_A,B(R) | |
|---|---|---|
| Keeps | rows matching the predicate | only listed columns |
| Schema | same as R | only A, B |
| Size | never larger than R | duplicates removed, may shrink |
- Relations are sets, so π always dedupes rows
2. Composition Order
- 1Rname, age
- 2σ age>20schema kept
- 3π nameresult
π_name(σ_age>20(R))worksσ_age>20(π_name(R))fails: age was projected away- Selections commute:
σ_c1(σ_c2(R)) = σ_c2(σ_c1(R)) = σ_c1∧c2(R)
3. Rename ρ
ρ_S(A→B)(R)renames the relation to S and attribute A to B
Set Operations and Cartesian Product
1. Set Operations
R ∪ S,R ∩ S,R − Sneed union-compatible relations- Same degree and matching domains per column
- Result takes attribute names from R
R − S ≠ S − R: difference is not commutativeR ∩ S = R − (R − S): intersection is derived
2. Cartesian Product
R × S | Value |
|---|---|
| Pairs | every tuple of R with every tuple of S |
| Degree | deg(R) + deg(S) |
| Cardinality | |R| · |S| |
- R × Sall pairs
- σ keys matchdrop junk pairs
- Joinmeaningful
- Product alone gives meaningless pairs; follow it with a selection
3. 'Not In' Queries
text
π_id(Customer) − π_cust_id(Order)
customers who never ordered
Joins and Division
1. Theta, Equi, Natural
- Theta join R ⋈_θ S = σ_θ(R × S)
- Equi-join: theta join with only
=conditions - Natural join ⋈: equates same-named attributes, keeps one copy each
2. Inner vs Outer
| Join | Symbol | Keeps |
|---|---|---|
| Inner | ⋈ | matches only |
| Left | ⟕ | all of R, NULL-padded |
| Right | ⟖ | all of S, NULL-padded |
| Full | ⟗ | all of both, NULL-padded |
- No common attributes: natural join becomes a Cartesian product
- Accidentally shared column names: silently joins on the wrong columns
3. Division
- R ÷ S: values of attrs(R) − attrs(S) paired with every tuple of S
- The 'for all' query: students who took all required courses
How SQL Maps to Algebra
1. Clause to Operator
| SQL | Algebra |
|---|---|
SELECT list | projection π |
WHERE | selection σ |
FROM R, S | product × |
JOIN ... ON | theta join ⋈_θ |
UNION / INTERSECT / EXCEPT | ∪ / ∩ / − |
2. Order and Bags
- FROMR × S
- WHEREσ
- SELECTπ
- Algebra mirrors it: π(σ(R × S))
- SQL tables are bags; algebra relations are sets
SELECTkeeps duplicates; addDISTINCTto match πUNION,INTERSECT,EXCEPTdrop duplicates;ALLkeeps them
3. Division in SQL
- No division operator in SQL
Relational Algebra Cheat Sheet
1. Operators
| Op | Meaning |
|---|---|
| σ | select rows |
| π | project columns |
| ρ | rename |
| ∪ | union |
| − | difference |
| × | product |
| ⋈ | join |
| ÷ | division |
- Fundamental: σ π ∪ − × ρ
- Derived: intersection, joins, division
R ∩ S = R − (R − S)
2. Keys and Compatibility
- Super keyunique
- Candidate keyminimal
- Primary keychosen, no NULL
- Foreign keypoints to a key
- Union compatible: same degree and same domains per column
Check yourself
Which fails, and why? A: π_name(σ_age>20(R)) B: σ_age>20(π_name(R))
- B fails.
π_namedropsage, so the selection has noageto test.
Takes(student, course) holds (Asha, DB), (Asha, OS), (Asha, Net), (Ravi, DB). Required = {DB, OS}. What is Takes ÷ Required?
- Only {Asha}.
- Ravi has DB but lacks OS.
Customer has cities Pune, Pune, Delhi. How many rows do π_city(Customer) and SELECT city FROM Customer return?
- Algebra returns 2, because projection removes duplicates.
- SQL returns 3, because tables are bags. Add
DISTINCTto get 2.