SQL: Query Language

SQL: Query Language
CEng-553, 2008
1
History of SQL
SQL is based on the relational tuple calculus
™ SEQUEL: Structured English QUEry Language;
part of SYSTEM R, 1974
™ SQL/86: ANSI & ISO standard
™ SQL/89: ANSI & ISO standard
™ SQL/92 or SQL2: ANSI & ISO standard
™ SQL3: in the works...
™ SQL2 supported by ORACLE, SYBASE,
INFORMIX, IBM DB2, SQL SERVER,
OPENINGRES,...
™
CEng-553, 2008
2
SQL
SQL consists of the following parts:
™ Data Definition Language (DDL)
™ Interactive Data Manipulation Language
(Interactive DML)
™ Embedded Data Manipulation Language
(Embedded DML)
™ Views
™ Integrity
™ Transaction Control
™ Authorization
™ Catalog and Dictionary Facilities
CEng-553, 2008
3
AIRPORT
airportcode
name
city
state
FLT-SCHEDULE
flt#
airline
dtime
from-airportcode
atime
to-airportcode
miles
price
FLT-WEEKDAY
flt#
weekday
FLT-INSTANCE
flt#
date plane#
#avail-seats
AIRPLANE
plane#
plane-type
total-#seats
CUSTOMER
cust#
first
middle
last
phone#
street
city
state
zip
RESERVATION
flt#
date
CEng-553, 2008
cust#
seat#
check-in-status
ticket#
4
DDL - Overview
primitive types
™ domains
™ schema
™ tables
™
CEng-553, 2008
5
DDL - Primitive Types
™
numeric
(or INT), SMALLINT are subsets of the integers
(machine dependent)
REAL, DOUBLE PRECISION are floating-point and
double-precision floating-point (machine
dependent)
FLOAT(N) is floating-point with at least N digits
DECIMAL(P,D) (or DEC(P,D), or NUMERIC(P,D)), with P
digits of which D are to the right of the decimal
point
ƒ INTEGER
ƒ
ƒ
ƒ
CEng-553, 2008
6
DDL - Primitive Types (cont.)
™
character-string
ƒ CHAR(N) (or CHARACTER(N))
is a fixed-length
character string
ƒ VARCHAR(N) (or CHAR VARYING(N), or CHARACTER
VARYING(N)) is a variable-length character string
with at most N characters
™
bit-strings
ƒ BIT(N) is
a fixed-length bit string
ƒ VARBIT(N) (or BIT VARYING(N)) is a bit string with at
most N bits
CEng-553, 2008
7
DDL - Primitive Types (cont.)
™
date: Dates, containing a (4 digit) year, month and
date
ƒ Example: date ‘2005-7-27’
™
time: Time of day, in hours, minutes and seconds.
ƒ Example: time ‘09:00:30’
™
time ‘09:00:30.75’
timestamp: date plus time of day
ƒ Example: timestamp ‘2005-7-27 09:00:30.75’
™
interval: period of time
ƒ Example: interval ‘1’ day
ƒ Subtracting a date/time/timestamp value from another
gives an interval value
ƒ Interval values can be added to date/time/timestamp
values
CEng-553, 2008
8
Large-Object Types
™
Large objects (photos, videos, CAD files, etc.)
are stored as a large object:
ƒ blob: binary large object -- object is a large collection
of uninterpreted binary data (whose interpretation is
left to an application outside of the database system)
ƒ clob: character large object -- object is a large
collection of character data
ƒ When a query returns a large object, a pointer is
returned rather than the large object itself.
CEng-553, 2008
9
DDL - Domains
™
a domain can be defined as follows:
CREATE DOMAIN AIRPORT-CODE CHAR(3);
CREATE DOMAIN FLIGHTNUMBER CHAR(5);
using domain definitions makes it easier to see
which columns are related
™ changing a domain definition one place changes
it consistently everywhere it is used
™ default values can be defined for domains
™ constraints can be defined for domains (later)
™
CEng-553, 2008
10
DDL - Domains (cont.)
all domains contain the value, NULL.
™ to define a different default value:
™
CREATE DOMAIN AIRPORT-CODE CHAR(3) DEFAULT ‘<literal>’;
CREATE DOMAIN AIRPORT-CODE CHAR(3) DEFAULT ‘niladic
function’;
literal, such as ‘???’, ‘NO-VALUE’,...
™ niladic function, such as USER, CURRENT-USER,
™
SESSION-USER, SYSTEM-USER, CURRENT-DATE, CURRENTTIME, CURRENT-TIMESTAMP
™
defaults defined in a column takes precedence
over the above
CEng-553, 2008
11
DDL - Domains (cont.)
™
a domain is dropped as follows:
DROP DOMAIN AIRPORT-CODE RESTRICT;
DROP DOMAIN AIRPORT-CODE CASCADE;
restrict: drop operation fails if the domain is used
in column definitions
™ cascade: drop operation causes columns to be
defined directly on the underlying data type
™
CEng-553, 2008
12
DDL - Schema
™
create a schema:
CREATE SCHEMA AIRLINE AUTHORIZATION LEO;
the schema AIRLINE has now been created and is
owner by the user “LEO”
™ tables can now be created and added to the schema
™
™
to drop a schema:
DROP SCHEMA AIRLINE RESTRICT;
DROP SCHEMA AIRLINE CASCADE;
restrict: drop operation fails if schema is not empty
™ cascade: drop operation removes everything in the
schema
™
CEng-553, 2008
13
DDL - Tables (cont.)
™
to drop a table:
DROP TABLE RESERVATION RESTRICT;
DROP TABLE RESERVATION CASCADE;
restrict: drop operation fails if the table is
referenced by view/constraint definitions
™ cascade: drop operation removes referencing
view/constraint definitions
™
CEng-553, 2008
14
DDL - Tables (cont.)
™
to add a column to a table:
ALTER TABLE AIRLINE.FLT-SCHEDULE
ADD PRICE DECIMAL(7,2);
if no DEFAULT is specified, the new column will
have NULL values for all tuples already in the
database
™ to drop a column from a table
™
ALTER TABLE AIRLINE.FLT-SCHEDULE
DROP PRICE RESTRICT (or CASCADE);
restrict: drop operation fails if the column is
referenced
™ cascade: drop operation removes referencing
view/constraint definitions
™
CEng-553, 2008
15
Constraints on a Single Relation
not null
™ primary key
™ unique
™ check (P ), where P is a predicate
™
CEng-553, 2008
16
Not Null Constraint
™
Declare branch_name for branch is not null
branch_name char(15) not null
™
Declare the domain Dollars to be not null
create domain Dollars numeric(12,2) not null
CEng-553, 2008
17
The Unique Constraint
unique ( A1, A2, …, Am)
™ The unique specification states that the
attributes
A1, A2, … Am
form a candidate key.
™ Candidate keys are permitted to be null (in
contrast to primary keys).
™
CEng-553, 2008
18
The check clause
™
check (P ), where P is a predicate
Example: Declare branch_name as the primary key for
branch and ensure that the values of assets are nonnegative.
create table branch
(branch_name char(15),
branch_city
char(30),
assets
integer,
primary key (branch_name),
check (assets >= 0))
CEng-553, 2008
19
The check clause (Cont.)
™
The check clause permits domains to be
restricted:
ƒ Use check clause to ensure that an hourly_wage
domain allows only values greater than a specified
value.
create domain hourly_wage numeric(5,2)
constraint value_test check(value > = 4.00)
ƒ The domain has a constraint that ensures that the
hourly_wage is greater than 4.00
ƒ The clause constraint value_test is optional; useful
to indicate which constraint an update violated.
CEng-553, 2008
20
Referential Integrity
™
™
Ensures that a value that appears in one relation for a given set of
attributes also appears for a certain set of attributes in another relation.
ƒ Example: If “Perryridge” is a branch name appearing in one of the
tuples in the account relation, then there exists a tuple in the branch
relation for branch “Perryridge”.
Primary and candidate keys and foreign keys can be specified as part of
the SQL create table statement:
ƒ The primary key clause lists attributes that comprise the primary
key.
ƒ The unique key clause lists attributes that comprise a candidate key.
ƒ The foreign key clause lists the attributes that comprise the foreign
key and the name of the relation referenced by the foreign key. By
default, a foreign key references the primary key attributes of the
referenced table.
CEng-553, 2008
21
Referential Integrity in SQL – Example
create table customer
(customer_name
char(20),
customer_street
char(30),
customer_city
char(30),
primary key (customer_name ))
create table branch
(branch_name
char(15),
branch_city
char(30),
assets
numeric(12,2),
primary key (branch_name ))
CEng-553, 2008
22
Referential Integrity in SQL – Example (Cont.)
create table account
(account_number
char(10),
branch_name
char(15),
balance
integer,
primary key (account_number),
foreign key (branch_name) references branch )
create table depositor
(customer_name
char(20),
account_number
char(10),
primary key (customer_name, account_number),
foreign key (account_number ) references account,
foreign key (customer_name ) references customer )
CEng-553, 2008
23
DDL - Tables
™
to create a table in the AIRLINE schema:
CREATE TABLE AIRLINE.FLT-SCHEDULE
(FLT#
FLIGHTNUMBER NOT NULL,
AIRLINE
VARCHAR(25),
FROM-AIRPORTCODE AIRPORT-CODE,
DTIME
TIME,
TO-AIRPORTCODE
AIRPORT-CODE,
ATIME
TIME,
PRIMARY KEY (FLT#),
FOREIGN KEY (FROM-AIRPORTCODE) REFERENCES
AIRPORT(AIRPORTCODE),
FOREIGN KEY (TO-AIRPORTCODE)
REFERENCES AIRPORT(AIRPORTCODE));
CEng-553, 2008
24
DDL - Tables (cont.)
CREATE TABLE AIRLINE.FLT-WEEKDAY
(FLT#
FLIGHTNUMBER NOT NULL,
WEEKDAY
CHAR(2),
UNIQUE(FLT#, WEEKDAY),
FOREIGN KEY (FLT#)
REFERENCES FLT-SCHEDULE(FLT#));
CREATE TABLE AIRLINE.FLT-INSTANCE
(FLT#
FLIGHTNUMBER NOT NULL,
DATE
DATE
NOT NULL,
#AVAIL-SEATS SMALLINT,
PRIMARY KEY(FLT#, DATE),
FOREIGN KEY FLT#
REFERENCES FLT-SCHEDULE(FLT#));
CEng-553, 2008
25
DDL - Tables (cont.)
CREATE TABLE AIRLINE.RESERVATION
(FLT#
FLIGHTNUMBER NOT NULL,
DATE
DATE
NOT NULL,
CUST#
INTEGER
NOT NULL,
SEAT#
CHAR(4),
CHECK-IN-STATUS CHAR,
UNIQUE(FLT#, DATE, CUST#),
FOREIGN KEY (FLT#)
REFERENCES FLT-INSTANCE(FLT#),
FOREIGN KEY (DATE)
REFERENCES FLT-INSTANCE(DATE),
FOREIGN KEY (CUST#)
REFERENCES CUSTOMER(CUST#));
CEng-553, 2008
26
Relational Query Languages
Query languages: Allow manipulation and retrieval
of data from a database.
™ Relational model supports simple, powerful QLs:
™
ƒ
ƒ
™
Strong formal foundation based on logic.
Allows for much optimization.
Query Languages != programming languages!
ƒ
ƒ
ƒ
QLs not expected to be “Turing complete”.
QLs not intended to be used for complex calculations.
QLs support easy, efficient access to large data sets.
CEng-553, 2008
27
Formal Relational Query Languages
™
Two mathematical Query Languages form
the basis for “real” languages (e.g. SQL), and
for implementation:
ƒ Relational Algebra: More operational, very useful
for representing execution plans.
ƒ Relational Calculus: Lets users describe what they
want, rather than how to compute it. (Nonoperational, declarative.)
CEng-553, 2008
28
Example Instances
™
™
R1 sid
22
58
“Sailors” and “Reserves”
sid
S1
relations for our examples.
22
We’ll use positional or
named field notation,
31
assume that names of fields
58
in query results are
`inherited’ from names of
S2 sid
fields in query input
28
relations.
31
44
58
CEng-553, 2008
bid
day
101 10/10/96
103 11/12/96
sname rating age
dustin
7
45.0
lubber
8
55.5
rusty
10 35.0
sname rating age
yuppy
9
35.0
lubber
8
55.5
guppy
5
35.0
rusty
10 35.0
29
Relational Algebra
™
Basic operations:
ƒ
ƒ
ƒ
ƒ
ƒ
™
×
−
Additional operations:
ƒ
™
Selection (σ ) Selects a subset of rows from relation.
Projection (π ) Deletes unwanted columns from relation.
Cross-product ( ) Allows us to combine two relations.
Set-difference ( ) Tuples in reln. 1, but not in reln. 2.
Union ( U ) Tuples in reln. 1 and in reln. 2.
Intersection, join, division, renaming: Not essential, but
(very!) useful.
Since each operation returns a relation, operations
can be composed! (Algebra is “closed”.)
CEng-553, 2008
30
Projection
™
™
™
Deletes attributes that are not in
projection list.
Schema of result contains exactly
the fields in the projection list,
with the same names that they
had in the (only) input relation.
Projection operator has to
eliminate duplicates! (Why??)
ƒ Note: real systems typically
don’t do duplicate elimination
unless the user explicitly asks
for it. (Why not?)
CEng-553, 2008
sname
rating
yuppy
lubber
guppy
rusty
9
8
5
10
π sname,rating(S2)
age
35.0
55.5
π age(S2)
31
Selection
™
™
™
™
Selects rows that satisfy
selection condition.
No duplicates in result!
Schema of result
identical to schema of
(only) input relation.
Result relation can be
the input for another
relational algebra
operation! (Operator
composition.)
CEng-553, 2008
sid sname rating age
28 yuppy 9
35.0
58 rusty
10
35.0
σ rating >8(S2)
sname rating
yuppy 9
rusty
10
π sname,rating(σ rating >8(S2))
32
Union, Intersection, Set-Difference
sid sname rating age
™
™
All of these operations take
two input relations, which
must be union-compatible:
ƒ Same number of fields.
ƒ `Corresponding’ fields
have the same type.
What is the schema of result?
sid sname
22 dustin
rating age
7
45.0
S1− S2
CEng-553, 2008
22
31
58
44
28
dustin
lubber
rusty
guppy
yuppy
7
8
10
5
9
45.0
55.5
35.0
35.0
35.0
S1∪ S2
sid sname rating age
31 lubber 8
55.5
58 rusty
10
35.0
S1∩ S2
33
Cross-Product
Each row of S1 is paired with each row of R1.
™ Result schema has one field per field of S1 and R1,
with field names `inherited’ if possible.
ƒ Conflict: Both S1 and R1 have a field called sid.
™
(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
10
35.0
22
101 10/10/96
58
rusty
10
35.0
58
103 11/12/96
ƒ Renaming operator:
CEng-553, 2008
ρ (C(1→ sid1, 5 → sid 2), S1× R1)
34
Joins
™
Condition Join:
(sid)
22
31
R >< c S = σ c ( R × S)
sname rating age
dustin 7
45.0
lubber 8
55.5
S1 ><
(sid) bid
58
103
58
103
S1. sid < R1. sid
day
11/12/96
11/12/96
R1
Result schema same as that of cross-product.
™ Fewer tuples than cross-product, might be
able to compute more efficiently
™ Sometimes called a theta-join.
™
CEng-553, 2008
35
Joins
™
Equi-Join: A special case of condition join where
the condition c contains only equalities.
sid
22
58
sname
dustin
rusty
rating age
7
45.0
10
35.0
S1 ><
™
™
bid
101
103
day
10/10/96
11/12/96
R1
sid
Result schema similar to cross-product, but only
one copy of fields for which equality is specified.
Natural Join: Equijoin on all common fields.
CEng-553, 2008
36
Division
Not supported as a primitive operator, but useful for
expressing queries like:
Find sailors who have reserved all boats.
™ Let A have 2 fields, x and y; B have only field y:
ƒ A/B = { x | ∃ x , y ∈ A ∀ y ∈ B}
™
ƒ
ƒ
™
i.e., A/B contains all x tuples (sailors) such that for every y
tuple (boat) in B, there is an xy tuple in A.
Or: If the set of y values (boats) associated with an x value
(sailor) in A contains all y values in B, the x value is in A/B.
In general, x and y can be any lists of fields; y is the
list of fields in B, and x ∪ y is the list of fields of A.
CEng-553, 2008
37
Examples of Division A/B
sno
s1
s1
s1
s1
s2
s2
s3
s4
s4
pno
p1
p2
p3
p4
p1
p2
p2
p2
p4
A
CEng-553, 2008
pno
p2
B1
pno
p2
p4
B2
pno
p1
p2
p4
B3
sno
s1
s2
s3
s4
sno
s1
s4
sno
s1
A/B1
A/B2
A/B3
38
Expressing A/B Using Basic Operators
™
Division is not essential op; just a useful shorthand.
ƒ
™
(Also true of joins, but joins are so common that systems
implement joins specially.)
Idea: For A/B, compute all x values that are not
`disqualified’ by some y value in B.
ƒ
x value is disqualified if by attaching y value from B, we
obtain an xy tuple that is not in A.
Disqualified x values:
A/B:
CEng-553, 2008
π x ( A) −
π x ((π x ( A) × B) − A)
all disqualified tuples
39
Find names of sailors who’ve reserved boat #103
™
Solution 1:
™
Solution 2:
π sname((σ
bid =103
ρ (Temp1, σ
Reserves) >< Sailors)
bid = 103
Re serves)
ρ ( Temp2, Temp1 >< Sailors)
π sname (Temp2)
™
Solution 3:
CEng-553, 2008
π sname (σ
bid =103
(Re serves >< Sailors))
40
Find names of sailors who’ve reserved a red boat
Information about boat color only available in
Boats; so need an extra join:
Boats) >< Re serves >< Sailors)
π sname ((σ
™
color =' red '
™
A more efficient solution:
π sname (π ((π σ
Boats) >< Re s) >< Sailors)
sid bid color =' red '
A query optimizer can find this, given the first solution!
CEng-553, 2008
41
“Find sailors who’ve reserved a red or a green boat”
™
Can identify all red or green boats, then find
sailors who’ve reserved one of these boats:
ρ (Tempboats, (σ
color =' red ' ∨ color =' green '
Boats))
π sname(Tempboats >< Re serves >< Sailors)
™
Can also define Tempboats using union! (How?)
™
What happens if ∨ is replaced by ∧ in this query?
CEng-553, 2008
42
Find sailors who’ve reserved a red and a green boat
™
Previous approach won’t work! Must identify
sailors who’ve reserved red boats, sailors
who’ve reserved green boats, then find the
intersection (note that sid is a key for Sailors):
ρ (Tempred, π
sid
ρ (Tempgreen, π
((σ
sid
color =' red '
((σ
Boats) >< Re serves))
color =' green'
Boats) >< Re serves))
π sname((Tempred ∩ Tempgreen) >< Sailors)
CEng-553, 2008
43
Find the names of sailors who’ve reserved all boats
™
Uses division; schemas of the input relations
to / must be carefully chosen:
ρ (Tempsids, (π
sid, bid
Re serves) / (π
bid
Boats))
π sname (Tempsids >< Sailors)
™
To find sailors who’ve reserved all ‘Interlake’ boats:
.....
/π
CEng-553, 2008
bid
(σ
bname =' Interlake'
Boats)
44
Summary
The relational model has rigorously defined
query languages that are simple and
powerful.
™ Relational algebra is more operational; useful
as internal representation for query
evaluation plans.
™ Several ways of expressing a given query; a
query optimizer should choose the most
efficient version.
™
CEng-553, 2008
45
Interactive DML - Overview
select-from-where
™ select clause
™ where clause
™ from clause
™ tuple variables
™ string matching
™ ordering of rows
™ set operations
™ built-in functions
™ nested subqueries
™ joins
™ recursive queries
™ insert, delete, update
™
CEng-553, 2008
46
Relational Model - Operations
™ Powerful
set-oriented query languages
™ Relational
Algebra:
procedural;
describes how to compute a query
Operators; Union, Intersect, Select,
Project, Cartesian Product, Difference,
Join, Division, Outer Join, Outer Union,
etc.
™ Relational
Calculus:
declarative;
describes the desired result, e.g. SQL,
QBE
™ Insert, delete, and update capabilities
CEng-553, 2008
47
Interactive DML - select-from-where
SELECT A1, A2, ... An
FROM
R1 , R2 , ... Rm
WHERE P
π
A
1 ,
A
2 , ...
A
n
(σ
P
(R
1
xR
2
x ... R
m
))
the SELECT clause specifies the columns of the
result
™ the FROM clause specifies the tables to be scanned
in the query
™ the WHERE clause specifies the condition on the
columns of the tables in the FROM clause
™ equivalent algebra statement:
™
CEng-553, 2008
48
Basic SQL Query
SELECT
FROM
WHERE
[DISTINCT]
target-list
relation-list
qualification
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!
™
CEng-553, 2008
49
Conceptual Evaluation Strategy
™
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.
CEng-553, 2008
50
Reserves
Example Instances
Sailors
™
We will use
these instances
of the Sailors,
Boats and
Reserves
relations in our
examples.
CEng-553, 2008
Boats
sid
22
31
58
sid bid
day
22 101 10/10/96
58 103 11/12/96
sname rating age
dustin
7
45.0
lubber
8
55.5
rusty
10 35.0
bid bname color
101
Intertake
blue
102
103
Intertake
Clipper
red
green
104
Marine
red
51
Example of Conceptual Evaluation
SELECT
FROM
WHERE
S.sname
Sailors S, Reserves R
S.sid=R.sid AND R.bid=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
10
35.0
22
101 10/10/96
58 rusty
10
35.0
58
103 11/12/96
CEng-553, 2008
52
A Note on Range Variables
™
Really needed only if the same relation
appears twice in the FROM clause. The
previous query can also be written as:
OR
SELECT
FROM
WHERE
S.sname
Sailors S, Reserves R
S.sid=R.sid AND bid=103
SELECT
FROM
WHERE
sname
Sailors, Reserves
Sailors.sid=Reserves.sid
AND bid=103
CEng-553, 2008
It is good style,
however, to use
range variables
always!
53
Expressions and Strings
SELECT S.age, age1=S.age-5, 2*S.age AS
FROM Sailors S
WHERE S.sname LIKE ‘B_%B’
™
™
™
age2
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 any
one character and `%’ stands for 0 or more arbitrary
characters.
CEng-553, 2008
54
Interactive DML - select clause
™
“Find the airlines in FLT-SCHEDULE”
SELECT AIRLINE
FROM FLT-SCHEDULE;
SELECT ALL AIRLINE
FROM FLT-SCHEDULE;
™
“Find the airlines in FLT-SCHEDULE with
duplicates removed”
SELECT DISTINCT AIRLINE
FROM FLT-SCHEDULE;
™
“Find all columns in FLT-SCHEDULE”
SELECT *
FROM FLT-SCHEDULE;
™
“Find FLT# and price raised by 10%”
SELECT FLT#, PRICE*1.1
FROM FLT-SCHEDULE;
CEng-553, 2008
55
Interactive DML - where clause
™
“Find FLT# and price in FLT-SCHEDULE for
flights out of Atlanta”
SELECT FLT#, PRICE
FROM FLT-SCHEDULE
WHERE FROM-AIRPORTCODE=“ATL”;
™
“Find FLT# and price in FLT-SCHEDULE for
flights out of Atlanta with a price over $200”
SELECT FLT#, PRICE
FROM FLT-SCHEDULE
WHERE FROM-AIRPORTCODE=“ATL”
AND PRICE > 200.00;
connectives: AND, OR, NOT, ()
™ comparisons: <, <=, >, >=, =, <>
™
CEng-553, 2008
56
Interactive DML - from clause
“Find FLT#, WEEKDAY, and FROM-AIRPORTCODE in
FLT-WEEKDAY and FLT-SCHEDULE”
™
SELECT FLT-SCHEDULE.FLT#,
WEEKDAY, FROM-AIRPORTCODE
FROM FLT-WEEKDAY, FLT-SCHEDULE
WHERE FLT-WEEKDAY.FLT# = FLT-SCHEDULE.FLT#;
™
dot-notation disambiguates FLT# in FLT-WEEKDAY and
FLT-SCHEDULE
™
this is a natural join:
π
(FLT-SCHEDULE
FLT-WEEKDAY)
FLT-SCHEDULE.FLT#, WEEKDAY, FROM-AIRPORTCODE
CEng-553, 2008
57
Interactive DML - string matching
™
wildcard searches use:
%: matches any substring
_: matches any character
SELECT S.FLT#, WEEKDAY
FROM FLT-WEEKDAY S, FLT-SCHEDULE T
WHERE S.FLT# = T.FLT# AND T.AIRLINE LIKE “%an%”;
™
“%an%” matches American, Airtran, Scandinavian,
Lufthansa, PanAm...
™
“A%” matches American, Airtran, ...
™
“_ _ _%” matches any string with at least three
characters
CEng-553, 2008
58
Interactive DML - ordering of rows
the order by clause orders the rows in a query
result in ascending (asc) or descending (desc)
order
™ “Find FLT#, airline, and price from FLTSCHEDULE for flights out of Atlanta ordered
by ascending airline and descending price:”
™
SELECT FLT#, AIRLINE, PRICE
FROM FLT-SCHEDULE
WHERE FROM-AIRPORTCODE=“ATL”
ORDER BY AIRLINE ASC, PRICE DESC;
CEng-553, 2008
59
Interactive DML - set operations
S
∪
T
T
S
S union T
™
“Find FLT# for flights on Tuesdays in FLTWEEKDAY and FLT# with more than 100 seats in
FLT-INSTANCE ”
SELECT FLT#
FROM FLT-WEEKDAY
WHERE WEEKDAY = “TU”
UNION
SELECT FLT#
FROM FLT-INSTANCE
WHERE #AVAIL-SEATS > 100;
™
UNION ALL
CEng-553, 2008
preserves duplicates
60
Interactive DML - set operation
S
∩
T
S
T
S intersect T
™
“Find FLT# for flights on Tuesdays in FLTWEEKDAY with more than 100 seats in FLTINSTANCE”
SELECT FLT#
FROM FLT-WEEKDAY
WHERE WEEKDAY = “TU”
INTERSECT
SELECT FLT#
FROM FLT-INSTANCE
WHERE #AVAIL-SEATS > 100;
™
INTERSECT ALL
CEng-553, 2008
preserves duplicates
61
Interactive DML - set operation
S-T
S
T
S minus T
™
“Find FLT# for flights on Tuesdays in FLTWEEKDAY except FLT# with more than 100 seats
in FLT-INSTANCE”
SELECT FLT#
FROM FLT-WEEKDAY
WHERE WEEKDAY = “TU”
EXCEPT
SELECT FLT#
FROM FLT-INSTANCE
WHERE #AVAIL-SEATS > 100;
™
EXCEPT ALL
CEng-553, 2008
preserves duplicates
62
Find sid’s 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).
If we replace OR by AND in
the first version, what do
we get?
Also available: EXCEPT
(What do we get if we
replace UNION by EXCEPT?)
CEng-553, 2008
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’)
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’
63
Find sid’s 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.
CEng-553, 2008
SELECT S.sid
FROM Sailors S, Boats B1, Reserves R1,
Boats B2, Reserves R2
WHERE 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’)
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’
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’
64
Nested Queries
“Find names of sailors who’ve reserved boat #103”
SELECT
FROM
WHERE
S.sname
Sailors S
S.sid IN (SELECT R.sid
FROM Reserves R
WHERE R.bid=103)
A very powerful feature of SQL: a WHERE clause can
itself contain an SQL query! (Actually, so can FROM
and HAVING clauses.)
™ To find sailors who’ve not reserved #103, use NOT IN.
™ To understand semantics of nested queries, think of a
nested loops evaluation: For each Sailors tuple, check the
qualification by computing the subquery.
™
CEng-553, 2008
65
Interactive DML - nested subqueries
Set membership: IN, NOT IN
™ “Find airlines from FLT-SCHEDULE where FLT#
is in the set of FLT#’s for flights on Tuesdays
from FLT-WEEKDAY”
™
SELECT DISTINCT AIRLINE
FROM FLT-SCHEDULE
WHERE FLT# IN
(SELECT FLT#
FROM FLT-WEEKDAY
WHERE WEEKDAY = “TU”);
™
“Find FLT#’s for flights on Tuesdays or
Thursdays from FLT-WEEKDAY”
SELECT DISTINCT FLT#
FROM FLT-WEEKDAY
WHERE WEEKDAY IN (“TU”, “TH”);
CEng-553, 2008
66
Nested Queries with Correlation
Find names of sailors who’ve reserved boat #103:
SELECT S.sname
FROM Sailors S
WHERE EXISTS (SELECT
FROM
WHERE
™
*
Reserves R
R.bid=103 AND S.sid=R.sid)
EXISTS is another set comparison operator, like IN.
If UNIQUE is used, and * is replaced by R.bid, finds
sailors with at most one reservation for boat #103.
(UNIQUE checks for duplicate tuples; * denotes all
attributes. Why do we have to replace * by R.bid?)
™ Illustrates why, in general, subquery must be recomputed for each Sailors tuple.
™
CEng-553, 2008
67
Interactive DML - nested subqueries
test empty relation: EXISTS, NOT EXISTS
™ “Find FLT# for flights from Atlanta to Chicago
with a price so low that there does not exist any
cheaper flights from Birmingham to Chicago”
™
SELECT S.FLT#
FROM FLT-SCHEDULE S
WHERE S.FROM-AIRPORTCODE=“ATL”
AND S.TO-AIRPORTCODE=“CHI” AND NOT
EXISTS
(SELECT T.FLT#
FROM FLT-SCHEDULE T
WHERE T.FROM-AIRPORTCODE=“BIR”
AND T.TO-AIRPORTCODE=“CHI”
AND T.PRICE < S.PRICE);
™
notice: reference out of inner scope
CEng-553, 2008
68
Interactive DML - nested subqueries
™
set comparison
=, <, <=, >, >=, <> ALL
=, <, <=, >, >=, <> SOME
™
“Find FLT# for flights from Atlanta to Chicago with a
price that is lower than all flights from Birmingham to
Chicago”
SELECT FLT#
FROM FLT-SCHEDULE
WHERE FROM-AIRPORTCODE=“ATL”
AND TO-AIRPORTCODE=“CHI” AND PRICE <
ALL (SELECT PRICE
FROM FLT-SCHEDULE
WHERE FROM-AIRPORTCODE=“BIR”
AND TO-AIRPORTCODE=“CHI”);
CEng-553, 2008
69
More on Set-Comparison Operators
We’ve already seen IN, EXISTS and UNIQUE. Can
also use NOT IN, NOT EXISTS and NOT UNIQUE.
™ Also available: op ANY, op ALL, op IN >, <, =, ≥,≤, ≠
™ “Find sailors whose rating is greater than that of some
sailor called Horatio”
™
SELECT
FROM
WHERE
CEng-553, 2008
*
Sailors S
S.rating > ANY (SELECT S2.rating
FROM
Sailors S2
WHERE S2.sname=‘Horatio’)
70
(1)
Division in SQL
“Find sailors who’ve reserved all boats.”
™
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
Sailors S such that ...
FROM
WHERE
there is no boat B without ...
AND
R.bid
Reserves R
R.bid=B.bid
R.sid=S.sid))
a Reserves tuple showing S reserved B
CEng-553, 2008
71
Reserves
Example Instances
Sailors
™
We will use
these instances
of the Sailors,
Boats and
Reserves
relations in our
examples.
Boats
“Find sailors who’ve reserved
all boats.”
CEng-553, 2008
sid bid
22
22
22
sid
22
31
58
101
103
102
day
10/10/96
12/10/96
13/10/96
sname rating age
dustin
7
45.0
lubber
8
55.5
rusty
10 35.0
bid bname color
101
Intertake
blue
102
103
Intertake
Clipper
red
green
72
Examples of Division A/Bi
sno
s1
s1
s1
s1
s2
s2
s3
s4
s4
pno
p1
p2
p3
p4
p1
p2
p2
p2
p4
A
CEng-553, 2008
pno
p2
B1
pno
p2
p4
B2
pno
p1
p2
p4
B3
sno
s1
s2
s3
s4
sno
s1
s4
sno
s1
A/B1
A/B2
A/B3
73
Interactive DML - joins
cross join: Cartesian product
™ [inner] join: only keeps rows that satisfy the join
condition
™ left outer join: keeps all rows from left table; fills
in nulls as needed
™ right outer join: keeps all rows from right table;
fills in nulls as needed
™ full outer join: keeps all rows from both tables;
fills in nulls as needed
™ natural or on-condition must be specified for all
inner and outer joins
™ natural: equi-join on columns with same name;
one column preserved
™
CEng-553, 2008
74
Interactive DML - joins
™
“Find all two-leg, one-day trips out of Atlanta; show
also a leg-one even if there is no connecting leg-two
the same day”
SELECT X.FLT# LEG-ONE, Y.FLT# LEG-TWO
FROM ((FLT-SCHEDULE NATURAL JOIN FLT-INSTANCE) X
LEFT OUTER JOIN
(FLT-SCHEDULE NATURAL JOIN FLT-INSTANCE) Y
ON (X.TO-AIRPORTCODE = Y.FROM-AIRPORTCODE
AND X.DATE=Y.DATE AND X.ATIME<Y.DTIME))
WHERE X.FROM-AIRPORTCODE=“ATL”;
CEng-553, 2008
75
Interactive DML- recursive queries
™
not in SQL2; maybe in SQL3...(?)
™
“Find all reachable airports for multi-leg trips out of
Atlanta”
WITH
PAIRS AS SELECT FROM-AIRPORTCODE D, TO-AIRPORTCODE A
FROM FLT-SCHEDULE,
RECURSIVE REACHES(D, A) AS /*initially empty*/
PAIRS
UNION
(SELECT PAIRS.D, REACHES.A
FROM PAIRS, REACHES
WHERE PAIRS.A=REACHES.D)
SELECT A FROM REACHES WHERE D=“ATL”;
CEng-553, 2008
76
Aggregate Operators
™
Significant extension of
relational algebra.
SELECT COUNT (*)
FROM Sailors S
SELECT AVG (S.age)
FROM Sailors S
WHERE S.rating=10
single column
SELECT S.sname
FROM Sailors S
WHERE S.rating= (SELECT MAX(S2.rating)
FROM Sailors S2)
SELECT COUNT (DISTINCT
FROM Sailors S
WHERE S.sname=‘Bob’
CEng-553, 2008
COUNT (*)
COUNT ( [DISTINCT] A)
SUM ( [DISTINCT] A)
AVG ( [DISTINCT] A)
MAX (A)
MIN (A)
S.rating) SELECT AVG ( DISTINCT S.age)
FROM Sailors S
WHERE S.rating=10
77
GROUP BY and HAVING
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 (!):
For i = 1, 2, ... , 10:
CEng-553, 2008
SELECT MIN (S.age)
FROM Sailors S
WHERE S.rating = i
78
Queries With GROUP BY and HAVING
SELECT
[DISTINCT] target-list
FROM
relation-list
WHERE
qualification
GROUP BY grouping-list
HAVING
group-qualification
™
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.)
CEng-553, 2008
79
Conceptual Evaluation
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.
™ In effect, an attribute in group-qualification that is
not an argument of an aggregate op also appears
in grouping-list.
™
CEng-553, 2008
80
Find the age of the youngest sailor with age ≥ 18,
for each rating with at least 2 such sailors
SELECT S.rating, MIN (S.age)
FROM Sailors S
WHERE S.age >= 18
GROUP BY S.rating
HAVING COUNT (*) > 1
™
™
Only S.rating and S.age are
mentioned in the SELECT,
GROUP BY or HAVING clauses;
other attributes `unnecessary’.
2nd column of result is
unnamed. (Use AS to name it.)
CEng-553, 2008
sid sname rating age
22 dustin
7
45.0
31 lubber
8
55.5
71 zorba
10 16.0
64 horatio
7
35.0
29 brutus
1
33.0
58 rusty
10 35.0
rating age
1
33.0
7
45.0
rating
7
35.0
7
35.0
8
55.5
Answer relation
10 35.0
81
“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’
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?
™
CEng-553, 2008
82
Interactive DML - built-in functions
count (COUNT), sum (SUM), average (AVG),
minimum (MIN), maximum (MAX)
™ “Count flights scheduled for Tuesdays from FLTWEEKDAY”
™
SELECT COUNT( *)
FROM FLT-WEEKDAY
WHERE WEEKDAY = “TU”;
™
“Find the average ticket price by airline from FLTSCHEDULE”
SELECT AIRLINE, AVG(PRICE)
FROM FLT-SCHEDULE
GROUP BY AIRLINE;
CEng-553, 2008
83
Interactive DML - built-in functions
™
“Find the average ticket price by airline for scheduled
flights out of Atlanta for airlines with more than 5
scheduled flights out of Atlanta from FLTSCHEDULE”
SELECT AIRLINE, AVG(PRICE)
FROM FLT-SCHEDULE
WHERE FROM-AIRPORTCODE = “ATL”
GROUP BY AIRLINE
HAVING COUNT (FLT#) >= 5;
™
“Find the highest priced flight(s) out of Atlanta from
FLT-SCHEDULE”
SELECT FLT#, MAX(PRICE)
FROM FLT-SCHEDULE
WHERE FROM-AIRPORTCODE = “ATL”;
CEng-553, 2008
84
Interactive DML - insert, delete, update
INSERT INTO FLT-SCHEDULE
VALUES (“DL212”, “DELTA”, 11-15-00, “ATL”,
13-05-00, ”CHI”, 650, 00351.00);
INSERT INTO FLT-SCHEDULE(FLT#,AIRLINE)
VALUES (“DL212”, “DELTA”); /*default nulls added*/
“Insert into FLT-INSTANCE all flights scheduled for
Thursday, 9/10/98”
INSERT INTO FLT-INSTANCE(FLT#, DATE)
(SELECT S.FLT#, 1998-09-10
FROM FLT-SCHEDULE S, FLT-WEEKDAY D
WHERE S.FLT#=D.FLT#
AND D.WEEKDAY=“TH”);
CEng-553, 2008
85
Interactive DML - insert, delete, update
“Cancel all flight instances for Delta on 9/10/98”
DELETE FROM FLT-INSTANCE
WHERE DATE=1998-09-10
AND FLT# IN
(SELECT FLT#
FROM FLT-SCHEDULE
WHERE AIRLINE=“DELTA”);
CEng-553, 2008
86
Interactive DML - insert, delete, update
“Update all reservations for customers on DL212 on
9/10/98 to reservations on AA121 on 9/10/98”
UPDATE RESERVATION
SET FLT#=“AA121”
WHERE DATE=1998-09-10
AND FLT#=“DL212”;
CEng-553, 2008
87
“Find the age of the youngest sailor with age > 18, for
each rating with at least 2 sailors (of any age)”
SELECT S.rating, MIN (S.age)
FROM Sailors S
WHERE S.age > 18
GROUP BY S.rating
HAVING 1 < (SELECT COUNT (*)
FROM Sailors S2
WHERE S.rating=S2.rating)
™
Shows HAVING clause can also contain a subquery.
CEng-553, 2008
88
“Find those ratings for which the average age is the
minimum over all ratings”
™
Aggregate operations cannot be nested! WRONG:
SELECT S.rating
FROM Sailors S
WHERE S.age = (SELECT MIN (AVG
v
(S2.age)) FROM Sailors S2)
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)
CEng-553, 2008
89
Null Values
™
Field values in a tuple are sometimes unknown (e.g.,
a rating has not been assigned) or inapplicable (e.g.,
no spouse’s name).
ƒ
™
SQL provides a special value null for such situations.
The presence of null complicates many issues. E.g.:
ƒ
ƒ
ƒ
ƒ
ƒ
Special operators needed to check if value is/is not null.
Is rating>8 true or false when rating is equal to null? What
about AND, OR and NOT connectives?
We need a 3-valued logic (true, false and unknown).
Meaning of constructs must be defined carefully. (e.g.,
WHERE clause eliminates rows that don’t evaluate to true.)
New operators (in particular, outer joins) possible/needed.
CEng-553, 2008
90
Views- definition, use, update
a view is a virtual table
™ how a view is defined:
™
CREATE VIEW ATL-FLT
AS SELECT FLT#, AIRLINE, PRICE
FROM FLT-SCHEDULE
WHERE FROM-AIRPORTCODE = “ATL”;
™
how a query on a view is written:
SELECT *
FROM ATL-FLT
WHERE PRICE <= 00200.00;
™
how a query on a view is computed:
SELECT FLT#, AIRLINE, PRICE
FROM FLT-SCHEDULE
WHERE FROM-AIRPORTCODE=“ATL” AND PRICE<00200.00;
™
how a view definition is dropped:
DROP VIEW ATL-FLT [RESTRICT|CASCADE];
CEng-553, 2008
91
Views- definition, use, update
views inherit column names of the base tables
they are defined from
™ columns may be explicitly named in the view
definition
™ column names must be named if inheriting
them causes ambiguity
™ views may have computed columns, e.g. from
applying built-in-functions; these must be
named in the view definition
™
CEng-553, 2008
92
Views- definition, use, update
these views are not updatable
CREATE VIEW ATL-PRICES
AS SELECT AIRLINE, PRICE
FROM FLT-SCHEDULE
WHERE FROM-AIRPORTCODE=“ATL”;
CREATE VIEW AVG-ATL-PRICES
AS SELECT AIRLINE, AVG(PRICE)
FROM FLT-SCHEDULE
WHERE FROM-AIRPORTCODE=“ATL”
GROUP BY AIRLINE;
this view is theoretically updatable, but
cannot be updated in SQL
no
tu
niq
ue
com
att pute
rib
ute d
joi
n
tab of t
les wo
CREATE VIEW FLT-SCHED-AND-DAY
AS SELECT S.*, D.WEEKDAY
FROM FLT-SCHEDULE S, FLT-WEEKDAY D
WHERE D.FLT# = S.FLT#;
CEng-553, 2008
93
Views- definition, use, update
a view is updatable if and only if:
™ it does not contain any of the keywords JOIN, UNION,
INTERSECT, EXCEPT
™ it does not contain the keyword DISTINCT
™ every column in the view corresponds to a uniquely
identifiable base table column
™ the FROM clause references exactly one table which must
be a base table or an updatable view
™ the table referenced in the FROM clause cannot be
referenced in the FROM clause of a nested WHERE clause
™ it does not have a GROUP BY clause
™ it does not have a HAVING clause
Updatable means insert, delete, update all ok
CEng-553, 2008
94
Views- definition, use, update
CREATE VIEW LOW-ATL-FARES /*updatable view*/
AS SELECT *
FROM FLT-SCHEDULE
WHERE FROM-AIRPORTCODE=“ATL”
AND PRICE<00200.00;
UPDATE
LOW-ATL-FARES
/*moves row
*/
SET PRICE = 00250.00
/* outside the view*/
WHERE TO-AIRPORTCODE = “BOS”;
INSERT INTO LOW-ATL-FARES /*creates row
*/
VALUES (“DL222”, ”DELTA”,
/*outside the view*/
”BIR”, 11-15-00, ”CHI”, 13-05-00, 00180.00);
CREATE VIEW LOW-ATL-FARES
AS SELECT *
FROM FLT-SCHEDULE
WHERE FROM-AIRPORTCODE=“ATL”
AND PRICE<00200.00
WITH CHECK OPTION;
/*prevents updates*/
/*outside the view*/
CEng-553, 2008
95
Triggers
™
Trigger: procedure that starts automatically if
specified changes occur to the DBMS
Common use is to maintain db consistent and
activated by specific kind of stmt (Insert,
Delete,update)
™
Three parts:
™
ƒ
ƒ
ƒ
Event
Condition
Action
CEng-553, 2008
(activates the trigger)
(tests whether the triggers should run)
(what happens if the trigger runs)
96
Triggers and Stored Procedures
™
Triggers can be defined to enforce constraints on a database, e.g.,
™
DEFINE TRIGGER DELETE-FLIGHT-SCHEDULE
ON DELETE FROM FLIGHT-SCHEDULE WHERE FLIGHT#=‘X’
ACTION DELETE FROM DEPT-AIRPORT WHERE FLIGHT#=‘X’;
FLIGHT-SCHEDULE
DEPT-AIRPORT
FLIGHT#
AIRLINE
101
delta
mo
156
101
atl
545
american
we
110
912
cph
912
scandinavian
fr
450
545
lax
242
usair
mo
231
242
bos
CEng-553, 2008
WEEKDAY PRICE
FLIGHT# AIRPORT-CODE
97
Triggers: Example (SQL2)
CREATE TRIGGER incr_count AFTER INSERT ON Students /* Event */
WHEN (new.age < 18)
/*Condition; ‘new’ is just inserted tuple*/
FOR EACH ROW
BEGIN
/*Action; a procedure in Oracle’sPL/SQL syntax*/
count := count + 1;
END
CREATE TRIGGER init_count BEFORE INSERT ON Students /*Event*/
DECLARE
count INTEGER
BEGIN
/*Action*/
count := 0;
END
CEng-553, 2008
98
Triggers: Example (SQL2)
CREATE TRIGGER youngSailorUpdate
AFTER INSERT ON SAILORS
REFERENCING NEW TABLE NewSailors
FOR EACH STATEMENT
INSERT
INTO YoungSailors(sid, name, age, rating)
SELECT sid, name, age, rating
FROM NewSailors N
WHERE N.age <= 18
CEng-553, 2008
99
Integrity- constraints
constraint: a conditional expression required not
to evaluate to false
™ a constraint cannot be created if it is already
violated
™ a constraint is enforced from the point of creation
forward
™ a constraint has a unique name
™ if a constraint is violated its name is made
available to the user
™ constraints cannot reference parameters or host
variables; they are application independent
™ data type checking is a primitive form of
constraint
™
CEng-553, 2008
100
Integrity Constraints (Review)
™
An IC describes conditions that every legal instance
of a relation must satisfy.
ƒ
ƒ
™
Inserts/deletes/updates that violate IC’s are disallowed.
Can be used to ensure application semantics (e.g., sid is a
key), or prevent inconsistencies (e.g., sname has to be a
string, age must be < 200)
Types of IC’s: Domain constraints, primary key
constraints, foreign key constraints, general
constraints.
ƒ
Domain constraints: Field values must be of right type.
Always enforced.
CEng-553, 2008
101
Integrity- base table, column constraints
™
associated with a specific base table
CREATE TABLE AIRLINE.FLT-SCHEDULE
(FLT#
FLIGHTNUMBER NOT NULL,
AIRLINE
VARCHAR(25),
FROM-AIRPORTCODE AIRPORT-CODE,
DTIME
TIME,
TO-AIRPORTCODE
AIRPORT-CODE,
ATIME
TIME,
CONSTRAINT FLTPK PRIMARY KEY (FLT#),
CONSTRAINT FROM-AIRPORTCODE-FK
FOREIGN KEY (FROM-AIRPORTCODE)
REFERENCES AIRPORT(AIRPORTCODE)
ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY (FROM-AIRPORTCODE)
REFERENCES AIRPORT(AIRPORTCODE)
ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT IC-DTIME-ATIME
CHECK DTIME < ATIME);
CEng-553, 2008
102
Integrity- general constraints
applies to an arbitrary combination of columns
and tables
™ connecting RESERVATIONS for a customer must
make sense:
™
CREATE ASSERTION IC-CONNECTING-FLIGHTS
CHECK (NOT EXISTS
(SELECT *
FROM FLT-SCHEDULE FS1 FS2, RESERVATION R1 R2
WHERE FS1.FLT#=R1.FLT#
AND FS2.FLT#=R2.FLT#
AND R1.DATE=R2.DATE
AND FS1.TO-AIRPORTCODE=FS2.FROM-AIRPORTCODE
AND FS1.ATIME+ INTERVAL “30” MINUTE
> FS2.DTIME));
CEng-553, 2008
103
Integrity- (not so) general constraints
™ not all constraints can be specified
CREATE TABLE AIRLINE.FLT-WEEKDAY
(FLT#
FLIGHTNUMBER
NOT NULL,
WEEKDAY CHAR(2),
.... ));
CREATE TABLE AIRLINE.FLT-INSTANCE
(FLT#
FLIGHTNUMBER NOT NULL,
DATE
DATE
NOT NULL,
.... ));
CREATE ASSERTION DATE-WEEKDAY-CHECK
(NOT EXISTS
(SELECT *
FROM FLT-INSTANCE FI, FLT-WEEKDAY
FSD
→
WHERE FI.FLT#=FSD.FLT#
AND weekday-of(FI.DATE) <> FSD.WEEKDAY));
™
weekday-of: DATE WEEKDAY
CEng-553, 2008
104
CREATE TABLE Sailors
( sid INTEGER,
sname CHAR(10),
rating INTEGER,
age REAL,
PRIMARY KEY (sid),
CHECK ( rating >= 1
AND rating <=
CREATE TABLE Reserves
( sname CHAR(10),
bid INTEGER,
day DATE,
PRIMARY KEY (bid,day),
CONSTRAINT noInterlakeRes
CHECK (`Interlake’ <>
( SELECT B.bname
FROM Boats B
WHERE B.bid=bid)))
General Constraints
™
™
™
Useful when
more general
ICs than keys
are involved.
Can use queries
to express
constraint.
Constraints can
be named.
CEng-553, 2008
10 )
105
Integrity- domain constraints
™
associated with a domain; applies to all columns
defined on the domain
CREATE DOMAIN WEEKDAY CHAR(2)
CONSTRAINT IC-WEEKDAY
CHECK (VALUE IN
( “MO”, “TU”, “WE”, “TH”, “FR”, “SA”, “SU”));
CREATE DOMAIN PRICE DECIMAL(7,2)
CONSTRAINT IC-PRICE
CHECK (VALUE > 00000.00 );
CREATE DOMAIN FLT# CHAR(5)
CONSTRAINT IC-FLT#
CHECK (VALUE NOT NULL);
CEng-553, 2008
106
Authorization
™
Discretionary Access Control (DAC) is supported
by GRANT and REVOKE:
GRANT <privileges>
ON <table>
TO <users>
[WITH GRANT OPTION];
REVOKE [GRANT OPTION FOR] <privileges>
ON <table>
FROM <users> {RESTRICT | CASCADE};
<privileges>: SELECT, INSERT(X), INSERT,
UPDATE(X), UPDATE, DELETE
CASCADE: revoke cascades through its subtree
RESTRICT: revoke succeeds only if there is no subtree
CEng-553, 2008
107
Authorization
GRANT INSERT, DELETE
ON FLT-SCHEDULE
TO U1, U2
WITH GRANT OPTION;
GRANT UPDATE(PRICE)
ON FLT-SCHEDULE
TO U3;
REVOKE GRANT OPTION FOR DELETE
ON FLT-SCHEDULE
FROM U2 CASCADE;
REVOKE DELETE
ON FLT-SCHEDULE
FROM U2 CASCADE;
CEng-553, 2008
108
Catalog and Dictionary Facilities
An INFORMATION_SCHEMA contains the following
tables (or rather views) for the
CURRENT_USER:
™
INFORMATION-_SCHEMA_CATALOG_NAME: single-row, single-column
table with the name of the catalog in which the INFORMATION_SCHEMA
resides
SCHEMATA created by CURRENT_USER
DOMAINS accessible to CURRENT_USER
TABLES accessible to CURRENT_USER
VIEWS accessible to CURRENT_USER
COLUMNS of tables accessible to CURRENT_USER
TABLE_PRIVILEGES granted by or to CURRENT_USER
COLUMN_PRIVILEGES granted by or to CURRENT_USER
USAGE_PRIVILEGES granted by or to CURRENT_USER
DOMAIN_CONSTRAINTS
TABLE_CONSTRAINTS
REFERENTIAL_CONSTRAINTS
CHECK_CONSTRAINTS
™
and 18 others ...
™
™
™
™
™
™
™
™
™
™
™
™
CEng-553, 2008
109
Summary
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 Rel. Alg. 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.
CEng-553, 2008
110
Summary (Contd.)
NULL for unknown field values brings many
complications
™ SQL allows specification of rich integrity
constraints
™ Triggers respond to changes in the database
™
CEng-553, 2008
111