create*and*drop*table4 basic*sfw*query4tddd12/timetable/fo2-sql-6in1.pdf · dbms4 •...

7
IDA / ADIT Databasteknik Databaser och bioinformatik SQL Fang Wei8Kleiner IDA / ADIT SQL SQL: Structured Query Language o Pronounced “S8Q8L” or “sequel” o The standard query language supported by most commercial DBMS A brief history o IBM System R o ANSI SQL89 o ANSI SQL92 (SQL2) o ANSI SQL99 (SQL3) o ANSI SQL 2003 (added OLAP, XML, etc.) o ANSI SQL 2006 (added more XML) o ANSI SQL 2008, … 2 IDA / ADIT Create and drop table CREATE TABLE table_name (…, column_name i column_type i , …); DROP TABLE table_name; Examples CREATE TABLE CREATE TABLE jbdept ( id INT, name VARCHAR(20), store INT NOT NULL, floor INT, manager INT); DROP table jbdept; 88 SQL is insensitive to white space. 88 SQL is insensitive to case (e.g., ...Hours... is equivalent to HOURS...) 3 IDA / ADIT Basic SFW query SELECT <a7ribute9list> FROM <table9list> WHERE <condition>; a7ribute9list: R1.A1, …, Rk.Ar A`ributes whose values to be required table9list: R1, …, Rk Relations to be queried condition: conditional (boolean) expression identifies the tuples that should be retrieved comparison operators(=, <>, >, >=, …) logical operators (and, or, not) 4 IDA / ADIT Reading a table List all information about the employees whose salary is more than 10000 SELECT * FROM jbemployee WHERE salary>10000; * is short hand for all columns. WHERE is optional. 5 IDA / ADIT Selection, Projection List name, birth year and salary for all employees whose last name contain ‘Ross’ SELECT name, salary, birthyear FROM jbemployee WHERE name LIKE '%Ross%'; LIKE matches a string against a pa`ern o % matches any sequence of 0 or more characters 6

Upload: others

Post on 21-Jul-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Create*and*drop*table4 Basic*SFW*query4TDDD12/timetable/Fo2-SQL-6in1.pdf · DBMS4 • A*brief*history4 o IBM*System*R4 o ANSI*SQL894 o ANSI*SQL92*(SQL2)4 o ANSI*SQL99*(SQL3)4 o ANSI*SQL*2003*(added*OLAP,*XML,*etc.)4

IDA / ADIT

Databasteknik*Databaser*och*bioinformatik*

SQL4

Fang*Wei8Kleiner4

IDA / ADIT

SQL4

•  SQL:*Structured*Query*Language4o  Pronounced*“S8Q8L”*or*“sequel”4o  The*standard*query*language*supported*by*most*commercial*

DBMS4•  A*brief*history4

o  IBM*System*R4o  ANSI*SQL894o  ANSI*SQL92*(SQL2)4o  ANSI*SQL99*(SQL3)4o  ANSI*SQL*2003*(added*OLAP,*XML,*etc.)4o  ANSI*SQL*2006*(added*more*XML)4o  ANSI*SQL*2008,*…4

2

IDA / ADIT

Create*and*drop*table4

CREATE TABLE*table_name)4(…,-column_namei-column_typei,-…);)

DROP TABLE table_name;4•  Examples4CREATE TABLE CREATE TABLE jbdept (

id INT, name VARCHAR(20), store INT NOT NULL, floor INT, manager INT); DROP table jbdept;

•  88*SQL*is*insensitive*to*white*space.4•  88*SQL*is*insensitive*to*case*(e.g.,*...Hours...*is*equivalent*

to*HOURS...)4

3 IDA / ADIT

Basic*SFW*query4

SELECT*<a7ribute9list>)FROM*<table9list>)WHERE*<condition>;44a7ribute9list:*R1.A1,-…,-Rk.Ar-)

4★*A`ributes*whose*values*to*be*required4table9list:-R1,-…,-Rk--

4★ Relations*to*be*queried4condition:*conditional*(boolean)*expression**4

4★ identifies*the*tuples*that*should*be*retrieved4•  comparison*operators(=,-<>,->,->=,-…)4•  logical*operators*(and,-or,-not)4

4

IDA / ADIT

Reading*a*table4

•  List*all*information*about*the*employees*whose*salary*is*more*than*100004

SELECT * FROM jbemployee

WHERE salary>10000;4

•  **is*short*hand*for*all*columns.4•  WHERE*is*optional.4

45 IDA / ADIT

Selection,*Projection4•  List*name,*birth*year*and*salary*for*all*employees*whose*

last*name*contain*‘Ross’

SELECT name, salary, birthyear FROM jbemployee

WHERE name LIKE '%Ross%';4

•  LIKE*matches*a*string*against*a*pa`ern4o  %*matches*any*sequence*of*0*or*more*characters4

6

Page 2: Create*and*drop*table4 Basic*SFW*query4TDDD12/timetable/Fo2-SQL-6in1.pdf · DBMS4 • A*brief*history4 o IBM*System*R4 o ANSI*SQL894 o ANSI*SQL92*(SQL2)4 o ANSI*SQL99*(SQL3)4 o ANSI*SQL*2003*(added*OLAP,*XML,*etc.)4

IDA / ADIT

Join*88*equijoin4

•  List*all*manager*names*of*the*departments4SELECT jbemployee.name, jbdept.name

FROM jbemployee, jbdept WHERE jbemployee.id = jbdept.manager;

Bargain Candy Jewelry

name jbdept manager

37 13 33

Ross, Stanley Ross, Stuart Edwards, Peter Thompson, Bob Smythe, Carol Hayes, Evelyn Evans, Michael Raveen, Lemon�

name jbemployee id

10 11 13 26 32 33 35 37

7 IDA / ADIT

Bargain Candy Jewelry

name jbdept manager

37 13 33

Ross, Stanley Ross, Stuart Edwards, Peter Thompson, Bob Smythe, Carol Hayes, Evelyn Evans, Michael Raveen, Lemon�

name jbemployee id

10 11 13 26 32 33 35 37

8

Join*partners4

Join*a`ributes4

Bargain Candy Jewelry

Raveen, Lemon Edwards, Peter Hayes, Evelyn

jbemployee.name jbdept.name

SELECT jbemployee.name, jbdept.name FROM jbemployee, jbdept WHERE jbemployee.id = jbdept.manager;

Bargain Candy Jewelry

Raveen, Lemon Edwards, Peter Hayes, Evelyn

jbemployee.name jbdept.name id

37 13 33

manager

37 13 33

IDA / ADIT

Ambiguous*names*88*Aliasing4

•  Whole*name 4SELECT jbemployee.name, jbdept.name FROM jbemployee, jbdept

WHERE jbemployee.id = jbdept.manager;

•  Alias 4 4SELECT e.name, d.name FROM jbemployee e, jbdept d

WHERE e.id = d.manager;

9 IDA / ADIT

Bag*vs.*set4

•  List*all*salaries4SELECT salary

FROM jbemployee; •  SQL*considers*a*table*as*a*multi8set*(bag),*i.e.*tuples*can*

occur*more*than*once*in*a*table*4•  Why?*4

o  Removing*duplicates*is*expensive4o  User*may*want*information*about*duplicates*(real*distribution)4o  Aggregation*operators4

4

salary&&300004400004250004430004380004250004250004550004

10

IDA / ADIT

Distinct4

•  List*all*salaries4SELECT salary

FROM jbemployee;

salary&&300004400004250004430004380004250004250004550004

•  List*all*salaries*without*duplicates4SELECT DISTINCT salary

FROM jbemployee;

salary&&300004400004250004430004380004550004

11 IDA / ADIT

Set*and*bag*operations4

•  Queries*can*be*combined*by*set*operations:*UNION, INTERSECT, EXCEPT*(MySQL*only*supports*UNION)4

•  Retrieve*all*first*names*of*all*people*in*our*mini*world4

(Set*semantic)4SELECT e.manager FROM jbemployee e UNION SELECT d.manager FROM jbdept d; (Bag*semantic)4SELECT e.manager FROM jbemployee e UNION ALL SELECT d.manager FROM jbdept d; 4

12

Page 3: Create*and*drop*table4 Basic*SFW*query4TDDD12/timetable/Fo2-SQL-6in1.pdf · DBMS4 • A*brief*history4 o IBM*System*R4 o ANSI*SQL894 o ANSI*SQL92*(SQL2)4 o ANSI*SQL99*(SQL3)4 o ANSI*SQL*2003*(added*OLAP,*XML,*etc.)4

IDA / ADIT

Subqueries4

•  List*suppliers*who*do*not*supply*part*No.*44SELECT supplier FROM jbsupplier, jbsupply WHERE supplier = id AND part != 4; Why*is*the*query*wrong?4•  Supplier*who*supplies*part*4*and*part*8,*9…4

!*False*positive*4•  Supplier*who*has*not*supplied*anything:*4

!*False*negative4!*By*using*join,*you*can*show*the*existence*of*something,*but*can*not*show*the*non8existence*of*something.

13 IDA / ADIT

Subqueries4

•  List*suppliers*who*do*not*supply*part*No.*44

SELECT jbsupplier.name FROM jbsupplier

WHERE jbsupplier.id NOT IN (SELECT supplier FROM jbsupply

WHERE part = 4); •  *x*IN*(*subquery)*checks*if**x*is*in*the*result*of*subquery4

14

IDA / ADIT

Subqueries4

•  List*suppliers*who*do*not*supply*part*No.*4*(second*solution*using*NOT EXISTS)

SELECT jbsupplier.name FROM jbsupplier WHERE NOT EXISTS(SELECT * from jbsupply

WHERE jbsupplier.id = jbsupply.supplie AND part =4);

•  EXISTS*(subquery*)*checks*if*the*result*of*subquery*is*non8empty4

•  This*is*a*correlated*subquery*88*a*subquery*that*references*tuple*variables*in*surrounding*queries4

15 IDA / ADIT

Operational*semantics*of*subquery4

•  List*suppliers*who*do*not*supply*part*No.*4* SELECT jbsupplier.name

FROM jbsupplier WHERE NOT EXISTS(SELECT * from jbsupply

WHERE jbsupplier.id = jbsupply.supplie AND part =4);

•  For*each*row*S*in*jbsupplier

o  Evaluate*the*subquery*with*the*appropriate*value*of*S.id o  If*the*result*of*the*subquery*is*not*empty,*output*S.name

•  !*a*nested*loop4

16

IDA / ADIT

Subquery*vs.*Join4

•  Avoid*unnecessary*using*of*subqueries!!

17

SELECT t1.c FROM t1, t2

WHERE t1.a = t2.b

SELECT t1.c FROM t1 WHERE t1.a IN (SELECT t2.b

FROM t2)

•  Using*join*is*more*efficient*!*many*dedicated*join*algorithms*for*speedup*of*query*processing4

•  By*using*subquery,*the*system*is*forced*to*execute*nested*loop*!*query*optimizer*can*make*it*more*efficient,*but*not*always 4

IDA / ADIT

Aggregates4

•  Standard*SQL*aggregate*functions:*COUNT, SUM, AVG, MIN, MAX

List*the*number*of*employees*and*their*average*salary4

SELECT COUNT(*), AVG(salary)

FROM jbemployee; 4! COUNT(*)*counts*the*number*of*rows4

18

Page 4: Create*and*drop*table4 Basic*SFW*query4TDDD12/timetable/Fo2-SQL-6in1.pdf · DBMS4 • A*brief*history4 o IBM*System*R4 o ANSI*SQL894 o ANSI*SQL92*(SQL2)4 o ANSI*SQL99*(SQL3)4 o ANSI*SQL*2003*(added*OLAP,*XML,*etc.)4

IDA / ADIT

Grouping4

•  Used*to*apply*an*aggregate*function*to*subgroups*of*tuples*in*a*relation4

! GROUP BY –*grouping*a`ributes44•  List*for*each*manager,*the*number*of*employees*(s)he*

manages*and*the*average*salary.*44SELECT manager, COUNT(*), AVG(salary)

FROM jbemployee GROUP BY manager;

19 IDA / ADIT

•  List*for*each*manager,*the*number*of*employees*(s)he*manages*and*the*average*salary.*4

4SELECT manager, COUNT(*), AVG(salary) FROM jbemployee GROUP BY manager;

manager--COUNT(*)-AVG(salary))45***************4************4747044***************3************2215641***************1************550004

20

manager----name-----------salary)45***************Smith**********6521044***************Lee**************2100045***************Brin*************4325044***************Page************1222045***************Jobs*************5675045***************Gates**********2467044***************Wills***********3325041***************Yang***********550004

IDA / ADIT

Operational*semantics*of*GROUP BY

SELECT … FROM … WHERE … GROUP BY … ; •  Compute**FROM*(join)4•  Compute**WHERE*(selection)4•  Compute**GROUP BY:*group*rows*according*to*the*values*

of**GROUP BY columns4•  Compute**SELECT*for*each*group*4•  For*aggregation*functions*with*DISTINCT*inputs,*first*

eliminate*duplicates*within*the*group4

"Number*of*groups*=*number*of*rows*in*the*final*output4

21 IDA / ADIT

Example*of*computing*GROUP BY SELECT manager, COUNT(*), AVG(salary) FROM jbemployee GROUP BY manager;

manager--COUNT(*)-AVG(salary))45***************4************4747044***************3************2215641***************1************550004

manager------name----------salary)45***************Smith**********6521044***************Lee**************2100045***************Brin*************4325044***************Page************1222045***************Jobs*************5675045***************Gates**********2467044***************Wills***********3325041***************Yang***********550004

manager----name-----------salary)45***************Smith**********6521045***************Brin*************4325045***************Jobs*************5675045***************Gates**********2467044***************Page************1222044***************Lee**************2100044***************Wills***********3325041***************Yang***********550004

Group*rows*according*to*the*values4of GROUP BY columns4

#Compute*SELECT*for*each*group4

22

IDA / ADIT

Restriction*on*SELECT

•  *If*a*query*uses*aggregation/group*by,*then*every*column*referenced*in*SELECT**must*be*either4o  Aggregated,*or4o  A GROUP BY column4

•  This*restriction*ensures*that*any*SELECT**expression*produces*only*one*value*for*each*group4

SELECT name, COUNT(*), AVG(salary) FROM jbemployee GROUP BY manager;

•  Recall*there*is*one*output*row*per*group4o  There*can*be*multiple*name*values*per*group4

23 IDA / ADIT

HAVING4

•  *Used*to*filter*groups*based*on*the*group*properties*(e.g.,*aggregate*values,*GROUP BY column*values)4

SELECT manager, COUNT(*), AVG(salary) FROM jbemployee

GROUP BY manager HAVING COUNT(*) >2; manager--COUNT(*)-AVG(salary))

45***************4************4747044***************3************2215641***************1************550004

24

Page 5: Create*and*drop*table4 Basic*SFW*query4TDDD12/timetable/Fo2-SQL-6in1.pdf · DBMS4 • A*brief*history4 o IBM*System*R4 o ANSI*SQL894 o ANSI*SQL92*(SQL2)4 o ANSI*SQL99*(SQL3)4 o ANSI*SQL*2003*(added*OLAP,*XML,*etc.)4

IDA / ADIT

Order*of*query*results4

•  Order*the*employee*table*by*the*salary4

SELECT * from jbemployee order by salary;4

25 IDA / ADIT

NULL*value4

•  SQL*solution*for*unknown*or*non8applicable*values*4o  A*special*value*NULL

o  For*every*domain4o  Special*rules*for*dealing*with*NULL’s4

•  Example: jbemployee(id, name, salary, manager) o  <199,*Bullock,*27000, NULL>4

•  When*we*operate*on*a*NULL**and*another*value*(including*another*NULL*)*using*+,*–,*etc.,*the*result*is*NULL4

•  *Aggregate*functions*ignore*NULL*,*except*COUNT(*) "(since*it*counts*rows)4

26

IDA / ADIT

Three8valued*logic4

•  TRUE*=*1,*FALSE*=*0,*UNKNOWN*=*0.54•  x**AND*y*=*min(x,y))•  x**OR*y*=*max(x,-y))•  NOT*x-=-1-–-x)•  When*we*compare*a**NULL*with*another*value*(including*

another**NULL)*using*=,*>,*etc.,*the*result*is**UNKNOWN4•  WHERE*and**HAVING*clauses*only*select*rows*for*output*if*

the*condition*evaluates*to*TRUE4o  UNKNOWN*is*not*enough4

27 IDA / ADIT

NULL*values4

•  SELECT AVG(SALARY) FROM jbemployee; •  SELECT SUM(SALARY)/COUNT(*) FROM jbemployee;

o  Not*equivalent4o  Although*AVG(SALARY) = SUM(SALARY)/COUNT(SALARY)**still4

•  SELECT * FROM jbemployee; •  SELECT * FROM EMPLOYEE WHERE SALARY=SALARY;

o  Not*equivalent4

•  List*all*employees*that*do*not*have*a*manager:4SELECT name FROM jbemployee WHERE manager IS NULL;

SALARY&&300004400004430004NULL4

28

IDA / ADIT

•  List*the*name*of*all*employees*together*with*the*names*of*their*managers.4o  Some*employees*do*not*have*any*manager4o  We*want*to*list*those*employees*too*–*where*manager*field*is*

noted*as*NULL4

SELECT E.name ‘Employee’, S.name ‘manager’

FROM jbemployee E, jbemployee S WHERE E.manager = S.id

o  Returns*only*‘Smith’*and*‘Wong’4o  Tuple*‘Borg’*does*not*have*a*join*partner4

Employee !manager!!Smith 4*** 4Borg4Wong****** 4 4Borg4

name !id !manager!!Smith******333445555* 4***1234567894Borg********123456789******4***NULL4Wong******888665555* 4***12345678944

name !id !manager!!Smith******333445555* 4***1234567894Borg********123456789******4***NULL4Wong******888665555* 4***12345678944

E4 S4

29

Outer**join4

IDA / ADIT

SELECT E.name 'Employee', S.name 'manager' FROM jbemployee E LEFT JOIN jbemployee S ON E.manager=S.id;4

•  A*left*outer*join*(LEFT JOIN)*of*R*with*S*includes*rows*in*R*join*S*plus*dangling*R*rows*padded*with*NULL4o  Dangling*R*rows*are*those*that*do*not*join*with*any*S*rows4

•  A*right*outer*join*(RIGHT JOIN)*of*R*with*S*includes*rows*in*R*join*S*plus*dangling*S*rows*padded*with*NULL4o  Dangling*S*rows*are*those*that*do*not*join*with*any*R*rows4

Employee !manager!!Smith 4*** 4Borg4Wong****** 4 4Borg4Borg 4 4NULL4

name !id !manager!!Smith******333445555* 4***1234567894Borg********123456789******4***NULL4Wong******888665555* 4***12345678944

name !id !manager!!Smith******333445555* 4***1234567894Borg********123456789******4***NULL4Wong******888665555* 4***12345678944

E4 S4

Dangling*row4

30

Page 6: Create*and*drop*table4 Basic*SFW*query4TDDD12/timetable/Fo2-SQL-6in1.pdf · DBMS4 • A*brief*history4 o IBM*System*R4 o ANSI*SQL894 o ANSI*SQL92*(SQL2)4 o ANSI*SQL99*(SQL3)4 o ANSI*SQL*2003*(added*OLAP,*XML,*etc.)4

IDA / ADIT

Add*tuples*into*table4

INSERT INTO <table>*(<a`r>,…)*VALUES*(*<val>,*…)*;4INSERT INTO*<table>*(<a`r>,*…)*<subquery>*;44

INSERT INTO jbcity VALUES(999, 'Indianapolis', 'Ind');4

31 IDA / ADIT

Update*data4

UPDATE*<table>*SET*<a`r>*=*<val>*,…**4WHERE*<condition>*;4

UPDATE*<table>*SET*(<a`r>,*….)*=*(*<subquery>*)*4WHERE*<condition>*;4

UPDATE*jbemployee4SET*salary*=*salary*1.14WHERE*manager*= 199;4

32

IDA / ADIT

Delete*data4

•  DELETE FROM <table>*WHERE*<condition>*;4

DELETE FROM jbemployee

WHERE id = 199;

33 IDA / ADIT

Constraints4

•  Restrictions*on*allowable*data*in*a*database4o  In*addition*to*the*simple*structure*and*type*restrictions*imposed*

by*the*table*definitions4o  Declared*as*part*of*the*schema4o  Enforced*by*the*DBMS4

•  Why*use*constraints?4o  Protect*data*integrity*(catch*errors)4o  Tell*the*DBMS*about*the*data*(so*it*can*optimize*be`er)4

34

IDA / ADIT

Type*of*SQL*constraints4

•  NOT NULL •  Key4•  Referential*integrity*(foreign*key)4•  General*assertion4•  Tuple8*and*a`ribute8based**CHECK’s4

35 IDA / ADIT

Key*declaration4

•  At*most*one*PRIMARY KEY**per*table4o  Typically*implies*a*primary*index4o  Rows*are*stored*inside*the*index,*typically*sorted*by*the*primary*

key*value*"*best*speedup*for*queries4

•  Any*number*of*UNIQUE**keys*per*table4o  Typically*implies*a*secondary*index4o  Pointers*to*rows*are*stored*inside*the*index*"*less*speedup*for*

queries4

36

Page 7: Create*and*drop*table4 Basic*SFW*query4TDDD12/timetable/Fo2-SQL-6in1.pdf · DBMS4 • A*brief*history4 o IBM*System*R4 o ANSI*SQL894 o ANSI*SQL92*(SQL2)4 o ANSI*SQL99*(SQL3)4 o ANSI*SQL*2003*(added*OLAP,*XML,*etc.)4

IDA / ADIT

Key*example4

CREATE TABLE jbemployee (id INT,

name VARCHAR(20), salary INT,

manager INT,

birthyear INT, startyear INT,

CONSTRAINT pk_employee PRIMARY KEY(id)) ENGINE=InnoDB;

37 IDA / ADIT

Referential*integrity*example4

•  jbsupply.supplier*references*jbsupplier.id o  Any*supplier*appears*in*jbsupply*must*also*exist*in*jbsupplier4

•  Jbsupply.part*references*jbparts.id o  Any*supplied*part must*exist*in*jbparts4

!*That*is,*no*“dangling*pointers”44•  Referenced*column(s)*must*be*PRIMARY KEY •  Referencing*column(s)*form*a*FOREIGN KEY ALTER TABLE*jbsupply*ADD CONSTRAINT y_supply_supplier*FOREIGN KEY*(supplier)*REFERENCES*jbsupplie(id);4ALTER TABLE*jbsupply*ADD CONSTRAINT y_supply_parts*FOREIGN KEY*(part)*REFERENCES*jbparts(id);4

38

IDA / ADIT

Enforcing*referential*integrity4

39

DELETE FROM jbemployee WHERE id = 199;

What*happens*to*the*rows*with*manager*value*of*199?*4

IDA / ADIT

Enforcing*referential*integrity4

40

What*happens*to*the*rows*with*manager*value*of*199?*4ALTER TABLE jbemployee ADD CONSTRAINT fk_emp_mgr FOREIGN KEY (manager) REFERENCES jbemployee(id) ON DELETE SET NULL;

IDA / ADIT

Enforcing*referential*integrity4

41

ALTER TABLE jbemployee ADD CONSTRAINT fk_emp_mgr FOREIGN KEY (manager) REFERENCES jbemployee(id) ON DELETE SET NULL;

IDA / ADIT

Views4

•  A*virtual*table*derived*from*other*–*possible*virtual*88*tables.4CREATE VIEW group_view AS SELECT manager, COUNT(*), AVG(SALARY) FROM jbemployee GROUP BY manager SELECT * FROM group_view •  Why?4

o  Simplify*query*commands4o  Provide*data*security4o  Enhance*programming*productivity4

•  Update*problems4

42