A tuple relational calculus equivalent for the given SQL query

relational-calculusrelational-theory

Well, here's a SQL query:

select Employee.Name,Department.Name
from Employee,Department
where Employee.Dept_no=Department.No

I have tried a tuple relational calculus on it as:

{t|Ǝ s ∈ Employee (s[Name]=t[Name] ^ 
Ǝ u ∈ Department(u[Name]=s[Name]^u[No]=s[No]))}

Is my approach correct? if not, then can anyone help me understand?

Best Answer

A tuple relational calculus expression should be written with the usual language of first order logic.

In this case, for instance, you could write:

{ [e.Name, d.Name] | Ǝ e ∈ Employee, Ǝ d ∈ Department . e.Dept_no = d.No }

(a part from minor variations on the notation used, like “:” instead of “.”).

The idea is that you obtain the result of the query (a well formed formula of a first order logic), by finding the elements of the relations that, substituted to the free variables of the formula, make the formula true.

In this case, we have two variables, d and e, that range on Departments and Employees respectively, a condition that must be true (e.Dept_no = d.No), and from every pair of tuples of the relations that satisfy this condition, you add to the resulting set a tuple formed by the Name of e and the Name of d (in other words you perform a simple join).

Note that the structure of the expression can be easily mapped to the SQL query (and actually this language is the origin of the SQL language, together with other additions). This can be a suggestion on how to write other queries similar to this one.