CSC 480 · Database Management
Our notes
2 min readIdea
Relational calculus is declarative: it describes what the result must satisfy, not how to compute it. It comes from first-order predicate logic. SQL is based largely on tuple calculus.
Tuple relational calculus (TRC)
Form: { t | P(t) }: the set of tuples t for which predicate P is true.
Variables range over tuples of a relation; t.A is attribute A of t.
Examples (running database):
- 400-level students: { s | Student(s) ∧ s.Level = 400 }
- Their names only: { s.FirstName, s.Surname | Student(s) ∧ s.Level = 400 }
- Students who registered CSC 480:
{ s | Student(s) ∧ ∃r (Registration(r) ∧ r.MatNo = s.MatNo ∧ r.CourseCode = 'CSC 480') }
Quantifiers
- ∃ (there exists): at least one tuple satisfies the condition (maps to SQL EXISTS or a join).
- ∀ (for all): every tuple satisfies it (expressed in SQL as NOT EXISTS ... NOT EXISTS).
Example with ∀ (division): students who registered every CSC course:
{ s | Student(s) ∧ ∀c (Course(c) ∧ c.DeptCode = 'CSC' → ∃r (Registration(r) ∧ r.MatNo = s.MatNo ∧ r.CourseCode = c.CourseCode)) }
Useful identity: ∀x P(x) ≡ ¬∃x ¬P(x).
Domain relational calculus (DRC)
Variables range over attribute values (domains) instead of whole tuples.
{ ⟨m, f, l⟩ | ∃s ∃d (Student(m, f, s, l, d) ∧ l = 400) }: MatNo, first name and level of 400-level students.
DRC is the basis of Query-By-Example (QBE), where users fill in a grid (the design view in Microsoft Access works like this).
Safe expressions
An expression is safe if it is guaranteed to return a finite result made only of values in the database. { t | ¬Student(t) } is unsafe: infinitely many tuples are "not students". Safe expressions limit variables to relations in the database.
Relational completeness
Codd showed that safe relational calculus and relational algebra have the same expressive power. A query language that can express every relational algebra query is called relationally complete. SQL is relationally complete (and goes beyond, with aggregates, ordering and recursion).
Algebra versus calculus
| Algebra | Calculus | |
|---|---|---|
| Style | procedural (how) | declarative (what) |
| Basis | operations on relations | predicate logic |
| Used by | query optimisers | query languages (SQL, QBE) |
Exams usually ask: write TRC and DRC expressions for English queries; explain ∃ and ∀ with examples; safe expressions; relational completeness; compare algebra and calculus.
Common mistake: forgetting to bind every variable to a relation. Each tuple variable needs a range condition like Student(s), or the expression becomes unsafe.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Tuple relational calculus
(opens en.wikipedia.org in a new tab)
Wikipedia, Tuple relational calculus, CC BY-SA 4.0 · licence
-
Domain relational calculus
(opens en.wikipedia.org in a new tab)
Wikipedia, Domain relational calculus, CC BY-SA 4.0 · licence
-
Relational calculus
(opens en.wikipedia.org in a new tab)
Wikipedia, Relational calculus, CC BY-SA 4.0 · licence
Watch
-
Best pick Tuple Relational Calculus (Formal Definition)
13 min · ≈ 59 MB · about ₦33
(opens youtube.com in a new tab)
-
Tuple Relational Calculus (Example Queries)
17 min · ≈ 77 MB · about ₦43
(opens youtube.com in a new tab)
-
Lec-58: Tuple Calculus in DBMS with examples
16 min · ≈ 72 MB · about ₦40
(opens youtube.com in a new tab)
-
CMU Database Systems - 02 Relational Algebra (Fall 2017)
1 h 21 min · ≈ 365 MB · about ₦205
(opens youtube.com in a new tab)
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.