SQL Structured Query Language Sequel Chapter 5 1

  • Slides: 50
Download presentation
SQL: Structured Query Language (‘Sequel’) Chapter 5 1

SQL: Structured Query Language (‘Sequel’) Chapter 5 1

SQL 1. Query Language 2. Constraints 3. Triggers 2

SQL 1. Query Language 2. Constraints 3. Triggers 2

SQL Overview v Data Manipulation Language (DML) • Queries, insert, delete, modify v Data

SQL Overview v Data Manipulation Language (DML) • Queries, insert, delete, modify v Data Definition Language (DDL) • Creation, deletion, modification tables and views • Creating and deleting index (commercial DB) v v Triggers and Advanced Integrity Constraints Embedded and Dynamic SQL • Calling SQL code using host language (C, etc. ) (Chapter 6) v Client-Server Execution and Remote Database Access • (chapter 7) v v v Transaction Management (chapter 21) Security Advanced features • OO, recursive queries, data mining, spatial data, text and XML 3

Running Example v Instances of the Sailors and Reserves relations in our examples. R

Running Example v Instances of the Sailors and Reserves relations in our examples. R 1 S 2 4

Basic SQL Query SELECT FROM WHERE v v v [DISTINCT] target-list relation-list qualification relation-list

Basic SQL Query SELECT FROM WHERE v v v [DISTINCT] target-list relation-list qualification relation-list A list of relation names target-list A list of attributes of relations in relation-list qualification • Comparisons (Attr op const or Attr 1 op Attr 2, where op is one of ) combined using AND, OR and NOT. v is optional keyword indicating that answer should not contain duplicates. Default is that duplicates are not eliminated! DISTINCT 5

Example Query SELECT FROM WHERE S. sname Sailors S, Reserves R S. sid=R. sid

Example Query SELECT FROM WHERE S. sname Sailors S, Reserves R S. sid=R. sid AND R. bid=103 6

Comparisons in Oracle 7

Comparisons in Oracle 7

Example Query SELECT FROM WHERE S. sname Sailors S, Reserves R S. sid=R. sid

Example Query SELECT FROM WHERE S. sname Sailors S, Reserves R S. sid=R. sid AND R. bid=103 8

Conceptual Evaluation Strategy v Semantics of SQL query defined in terms of following conceptual

Conceptual Evaluation Strategy v Semantics of SQL query defined in terms of following conceptual evaluation strategy: • • Compute cross-product of relation-list. Discard resulting tuples if they fail qualifications. Delete attributes that are not in target-list. If DISTINCT is specified, eliminate duplicate rows. v Probably the least efficient way! v Optimizer will find more efficient strategies to compute the same answers. 9

R S SELECT S. sname FROM Sailors S, Reserves R WHERE S. sid=R. sid

R S SELECT S. sname FROM Sailors S, Reserves R WHERE S. sid=R. sid AND R. bid=103 10

A Note on Range Variables v v Needed only if same relation appears twice

A Note on Range Variables v v Needed only if same relation appears twice in FROM clause. The previous query can also be written as: SELECT S. sname FROM Sailors S, Reserves R WHERE S. sid=R. sid AND bid=103 OR SELECT sname FROM Sailors, Reserves WHERE Sailors. sid=Reserves. sid AND bid=103 It is good style, however, to use range variables always! 12

Find sailors who’ve reserved at least one boat SELECT S. sid FROM Sailors S,

Find sailors who’ve reserved at least one boat SELECT S. sid FROM Sailors S, Reserves WHERE S. sid=R. sid v R Questions: • • What is the exact output of this query ? Would adding DISTINCT to this query make a difference? What is the effect of replacing S. sid by S. sname in SELECT clause? Would adding DISTINCT to this variant of the query make a difference? 13

Expressions and Strings SELECT S. age, age 1=S. age-5, 2*S. age AS FROM Sailors

Expressions and Strings SELECT S. age, age 1=S. age-5, 2*S. age AS FROM Sailors S WHERE S. sname LIKE ‘B_%B’ v v v age 2 AS and = are two ways to name fields in result. LIKE is used for string matching. `_’ stands for any one character and `%’ stands for 0 or more arbitrary characters. Find triples (of ages of sailors and two fields defined by expressions) for sailors whose names begin and end with B and contain at least three characters. 14

Find sid’s of sailors who’ve reserved a red or a green boat SELECT S.

Find sid’s of sailors who’ve reserved a red or a green boat SELECT S. sid FROM Sailors S, Reserves R, Boats B WHERE S. sid=R. sid AND R. bid=B. bid AND (B. color=‘red’ OR B. color=‘green’) v UNION compute union of any two unioncompatible sets of tuples SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘red’ UNION SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘green’ 15

Find sid’s of sailors who’ve reserved a red or a green boat SELECT S.

Find sid’s of sailors who’ve reserved a red or a green boat SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND (B. color=‘red’ OR B. color=‘green’) v If we replace OR by AND in the first version, what do we get? SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘red’ UNION SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘green’ 16

Find sid’s of sailors who’ve reserved a red and a green boat v v

Find sid’s of sailors who’ve reserved a red and a green boat v v v INTERSECT: Can be used to compute the intersection of any two union-compatible sets of tuples. Key field! SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘red’ What happens if change S. sid as S. sname? ? ? INTERSECT SQL/92 standard, but some systems don’t support INTERSECT ? SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘green’ 17

Find sid’s of sailors who’ve reserved a red and a Key field! green boat

Find sid’s of sailors who’ve reserved a red and a Key field! green boat v What would query look like without INTERSECT? SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘red’ INTERSECT SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘green’ SELECT S. sid FROM Sailors S, Boats B 1, Reserves R 1, Boats B 2, Reserves R 2 WHERE S. sid=R 1. sid AND R 1. bid=B 1. bid AND S. sid=R 2. sid AND R 2. bid=B 2. bid AND (B 1. color=‘red’ AND B 2. color=‘green’) 18

Find sid’s of sailors who’ve reserved a red and not a green boa v

Find sid’s of sailors who’ve reserved a red and not a green boa v What if we want to compute the DIFFERENCE, called EXCEPT in SQL? v SQL/92 standard, but some systems don’t support EXCEPT. SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘red’ EXCEPT SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘green’ SELECT S. sid FROM Sailors S, Boats B 1, Reserves R 1, Boats B 2, Reserves R 2 WHERE S. sid=R 1. sid AND R 1. bid=B 1. bid AND S. sid=R 2. sid AND R 2. bid=B 2. bid AND (B 1. color='red' AND NOT(B 2. color = 'green')) 20

About duplication v Default: Eliminate duplicate v Unless Use UNION ALL INTERSECT ALL EXCEPT

About duplication v Default: Eliminate duplicate v Unless Use UNION ALL INTERSECT ALL EXCEPT ALL # of copies of a row in result (m+n) min(m, n) m-n 21

Nested Queries Find names of sailors who’ve reserved boat #103: SELECT S. sname FROM

Nested Queries Find names of sailors who’ve reserved boat #103: SELECT S. sname FROM Sailors S WHERE S. sid IN (SELECT R. sid FROM Reserves R WHERE R. bid=103) clauses can itself contain an SQL query! powerful feature of SQL. v WHERE, FROM, HAVING v nested loops evaluation: For each Sailors tuple, check the qualification by re-computing the subquery. 22

Nested Queries Evaluation Find names of sailors who’ve reserved boat #103: SELECT S. sname

Nested Queries Evaluation Find names of sailors who’ve reserved boat #103: SELECT S. sname FROM Sailors S WHERE S. sid IN (SELECT R. sid FROM Reserves R WHERE R. bid=103) v Nested loops evaluation: § For each Sailors tuple, check qualification by re-computing subquery. 23

Nested Queries Find names of sailors who’ve reserved boat #103: SELECT S. sname FROM

Nested Queries Find names of sailors who’ve reserved boat #103: SELECT S. sname FROM Sailors S WHERE S. sid IN (SELECT R. sid FROM Reserves R WHERE R. bid=103) v Change query to find sailors who’ve not reserved #103: S. sname FROM Sailors S WHERE S. sid NOT IN (SELECT R. sid FROM Reserves R WHERE R. bid=103) SELECT 24

Nested Queries Find names of sailors who’ve reserved boat #103: SELECT S. sname FROM

Nested Queries Find names of sailors who’ve reserved boat #103: SELECT S. sname FROM Sailors S WHERE S. sid IN (SELECT R. sid FROM Reserves R WHERE R. bid=103) v Could this query be rewritten without nesting? 25

Nested Queries with Correlation Find names of sailors who’ve reserved boat #103: SELECT S.

Nested Queries with Correlation Find names of sailors who’ve reserved boat #103: SELECT S. sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R. bid=103 AND v EXISTS S. sid=R. sid) is another set comparison operator, like IN. 26

Nested Queries with Correlation Find names of sailors who’ve reserved boat #103: SELECT S.

Nested Queries with Correlation Find names of sailors who’ve reserved boat #103: SELECT S. sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R. bid=103 AND v S. sid=R. sid) Why, in general, this subquery must be recomputed for each Sailors tuple ? 27

Nested Queries with Correlation Find names of sailors who’ve reserved boat #103: SELECT S.

Nested Queries with Correlation Find names of sailors who’ve reserved boat #103: SELECT S. sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R. bid=103 AND S. sid=R. sid) § Question: Finds sailors with at most one reservation for boat #103. § UNIQUE is used instead of EXISTS § * is replaced by R. bid. (Why? ) 28

More on Set-Comparison Operators v op ANY, op ALL, op IN v Op =

More on Set-Comparison Operators v op ANY, op ALL, op IN v Op = { v e. g. Find sailors whose rating is greater than that of some sailor called Horatio: SELECT * FROM Sailors S WHERE S. rating } > ANY (SELECT S 2. rating FROM Sailors S 2 WHERE S 2. sname=‘Horatio’) v IN is equal to =ANY v NOT IN is equal to <>ANY 29

Rewriting INTERSECT Queries Using IN Find sid’s of sailors who’ve reserved both a red

Rewriting INTERSECT Queries Using IN Find sid’s of sailors who’ve reserved both a red and a green boat: SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘red’ AND S. sid IN (SELECT S 2. sid FROM Sailors S 2, Boats B 2, Reserves R 2 WHERE S 2. sid=R 2. sid AND R 2. bid=B 2. bid AND B 2. color=‘green’) 30

Rewriting INTERSECT Queries Using IN Find sid’s of sailors who’ve reserved both a red

Rewriting INTERSECT Queries Using IN Find sid’s of sailors who’ve reserved both a red and a green boat: SELECT S. sid FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND B. color=‘red’ AND S. sid IN (SELECT S 2. sid FROM Sailors S 2, Boats B 2, Reserves R 2 WHERE S 2. sid=R 2. sid AND R 2. bid=B 2. bid AND B 2. color=‘green’) v Similarly, EXCEPT queries re-written using NOT IN. To find names (not sid’s) of Sailors who’ve reserved both red and green boats 31

Division in SQL Find sailors who’ve reserved all boats. SELECT S. sname FROM Sailors

Division in SQL Find sailors who’ve reserved all boats. SELECT S. sname FROM Sailors S WHERE NOT EXISTS ((SELECT B. bid FROM Boats B) EXCEPT (SELECT R. bid FROM Reserves R WHERE R. sid=S. sid)) 32

(1) Division in SQL Find sailors who’ve reserved all boats. v Let’s do it

(1) Division in SQL Find sailors who’ve reserved all boats. v Let’s do it without EXCEPT: SELECT S. sname FROM Sailors S WHERE NOT EXISTS ((SELECT B. bid FROM Boats B) EXCEPT (SELECT R. bid FROM Reserves R WHERE R. sid=S. sid)) (2) SELECT S. sname FROM Sailors S WHERE NOT EXISTS (SELECT B. bid FROM Boats B WHERE NOT EXISTS (SELECT R. bid Sailors S such that. . . FROM Reserves R there is no boat B without. . . WHERE R. bid=B. bid AND R. sid=S. sid)) a Reserves tuple showing S reserved B 33

Aggregate Operators v Significant extension of relational algebra. COUNT (*) COUNT ( [DISTINCT] A)

Aggregate Operators v Significant extension of relational algebra. COUNT (*) COUNT ( [DISTINCT] A) SUM ( [DISTINCT] A) AVG ( [DISTINCT] A) MAX (A) MIN (A) SELECT COUNT (*) FROM Sailors S Why no Distinct? SELECT AVG (S. age) FROM Sailors S WHERE S. rating=10 SELECT COUNT (DISTINCT FROM Sailors S WHERE S. sname=‘Bob’ S. rating) 35

Aggregate Operators v Significant extension of relational algebra. SELECT S. sname FROM Sailors S

Aggregate Operators v Significant extension of relational algebra. SELECT S. sname FROM Sailors S WHERE S. rating= (SELECT MAX(S 2. rating) FROM Sailors S 2) SELECT AVG ( DISTINCT S. age) FROM Sailors S WHERE S. rating=10 36

Find name and age of the oldest sailor(s) SELECT S. sname, MAX FROM Sailors

Find name and age of the oldest sailor(s) SELECT S. sname, MAX FROM Sailors S (S. age) • Is this first query legal? • No! Why not ? SELECT S. sname, S. age FROM Sailors S WHERE S. age = (SELECT MAX (S 2. age) FROM Sailors S 2) Is this second query legal? 39

Motivation for Grouping v v v So far, aggregate operators to all (qualifying) tuples.

Motivation for Grouping v v v So far, aggregate operators to all (qualifying) tuples. Question? apply aggregate to each group of tuples. Consider: Find the age of the youngest sailor for each rating level. § Suppose rating values {1, 2, …, 10}, 10 queries: For i = 1, 2, . . . , 10: § SELECT MIN (S. age) FROM Sailors S WHERE S. rating = i Question? • • we don’t know how many rating levels exist ? what the rating values for these levels are! 40

Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP

Queries With GROUP BY and HAVING SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification v The target-list contains : (i) attribute names (ii) aggregate-op (column-name) (e. g. , MIN (S. age)). v A group is a set of tuples that have the same value for all attributes in grouping-list. v REQUIREMENT: - The target-list (i) grouping-list. - Why? each answer tuple of a group must have a single value. 41

Find age of the youngest sailor with age 18, for each rating with at

Find age of the youngest sailor with age 18, for each rating with at least 2 such sailors SELECT S. rating, MIN (S. age) AS FROM Sailors S WHERE S. age >= 18 GROUP BY S. rating HAVING COUNT (*) > 1 min-age 42

Group. By --- Conceptual Evaluation 5. Compute the cross-product of relation-list Discard tuples that

Group. By --- Conceptual Evaluation 5. Compute the cross-product of relation-list Discard tuples that fail qualification Delete `unnecessary’ fields Partition the remaining tuples into groups by the value of attributes in grouping-list. (Group. By) Eliminate groups using the group-qualification (Having) v We will have a single value per group! 1. 2. 3. 4. That is, one answer tuple is generated per qualifying group. 43

Find age of the youngest sailor with age 18, for each rating with at

Find age of the youngest sailor with age 18, for each rating with at least 2 such sailors S. rating, MIN (S. age) AS min-age FROM Sailors S WHERE S. age >= 18 GROUP BY S. rating HAVING COUNT (*) > 1 SELECT Sailors instance: Answer relation: Q: Why not rating =9 ? 44

Find age of the youngest sailor with age 18, for each rating with at

Find age of the youngest sailor with age 18, for each rating with at least 2 such sailors. 45

Find age of the youngest sailor with age 18, for each rating with at

Find age of the youngest sailor with age 18, for each rating with at least 2 such sailors S. rating, MIN (S. age) AS min-age FROM Sailors S WHERE S. age >= 18 GROUP BY S. rating HAVING COUNT (*) > 1 SELECT Find age of the youngest sailor with age 18, for each rating with at least 2 such sailors and with every sailor under 60. HAVING COUNT (*) > 1 AND EVERY (S. age <=60) 46

Find age of the youngest sailor with age 18, for each rating with at

Find age of the youngest sailor with age 18, for each rating with at least 2 such sailors and with every sailor under 60. HAVING COUNT (*) > 1 AND EVERY (S. age <=60) What is the result of changing EVERY to ANY? 47

Find age of the youngest sailor with age 18, for each rating with at

Find age of the youngest sailor with age 18, for each rating with at least 2 sailors between 18 and 60. S. rating, MIN (S. age) AS min-age FROM Sailors S WHERE S. age >= 18 AND S. age <= 60 GROUP BY S. rating HAVING COUNT (*) > 1 SELECT Sailors instance: Answer relation: 48

For each red boat, find the number of reservations for this boat SELECT B.

For each red boat, find the number of reservations for this boat SELECT B. bid, COUNT (*) AS scount FROM Sailors S, Boats B, Reserves R WHERE S. sid=R. sid AND R. bid=B. bid AND GROUP BY B. bid B. color=‘red’ v Grouping over a Join of three relations. v Q: What do we get if we remove B. color=‘red’ from the WHERE clause and add a HAVING clause with this condition? • No difference. Illegal. • Only column in Group. By can appear in Having, unless in aggregate operator of Having v Q: What if we drop Sailors and the condition involving S. sid? 49

Find age of the youngest sailor with age > 18, for each rating with

Find age of the youngest sailor with age > 18, for each rating with at least 2 sailors (of any age) v HAVING clause can also contain a subquery. SELECT S. rating, MIN (S. age) FROM Sailors S WHERE S. age > 18 GROUP BY S. rating HAVING 1 < (SELECT COUNT (*) FROM Sailors S 2 WHERE S. rating=S 2. rating) v v Q: find only ratings with 2 sailors over 18 ? What if HAVING clause is replaced by: (two such sailors) § HAVING COUNT(*) >1 50

Find those ratings for which the average is the minimum over all ratings v

Find those ratings for which the average is the minimum over all ratings v WRONG : Aggregate operations cannot be nested! SELECT S. rating FROM Sailors S WHERE S. age = (SELECT MIN (AVG (S 2. age)) FROM Sailors S 2) v Correct solution (in SQL/92): SELECT Temp. rating, Temp. avgage FROM (SELECT S. rating, AVG (S. age) AS avgage FROM Sailors S GROUP BY S. rating) AS Temp WHERE Temp. avgage = (SELECT MIN (Temp. avgage) FROM Temp) 51

Null Values v Field values in a tuple are sometimes : § unknown (e.

Null Values v Field values in a tuple are sometimes : § unknown (e. g. , a rating has not been assigned) or § inapplicable (e. g. , no spouse’s name). § v SQL provides a special value null for such situations. The presence of null complicates many issues. § rating>8 ? ? If rating = null? What about AND, OR and NOT connectives? 52

Null Values v v SQL provides a special value null The presence of null

Null Values v v SQL provides a special value null The presence of null complicates many issues. § rating>8 ? ? If rating = null? What about AND, OR and NOT connectives? § We need a 3 -valued logic (true, false and unknown). Special operators IS NULL to check if value is/is not null. Disallow NULL value : NOT NULL § § § e. g. , WHERE clause eliminates rows that don’t evaluate to true. Duplicates? ? ? (treated as true implicitly) problem! New operators (outer Joins). 53

Outer Joins v v v Left Outer Join Right Outer Join Full Outer Join

Outer Joins v v v Left Outer Join Right Outer Join Full Outer Join SELECT S. sid, R. bid FROM Sailors S LEFT OUTER JOIN Reserves R WHERE S. sid = R. sid Q: Right Outer Join? Full Outer Join? SELECT S. sid, R. bid FROM Sailors S NATURAL LEFT OUTER JOIN Reserves R 54

Summary v v SQL was an important factor in the early acceptance of the

Summary v v SQL was an important factor in the early acceptance of the relational model; more natural than earlier procedural query languages. Relationally complete; in fact, significantly more expressive power than relational algebra. Even queries that can be expressed in RA can often be expressed more naturally in SQL. Many alternative ways to write a query; optimizer should look for most efficient evaluation plan. § In practice, users need to be aware of how queries are optimized and evaluated for best results. 55