copyright © 2003-2008 curt hill the relational calculus another way to do queries
DESCRIPTION
Copyright © Curt Hill Formulas Operators Quantifiers Constants Comparisons All tuples that makes the formula true are in the answer tableTRANSCRIPT
Copyright © 2003-2008 Curt Hill
The Relational Calculus
Another way to do queries
Copyright © 2003-2008 Curt Hill
Calculus• Declarative way to form a query• Two types
– Tuple Relational Calculus– Domain Relational Calculus
• Subsets of first order logic• Formulas are the main unit
– Used to construct the tables that satisfy the query
Copyright © 2003-2008 Curt Hill
Formulas• Operators• Quantifiers• Constants• Comparisons• All tuples that makes the formula
true are in the answer table
Copyright © 2003-2008 Curt Hill
TRC and DRC• Tuple Relational Calculus considers
a tuple as variable• Domain Relational Calculus
considers a Domain a variable– A domain is math for the field or
attribute values
Copyright © 2003-2008 Curt Hill
Atomic Formulas• A simple statement connecting a
tuple or domain to some data• We may compare any tuple value
or domain value with constants• Since tables are sets they are
expressed as set expressions:– {C|CCourses}– The set of all tuples C such that C is
an element of the Courses relation• You may also compare a tuple
element with a value
Copyright © 2003-2008 Curt Hill
Formulas• Atomic formulas need to be
combined to be useful• They are combined with AND ()
OR () and modified with NOT ()• {C|CCourses C.dept=CS}
– This should be easy to convert into a relational algebra selection
– This is declarative not procedural
Copyright © 2003-2008 Curt Hill
Query By Example• QBE is a graphical way to specify this• {C|CCourses C.dept=CS}• Click a table to indicater• Fill in the desired values
Dept Number Crhr Title
CS
Copyright © 2003-2008 Curt Hill
Quantifiers• Two common quantifiers
– There existsT(predicate(T))– For all T(predicate(T))
• These bind a formula to a variable• This variable may then be used in
a larger formula Courses(Courses.number>299)
– True if there is one course that is so numbered
Copyright © 2003-2008 Curt Hill
Quantified queries• Any student taking any upper level
course• {S | SStudents
GGrades (G.number>299 S.naid=G.naid)}
• Any student taking only upper level courses
• {S | SStudents GGrades (G.number>299 S.naid=G.naid)}
Copyright © 2003-2008 Curt Hill
Unsafe queries• Any query with an infinite number
of answers– Legal syntax, illegal results
• {C|CCourses}• This possibility is not present in the
algebra– The algebra always deals with finite
relations
Copyright © 2003-2008 Curt Hill
Equivalence and completeness• Every series of relational algebra
expressions may be expressed as a safe calculus formula
• Every safe calculus formula may be expressed as a series of relational algebra operations– A must for implementation
• Any language that can express any relational algebra expression is said to be relationally complete– SQL is relationally complete
Copyright © 2003-2008 Curt Hill
Domain Relational Calculus
• Most of the previous examples were of the Tuple Relational Calculus
• In the DRC we show the construction of the tuple using domain values or only show the domain values
• Different syntax, but equivalent
Domain Relational Constructions
• In the DRC we construct tuples from fields– Not rely on them as variables
• Then we determine the values we want these fields to have
• The notation for this construction is:<f1,f2,f3…fn>where the fs are fields
Copyright © 2003-2008 Curt Hill
DRC Example• Consider this formula
{<a, n>| m>299 d, m, x, n (<d,m,x,n> Grades) n, a, r(<n,a,r> Students)}
• This says construct a table of tuples– Two elements in each tuple, a and m
• Student name is a, naid is n– There must exist a grade tuple and a
student tuple with these characteristics• Same naid and course number > 299
Copyright © 2003-2008 Curt Hill
Copyright © 2003-2008 Curt Hill
DRC and TRC example• In TRC we might say:
{S | SStudents GGrades (G.number>299 S.naid=G.naid)}
• This gives approximately the same table as the previous except there are more fields in each row
• The DRC makes it easy to construct tuples that do not exist in any table
Copyright © 2003-2008 Curt Hill
Queries using relational calculus
• Consider the college schema tables:– Course– Students– Grade– Faculty– Faculty_teach– Department– Division
Copyright © 2003-2008 Curt Hill
Find student grades• DRC
{<naid,addr,dp,cr,sc>| sc(<dp,cr,naid,sc> Grades) name(<naid,name,addr> Students)}
• TRC{T| T2 Grades T.naid = T2.naid T3 Students T3.naid = T.naid T.addr = T3.addr T.dp = T2.dp T.cr = T2.cr T.sc = T2.sc}
Copyright © 2003-2008 Curt Hill
Find all the courses taught by faculty members (include
credit hours)• {<name,dept,number,crhr>|
naid,dp,dg,ar,title(<naid,name,dp,dg,ar>Faculty) <dept,number,crhr,title>Course <dept,number,naid>Faculty_Teach)}
• Implied equality in naid, dept, number, crhr
Copyright © 2003-2008 Curt Hill
Access Choose Tables
Copyright © 2003-2008 Curt Hill
Access Choose Fields
Copyright © 2003-2008 Curt Hill
Some others• Find the departmental chairs
for each faculty member• Find all the students who got a B or
better in any CS class• Find all the students that each
faculty member has• Find all the students who got
an A in Calculus and an A in CIS 385