STRUCTURE OF PRESENTATION :

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

Aliaksei A. HolubeuAdvances in Database Query Processing Universität Konstanz, 2005 Optimizing an SQL-like Nested Query.
Database Languages Chapter 7. The Relational Algebra.
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 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Chapter 3 An Introduction to Relational Databases.
 CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014.
Domains, Relations & Base RelVars (Ch
Database Systems More SQL Database Design -- More SQL1.
Chapter 6 Relations. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Topics in this Chapter Tuples Relation Types Relation Values Relation.
Chapter 6 Relations. Topics in this Chapter Tuples Relation Types Relation Values Relation Variables SQL Facilities.
Database Systems Lecture # 8 11 th Feb,2011. The Relational Model of Data The term relation is basically just a mathematical term for a table. DBMS products.
Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming.
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.
6.8 Case Study: E-R for Supplier-and-Parts Database
Logical Database Design ( 補 ) Unit 7 Logical Database Design ( 補 )
Chapter 10 Views. Topics in this Chapter What are Views For? View Retrievals View Updates Snapshots SQL Facilities.
Chapter 7 Relational Algebra. Topics in this Chapter Closure Revisited The Original Algebra: Syntax and Semantics What is the Algebra For? Further Points.
Further Normalization I
Data Definition After this lecture, you should be able to:
Chapter 7 Relational Algebra. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-2 Topics in this Chapter Closure Revisited The Original 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.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
1 Algebra of Queries Classical Relational Algebra It is a collection of operations on relations. Each operation takes one or two relations as its operand(s)
Perform DFS from A A B C D E F G. Perform BFS from A A B C D E F G.
An Introduction to SQL For CS Overview of SQL  It is the standard language for relational systems, although imperfect  Supports data definition.
Relational Algebra Prof. Yin-Fu Huang CSIE, NYUST Chapter 7.
Unit 2 DB2 and SQL. 2-2 Wei-Pang Yang, Information Management, NDHU Outline of Unit Overview 2.2 Data Definition 2.3 Data Manipulation 2.4 The System.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
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.
Structured Query Language IV Asma Ahmad (C. J. Date) Database Systems.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 8 Relational Calculus.
Chapter (6) The Relational Algebra and Relational Calculus Objectives
STRUCTURE OF PRESENTATION :
More SQL: Complex Queries,
Lecture 3 : Structured Query Language (SQL)
CS580 Advanced Database Topics
Unit 2 DB2 and SQL.
6/22/2018.
STRUCTURE OF PRESENTATION :
for Computer Professionals
STRUCTURE OF PRESENTATION :
Chapter 3: Relational Model III
STRUCTURE OF PRESENTATION :
STRUCTURE OF PRESENTATION :
Chapter 8 Relational Calculus.
CS 405G: Introduction to Database Systems
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
Introduction to Database
INSTALLMENT NUMBER 21 Relational Comparisons
پايگاه داده ها.
CS 3630 Database Design and Implementation
Instructor: Mohamed Eltabakh
Φροντιστήριο SQL (από το βιβλίο του Date)
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Advanced Database System
CSCE 315 – Programming Studio Spring 2010 Project 1, Lecture 4
CS4222 Principles of Database System
SQL: Structured Query Language
Unit 7 Normalization (表格正規化).
Query Functions.
SQL: Structured Query Language
Objectives of the Lecture :
Joins and other advanced Queries
Question 1: Basic Concepts (45 %)
STRUCTURE OF PRESENTATION :
CS 405G: Introduction to Database Systems
Presentation transcript:

STRUCTURE OF PRESENTATION : 1. Basic database concepts ** Entr’acte 2. Relations 8. SQL tables 3. Keys and foreign keys 9. SQL operators I 4. Relational operators I 10. SQL operators II Relational operators II 11. Constraints and predicates 12. The relational model A. SQL constraints SQL vs. the relational model References Copyright C. J. Date 2013

SOME ADDITIONAL OPERATORS : MATCHING and NOT MATCHING EXTEND Image relations Aggregation, summarization, and related matters Copyright C. J. Date 2013

1. MATCHING : Many (most?) queries etc. involving join at all really require semijoin … E.g.: Get supplier information for suppliers who supply at least one part: S MATCHING SP /* result on next page */ Copyright C. J. Date 2013

S MATCHING SP : SNO SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 S4 Clark Copyright C. J. Date 2013

MATCHING (cont.) : Many (most?) queries etc. involving join at all really require semijoin … E.g.: Get supplier information for suppliers who supply at least one part: S MATCHING SP /* result on next page */ Shorthand for ( S JOIN SP ) { SNO , SNAME , STATUS , CITY } rx MATCHING ry  ( rx JOIN ry ) { A1, ..., An } where { A1, ..., An } = heading of rx Copyright C. J. Date 2013

1 (cont.). NOT MATCHING : Many (most?) queries etc. involving difference at all really require semidifference … E.g.: Get supplier information for suppliers who supply no parts at all: S NOT MATCHING SP /* result on next page */ Copyright C. J. Date 2013

S NOT MATCHING SP : SNO SNAME STATUS CITY S5 Adams 30 Athens Copyright C. J. Date 2013

NOT MATCHING (cont.) : Many (most?) queries etc. involving difference at all really require semidifference … E.g.: Get supplier information for suppliers who supply no parts at all: S NOT MATCHING SP /* result on next page */ Shorthand for S MINUS ( S MATCHING SP ) rx NOT MATCHING ry  rx MINUS ( rx MATCHING ry ) Copyright C. J. Date 2013

BY THE WAY : If the relations denoted by rx and ry are of the same type (i.e., have the same heading), then rx NOT MATCHING ry degenerates to rx MINUS ry E.g., consider S { CITY } NOT MATCHING P { CITY } /* analogous remark NOT true of semijoin */ Copyright C. J. Date 2013

2. EXTEND : For each part, get full part information, together with part weight in grams: EXTEND P : { GMWT := WEIGHT * 454 } PNO PNAME COLOR WEIGHT CITY GMWT P1 Nut Red 12.0 London 5448.0 P2 Bolt Green 17.0 Paris 7718.0 P3 Screw Blue Oslo P4 14.0 6356.0 P5 Cam P6 Cog 19.0 8626.0 Copyright C. J. Date 2013

ANOTHER EXAMPLE : Get PNO and gram weight for parts with gram weight > 7000.0: ( ( EXTEND P : { GMWT := WEIGHT * 454 } ) WHERE GMWT > 7000.0 ) { PNO , GMWT } Tutorial D syntax (EXTEND): EXTEND rx : { attribute assignment commalist } Copyright C. J. Date 2013

FURTHER EXAMPLES : EXTEND S : { TAG := 'Supplier' } SNO SNAME STATUS CITY TAG S1 Smith 20 London Supplier S2 Jones 10 Paris S3 Blake 30 S4 Clark S5 Adams Athens Copyright C. J. Date 2013

EXTEND ( P JOIN SP ) : { SHIPWT := WEIGHT * QTY } .. .. PNAME .. .. PNAME Nut . WEIGHT 12.0 SNO PNO QTY SHIPWT S1 P1 300 3600.0 etc., etc. Copyright C. J. Date 2013

RENAME revisited : RENAME is really just shorthand! S RENAME { CITY AS SCITY } Equivalent to: ( EXTEND S : { SCITY := CITY } ) { ALL BUT CITY } SNO SNAME STATUS SCITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 S4 Clark S5 Adams Athens RENAME is really just shorthand! Copyright C. J. Date 2013

What if part weights were recorded in grams, not pounds? EXTEND P : { WEIGHT := WEIGHT * 454 } PNO PNAME COLOR WEIGHT CITY P1 Nut Red 5448.0 London P2 Bolt Green 7718.0 Paris P3 Screw Blue Oslo P4 6356.0 P5 Cam P6 Cog 8626.0 Copyright C. J. Date 2013

3. IMAGE RELATIONS : Image relation = “image” in some relation of some tuple (usually a tuple in some other relation) E.g., image in SP of tuple in S for S4: ( SP WHERE SNO = 'S4' ) { ALL BUT SNO } PNO QTY P2 P4 P5 200 300 400 Very useful and widely applicable concept! So we define a shorthand ... Copyright C. J. Date 2013

S WHERE ( !!SP ) { PNO } = P { PNO } image in SP of “current tuple” in S relational equality “Get suppliers who supply all parts” !!! SNO SNAME STATUS CITY S1 Smith 20 London Copyright C. J. Date 2013

RECALL : t2 := ( t1 { SNO } ) , t3 := ( SP RENAME { PNO AS x } ) ) : Get part numbers for parts supplied by all suppliers in Paris WITH ( t1 := ( S WHERE CITY = 'Paris' ) , t2 := ( t1 { SNO } ) , t3 := ( SP RENAME { PNO AS x } ) ) : ( P WHERE ( t3 WHERE x = PNO ) { SNO } ⊇ t2 ) { PNO } Notes: WITH is just a syntactic trick Note use of relational comparison op “⊇” There’s a neater way to formulate this query /* see image relations, later */ Copyright C. J. Date 2013

Get part numbers for parts supplied by all suppliers in Paris ( P WHERE ( !!SP ) { SNO } ⊇ ( S WHERE CITY = 'Paris' ) { SNO } ) { PNO } Copyright C. J. Date 2013

ANOTHER EXAMPLE : S { SNO } /* suppliers */ SP { SNO , PNO } /* supplier supplies part PJ { PNO , JNO } /* part is used in project J { JNO } /* projects Get all (sno,jno) pairs such that: SNO sno currently appears in S, and ... JNO jno currently appears in J, and … Supplier sno supplies all parts used in project jno ( S JOIN J ) WHERE !!PJ  !!SP Pretty straightforward … Copyright C. J. Date 2013

IMAGE RELATIONS : “Very useful and widely applicable concept!” We’ll see more examples quite soon … Copyright C. J. Date 2013

4. AGGREGATE OPERATORS : An aggregate operator is not, in general, a relational operator (because the result usually isn’t a relation) … It’s an operator that derives a single value from the “aggregate” (i.e., the set or bag) of values of some attribute of some relation—or, for COUNT, from the entire relation. E.g.: X := COUNT ( S ) ; /* X = 5 */ Y := COUNT ( S { STATUS } ) ; /* Y = 3 */ Z := SUM ( SP { QTY } ) ; /* Z = 1000 */ Tutorial D syntax (aggregate operator invocation): agg op name ( rel exp [ , exp ] ) Copyright C. J. Date 2013

Tutorial D EXAMPLES : SUM ( SP { QTY } ) /* 1000 */ ( SP , QTY ) /* 3100 */ AVG ( SP , 3 * QTY ) /* 775 */ Legal agg op names include: COUNT SUM AVG MAX MIN AND OR XOR The exp can include attribute refs (in practice, almost always does) The exp must be omitted for COUNT ... Otherwise, it can be omitted only if the relation denoted by rel exp is of degree one, as in the first example above Copyright C. J. Date 2013

Aggregate operators are useful: In summarizations (i.e., aggregating something for each tuple in some projection of some relation, loosely speaking) In WHERE clauses (“generalized restriction”) Note: In both cases, image relations are almost always involved too Copyright C. J. Date 2013

SUMMARIZATION : For each supplier, get SNO and number of parts supplied: EXTEND S { SNO } : { PCT := COUNT ( !!SP ) } /* note the image relation reference */ SNO PCT S1 6 S2 2 S3 1 S4 3 S5 count of an empty set ... Copyright C. J. Date 2013

SUMMARIZATION (cont.) : For each supplier, get SNO and total shipment quantity: EXTEND S { SNO } : { TOTQ := SUM ( !!SP , QTY ) } /* note the image relation reference */ SNO TOTQ S1 1300 S2 700 S3 200 S4 900 S5 sum of an empty set of integers ... Copyright C. J. Date 2013

SUMMARIZATION (cont.) : For each Paris supplier, get supplier details and total, maximum, and minimum shipment quantity: EXTEND ( S WHERE CITY = 'Paris' ) : { TOTQ := SUM ( !!SP , QTY ) , MAXQ MAX ( !!SP , QTY ) , MINQ MIN ( !!SP , QTY ) } /* note use of “multiple EXTEND” */ Copyright C. J. Date 2013

ASIDE : Tutorial D currently supports explicit SUMMARIZE as well: EXTEND S { SNO } : { PCT := COUNT ( !!SP ) } SUMMARIZE S BY { SNO } : { PCT := COUNT ( ) } EXTEND S { SNO } : { TOTQ := COUNT ( !!SP ) } SUMMARIZE S BY { SNO } : { TOTQ := SUM ( QTY ) } But “summaries” (COUNT(), SUM(QTY)), etc. are somewhat suspect ... Copyright C. J. Date 2013

“GENERALIZED RESTRICTION” : Get suppliers with fewer than three shipments: S WHERE COUNT ( !!SP ) < 3 /* result = S2, S3, S5 */ Copyright C. J. Date 2013

Get suppliers for whom the total shipment quantity is less than 1000: S WHERE SUM ( !!SP , QTY ) < 1000 /* result = S2, S3, S4, S5 */ Copyright C. J. Date 2013

( EXTEND S { SNO } : { TOTQ := SUM ( !!SP , QTY ) } ) Get SNO and total shipment quantity for suppliers where total quantity > 250: ( EXTEND S { SNO } : { TOTQ := SUM ( !!SP , QTY ) } ) WHERE TOTQ > 250 Copyright C. J. Date 2013

ONE FINAL POINT : Result if argument empty for: COUNT 0 SUM AVG MAX MIN AND OR XOR 0 /* of appropriate type */ undefined undefined undefined TRUE FALSE FALSE Copyright C. J. Date 2013

SOME ADDITIONAL OPERATORS : MATCHING and NOT MATCHING EXTEND Image relations Aggregation, summarization, and related matters Copyright C. J. Date 2013

EXERCISES : What do the following expressions denote? P MATCHING S S NOT MATCHING ( SP WHERE PNO = 'P2' ) EXTEND P : { SCT := COUNT (!!SP) } P WHERE SUM ( !!SP , QTY ) < 500 Copyright C. J. Date 2013