Download - 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

Transcript
Page 1: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

1

Chapter 5

SQL: QUERIES, CONSTRAINTS, TRIGGERS

Page 2: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

2

INTRODUCTION- The current presentation is consistent with both SQL-92

and SQL: 99 (differences will be noted when necessary).

- We shall cover

- query capabilities

- advanced integrity constraints

- transaction management (later)

- We shall use, in the examples, the same sample relational instances as those used in the previous chapter.

Page 3: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

3

THE BASIC SQL QUERY- The basic form of an SQL query: optional (removes duplicate rows)

SELECT [DISTINCT] select-listFROM from-listWHERE qualification

- The conceptual evaluation strategy is an algorithm that constructs the relational instance that is created in response tq an SQL query statement:1. Compute the cross-product of the tables in the from-list.2. Delete rows in the cross-product that fail the qualification condition.3. Delete all columns that do not appear in the select-list.4. If DISTINCT is specified, eliminate duplicate rows.

This algorithm is laborious and may demand construction of very large tables.- A practical approach:

1. Locate the relevant tables in the from-list.2. Locate the attributes affected by the qualification condition. 3. locate the attributes required by the select-list.4. ‘Create a path’ between the tables containing the attributes from (3) above and the tables containing the attributes from (2) above (this path is established by ‘linking’ common attributes in pairs of tables, hopping between tables as necessary).

Page 4: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

4

EXAMPLES AND APPLICATIONS (1)Given the two sample instances

(Q1): Find the names of sailors who have reserved boat number 103

The SQL query is

SELECT S.sname

FROM Sailors S, Reserves R

WHERE S,sid = R.sid AND R.bid = 103

Page 5: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

5

EXAMPLES AND APPLICATIONS (2)Application of the conceptual evaluation strategy:

(1) Construct the cross-product S4 R3:

(2) Apply the qualification S.sid = R.sid AND R.bid = 103:

That eliminates all but the last row.

(3) Eliminate unwanted columns, leaving only S.sname.

The final result is {<rusty>}.

Page 6: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

6

EXAMPLES AND APPLICATIONS (3)The ‘practical’ approach would be to proceed as follows:

1. Locate the relevant tables: ‘Sailors’ and ‘Reserves’.

2. Locate attribute affected by the qualification i.e. R.bid

3. Locate attribute required by select list i.e. S.sname

4. Create a path from (2) to (3): The only attribute these two tables have in common is ‘sid’; therefore they must be linked through this common attribute.

Page 7: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

7

EXAMPLES OF BASIC SQL QUERIES (1)(Q16) Find the sids of sailors who have reserved a red boat.

SELECT R.sid

FROM Boats B, Reserves R

WHERE B.bid = R.bid AND B.color = ‘red’

N. B. We have to hop from ‘Boats’ to ‘Reserves’.

(Q2) Find the names of sailors who have reserved a red boat.

SELECT S. name

FROM Sailors S, Reserves R, Boats B

WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = ‘red’

N. B. We have to hop from ‘Boats’ to ‘Reserves’ to ‘Sailors’.

Page 8: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

8

EXAMPLES OF BASIC SQL QUERIES (2)

(Q3) Find the colors of boats reserved by Lubber.

SELECT B.color

FROM Sailors S, Reserves R, Boats B

WHERE S.sid = R.sid AND R.bid = B.bid AND S.name = ‘Lubber’

(Q4) Find the names of sailors who have reserved at least one boat.

SELECT S. name

FROM Sailors S, Reserves R

WHERE S.sid = R.sid

Page 9: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

9

EXPRESSIONS AND STRINGS IN THE SELECT COMMAND (1)

- Each item in a select-list can be of the formexpression AS column_name

where expression is any arithmetic or string expression over column names and column_name is a new name for this column in the answer.

- Each item can contain aggregates (see section 5.5).- Expressions over date and time values are part of the SQL standard.- Many implementations support various built-in functions (sqrt, sin, mod).- Each item in a qualification can be of the form

expression1 = expression2.- String comparison is available through the usual comparison operators. SQL

also allows the user to specify the alphabetical collation order.- A limited form of pattern matching can be realized with the LIKE operator, using the following symbols:

‘%’ which stands for zero or more arbitrary symbols ‘_’ (underscore) which stands for one arbitrary character.

Page 10: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

10

EXPRESSIONS AND STRINGS IN THE SELECT COMMAND (2)

(Q17) Compute increments for the rating of persons who have sailed two different boats on the same day.

SELECT S,name, S.rating + 1 AS rating

FROM Sailors S, Reserves R1, Reserves R2

WHERE S.sid = R1.sid AND S.sid = R2.sid

AND R1.day = R2.day AND R1.bid <> R2.bid

As another illustration:

SELECT S1.sname AS name1, S2.name AS name2

FROM Sailors S1, Sailors S2

WHERE 2*S1.rating = S2.rating - 1

Page 11: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

11

EXPRESSIONS AND STRINGS IN THE SELECT COMMAND (3)

(Q18) Find the ages of sailors whose name begins and ends with B and has at least three characters.

SELECT S.age

FROM Sailors S

WGERE S.name LIKE ‘B_%B’

Page 12: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

12

SET OPERATIONS IN SQLIt is often useful to think of the operations described by an SQL statement as a

sequence of set operations working on the tuples of various relational instances with the following operators

∪ : UNION

∩ : INTERSECT

- : EXCEPT (or MINUS)

∈ : IN

∉ : NOT IN

∃ : EXISTS

∄ : NOT EXISTS

‘op ANY’ and ‘op ALL’ compare a value with the elements in a given set, using comparison operator ‘op’.

N.B. Remember that, while relational algebra and calculus handle strictly sets, SQL usually deals with multiset unless stated otherwise.

Page 13: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

13

EXAMPLES OF SET OPERATIONS IN SQL (Q6) Find the names of sailors who have reserved both a red and a green boat. This example illustrates especially well the set operational aspect of SQL. The answer is

easily understood as the union of the sets of sailors who have reserved red boats and those who have reserved green boats.

SELECT S.name

FROM Sailors S, Reserves R, Boats B

WHERE B,color = ‘red’ AND B.bid = R.bid AND R.sid = S.sid

UNION

SELECT S2.name,

FROM Sailors S2, Boats B2, Reserves R2

WHERE B2.color = ‘green’ AND B2.bid = R2.bid AND R2.sid = B2.sid

N.B.

(1) Note that the order of the logical ‘and’ operand is immaterial, which is not always the case in natural language.

(2) If the query is changed to ‘or’, the SQL statement must use INTERSECTION.

(3) If the query requests the names of sailors who have reserved a red but not a green boat, the SQL statement must use EXCEPT.

(4) The default for UNION, INTERSECTION, and EXCEPT is that duplicates are eliminated unless ALL is added as in UNION ALL.

Page 14: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

14

NESTED QUERIESThe power of the set operators shows up in so-called nested queries,

Example:

(Q1) Find the names of sailors who have reserved boat 103.

SELECT S.name

FROM Sailors S

WHERE S.sid IN ( SELECT R.sid

FROM ReservesR

WHERE R.bid = 103 )

The inner set is created first and the outer set selects from it the required rows.

There can be as many degree of nesting as desired.

Effectively, nesting is used instead of ‘table-hopping’.

Inner set

Outer set

Page 15: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

15

MORE NESTED QUERIES(Q2) Find the names of sailors who have reserved a red boat.

SELECT S.name

FROM Sailors S

WHERE S.sid IN ( SELECT R.sid

FROM Reserves R

WHERE R.bid IN ( SELECT B.bid

FROM Boats B

WHERE B.color = ‘red’ ) )

Page 16: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

16

MORE ON NESTED QUERIES (1)- Nested queries can be correlated (section 5.4.2)

- Nested queries can use set comparison operators (these may be tricky)

Example:

(Q22) Find sailors whose rating is better (l.e. higher) than some sailor called Horatio.

SELECT S.sid

FROM Sailors S

WHERE S.rating > ANY ( SELECT S2.rating

FROM Sailors S2

WHERE S2.name = ‘Horatio’ )

N. B.

- What if the inner set is empty?

- In that case the comparison ‘>’ is defined to return false, and the query returns an empty answer set.

Page 17: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

17

MORE ON NESTED QUERIES (2)(Q23) Find sailors whose rating is better than every sailor called Horatio.

SELECT S.sidFROM Sailors SWHERE S.rating > ALL ( SELECT S2.rating

FROM Sailors S2 WHERE S2.name = ‘Horatio’ )

N. B.

(1) What if the inner set is empty? In this case the comparison is defined to return true! The query would then return the names of all the sailors.

(2) ‘IN’ is equivalent to ‘= ANY’. ‘NOT IN’ is equivalent to ‘<>’.

Page 18: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

18

AGGREGATE OPERATORSThe aggregate operators listed below were added for convenience to

SQL although they are not part of relational algebra nor of relational calculus.

1. COUNT ([DISTINCT] A): # of (unique) values in attribute A.

2. SUM ([DIDTINCT] A): Sum of all (unique) values in attribute A.

3. AVG ([DISTINCT] A): Average of all (unique) values in A.

4. MAX (A): Maximum value in attribute A.

5. MIN (A): Minimum value in attribute A.

N. B. There are a few tricky features in these operators.

Page 19: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

19

EXAMPLES OF AGGREGATE OPERATORS (1)(Q25) Find the average age of sailors with a rating of 10.

SELECT AVG (S.age)

FROM Sailors S

WHERE S.rating =10

(Q27) Find the name and age of the oldest sailor.

The following SQL statement is illegal:

SELECT S.name, MAX (S.age)

FROM Sailors S

It is illegal because

if the SELECT clause uses an aggregate operation, then it must use only aggregate operations unless the query uses a GROUP BY clause (we shall see these shortly).

Page 20: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

20

EXAMPLES OF AGGREGATE OPERATORS (2)

To avoid the previous problem we have to use a nested query:

SELECT S.name, S.age

FROM Sailors S

WHERE S.age = ( SELECT MAX (S2.age)

FROM Sailors S2)

Page 21: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

21

‘GROUP BY’ AND ‘HAVING’ CLAUSES (1)These special clauses are used when we want to apply aggregate operations to a

group of tuples (corresponding to some subset of the instance) grouped according to some specified condition.

Example:

(Q31) Find the age of the youngest sailor for each rating.

SELECT S.rating, MIN (S.age) AS minage

FROM Sailors S

GROUP BY S.rating

This query yields the answer:rating minage

1 33.0

3 25.5

7 35.0

8 25.5

9 35.0

10 35.0

Page 22: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

22

‘GROUP BY’ AND ‘HAVING’ CLAUSES (2)The general form of an SQL query including these new extensions is

now:

SELECT [DISTINCT] select-list

FROM from-list

WHERE qualification

GROUP BY grouping-list

HAVING group-qualification

Remarks:

1. Every attribute in select-list must also appear in grouping-list.

2. The expressions appearing in the group-qualification must have a single value per group.

3. If GROUP BY is omitted, the entire table is regarded as a single group.

Page 23: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

23

‘GROUP BY’ AND ‘HAVING’ CLAUSES (3)The conceptual evaluation strategy is extended as follows:

1. Construct the cross-product of tables in the from-list (as before).

2. Eliminate unwanted rows as per the WHERE clause (as before).

3. Eliminate unwanted columns keeping only those mentioned in the

SELECT, GROUP BY and HAVING clauses.

4. Sort the table according to the GROUP BY clause.

5. Eliminate unwanted rows by applying the group-qualification in the HAVING clause.

6. Generate one answer row for each qualified group.

N. B. (1) The order of steps 2 and 3 are especially important.

(2) Nesting can occur in the HAVING and (in some

implementations) in the FROM clauses.

Page 24: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

24

EXAMPLES OF ‘GROUP BY’ AND ‘HAVING’ CLAUSES (2)

Given

(Q33) For each red boat, find the number of reservations for this boat.SELECTB.bid, COUNT (*) AS reservationcountFROM Boats B, Reserves RWHERE B.color = ‘red’ AND B.bid = R.bidGROUP BY B.bid

Answer: {<102, 3>, <104, 2>}

Page 25: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

25

EXAMPLES OF ‘GROUP BY’ AND ‘HAVING’ CLAUSES (3)

(Q35) Find the average age of sailors who are of voting age (i.e. at least 18 years old) for each rating level that has at least two sailors.

SELECT S.rating, AVG ( S.age) AS average

FROM Sailors S

WHERE S.age >= 18

GROUP BY S.rating

HAVING 1 < ( SELECT COUNT (*)

FROM Sailors S2

WHERE S.rating = S2.rating )

Page 26: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

26

NULL VALUES – PROBLEMS (1)- NULL values represent a significant problem in SQL:

- (1) They cannot be avoided in real practice.

- (2) Their semantics is ambiguous.

- (3) They cannot be used in primary keys.

- (4) They represent special integrity constraints

- (5) They require special treatment in

- (A) predicate operations

- (B) relational operations

- (C) arithmetical operations

- (D) aggregate operations

We shall consider each of these problems in turn.

Page 27: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

27

NULL VALUES – PROBLEMS (2)- Impossible to avoid:

(i) They often show up as initial values under various conditions:

- certain attributes may be unknown at creation time

- certain attributes may not be applicable (e. g. spouse name)

- certain attributes may be confidential (e. g. telephone #)

(ii) They may appear as a result of certain join operations.

- The three different conditions shown above are not differentiated.

- NULL values in primary keys destroy unique identity.

- NOT NULL must be specified explicitly.

We shall next consider the special handling needed in various situations where null values are found.

Page 28: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

28

NULL VALUES – PROBLEMS (3)(A) Predicate Handling (logical values)A predicate specifying a NULL value for a variable cannot be

classified as either TRUE or FALSE. Three-valued logic is then used with the new value called UNKNOWN, yielding the following truth tables:

a ¬a

TRUE FALSE

FALSE TRUE

UNKNOWN UNKNOWN

a b a AND b A OR b

TRUE TRUE TRUE TRUE

TRUE FALSE FALSE TRUE

TRUE UNKNOWN UNKNOWN TRUE

FALSE TRUE FALSE TRUE

FALSE FALSE FALSE FALSE

FALSE UNKNOWN FALSE UNKNOWN

UNKNOWN TRUE UNKNOWN TRUE

UNKNOWN FALSE FALSE UNKNOWN

UNKNOWN UNKNOWN UNKNOWN UNKNOWN

Page 29: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

29

NULL VALUES – PROBLEMS (4)(B) Relational Operations

- If either, or both, operand in the comparison operations (<, >, =. etc.) is UNKNOWN, then the result is UNKNOWN.

(C) Arithmetic Operations

- If one or both arguments are UNKNOWN the result is also UNKNOWN.

(D) Aggregate Operations

- All the aggregate operators (with or without DISTINCT) simply discard NULL values except COUNT(*) where they get counted.

If these operators (apart from COUNT) are applied to only NULL values the result is NULL.

Page 30: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

30

NULL VALUES – PROBLEMS (5)Outer Joins

There are three kinds of outer joins:

right outer join, left outer join, full outer join.

Given the following instances

We obtain the following resultssid bid

22 101

31 Null

58 103

sid bid

22 101

58 103

sid bid

22 101

31 Null

58 103

Left Outer Join Right Outer Join Full Outer Join

Page 31: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

31

COMPLEX INTEGRITY CONSTRAINTS (1)

1. Constraints Over a Single Table (table constraints)

Format: CHECK conditional-expression

Example:

CREATE TABLE Sailors (sid INTEGER,

sname CHAR (10),

rating INTEGER,

age REAL,

PRIMARY KEY (sid),

CHECK (rating >= 1 AND rating <= 10 ))

Page 32: 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.

32

COMPLEX INTEGRITY CONSTRAINTS (2)

2. Constraints Over Several Tables (Assertions)

Example: Suppose we wish to enforce the constraint that the number of boats plus the number of sailors be less than 100.

CREATE ASSERTION

CHECK ( ( SELECT COUNT (S.sid) FROM Sailors S)

+ ( SELECT COUNT (B.bid) FROM Boats B)

< 100 )