Pages

Friday 12 November 2021

Relational Calculus.


Relational Calculus in non-procedural query language and has no description about how the query will work or the data will be fetched. It only focuses on what to do, and not on how to do it. It is contrary to Relational Algebra which is a procedural query language to fetch data.
Relational Calculus exists in two forms:
Tuple Relational Calculus (TRC)
Domain Relational Calculus (DRC)

Tuple Relational Calculus (TRC)
In tuple relational calculus, we work on filtering tuples based on the given condition.
Syntax:
{ T | Condition }

In this form of relational calculus, we define a tuple variable, specify the 
table(relation) name in which the tuple is to be searched for, along with a condition. We can also specify column name using a . dot operator, with the tuple variable to only get a certain attribute(column) in result.A tuple variable is nothing but a name, can be anything, generally we use a single alphabet for this, so let's say T is a tuple variable.To specify the name of the relation(table) in which we want to look for data, 
we do the following:
Relation(T), where T is our tuple variable.

Ex: if our table is Student, we would put it as Student(T)Then comes the condition part, to specify a condition applicable for a particular attribute(column), we can use the . dot variable with the tuple variable to specify it, like in table Student, if we want to get data for students with age greater than 17, then we can write it as,
T.age > 17, where T is our tuple variable.
Putting it all together, if we want to use Tuple Relational Calculus to fetch names of students, from table Student, with age greater than 17, then, for T being our tuple variable,

Ex1:
Fetch names of students, from table Student, with age greater than 17
T.name | Student(T) AND T.age > 17

Ex2:
Returns tuples with 'name' from Author who has written article on 'database'.
{ T.name | Author(T) AND T.article = 'database' }

Domain Relational Calculus (DRC)
In domain relational calculus we use list of attribute to be selected from the relation based on the condition. It is same as TRC, but differs by selecting the attributes rather than selecting whole tuples. It is denoted as below:
Syntax:
{ c1, c2, c3, ..., cn | F(c1, c2, c3, ... ,cn)}

Domain relational calculus uses the same operators as tuple calculus. It uses logical connectives ∧ (and), ∨ (or) and ┓ (not).Where c1, c2, c3, … cn are attributes of the relation and F is the condition and F defines the formula including the condition for fetching the data.

Ex1: 
{< name, age > | ∈ Student ∧ age > 17}
The above query will return the names and ages of the students in the table Student whose age is greater than 17.

Ex2: 
{< article, page > |∈ Oracle ∧ topic=’CDB’}
The above query will return the article and page columns of Oracle table where topic is ‘CDB’.

Ex3:
{< article, page, subject > | ∈ books ∧ subject = 'database'}
The above query will return the article, page, and subject from the relation books, where the subject is a database

No comments:

Post a Comment

Conflict Serializability

Find out conflict serializability for the given transactions T1 T2 T3 R(X)     ...