SlideShare a Scribd company logo
3
Most read
4
Most read
10
Most read
Prof P Sreenivasa Kumar,
Department of CS&E, IITM.
1
Tuple Relational Calculus ( TRC )
Introduction
Procedural Query language
query specification involves giving a step by step process of
obtaining the query result
e.g., relational algebra
usage calls for detailed knowledge of the operators involved
difficult for the use of non-experts
Declarative Query language
query specification involves giving the logical conditions the
results are required to satisfy
easy for the use of non-experts
Prof P Sreenivasa Kumar,
Department of CS&E, IITM.
2
TRC – a declarative query language
Tuple variable – associated with a relation
( called the range relation )
• takes tuples from the range relation as its values
• t: tuple variable over relation r with scheme R(A,B,C )
t.A stands for value of column A etc
TRC Query – basic form:
{ t1.Ai1
, t2.Ai2
,…tm.Aim
| θ }
predicate calculus expression
involving tuple variables
t1, t2,…, tm, tm+1,…,ts
- specifies the condition to be satisfied
Prof P Sreenivasa Kumar,
Department of CS&E, IITM.
3
An example TRC query
student (rollNo, name, degree, year, sex, deptNo, advisor )
department (deptId, name, hod, phone )
Obtain the rollNo, name of all girl students
in the Maths Dept (deptId = 2)
{s.rollNo,s.name| student(s)^ s.sex=‘F’^ s.deptNo=2}
attributes
required in
the result
This predicate is true whenever
value of s is a tuple from the
student relation, false otherwise
In general, if t is a tuple variable with range
relation r, r( t ) is taken as a predicate which
is true if and only if the value of t is a tuple in r
Prof P Sreenivasa Kumar,
Department of CS&E, IITM.
4
Atomic expressions are the following:
1. r ( t ) -- true if t is a tuple in the relation instance r
2. t1. Ai <compOp> t2 .Aj compOp is one of {<, ≤ , >, ≥, =, ≠ }
3. t.Ai <compOp> c c is a constant of appropriate type
Composite expressions:
1. Any atomic expression
2. F1 ∧ F2 ,, F1 ∨ F2 , ¬ F1 where F1 and F2 are expressions
3. (∀t) (F), (∃t) (F) where F is an expression
and t is a tuple variable
Free Variables
Bound Variables – quantified variables
General form of the condition in TRC queries
Prof P Sreenivasa Kumar,
Department of CS&E, IITM.
5
All possible tuple assignments to the free variables in the query are
considered.
For any specific assignment,
if the expression to the right of the vertical bar evaluates to true,
that combination of tuple values
would be used to produce a tuple in the result relation.
While producing the result tuple, the values of the attributes for the
corresponding tuple variables as specified on the left side of the
vertical bar would be used.
Note: The only free variables are the ones that appear to the left
of the vertical bar
Interpretation of the query in TRC
Prof P Sreenivasa Kumar,
Department of CS&E, IITM.
6
Obtain the rollNo, name of all girl students in the
Maths Dept
{s.rollNo,s.name | student(s) ^ s.sex=‘F’ ^
(∃ d)(department(d) ^ d.name=‘Maths’
^ d.deptId = s.deptNo)}
s: free tuple variable d: existentially bound tuple variable
Existentially or universally quantified tuple variables can be used
on the RHS of the vertical bar to specify query conditions
Attributes of free (or unbound ) tuple variables can be used on LHS
of vertical bar to specify attributes required in the results
Example TRC queries
Prof P Sreenivasa Kumar,
Department of CS&E, IITM.
7
Example Relational Scheme
student (rollNo, name, degree, year, sex, deptNo, advisor)
department (deptId, name, hod, phone)
professor (empId, name, sex, startYear, deptNo, phone)
course (courseId, cname, credits, deptNo)
enrollment (rollNo, courseId, sem, year, grade)
teaching (empId, courseId, sem, year, classRoom)
preRequisite (preReqCourse, courseID)
Q2
Q3
Q4
Q5
Prof P Sreenivasa Kumar,
Department of CS&E, IITM.
8
Example queries in TRC (1/5)
1)Determine the departments that do not have
any girl students
student (rollNo, name, degree, year, sex, deptNo, advisor)
department (deptId, name, hod, phone)
{d.name|department(d) ^
¬(∃ s)(student(s) ^
s.sex =‘F’ ^ s.deptNo = d.deptId)
Prof P Sreenivasa Kumar,
Department of CS&E, IITM.
9
2)Obtain the names of courses enrolled by student
named Mahesh
{c.name | course(c) ^
(∃s) (∃e) ( student(s) ^ enrollment(e)
^ s.name = “Mahesh”
^ s.rollNo = e.rollNo
^ c.courseId = e.courseId }
Examples queries in TRC (2/5) Schema
Prof P Sreenivasa Kumar,
Department of CS&E, IITM.
10
3)Get the names of students who have scored ‘S’ in all
subjects they have enrolled. Assume that every
student is enrolled in at least one course.
{s.name | student(s) ^
(∀e)(( enrollment(e) ^
e.rollNo = s.rollNo) → e.grade =‘S’)}
person P with all S grades:
for enrollment tuples not having her roll number, LHS is false
for enrollment tuples having her roll number, LHS is true, RHS also true
so the implication is true for all e tuples
person Q with some non-S grades:
for enrollment tuples not having her roll number, LHS is false
for enrollment tuples having her roll number, LHS is true, but RHS is false for
at least one tuple.
So the implication is not true for at least one tuple.
Examples queries in TRC (3/5) Schema
Prof P Sreenivasa Kumar,
Department of CS&E, IITM.
11
4) Get the names of students who have taken at least
one course taught by their advisor
{s.name | student(s) ^
(∃e)(∃t)(enrollment(e) ^ teaching(t) ^
e.courseId = t.courseId ^
e.rollNo = s.rollNo ^
t.empId = s.advisor}
5) Display the departments whose HODs are teaching
at least one course in the current semester
{d.name | department(d) ^(∃t)(teaching(t) ^
t.empid = d.hod
^ t.sem = ‘odd’ ^ t.year = ‘2008’)}
Examples queries in TRC (4/5) Schema
Prof P Sreenivasa Kumar,
Department of CS&E, IITM.
12
6)Determine the students who are enrolled for every
course taught by Prof Ramanujam. Assume that Prof
Ramanujam teaches at least one course.
1. {s.rollNo | student (s) ^
2. (∀c)(course (c) ^
3. ((∃t),(∃p)( teaching(t) ^ professor(p) ^
4. t.courseId = c.courseId ^
5. p.name = “Ramanujam” ^
6. p.empId = t.empId )) →
7. (∃e) (enrollment(e) ^
8. e.courseId = c.courseId ^
9. e.rollNo = s.rollNo)
10. )
11. }
Examples queries in TRC (5/5) Schema
Prof P Sreenivasa Kumar,
Department of CS&E, IITM.
13
What is the result of the query:
{s.rollNo | ¬ student(s)} ?
Infinite answers !!
Unsafe TRC expression :
Any expression whose result uses “constants / values” that do not
appear in the instances of any of the database relations.
Unsafe expressions are to be avoided while specifying TRC queries.
Problem with unrestricted use of Negation
Prof P Sreenivasa Kumar,
Department of CS&E, IITM.
14
It can be shown that
both Tuple Relational Calculus and Relational Algebra
have the same expressive power
A query can be formulated in (safe) TRC
if and only if it can be formulated in RA
Both can not be used to formulate queries involving
transitive closure
-- find all direct or indirect pre-requisites of a course
-- find all subordinates of a specific employee etc.
Expressive power of TRC and Relational Algebra

More Related Content

PPT
2.3 bayesian classification
PPTX
ER model to Relational model mapping
PPTX
Normal forms
PPTX
Relational algebra ppt
PDF
Sql Basics | Edureka
PPT
ER-Model-ER Diagram
PPTX
Database abstraction
2.3 bayesian classification
ER model to Relational model mapping
Normal forms
Relational algebra ppt
Sql Basics | Edureka
ER-Model-ER Diagram
Database abstraction

What's hot (20)

PPTX
Relational Data Model Introduction
PPTX
joins in database
PDF
DBMS 7 | Relational Query Language
PPTX
Nested queries in database
PPT
decison tree and rules in data mining techniques
PPT
Regular expressions-Theory of computation
PDF
Dimensionality Reduction
PDF
Relational algebra in dbms
PPTX
EER modeling
PPTX
Oltp vs olap
PPTX
2. R-basics, Vectors, Arrays, Matrices, Factors
PPTX
Multidimensional schema of data warehouse
PPTX
Machine Learning-Linear regression
PPSX
Functional dependency
PPTX
Presentation on supervised learning
PDF
Linear Regression With R
PPTX
Normalization 1 nf,2nf,3nf,bcnf
PDF
Linear regression
PPT
relational algebra
PPTX
Relational model
Relational Data Model Introduction
joins in database
DBMS 7 | Relational Query Language
Nested queries in database
decison tree and rules in data mining techniques
Regular expressions-Theory of computation
Dimensionality Reduction
Relational algebra in dbms
EER modeling
Oltp vs olap
2. R-basics, Vectors, Arrays, Matrices, Factors
Multidimensional schema of data warehouse
Machine Learning-Linear regression
Functional dependency
Presentation on supervised learning
Linear Regression With R
Normalization 1 nf,2nf,3nf,bcnf
Linear regression
relational algebra
Relational model
Ad

Similar to 3.1 tuple relational_calculus (20)

PPTX
Lect 3 Relational Calculus.pptx from mmu
PPT
Trc final
PPTX
Chapter-7 Relational Calculus
PPTX
Relational Algebra in Database Systems.pptx
PPTX
UNIT-2 Relation algebra&RelationalCalculus.pptx
PPTX
database chapter 6.pptx advanced database
PPTX
DBMS ppt (1).pptx
PDF
3_Relational_Model.pdf
PPTX
Relational Calculus in Database Management System
PPT
Formal- Relational- Query- Languages.ppt
PPT
Formal-Relational-Query-Languages.ppt for education
PPTX
Relational Model,relational calulus.pptx
PDF
3 relational model
PPTX
Relational Algebra.Pptxjejejjdjdh jsjsjd
PPTX
Introduction to Relational Database Management Systems
PDF
6 - Relational Calculus.pdf
PPT
lecture9Calc.ppt
PDF
Relational Algebra & Calculus
PPT
lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...
PPT
lecture8Alg.ppt
Lect 3 Relational Calculus.pptx from mmu
Trc final
Chapter-7 Relational Calculus
Relational Algebra in Database Systems.pptx
UNIT-2 Relation algebra&RelationalCalculus.pptx
database chapter 6.pptx advanced database
DBMS ppt (1).pptx
3_Relational_Model.pdf
Relational Calculus in Database Management System
Formal- Relational- Query- Languages.ppt
Formal-Relational-Query-Languages.ppt for education
Relational Model,relational calulus.pptx
3 relational model
Relational Algebra.Pptxjejejjdjdh jsjsjd
Introduction to Relational Database Management Systems
6 - Relational Calculus.pdf
lecture9Calc.ppt
Relational Algebra & Calculus
lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...
lecture8Alg.ppt
Ad

More from Utkarsh De (7)

PPTX
4 k Display Technology
PDF
6 relational schema_design
PDF
5 data storage_and_indexing
PDF
4 the sql_standard
PDF
2 entity relationship_model
PDF
1 introduction
PPTX
Four way traffic light conrol using Verilog
4 k Display Technology
6 relational schema_design
5 data storage_and_indexing
4 the sql_standard
2 entity relationship_model
1 introduction
Four way traffic light conrol using Verilog

Recently uploaded (20)

PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPT
Mechanical Engineering MATERIALS Selection
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Well-logging-methods_new................
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPT
Project quality management in manufacturing
PPTX
Geodesy 1.pptx...............................................
PPTX
additive manufacturing of ss316l using mig welding
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Welding lecture in detail for understanding
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
CH1 Production IntroductoryConcepts.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
R24 SURVEYING LAB MANUAL for civil enggi
Mechanical Engineering MATERIALS Selection
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Well-logging-methods_new................
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Sustainable Sites - Green Building Construction
CYBER-CRIMES AND SECURITY A guide to understanding
Project quality management in manufacturing
Geodesy 1.pptx...............................................
additive manufacturing of ss316l using mig welding
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Welding lecture in detail for understanding

3.1 tuple relational_calculus

  • 1. Prof P Sreenivasa Kumar, Department of CS&E, IITM. 1 Tuple Relational Calculus ( TRC ) Introduction Procedural Query language query specification involves giving a step by step process of obtaining the query result e.g., relational algebra usage calls for detailed knowledge of the operators involved difficult for the use of non-experts Declarative Query language query specification involves giving the logical conditions the results are required to satisfy easy for the use of non-experts
  • 2. Prof P Sreenivasa Kumar, Department of CS&E, IITM. 2 TRC – a declarative query language Tuple variable – associated with a relation ( called the range relation ) • takes tuples from the range relation as its values • t: tuple variable over relation r with scheme R(A,B,C ) t.A stands for value of column A etc TRC Query – basic form: { t1.Ai1 , t2.Ai2 ,…tm.Aim | θ } predicate calculus expression involving tuple variables t1, t2,…, tm, tm+1,…,ts - specifies the condition to be satisfied
  • 3. Prof P Sreenivasa Kumar, Department of CS&E, IITM. 3 An example TRC query student (rollNo, name, degree, year, sex, deptNo, advisor ) department (deptId, name, hod, phone ) Obtain the rollNo, name of all girl students in the Maths Dept (deptId = 2) {s.rollNo,s.name| student(s)^ s.sex=‘F’^ s.deptNo=2} attributes required in the result This predicate is true whenever value of s is a tuple from the student relation, false otherwise In general, if t is a tuple variable with range relation r, r( t ) is taken as a predicate which is true if and only if the value of t is a tuple in r
  • 4. Prof P Sreenivasa Kumar, Department of CS&E, IITM. 4 Atomic expressions are the following: 1. r ( t ) -- true if t is a tuple in the relation instance r 2. t1. Ai <compOp> t2 .Aj compOp is one of {<, ≤ , >, ≥, =, ≠ } 3. t.Ai <compOp> c c is a constant of appropriate type Composite expressions: 1. Any atomic expression 2. F1 ∧ F2 ,, F1 ∨ F2 , ¬ F1 where F1 and F2 are expressions 3. (∀t) (F), (∃t) (F) where F is an expression and t is a tuple variable Free Variables Bound Variables – quantified variables General form of the condition in TRC queries
  • 5. Prof P Sreenivasa Kumar, Department of CS&E, IITM. 5 All possible tuple assignments to the free variables in the query are considered. For any specific assignment, if the expression to the right of the vertical bar evaluates to true, that combination of tuple values would be used to produce a tuple in the result relation. While producing the result tuple, the values of the attributes for the corresponding tuple variables as specified on the left side of the vertical bar would be used. Note: The only free variables are the ones that appear to the left of the vertical bar Interpretation of the query in TRC
  • 6. Prof P Sreenivasa Kumar, Department of CS&E, IITM. 6 Obtain the rollNo, name of all girl students in the Maths Dept {s.rollNo,s.name | student(s) ^ s.sex=‘F’ ^ (∃ d)(department(d) ^ d.name=‘Maths’ ^ d.deptId = s.deptNo)} s: free tuple variable d: existentially bound tuple variable Existentially or universally quantified tuple variables can be used on the RHS of the vertical bar to specify query conditions Attributes of free (or unbound ) tuple variables can be used on LHS of vertical bar to specify attributes required in the results Example TRC queries
  • 7. Prof P Sreenivasa Kumar, Department of CS&E, IITM. 7 Example Relational Scheme student (rollNo, name, degree, year, sex, deptNo, advisor) department (deptId, name, hod, phone) professor (empId, name, sex, startYear, deptNo, phone) course (courseId, cname, credits, deptNo) enrollment (rollNo, courseId, sem, year, grade) teaching (empId, courseId, sem, year, classRoom) preRequisite (preReqCourse, courseID) Q2 Q3 Q4 Q5
  • 8. Prof P Sreenivasa Kumar, Department of CS&E, IITM. 8 Example queries in TRC (1/5) 1)Determine the departments that do not have any girl students student (rollNo, name, degree, year, sex, deptNo, advisor) department (deptId, name, hod, phone) {d.name|department(d) ^ ¬(∃ s)(student(s) ^ s.sex =‘F’ ^ s.deptNo = d.deptId)
  • 9. Prof P Sreenivasa Kumar, Department of CS&E, IITM. 9 2)Obtain the names of courses enrolled by student named Mahesh {c.name | course(c) ^ (∃s) (∃e) ( student(s) ^ enrollment(e) ^ s.name = “Mahesh” ^ s.rollNo = e.rollNo ^ c.courseId = e.courseId } Examples queries in TRC (2/5) Schema
  • 10. Prof P Sreenivasa Kumar, Department of CS&E, IITM. 10 3)Get the names of students who have scored ‘S’ in all subjects they have enrolled. Assume that every student is enrolled in at least one course. {s.name | student(s) ^ (∀e)(( enrollment(e) ^ e.rollNo = s.rollNo) → e.grade =‘S’)} person P with all S grades: for enrollment tuples not having her roll number, LHS is false for enrollment tuples having her roll number, LHS is true, RHS also true so the implication is true for all e tuples person Q with some non-S grades: for enrollment tuples not having her roll number, LHS is false for enrollment tuples having her roll number, LHS is true, but RHS is false for at least one tuple. So the implication is not true for at least one tuple. Examples queries in TRC (3/5) Schema
  • 11. Prof P Sreenivasa Kumar, Department of CS&E, IITM. 11 4) Get the names of students who have taken at least one course taught by their advisor {s.name | student(s) ^ (∃e)(∃t)(enrollment(e) ^ teaching(t) ^ e.courseId = t.courseId ^ e.rollNo = s.rollNo ^ t.empId = s.advisor} 5) Display the departments whose HODs are teaching at least one course in the current semester {d.name | department(d) ^(∃t)(teaching(t) ^ t.empid = d.hod ^ t.sem = ‘odd’ ^ t.year = ‘2008’)} Examples queries in TRC (4/5) Schema
  • 12. Prof P Sreenivasa Kumar, Department of CS&E, IITM. 12 6)Determine the students who are enrolled for every course taught by Prof Ramanujam. Assume that Prof Ramanujam teaches at least one course. 1. {s.rollNo | student (s) ^ 2. (∀c)(course (c) ^ 3. ((∃t),(∃p)( teaching(t) ^ professor(p) ^ 4. t.courseId = c.courseId ^ 5. p.name = “Ramanujam” ^ 6. p.empId = t.empId )) → 7. (∃e) (enrollment(e) ^ 8. e.courseId = c.courseId ^ 9. e.rollNo = s.rollNo) 10. ) 11. } Examples queries in TRC (5/5) Schema
  • 13. Prof P Sreenivasa Kumar, Department of CS&E, IITM. 13 What is the result of the query: {s.rollNo | ¬ student(s)} ? Infinite answers !! Unsafe TRC expression : Any expression whose result uses “constants / values” that do not appear in the instances of any of the database relations. Unsafe expressions are to be avoided while specifying TRC queries. Problem with unrestricted use of Negation
  • 14. Prof P Sreenivasa Kumar, Department of CS&E, IITM. 14 It can be shown that both Tuple Relational Calculus and Relational Algebra have the same expressive power A query can be formulated in (safe) TRC if and only if it can be formulated in RA Both can not be used to formulate queries involving transitive closure -- find all direct or indirect pre-requisites of a course -- find all subordinates of a specific employee etc. Expressive power of TRC and Relational Algebra