Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will."— Presentation transcript:

1 1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS

2 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.

3 3 THE BASIC SQL QUERY - The basic form of an SQL query: optional (removes duplicate rows) SELECT [DISTINCT] select-list FROM from-list WHERE 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).

4 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

5 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 { }.

6 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.

7 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’.

8 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

9 9 EXPRESSIONS AND STRINGS IN THE SELECT COMMAND (1) - Each item in a select-list can be of the form expression 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.

10 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

11 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’

12 12 SET OPERATIONS IN SQL It 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.

13 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, FROMSailors S2, Boats B2, Reserves R2 WHEREB2.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.

14 14 NESTED QUERIES The 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

15 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’ ) )

16 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.

17 17 MORE ON NESTED QUERIES (2) (Q23) Find sailors whose rating is better than every sailor called Horatio. SELECT S.sid FROM Sailors S WHERE 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 ‘<>’.

18 18 AGGREGATE OPERATORS The 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.

19 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).

20 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)

21 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: ratingminage 133.0 325.5 735.0 825.5 935.0 1035.0

22 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.

23 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.

24 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 reservationcount FROMBoats B, Reserves R WHEREB.color = ‘red’ AND B.bid = R.bid GROUP BY B.bid Answer: {, }

25 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 )

26 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.

27 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.

28 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 TRUEFALSE TRUE UNKNOWN aba AND bA OR b TRUE FALSE TRUE UNKNOWN TRUE FALSETRUEFALSETRUE FALSE UNKNOWNFALSEUNKNOWN TRUEUNKNOWNTRUE UNKNOWNFALSE UNKNOWN

29 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.

30 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 results sidbid 22101 31Null 58103 sidbid 22101 58103 sidbid 22101 31Null 58103 Left Outer Join Right Outer Join Full Outer Join

31 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 ))

32 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 )


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

Similar presentations


Ads by Google