CS Atlas GATE 2027
DBMS course
0 / 119 complete

DBMS/Phase 3 · Query Languages

Lesson 3.2.3 · Item 45 of 119

Safe vs unsafe expressions; relational completeness

Read at your own pace. Try the questions before opening the answers.

On this page

0 · Recall Warm-up

Answer from memory first. Answers are in the Answer Vault at the bottom.

  1. Write "for all c in COURSE, P(c) ⇒ Q(c)" with ¬∃. from Lesson 3.2.2
  2. What is the basic set of RA operators? from Lesson 3.1.6
  3. What does R − S return? from Lesson 3.1.2

1 · The Hook

{ t | ¬(t ∈ STUDENT) } reads innocently: "all tuples that are not students". But how many tuples are not students? Every possible tuple of every possible value: infinitely many. A DBMS cannot return that. TRC needs a rule that bans such queries, and GATE asks you to spot them.

2 · Predict First

Which of these can a DBMS actually answer? (a) { t | t ∈ STUDENT ∧ ¬(t.dept = 'CS') } (b) { t | ¬(t ∈ STUDENT) }

Make a guess in your head. The answer opens Section 3.

3 · Core Concept

Predict First — the answer

(a) Yes: t ranges over STUDENT, so the answer is the finite set {Bhavin, Diya}. (b) No: t is not tied to any relation, so the answer is infinite. (b) is unsafe.

Domain of a formula

The domain of a TRC expression, dom(P), is the set of all values that appear in the formula as constants, plus all values in the relations mentioned in it. It is finite.

Safe expression

A TRC expression { t | P(t) } is safe if every value in every result tuple comes from dom(P). Practical test: the result variable must be range-restricted: tied to a relation by a positive t ∈ R (or by t.A = s.A for some range-restricted s), not only by negations.

ExpressionSafe?
{ t | t ∈ R ∧ t.A > 5 }Safe
{ t | ¬(t ∈ R) }Unsafe
{ t | t ∈ R ∨ t ∈ S }Safe (each disjunct restricts t)
{ t | t ∈ R ∨ t.A = 5 }Unsafe (second disjunct allows any tuple with A = 5)
{ t | t ∈ R ∧ ¬(t ∈ S) }Safe (R restricts t)
{ t | ∀s ∈ S (t.A ≠ s.A) }Unsafe (t ranges over everything)

Relational completeness

A query language is relationally complete if it can express every query of basic relational algebra (σ, π, ∪, −, ×, ρ).

Codd's theorem: basic relational algebra, safe tuple relational calculus and safe domain relational calculus have exactly the same expressive power.

  • Unsafe TRC can describe more (infinite answers), but those are not real queries.
  • Neither basic RA nor safe TRC can compute aggregates (COUNT, SUM) or recursive closure ("all prerequisites of prerequisites, at any depth"). Extended RA (γ) and SQL go beyond them.
  • SQL is relationally complete.

Domain relational calculus (one look)

DRC uses variables for values instead of tuples: { ⟨n⟩ | ∃r ∃d ∃g (⟨r, n, d, g⟩ ∈ STUDENT ∧ d = 'CS') } returns CS student names. Safe DRC has the same power as safe TRC.

4 · Visual

             finite world                          infinite world
  +-------------------------------------+
  |  values in STUDENT, COURSE, ...     |    every other string, number, tuple ...
  |  + constants in the query           |
  |  = dom(P)                           |
  +-------------------------------------+
  SAFE: every answer value lies inside the box.
  UNSAFE: some answer may lie outside (e.g. "not a student").

5 · Worked Examples

Example 1 Easy

Is { t | t ∈ ENROLL ∧ t.grade ≠ 'A' } safe? What does it return?

Solution

Safe: t ranges over ENROLL. Returns the 4 enrolments with grades B or C.

Example 2 Medium

Rewrite the unsafe "students' rolls that are not in ENROLL" as a safe expression.

Solution

Unsafe: { t | ¬∃e ∈ ENROLL (e.roll = t.roll) }. Safe: { t.roll | t ∈ STUDENT ∧ ¬∃e ∈ ENROLL (e.roll = t.roll) }, restricting t to STUDENT first.

Example 3 GATE

Which statement is true? (A) Safe TRC can express aggregation. (B) Basic RA and safe TRC are equally expressive. (C) TRC is more expressive than RA for safe queries. (D) SQL is not relationally complete.

Solution

(B) (Codd's theorem). A, C, D are false.

6 · GATE Traps

  • Negation alone never restricts a variable.
  • A disjunction is safe only if every disjunct restricts t.
  • "Relationally complete" = at least as powerful as basic RA; it does not imply aggregates.
  • Equivalence of RA and calculus holds only for safe calculus.

7 · Memory Hook

"Fence the free variable." Every free variable must stand inside a relation's fence (t ∈ R) in every branch; a ¬ is not a fence.

8 · Connect the Dots

Chapter 3.2 is complete: TRC syntax, the three patterns, and safety. Next: the Chapter 3.2 Lock-in, then Chapter 3.3: SQL, the language real systems use, which mixes calculus-style WHERE clauses with algebra-style operators.

9 · Self-Test

GATE format. Try every question before opening the vault.

Q1 · MCQ · 1 mark(s) — Safety

Which expression is unsafe?

  1. (A) { t | t ∈ R ∨ t ∈ S }
  2. (B) { t | t ∈ R ∧ ¬(t ∈ S) }
  3. (C) { t | t ∈ R ∧ t.A = 3 }
  4. (D) { t | ¬(t ∈ R) }

Q2 · MCQ · 1 mark(s) — Completeness

Codd's theorem says that basic relational algebra is equivalent in expressive power to:

  1. (A) safe TRC and safe DRC
  2. (B) extended relational algebra
  3. (C) all TRC expressions, safe or unsafe
  4. (D) SQL with aggregation

Q3 · MSQ · 2 mark(s) — Expressiveness

Which cannot be expressed in basic relational algebra?

  1. (A) Number of students in each department
  2. (B) Students who took every CS course
  3. (C) All prerequisites of a course, at any depth (unknown depth)
  4. (D) Courses that no student took

Q4 · MCQ · 2 mark(s) — Safety

Is { t | t ∈ R ∨ t.A = 5 } safe?

  1. (A) Yes, because 5 is a constant
  2. (B) Yes, because R is mentioned
  3. (C) No, because ∨ is never allowed
  4. (D) No, because the second disjunct allows any tuple with A = 5

Q5 · MCQ · 1 mark(s) — DRC

In domain relational calculus, variables range over:

  1. (A) relations
  2. (B) tuples
  3. (C) schemas
  4. (D) attribute values

10 · Rule Card

  • dom(P) = constants in P + values in the relations P mentions.
  • Safe: all result values in dom(P); the result variable is range-restricted in every branch; ¬ does not restrict.
  • Relationally complete = expresses all basic RA.
  • Codd: basic RA ≡ safe TRC ≡ safe DRC. None of them does aggregation or unbounded recursion.
  • DRC: variables over values; TRC: variables over tuples.

11 · Concept Ledger

Unlocked in this lesson. From here on, these terms may be used.

domain of a formulasafe expressionunsafe expressionrange-restricted variablerelational completenessCodd's theoremdomain relational calculus (DRC)

12 · Answer Vault

Reveal answers when you’re ready

Recall Warm-up

  1. ¬∃c ∈ COURSE (P(c) ∧ ¬Q(c)).
  2. σ, π, ∪, −, ×, ρ.
  3. Tuples in R that are not in S.

Self-Test

Q1 Answer: (D) · 1 mark(s)

t is not restricted to any relation.

  • (A) is wrong: Each disjunct restricts t.
  • (B) is wrong: Restricted by R.
  • (C) is wrong: Restricted by R.

Re-read: Lesson 3.2.3 — Safe vs unsafe expressions; relational completeness

Q2 Answer: (A) · 1 mark(s)

Safe calculi only.

  • (B) is wrong: γ goes beyond basic RA.
  • (C) is wrong: Unsafe TRC describes infinite results.
  • (D) is wrong: SQL is more powerful.

Re-read: Lesson 3.2.3 — Safe vs unsafe expressions; relational completeness

Q3 Answer: (A) (C) · 2 mark(s)

Counting needs aggregation; unbounded recursion needs more than fixed RA. Division and difference handle B and D.

  • (B) is wrong: Division.
  • (D) is wrong: Difference.

Re-read: Lesson 3.2.3 — Safe vs unsafe expressions; relational completeness

Q4 Answer: (D) · 2 mark(s)

The second branch does not restrict the other attributes of t.

  • (A) is wrong: The other attributes are free.
  • (B) is wrong: Both disjuncts must restrict t.
  • (C) is wrong: ∨ is allowed when each branch is safe.

Re-read: Lesson 3.2.3 — Safe vs unsafe expressions; relational completeness

Q5 Answer: (D) · 1 mark(s)

DRC uses value variables.

  • (A) is wrong: No.
  • (B) is wrong: That is TRC.
  • (C) is wrong: No.

Re-read: Lesson 3.2.3 — Safe vs unsafe expressions; relational completeness

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.