SQL: Queries, Constraints, Triggers

41
SQL: Queries, Constraints, Triggers Chapter 5

description

SQL: Queries, Constraints, Triggers. Chapter 5. SQL . Data Manipulation Language (DML): subset of SQL which allows users to create queries and to insert , delete and modify rows . - PowerPoint PPT Presentation

Transcript of SQL: Queries, Constraints, Triggers

Page 1: SQL:  Queries, Constraints, Triggers

SQL: Queries, Constraints, TriggersChapter 5

Page 2: SQL:  Queries, Constraints, Triggers

SQL Data Manipulation Language (DML): subset of SQL which allows

users to create queries and to insert, delete and modify rows. Data Definition Language (DDL): subset of SQL which supports the

creation, deletion and modifications for table and views. Triggers and Advanced Integrity Constraints: actions that

executed by the DBMS whenever changes to the database meet conditions specified in the trigger.

Embedded and Dynamic SQL: allow SQL code to be called from a host language such as C or COBOL.

Client-Server Execution and Remote Database Access: controls how client application program can connect to an SQL database server, or access data from a database over a network.

Transaction management: commands allow a user to explicitly control aspects of how a transaction is to be executed.

Security: Provide mechanisms to control user’s access to data objects such as tables and views.

Advanced features: such as object-oriented features, recursive queries, decision support queries, data mining, spatial data, text and XML data management

Page 3: SQL:  Queries, Constraints, Triggers

The form of Basic SQL query: Select [DISTINCT] select-list From from-list Where qualification

Select should specify columns to be retained in the resultsFrom should specify a cross-product of tablesWhere specify selection conditions on the tables mentioned in

the from clause.

Example:Sailor(sid: integer, sname: string, rating: integer, age: real)Boats(bid: integer, bname: string, color: string)Reservation(sid: integer, bid: integer, day:date)

Page 4: SQL:  Queries, Constraints, Triggers

Example Instances We will use these

instances of the Sailors and Reserves relations in our examples.

If the key for the Reserves relation contained only the attributes sid and bid, how would the semantics differ?

sid sname rating age22 dustin 7 45.031 lubber 8 55.558 rusty 10 35.0

sid sname rating age28 yuppy 9 35.031 lubber 8 55.544 guppy 5 35.058 rusty 10 35.0

sid bid day22 101 10/10/9658 103 11/12/96

R1

S1

S2

Page 5: SQL:  Queries, Constraints, Triggers

relation-list A list of relation names (possibly with a range-variable after each name).

target-list A list of attributes of relations in relation-list

qualification Comparisons (Attr op const or Attr1 op Attr2, where op is one of ) combined using AND, OR and NOT.

DISTINCT is an optional keyword indicating that the answer should not contain duplicates. Default is that duplicates are not eliminated!

Basic SQL QuerySELECT [DISTINCT] target-listFROM relation-listWHERE qualification

, , , , ,

Page 6: SQL:  Queries, Constraints, Triggers

Find the names and ages of all sailors Select DISTINCT S.sname, S.age From Sailor.S The result without using DISTINCT is:

Basic SQL QuerySELECT [DISTINCT] target-listFROM relation-listWHERE qualification

sname age

Dustin 45.0

Brutus 33.0

lubber 55.5

Andy 25.5

Rusty 35.0

Horatio 35.0

Zorba 16.0

Art 25.5

Bob 63.5

Page 7: SQL:  Queries, Constraints, Triggers

Semantics of an SQL query defined in terms of the following conceptual evaluation strategy:◦ Compute the 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.

This strategy is probably the least efficient way to compute a query! An optimizer will find more efficient strategies to compute the same answers.

Conceptual Evaluation Strategy

Page 8: SQL:  Queries, Constraints, Triggers

Instance R3 of Reserve Instance S4 of Sailors

Example of Conceptual Evaluation

sid bid day22 101 10/10/9658 103 11/12/96

sid sname

rating age

22 dustin 7 45.031 lubber 8 55.558 rusty 10 35.0

Find the names of sailors who have reserved boat number 103.

(sid) sname rating age (sid) bid day 22 dustin 7 45.0 22 101 10/ 10/ 96 22 dustin 7 45.0 58 103 11/ 12/ 96 31 lubber 8 55.5 22 101 10/ 10/ 96 31 lubber 8 55.5 58 103 11/ 12/ 96 58 rusty

rusty 10 35.0 22 101 10/ 10/ 96

58 10 35.0 58 103 11/ 12/ 96

SELECT S.snameFROM Sailors S, Reserves RWHERE S.sid=R.sid AND R.bid=103

The first step is to construct S4xR3 which shows the following results:

Page 9: SQL:  Queries, Constraints, Triggers

Example of Conceptual EvaluationThe second step is to apply the qualification S.sid R.sid AND R.bid=103.This step eliminates all but the last row from the instance shown in the following figure.

The third step is to eliminate unwanted columns, so only sname appears in the select clause

(sid) sname rating age (sid) bid day 22 dustin 7 45.0 22 101 10/ 10/ 96 22 dustin 7 45.0 58 103 11/ 12/ 96 31 lubber 8 55.5 22 101 10/ 10/ 96 31 lubber 8 55.5 58 103 11/ 12/ 96 58 rusty

rusty 10 35.0 22 101 10/ 10/ 96

58 10 35.0 58 103 11/ 12/ 96

sname

rusty

Page 10: SQL:  Queries, Constraints, Triggers

Really needed only if the same attribute (sid) appears twice in the FROM clause. The previous query can also be written as:

A Note on Range Variables

SELECT S.snameFROM Sailors S, Reserves RWHERE S.sid=R.sid AND bid=103

SELECT snameFROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103

It is good style,however, to userange variablesalways!OR

Page 11: SQL:  Queries, Constraints, Triggers

The join of Sailors and Reserves ensures that for each selected sname, the sailor has made some reservation.

Find sailors who’ve reserved at least one boat

SELECT S.snameFROM Sailors S, Reserves RWHERE S.sid=R.sid

Page 12: SQL:  Queries, Constraints, Triggers

This query contains a join of two tables, followed by a selection on the color of boats.

Find the sids of sailors who have reserved a red boat.

SELECT R.sidFROM Boats B, Reserves RWHERE B.bid = R.bid AND B.color = ‘red’

Page 13: SQL:  Queries, Constraints, Triggers

This query contains a join of three tables, followed by a selection on the color of boats.

The join with Sailors allow us to find the name of the sailor who according to reserves tuple R, has reserved a red boat scribed by tuple B.

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

SELECT S.snameFROM Sailors S, Reserves R, Boats BWHERE S.sid=R.sid AND B.bid = R.bid AND B.color = ‘red’;

Page 14: SQL:  Queries, Constraints, Triggers

Illustrates use of arithmetic expressions and string pattern matching: 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.

AS and = are two ways to name fields in result. LIKE is used for string matching. `_’ stands for exactly

one character and `%’ stands for 0 or more arbitrary characters.

Thus, ‘-AB%’ denote a pattern marching every string that contains at least three characters with the second and third characters being A and B respectively.

‘B-%B’ names begins and ends with B and has at least three characters.

Expressions and StringsSELECT S.age, age1= S.age-5, 2*S.age AS age2FROM Sailors SWHERE S.sname LIKE ‘B_%B’

Page 15: SQL:  Queries, Constraints, Triggers

Find the names of sailors who’ve reserved a red or a green boat

The query is easily expressed using the OR connective in the WHERE. However, the following query which is identical except for the use of ‘and’ rather ‘or’ turns out to be much more difficult:

Find the names of sailors who’ve reserved a red and a green boat Why? If we replace the use of OR in the previous query by AND, we would

retrieve the names of sailors who have reserved a boat that is both red and green.

The integrity constraint that bid is a key for Boats tells us that the same boat cannot have two colors.

So what will be the result of this query?

Empty

SELECT S.snameFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND (B.color=‘red’ OR B.color=‘green’)

Page 16: SQL:  Queries, Constraints, Triggers

Find the names of sailors who’ve reserved a red AND a green boat

We can think of R1 and B1 as rows that prove that sailor S.sid has reserved a red boat. R2 and B2 similarly prove that the same sailor has reserved a green boat.

S.sname is not included in the results unless five such rows S, R1, B1, R2, and B2 are found.

But, the previous query is difficult to understand, thus, a better solution foe these two queries is to use UNION and INTERSECT.

A correct statement of the above query using AND will be as follow:

SELECT S.snameFROM Sailors S, Reserves R1, Boats B1, Reserves R2, Boats B2, WHERE S.sid=R1.sid AND R1.bid=B1.bidAND S.sid=R2.sid AND R2.bid=B2.bid AND (B1.color=‘red’ AND B2.color=‘green’)

Page 17: SQL:  Queries, Constraints, Triggers

Find the names of sailors who’ve reserved a red or a green boat

UNION: Can be used to compute the union of any two union-compatible sets of tuples (which are themselves the result of SQL queries).

Also available: EXCEPT (What do we get if we replace UNION by EXCEPT?)

SELECT S.snameFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND (B.color=‘red’ OR B.color=‘green’)

SELECT S.snameFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’UNIONSELECT S2.snameFROM Sailors S, Boats B2, Reserves R2WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=‘green’

Page 18: SQL:  Queries, Constraints, Triggers

Find name of sailors who’ve reserved a red and a green boat

INTERSECT: Can be used to compute the intersection of any two union-compatible sets of tuples.

Included in the SQL/92 standard, but some systems don’t support it.

Now try the same query but for sid not sname.

SELECT S.snameFROM Sailors S, Boats B1, Reserves R1, Boats B2, Reserves R2WHERE S.sid=R1.sid AND R1.bid=B1.bid AND S.sid=R2.sid AND R2.bid=B2.bid AND (B1.color=‘red’ AND B2.color=‘green’)

SELECT S.snameFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’INTERSECTSELECT S2.snameFROM Sailors S2, Boats B2, Reserves R2WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=‘green’

Page 19: SQL:  Queries, Constraints, Triggers

Find the sids of all sailors who have reserved red boats but not green boats.

Sailors 22, 64 and 31 have reserved red boats. Sailors 22, 74 and 31 have reserved green boats. Hence the answer contains just the 64.

But do we need all relations in this query????

So do it.SELECT S.sidFROM Boats B, Reserves RWHERE R.bid=B.bid AND

B.color=‘red’EXCEPTSELECT S2.sidFROM Boats B2, Reserves R2WHERE R2.bid=B2.bid AND B2.color=‘green’

SELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’EXCEPTSELECT S2.sidFROM Sailors S2, Boats B2, Reserves R2WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=‘green’

Page 20: SQL:  Queries, Constraints, Triggers

Find the sids of all sailors who have a rating of 10 or reserved boat 104.

The first part of the union returns the sids 58 and 71. The second part returns 22 and 31. The answer is, therefore, the set of sids 22, 31, 58 and 71

SELECT S.sidFROM Sailors S, WHERE S.rating = 10UNIONSELECT R.sidFROM Reserves RWHERE R.bid = 104;

Page 21: SQL:  Queries, Constraints, Triggers

Is a query that has another query embedded within it; the embedded query is called subquery.

A very powerful feature of SQL: a WHERE clause can itself contain an SQL query! (Actually, so can FROM and HAVING clauses.)

To understand semantics of nested queries, think of a nested loops evaluation: For each Sailors tuple, check the qualification by computing the subquery.

Nested Queries

Page 22: SQL:  Queries, Constraints, Triggers

The nested subquery computes the (multi) set of sids for sailors who have reserved boat 103 (22,31 and 74 on instances R2 and S3) and the top query retrieve the names of sailors whose sid is in this set.

To find sailors who’ve not reserved #103, use NOT IN. The best way to understand a nested query is to think of it in terms of

conceptual strategy. In our example, the strategy consists of examining rows of Sailors and for each row, evaluating the subquery over Reserves.

Construct the cross-product of the tables in the FROM clause of the top level query as before. For each row in the cross product, recompute the subquery.

Nested Queries

SELECT S.snameFROM Sailors SWHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103)

Find names of sailors who’ve reserved boat #103:

Page 23: SQL:  Queries, Constraints, Triggers

The innermost subquery find the set of bids of red boats (102 and 104). The subquery one level above finds the set of sids of sailors who have reserved one of these boats (22,31 and 64).

The top level query finds the names of sailors whose sid is in this set of sids. So we get Dustin, Lubber and Horatio.

Q:

Nested Queries

SELECT S.snameFROM Sailors SWHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid IN (Select B.bid

FROM Boats B WHERE B.color = ‘red’)

Find names of sailors who’ve reserved a red boat: The subquery might itself contain another nested subquery, in

which case we apply the same idea one more time, leading to an evaluation strategy with several levels of nested loop.

Page 24: SQL:  Queries, Constraints, Triggers

EXISTS is another set comparison operator, like IN. It test whether a set is nonempty.

Thus for each Sailor row S we test whether the set of Reserves rows R such that R.bid =103 AND S.sid =R.sid is non empty. If so, Sailor S has reserved boat 103, and we retrieve the name.

The occurrence of S in the subquery is called correlation and such queries are called correlated queries.

Nested Queries with Correlation

SELECT S.snameFROM Sailors SWHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid)

Find names of sailors who’ve reserved boat #103:

In the nested query, the inner subquery has been completely independent of the outer query. The inner sub query could depend on the rows currently being examined in the outer query.

Page 25: SQL:  Queries, Constraints, Triggers

We’ve already seen IN, EXISTS and UNIQUE. Can also use NOT IN, NOT EXISTS and NOT UNIQUE.

Also available: op ANY, op ALL, where op is one of the arithmetic comparison operator

Find sailors whose rating is greater than that of some sailor called Horatio:

What this query will compute ? On instance Sailors, this computes the sids 31, 32, 58, 71 and 74. What if the query was: Find sailors whose rating is better than

every sailor called Horatio? Just replace ANY with ALL. What is the result? Sids 58 and 71.

More on Set-Comparison Operators

,,,,,

SELECT *FROM Sailors SWHERE S.rating > ANY (SELECT S2.rating FROM Sailors S2 WHERE S2.sname=‘Horatio’)

Page 26: SQL:  Queries, Constraints, Triggers

Find the sailors with the highest rating

The outer WHERE condition is satisfied only when S.rating is greater than or equal to each of these rating values, that is when it is the largest rating value.

In the instance S3, the condition is satisfied only for rating 10. and the answer is sids: 58 and 71

More on Set-Comparison Operators

SELECT S.sidFROM Sailors SWHERE S.rating >= ALL (SELECT S2.rating FROM Sailors S2)

Page 27: SQL:  Queries, Constraints, Triggers

Find the names of sailors who have reserved both a red and a green boats.

The query can be understood as follow: Find all sailors who have reserved a red boat and further have sids that are included in the set of sids of Sailors who have reserved a green boats. This formulation of the query illustrates how queries involving INTERSECT can be rewritten using IN

Queries using EXCEPT can be similarly rewritten by using NOT IN. Find the sids of Sailors who have reserved red boats but not green?

More on Nested query

SELECT S.snameFROM Sailors S, Reserves R, Boats BWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’

AND S.sid IN (SELECT S2.sidFROM Sailors S2, Boats B2, Reserves R2WHERE S2.sid=R2.sid AND R2.bid=B2.bid

AND B2.color=‘green’

Page 28: SQL:  Queries, Constraints, Triggers

Aggregate Operators It can be used to perform some computation

and summarization.

COUNT (*)COUNT ( [DISTINCT] A)SUM ( [DISTINCT] A)AVG ( [DISTINCT] A)MAX (A)MIN (A)

Find the average age of sailors with a rating of 10.SELECT AVG (S.age)FROM Sailors SWHERE S.rating=10

Count the number of Sailors:SELECT COUNT (*)FROM Sailors S;What is the answer? 10

Find the name and age of oldest Sailor SELECT S.sname, MAX (S.age)FROM Sailors SThis query is illegal, we must use GROUP BY

Find the name and age of oldest Sailor SELECT S.sname, S.ageFROM Sailors SWHERE S.age=(SELECT MAX(S2.age) FROM Sailors S2)

single column

Page 29: SQL:  Queries, Constraints, Triggers

Find name and age of the oldest sailor(s)

The first query is illegal! (We’ll look into the reason a bit later, when we discuss GROUP BY.)

The third query is equivalent to the second query, and is allowed in the SQL/92 standard, but is not supported in some systems.

SELECT S.sname, MAX (S.age)FROM Sailors SSELECT S.sname, S.ageFROM Sailors SWHERE S.age = (SELECT MAX (S2.age) FROM Sailors S2)

SELECT S.sname, S.ageFROM Sailors SWHERE (SELECT MAX (S2.age) FROM Sailors S2) = S.age

Page 30: SQL:  Queries, Constraints, Triggers

So far, we’ve applied aggregate operators to all (qualifying) tuples. Sometimes, we want to apply them to each of several groups of tuples.

Consider: Find the age of the youngest sailor for each rating level.◦ In general, we don’t know how many rating levels

exist, and what the rating values for these levels are!◦ Suppose we know that rating values go from 1 to 10;

we can write 10 queries that look like this (!):

Motivation for Grouping

SELECT MIN (S.age)FROM Sailors SWHERE S.rating = i

For i = 1, 2, ... , 10:

Page 31: SQL:  Queries, Constraints, Triggers

The target-list contains (i) attribute names (ii) terms with aggregate operations (e.g., MIN (S.age)).◦ The attribute list (i) must be a subset of grouping-list.

Intuitively, each answer tuple corresponds to a group, and these attributes must have a single value per group. (A group is a set of tuples that have the same value for all attributes in grouping-list.)

Queries With GROUP BY and HAVING

SELECT [DISTINCT] target-listFROM relation-listWHERE qualificationGROUP BY grouping-listHAVING group-qualification

Page 32: SQL:  Queries, Constraints, Triggers

Find the age of the youngest sailor for each rating level:

Select S.rating, Min (S.age)From Sailor SGroup By S.rating

Queries With GROUP BY and HAVING

Page 33: SQL:  Queries, Constraints, Triggers

The cross-product of relation-list is computed Tuples that fail qualification are discarded,

`unnecessary’ fields are deleted, and The remaining tuples are partitioned into groups

by the value of attributes in grouping-list. The group-qualification is then applied to

eliminate some groups. Expressions in group-qualification must have a single value per group!◦ In effect, an attribute in group-qualification that is not an

argument of an aggregate op also appears in grouping-list. (SQL does not exploit primary key semantics here!)

One answer tuple is generated per qualifying group.

Conceptual Evaluation

Page 34: SQL:  Queries, Constraints, Triggers

rating minage 3 25.5 7 35.0 8 25.5

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 minage

FROM Sailors SWHERE S.age >= 18GROUP BY S.ratingHAVING COUNT (*) > 1

sid sname rating age 22 dustin 7 45.0 29 brutus 1 33.0 31 lubber 8 55.5 32 andy 8 25.5 58 rusty 10 35.0 64 horatio 7 35.0 71 zorba 10 16.0 74 horatio 9 35.0 85 art 3 25.5 95 bob 3 63.5 96 frodo 3 25.5

Answer relation:

Sailors instance:

Page 35: SQL:  Queries, Constraints, Triggers

rating minage 3 25.5 7 35.0 8 25.5

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

rating age 7 45.0 1 33.0 8 55.5 8 25.5 10 35.0 7 35.0 10 16.0 9 35.0 3 25.5 3 63.5 3 25.5

rating age 1 33.0 3 25.5 3 63.5 3 25.5 7 45.0 7 35.0 8 55.5 8 25.5 9 35.0

10 35.0

Page 36: SQL:  Queries, Constraints, Triggers

rating minage 7 35.0 8 25.5

Find age of the youngest sailor with age 18, for each rating with at least 2 such sailors and with every sailor under 60.

rating age 7 45.0 1 33.0 8 55.5 8 25.5 10 35.0 7 35.0 10 16.0 9 35.0 3 25.5 3 63.5 3 25.5

rating age 1 33.0 3 25.5 3 63.5 3 25.5 7 45.0 7 35.0 8 55.5 8 25.5 9 35.0

10 35.0

HAVING COUNT (*) > 1 AND EVERY (S.age <=60)

Page 37: SQL:  Queries, Constraints, Triggers

rating minage 3 25.5 7 35.0 8 25.5

Find age of the youngest sailor with age 18, for each rating with at least 2 sailors between 18 and 60.

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

FROM Sailors SWHERE S.age >= 18 AND S.age <= 60GROUP BY S.ratingHAVING COUNT (*) > 1

sid sname rating age 22 dustin 7 45.0 29 brutus 1 33.0 31 lubber 8 55.5 32 andy 8 25.5 58 rusty 10 35.0 64 horatio 7 35.0 71 zorba 10 16.0 74 horatio 9 35.0 85 art 3 25.5 95 bob 3 63.5 96 frodo 3 25.5

Answer relation:

Sailors instance:

Page 38: SQL:  Queries, Constraints, Triggers

Grouping over a join of three relations. What do we get if we remove B.color=‘red’ from the

WHERE clause and add a HAVING clause with this condition?

What if we drop Sailors and the condition involving S.sid?

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

SELECT B.bid, COUNT (*) AS scountFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’GROUP BY B.bid

Page 39: SQL:  Queries, Constraints, Triggers

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.

Summary

Page 40: SQL:  Queries, Constraints, Triggers

Consider the following schema:Flights(flno: integer, from: string, to: string, distance: integer, departs: time, arrives: time, price: real)Aircraft(aid: integer, aname: string, cruisingrange: integer)Certified(eid: integer, aid: integer)Employees(eid: integer, ename: string, salary: integer)

Note that the Employees relation describes pilots and other kinds of employees as well; every pilot is certified for some aircraft, and only pilots are certified to fly. Write each of the following queries in SQ:

1. Find the names of aircraft such that all pilots certified to operate them have salaries more than $80,000.

2. For each pilot who is certified for more than three aircraft, find the eid and the maximum cruisingrange of the aircraft for which she or he is certified.

3. Find the names of pilots whose salary is less than the price of the cheapest route from Los Angeles to Honolulu.

4. For all aircraft with cruisingrange over 1000 miles, find the name of the aircraft and the average salary of all pilots certified for this aircraft.

6. Find the aids of all aircraft that can be used on routes from Los Angeles to Chicago

End chapter exercises

Page 41: SQL:  Queries, Constraints, Triggers

End chapter excersises

sid sname

rating age

18 jones 3 30.041 jonah 6 56.022 ahab 7 44.063 moby null 15.0An Instance of Sailors

Write SQL queries to compute the average rating, using AVG; the sum of the ratings, using SUM; and the number of ratings, using COUNT.