Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Management Systems,1 Relational Calculus.

Similar presentations


Presentation on theme: "Database Management Systems,1 Relational Calculus."— Presentation transcript:

1 Database Management Systems,1 Relational Calculus

2 Database Management Systems,2 Tuple Relational Calculus(TRC)  Tuple variable is a variable that takes on the tuples of a particular relation schema as values  It has the form {T|p(T)} –Where T is the tuple variable and p(.) is the formula that describe T. –The result of the query is the set of all tuples for which the formula is true…  The formula is constructed using the tuples and relational operators: >, <, =, not equal, less than or equal, greater than or equal.  The formulas may be atomic: composed of tuple variables, operators, and constants  Formula may also be recursively defined in terms of other sub-formulas.

3 Database Management Systems,3 Examples  Find all sailors with rating above 7: (Q11) {S|S  Sailors  S.rating>7} –S is instantiated for each tuple of Sailor and the test is applied to it. The answer is the set of those instances passing the test.  Find the names and ages of the sailors with rating above 7: (Q12) {P|S  Sailors (S.rating>7  P.sname=S.sname  P.age=S.age)}  Find the sailor names, boat id, and reservation dates for each reservation: (Q13) {P|  R  Reserves  S  Sailors (R.sid=S.sid  P.bid=R.bid  P.day=R.day  P.sname=S.sname)}

4 Database Management Systems,4 Examples (cont.)  Find the name of the sailors who have reserved boat 103 :(Q1) {P|  S  Sailors  R  Reserves (R.sid=S.sid  R.bid=103  P.sname=S.sname )}  Find the name of the sailors who have reserved a red boat: (Q2) {P|  S  Sailors  R  Reserves   B  Boats (R.sid=S.sid  B.bid=R.bid  B.color=‘red’  P.sname=S.sname)}  Find the name of the sailors who have reserved a at least two boats :(Q7) {P|  S  Sailors  R1  Reserves   R2  Reserves (S.sid=R1.sid  R1.sid=R2.sid  R1.bid  R2.bid  P.sname=S.sname)}

5 Database Management Systems,5 Examples (cont.)  Find the name of the sailors who have reserved all boats :(Q9) {P|  S  Sailors  B  Boats  R  Reserves(S.sid=R.sid  R.bid=B.bid  P.sname=S.sname)} –English statement for this query is “find sailors S such that for all boats B there is a Reserves tuple…”  Find the name of the sailors who have reserved all red boats :(Q14) {P|  S  Sailors   B  Boats (B.color =‘red’  (  R  Reserves(S.sid=R.sid  R.bid=B.bid  P.sname=S.sname)))} or replacing implication by NOT and OR: {P|  S  Sailors   B  Boats (B.color  ‘red’  (  R  Reserves(S.sid=R.sid  R.bid=B.bid  P.sname=S.sname)))} –Find sailors S such that for all boats B, either the boat is not red or the sailor S has reserved boat B.

6 Database Management Systems,6 Aggregate Functions  Aggregate functions and grouping are not included as basic algebraic operations. However, they are useful, like division and join operation. Count and Average, Minimum, Maximum, and Sum are aggregate functions.  Example for Count and Average:  Number of sailors reserving each boat: bid ₣ count sid (Reserves)  Total number of students and the average rating: ₣ count sid, Average rating (Sailors)

7 Database Management Systems,7 Review  Major problem with calculus: unsafe queries (P|  S  Sailors) is syntactically correct but it is an unsafe query.  If a query language can express all the queries that can be expresses in relational algebra it is called a relationally complete language  Relational algebra query describe a procedure to compute output relation from input relations, using RelAlg operators  Relational calculus describe the tuples in output relation, using a subset of first order predicate logic  All relational algebra queries can be expresses in relational calculus, the converse is true only for restricted relational calculus


Download ppt "Database Management Systems,1 Relational Calculus."

Similar presentations


Ads by Google