UNIHIVE

Study Hub UNIPORT CSC 480

CSC 480 · Database Management

Relational calculus

The non-procedural query languages of the relational model: tuple relational calculus, domain relational calculus, quantifiers, safe expressions, relational completeness and query-by-example.

Our notes

2 min read

Idea

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):

  1. 400-level students: { s | Student(s) ∧ s.Level = 400 }
  2. Their names only: { s.FirstName, s.Surname | Student(s) ∧ s.Level = 400 }
  3. 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

AlgebraCalculus
Styleprocedural (how)declarative (what)
Basisoperations on relationspredicate logic
Used byquery optimisersquery 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

Watch

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.

All CSC 480 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.