SQLSQL asas DataData ManipulationManipulation LanguageLanguage (DML)(DML)
Insert and update data Simple SQL queries Advanced SQL queries Views SQLSQL // DML:DML: OverviewOverview
Insert, update, delete data
Query data Interactively (Embedded in host language)
Data presentation to users Output improvements
FU-Berlin, DBS I 2006, FU-Berlin, DBS Views Hinze / Scholz
2 SQLSQL // DML:DML: InsertInsert datadata
Complete form: Predefined order of values
INSERT INTO Customer VALUES (001, 'Müller', 'Tina', NULL,NULL);
Incomplete form: Free order of values FU-Berlin, DBS I 2006, FU-Berlin, DBS INSERT INTO Customer (last_name, mem_no) VALUES ('Müller', 001); Hinze / Scholz
3 SQLSQL // DML:DML: InsertInsert datadata
Inserting dates
INSERT INTO movie VALUES (95, 'Psycho', 'suspense', TO_DATE('1969', 'yyyy'), 'Hitchcock', 2.00, NULL);
Conversion functions String to date
FU-Berlin, DBS I 2006, FU-Berlin, DBS TO_DATE(
Date to string
Hinze / Scholz TO_CHAR(
4 SQLSQL // DML:DML: InsertInsert datadata
Loading data from files System dependent Oracle: INSERT INTO … Bulk load from file: SQL loader Bulk load from other database: export / import tool MySQL: INSERT INTO … Bulk load: LOAD DATA … Bulk load from other database: FU-Berlin, DBS I 2006, FU-Berlin, DBS SELECT … INTO OUTFILE, LOAD DATA… Postgres: INSERT INTO …
Hinze / Scholz Bulk load: Copy … Bulk load from other database: Copy … 5 SQLSQL // DML:DML: ImplementationsImplementations ofof bulkbulk loadload
Oracle SQL loader control file data file
SQL*Loader rejected field processing rows discarded rows record selection bad file FU-Berlin, DBS I 2006, FU-Berlin, DBS
(discard file) log file Oracle server Hinze / Scholz
6 SQLSQL // DML:DML: ImplementationsImplementations ofof bulkbulk loadload
loadtest.dat Example: CREATE TABLE loadtest( name varchar(20), 'four''four' ,, 44 num number(10,2)); 'five''five' ,, 55 'six''six' ,, 66 Oracle Syntax: sqlldr
FU-Berlin, DBS I 2006, FU-Berlin, DBS badfilebadfile 'loadtest.bad' 'loadtest.bad' discardfilediscardfile 'loadtest.dis' 'loadtest.dis' APPENDAPPEND INTOINTO tabletable loadtest loadtest fieldsfields terminated terminated by by " " ,, "" Hinze / Scholz optionallyoptionally enclosed enclosed by by " " '' "" (name(name char, char, numnum integer integer external)external) 7 SQLSQL // DML:DML: ImplementationsImplementations ofof bulkbulk loadload
MySQL Example:
Mysql>Mysql> LOADLOAD DATADATA INFILEINFILE 'loadtest.dat''loadtest.dat' ->-> IGNOREIGNORE ->-> INTOINTO TABLETABLE loadtestloadtest ->-> FIELDSFIELDS ->-> TERMINATEDTERMINATED BYBY ",""," ->-> OPTIONALLYOPTIONALLY ENCLOSEDENCLOSED BYBY '''' ->-> (name,(name, numnum ); ); QueryQuery OK,OK, 33 rowsrows affected affected (0.01 (0.01 sec)sec) Records:Records: 33 Deleted:0Deleted:0 Skipped:Skipped: 00 Warnings:Warnings: 00 FU-Berlin, DBS I 2006, FU-Berlin, DBS Hinze / Scholz
8 SQLSQL // DML:DML: InsertInsert uniqueunique datadata
For synthetic keys, e.g. tapeId, …
Counter-variables in application not sufficient Session dependent Concurrent access problematic
Counter relation in database Expensive
FU-Berlin, DBS I 2006, FU-Berlin, DBS Proprietary solutions in existing DBMS MySQL: autoincrement keyword SQL-Server: identity keyword
Hinze / Scholz PostgreSQL: serial type and sequence Oracle: sequence-object 9 SQLSQL // DML:DML: InsertInsert uniqueunique datadata -- sequencesequence
Abstract sequence-object (Oracle) Creates unique integer values
Syntax: CREATE SEQUENCE
Hinze / Scholz Example: create sequence tape_sequence;
10 SQLSQL // DML:DML: InsertInsert uniqueunique datadata -- sequencesequence
Value Access
SELECT
Example:
FU-Berlin, DBS I 2006, FU-Berlin, DBS INSERT INTO tape VALUES(tape_sequence.nextval, 'DVD', 95); Hinze / Scholz
11 SQLSQL // DML:DML: DeleteDelete datadata
Syntax: DELETE from
Delete all rows : DELETE from
Example:
FU-Berlin, DBS I 2006, FU-Berlin, DBS DELETE from tape WHERE format= 'Beta'; Hinze / Scholz
12 SQLSQL // DML:DML: UpdateUpdate datadata
Syntax: UPDATE
Examples: UPDATE Customer SET telephone = 456789 WHERE mem_no = 200;
UPDATE Rental
FU-Berlin, DBS I 2006, FU-Berlin, DBS SET until_date = SYSDATE WHERE tape_ID = 3 AND mem_no = 200 AND TO_CHAR(from_date,'yyyy-mm-dd')='2002-05-01'; Hinze / Scholz
13 SQLSQL // DML:DML: ExampleExample databasedatabase
insert into customer values (001, 'Müller', 'Tina', NULL,NULL); insert into customer values (007, 'Katz', 'Anna', NULL,NULL); insert into customer values (002, 'Maus', 'Carla', NULL,NULL); ....
insert into movie values (95, 'Psycho', 'suspense', to_date('1969', 'yyyy'), 'Hitchcock', 2.00, NULL); insert into movie values (112, 'ET', 'comedy', to_date('1982', 'yyyy'), 'Spielberg', 1.50, NULL); ....
insert into format values('DVD', '2.00'); insert into format values('Beta', '0.00'); insert into format values('VHS', '0.00'); FU-Berlin, DBS I 2006, FU-Berlin, DBS
create sequence tape_sequence; insert into tape values (tape_sequence.nextval, 'DVD', 95);
Hinze / Scholz insert into tape values (tape_sequence.nextval, 'DVD', 112); insert into tape values (tape_sequence.nextval, 'VHS', 222); 14 SQLSQL // DML:DML: ExampleExample databasedatabase
insert into rental values (3, 1, to_date('2002-05-01','yyyy-mm-dd'), NULL); insert into rental values (4, 1, to_date('2002-05-01','yyyy-mm-dd'), NULL); insert into rental values (5, 3, to_date('2002-05-01','yyyy-mm-dd'), to_date('2002-05-02','yyyy-mm-dd'));
insert into actor values ('Hitchcock', 'Hitchcock', to_date(1899-08-13','yyyy-mm-dd')); insert into actor values ('Harrison Ford', 'Harrison Ford', to_date('1942-07-13','yyyy-mm-dd')); FU-Berlin, DBS I 2006, FU-Berlin, DBS
insert into play values(290,'Harrison Ford'); insert into play values(98,'Hitchcock'); Hinze / Scholz
15 SQLSQL // DML:DML: QueryingQuerying LanguageLanguage
SQL is relational complete
Additional query concepts
Advanced search expressions on strings e.g., find all movies starting with “star wars”
Arithmetic in expressions, e.g., number of tapes for each movie FU-Berlin, DBS I 2006, FU-Berlin, DBS Grouping and predicates over sets e.g., total receipts of each movie within the last year Hinze / Scholz
16 SQLSQL // DML:DML: BasicsBasics
Basic query pattern:
SELECT [DISTINCT] A1, A2,...,An FROM R1, R2,...Rm WHERE predicate P;
A1 , A2 , ..., An attribute names,
R1 , R2 , ..., Rm relation names, P Boolean predicate on attributes and constants
Equivalent to relational algebra expression: FU-Berlin, DBS I 2006, FU-Berlin, DBS Π A1, A2, ..., An ( σP ( R1 X R2 X ... X Rm ))
Projection (RA) → SELECT (SQL)
Hinze / Scholz Cartesian Product (RA) → FROM (SQL) Selection (RA) → WHERE (SQL) 17 SQLSQL // DML:DML: BasicsBasics
Query result is relation
Query evaluation order: 1.FROM-clause 2.WHERE-clause 3.SELECT-clause SQL>SQL> SELECTSELECT last_namelast_name 22 FROMFROM Customer;Customer; No duplicate removal LAST_NAME (performance!) LAST_NAME ------FU-Berlin, DBS I 2006, FU-Berlin, DBS MüllerMüller KatzKatz MausMaus HinzHinz Hinze / Scholz KunzKunz MüllerMüller 18 SQLSQL // DML:DML: BasicsBasics
Eliminating duplicates: Targetlist contains KEY attribute Targetlist constrains UNIQUE attribute Targetliste defined with DISTINCT
SELECT mem_no, last_name SELECT DISTINCT last_name FROM Customer FROM Customer MEM_NOMEM_NO LAST_NAMELAST_NAME LAST_NAMELAST_NAME ------11 MüllerMüller HinzHinz 77 KatzKatz KatzKatz FU-Berlin, DBS I 2006, FU-Berlin, DBS 22 MausMaus KunzKunz 1111 HinzHinz MausMaus 2323 KunzKunz MüllerMüller 111111 MüllerMüller Hinze / Scholz
19 SQLSQL // DML:DML: BasicsBasics
WHERE-clause structure:
Simple Boolean predicates similar to RA and Calculus
Additional simple predicates:
FU-Berlin, DBS I 2006, FU-Berlin, DBS Advanced predicated with sub-queries Set-operators (IN, NOT IN, SOME, ALL, EXISTS) Hinze / Scholz
20 SQLSQL // DML:DML: SimpleSimple queriesqueries
Example: All customers named Anna SQL>SQL> selectselect mem_no, mem_no, last_name,last_name, first_namefirst_name 22 fromfrom customer customer 33 wherewhere first_name='Anna'; first_name='Anna';
MEM_NOMEM_NO LAST_NAMELAST_NAME FIRST_NAMEFIRST_NAME ------77 KatzKatz AnnaAnna 2323 KunzKunz AnnaAnna
Example: All movies by Lucas from 1999 or later SQL>SQL> selectselect id,id, titletitle 2 from movie
FU-Berlin, DBS I 2006, FU-Berlin, DBS 2 from movie 33 wherewhere director='Lucas'director='Lucas' 44 andand to_char(year,'yyyy')>='1999';to_char(year,'yyyy')>='1999';
IDID TITLETITLE Hinze / Scholz ------345345 StarStar WarsWars II 21 SQLSQL // DML:DML: SimpleSimple queriesqueries
More examples:
All formats with extra charge between 1 and 2 Euro SELECT * FROM Format WHERE charge BETWEEN 1.00 and 2.00;
All tapes currently on loan FU-Berlin, DBS I 2006, FU-Berlin, DBS SELECT tape_id FROM Rental WHERE until_date IS NULL; Hinze / Scholz
22 SQLSQL // DML:DML: SimpleSimple queriesqueries -- expressionsexpressions Core LIKE - expression SQL:1999 Simple form of regular expression % : any sequence of characters _ : exactly one character
Example: All ‘star wars’ movies
SQL>SQL> selectselect id,id, title,title, directordirector 22 fromfrom moviemovie 33 wherewhere titletitle likelike 'Star'Star WarsWars %';%'; FU-Berlin, DBS I 2006, FU-Berlin, DBS IDID TITLETITLE DIRECTORDIRECTOR ------345345 StarStar WarsWars II LucasLucas
Hinze / Scholz 290290 StarStar WarsWars IVIV LucasLucas
23 SQLSQL // DML:DML: SimpleSimple queriesqueries -- expressionsexpressions enhanced SIMILAR - expression SQL:1999 Advanced form of regular expression
Example:
All ‘star wars’ movies SELECT id, title, director FROM movie WHERE title SIMILAR TO
FU-Berlin, DBS I 2006, FU-Berlin, DBS 'Star Wars (I | IV | V | VI | 1 | [4-6])'; Hinze / Scholz
24 SQLSQL // DML:DML: SimpleSimple queriesqueries
Member in set: IN
All movies from Spielberg or Lukas Core SELECT title, director SQL:1999 FROM Movie WHERE director IN ('Spielberg','Lucas');
All movies from Lucas in 1999 enhanced SELECT title, director SQL:1999 FU-Berlin, DBS I 2006, FU-Berlin, DBS FROM Movie WHERE (director, year) IN (('Lucas',to_date(1999,'yyyy'))); Hinze / Scholz
25 SQLSQL // DML:DML: SimpleSimple queriesqueries -- expressionsexpressions
Functions Expressions may contain functions Arithmetical and string built-in functions User defined functions on user defined types
String function examples: SOUNDEX (
SQL>SQL> SELECTSELECT title,title, directordirector 22 FROMFROM MovieMovie FU-Berlin, DBS I 2006, FU-Berlin, DBS 33 WHEREWHERE SOUNDEX(directorSOUNDEX(director)) == SOUNDEX('Spilbak');SOUNDEX('Spilbak');
TITLETITLE DIRECTORDIRECTOR ------Hinze / Scholz ETET SpielbergSpielberg PsychoPsycho SpielbergSpielberg JawsJaws SpielbergSpielberg 26 SQLSQL // DML:DML: SimpleSimple queriesqueries -- expressionsexpressions
Arithmetic function examples SQRT(
All tapes, their price and tax SQL>SQL> SELECTSELECT id,id, pricepday,pricepday, 22 0.16*pricepday0.16*pricepday as as taxtax 33 FROMFROM Movie;Movie;
IDID PRICEPDAYPRICEPDAY TAXTAX ------FU-Berlin, DBS I 2006, FU-Berlin, DBS 9595 22 .32.32 112112 1.51.5 .24.24 345345 22 .32.32 222 2.2 .352
Hinze / Scholz 222 2.2 .352 290290 22 .32.32 100 1.5 .24 100 1.5 .24 27 SQLSQL // DML:DML: SimpleSimple queriesqueries -- expressionsexpressions
Date function examples differ heavily between systems
Oracle: SYSDATE, MONTHS_BETWEEN, ADD_MONTHS
SQL>SQL> SELECTSELECT title,title, to_char(to_char(year,'yyyy')year,'yyyy') asas yearyear 22 FROMFROM moviemovie 33 WHEREWHERE months_BETWmonths_BETWEEN(SYSDATE,year)>EEN(SYSDATE,year)> 120120 ;;
TITLETITLE YEARYEAR ------FU-Berlin, DBS I 2006, FU-Berlin, DBS PsychoPsycho 19691969 ETET 19821982 JawsJaws 19751975 Hinze / Scholz
28 SQLSQL // DML:DML: SimpleSimple queriesqueries Core Combination of relations SQL:1999 Schema compatible relations UNION, INTERSECT, EXCEPT
Syntax: UNION | INTERSECT | EXCEPT [DISTINCT | ALL] [CORRESPONDING [BY
Default DISTINCT FU-Berlin, DBS I 2006, FU-Berlin, DBS
Default: all attributes used CORRESPONDING BY: defines used common attributes Hinze / Scholz CORRESPONDING: uses all common attributes 29 SQLSQL // DML:DML: SimpleSimple queriesqueries
Example:
All movies from Spielberg or Lukas (SELECT title, director FROM Movie WHERE director like 'Spielberg') UNION (SELECT title, director FROM Movie WHERE director like 'Lucas'); FU-Berlin, DBS I 2006, FU-Berlin, DBS Hinze / Scholz
30 SQLSQL // DML:DML: SimpleSimple queriesqueries
More examples: All movies not by Lucas (SELECT * FROM Movie) EXCEPT (SELECT * from Movie WHERE director='Lucas');
All directors and actors in our database
FU-Berlin, DBS I 2006, FU-Berlin, DBS (SELECT director as celebrity FROM Movie) UNION DISTINCT (SELECT stage_name as celebrity Hinze / Scholz FROM Actor);
31 SQLSQL // DML:DML: ImplementationsImplementations combinationscombinations
Oracle: UNION MINUS implements EXCEPT INTERSECT CORRESPONDING [BY] not implemented
PostgreSQL: UNION EXCEPT INTERSECT FU-Berlin, DBS I 2006, FU-Berlin, DBS CORRESPONDING [BY] not implemented
Hinze / Scholz MySQL: UNION, EXCEPT, INTERSECT not implemented 32 SQLSQL // DML:DML: SimpleSimple queriesqueries withwith joinsjoins Core Simple joins SQL:1999 Search predicates and join conditions mixed
Example: All Tapes and their corresponding movie
SQL>SQL> SELECTSELECT t.id,t.id, t.fot.format,rmat, m.id,m.id, m.titlem.title 22 FROMFROM TapeTape t,t, MovieMovie mm 33 WHEREWHERE m.idm.id == t.movie_id;t.movie_id;
IDID FORMATFORMAT IDID TITLETITLE
------FU-Berlin, DBS I 2006, FU-Berlin, DBS 11 DVDDVD 9595 PsychoPsycho 22 DVDDVD 112112 ETET 33 VHSVHS 222222 PsychoPsycho 44 DVDDVD 345345 StarStar WarsWars II Hinze / Scholz 55 VHSVHS 345345 StarStar WarsWars II 99 VHSVHS 345345 StarStar WarsWars II 33 SQLSQL // DML:DML: SimpleSimple queriesqueries withwith joinsjoins enhanced Cross join (cross product) SQL:1999
Natural inner join
Example: FU-Berlin, DBS I 2006, FU-Berlin, DBS SELECT * FROM Rental NATURAL INNER JOIN Customer; Hinze / Scholz
34 SQLSQL // DML:DML: SimpleSimple queriesqueries withwith joinsjoins enhanced Inner join with attribute list SQL:1999
Subset of attributes in common
Example: SELECT *
FU-Berlin, DBS I 2006, FU-Berlin, DBS FROM Rental r JOIN Customer c USING (mem_no); Hinze / Scholz
35 SQLSQL // DML:DML: SimpleSimple queriesqueries withwith joinsjoins enhanced Inner join with condition SQL:1999
Examples: SELECT * FROM Tape t JOIN Movie m ON t.movie_id = m.id;
FU-Berlin, DBS I 2006, FU-Berlin, DBS SELECT * FROM Rental r JOIN Customer c ON r.mem_no = c.mem_no; Hinze / Scholz
36 SQLSQL // DML:DML: SimpleSimple queriesqueries withwith joinsjoins
All Customers who have rented at least one science fiction film
SELECT c.mem_no, c.last_name, c.first_name FROM ((Customer c JOIN Rental r ON c.mem_no = r.mem_no) JOIN Tape t ON t.id = r.tape_id ) JOIN Movie m ON t.movie_id = m.id WHERE m.category='Scifi';
SELECT c.mem_no, c.last_name, c.first_name
FU-Berlin, DBS I 2006, FU-Berlin, DBS FROM Customer c, Rental r, Tape t, Movie m WHERE c.mem_no=r.mem_no AND t.id = r.tape_id AND t.movie_id = m.id Hinze / Scholz AND m.category='Scifi';
37 SQLSQL // DML:DML: SimpleSimple queriesqueries withwith joinsjoins enhanced Natural outer join SQL:1999
Outer join with condition
FU-Berlin, DBS I 2006, FU-Berlin, DBS Example: SELECT * FROM Rental r RIGHT OUTER JOIN Customer c
Hinze / Scholz ON r.mem_no = c.mem_no;
38 SQLSQL // DML:DML: SimpleSimple queriesqueries withwith joinsjoins
Example (extended)
SQL>SQL> SELECTSELECT r.tape_id,r.tape_id, r.frr.from_date,om_date, c.mem_no,c.first_namec.mem_no,c.first_name 22 FROMFROM RentalRental r r RIGHTRIGHT OUTEROUTER JOINJOIN CustomerCustomer c c 33 ONON r.mem_nor.mem_no = = c.mem_no;c.mem_no;
TAPE_IDTAPE_ID FROM_DATEFROM_DATE MEM_NOMEM_NO FIRST_NAMEFIRST_NAME ------33 01-MAY-0201-MAY-02 11 TinaTina 44 01-MAY-0201-MAY-02 11 TinaTina 55 01-MAY-0201-MAY-02 22 CarlaCarla 2323 AnnaAnna 111111 BertBert FU-Berlin, DBS I 2006, FU-Berlin, DBS 1111 FritzFritz 77 AnnaAnna Hinze / Scholz
39 SQLSQL // DML:DML: ImplementationsImplementations ofof joinsjoins
Oracle/Postgres: Simple join Cross join (natural) inner join with attribute list, with condition (natural) Right, left, full outer join with condition
recommends ANSI-syntax for compatibility
MySQL: Simple join FU-Berlin, DBS I 2006, FU-Berlin, DBS Cross join Straight join (left table always read before right one) Inner join with condition Hinze / Scholz (natural) left, right outer join with condition
40 SQLSQL // DML:DML: ImprovingImproving thethe outputoutput
Not feature of relational algebra Example: Rename column title for this query Order tuples
SQL>SQL> SELECTSELECT m.titlem.title asas Movies,Movies, t.id,t.id, t.formatt.format 22 FROMFROM MovieMovie m,m, TapeTape tt 33 WHEREWHERE m.idm.id == t.movie_idt.movie_id 44 ORDERORDER BYBY title;title;
MOVIESMOVIES IDID FORMATFORMAT ------
FU-Berlin, DBS I 2006, FU-Berlin, DBS ------ETET 22 DVDDVD PsychoPsycho 11 DVDDVD PsychoPsycho 33 VHSVHS StarStar WarsWars II 44 DVDDVD Hinze / Scholz StarStar WarsWars II 55 VHSVHS StarStar WarsWars II 99 VHSVHS 41 SQLSQL // DML:DML: ImprovingImproving thethe outputoutput
Syntax: ORDER BY
Ordering expression No advanced expressions (no sub-query, no grouping) At least one attribute reference References in order expression ⊆ result attributes
Multiple sort attributes: Primary ordering by first attribute
FU-Berlin, DBS I 2006, FU-Berlin, DBS Secondary ordering by second attribute, … SELECT m.title as Movies, t.id, t.format FROM Movie m, Tape t
Hinze / Scholz WHERE m.id = t.movie_id ORDER BY title, format; 42 SQLSQL // DML:DML: ImprovingImproving thethe outputoutput
Advanced features system dependent Oracle SQL+ Example: Format column title for all queries Don’t repeat identical titles SQL>SQL> BREAKBREAK ONON titletitle SQL>SQL> COLUMNCOLUMN titletitle HEADHEADINGING "Movies""Movies" FORMATFORMAT A15A15
SQL>SQL> SELECTSELECT m.title,m.title, t.id,t.id, t.formatt.format 22 FROMFROM MovieMovie m,m, TapeTape tt 33 WHEREWHERE m.idm.id == t.movie_id;t.movie_id;
Movies ID FORMAT
FU-Berlin, DBS I 2006, FU-Berlin, DBS Movies ID FORMAT ------PsychoPsycho 11 DVDDVD ETET 22 DVDDVD PsychoPsycho 3 3 VHSVHS Hinze / Scholz StarStar WarsWars II 44 DVDDVD 55 VHSVHS 99 VHSVHS 43 SQLSQL // DML:DML: SubSub--queriesqueries Core Sub-queries with single results SQL:1999 Operators {=, ≤, ≥, ≠, <, >} Expressible without sub-query
Example: Movies shorter than 'Star Wars I‘ (id 345)
SELECT m.id FROM Movie m
FU-Berlin, DBS I 2006, FU-Berlin, DBS WHERE m.length < (SELECT m1.length FROM Movie m1 WHERE m1.id = 345); Hinze / Scholz
44 SQLSQL // DML:DML: SubSub--queriesqueries Core Set Operator IN SQL:1999
Independent sub-query example:
All Tapes for movie ‘Star Wars ’
SELECT t.id, t.format FROM Tape t WHERE t.movie_id IN (SELECT m.id
FU-Berlin, DBS I 2006, FU-Berlin, DBS FROM Movie m WHERE m.title like 'Star Wars %'); Hinze / Scholz
45 SQLSQL // DML:DML: SubSub--queriesqueries Core Set Operator IN SQL:1999
Correlated sub-query example:
Directors playing in their own movies
SELECT m.director FROM Movie m WHERE m.director IN (SELECT p.actor_name
FU-Berlin, DBS I 2006, FU-Berlin, DBS FROM Play p WHERE p.movie_id = m.id); Hinze / Scholz
46 SQLSQL // DML:DML: SubSub--queriesqueries Core Alternative syntax: EXISTS SQL:1999
Example: SELECT t.id, t.format FROM Tape t WHERE EXISTS (SELECT * FROM Movie m WHERE t.movie_id = m.id AND m.title like 'Star Wars %');
FU-Berlin, DBS I 2006, FU-Berlin, DBS SELECT m.director FROM Movie m WHERE EXISTS (SELECT * FROM Play p Hinze / Scholz WHERE p.movie_id = m.id AND m.director like p.actor_name); 47 SQLSQL // DML:DML: QueryQuery rewritingrewriting
Rewriting possible for IN, EXISTS
Examples:
SELECT t.id, t.format FROM Tape t, Movie m WHERE m.id = t.movie_id AND m.title like 'Star Wars %';
SELECT m.director FU-Berlin, DBS I 2006, FU-Berlin, DBS FROM Movie m, Play p WHERE p.movie_id = m.id AND m.director like p.actor_name; Hinze / Scholz
48 SQLSQL // DML:DML: SubSub--queriesqueries Core Negation NOT EXISTS, NOT IN SQL:1999
All tapes that never have been on loan SELECT t.id FROM Tape t WHERE NOT EXISTS (SELECT * FROM Rental r WHERE r.tape_id = t.id);
All movies no copy of which are currently on loan SELECT distinct m.id
FU-Berlin, DBS I 2006, FU-Berlin, DBS FROM Movie m Where NOT EXISTS (SELECT * FROM Rental r, Tape t WHERE r.tape_id = t.id Hinze / Scholz AND m.id = t.movie_id AND r.until_date IS NULL); 49 SQLSQL // DML:DML: QuantifiedQuantified subsub--queriesqueries enhanced Quantified comparison operators SQL:1999 No re-writing without sub-query Quantification: ALL, SOME (synonym ANY) Operators ∈ {=, ≤, ≥, ≠, <, >}
Quantification All Sub-query true if true for all tuples
Most expensive movies FU-Berlin, DBS I 2006, FU-Berlin, DBS SELECT m.id, m.pricepday FROM Movie m WHERE m.pricepday >= ALL (SELECT m1.pricepday Hinze / Scholz FROM MOVIE m1);
50 SQLSQL // DML:DML: QuantifiedQuantified subsub--queriesqueries enhanced Quantification: SOME SQL:1999 Sub-query true if true for at least one tuple
SELECT title, pricePDay FROM Movie m WHERE pricePday < SOME (SELECT m1.pricepday FROM Movie m1);
= SOME equivalent IN
FU-Berlin, DBS I 2006, FU-Berlin, DBS SELECT m.director FROM Movie m WHERE m.director = SOME (SELECT p.actor_name
Hinze / Scholz FROM Play p WHERE p.movie_id = m.id); 51 SQLSQL // DML:DML: ImplementationsImplementations ofof subsub--queriesqueries
Oracle: Sub-queries with single results Sub-queries with [NOT] IN, [NOT] EXISTS Quantified comparison ALL, SOME, ANY
PostgreSQL Similar to Oracle FU-Berlin, DBS I 2006, FU-Berlin, DBS MySQL: No sub-queries supported Hinze / Scholz
52 SQLSQL // DML:DML: UniversalUniversal quantifiersquantifiers
∀ – Quantification ∃ - Quantification (exactly one) Describe counterexample Combine with NOT EXISTS
Movies with only one tape SELECT m.id FROM Movie m WHERE NOT EXISTS (SELECT * FROM Tape t1, Tape t2 FU-Berlin, DBS I 2006, FU-Berlin, DBS WHERE t1.movie_id = t2.movie_id AND t1.id <> t2.id AND t2.movie_id = m.id ); Hinze / Scholz
53 SQLSQL // DML:DML: UniversalUniversal quantifiersquantifiers
All Customers whose rented movies all have category “suspense“
SELECT c.mem_no FROM Customer c WHERE NOT EXISTS (SELECT m.id FROM Movie m, Rental r, Tape t WHERE m.id = t.movie_id AND r.tape_id = t.id AND c.mem_no = r.mem_no AND m.category <> 'suspense'); FU-Berlin, DBS I 2006, FU-Berlin, DBS Hinze / Scholz
54 SQLSQL // DML:DML: UniversalUniversal quantifiersquantifiers
Customers that had rented all movies
SELECT c.mem_no FROM Customer c WHERE NOT EXISTS (SELECT m.id FROM Movie m WHERE NOT EXISTS (SELECT * FROM Rental r, Tape t WHERE m.id = t.movie_id AND r.tape_id = t.id FU-Berlin, DBS I 2006, FU-Berlin, DBS AND c.mem_no = r.mem_no)); Hinze / Scholz
55 SQLSQL // DML:DML: UniversalUniversal quantifiersquantifiers
Customers that rented only one movie
SELECT c.mem_no FROM Customer c, Rental r, Tape t, Movie m WHERE c.mem_no = r.mem_no AND r.tape_id = t.id AND t.movie_id = m.id AND NOT EXISTS (SELECT m1.id FROM Rental r1, Tape t1, Movie m1 WHERE r1.tape_id = t1.id AND t1.movie_id = m1.id FU-Berlin, DBS I 2006, FU-Berlin, DBS AND c.mem_no = r1.mem_no AND m1.id <> m.id); Hinze / Scholz
56 SQLSQL // DML:DML: AggregateAggregate functionsfunctions
Mathematical aggregate functions on data sets Example: SUM, AVG, MIN, MAX, COUNT Not in relational algebra
SQL>SQL> SELECTSELECT MIN(pricePDay)MIN(pricePDay) asas MIN,MIN, 22 MAX(pricePDay)MAX(pricePDay) asas MAX,MAX, AVG(pricePDay)AVG(pricePDay) 33 FROMFROM Movie;Movie;
MINMIN MAXMAX AVG(PRICEPDAY)AVG(PRICEPDAY) ------1.51.5 2.22.2 1.866666671.86666667 FU-Berlin, DBS I 2006, FU-Berlin, DBS
Target list: only aggregate functions or none
Hinze / Scholz Exception: GROUP BY
57 SQLSQL // DML:DML: AggregateAggregate functionsfunctions
Comparison using aggregates: sub-queries
Movies with price above average
SQL>SQL> SELECTSELECT m.id,m.id, m.Title,m.Title, m.pricepdaym.pricepday 22 FROMFROM MovieMovie mm 33 WHEREWHERE pricePDaypricePDay > > 44 (SELECT(SELECT AVG(pricePDay)AVG(pricePDay) 55 FROMFROM Movie);Movie);
IDID TITLETITLE PRICEPDAYPRICEPDAY ------FU-Berlin, DBS I 2006, FU-Berlin, DBS 9595 PsychoPsycho 22 345345 StarStar WarsWars II 22 222222 PsychoPsycho 2.22.2 290290 StarStar WarsWars IVIV 22 Hinze / Scholz
58 SQLSQL // DML:DML: AggregateAggregate functionsfunctions
Examples:
Movies with minimal price SELECT m.Title, m.pricepday FROM Movie m WHERE pricePDay = (SELECT MIN(pricePDay) FROM Movie);
Movie with more than 2 tapes
FU-Berlin, DBS I 2006, FU-Berlin, DBS SELECT m.id, m.title FROM Movie m WHERE 2 < (SELECT count(t.id) FROM tape t Hinze / Scholz WHERE t.movie_id = m.id)
59 SQLSQL // DML:DML: AggregateAggregate functionsfunctions
More examples:
Movies having tapes in one format only SELECT m.id, m.title FROM Movie m, Tape t1 WHERE m.id = t1.movie_id AND 0 = (SELECT COUNT(*) FROM Tape t2 WHERE t1.id <> t2.id AND t1.format <> t2.format FU-Berlin, DBS I 2006, FU-Berlin, DBS AND t2.movie_id = m.id); Hinze / Scholz
60 SQLSQL // DML:DML: AggregateAggregate functionsfunctions
Additional qualification with DISTINCT | ALL
Example:
Movies that are available in all formats SELECT DISTINCT t1.movie_id FROM Tape t1 WHERE (SELECT COUNT(DISTINCT format) FROM Tape t2 WHERE t2.movie_id = t1.movie_id) FU-Berlin, DBS I 2006, FU-Berlin, DBS = (SELECT COUNT(*) FROM Format); Hinze / Scholz
61 SQLSQL // DML:DML: GroupingGrouping
Syntax: SELECT
Groups all rows with same values in
Example:
FU-Berlin, DBS I 2006, FU-Berlin, DBS Number of tapes in each format SELECT t.format, count(t.id) FROM Tape t
Hinze / Scholz GROUP BY t.format;
62 SQLSQL // DML:DML: GroupingGrouping
Aggregates evaluated over groups
Number of tapes for each movie
SQL>SQL> SELECTSELECT t.movie_id,t.movie_id, count(*)count(*) 22 FROMFROM TapeTape tt 33 GROUPGROUP BYBY t.movie_id;t.movie_id;
MOVIE_IDMOVIE_ID COUNT(*)COUNT(*) ------9595 11 100100 11 FU-Berlin, DBS I 2006, FU-Berlin, DBS 112112 11 222222 11 290290 11 345 4
Hinze / Scholz 345 4
63 SQLSQL // DML:DML: GroupingGrouping
Movie id title cat. year director price leng. 095 Psycho ...... Hitchcock 2.00 ... 112 ET ...... Spielberg 1.50 ... 345 Star Wars I ...... Lucas 2.00 ... 222 Psycho ...... Van Sant 2.20 ... 290 Star Wars IV ...... Lucas 2.00 ... 100 Jaws ...... Spielberg 1.50 ......
FU-Berlin, DBS I 2006, FU-Berlin, DBS SELECT sum(price) 11.2 FROM movie;
Hinze / Scholz Implicit group: all tuples in table
64 SQLSQL // DML:DML: GroupingGrouping
Movie id title cat. year director price leng. 095 Psycho ...... Hitchcock 2.00 ... 112 ET ...... Spielberg 1.50 ... 345 Star Wars I ...... Lucas 2.00 ... 222 Psycho ...... Van Sant 2.20 ... 290 Star Wars IV ...... Lucas 2.00 ... 100 Jaws ...... Spielberg 1.50 ......
Hitchcock 2.0 FU-Berlin, DBS I 2006, FU-Berlin, DBS SELECT director, sum(price) Spielberg 3.0 FROM movie Lucas 4.0 Group by director; Van Sant 2.2 Hinze / Scholz
65 SQLSQL // DML:DML: GroupingGrouping
Total receipts of each tape within the last year
SQL>SQL> SELECTSELECT t.id,t.id, count(*)count(*) 22 FROMFROM TapeTape t,t, RentalRental rr 33 WHEREWHERE t.idt.id = = r.tape_idr.tape_id 44 ANDAND to_char(r.from_date,'yyyy')to_char(r.from_date,'yyyy') >=>= 20052005 55 GROUPGROUP BYBY t.id;t.id;
IDID COUNT(*)COUNT(*) ------11 11 22 11 FU-Berlin, DBS I 2006, FU-Berlin, DBS 33 22 44 22 55 11 1111 11 Hinze / Scholz 1212 11
66 SQLSQL // DML:DML: GroupingGrouping
Total receipts of each movie within the last year
SQL>SQL> SELECTSELECT t.movie_id,t.movie_id, count(*)count(*) 22 FROMFROM TapeTape t,t, RentalRental rr 33 WHEREWHERE t.idt.id = = r.tape_idr.tape_id 44 ANDAND to_char(r.from_date,'yyyy')to_char(r.from_date,'yyyy') >=>= 20052005 55 GROUPGROUP BYBY t.movie_id;t.movie_id;
MOVIE_IDMOVIE_ID COUNT(*)COUNT(*) ------9595 11 100100 11 FU-Berlin, DBS I 2006, FU-Berlin, DBS 112112 11 222222 22 290290 11 345345 33 Hinze / Scholz
67 SQLSQL // DML:DML: GroupingGrouping ++ HavingHaving
Qualifying predicate for groups
SQL>SQL> SELECTSELECT f.name,f.name, sum(charge)sum(charge) 22 FROMFROM RentalRental r, r, TapeTape t,t, formatformat f f 33 WHEREWHERE t.idt.id = = r.tape_idr.tape_id 44 ANDAND t.format=f.namet.format=f.name 55 GROUPGROUP BYBY f.name;f.name; SQL>SQL> SELECTSELECT f.name,f.name, sum(charge)sum(charge) NAMENAME SUM(CHARGE)SUM(CHARGE) 22 FROMFROM RentalRental r, r, TapeTape t,t, formatformat f f ------33 WHEREWHERE t.idt.id = = r.tape_idr.tape_id BetaBeta 00 44 ANDAND t.format=f.namet.format=f.name DVDDVD 88 55 GROUPGROUP BYBY f.namef.name FU-Berlin, DBS I 2006, FU-Berlin, DBS VHSVHS 00 66 havinghaving count(f.name)>2; count(f.name)>2;
NAMENAME SUM(CHARGE)SUM(CHARGE) ------Hinze / Scholz ------DVDDVD 88 VHSVHS 00 68 SQLSQL // DML:DML: GroupingGrouping ++ HavingHaving
Movie id title cat. year director price leng. 095 Psycho ...... Hitchcock 2.00 ... 112 ET ...... Spielberg 1.50 ... 345 Star Wars I ...... Lucas 2.00 ... 222 Psycho ...... Van Sant 2.20 ... 290 Star Wars IV ...... Lucas 2.00 ... 100 Jaws ...... Spielberg 1.50 ......
Hitchcock 2.0 FU-Berlin, DBS I 2006, FU-Berlin, DBS SELECT director, sum(price) Spielberg 3.0 FROM movie Lucas 4.0 Group by director Van Sant 2.2 HAVING sum(price)>2.00; Hinze / Scholz
69 SQLSQL // DML:DML: GroupingGrouping ++ HavingHaving
Movie id title cat. year director price leng. 095 Psycho ...... Hitchcock 2.00 ... 112 ET ...... Spielberg 1.50 ... 345 Star Wars I ...... Lucas 2.00 ... 222 Psycho ...... Van Sant 2.20 ... 290 Star Wars IV ...... Lucas 2.00 ... 100 Jaws ...... Spielberg 1.50 ...... max Hitchcock 2.0 2.0 FU-Berlin, DBS I 2006, FU-Berlin, DBS SELECT director, sum(price) Spielberg 3.0 1.5 FROM movie Lucas 4.0 2.0 Group by director Van Sant 2.2 2.2 HAVING max(price)>2.00; Hinze / Scholz
70 SQLSQL // DML:DML: GroupingGrouping ++ HavingHaving
Movie id title cat. year director price leng. 095 Psycho ...... Hitchcock 2.00 ... 112 ET ...... Spielberg 1.50 ... 345 Star Wars I ...... Lucas 2.00 ... 222 Psycho ...... Van Sant 2.20 ... 290 Star Wars IV ...... Lucas 2.00 ... 100 Jaws ...... Spielberg 1.50 ...... max Hitchcock, Psycho 2.0 2.0 FU-Berlin, DBS I 2006, FU-Berlin, DBS SELECT director,title, sum(price) Spielberg, ET 1.5 1.5 FROM movie Lucas, SW I 2.0 2.0 Group by director, title Van Sant, Psycho 2.2 2.2 HAVING max(price)>2.00; Hinze / Scholz Lucas, SW IV 2.0 2.0 Spielberg, Jaws 1.5 1.5 71 SQLSQL // DML:DML: GroupingGrouping ++ HavingHaving
HAVING without GROUP BY Implicit single group contains all tuples
SQL>SQL> SELECTSELECT sum(charge)sum(charge) 22 FROMFROM RentalRental r, r, TapeTape t,t, formatformat f f 33 WHEREWHERE t.idt.id = = r.tape_idr.tape_id 44 ANDAND t.format=f.namet.format=f.name 55 havinghaving count(f.name)>2; count(f.name)>2;
SUM(CHARGE)SUM(CHARGE) ------
FU-Berlin, DBS I 2006, FU-Berlin, DBS 88 Hinze / Scholz
72 SQLSQL // DML:DML: GroupingGrouping ++ HavingHaving
Query evaluation order: 1.FROM-clause 2.WHERE-clause 3.GROUP BY–clause 4.HAVING-clause 5.SELECT-clause
Number of rentals for all customers named Anna or Tina, which rented some tapes more than once SELECT c.mem_no, count(*)
FU-Berlin, DBS I 2006, FU-Berlin, DBS FROM Rental r, Customer c WHERE r.mem_no= c.mem_no AND c.first_name ='Anna' OR c.first_name='Tina' Hinze / Scholz GROUP BY c.mem_no HAVING count(DISTINCT r.tape_id) Nested aggregation using groups Most loaned movie SELECT t.movie_id, count(t.movie_id) FROM Rental r, Tape t WHERE r.tape_id = t.id GROUP BY t.movie_id HAVING COUNT(t.movie_id) > = ALL (SELECT count(t1.movie_id) FROM Rental r1, Tape t1 FU-Berlin, DBS I 2006, FU-Berlin, DBS WHERE r1.tape_id = t1.id Group BY t1.movie_id); Hinze / Scholz 74 SQLSQL // DML:DML: NestedNested aggregationaggregation withwith groupsgroups Movie with maximal number of tapes, show number of tapes SELECT m.id, m.title, t1.t_no FROM (SELECT t.movie_id, count(*) as t_no FROM tape t GROUP BY t.movie_id) t1, movie m WHERE m.id=t1.movie_id AND t1.t_no = (SELECT max(count(*)) FROM tape t GROUP by t.movie_id); FU-Berlin, DBS I 2006, FU-Berlin, DBS Hinze / Scholz 75 SQLSQL // DML:DML: OutputOutput improvementimprovement Core Select values depending an condition SQL:1999 Complete CASE form: CASE WHEN Example: FU-Berlin, DBS I 2006, FU-Berlin, DBS SELECT length, CASE WHEN length is NULL then 'not defined' WHEN length < 90 THEN 'short' ELSE 'long' Hinze / Scholz END FROM Movie; 76 SQLSQL // DML:DML: OutputOutput improvementimprovement Core Simple CASE form SQL:1999 CASE Example: select f.name, case f.name FU-Berlin, DBS I 2006, FU-Berlin, DBS when 'DVD' then 'DISC' when 'Beta' then 'TAPE' when 'VHS' then 'TAPE' else NULL Hinze / Scholz end from Format f; 77 SQLSQL // DML:DML: TransitiveTransitive closureclosure enhanced Recursive queries SQL:1999 Name recursion expression Use name in associated query expression SYNTAX: WITH RECURSIVE 78 SQLSQL // DML:DML: TransitiveTransitive closureclosure enhanced Example: All lectures required for lecture XYZ SQL:1999 create table lecture( lnr integer primary key, name varchar(20)); create table requires( pre integer references lecture(lnr), suc integer references lecture(lnr), constraint req_pk primary key(pre, suc)); WITH RECURSIVE preLecture(pre, suc) FU-Berlin, DBS I 2006, FU-Berlin, DBS AS (SELECT pre,suc FROM requires) SELECT l.lnr as prerequisite FROM preLecture p1, preLecture p2, lecture l WHERE p2.suc = l.lnr Hinze / Scholz AND l.name = ‘databases’ AND p1.suc = p2.pre; 79 SQLSQL // DML:DML: TransitiveTransitive closureclosure Different implementation in oracle: Lecture:LNR NAME Requires: PRE SUC ------1 databases 2 3 2 algorithms I 3 1 3 algorithms II SQL>SQL> SELECTSELECT r.prer.pre 22 FROMFROM requiresrequires r,r, lecturelecture ll 33 WHEREWHERE l.name=l.name= 'databases''databases' 44 STARTSTART WITHWITH r.sucr.suc = = l.lnrl.lnr FU-Berlin, DBS I 2006, FU-Berlin, DBS 55 CONNECTCONNECT BYBY PRIORPRIOR prepre == suc;suc; PREPRE ------ Hinze / Scholz ------33 2 2 80 SQLSQL // DML:DML: StructuringStructuring enhanced Difficult structuring of complex queries SQL:1999 No naming of commands / relations for re-use Temporary table CREATE TABLE Stores temporal query result FU-Berlin, DBS I 2006, FU-Berlin, DBS LOCAL: Only visible to owner Dropped at end of session Hinze / Scholz 81 SQLSQL // DML:DML: TemporaryTemporary tabletable Example: Test1: CREATE TABLE testsource(id integer); CREATE GLOBAL TEMPORARY TABLE test1(id integer) ON COMMIT PRESERVE ROWS; No rows INSERT INTO testsource values(1); No rows INSERT INTO test1 SELECT * FROM testsource; 1 FU-Berlin, DBS I 2006, FU-Berlin, DBS INSERT INTO testsource values(2); 1 COMMIT; Hinze / Scholz 1 82 SQLSQL // DML:DML: TemporaryTemporary tabletable Example: Test2: CREATE TABLE testsource(id integer); CREATE GLOBAL TEMPORARY TABLE test2(id integer) ON COMMIT DELETE ROWS; No rows INSERT INTO testsource values(1); No rows INSERT INTO test2 SELECT * FROM testsource; 1 FU-Berlin, DBS I 2006, FU-Berlin, DBS INSERT INTO testsource values(2); 1 COMMIT; Hinze / Scholz No rows 83 SQLSQL // DML:DML: ViewView SQL-object (virtual relation) Important concept Important for Tailoring database schema for different applications Access protection, Privacy Structuring complex queries Relational concept for external schema Syntax: CREATE VIEW FU-Berlin, DBS I 2006, FU-Berlin, DBS Example: CREATE VIEW rental_overview AS SELECT r.mem_no, c.last_name, r.tape_id Hinze / Scholz FROM rental r, customer c WHERE r.mem_no = c.mem_no; 84 SQLSQL // DML:DML: ViewView updatesupdates Core Updateable views: SQL:1999 1. No SELECT DISTINCT 2. No duplicate attribute in target list 3. Only one table reference 4. No GROUP BY 5. No aggregates 6. No set operators (INTERSECT, EXCEPT, UNION) Formal: FU-Berlin, DBS I 2006, FU-Berlin, DBS u (V(B)) = V (cu (B) ) V(B) view on base tables B u(V(B)) update on view Hinze / Scholz cu : equivalent update(s) on base relations must exist 85 SQLSQL // DML:DML: ViewView updatesupdates Core Examples: SQL:1999 Not updateable ( distinct, group by ): CREATE VIEW movieformats (movie, numFormats) AS SELECT movie_id, count(distinct format) FROM Tape t GROUP BY movie_id; Updateable: CREATE VIEW movies (movie, name) AS SELECT id, title FU-Berlin, DBS I 2006, FU-Berlin, DBS FROM Movie WHERE id > 100; u: INSERT INTO movies VALUES (47,‘The Fugitive‘); Hinze / Scholz INSERT INTO movie (id, title) cu: VALUES (47,‘The Fugitive‘); 86 SQLSQL // DML:DML: ViewView updatesupdates Additional conditions: If u does not have an effect, then cu should not No side effects: c should only effect tuples in B which are represented in V Inverse update: For a view update u there should be an inverse update w such that w (u ( V( B )) ) = V (B) FU-Berlin, DBS I 2006, FU-Berlin, DBS No constraint on base tables must be violated by u Hinze / Scholz 87 SQLSQL // DML:DML: ViewView updatesupdates Views with check option CHECK OPTION prevent side effects on base tables Syntax: CREATE VIEW Theoretically more views updateable e.g. UNIQUE columns in joins FU-Berlin, DBS I 2006, FU-Berlin, DBS Enhanced SQL: 1999 additional complex conditions for view update Hinze / Scholz 88 SQLSQL // DML:DML: RemarksRemarks aboutabout NULLNULL NULL treated as "unknown" Predicates: NULL AND TRUE = NULL NULL OR TRUE = TRUE predicate evaluates to NULL for row r → r not returned Arithmetical expression: If NULL involved → expression evaluates to NULL FU-Berlin, DBS I 2006, FU-Berlin, DBS Aggregates: Count(*) counts also NULL avg(*) ≠ sum(*)/count(*) Hinze / Scholz 89 SQLSQL // DML:DML: SummarySummary SQL as Data manipulation language Declarative language Relational complete query language Important terms and concepts: Insert, update, delete data Basic query pattern DISTINCT, ALL Set combination (UNION, INTERSECT, EXCEPT) Joins FU-Berlin, DBS I 2006, FU-Berlin, DBS Sub-queries (IN, EXISTSSOME, ALL) Aggregate functions GROUP BY, HAVING Hinze / Scholz View, view updates 90 [ON COMMIT {PRESERVE ROWS | DELETE ROWS}]