0% found this document useful (0 votes)
46 views19 pages

Lect 11 Relational Calculus

This document provides an overview of relational calculus. It discusses tuple relational calculus and how it is based on first-order logic and uses tuple variables. Examples are provided to demonstrate how to write queries using selection, projection, and joins. The use of existential and universal quantifiers in queries is explained. Practice queries with solutions are also included to illustrate how to write queries using relational calculus.

Uploaded by

Sourabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views19 pages

Lect 11 Relational Calculus

This document provides an overview of relational calculus. It discusses tuple relational calculus and how it is based on first-order logic and uses tuple variables. Examples are provided to demonstrate how to write queries using selection, projection, and joins. The use of existential and universal quantifiers in queries is explained. Practice queries with solutions are also included to illustrate how to write queries using relational calculus.

Uploaded by

Sourabh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Lecture 11

RELATIONAL
CALCULUS
RELATIONAL CALCULUS

Relational Calculus 
outputs a new relation

In terms of columns of In terms of rows of


stored relation stored relations

Domain Calculus Tuple Calculus


RELATIONAL CALCULUS

 In a calculus expression, there is no order of operations to


specify how to retrieve the query result.

 A calculus expression specifies only what information should


be retrieved, not how it should be retrieved. This is the main
distinguishing feature between relational algebra and
relational calculus.

 Thus, Relational Calculus is a


nonprocedural language.
TUPLE RELATIONAL CALCULUS
 Tuple Relational Calculus is based on first-order logic or predicate calculus.
 A predicate is a statement which results either ‘True’ or ‘False’, but not
both.
 In tuple relational calculus, the aim is to find tuples for which a predicate is
true.
 The calculus is dependent on the use of tuple variables.
 Each tuple variable ranges over a particular database relation, i.e. the
variable may take as its value any individual tuple from that relation.
 A simple tuple relational calculus query is of the form

{t | COND(t)}
where t is a tuple variable and COND (t) is a conditional expression
involving t. The result of such a query is the set of all tuples t that satisfy
COND (t).
COMPANY DATABASE
EXAMPLE
 To find the first and last names of all employees whose
salary is above $50,000.

{ t.FNAME, t.LNAME | EMPLOYEE(t) AND t.SALARY > 50000 }

Projection of first and last Selection of tuples


name of each EMPLOYEE satisfying  SALARY >50000
tuple t satisfying the
specified condition

range relation of tuple


variable t is EMPLOYEE
EXAMPLE

 Retrieve the birth date and address of the employee (or


employees) whose name is John B. Smith.

{ t.Bdate, t.Address | EMPLOYEE(t) AND

t.Fname=‘John’ AND t.Minit=‘B’ AND t.Lname=‘Smith’ }

Projection of first and last Selection of tuples


name of each EMPLOYEE satisfying  SALARY >50000
tuple t satisfying the
specified condition

range relation of tuple


variable t is EMPLOYEE
THE EXISTENTIAL AND UNIVERSAL
QUANTIFIERS
 Two special symbols called quantifiers can appear in formulas:
 universal quantifier ()
 existential quantifier ().

 A tuple variable t is bound if it is quantified, i.e. it appears in an ( t) or


( t) clause; otherwise, it is free.

 If F is a formula, then so is ( t)(F), where t is a tuple variable. The


formula (  t)(F) is true if the formula F evaluates to true for some (at
least one) tuple assigned to free occurrences of t in F; otherwise ( t)(F)
is false.

 If F is a formula, then so is ( t)(F), where t is a tuple variable. The


formula (  t)(F) is true if the formula F evaluates to true for every tuple
(in the universe) assigned to free occurrences of t in F; otherwise ( t)
(F) is false.
EXAMPLE QUERY USING EXISTENTIAL QUANTIFIER

 Retrieve the name and address of all employees who work for the
‘Research’ department.

Projection of first and last range relation of tuple


name and address of each variable t is EMPLOYEE
EMPLOYEE tuple t satisfying and d is DEPARTMENT
the specified condition

{ t.FNAME, t.LNAME, t.ADDRESS | EMPLOYEE(t) and ( d)

(DEPARTMENT(d) and d.DNAME = ‘Research’ and d.DNUMBER = t.DNO) }

Selection condition Join condition


EXAMPLE QUERY USING UNIVERSAL QUANTIFIER

 Find the names of employees who work on all the projects controlled
by department number 5.

{e.LNAME, e.FNAME | EMPLOYEE(e) and

( ( x) (not (PROJECT(x)) or not (x.DNUM = 5) OR

( ( w) (WORKS_ON(w) and w.ESSN = e.SSN and x.PNUMBER=w.PNO) ) ) )}

 The expression not (PROJECT(x)) inside the universally quantified formula


evaluates to true all tuples x that are not in the PROJECT relation.
 Further, exclude the tuples we are not interested in from R itself. The
expression not (x.DNUM=5) evaluates to true all tuples x that are in the
project relation but are not controlled by department 5.
UNIVERSAL EXISTENTIAL
QUANTIFIERS

 (∀x) (P(x)) ≡ NOT (∃x) (NOT (P(x)))


 (∃x) (P(x)) ≡ NOT (∀x) (NOT (P(x)))
 (∀x) (P(x) AND Q(x)) ≡ NOT (∃x) (NOT (P(x)) OR NOT(Q(x)))
 (∀x) (P(x) OR Q(x)) ≡ NOT (∃x) (NOT (P(x)) AND NOT (Q(x)))
 (∃x) (P(x)) OR Q(x)) ≡ NOT (∀x) (NOT (P(x)) AND NOT (Q(x)))
 (∃x) (P(x) AND Q(x)) ≡ NOT (∀x) (NOT (P(x)) OR NOT (Q(x)))
 (∀x)(P(x)) ⇒ (∃x)(P(x))
⇒ implies
 NOT (∃x)(P(x)) ⇒ NOT (∀x)(P(x))
EXAMPLE

 Find the names of employees who work on all the projects controlled
by department number 5.

{e.LNAME, e.FNAME | EMPLOYEE(e) and

( ( x) (not (PROJECT(x)) or not (x.DNUM = 5) OR

( ( w) (WORKS_ON(w) and w.ESSN = e.SSN and x.PNUMBER=w.PNO) ) ) )}

=========================

{e.Lname, e.Fname | EMPLOYEE(e) AND (NOT (∃x) (PROJECT(x) AND

(x.Dnum=5) AND (NOT (∃w)(WORKS_ON(w) AND w.Essn=e.Ssn

AND x.Pnumber=w.Pno))))}
PRACTICE QUERIES

 Q1: For every project located in ‘Stafford’, list the project


number the controlling department number and the
department manager’s last name, birth date, and address.

 Q2: For each employee, retrieve the employee’s first and


last name and the first and last name of his or her immediate
supervisor
PRACTICE QUERIES SOLUTION

 Q1:

{p.Pnumber, p.Dnum, m.Lname, m.Bdate, m.Address | PROJECT(p) AND


EMPLOYEE(m) AND p.Plocation = ‘Stafford’ AND ((∃d)(DEPARTMENT(d)

AND p.Dnum = d.Dnumber AND d.Mgr_ssn = m.Ssn))}

 Q2:

{e.Fname, e.Lname, s.Fname, s.Lname | EMPLOYEE(e) AND EMPLOYEE(s)


AND e.Super_ssn = s.Ssn}
PRACTICE QUERIES

 Q3: List the name of each employee who works on some


project controlled by department number 5.

 Q4: Make a list of project numbers for projects that involve


an employee whose last name is ‘Smith’, either as a worker
or as manager of the controlling department for the project.
PRACTICE QUERIES SOLUTION
 Q3:

{e.Lname, e.Fname | EMPLOYEE(e) AND ((∃x)(∃w)(PROJECT(x) AND


WORKS_ON(w) AND x.Dnum = 5 AND w.Essn = e.Ssn AND

x.Pnumber = w.Pno))}
 Q4:

{p.Pnumber | PROJECT(p) AND (((∃e)(∃w)(EMPLOYEE(e)

AND WORKS_ON(w) AND w.Pno = p.Pnumber

AND e.Lname = ‘Smith’ AND e.Ssn = w.Essn) )

OR ((∃m)(∃d)(EMPLOYEE(m) AND DEPARTMENT(d)

AND p.Dnum = d.Dnumber AND d.Mgr_ssn = m.Ssn

AND m.Lname = ‘Smith’)))}


PRACTICE QUERIES

 Q5: List the names of employees who have no dependents.

 Q6: List the names of managers who have at least one


dependent.
PRACTICE QUERIES SOLUTION
 Q5:

{e.Fname, e.Lname | EMPLOYEE(e) AND (NOT (∃d)(DEPENDENT(d)


AND e.Ssn = d.Essn))}

=========================

{e.Fname, e.Lname | EMPLOYEE(e) AND ((∀d)(NOT(DEPENDENT(d))


OR NOT(e.Ssn = d.Essn)))}

 Q6:

{e.Fname, e.Lname | EMPLOYEE(e) AND ((∃d)(∃ ρ)(DEPARTMENT(d)


AND DEPENDENT (ρ)

AND e.Ssn = d.Mgr_ssn AND ρ.Essn = e.Ssn))}


THANKS!!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy