course%registration:%system%problems%from%registration ...tddd37/fo/fo2-sql_6_in_1_new.pdf ·...

8
TDDD37 TDDD37 Database technology SQL Fang Wei9Kleiner [email protected] hBp://www.ida.liu.se/~TDDD37 TDDD37 Announcement Course registration: system problems from registration office. Be patient. Registration for the lab: possible without being registered to the course ! do that now. Encourage building a lab group with two. Temporary solution for the lab homework without an DB account: install mySQL and download the scripts from the lab website. 2 TDDD37 SQL SQL: Structured Query Language o Pronounced “S9Q9L” 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, … 3 TDDD37 Create and drop table CREATE TABLE table_name (…, column_name i column_type i , …); DROP TABLE table_name; Examples CREATE TABLE WORKS_ON ( ESSN integer, PNO integer, HOURS decimal(3,1)); DROP table Student; 99 SQL is insensitive to white space. 99 SQL is insensitive to case (e.g., ...Hours... is equivalent to HOURS...) 4 TDDD37 Basic SFW query SELECT <a7ribute9list> FROM <table9list> WHERE <condition>; a7ribute9list: R1.A1, …, Rk.Ar ABributes 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) 5 TDDD37 Reading a table List all information about the employees of department 5 SELECT * FROM EMPLOYEE WHERE DNO = 5; * is short hand for all columns. WHERE is optional. 6

Upload: others

Post on 26-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Course%registration:%system%problems%from%registration ...TDDD37/fo/Fo2-SQL_6_in_1_new.pdf · TDDD37 Operational%semantics%of%subquery5 • List%employees%do%not%have%project%assignment%more%than%10%

TDDD37

TDDD37%%Database%technology%

SQL5

Fang%[email protected]

hBp://www.ida.liu.se/~TDDD375

TDDD37

Announcement5

•  Course%registration:%system%problems%from%registration%office.%Be%patient.5

•  Registration%for%the%lab:%possible%without%being%registered%to%the%course%!%do%that%now.5

•  Encourage%building%a%lab%group%with%two.5•  Temporary%solution%for%the%lab%homework%without%an%DB%

account:%install%mySQL%and%download%the%scripts%from%the%lab%website.5

2

TDDD37

SQL5

•  SQL:%Structured%Query%Language5o  Pronounced%“S9Q9L”%or%“sequel”5o  The%standard%query%language%supported%by%most%commercial%

DBMS5•  A%brief%history5

o  IBM%System%R5o  ANSI%SQL895o  ANSI%SQL92%(SQL2)5o  ANSI%SQL99%(SQL3)5o  ANSI%SQL%2003%(added%OLAP,%XML,%etc.)5o  ANSI%SQL%2006%(added%more%XML)5o  ANSI%SQL%2008,%…5

3 TDDD37

Create%and%drop%table5

CREATE TABLE%table_name)5(…,-column_namei-column_typei,-…);)

DROP TABLE table_name;5•  Examples5CREATE TABLE WORKS_ON (

ESSN integer, PNO integer, HOURS decimal(3,1));

DROP table Student; •  99%SQL%is%insensitive%to%white%space.5•  99%SQL%is%insensitive%to%case%(e.g.,%...Hours...%is%equivalent%to%

HOURS...)5

4

TDDD37

Basic%SFW%query5

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

5★%ABributes%whose%values%to%be%required5table9list:-R1,-…,-Rk--

5★ Relations%to%be%queried5condition:%conditional%(boolean)%expression%%5

5★ identifies%the%tuples%that%should%be%retrieved5•  comparison%operators(=,-<>,->,->=,-…)5•  logical%operators%(and,-or,-not)5

5 TDDD37

Reading%a%table5

•  List%all%information%about%the%employees%of%department%55

SELECT * FROM EMPLOYEE

WHERE DNO = 5;

5•  *%is%short%hand%for%all%columns.5•  WHERE%is%optional.5

5

6

Page 2: Course%registration:%system%problems%from%registration ...TDDD37/fo/Fo2-SQL_6_in_1_new.pdf · TDDD37 Operational%semantics%of%subquery5 • List%employees%do%not%have%project%assignment%more%than%10%

TDDD37

Selection%and%projection5

•  List%last%name,%birth%date%and%address%for%all%employees%whose%name%is%‘Alicia J. Zelaya’

SELECT LNAME, BDATE, ADDRESS FROM EMPLOYEE

WHERE FNAME = ‘Alicia’ AND MINIT = ‘J’ AND LNAME = ‘Zeleya’; 5•  String%literals%(case%sensitive)%are%enclosed%in%single%quote5

7 TDDD37

PaBern%matching5

•  List%last%name,%birth%date%and%address%for%all%employees%whose%last%name%contain%‘aya’

SELECT LNAME, BDATE, ADDRESS FROM EMPLOYEE

WHERE LNAME LIKE ‘%aya%’; 5•  LIKE%matches%a%string%against%a%paBern5

o  %%matches%any%sequence%of%0%or%more%characters5

8

TDDD37

Join%99%equijoin5

•  List%all%employees%and%names%of%their%department5SELECT LNAME, DNAME

FROM EMPLOYEE, DEPARTMENT WHERE DNO = DNUMBER;

Research Administration headquarters

DNAME DEPARTMENT DNUM

5 4 1

Smith Wong Zelaya Wallace Narayan English Jabbar Borg�

LNAME EMPLOYEE DNO

5 5 4 4 5 5 4 1

9 TDDD37

Ambiguous%names%99%Aliasing5

Research Administration headquarters

NAME DEPARTMENT DNUM

5 4 1

Smith Wong Zelaya Wallace Narayan English Jabbar Borg�

NAME EMPLOYEE DNO

5 5 4 4 5 5 4 1

•  Same%aBribute%name%used%in%different%relations5

SELECT NAME, NAME FROM EMPLOYEE, DEPARTMENT WHERE DNO=DNUMBER;

10

TDDD37

Ambiguous%names%99%Aliasing5

•  No%alias%(wrong)%5SELECT NAME, NAME FROM EMPLOYEE, DEPARTMENT

WHERE DNO=DNUMBER; •  Whole%name 5SELECT EMPLOYEE.NAME, DEPARTMENT.NAME

FROM EMPLOYEE, DEPARTMENT WHERE EMPLOYEE.DNO= DEPARTMENT.DNUMBER;

•  Alias 5 5SELECT E.NAME, D.NAME FROM EMPLOYEE E, DEPARTMENT D WHERE E.DNO=D.DNUMBER;

11 TDDD37

Self%join5

•  List%last%name%for%all%employees%together%with%last%names%of%their%bosses5

SELECT E.LNAME “Employee”, S. LNAME “Boss” FROM EMPLOYEE E, EMPLOYEE S

WHERE E.SUPERSSN = S.SSN;

Employee((Boss++Smith 5%%%Wong5Wong 5%%%Borg5Zelaya 5%%%Wallace5Wallace5%%%Borg5Narayan5%%%Wong5English5%%%Wong5Jabbar 5%%%Wallace)

12

Page 3: Course%registration:%system%problems%from%registration ...TDDD37/fo/Fo2-SQL_6_in_1_new.pdf · TDDD37 Operational%semantics%of%subquery5 • List%employees%do%not%have%project%assignment%more%than%10%

TDDD37

Bag%vs.%set5

•  List%all%salaries5SELECT SALARY

FROM EMPLOYEE; •  SQL%considers%a%table%as%a%multi9set%(bag),%i.e.%tuples%can%

occur%more%than%once%in%a%table%5•  Why?%5

o  Removing%duplicates%is%expensive5o  User%may%want%information%about%duplicates%(real%distribution)5o  Aggregation%operators5

5

SALARY++300005400005250005430005380005250005250005550005

13 TDDD37

Distinct5

•  List%all%salaries5SELECT SALARY

FROM EMPLOYEE;

SALARY++300005400005250005430005380005250005250005550005

•  List%all%salaries%without%duplicates5SELECT DISTINCT SALARY

FROM EMPLOYEE;

SALARY++300005400005250005430005380005550005

14

TDDD37

Set%and%bag%operations5

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

•  Retrieve%all%first%names%of%all%people%in%our%mini%world5

(Set%semantic)5SELECT FNAME FROM EMPLOYEE UNION SELECT DEPENDENT_NAME FROM DEPENDENT; (Bag%semantic)5SELECT FNAME FROM EMPLOYEE UNION ALL SELECT DEPENDENT_NAME FROM DEPENDENT; 5

15 TDDD37

Subqueries5

•  List%employees%do%not%have%project%assignment%more%than%10%hours.5

SELECT LNAME

FROM EMPLOYEE, WORKS_ON WHERE SSN = ESSN AND HOURS <= 10.0; Why%is%the%query%wrong?5•  Employees%who%do%not%work%in%any%project:%5

o  They%should%be%in%the%answer%set,%but%is%not%from%the%above%query%!%their%SSN%does%not%occur%in%WORKS_ON

16

TDDD37

Subqueries5

•  List%employees%do%not%have%project%assignment%more%than%10%hours.5

SELECT LNAME FROM EMPLOYEE

WHERE SSN NOT IN (SELECT ESSN FROM WORKS_ON WHERE HOURS > 10.0);

•  %x%IN%(%subquery)%checks%if%%x%is%in%the%result%of%subquery5

17 TDDD37

Subqueries5

•  List%employees%do%not%have%project%assignment%more%than%10%hours.%(solution%2%using%NOT EXISTS)5

SELECT LNAME FROM EMPLOYEE WHERE NOT EXISTS (SELECT * FROM WORKS_ON WHERE SSN = ESSN AND HOURS > 10.0);

•  EXISTS%(subquery%)%checks%if%the%result%of%subquery%is%non9empty5

•  This%is%a%correlated%subquery%99%a%subquery%that%references%tuple%variables%in%surrounding%queries5

18

Page 4: Course%registration:%system%problems%from%registration ...TDDD37/fo/Fo2-SQL_6_in_1_new.pdf · TDDD37 Operational%semantics%of%subquery5 • List%employees%do%not%have%project%assignment%more%than%10%

TDDD37

Operational%semantics%of%subquery5

•  List%employees%do%not%have%project%assignment%more%than%10%hours.5

SELECT LNAME FROM EMPLOYEE WHERE NOT EXISTS (SELECT * FROM WORKS_ON WHERE SSN = ESSN AND HOURS > 10.0);

•  For%each%row%E%in%EMPLOYEE o  Evaluate%the%subquery%with%the%appropriate%value%of%E.SSN o  If%the%result%of%the%subquery%is%not%empty,%output%E.LNAME

•  The%DBMS%query%optimizer%may%choose%to%process%the%query%in%an%equivalent,%but%more%efficient%way5

19 TDDD37

Aggregates5

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

•  List%the%number%of%employees%and%their%average%salary5

SELECT COUNT(*), AVG(SALARY) FROM EMPLOYEE;

5•  COUNT(*)%counts%the%number%of%rows5

20

TDDD37

Grouping5

•  Used%to%apply%an%aggregate%function%to%subgroups%of%tuples%in%a%relation5

GROUP BY –%grouping%aBributes55•  List%for%each%department%the%department%number,%the%

number%of%employees%and%the%average%salary.%55SELECT DNO, COUNT(*), AVG(SALARY)

FROM EMPLOYEE GROUP BY DNO;

21 TDDD37

•  List%for%each%department%the%department%number,%the%number%of%employees%and%the%average%salary.%5

5SELECT DNO, COUNT(*), AVG(SALARY) FROM EMPLOYEE GROUP BY DNO;

DNO--COUNT(*)-AVG(SALARY))55%%%%%%%%%%%%%%%4%%%%%%%%%%%%4747054%%%%%%%%%%%%%%%3%%%%%%%%%%%%2215651%%%%%%%%%%%%%%%1%%%%%%%%%%%%550005

22

DNO------NAME-----------SALARY)55%%%%%%%%%%%%%%%Smith%%%%%%%%%%6521054%%%%%%%%%%%%%%%Lee%%%%%%%%%%%%%%2100055%%%%%%%%%%%%%%%Brin%%%%%%%%%%%%%4325054%%%%%%%%%%%%%%%Page%%%%%%%%%%%%1222055%%%%%%%%%%%%%%%Jobs%%%%%%%%%%%%%5675055%%%%%%%%%%%%%%%Gates%%%%%%%%%%2467054%%%%%%%%%%%%%%%Wills%%%%%%%%%%%3325051%%%%%%%%%%%%%%%Yang%%%%%%%%%%%550005

TDDD37

Operational%semantics%of%GROUP BY

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

of%%GROUP BY columns5•  Compute%%SELECT%for%each%group%5•  For%aggregation%functions%with%DISTINCT%inputs,%first%

eliminate%duplicates%within%the%group5

"Number%of%groups%=%number%of%rows%in%the%final%output5

23 TDDD37

Example%of%computing%GROUP BY SELECT DNO, COUNT(*), AVG(SALARY) FROM EMPLOYEE GROUP BY DNO;

DNO--COUNT(*)-AVG(SALARY))55%%%%%%%%%%%%%%%4%%%%%%%%%%%%4747054%%%%%%%%%%%%%%%3%%%%%%%%%%%%2215651%%%%%%%%%%%%%%%1%%%%%%%%%%%%550005

DNO------NAME-----------SALARY)55%%%%%%%%%%%%%%%Smith%%%%%%%%%%6521054%%%%%%%%%%%%%%%Lee%%%%%%%%%%%%%%2100055%%%%%%%%%%%%%%%Brin%%%%%%%%%%%%%4325054%%%%%%%%%%%%%%%Page%%%%%%%%%%%%1222055%%%%%%%%%%%%%%%Jobs%%%%%%%%%%%%%5675055%%%%%%%%%%%%%%%Gates%%%%%%%%%%2467054%%%%%%%%%%%%%%%Wills%%%%%%%%%%%3325051%%%%%%%%%%%%%%%Yang%%%%%%%%%%%550005

DNO-----NAME-----------SALARY)55%%%%%%%%%%%%%%%Smith%%%%%%%%%%6521055%%%%%%%%%%%%%%%Brin%%%%%%%%%%%%%4325055%%%%%%%%%%%%%%%Jobs%%%%%%%%%%%%%5675055%%%%%%%%%%%%%%%Gates%%%%%%%%%%2467054%%%%%%%%%%%%%%%Page%%%%%%%%%%%%1222054%%%%%%%%%%%%%%%Lee%%%%%%%%%%%%%%2100054%%%%%%%%%%%%%%%Wills%%%%%%%%%%%3325051%%%%%%%%%%%%%%%Yang%%%%%%%%%%%550005

Group%rows%according%to%the%values5of GROUP BY columns5

#Compute%SELECT%for%each%group5

24

Page 5: Course%registration:%system%problems%from%registration ...TDDD37/fo/Fo2-SQL_6_in_1_new.pdf · TDDD37 Operational%semantics%of%subquery5 • List%employees%do%not%have%project%assignment%more%than%10%

TDDD37

Restriction%on%SELECT

•  %If%a%query%uses%aggregation/group%by,%then%every%column%referenced%in%SELECT%%must%be%either5o  Aggregated,%or5o  A GROUP BY column5

•  This%restriction%ensures%that%any%SELECT%%expression%produces%only%one%value%for%each%group5

SELECT NAME, COUNT(*), AVG(SALARY) FROM EMPLOYEE GROUP BY DNO;

•  Recall%there%is%one%output%row%per%group5o  There%can%be%multiple%NAME%values%per%group5

25 TDDD37

HAVING5

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

SELECT DNO, COUNT(*), AVG(SALARY) FROM EMPLOYEE

GROUP BY DNO HAVING COUNT(*) >2; DNO--COUNT(*)-AVG(SALARY))

55%%%%%%%%%%%%%%%4%%%%%%%%%%%%4747054%%%%%%%%%%%%%%%3%%%%%%%%%%%%2215651%%%%%%%%%%%%%%%1%%%%%%%%%%%%550005

26

TDDD37

Order%of%query%results5

•  Select%department%names%and%their%locations%in%alphabetical%order.5

SELECT DNAME, DLOCATION

FROM DEPARTMENT D, DEPT_LOCATIONS DL WHERE D.DNUMBER = DL.DNUMBER

ORDER BY DNAME ASC, DLOCATION DESC; DNAME----------DLOCATION)5Administration%%%Stafford%5Headquarters%%%%%%Houston%%5Research%%%%%%%%%%%%%%Sugarland5Research%%%%%%%%%%%%%Houston%%5Research%%%%%%%%%%%%%Bellaire5

27 TDDD37

NULL%value5

•  SQL%solution%for%unknown%or%non9applicable%values%5o  A%special%value%NULL

o  For%every%domain5o  Special%rules%for%dealing%with%NULL’s5

•  Example: EMPLOYEE(LNAME, SSN, SALARY, SUPERSSN) o  <Borg,%8888888,%55000, NULL>5

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

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

28

TDDD37

Three9valued%logic5

•  TRUE%=%1,%FALSE%=%0,%UNKNOWN%=%0.55•  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%%UNKNOWN5•  WHERE%and%%HAVING%clauses%only%select%rows%for%output%if%

the%condition%evaluates%to%TRUE5o  UNKNOWN%is%not%enough5

29 TDDD37

NULL%values5

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

o  Not%equivalent5o  Although%AVG(SALARY) = SUM(SALARY)/COUNT(SALARY)%%still5

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

o  Not%equivalent5

•  List%all%employees%that%do%not%have%a%boss:5SELECT LNAME FROM EMPLOYEE WHERE SUPERSSN IS NULL;

SALARY++300005400005430005NULL5

30

Page 6: Course%registration:%system%problems%from%registration ...TDDD37/fo/Fo2-SQL_6_in_1_new.pdf · TDDD37 Operational%semantics%of%subquery5 • List%employees%do%not%have%project%assignment%more%than%10%

TDDD37

•  List%the%last%name%of%all%employees%together%with%the%names%of%their%bosses.5o  Some%employees%do%not%have%any%boss5o  We%want%to%list%the%bossless%employees%too%–%where%boss%field%is%

noted%as%NULL5

SELECT E.LNAME “Employee”, S.LNAME “Boss”

FROM EMPLOYEE E, EMPLOYEE S WHERE E.SUPERSSN = S.SSN

o  Returns%only%‘Smith’%and%‘Wong’5o  Tuple%of%‘Borg’%does%not%have%a%join%partner5

Employee !Boss!!Smith 5%%% 5Borg5Wong%%%%%% 5 5Borg5

LNAME !SSN !SUPERSSN!!Smith%%%%%%333445555% 5%%%1234567895Borg%%%%%%%%123456789%%%%%%5%%%NULL5Wong%%%%%%888665555% 5%%%12345678955

LNAME !SSN !SUPERSSN!!Smith%%%%%%333445555% 5%%%1234567895Borg%%%%%%%%123456789%%%%%%5%%%NULL5Wong%%%%%%888665555% 5%%%12345678955

E5 S5

31

Outer%%join5

TDDD37

SELECT E.LNAME “Employee”, S.LNAME “Boss” FROM EMPLOYEE E LEFT JOIN EMPLOYEE S ON E.SUPERSSN = S.SSN

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

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

Employee !Boss!!Smith 5%%% 5Borg5Wong%%%%%% 5 5Borg5Borg 5 5NULL5

LNAME !SSN !SUPERSSN!!Smith%%%%%%333445555% 5%%%1234567895Borg%%%%%%%%123456789%%%%%%5%%%NULL5Wong%%%%%%888665555% 5%%%12345678955

LNAME !SSN !SUPERSSN!!Smith%%%%%%333445555% 5%%%1234567895Borg%%%%%%%%123456789%%%%%%5%%%NULL5Wong%%%%%%888665555% 5%%%12345678955

E5 S5

Dangling%row5

32

TDDD37

Add%tuples%into%table5

INSERT INTO <table>%(<aBr>,…)%VALUES%(%<val>,%…)%;5INSERT INTO%<table>%(<aBr>,%…)%<subquery>%;55•  Store%information%about%how%many%hours%an%employee%

works%for%the%project%’1|%into%WORKS_ON5 INSERT INTO%WORKS_ON%VALUES%(123456789,%1,%32.5);555

33 TDDD37

Update%data5

UPDATE%<table>%SET%<aBr>%=%<val>%,…%%5WHERE%<condition>%;5

UPDATE%<table>%SET%(<aBr>,%….)%=%(%<subquery>%)%5WHERE%<condition>%;5

•  Give%all%employees%in%the%‘Research’%department%a%10%%raise%in%salary.5

UPDATE%EMPLOYEE5SET%SALARY%=%SALARY*1.15WHERE%DNO%IN%%%%%(SELECT%DNUMBER%5%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%FROM%DEPARTMENT5%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%WHERE%DNAME%=%‘Research’);5

34

TDDD37

Delete%data5

•  DELETE FROM <table>%WHERE%<condition>%;5

•  Delete%employees%having%the%last%name%‘Borg’%from%the%EMPLOYEE%table5

DELETE FROM EMPLOYEE WHERE LNAME = ‘Borg’;

35 TDDD37

Constraints5

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

by%the%table%definitions5o  Declared%as%part%of%the%schema5o  Enforced%by%the%DBMS5

•  Why%use%constraints?5o  Protect%data%integrity%(catch%errors)5o  Tell%the%DBMS%about%the%data%(so%it%can%optimize%beBer)5

36

Page 7: Course%registration:%system%problems%from%registration ...TDDD37/fo/Fo2-SQL_6_in_1_new.pdf · TDDD37 Operational%semantics%of%subquery5 • List%employees%do%not%have%project%assignment%more%than%10%

TDDD37

Type%of%SQL%constraints5

•  NOT NULL •  Key5•  Referential%integrity%(foreign%key)5•  General%assertion5•  Tuple9%and%aBribute9based%%CHECK’s5

37 TDDD37

NOT NULL example5

CREATE TABLE EMPLOYEE (SSN INTEGER NOT NULL,

LNAME VARCHAR(30) NOT NULL, ADDRESS VARCHAR(30),

SALARY INTEGER,

SUPERSSN INTEGER);

38

TDDD37

Key%declaration5

•  At%most%one%PRIMARY KEY%%per%table5o  Typically%implies%a%primary%index5o  Rows%are%stored%inside%the%index,%typically%sorted%by%the%primary%

key%value%"%best%speedup%for%queries5

•  Any%number%of%UNIQUE%%keys%per%table5o  Typically%implies%a%secondary%index5o  Pointers%to%rows%are%stored%inside%the%index%"%less%speedup%for%

queries5

39 TDDD37

Key%example5

CREATE TABLE EMPLOYEE (SSN INTEGER NOT NULL PRIMARY KEY,

LNAME VARCHAR(30) NOT NULL, EMAIL VARCHAR(30) UNIQUE,

SALARY INTEGER,

SUPERSSN INTEGER);

40

TDDD37

Referential%integrity%example5

•  WORKS_ON.ESSN%references%EMPLOYEE.SSN5o  If%an%ESSN%appears%in%WORKS_ON,%it%must%appear%in%

EMPLOYEE5

•  WORKS_ON.PNO%references%PROJECT.PNUMBER5o  If%a%PNO%appears%in%WORKS_ON,%it%must%appear%in%PROJECT5

"That%is,%no%“dangling%pointers”5

•  Referenced%column(s)%must%be%PRIMARY KEY •  Referencing%column(s)%form%a%FOREIGN KEY

41 TDDD37

COMPANY%schema5•  EMPLOYEE%(FNAME,-MINIT,-LNAME,-SSN,-BDATE,-ADDRESS,-SEX,-SALARY,---SUPERSSN,----DNO)5

•  DEPT_LOCATIONS%(DNUMBER,-DLOCATION)5

•  DEPARTMENT%(DNAME,-DNUMBER,-MGRSSN,-MGRSTARTDATE)5

•  WORKS_ON2(ESSN,-PNO,-HOURS)%5

•  PROJECT%(PNAME,-PNUMBER,-PLOCATION,-DNUM)5

•  DEPENDENT%(ESSN,-DEPENDENT9NAME,-SEX,-BDATE,-RELATIONSHIP)5

42

Page 8: Course%registration:%system%problems%from%registration ...TDDD37/fo/Fo2-SQL_6_in_1_new.pdf · TDDD37 Operational%semantics%of%subquery5 • List%employees%do%not%have%project%assignment%more%than%10%

TDDD37

Create%tables5

CREATE TABLE WORKS_ON ( ESSN integer

constraint fk_works_emp references EMPLOYEE(SSN),

PNO integer

constraint fk_works_proj references PROJECT(PNUMBER),

HOURS decimal(3,1), constraint pk_workson primary key (ESSN, PNO) ); 5

43 TDDD37

Enforcing%referential%integrity5

DEPARTMENT+

888665555 1 Headquarters

987654321 4 Administration

333445555 5 Research

MGRSSN DNUMBER!DNAME

Foreign(key+

EMPLOYEE+ FNAME M LNAME SSN!

Ramesh K Narayan 666884444

Joyce A English 453453453

Ahmad V Jabbar 987987987

James E Borg 888665555

SET NULL ? SET%DEFAULT ? CASCADE ?

referential%integrity%constraints5

Delete%employees%having%the%last%name%‘Borg’%from%the%EMPLOYEE%table55DELETE FROM EMPLOYEE WHERE LNAME = ‘Borg’;

44

TDDD37

Views5

•  A%virtual%table%derived%from%other%–%possible%virtual%99%tables.5

CREATE VIEW dept_view AS SELECT DNO, COUNT(*), AVG(SALARY) FROM EMPLOYEE GROUP BY DNO •  Why?5

o  Simplify%query%commands5o  Provide%data%security5o  Enhance%programming%productivity5

•  Update%problems5

45