Relational calculus
Relational Calculus in non-procedural query language and has no description regarding how the query can work or the data will be fetched. It uses mathematical functional calculus. It only focusses on what to do, and not on how to do it. Relational Calculus exists in two forms-:
- Tuple Relational Calculus (TRC) was originally proposed by Codd in the year 1972
- Domain Relational Calculus (DRC) was proposed by Lacroix and Pirotte in the year1977
Tuple Relational Calculus (TRC)
A tuple relational calculus may be a non-procedural query language that specifies to select the tuples during a relation and work on filtering tuples based on the given condition. You may have to find tuples that a predicate is true. The calculus is dependent on the use of tuple variables.
Syntax: {T| P (T)} or {T | Condition (T)}
Where T is the tuple variable and p (T) is condition used to fetch T.
Example-: {T | TEACHERS (T) and T.SALARY>20,000} - implies that it selects the tuples from TEACHERS relation such that resulting TEACHERS tuples will have salary greater than 20,000. It is example of selecting a range of values.
{T | TEACHERS (T) AND T.DEPTMENT_NO= 20} – This select all the tuples of TEACHERS name who work for Department 10.
The variable which is used in the condition is termed as the Tuple variable. In given example T.SALARY and T.DEPTMENT_NO are tuple variables. In the first example above, we have specified the condition T.SALARY >20,000. What is the meaning of it? For all the SALARY>20,000, display the employees. Here the SALARY is termed as bound variable. Any tuple variable with ‘For All’ (?) or ‘there exists’ (?) condition is called bound variable. Here, for any range of values of SALARY greater than 20,000, the meaning of the condition remains the same. Bound variables are those ranges of tuple variables whose meaning will not change if the tuple variable is replaced by another tuple variable.
In the second example, we used DEPTMENT_NO= 20. That means display employee details whose DEPTMENT_NO = 20. Any tuple variable without any ‘For All’ or ‘there exists’ condition is called Free Variable. All the conditions used in the tuple expression are known as well-formed formula (WFF). All the conditions in the expression are combined by using logical operators like AND, OR and NOT, and qualifiers like ‘For All’ (?) or ‘there exists’ (?). If the tuple variables are all bound variables in a WFF is called closed WFF. In an open WFF, there are at least one free variable.
Domain Relational Calculus
Domain relational calculus use the list of attribute and variables to be selected from the relation based on the condition. It is similar as TRC, but only different thing is that it selecting the attributes rather than selecting whole tuples.
It is denoted as below:
{< a1, a2, a3,… an > | P(a1, a2, a3, … an)}
Where a1, a2, a3, an are attributes and variables of the relation
Where P is the condition.
For example-: select STU_ID and STU _NAME of Student who study department 10
{< STU _ID, STU _NAME> | < STU _ID, STU _NAME>? STUDENT Λ DEPT_ID = 10}
Get name of the department name that Tammy study in.
{STU _NAME |< STU _NAME >? DEPT Λ? DEPT_ID (<DEPT_ID>? STUDENT Λ STU _NAME = Tammy)}