<<

TheRelationalModel

Chapter3

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 1

WhyStudytheRelationalModel?

Mostwidelyusedmodel.

¡ Vendors:IBM,Informix,Microsoft,Oracle, Sybase,etc. “Legacysystems”inoldermodels

¡ E.G.,IBM’sIMS Recentcompetitor:object-orientedmodel

¡ ObjectStore,Versant, Ontos

¡ Asynthesisemerging:object-relationalmodel • InformixUniversalServer, UniSQL,O2,Oracle,DB2

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 2

RelationalDatabase:Definitions

Relationaldatabase: aofrelations : madeupof2parts: ¡ Instance :a, withrowsandcolumns. #Rows=cardinality,#fields=degree/ arity. ¡ Schema: specifies nameofrelation,plusnameand typeofeach. • E.G.Students(sid:string,name:string,login:string, age:integer, gpa:real). Canthinkofarelationasaset ofrowsor (i.e.,allrowsaredistinct).

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 3 ExampleInstanceofStudentsRelation

sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8

Cardinality=3,degree=5,allrowsdistinct

Doallcolumnsinarelationinstancehaveto bedistinct?

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 4

RelationalQueryLanguages

Amajorstrengthoftherelationalmodel: supportssimple,powerfulquerying ofdata. Queriescanbewrittenintuitively,andthe DBMSisresponsibleforefficientevaluation.

¡ Thekey:precisesemanticsforrelationalqueries.

¡ Allowstheoptimizertoextensivelyre-order operations,andstillensurethattheanswerdoes notchange.

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 5

TheSQLQueryLanguage

DevelopedbyIBM(systemR)inthe1970s Needforastandardsinceitisusedbymany vendors Standards:

¡ SQL-86

¡ SQL-89(minorrevision)

¡ SQL-92(majorrevision)

¡ SQL-99(majorextensions,currentstandard)

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 6 TheSQLQueryLanguage

Tofindall18yearoldstudents,wecanwrite:

SELECT * sid name login age gpa FROM StudentsS 53666 Jones jones@cs 18 3.4 WHERE S.age=18 53688 Smith smith@ee 18 3.2

•Tofindjustnamesandlogins,replacethefirstline:

SELECT S.name,S.login

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 7

QueryingMultipleRelations

Whatdoesthefollowingquerycompute? SELECT S.name,E.cid FROM StudentsS,EnrolledE WHERE S.sid=E.sid AND E.grade=“A”

Giventhefollowinginstance sid cid grade ofEnrolled(isthispossibleif 53831 Carnatic101 C theDBMSensuresreferential 53831 Reggae203 B integrity?): 53650 Topology112 A 53666 History105 B

weget: S.name E.cid Smith Topology112

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 8

CreatingRelationsinSQL

CreatestheStudents CREATETABLEStudents relation.Observethatthe (sid:CHAR(20), type(domain)ofeachfield name:CHAR(20), isspecified,andenforcedby login:CHAR(10), age:INTEGER, theDBMSwhenever tuples gpa:REAL) areaddedormodified. Asanotherexample,the CREATETABLEEnrolled Enrolledtableholds (sid:CHAR(20), aboutcourses cid:CHAR(20), thatstudentstake. grade:CHAR(2))

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 9 DestroyingandAlteringRelations

DROPTABLEStudents

DestroystherelationStudents.Theschema informationand the tuples aredeleted.

ALTERTABLEStudents ADDCOLUMN firstYear:integer

TheschemaofStudentsisalteredbyaddinga newfield;every inthecurrentinstance isextendedwitha valueinthenewfield.

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 10

AddingandDeleting Tuples

Caninsertasingle tuple using:

INSERTINTOStudents(sid,name,login,age, gpa) VALUES (53688,‘Smith’,‘smith@ee’,18,3.2)

Candeleteall tuples satisfyingsome condition(e.g.,name=Smith):

DELETE FROM StudentsS WHERE S.name=‘Smith’

* Powerfulvariantsofthesecommandsareavailable;morelater! DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 11

IntegrityConstraints(ICs)

IC: conditionthatmustbetrueforanyinstance ofthedatabase;e.g.,domainconstraints.

¡ ICsarespecifiedwhenschemaisdefined.

¡ ICsarecheckedwhenrelationsaremodified. Alegal instanceofarelationisonethatsatisfies allspecifiedICs.

¡ DBMSshouldnotallowillegalinstances. IftheDBMSchecksICs,storeddataismore faithfultoreal-worldmeaning.

¡ Avoidsdataentryerrors,too!

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 12 PrimaryKeyConstraints

Asetoffieldsisakey forarelationif: 1.Notwodistinct tuples canhavesamevaluesinall keyfields,and 2.Thisisnottrueforanyofthekey.

¡ Part2false?A .

¡ Ifthere’s>1keyforarelation,oneofthekeysis chosen(byDBA)tobetheprimarykey. E.g., sid isakeyforStudents.(Whatabout name?)Theset{sid, gpa}isa superkey.

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 13

PrimaryandCandidateKeysinSQL

Possiblymanycandidatekeys (specifiedusing UNIQUE),oneofwhichischosenastheprimarykey.

“Foragivenstudentandcourse, CREATETABLE Enrolled thereisasinglegrade.”vs. (sid CHAR(20) “Studentscantakeonlyone cidCHAR(20), course,andreceiveasinglegrade gradeCHAR(2), forthatcourse;further,notwo PRIMARYKEY(sid,cid)) studentsinacoursereceivethe CREATETABLE Enrolled samegrade.” (sid CHAR(20) Usedcarelessly,anICcanprevent cidCHAR(20), thestorageofdatabaseinstances gradeCHAR(2), thatariseinpractice! PRIMARYKEY(sid), UNIQUE (cid,grade)) DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 14

ForeignKeys,ReferentialIntegrity

Foreignkey :Setoffieldsinonerelationthatisused to`refer’toa tuple inanotherrelation.(Must correspondtoprimarykeyofthesecondrelation.) Likea`logicalpointer’. E.g. sid isaforeignkeyreferringtoStudents:

¡ Enrolled(sid:string,cid:string,grade:string)

¡ Ifallforeignkeyconstraintsareenforced,referential integrity isachieved,i.e.,nodanglingreferences.

¡ Canyounameadatamodelw/oreferentialintegrity? • LinksinHTML!

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 15 ForeignKeysinSQL

OnlystudentslistedintheStudentsrelationshould beallowedtoenrollforcourses. CREATETABLE Enrolled (sid CHAR(20),cidCHAR(20),gradeCHAR(2), PRIMARYKEY(sid,cid), FOREIGNKEY(sid)REFERENCES Students) Enrolled sid cid grade Students 53666 Carnatic101 C sid name login age gpa 53666 Reggae203 B 53666 Jones jones@cs 18 3.4 53650 Topology112 A 53688 Smith smith@eecs 18 3.2 53666 History105 B 53650 Smith smith@math 19 3.8

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 16

EnforcingReferentialIntegrity

ConsiderStudentsandEnrolled; sid inEnrolledisa foreignkeythatreferencesStudents. WhatshouldbedoneifanEnrolled tuple witha non-existentstudentidisinserted?(Rejectit!) WhatshouldbedoneifaStudents tuple isdeleted? ¡ AlsodeleteallEnrolled tuples thatrefertoit. ¡ DisallowdeletionofaStudents tuple thatisreferredto. ¡ Set sid inEnrolled tuples thatrefertoittoadefault sid. ¡ (InSQL,also:Set sid inEnrolled tuples thatrefertoittoa specialvaluenull,denoting`unknown’ or`inapplicable’.) SimilarifprimarykeyofStudents tuple isupdated.

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 17

ReferentialIntegrityinSQL

SQL/92andSQL:1999 CREATETABLE Enrolled supportall4optionson (sid CHAR(20), deletesandupdates. cidCHAR(20), ¡ DefaultisNOACTION gradeCHAR(2), (delete/updateisrejected) PRIMARYKEY(sid,cid),

CASCADE (alsodelete FOREIGNKEY(sid) all tuples thatreferto REFERENCES Students deleted tuple) ONDELETECASCADE

ONUPDATESETDEFAULT) SETNULL/ SETDEFAULT (setsforeignkeyvalue ofreferencing tuple)

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 18 WheredoICsComeFrom?

ICsarebaseduponthesemanticsofthereal- worldenterprisethatisbeingdescribedinthe databaserelations. Wecancheckadatabaseinstancetoseeifan ICisviolated,butwecanNEVER inferthat anICistruebylookingataninstance.

¡ AnICisastatementaboutallpossibleinstances!

¡ Fromexample,weknowname isnotakey,butthe assertionthat sid isakeyisgiventous. KeyandforeignkeyICsarethemost common;moregeneralICssupportedtoo. DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 19

LogicalDBDesign:ERtoRelational

Entitysetstotables:

CREATETABLEEmployees name (ssn CHAR(11), ssn lot nameCHAR(20), lotINTEGER, Employees PRIMARYKEY(ssn))

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 20

RelationshipSetstoTables

Intranslatingarelationship CREATETABLEWorks_In( settoarelation,attributesof ssn CHAR(1), therelationmustinclude:

didINTEGER, ¡ Keysforeach sinceDATE, participatingentityset PRIMARYKEY(ssn,did), (asforeignkeys). FOREIGNKEY(ssn) • Thissetofattributes REFERENCES Employees, formsa superkey for FOREIGNKEY(did) therelation. REFERENCES Departments)

¡ Alldescriptiveattributes.

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 21 Review:KeyConstraints

since Eachdepthasat name dname mostonemanager, ssn lot did budget accordingtothe keyconstraint on Employees Manages Departments Manages.

Translationto relationalmodel?

1-to-1 1-toMany Many-to-1 Many-to-Many

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 22

TranslatingERDiagramswithKeyConstraints

CREATETABLEManages( Maprelationshiptoa ssn CHAR(11), table: didINTEGER, ¡ Notethatdid is sinceDATE, PRIMARYKEY(did), thekeynow! FOREIGNKEY(ssn)REFERENCES Employees, ¡ Separatetablesfor FOREIGNKEY(did)REFERENCESDepartments) Employeesand Departments.

CREATETABLEDept_Mgr(

Sinceeach didINTEGER, departmenthasa dname CHAR(20), uniquemanager,we budgetREAL, couldinstead ssn CHAR(11), sinceDATE, combineManages PRIMARYKEY(did), andDepartments. FOREIGNKEY(ssn)REFERENCES Employees)

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 23

Review:ParticipationConstraints

Doeseverydepartmenthaveamanager?

¡ Ifso,thisisaparticipationconstraint:theparticipationof DepartmentsinManagesissaidtobetotal (vs.partial). • Everydid valueinDepartmentstablemustappearina oftheManagestable(withanon-null ssn value!) since name dname ssn lot did budget

Employees Manages Departments

Works_In

DatabaseManagementSystems3ed,R.RamakrishnanasnindcJ.eGehrke 24 ParticipationConstraintsinSQL

Wecancaptureparticipationconstraintsinvolving oneentitysetinabinaryrelationship,butlittleelse (withoutresortingtoCHECK constraints).

CREATETABLEDept_Mgr( didINTEGER, dname CHAR(20), budgetREAL, ssn CHAR(11)NOTNULL, sinceDATE, PRIMARYKEY(did), FOREIGNKEY(ssn)REFERENCES Employees, ONDELETENOACTION)

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 25

Review:WeakEntities

Aweakentitycanbeidentifieduniquelyonlyby consideringtheprimarykeyofanother(owner)entity.

¡ Ownerentitysetandweakentitysetmustparticipateina one-to-manyrelationshipset(1owner,manyweakentities).

¡ Weakentitysetmusthavetotalparticipationinthis identifyingrelationshipset.

name cost ssn lot pname age

Employees Policy Dependents

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 26

TranslatingWeakEntitySets Weakentitysetandidentifyingrelationship setaretranslatedintoasingletable.

¡ Whentheownerentityisdeleted,allownedweak entitiesmustalsobedeleted. CREATETABLE Dep_Policy( pname CHAR(20), ageINTEGER, costREAL, ssn CHAR(11)NOTNULL, PRIMARYKEY(pname, ssn), FOREIGNKEY(ssn)REFERENCES Employees, ONDELETECASCADE)

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 27 name ssn lot

Review:ISAHierarchies Employees

hourly_wages hours_worked ISA AsinC++,orother PLs, contractid attributesareinherited.

Hourly_Emps Contract_Emps IfwedeclareAISA B,everyA entityisalsoconsideredtobeaB entity.

Overlapconstraints:CanJoebeanHourly_Emps aswellas aContract_Emps entity?(Allowed/disallowed) Coveringconstraints:DoeseveryEmployeesentityalsohave tobeanHourly_Emps oraContract_Emps entity? (Yes/no)

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 28

TranslatingISAHierarchiestoRelations

Generalapproach: ¡ 3relations:Employees,Hourly_Emps andContract_Emps. • Hourly_Emps:Everyemployeeisrecordedin Employees.Forhourly emps,extrainforecordedin Hourly_Emps (hourly_wages,hours_worked, ssn);must deleteHourly_Empstuple ifreferencedEmployees tuple isdeleted). • Queriesinvolvingallemployeeseasy,thoseinvolving justHourly_Emps requireatogetsomeattributes. Alternative:JustHourly_Emps andContract_Emps. ¡ Hourly_Emps: ssn,name,lot,hourly_wages,hours_worked. ¡ Eachemployeemustbeinoneofthesetwosubclasses.

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 29

Review:Binaryvs.Ternary

Relationships name ssn lot pname age

Employees Covers Dependents Whatarethe Baddesign Policies additional constraintsin policyid cost name pname age the2nd ssn lot diagram? Dependents Employees

Purchaser Beneficiary

Betterdesign Policies

policyid cost DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 30 Binaryvs.TernaryRelationships(Contd.)

CREATETABLEPolicies(

Thekey policyidINTEGER, constraintsallow costREAL, ustocombine ssnCHAR(11)NOTNULL, Purchaserwith Policiesand PRIMARYKEY(policyid). Beneficiarywith FOREIGNKEY(ssn)REFERENCES Employees, Dependents. ONDELETECASCADE) Participation CREATETABLEDependents ( constraintsleadto pnameCHAR(20), NOTNULL ageINTEGER, constraints. INTEGER policyid , WhatifPoliciesis PRIMARYKEY(pname,policyid). aweakentityset? FOREIGNKEY(policyid)REFERENCES Policies, ONDELETECASCADE) DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 31

Views

A isjustarelation,butwestorea definition,ratherthanasetoftuples.

CREATEVIEWYoungActiveStudents(name,grade) AS SELECTS.name,E.grade FROM StudentsS,EnrolledE

WHERE S.sid=E.sidandS.age<21

ViewscanbedroppedusingtheDROPVIEWcommand.

¡ HowtohandleDROPTABLEifthere’saviewonthetable?

• DROPTABLEcommandhasoptionstolettheuserspecify this.

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 32

ViewsandSecurity

Viewscanbeusedtopresentnecessary information(orasummary),whilehiding detailsinunderlyingrelation(s).

¡ GivenYoungStudents,butnotStudentsor Enrolled,wecanfindstudentsswhohaveare enrolled,butnotthecid’softhecoursestheyare enrolledin.

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 33 RelationalModel:Summary

Atabularrepresentationofdata. Simpleandintuitive,currentlythemostwidelyused. IntegrityconstraintscanbespecifiedbytheDBA, basedonapplicationsemantics.DBMSchecksfor violations.

TwoimportantICs:primaryandforeignkeys

Inaddition,wealways havedomainconstraints. Powerfulandnaturalquerylanguagesexist. RulestotranslateERtorelationalmodel

DatabaseManagementSystems3ed,R.RamakrishnanandJ.Gehrke 34