Advanced Database System

Slides:



Advertisements
Similar presentations
Database Languages Chapter 7. The Relational Algebra.
Advertisements

Copyright © C. J. Date 2005page 97 S#Y S1DURINGS3DURING [d04:d10][d08:d10] S2DURINGS4DURING [d02:d04][d04:d10] [d08:d10] WITH ( EXTEND T2 ADD ( COLLAPSE.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
1 541: Relational Calculus. 2 Relational Calculus  Comes in two flavours: Tuple relational calculus (TRC) and Domain relational calculus (DRC).  Calculus.
UNIT 15 Query Optimization Wei-Pang Yang, Information Management, NDHU Contents  15.1 Introduction to Query Optimization  15.2 The Optimization.
1 Relational Calculus Chapter 4 – Part II. 2 Formal Relational Query Languages  Two mathematical Query Languages form the basis for “real” languages.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Relational Calculus. Another Theoretical QL-Relational Calculus n Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus.
SPRING 2004CENG 3521 E-R Diagram for the Banking Enterprise.
Domains, Relations & Base RelVars (Ch
Relational Calculus CS 186, Fall 2003, Lecture 6 R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion.
Rutgers University Relational Calculus 198:541 Rutgers University.
Functional Dependencies Prof. Yin-Fu Huang CSIE, NYUST Chapter 11.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
Relational Algebra II. Semantics - where A WHERE X  Y –  : an operator like =, >, … Result of restriction A by the condition X  Y is A relation –with.
CS609 Introduction. Databases Current state? Future?
3-1 Unit 3 The Relational Model. 3-2 Wei-Pang Yang, Information Management, NDHU Outline  3.1 Introduction  3.2 Relational Data Structure  3.3 Relational.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Section 4.3.
3-1 Unit 3 The Relational Model. 3-2 Wei-Pang Yang, Information Management, NDHU Outline  3.1 Introduction  3.2 Relational Data Structure  3.3 Relational.
Logical Database Design ( 補 ) Unit 7 Logical Database Design ( 補 )
SQL (Chapter 2: Simple queries; Chapter 7 and 8: Nested and DML queries) Many of the examples in this document are based on the tables in the next slide.
Chapter 8 Relational Calculus. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-2 Topics in this Chapter Tuple Calculus Calculus vs. Algebra.
Chapter 10 Views. Topics in this Chapter What are Views For? View Retrievals View Updates Snapshots SQL Facilities.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4.
Chapter 7 Relational Algebra. Topics in this Chapter Closure Revisited The Original Algebra: Syntax and Semantics What is the Algebra For? Further Points.
Chapter 7 Relational Algebra. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-2 Topics in this Chapter Closure Revisited The Original Algebra:
Relational Calculus R&G, Chapter 4. Relational Calculus Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). Calculus.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
Relational Algebra.
Data Manipulation 21 After this lecture, you should be able to:  Use SQL SELECT statement effectively to retrieve the data from multiple related tables.
Algebra1 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
Relational Algebra Prof. Yin-Fu Huang CSIE, NYUST Chapter 7.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B.
Relations Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
Integrity Prof. Yin-Fu Huang CSIE, NYUST Chapter 9.
Views Prof. Yin-Fu Huang CSIE, NYUST Chapter 10. Advanced Database System Yin-Fu Huang 10.1Introduction Example: Var Good_Supplier View (S Where Status.
Relational Calculus. Relational calculus query specifies what is to be retrieved rather than how to retrieve it. – No description of how to evaluate a.
Structured Query Language IV Asma Ahmad (C. J. Date) Database Systems.
Relational Calculus Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Relational Calculus Chapter 4, Section 4.3.
Chapter 8 Relational Calculus.
Relational Algebra & Calculus
CSE202 Database Management Systems
STRUCTURE OF PRESENTATION :
UNIT 11 Query Optimization
Unit 2 DB2 and SQL.
STRUCTURE OF PRESENTATION :
Relational Calculus Chapter 4, Part B
STRUCTURE OF PRESENTATION :
STRUCTURE OF PRESENTATION :
Chapter 8 Relational Calculus.
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
DATABASE SYSTEM.
پايگاه داده ها.
Relational Calculus.
Φροντιστήριο SQL (από το βιβλίο του Date)
Unit 7 Normalization (表格正規化).
CS 186, Fall 2002, Lecture 8 R&G, Chapter 4
Chapter 6: Formal Relational Query Languages
STRUCTURE OF PRESENTATION :
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
Question 1: Basic Concepts (45 %)
Relational Algebra & Calculus
Relational Calculus Chapter 4, Part B 7/1/2019.
STRUCTURE OF PRESENTATION :
Relational Calculus Chapter 4 – Part II.
Relational Calculus Chapter 4, Part B
Presentation transcript:

Advanced Database System 8.1 Introduction The algebra provides a set of explicit operators that can be used to tell the system how to construct some desired relation from certain given relations. The calculus merely provides a notation for stating the definition of that desired relation in terms of those given relations. The query “Get supplier numbers and cities for suppliers who supply part P2” Algebra: 1. join supplier and shipment over S#. 2. restrict the result of that join to tuples for part P2. 3. project the result of that restriction over S# and City. Calculus: Get S# and City for suppliers such that there exists a shipment Sp with the same S# value and with P# value P2. Advanced Database System

Advanced Database System 8.1 Introduction (Cont.) prescriptive (Algebra) vs. descriptive (Calculus) procedural (Algebra) vs. nonprocedural (Calculus) The algebra and the calculus are logically equivalent. Relation calculus is based on a branch of mathematical logic called predicate calculus. Tuple calculus: range variables over relations Language called QUEL Range of Sx Is S; Retrieve (Sx.S#) Where Sx.City=“London”; Domain calculus: range variables over domains Language called Query-By-Example (QBE) Advanced Database System

Advanced Database System 8.2 Tuple Calculus Syntax (See Page 215-216) <relation exp> ::= Relation {<tuple exp commalist>} | <relvar name> | <relation op inv> | <with exp> | <introduced name> | (<relation exp>) <range var def> ::= Rangevar <range var name> Ranges Over <relation exp commalist>; <range attribute ref> ::= <range var name> . <attribute name> [ As <attribute name> ] Advanced Database System

Advanced Database System 8.2 Tuple Calculus (Cont.) <bool exp> ::= …all the usual possibilities, together with: | <quantified bool exp> <quantified bool exp> ::= <quantifier> <range var name> ( <bool exp> ) <quantifier> ::= Exists | Forall <relation op inv> ::= <proto tuple> [ Where <bool exp> ] <proto tuple> ::= …see the body of the text Advanced Database System

Advanced Database System 8.2 Tuple Calculus (Cont.) Range variables e.g. Rangevar Sx Ranges Over S; Rangevar Spx Ranges Over Sp; Rangevar Su Ranges Over ( Sx Where Sx.City = ‘London’ ), ( Sx Where Exists Spx ( Spx.S# = Sx.S# And Spx.P# = P#(‘P1’) ) ); Advanced Database System

Advanced Database System 8.2 Tuple Calculus (Cont.) Free and bound variable references Let V be a range variable References to V in Not p ⇒ according as they are free or bound in p References to V in p And q and p Or q ⇒ according as they are free or bound in p or q References to V that are free in p are bound in Exists V (p) and Forall V (p) Advanced Database System

Advanced Database System 8.2 Tuple Calculus (Cont.) Examples Sx.S#=S#(‘S1’) Sx.S#=Spx.S# Spx.P#≠Px.P# ⇒ Sx, Px, and Spx are free Px.Weight < Weight(15.5) And Px.City=’Oslo’ Not (Sx.City=‘London’) Sx.S#=Spx.S# And Spx.P#≠Px.P# Px.Color=Color(‘Red’) Or Px.City=‘London’ Exists Spx (Spx.S#=Sx.S# And Spx.P#=P#(‘P2’)) Forall Px (Px.Color=Color(‘Red’)) ⇒ Spx and Px are bound and Sx is free. Advanced Database System

Advanced Database System 8.2 Tuple Calculus (Cont.) Quantifiers Exists V ( p (V) ) ⇒ False Or p(t1) Or ... Or p(tm) Forall V ( p(V) ) ⇒ True And p(t1) And ... And p(tm) Free and bound variable references revisited Exists x ( x > 3 ) ⇔ Exists y ( y > 3 ) Exists x ( x > 3 ) and x < 0 ⇔ Exists y ( y > 3 ) and x < 0 Exists y ( y > 3 ) and y < 0 Advanced Database System

Advanced Database System 8.3 Examples Exam 1: {Sx.S#, Sx.Status} Where Sx.City=‘Paris’ And Sx.Status > 20 Exam 2: {Sx.S# As Sa, Sy.S# As Sb} Where Sx.City=Sy.City And Sx.S# < Sy.S# Exam 3: Sx Where Exists Spx (Spx.S#=Sx.S# And Spx.P#=P#(‘P2’)) Exam 4: Sx.Sname Where Exists Spx (Sx.S#=Spx.S# And Exists Px (Px.P#=Spx.P# And Px.Color=Color(‘Red’))) Exam 5: Sx.Sname Where Exists Spx ( Exists Spy (Sx.S#=Spx.S# And (Spx.P#=Spy.P# And Spy.S#=S#(‘S2’))) Advanced Database System

Advanced Database System 8.3 Examples (Cont.) Exam 6: Sx.Sname Where Forall Px ( Exists Spx (Spx.S#=Sx.S# And Spx.P#=Px.P#)) Exam 7: Sx.Sname Where Not Exists Spx (Spx.S#=Sx.S# And Spx.P#=P#(‘P2’)) Exam 8: Sx.S# Where Forall Spx (Spx.S#≠S#(‘S2’) Or Exists Spy (Spy.S#=Sx.S# And Spy.P#=Spx.P#)) If p Then q End If ⇔ (Not p) Or q Exam 9: Rangevar Pu Ranges Over (Px.P# Where Px.Weight > Weight(16.0)), (Spx.P# Where Spx.S#=S#(‘S2’)); Pu.P# Advanced Database System

Advanced Database System 8.4 Calculus vs. Algebra The algebra is at least as powerful as the calculus. Codd‘s reduction algorithm: by which an arbitrary expression of the calculus could be reduced to a semantically equivalent expression of the algebra. Example Query: Get names and cities for suppliers who supply at least one Athens project with at least 50 of every part. A calculus expression: {Sx.Sname, Sx.City} Where Exists Jx Forall Px Exists Spjx (Jx.City=‘Athens’ And Jx.J#=Spjx.J# And Px.P#=Spjx.P# And Sx.S#=Spjx.S# And Spjx.Qty≧Qty ( 50 ) ) Advanced Database System

8.4 Calculus vs. Algebra (Cont.) (See Page 226-228 & Fig. 8.1) Step 1: Advanced Database System

8.4 Calculus vs. Algebra (Cont.) Step 2: Cartesian product 5*6*2*24 =1440 tuples Step 3: Restriction Jx.J#=Spjx.J# And Px.P#=Spjx.P# And Sx.S#=Spjx.S# Advanced Database System

8.4 Calculus vs. Algebra (Cont.) Step 4: Apply the quantifiers from right to left Exists V ⇒ project the current intermediate result to eliminate all attributes of relation r. Forall V ⇒ divide the current intermediate result by the “restricted range” relation associated with V. Step 5: Projection Advanced Database System

8.4 Calculus vs. Algebra (Cont.) Why Codd defined precisely the eight algebraic operators? 1. Inherently implementable 2. A yardstick for measuring the expressive power of any given database language A language is said to be relationally complete if it is at least as powerful as the calculus. Any given language L is complete, if it is sufficient to show that L includes analogs of each of the eight algebraic operators. e.g. SQL Relational completeness does not necessarily imply any other kind of completeness. Advanced Database System

8.5 Computational Capabilities Exam 1: {Px.P#, Px.Weight*454 As Gmwt} Where Px.Weight*454 > Weight(10000.0) Exam 2: {Sx, ‘Supplier’ As Tag} Exam 3: {Spx, Px.Weight*Spx.Qty As Shipwt} Where Px.P#=Spx.P# Exam 4: {Px.P#, Sum(Spx Where Spx.P#=Px.P#, Qty) As Totqty} Exam 5: Sum(Spx, Qty) As Grandtotal Exam 6: {Sx.S#, Count(Spx Where Spx.S#=Sx.S#) As #_Of_Part} Exam 7: Rangevar Py Ranges Over P; Px.City Where Count (Py Where Py.City=Px.City And Py.Color=(‘Red’)) > 5 Advanced Database System

Advanced Database System 8.6 SQL Facilities Exam 1: Select Px.Color, Px.City From P As Px Where Px.City <> ‘Paris’ And Px.Weight > Weight (10.0); Exam 2: Select P.P#, P.Weight*454 As Gmwt From P; Exam 3: 1. Select S.*, P.P#, P.Pname, P.Color, P.Weight From S, P Where S.City=P.City; 2. S Join P Using City; 3. S Natural Join P; Exam 4: Select Distinct S.City As Scity, P.City As Pcity From S Join SP Using S# Join P Using P#; Exam5: Select A.S# As Sa, B.S# As Sb From S As A, S As B Where A.City=B.City And A.S# < B.S#; Advanced Database System

Advanced Database System 8.6 SQL Facilities (Cont.) Exam 6: Select Count(*) As N From S; Exam 7: Select Max(Sp.Qty) As Maxq, Min(Sp.Qty) As Minq From Sp Where Sp.P#=P#(‘P2’); Exam 8: Select Sp.P#, Sum(Sp.Qty) As Totqty Group By Sp.P#; Exam 9: Select Sp.P# Group By Sp.P# Having Count(Sp.S#) > 1; Exam 10: Select Distinct S.Sname From S Where S.S# In (Select Sp.S# Where Sp.P#=P#(‘P2’)); Advanced Database System

Advanced Database System 8.6 SQL Facilities (Cont.) Exam 11: Select Distinct S.Sname From S Where S.S# In (Select Sp.S# From Sp Where Sp.P# In (Select P.P# From P Where P.Color=Color(‘Red’))); Exam 12: Select S.S# Where S.Status < (Select Max(S.Status) From S); Exam 13: Select Distinct S.Sname From S Where Exists (Select * From Sp Where Sp.S#=S.S# And Sp.P#=P#(‘P2’)); Advanced Database System

Advanced Database System 8.6 SQL Facilities (Cont.) Exam 14: Select Distinct S.Sname From S Where Not Exists (Select * From Sp Where Sp.S#=S.S# And Sp.P#=P#(‘P2’)); Exam 15: Select Distinct S.Sname From P And Sp.P#=P.P#)); Exam 16: Select P.P# From P Where P.Weight > P#(16.0) Union Select Sp.P# From Sp Where Sp.S#=S#(‘S2’) Exam 17: With T1 As (Select P.P#, P.Weight*454 As Gmwt From P) Select T1.P#, T1.Gmwt From T1 Where T1.Gmwt > Weight(10000.0); Advanced Database System

Advanced Database System 8.7 Domain Calculus The most immediately obvious difference between the domain and the tuple calculus is that the former supports an additional form of <bool exp> called a membership condition. R { <pair commalist> } e.g. Sx Sx Where S {S# Sx} Sx Where S {S# Sx, City ‘London’} {Sx, Cityx} Where S {S# Sx, City Cityx} And Sp {S# Sx, P# P#(‘P2’)} {Sx, Px} Where S {S# Sx, City Cityx} And P {P# Px, City Cityy} And Cityx≠Cityy Advanced Database System

8.7 Domain Calculus (Cont.) Exam 1: Sx Where Exists Statusx (Statusx > 20 And S {S# Sx, Status Statusx, City ‘Paris’}) Exam 2: {Sx As Sa, Sy As Sb} Where Exists Cityz (S {S# Sx, City Cityz} And S {S# Sy, City Cityz} And Sx < Sy) Exam 3: Namex Where Exists Sx Exists Px (S {S# Sx, Sname Namex} And Sp {S# Sx, P# Px} And P {P# Px, Color Color(‘Red’)}) Exam 4: Namex Where Exists Sx Exists Px Sp {S# S#(‘S2’), P# Px}) Advanced Database System

8.7 Domain Calculus (Cont.) Exam 5: Namex Where Exists Sx (S {S# Sx, Sname Namex} And Forall Px (If P {P# Px} Then Sp {S# Sx, P# Px} End If)) Exam 6: Namex Where Exists Sx (S {S# Sx, Sname Namex} And Not Sp {S# Sx, P# P#(‘P2’)}) Exam 7: Sx Where Forall Px (If Sp {S# S#(‘S2’), P# Px} End If) Exam 8: Px Where Exists Weightx (P {P# Px, Weight Weightx} And Weightx > Weight(16.0)) Or Sp {S# S#(‘S2’), P# Px} Advanced Database System

Advanced Database System 8.8 Query-By-Example Exam 1: Exam 2: Exam 3: Exam 4: S S# Sname Status City P. > 20 Paris SP S# P# Qty UNQ. P. S S# Status City P.AO(2). P.DO(1). Paris S S# Status City P. Paris > 20 Advanced Database System

8.8 Query-By-Example (Cont.) Weight P. >= 16.0 <= 19.0 P P# Weight Gmwt P. _PW P. _PW * 454 S S# Sname _SX P. SP S# P# _SX P2 S S# City _SX _CX P P# City _PX _CX P. _SX _PX Advanced Database System

8.8 Query-By-Example (Cont.) S S# City _SX _CZ _SY P. _SX _SY SP S# P# Qty P2 _QX P.SUM._QX SP S# P# Qty G.P. _QY P.SUM._QY SP S# P# _SX G.P. Conditions CNT._SX > 1 Advanced Database System

8.8 Query-By-Example (Cont.) Weight _PX > 16.0 SP S# P# S2 _PY P. _PX _PY P P# Pname Color Weight City I. P7 24.0 Athens SP S# P# Qty D. > 300 P P# Pname Color Weight City P2 U.Yellow _WT U._WT+5 U.Oslo Advanced Database System

8.8 Query-By-Example (Cont.) SP S# Qty _SX U.5 S S# City _SX London Advanced Database System

Advanced Database System The End. Advanced Database System