SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available...
-
date post
19-Dec-2015 -
Category
Documents
-
view
215 -
download
0
Transcript of SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available...
Administrivia
• Homework 2 assignment now available– Due a week from Sunday
• Midterm exam will be evening of 10/14
Review – Query Optimization
• External Sorting with Merge sort– It is possible to sort most tables in 2 passes
• Join Algorithms– Nested Loops– Indexed Nested Loops– Sort-Merge Join– Hash Join
• Relational Algebra Equivalences
Review – Query Optimization (cont.)
• DBMSs keep statistics in system catalogs– Example from Postgres
• Access Paths: scans and indexes
• Query Plans a la System R– Consider all left-deep query trees– Consider different access plans for selections– Consider different algorithms for joins
Query Optimization – Some Resources
• Animation of Join Algorithmshttp://www.animal.ahrgr.de/en/AnimList_algo.html
• System CatalogsPostGres catalogs: http://www.postgres.org/docs/7.2/interactive/pg-system-catalogs.html
• Query Optimization“explain” command, type “explain <sql query>”
Review - SQL
• Data Definiton Language (DDL)– “create schema”, constraints, etc.
• Data Manipulation Language (DML)– Range variables in Select clause– Expressions in Select, Where clauses– Set operators between queries:
• Union, Intersect, Except/Minus
– Set operators in nested queries:• In, Exists, Unique, <op> Any, <op> All
– Aggregates: Count, Sum, Avg, Min, Max– Group By– Group By/Having
Today – Further SQL Features
• Insert• Delete• Update• Null Values – Outer Joins• Views• Order By• Access Control• Integrity Constraints
INSERT
INSERT INTO Boats VALUES ( 105, ‘Clipper’, ‘purple’)INSERT INTO Boats (bid, color) VALUES (99, ‘yellow’)
“bulk insert” from one table to another (must be type compatible):INSERT INTO TEMP(bid)SELECT r.bid FROM Reserves R WHERE r.sid = 22;
“bulk insert” from files (in Postgres)Copy
INSERT [INTO] table_name [(column_list)]VALUES ( value_list)
INSERT [INTO] table_name [(column_list)]<select statement>
DELETE & UPDATE
DELETE FROM Boats WHERE color = ‘red’
DELETE FROM Boats b
WHERE b. bid =
(SELECT r.bid FROM Reserves R WHERE r.sid = 22)
Can also modify tuples using UPDATE statement.
UPDATE Boats
SET Color = “green”
WHERE bid = 103;
DELETE [FROM] table_name[WHERE qualification]
Null Values
• Values 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. – “rating>8” - true or false when rating is null? What about
AND, OR and NOT connectives?– 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.
Null Values – 3 Valued Logic
AND T F Null
T
F
NULL
(null > 0)
(null + 1)
(null = 0)
null AND true
is null
is null
is null
is null
OR T F Null
T
F
NULL
T
F Null
T F
F F
Null
NullF
F
T T
T
T NullNullNull
What about joins where one value is Null?
• Join is comparing (attr1 = attr2)
• What to do if attr1 is null?
• By default, don’t join the tuples (INNER join).
• Sometimes, though, do want to join tuples.
Joins
Explicit join semantics needed unless it is an INNER join
(INNER is default)
SELECT (column_list)FROM table_name [INNER | {LEFT |RIGHT | FULL } OUTER] JOIN table_name ON qualification_listWHERE …
Inner Join
Only rows that match search conditions are returned.
SELECT s.sid, s.name, r.bidFROM Sailors s INNER JOIN Reserves rON s.sid = r.sid
Returns only those sailors who have reserved boats
SQL-92 also allows: SELECT s.sid, s.name, r.bidFROM Sailors s NATURAL JOIN Reserves r
“NATURAL” means equi-join for each pair of attributes with the same name
SELECT s.sid, s.name, r.bidFROM Sailors s INNER JOIN Reserves rON s.sid = r.sid
s.sid s.name r.bid22 Dustin 10195 Bob 103
sid sname rating age
22 Dustin 7 45.0
31 Lubber 8 55.595 Bob 3 63.5
sid bid day
22 101 10/10/9695 103 11/12/96
Left Outer Join
Left Outer Join returns all matched rows, plus all unmatched rows from the table on the left of the join clause
(use nulls in fields of non-matching tuples)
SELECT s.sid, s.name, r.bidFROM Sailors s LEFT OUTER JOIN Reserves rON s.sid = r.sid
Returns all sailors & information on whether they have reserved boats
SELECT s.sid, s.name, r.bidFROM Sailors s LEFT OUTER JOIN Reserves rON s.sid = r.sid
s.sid s.name r.bid22 Dustin 10195 Bob 10331 Lubber
sid sname rating age
22 Dustin 7 45.0
31 Lubber 8 55.595 Bob 3 63.5
sid bid day
22 101 10/10/9695 103 11/12/96
Right Outer Join
Right Outer Join returns all matched rows, plus all unmatched rows from the table on the right of the join clause
SELECT r.sid, b.bid, b.nameFROM Reserves r RIGHT OUTER JOIN Boats
bON r.bid = b.bid
Returns all boats & information on which ones are reserved.
SELECT r.sid, b.bid, b.nameFROM Reserves r RIGHT OUTER JOIN Boats bON r.bid = b.bid
r.sid b.bid b.name22 101 Interlake
102 Interlake95 103 Clipper
104 Marine
sid bid day
22 101 10/10/9695 103 11/12/96
bid bname color101 Interlake blue102 Interlake red103 Clipper green104 Marine red
Full Outer Join
Full Outer Join returns all (matched or unmatched) rows from the tables on both sides of the join clause
SELECT r.sid, b.bid, b.nameFROM Reserves r FULL OUTER JOIN Boats bON r.bid = b.bid
Returns all boats & all information on reservations
SELECT r.sid, b.bid, b.nameFROM Reserves r FULL OUTER JOIN Boats bON r.bid = b.bid
r.sid b.bid b.name22 101 Interlake
102 Interlake95 103 Clipper
104 MarineNote: in this case it is the same as the ROJ becausebid is a foreign key in reserves, so all reservations musthave a corresponding tuple in boats.
sid bid day
22 101 10/10/9695 103 11/12/96
bid bname color101 Interlake blue102 Interlake red103 Clipper green104 Marine red
Views
CREATE VIEW view_nameAS select_statement
Makes development simplerOften used for securityOften not instantiated - otherwise updates tricky
CREATE VIEW RedsAS SELECT B.bid, COUNT (*) AS scount FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ GROUP BY B.bid
CREATE VIEW RedsAS SELECT B.bid, COUNT (*) AS scount FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ GROUP BY B.bid
b.bid scount102 1 Reds
SELECT bname, scount FROM Reds R, Boats B WHERE R.bid=B.bid
AND scount < 10
b.bid scount102 1 Reds
CREATE VIEW RedsAS SELECT B.bid, COUNT (*) AS scount FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ GROUP BY B.bid
Views Instead of Relations in Queries
Sorting the Results of a Query
• ORDER BY column [ ASC | DESC] [, ...]
• Can order by any column in SELECT list, including expressions or aggs:
SELECT S.rating, S.sname, S.ageFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid
AND R.bid=B.bid AND B.color=‘red’ORDER BY S.rating, S.sname;
SELECT S.sid, COUNT (*) AS redrescntFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid
AND R.bid=B.bid AND B.color=‘red’GROUP BY S.sidORDER BY redrescnt DESC;
Discretionary Access Control
GRANT privileges ON object TO users [WITH GRANT OPTION]
• Object can be a Table or a View• Privileges can be:
• Select• Insert• Delete• References (cols) – allow to create a
foreign key that references the specified column(s)
• All• Can later be REVOKEd• Users can be single users or groups• See Chapter 17 for more details.
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.– Primary key and foreign key constraints: you know
them.
General Constraints
• Useful when more general ICs than keys are involved.
• Can use queries to express constraint.
• Checked on insert or update.
• Constraints can be named.
CREATE TABLE Sailors( sid INTEGER,sname CHAR(10),rating INTEGER,age REAL,PRIMARY KEY (sid),CHECK ( rating >= 1
AND rating <= 10 )) CREATE TABLE Reserves
( sname CHAR(10),bid INTEGER,day DATE,PRIMARY KEY (bid,day),CONSTRAINT noInterlakeResCHECK (`Interlake’ <>
( SELECT B.bnameFROM Boats BWHERE B.bid=bid)))
Constraints Over Multiple Relations
CREATE TABLE Sailors( sid INTEGER,sname CHAR(10),rating INTEGER,age REAL,PRIMARY KEY (sid),CHECK ( (SELECT COUNT (S.sid) FROM Sailors S)+ (SELECT COUNT (B.bid) FROM
Boats B) < 100 )
• Awkward and wrong!• Only checks sailors!• Only required to hold
if the associated table is non-empty.
• ASSERTION is the right solution; not associated with either table.
• Unfortunately, not supported in many DBMS.
• Triggers are another solution.
CREATE ASSERTION smallClubCHECK ( (SELECT COUNT (S.sid) FROM Sailors S)+ (SELECT COUNT (B.bid) FROM Boats B) < 100 )
Number of boatsplus number of sailors is < 100