Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jun-Ki Min.  Based on predicate calculus ◦ Predicate  a function whose value is true or false  non-procedural ◦ A relational calculus expression creates.

Similar presentations


Presentation on theme: "Jun-Ki Min.  Based on predicate calculus ◦ Predicate  a function whose value is true or false  non-procedural ◦ A relational calculus expression creates."— Presentation transcript:

1 Jun-Ki Min

2  Based on predicate calculus ◦ Predicate  a function whose value is true or false  non-procedural ◦ A relational calculus expression creates a new relation, which is specified in terms of variables that range over rows of the stored database relations (in tuple calculus) or over columns of the stored relations (in domain calculus). ◦ In a calculus expression, there is no order of operations to specify how to retrieve the query result—a calculus expression specifies only what information the result should contain. ◦ This is the main distinguishing feature between relational algebra and relational calculus.

3  The tuple relational calculus is based on specifying a number of tuple variables.  Each tuple variable usually ranges over a particular database relation, meaning that the variable may take as its value any individual tuple from that relation.  A simple tuple relational calculus query is of the form {t | COND(t)} ◦ where t is a tuple variable and COND (t) is a conditional expression involving t.  tuple variable : t  Range Expression: R(t)  R: t’s range relation ◦ The result of such a query is the set of all tuples t that satisfy COND (t).

4 Slide 6- 4  Example: To find the first and last names of all em ployees whose salary is above $50,000, we can writ e the following tuple calculus expression: {t.FNAME, t.LNAME | EMPLOYEE(t) AND t.SALARY>500 00}  The condition EMPLOYEE(t) specifies that the range relation of tuple variable t is EMPLOYEE.  The first and last name (PROJECTION  FNAME, LNAME ) o f each EMPLOYEE tuple t that satisfies the condition t.SALARY>50000 (SELECTION  SALARY >50000 ) will be retrieved. ◦ Qualified attribute: t.A  Tuple t’s attribute A

5 5  Component (con’t) ⅲ. atomic formula  Range expression: R(t) t : tuple variable R : t’s range relation  Condition: t.A θ u.B A, B : attribute θ : 비 =, ≠,,≥  Condition: t.A θ c c: constant ◦ Evaluation of atomic formula is true or false

6 6 ⅳ.WFF, well-formed formula  Atomic formula, boolean operator(∧,∨, ¬ ), quantifier (∀,∃) ① All atomic formulae are WFF ② if F is WFF, (F) and  F are WFF ③ if F and G are WFF, F∧G and F∨G are WFF ④ If F(f) is WFF where tuple variable t is free variable, ∀t(F(t)) and ∃t(F(t))are WFF ⑤ Expression applied above rules is WFF ◦ Example s.Sno = 100 c.Cno ≠ e.Cno s.Sno = e.Sno ∧ e.Cno ≠ c.Cno (∃e)(e.Sno = s.Sno ∧ e.Cno = 'C413')

7 Slide 6- 7  Two special symbols called quantifiers can appear i n formulas; these are the universal quantifier  an d the existential quantifier   Informally, a tuple variable t is bound if it is quantif ied, meaning that it appears in an  t  or  t  claus e; otherwise, it is free.  If F is a formula, then so are  t)(F) and  t)(F), wh ere t is a tuple variable. ◦ The formula  t)(F) is true if the formula F evaluates t o true for some (at least one) tuple assigned to free oc currences of t in F; otherwise  t)(F) is false. ◦ The formula  t)(F) is true if the formula F evaluates t o true for every tuple (in the universe) assigned to free occurrences of t in F; otherwise  t)(F) is false.

8 Slide 6- 8   is called the universal or “for all” quantifier because every tuple in “the universe of” tuple s must make F true to make the quantified for mula true.   is called the existential or “there exists” qua ntifier because any tuple that exists in “the un iverse of” tuples may make F true to make the quantified formula true.

9  example ◦ {s.Sname | STUDENT(s) } ◦ {s.Sname | STUDENT(s)∧s.Dept=‘CS’ } ◦ {s.Sname, s.Dept | STUDENT(s)∧(∃e)(ENROL(e)∧ s.Sno=e.Sno∧e.Grade='A')}  In left side of bar, only free variable can be located.

10 Slide 6- 10  Another variation of relational calculus called the domain relat ional calculus, or simply, domain calculus is equivalent to tupl e calculus and to relational algebra.  The language called QBE (Query-By-Example) that is related t o domain calculus was developed almost concurrently to SQL at IBM Research, Yorktown Heights, New York. ◦ Domain calculus was thought of as a way to explain what Q BE does.  Domain calculus differs from tuple calculus in the type of vari ables used in formulas: ◦ Rather than having variables range over tuples, the variable s range over single values from domains of attributes.  To form a relation of degree n for a query result, we must hav e n of these domain variables— one for each attribute.

11 Slide 6- 11  An expression of the domain calculus is of th e form { x 1, x 2,..., x n | COND(x 1, x 2,..., x n, x n+1, x n+2,..., x n+m )} ◦ where x 1, x 2,..., x n, x n+1, x n+2,..., x n+m are domai n variables that range over domains (of attributes) ◦ and COND is a condition or formula of the domain r elational calculus.

12 12  Component ⅰ. domain variable  xSno, xSname, xDept, xYear, …  Range  Declaration of Domain variable  STUDENT(xSno, xSname, xDept, xYear)

13 13  Components(con’t) ⅱ. atomic formula ① Range Expression, R(x 1,x 2,…,x n ) x i : Domain variable R : x i ’s range relation Value list is a relation R’s tuple ② Condition Expression, x θ y x, y : Domain variable θ: =, ≠,,≥ ③ Condition Expression, x θ c x : domain variable c : Constant

14 14  Component(con’t) iii. WFF, Well-formed formula  Atomic formula, boolean operator(∧,∨, ¬ ), quantifier (∀,∃) ① All atomic formulae are WFF ② if F is WFF, (F) and  F are WFF ③ if F and G are WFF, F∧G and F∨G are WFF ④ If F(f) is WFF where tuple variable t is free variable, ∀t(F(t)) and ∃t(F(t))are WFF ⑤ Expression applied above rules is WFF

15 Slide 6- 15 ◦ example ① { xSname | STUDENT(xSno, xSname, xYear, xDept)} ② { xSname | (∃xDept) (STUDENT(xSno, xSname, xYear, xDept)∧ xDept=‘CS’) } ③ { xSno, xDept | STUDENT(xSno, xSname, xYear, xDept)∧(∃xxSno)(∃xGrade) (ENROL(xxSno, xCno, xGrade, xMidterm, xFinal)∧ xSno=xxSno∧xGrade='A‘) }  Retrieve sno and sname of a student whose final is greater than or equal to 90. ◦ {xSno,xSname | (STUDENT(xSno,xSname,xYear,xDept) ∧ (∃xFinal)(∃xxSno) (ENROL(xxSno, xCno, xGrade, xMidterm, xFinal)∧xSno=xxSno ∧ xFinal ≥ 90) }

16 16  QBE (Query By Example)  In 1975, Zloof with IBM  Based on domain relational calculus  Use graphic display terminal  two-dimensional syntax  Table formed “skeleton“  Example  P. (called P dot) operator (for “print”) is placed in those columns which are requested for the result of the query. ◦ example element) : domain variable STUDENT SnoSnameYearDept P._STX3

17 Slide 6- 17  The language is very user-friendly, because it uses minimal syntax.  QBE was fully developed further with facilities for grouping, aggregation, updating etc. and i s shown to be equivalent to SQL.  The language is available under QMF (Query Management Facility) of DB2 of IBM and has b een used in various ways by other products li ke ACCESS of Microsoft, and PARADOX.

18 18  Simple Condition ◦ Retrieve sno and sname of students whose year = 4 and dept = ‘CS’  Duplication Elimination  'ALL’  allow duplication STUDENT SnoSnameYearDept P. 4CS STUDENT Sno SnameYearDept P.ALL4CS

19 19  Retrieve a table ◦ Retrieve all student  Simple way: P. in table name STUDENT SnoSnameYearDept P. STUDENT SnoSnameYearDept P.

20 20  Multiple condition ◦ 'OR' condition : Several rows ◦ 'AND' coondition : single row ENROL SnoCnoFinalMidterm P. C413  85 ENROL SnoCnoFinalMidterm P.C413  85

21 21  Multiple condition(con’t) ◦ Use condition box ENROL SnoCnoFinalMidterm P._EC_EF CONDITIONS _EC=C413 AND _EF  85

22 22  Retrieve with several tables  Join ◦ Retrieve name of student whose Final >= 85 or Cno = ‘C413’  Name is in STUDENT table  Sno is a join attribute ENROL SnoCnoFinalMidterm _STX _STY C413  85 STUDENT SnoSnameYearDept _STX _STY P.

23 23  Simple Insert ◦ Insert a tuple whose sno = 100 and cno =‘C413’ into ENROL. Note : primary key (Sno) should not be null  Insert with retrieval result ◦ Insert sno of a 4 year student into SENIOR ENROL SnoCnoGradeMidterm I.100C413 Final STUDENT SnoSnameYearDept _STX4 SENIOR Sno I._STX

24 24  Simple Delete ◦ Delete a tuple whose sno = 100  Delete with several table ◦ Delete student whose final < 60 in ENRON and STUDENT STUDENT SnoSnameYearDept D.100 STUDENT SnoSnameYearDept D._STX ENROL SnoCnoGradeMidterm D. _STX Final <60

25 25  Simple Update ◦ Update year to 2 of student whose snn = 300.  Update with arithmetic expression ◦ Increase final by 5 of student whose Cno = ‘C413’ STUDENT SnoSnameYearDept 300U.2 STUDENT SnoSnameYearDept U.3002 ENROL SnoCnoFinalMidterm U. C413 _G+5 _G


Download ppt "Jun-Ki Min.  Based on predicate calculus ◦ Predicate  a function whose value is true or false  non-procedural ◦ A relational calculus expression creates."

Similar presentations


Ads by Google