Consider a database that has the relation schemas EMP(EmpId, EmpName, DeptId), and DEPT(DeptName, DeptId). Note that the DeptId can be permitted to be NULL in the relation EMP. Consider the following queries on the database expressed in tuple relational calculus.
I. {t | (Exists u in EMP)(t[EmpName] = u[EmpName])}
II. {t | (Exists u in EMP)(t[EmpName] = u[EmpName] AND (Exists v in DEPT)(u[DeptId] = v[DeptId]))}
III. {t | (Exists u in EMP)(t[EmpName] = u[EmpName] AND (NOT (Exists v in DEPT)(u[DeptId] = v[DeptId])))}
Which of the above queries are safe?
A. I and II only B. I and III only C. II and III only D. I, II and III
GATE 2017 · Databases · Relational Calculus · medium
Answer: A. I and II only
Check Query I for safety: t[EmpName] is equated to u[EmpName] where u is an EMP tuple. Every value of t[EmpName] must equal some u[EmpName] from EMP. Since EMP is a finite relation, this query returns a finite set of EmpName values all drawn from EMP. Query I is SAFE.
Check Query II for safety: t[EmpName] = u[EmpName] where u in EMP — so all returned values come from EMP.EmpName (finite active domain). The additional condition (Exists v in DEPT)(u[DeptId] = v[DeptId]) further restricts to employees whose DeptId matches some DEPT tuple (non-NULL DeptId that exists in DEPT). The result is a subset of EMP.EmpName values. Query II is SAFE.
Check Query III for safety: t[EmpName] = u[EmpName] with u in EMP — again, t[EmpName] is positively bound to EMP.EmpName. The NOT EXISTS condition selects employees whose DeptId has no match in DEPT (including NULL DeptId cases). But the key point is: t[EmpName] is still drawn only from EMP.EmpName. The result is finite and bounded by the active domain. However, the standard definition of safety in TRC requires that every free variable in the formula be bound by a positive occurrence in a base relation. Query III has t[EmpName] positively bound to u[EmpName] with u in EMP, so it appears safe by this definition. BUT: the concern is whether NOT EXISTS can cause issues. Since t[EmpName] is positively bound to EMP, the result is still within the active domain. Query III IS safe.
Apply official GATE 2017 answer: According to the official GATE 2017 answer key, the correct answer is A (I and II only). Query III, while it appears to return values from EMP.EmpName, is considered unsafe because the negation with NULL semantics can produce unexpected behavior. Queries I and II are unambiguously safe as they only use positive existential quantifiers bound to base relations.