You are on page 1of 5

Relational Calculus

 Retrieval of data
 Non procedural Language
a. Specifies what is to be retrieved rather how to retrieve.
 Two types
o Tuple Rel. Cal.
o Domain Rel. Cal.

Tuple Relational Calculus

 Based on specifying a number of tuples variables


 A general form is
o { t | COND(t)}
ohere t is a tuple variable and COND(t) is conditional expression on t. All tuples t that
satisfy COND(t) will appear in result set.
o T contains all rows and columns of table specified in condition
o COND(t) is known as condition or formula.
 Example
o Display details of student Tina
 RA: σ name=’Tina’ (STUDENT)…. How to reterive data … using select
 Relational calculus: { t | STUDENT(t) AND t.Name=’Tina’}
o Display address of Ram
 A <- σ name=’Ram’ (STUDENT) /// A contain alldetails of student ram
 B< -π address (A)
 Relational Calculus {t.Address | STUDENT(t) and t.name=’Ram’}
o Display Name and roll number of student who are 19 years old and from Chennai
 A <- σ age=19 and city=’chennai (STUDENT))
 B<-Pi name,rno (A)
 {a.Name, a.RollNo | STUDENT(a) AND a.age=19 AND a.city=’Chennai’}
 The two quantifiers Universal Quantifier (Ɐ) and existential quantifier(ⱻ) can appear in
formulas.
o Example:
 List name and roll number of all student who score 50 in DBMS paper
 A <- STUDENT join MARKS
 B <- σ dbms=50 (A)
 C <- π name,rollno(B)

 {t.Name, t.RollNo | STUDENT(t) AND (ⱻm) (MARKS(m) AND


m.dbms=50}
Domain Relational Calculus

 In Domain calculus, instead of having one variable over tuples, the variable range over single
values from domain of attributes.
 The general form is {x1, x2, x3, …… xn| COND(x1, x2, x3, …… xn, xn+1, xn+2,….., xn+m)}
 Example: Consider a relation STUDENT with five attributes RollNo, Name, Age, Gender and
City

o Display details of student Tina
 { a,b,c,d,e| (STUDENT(a,b,c,d,e) AND b=’Tina’)}
 { a,b,c,d,e| (STUDENT(a,’Tina’,c,d,e) }

o Display rollno and age of tina
 {a,b |Student(a,’Tina’,b,c,d,e)}
o Display Name and roll number of student who are 19 years old and from Chennai
 {a,b | (ⱻc) (ⱻd) (ⱻe) STUDENT(a,b,c,d,e) AND c=19 AND e=’Chennai’}
EMPLOYEE(SSNo, FirstName, LastName, BDate, Salary, Gender,DeptNo)

DEPARTMENT(DeptNo,DeptName,Location)

RELATIONALCALCULUS

{t | COND(t)} …. Tuple calculus ,,,, t is a variable containg all rows and col

Domain calculus …. Insetad of taling one variable for entire table ,, use different variables for each
attribute

1. Display name and gender of each employee


a. TC: {t.FirstName,t.LastName,t.gender | EMPLOYEE(t)}
b. DC: {b,c,f | EMPLOYEE (a,b,c,d,e,f,g)
2. Display department names …..
a. TC: {a.DeptName | DEPARTMENT(a)}
b. DC: { b | DEPARTMENT(a,b,c)}
3. Display name of department located in Chennai
a. TC: {d.deptName | DEPARTMENT(d) AND d.location=’Chennai’}
b. DC{ y|DEPARTMENT(x,y,z) AND z=’Chennai’}
c. DC: {y | DEPARTMENT(x,y,’Chennai’)}

EMPLOYEE(SSNo, FirstName, LastName, BDate, Salary, Gender,DeptNo)

DEPARTMENT(DeptNo,DeptName,Location)

4. Display names of female employee


a. TC: {t.FirstName,t.LastName | EMPLOYEE(t) and t.gender=’Female’}
b. DC: { b,c| EMPLOYEE(a,b,c,d,e,f,g) and f=’female’}
c. {b,c | EMPLOYEE(a,b,c,d,e,’Female’,g)}
d.
5. Display name of employee from Chennai?
a. TC{t.FirstName, t.LastName | Employee(t) AND (ⱻm) (DEPARTMENT(m) AND
m.deptName=’Chennai’}
1. Display name and gender of each employee

Tuple Relation Calculus: {a.FirstName, a.LastName, a.Gender | EMPLOYEE(a)}

Domain Relational Calculus : {b,c,f | EMPLOYEE(a ,b,c,d,e,f,g)}

2. Display department names

Tuple Rel. Cal. {d.deptname | DEPARTMENT(d)}

Domin Rel. Cal. { b | DEPARTMENT(a,b,c)}

Display department names located in Chennai

Tup. Rel. Cal. {d.deptname | DEPARTMENT(d) AND d.deptname=’Chennai’}

Domain. Rel. Cal. { b |DEPARTMENT(a,b,c) AND c=’Chennai’}

{ b | DEPARTMENT (a,b,’Chennai’)}

Display names of employee who are male and born in the year 1990.

{ e.FirstName, e.LastName | EMPLOYEE(e) and e.gender=’male’ and e.bdate=1990}

{ b,c| EMPLOYEE(a,b,c,d,e,f,g)AND f.gender=’male’ and d=1990 }

{ b,c| EMPLOYEE(a,b,c,1990,e,’Male’,g) }

Display salary of Ram and Sita.

{ a.salary | EMPLOYEE(a) AND e.firstname=’Ram’ OR e.firstname=’Sita’}

Display name of employees working in Chennai.

{e.firstname,e.lastname | EMPLOYEE(e) AND (ⱻd)(DEPARTMENT(d) AND d.location=’Chennai’)}4

Display employee name and department name of all employees

{e.firstname,e.lastname, d.deptname | EMPLOYEE(e) AND (Ɐ d)(DEPARTMENT(d) }


Display location of Ram

{a.location|DEPARTMENT(a) AND (ⱻd) (EMPLOYEE(d) AND d.firstName=’Ram’)}

You might also like