DBMS/Lesson 3 of 9/5 min read
Relational Model, Keys and Relational Algebra
Relations, tuples and domains, all types of keys, integrity constraints, relational algebra operators, joins in algebra, and relational calculus.
On this page
Relational Model
The relational model (E. F. Codd, 1970) stores data in relations (tables).
| Term | Informal name |
|---|---|
| Relation | Table |
| Tuple | Row / record |
| Attribute | Column / field |
| Domain | Set of allowed values for an attribute |
| Degree | Number of attributes (columns) |
| Cardinality | Number of tuples (rows) |
| Relation schema | Table definition, e.g. Student(id, name, dept) |
Relation
A relation is a set of tuples. Properties:
- No duplicate tuples (a set). SQL tables can have duplicates unless a key prevents it.
- Order of tuples and attributes does not matter.
- Every value is atomic (first normal form).
Tuple
A tuple is one row: an ordered list of values, one per attribute.
Keys
Example table Student(roll_no, email, aadhaar, name, dept).
Super Key
A super key is any set of attributes that uniquely identifies a tuple. {roll_no}, {roll_no, name}, {email, dept} are all super keys. A table with n attributes can have many super keys.
Candidate Key
A candidate key is a minimal super key: remove any attribute and it stops being unique. Here: {roll_no}, {email}, {aadhaar}. Attributes in some candidate key are prime attributes; others are non-prime (important for normalization).
Primary Key
The primary key is the candidate key chosen by the designer to identify rows. It must be unique and NOT NULL. Only one per table.
Alternate Key
Candidate keys not chosen as primary key: email, aadhaar. Usually enforced with UNIQUE constraints.
Foreign Key
A foreign key is an attribute in one table that refers to the primary key (or a unique key) of another table, creating a link.
CREATE TABLE enrollment (
roll_no INT REFERENCES student(roll_no) ON DELETE CASCADE,
course_id INT REFERENCES course(id)
);A foreign key can be NULL (unless declared NOT NULL) and can repeat.
Composite Key
A key made of two or more attributes: (roll_no, course_id) in an enrollment table.
Surrogate Key
An artificial key with no business meaning, such as an auto-increment id or UUID. Stable even if business data changes. The opposite is a natural key (email, ISBN).
Every candidate key is a super key, but not every super key is a candidate key. Primary key is one chosen candidate key.
Integrity Constraints
Rules that keep data valid:
- Domain constraint: values come from the attribute's domain (type,
CHECK (age >= 18)). - Entity integrity: primary key cannot be NULL.
- Key constraint: key values are unique.
- Referential integrity: a foreign key value must match an existing primary key value in the referenced table, or be NULL.
Referential Integrity actions
When a referenced row is deleted or updated:
| Option | Effect |
|---|---|
RESTRICT / NO ACTION | Reject the change |
CASCADE | Delete or update the referencing rows too |
SET NULL | Set the foreign key to NULL |
SET DEFAULT | Set it to the default value |
Relational Algebra
Relational algebra is a procedural query language: each operator takes relations and returns a relation. SQL queries are translated into algebra trees inside the optimizer.
Basic operators
| Operator | Symbol | Meaning | SQL equivalent |
|---|---|---|---|
| Selection | σ | Filter rows by condition | WHERE |
| Projection | π | Choose columns, remove duplicates | SELECT DISTINCT cols |
| Union | ∪ | Rows in either relation | UNION |
| Set difference | − | Rows in R but not S | EXCEPT / MINUS |
| Cartesian product | × | Every pair of rows | CROSS JOIN |
| Rename | ρ | Rename relation or attributes | AS |
Union and difference need union-compatible relations: same number of attributes with matching domains.
Example: names of CS students:
π name ( σ dept = 'CS' (Student) )Derived operators
- Intersection
R ∩ S = R − (R − S). - Theta join
R ⋈θ S = σθ (R × S): join with any condition. - Equi join: theta join where the condition is only equality.
Natural Join
Natural join R ⋈ S joins on all attributes with the same name and keeps one copy of them. If no common attributes, it becomes a Cartesian product.
Outer joins keep unmatched tuples, padding with NULL: left outer (⟕), right outer (⟖), full outer (⟗). See SQL joins.
Division Operator
Division R ÷ S answers "for all" queries: find x in R related to every y in S.
Example: students who took all courses:
Enrolled(student, course) ÷ Course(course)Expressed with basic operators:
R ÷ S = π X (R) − π X ( (π X (R) × S) − R )Relational Calculus
Relational calculus is a non-procedural (declarative) language: you say what you want, not how.
- Tuple relational calculus (TRC):
{ t | t ∈ Student ∧ t.dept = 'CS' }. - Domain relational calculus (DRC): variables range over attribute values.
Codd's theorem: relational algebra and safe relational calculus have the same expressive power. A language that can express all of them is called relationally complete; SQL is.
Quick revision
- Super key ⊇ candidate key (minimal) ⊇ primary key (chosen, not null).
- Foreign key enforces referential integrity; can be NULL.
- σ filters rows, π picks columns, ⋈ joins, ÷ answers "for all".
- Algebra is procedural, calculus is declarative, both equally expressive.