Download presentation
Presentation is loading. Please wait.
Published byAubrey Davidson Modified over 8 years ago
1
Copyright © 2003-2008 Curt Hill The Relational Calculus Another way to do queries
2
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
3
Copyright © 2003-2008 Curt Hill Formulas Operators Quantifiers Constants Comparisons All tuples that makes the formula true are in the answer table
4
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
5
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|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
6
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|C Courses C.dept=CS} –This should be easy to convert into a relational algebra selection –This is declarative not procedural
7
Copyright © 2003-2008 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
8
Copyright © 2003-2008 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
9
Copyright © 2003-2008 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)}
10
Copyright © 2003-2008 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
11
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
12
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
13
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 © 2003-2008 Curt Hill
14
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 © 2003-2008 Curt Hill
15
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
16
Copyright © 2003-2008 Curt Hill Queries using relational calculus Consider the college schema tables: –Course –Students –Grade –Faculty –Faculty_teach –Department –Division
17
Copyright © 2003-2008 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}
18
Copyright © 2003-2008 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
19
Copyright © 2003-2008 Curt Hill Access Choose Tables
20
Copyright © 2003-2008 Curt Hill Access Choose Fields
21
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.