Copyright © Curt Hill The Relational Calculus Another way to do queries
Copyright © 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 © Curt Hill Formulas Operators Quantifiers Constants Comparisons All tuples that makes the formula true are in the answer table
Copyright © 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 © 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|C Courses} –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 © Curt Hill Formulas Atomic formulas need to be combined to be useful They are combined with AND ( ) OR ( ) and modified with NOT ( ) {C|C Courses C.dept=CS} –This should be easy to convert into a relational algebra selection –This is declarative not procedural
Copyright © Curt Hill Query By Example QBE is a graphical way to specify this {C|C Courses C.dept=CS} Click a table to indicater Fill in the desired values DeptNumberCrhrTitle CS
Copyright © Curt Hill Quantifiers Two common quantifiers –There exists T(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 © Curt Hill Quantified queries Any student taking any upper level course {S | S Students G Grades (G.number>299 S.naid=G.naid)} Any student taking only upper level courses {S | S Students G Grades (G.number>299 S.naid=G.naid)}
Copyright © Curt Hill Unsafe queries Any query with an infinite number of answers –Legal syntax, illegal results {C| C Courses} This possibility is not present in the algebra –The algebra always deals with finite relations
Copyright © 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 © 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: where the fs are fields Copyright © Curt Hill
DRC Example Consider this formula { | m>299 d, m, x, n ( Grades) 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 © Curt Hill
DRC and TRC example In TRC we might say: {S | S Students G Grades (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 © Curt Hill Queries using relational calculus Consider the college schema tables: –Course –Students –Grade –Faculty –Faculty_teach –Department –Division
Copyright © Curt Hill Find student grades DRC { | sc( Grades) name( 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 © Curt Hill Find all the courses taught by faculty members (include credit hours) { | naid,dp,dg,ar,title( Faculty) Course Faculty_Teach )} Implied equality in naid, dept, number, crhr
Copyright © Curt Hill Access Choose Tables
Copyright © Curt Hill Access Choose Fields
Copyright © 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