Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017...

39
CompSci 516 Data Intensive Computing Systems Lecture 2 SQL - 1 Instructor: Sudeepa Roy 1 Duke CS, Fall 2017 CompSci 516: Database Systems

Transcript of Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017...

Page 1: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

CompSci 516DataIntensiveComputingSystems

Lecture2SQL- 1

Instructor:Sudeepa Roy

1DukeCS,Fall2017 CompSci516:DatabaseSystems

Page 2: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

Announcements• Ifyouareenrolledtotheclass,buthavenotreceivedtheemail

fromPiazza,pleasesendmeanemail• TAofficehoursinLSRCD301• HW1postedonsakai

– Dueon09/21(Thurs),11:55pm.Startearly– nolatedays• Markyourcalendarformidtermdays:10/11,11/29

– Midtermswon’tbeheldlaterifyoumissanexam– Missedexampolicy:gradewillbescaledafter20%deductionfromthe

otherexam(deductionwaivedwithwrittenlettersfromDukeauthority)– Bothexamsmissed->incompletegrade

• Yourgradiance accountshouldbeactivebynow– Bringalaptopinclassforoccasionalpopupquizzes

• Lecture“notes”(incomplete)willbeuploadedbeforetheclass,“slides”(completed)aftertheclass

DukeCS,Fall2017 CompSci516:DatabaseSystems 2

Page 3: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

Recap:Lecture1

• WhyuseaDBMS• Structureddatamodel:Relationaldatamodel– table,schema,instance,tuples,attributes– bagandsetsemantic

• Logicalandphysicaldataindependence

DukeCS,Fall2017 CompSci516:DatabaseSystems 3

Page 4: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

Today’stopic• OverviewofXML• SQLinanutshell– Readingmaterial:[RG]Chapters3and5– Additionalreadingforpractice:[GUW]Chapter6

• TrySQLfromtoday’slectureonatoydatasetorDBLPdatasetonPostGres!

DukeCS,Fall2017 CompSci516:DatabaseSystems 4

Acknowledgement:Thefollowingslideshavebeencreatedadaptingtheinstructormaterialofthe[RG]bookprovidedbytheauthorsDr.Ramakrishnan andDr.Gehrke.

Page 5: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

XML:anoverview

DukeCS,Fall2017 CompSci516:DatabaseSystems 5

Page 6: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

Semi-structuredDataandXML

• XML:ExtensibleMarkupLanguage

• Willnotbecoveredindetailinclass,butmanydatasetsavailabletodownloadareinthisform– YouwilldownloadtheDBLPdatasetinXMLformatandtransformintorelationalform(inHW1)

• Datadoesnothaveafixedschema– “Attributes”arepartofthedata– Thedatais“self-describing”– Tree-structured

DukeCS,Fall2017 CompSci516:DatabaseSystems 6

Page 7: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

XML:Example<articlemdate="2011-01-11”key="journals/acta/Saxena96">

<author>SanjeevSaxena</author><title>ParallelIntegerSortingandSimulationAmongstCRCW

Models.</title><pages>607-619</pages><year>1996</year><volume>33</volume><journal>Acta Inf.</journal><number>7</number><url>db/journals/acta/acta33.html#Saxena96</url><ee>http://dx.doi.org/10.1007/BF03036466</ee>

</article>

DukeCS,Fall2017 CompSci516:DatabaseSystems 7

Attributes

Elements

Page 8: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

Attributevs.Elements

• Elementscanberepeatedandnested• Attributesareuniqueandatomic

DukeCS,Fall2017 CompSci516:DatabaseSystems 8

Page 9: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

WhyXML?

+ Servesasamodelsuitableforintegrationofdatabasescontainingsimilardatawithdifferentschemas

– e.g.trytointegratetwostudentdatabases:S1(sid,name,gpa)andS2(sid,dept,year)

– Many“nulls”ifdoneinrelationalmodel,veryeasyinXML• NULL=Akeywordtodenotemissingorunknownvalues

+Flexible– easytochangetheschemaanddata

- Makesqueryprocessingmoredifficult

Whichoneiseasier?• XML(semi-structured)torelational(structured)or• relational(structured)toXML(semi-structured)?

DukeCS,Fall2017 CompSci516:DatabaseSystems 9

Page 10: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

XMLtoRelationalModel

• Problem1:Repeatedattributes<book>

<author>Ramakrishnan</author><author>Gehrke</author><title>DatabaseManagementSystems</title><publisher>McGrawHill

</book>

Whatisagoodrelationalschema?

DukeCS,Fall2017 CompSci516:DatabaseSystems 10

Page 11: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

XMLtoRelationalModel

• Problem1:Repeatedattributes<book>

<author>Ramakrishnan</author><author>Gehrke</author><title>DatabaseManagementSystems</title><pubisher>McGrawHill</publisher>

</book>

DukeCS,Fall2017 CompSci516:DatabaseSystems 11

Title Publisher Author1 Author2

Whatifthepaperhasasingleauthor?

Page 12: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

XMLtoRelationalModel

• Problem1:Repeatedattributes<book>

<author>Garcia-Molina</author><author>Ullman</author><author>Widom</author><title>DatabaseSystems– TheCompleteBook</title><pubisher>PrenticeHall</publisher>

</book>

DukeCS,Fall2017 CompSci516:DatabaseSystems 12

Title Publisher Author1 Author2

Doesnotwork

Page 13: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

XMLtoRelationalModel

DukeCS,Fall2017 CompSci516:DatabaseSystems 13

BookId Title Publisher

b1 DatabaseManagementSystems

McGrawHill

b2 DatabaseSystems– TheCompleteBook

PrenticeHall

BookBookId Author

b1 Ramakrishnan

b1 Gehrke

b2 Garcia-Molina

b2 Ullman

b2 Widom

BookAuthoredBy

Page 14: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

XMLtoRelationalModel

• Problem2:Missingattributes

<book><author>Ramakrishnan</author><author>Gehrke</author><title>DatabaseManagementSystems</title><pubisher>McGrawHill<edition>Third</edition>

</book><book>

<author>Garcia-Molina</author><author>Ullman</author><author>Widom</author><title>DatabaseSystems– TheComplete

Book</title><pubisher>PrenticeHall</publisher>

</book>

DukeCS,Fall2017 CompSci516:DatabaseSystems 14

BookId

Title Publisher Edition

b1 DatabaseManagementSystems

McGrawHill

Third

b2 DatabaseSystems–TheCompleteBook

PrenticeHall

null

Page 15: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

Summary:DataModels

• Relationaldatamodelisthemoststandardfordatabasemanagements– andisthemainfocusofthiscourse

• Semi-structuredmodel/XMLisalsousedinpractice– youwillusetheminhwassignments

• Unstructureddata(text/photo/video)isunavoidable,butwon’tbecoveredinthisclass

DukeCS,Fall2017 CompSci516:DatabaseSystems 15

Page 16: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

SQL

DukeCS,Fall2017 CompSci516:DatabaseSystems 16

Page 17: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

RelationalQueryLanguages

• Amajorstrengthoftherelationalmodel:supportssimple,powerfulquerying ofdata.

• Queriescanbewrittenintuitively,andtheDBMSisresponsibleforanefficientevaluation– Thekey:precisesemanticsforrelationalqueries.– Allowstheoptimizertoextensivelyre-orderoperations,andstillensurethattheanswerdoesnotchange.

DukeCS,Fall2017 CompSci516:DatabaseSystems 17

Page 18: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

TheSQLQueryLanguage

• DevelopedbyIBM(systemR)inthe1970s• Needforastandardsinceitisusedbymanyvendors

• Standards:– SQL-86– SQL-89(minorrevision)– SQL-92(majorrevision)– SQL-99(majorextensions,currentstandard)

DukeCS,Fall2017 CompSci516:DatabaseSystems 18

Page 19: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

PurposesofSQL

• DataManipulationLanguage(DML)– Querying:SELECT-FROM-WHERE–Modifying:INSERT/DELETE/UPDATE

• DataDefinitionLanguage(DDL)– CREATE/ALTER/DROP

19DukeCS,Fall2017 CompSci516:DatabaseSystems

Page 20: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

TheSQLQueryLanguage

• Tofindall18yearoldstudents,wecanwrite:

SELECT *FROM Students SWHERE S.age=18

•To find just names and logins, replace the first line:

SELECT S.name, S.login

sid name login age gpa53666 Jones jones@cs 18 3.453688 Smith smith@ee 18 3.2

DukeCS,Fall2017 CompSci516:DatabaseSystems 20

allattributes

Page 21: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

QueryingMultipleRelations• Whatdoesthefollowing

querycompute?SELECT S.name, E.cidFROM Students S, Enrolled EWHERE S.sid=E.sid AND E.grade=“A”

sid cid grade53831 Carnatic101 C53831 Reggae203 B53650 Topology112 A53666 History105 B

Given the following instances of Enrolled and Students:

we get: ??

sid name login age gpa53666 Jones jones@cs 18 3.453688 Smith smith@eecs 18 3.253650 Smith smith@math 19 3.8

Students

Enrolled

DukeCS,Fall2016 CompSci516:DataIntensiveComputingSystems

Page 22: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

QueryingMultipleRelations• Whatdoesthefollowing

querycompute?SELECT S.name, E.cidFROM Students S, Enrolled EWHERE S.sid=E.sid AND E.grade=“A”

S.name E.cid Smith Topology112

sid cid grade53831 Carnatic101 C53831 Reggae203 B53650 Topology112 A53666 History105 B

Given the following instances of Enrolled and Students:

we get:

sid name login age gpa53666 Jones jones@cs 18 3.453688 Smith smith@eecs 18 3.253650 Smith smith@math 19 3.8

Students

Enrolled

DukeCS,Fall2016 CompSci516:DataIntensiveComputingSystems

Page 23: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

CreatingRelationsinSQL• Createsthe“Students”relation

– thetype(domain)ofeachfieldisspecified

– enforcedbytheDBMSwhenevertuplesareaddedormodified

• Asanotherexample,the“Enrolled”tableholdsinformationaboutcoursesthatstudentstake

CREATE TABLE Students(sid CHAR(20), name CHAR(20), login CHAR(10),age INTEGER,gpa REAL)

CREATE TABLE Enrolled(sid CHAR(20), cid CHAR(20), grade CHAR(2))

DukeCS,Fall2017 CompSci516:DatabaseSystems 23

sid cid grade53831 Carnatic101 C53831 Reggae203 B53650 Topology112 A53666 History105 B

sid name login age gpa53666 Jones jones@cs 18 3.453688 Smith smith@eecs 18 3.253650 Smith smith@math 19 3.8

StudentsEnrolled

Page 24: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

DestroyingandAlteringRelations

• DestroystherelationStudents– Theschemainformationand thetuplesaredeleted.

DROP TABLE Students

• The schema of Students is altered by adding a new field; every tuple in the current instance is extended with a null value in the new field.

ALTER TABLE Students ADD COLUMN firstYear: integer

DukeCS,Fall2017 CompSci516:DatabaseSystems 24

Page 25: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

AddingandDeletingTuples

• Caninsertasingletupleusing:INSERT INTO Students (sid, name, login, age, gpa)VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2)

• Can delete all tuples satisfying some condition (e.g., name = Smith):

DELETEFROM Students SWHERE S.name = ‘Smith’

DukeCS,Fall2017 CompSci516:DatabaseSystems 25

Page 26: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

IntegrityConstraints(ICs)

• IC: conditionthatmustbetrueforanyinstanceofthedatabase– e.g.,domainconstraints– ICsarespecifiedwhenschemaisdefined– ICsarecheckedwhenrelationsaremodified

• AlegalinstanceofarelationisonethatsatisfiesallspecifiedICs– DBMSwillnotallowillegalinstances

• IftheDBMSchecksICs,storeddataismorefaithfultoreal-worldmeaning– Avoidsdataentryerrors,too!

DukeCS,Fall2017 CompSci516:DatabaseSystems 26

Page 27: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

KeysinaDatabase

• Key/CandidateKey• PrimaryKey• SuperKey• ForeignKey

• Primarykeyattributesareunderlined inaschema– Person(pid,address,name)– Person2(address,name,age,job)

DukeCS,Fall2017 CompSci516:DatabaseSystems 27

Page 28: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

PrimaryKeyConstraints

• Asetoffieldsisakey forarelationif:1.Notwodistincttuplescanhavesamevaluesinallkeyfields,and

2.Thisisnottrueforanysubsetofthekey

• Part2false?Asuperkey• Ifthereare>1keysforarelation,oneofthekeysischosen(byDBA=DBadmin)tobetheprimarykey– E.g.,sidisakeyforStudents– Theset{sid,gpa}isasuperkey.

• Isthereanypossiblebenefittorefertoatupleusingprimarykey(thananykey)?

DukeCS,Fall2017 CompSci516:DatabaseSystems 28

Page 29: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

PrimaryandCandidateKeysinSQL

CREATE TABLE Enrolled(sid CHAR(20)cid CHAR(20),grade CHAR(2),PRIMARY KEY ???)

• “For a given student and course, there is a single grade.”

DukeCS,Fall2017 CompSci516:DatabaseSystems 29

• Possiblymanycandidatekeys– specifiedusingUNIQUE– oneofwhichischosenastheprimarykey.

Page 30: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

PrimaryandCandidateKeysinSQL

CREATE TABLE Enrolled(sid CHAR(20)cid CHAR(20),grade CHAR(2),PRIMARY KEY (sid,cid) )

DukeCS,Fall2017 CompSci516:DatabaseSystems 30

• Possiblymanycandidatekeys– specifiedusingUNIQUE– oneofwhichischosenastheprimarykey.

• “For a given student and course, there is a single grade.”

Page 31: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

PrimaryandCandidateKeysinSQL

CREATE TABLE Enrolled(sid CHAR(20)cid CHAR(20),grade CHAR(2),PRIMARY KEY (sid,cid) )

• “For a given student and course, there is a single grade.”

• vs.

• “Students can take only one course, and receive a single grade for that course; further, no two students in a course receive the same grade.”

CREATE TABLE Enrolled(sid CHAR(20)cid CHAR(20),grade CHAR(2),PRIMARY KEY ???,UNIQUE ??? )

DukeCS,Fall2017 CompSci516:DatabaseSystems 31

• Possiblymanycandidatekeys– specifiedusingUNIQUE– oneofwhichischosenastheprimarykey.

Page 32: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

PrimaryandCandidateKeysinSQL

DukeCS,Fall2017 CompSci516:DatabaseSystems 32

CREATE TABLE Enrolled(sid CHAR(20)cid CHAR(20),grade CHAR(2),PRIMARY KEY (sid,cid) )

• “For a given student and course, there is a single grade.”

• vs.

• “Students can take only one course, and receive a single grade for that course; further, no two students in a course receive the same grade.”

CREATE TABLE Enrolled(sid CHAR(20)cid CHAR(20),grade CHAR(2),PRIMARY KEY sid,UNIQUE (cid, grade))

• Possiblymanycandidatekeys– specifiedusingUNIQUE– oneofwhichischosenastheprimarykey.

Page 33: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

PrimaryandCandidateKeysinSQL

DukeCS,Fall2017 CompSci516:DatabaseSystems 33

CREATE TABLE Enrolled(sid CHAR(20)cid CHAR(20),grade CHAR(2),PRIMARY KEY (sid,cid) )

• “For a given student and course, there is a single grade.”

• vs.

• “Students can take only one course, and receive a single grade for that course; further, no two students in a course receive the same grade.”

• Used carelessly, an IC can prevent the storage of database instances that arise in practice!

CREATE TABLE Enrolled(sid CHAR(20)cid CHAR(20),grade CHAR(2),PRIMARY KEY sid,UNIQUE (cid, grade))

• Possiblymanycandidatekeys– specifiedusingUNIQUE– oneofwhichischosenastheprimarykey.

Page 34: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

ForeignKeys,ReferentialIntegrity

• Foreignkey:Setoffieldsinonerelationthatisusedto`refer’toatupleinanotherrelation– Mustcorrespondtoprimarykeyofthesecondrelation– Likea`logicalpointer’

• E.g.sid isaforeignkeyreferringtoStudents:– Enrolled(sid:string,cid:string,grade:string)– Ifallforeignkeyconstraintsareenforced,referentialintegrity isachieved

– i.e.,nodanglingreferences

DukeCS,Fall2017 CompSci516:DatabaseSystems 34

Page 35: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

ForeignKeysinSQL• OnlystudentslistedintheStudentsrelationshouldbeallowedtoenrollforcourses

CREATE TABLE Enrolled(sid CHAR(20), cid CHAR(20), grade CHAR(2),PRIMARY KEY (sid,cid),FOREIGN KEY (sid) REFERENCES Students )

sid name login age gpa53666 Jones jones@cs 18 3.453688 Smith smith@eecs 18 3.253650 Smith smith@math 19 3.8

sid cid grade53666 Carnatic101 C53666 Reggae203 B53650 Topology112 A53666 History105 B

EnrolledStudents

DukeCS,Fall2017 CompSci516:DatabaseSystems 35

Page 36: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

EnforcingReferentialIntegrity• ConsiderStudentsandEnrolled

– sidinEnrolledisaforeignkeythatreferencesStudents.

• WhatshouldbedoneifanEnrolledtuplewithanon-existentstudentidisinserted?– Rejectit!

• WhatshouldbedoneifaStudentstupleisdeleted?– ThreesemanticsallowedbySQL1. AlsodeleteallEnrolledtuplesthatrefertoit(cascadedelete)2. DisallowdeletionofaStudentstuplethatisreferredto3. SetsidinEnrolledtuplesthatrefertoittoadefaultsid4. (inadditioninSQL):SetsidinEnrolledtuplesthatrefertoittoaspecial

valuenull,denoting`unknown’or`inapplicable’

• SimilarifprimarykeyofStudentstupleisupdated

DukeCS,Fall2017 CompSci516:DatabaseSystems 36

Page 37: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

ReferentialIntegrityinSQL

• SQL/92andSQL:1999supportall4optionsondeletesandupdates.– DefaultisNOACTION(delete/updateisrejected)

– CASCADE (alsodeletealltuplesthatrefertodeletedtuple)

– SETNULL/ SETDEFAULT (setsforeignkeyvalueofreferencingtuple)

CREATE TABLE Enrolled(sid CHAR(20),cid CHAR(20),grade CHAR(2),PRIMARY KEY (sid,cid),FOREIGN KEY (sid)REFERENCES StudentsON DELETE CASCADEON UPDATE SET DEFAULT )

DukeCS,Fall2016 CompSci516:DataIntensiveComputingSystems

Page 38: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

WheredoICsComeFrom?• ICsarebaseduponthesemanticsofthereal-worldenterprise

thatisbeingdescribedinthedatabaserelations

• CanweinferICsfromaninstance?– WecancheckadatabaseinstancetoseeifanICisviolated,butwe

canNEVER inferthatanICistruebylookingataninstance.– AnICisastatementaboutallpossibleinstances!– Fromexample,weknownameisnotakey,buttheassertionthatsidis

akeyisgiventous.

• KeyandforeignkeyICsarethemostcommon;moregeneralICssupportedtoo

DukeCS,Fall2017 CompSci516:DatabaseSystems 38

Page 39: Lecture 2 SQL -1 · 2017. 8. 30. · Lecture 2 SQL -1 Instructor: SudeepaRoy Duke CS, Fall 2017 CompSci 516: Database Systems 1. Announcements • If you are enrolled to the class,

ExampleInstances

• WewillusetheseinstancesoftheSailorsandReservesrelationsinourexamples

• IfthekeyfortheReservesrelationcontainedonlytheattributessidandbid,howwouldthesemanticsdiffer?

sid sname rating age22 dustin 7 4531 lubber 8 5558 rusty 10 35

sid bid day22 101 10/10/9658 103 11/12/96

Reserves

Sailor

DukeCS,Fall2016 CompSci516:DataIntensiveComputingSystems