HandOut M3.1 TRC
HandOut M3.1 TRC
Introduction
Predicates
We have studied “Predicate Logic” in Discrete Maths course
What is a “predicate”?
A predicate is an expression with place-holders (aka variables)
– it takes a Boolean value when values are substituted for variables
lessThan(x,y) – lessThan(2,5) is true; lessThan(5,1) is false
1
TRC – a declarative query language
Tuple variable – associated with a relation
(called the range relation)
• takes tuples as its values
• t : tuple variable over relation r with scheme R(A,B,C )
t.A stands for value of column/attribute A in t etc
attributes
This predicate is true whenever
required in
value of s is a tuple from the
the result
Student instance, 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, 5
Department of CS&E, IITM.
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
2
Interpretation of the query in TRC
All possible tuple assignments to the free variables in the query are
considered.
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
Prof P Sreenivasa Kumar, 7
Department of CS&E, IITM.
3
Example queries in TRC (1/5)
{d.name|department(d) ^
¬(∃ s)(student(s) ^
s.sex =‘F’ ^ s.deptNo = d.deptId)
{c.name | course(c) ^
(∃s) (∃e) ( student(s) ^ enrollment(e)
^ s.name = “Mahesh”
^ s.rollNo = e.rollNo
^ c.courseId = e.courseId }
4
Examples queries in TRC (4/5) Schema
{s.name | student(s) ^
(∃e)(∃t)(enrollment(e) ^ teaching(t) ^
e.courseId = t.courseId ^
e.rollNo = s.rollNo ^
t.empId = s.advisor}
{s.rollNo | ¬ student(s)} ?
Infinite answers !!
5
Expressive power of TRC and Relational Algebra
It can be shown that
both Tuple Relational Calculus and Relational Algebra
have the same expressive power
6
Try this query - 3
List the name, course number of the prerequisites of the course
“Data Mining”.
{c.courseId, c.cname |
course(c) ^
(∃d)(∃q)( course(d) ^ d.cname = “Data Mining” ^
preRequisite(q) ^
q.courseId = d.courseId ^
q.preReqCourse = c.courseId )
}