You are on page 1of 8

Chapter 3: The Relational Data Model and Relational Database Constraints

CHAPTER 3: THE RELATIONAL DATA MODEL AND RELATIONAL DATABASE CONSTRAINTS


Answers to Selected Exercises 3. - Suppose each of the following Update operations is applied directly to the database of Figure 3 ! Discuss all integrity constraints "iolated by each operation# if any# and the different ways of enforcing these constraints: $a% &nsert ' (Robert(# (F(# (Scott(# ()*3++,,*3(# (-.-/U0-*-(# (-3!, 0ewcastle Rd# 1ellaire# T2(# M# ,3444# (333!!,,,,(# . 5 into 6M789:66 $b% &nsert ' (7roduct;(# *# (1ellaire(# - 5 into 7R9/6CT $c% &nsert ' (7roduction(# *# ()*3++,,*3(# (4.-9CT-33( 5 into D67;RTM60T $d% &nsert ' (!++!+3)3)(# null# (*4 4( 5 into <9R=S>90 $e% &nsert ' (*,3*,3*,3(# (/ohn(# M# (.--D6C-!4(# (S79US6( 5 into D6760D60T $f% Delete the <9R=S>90 tuples with 6SS0? (333**,,,,( $g% Delete the 6M789:66 tuple with SS0? ()3+!,*3-.( $h% Delete the 7R9/6CT tuple with 70;M6? (7roduct2( $i% Modify the M@RSS0 and M@RST;RTD;T6 of the D67;RTM60T tuple with D0UM16R?, to (.-3*,!+3)( and (4.-9CT-33(# respecti"ely $A% Modify the SU76RSS0 attribute of the 6M789:66 tuple with SS0? ()))33++++( to ()*3++,,*3( $B% Modify the C9URS attribute of the <9R=S>90 tuple with 6SS0? ()))33++++( and 709? .4 to (, 4( Answers: $a% 0o constraint "iolations $b% Diolates referential integrity because D0UM?- and there is no tuple in the D67;RTM60T relation with D0UM16R?- <e Eay enforce the constraint by: $i% reAecting the insertion of the new 7R9/6CT tuple# $ii% changing the "alue of D0UM in the new 7R9/6CT tuple to an eFisting D0UM16R "alue in the D67;RTM60T relation# or $iii% inserting a new D67;RTM60T tuple with D0UM16R?$c% Diolates both the Bey constraint and referential integrity Diolates the Bey constraint because there already eFists a D67;RTM60T tuple with D0UM16R?* <e Eay enforce this constraint by: $i% reAecting the insertion# or $ii% changing the "alue of D0UM16R in the new D67;RTM60T tuple to a "alue that does not "iolate the Bey constraint Diolates referential integrity because M@RSS0?()*3++,,*3( and there is no tuple in the 6M789:66 relation with SS0?()*3++,,*3( <e Eay enforce the constraint by: $i% reAecting the insertion# $ii% changing the "alue of M@RSS0 to an eFisting SS0 "alue in 6M789:66# or $iii% inserting a new 6M789:66 tuple with SS0?()*3++,,*3( $d% Diolates both the entity integrity and referential integrity Diolates entity integrity
Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.

Chapter 3: The Relational Data Model and Relational Database Constraints

because 709# which is part of the priEary Bey of <9R=S>90# is null <e Eay enforce this constraint by: $i% reAecting the insertion# or $ii% changing the "alue of 709 in the new <9R=S>90 tuple to a "alue of 70UM16R that eFists in the 7R9/6CT relation Diolates referential integrity because 6SS0?(!++!+3)3)( and there is no tuple in the 6M789:66 relation with SS0?(!++!+3)3)( <e Eay enforce the constraint by: $i% reAecting the insertion# $ii% changing the "alue of 6SS0 to an eFisting SS0 "alue in 6M789:66# or $iii% inserting a new 6M789:66 tuple with SS0?(!++!+3)3)( $e% 0o constraint "iolations $f% 0o constraint "iolations $g% Diolates referential integrity because se"eral tuples eFist in the <9R=S>90# D6760D60T# D67;RTM60T# and 6M789:66 relations that reference the tuple being deleted froE 6M789:66 <e Eay enforce the constraint by: $i% reAecting the deletion# or $ii% deleting all tuples in the <9R=S>90# D6760D60T# D67;RTM60T# and 6M789:66 relations whose "alues for 6SS0# 6SS0# M@RSS0# and SU76RSS0# respecti"ely# is eGual to()3+!,*3-.( $h% Diolates referential integrity because two tuples eFist in the <9R=S>90 relations that reference the tuple being deleted froE 7R9/6CT <e Eay enforce the constraint by: $i% reAecting the deletion# or $ii% deleting the tuples in the <9R=S>90 relation whose "alue for 709?. $the "alue for the priEary Bey 70UM16R for the tuple being deleted froE 7R9/6CT% $i% 0o constraint "iolations $A% Diolates referential integrity because the new "alue of SU76RSS0?()*3++,,*3( and there is no tuple in the 6M789:66 relation with SS0?()*3++,,*3( <e Eay enforce the constraint by: $i% reAecting the deletion# or $ii% inserting a new 6M789:66 tuple with SS0?()*3++,,*3( $B% 0o constraint "iolations 3. ! - Consider the ;&R8&06 relational database scheEa shown in Figure 3 3# which describes a database for airline flight inforEation 6ach F8&@CT is identified by a flight 0UM16R# and consists of one or Eore F8&@CT>86@s with 86@>0UM16Rs .# -# 3# etc 6ach leg has scheduled arri"al and departure tiEes and airports# and has Eany 86@>&0ST;0C6s--one for each D;T6 on which the flight tra"els F;R6S are Bept for each flight For each leg instance# S6;T>R6S6RD;T&90s are Bept# as is the ;&R78;06 used in the leg# and the actual arri"al and departure tiEes and airports ;n ;&R78;06 is identified by an ;&R78;06>&D# and is of a particular ;&R78;06>T:76 C;0>8;0D relates ;&R78;06>T:76s to the ;&R79RTs in which they can land ;n ;&R79RT is identified by an ;&R79RT>C9D6 Consider an update for the ;&R8&06 database to enter a reser"ation on a particular flight or flight leg on a gi"en date $a% @i"e the operations for this update $b% <hat types of constraints would you eFpect to checBH $c% <hich of these constraints are Bey# entity integrity# and referential integrity constraints and which are notH

Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.

Chapter 3: The Relational Data Model and Relational Database Constraints

$d% Specify all the referential integrity constraints on Figure 3 3 Answers: $a% 9ne possible set of operations for the following update is the following: &0S6RT 'F09#809#DT#S6;T>09#CUST>0;M6#CUST>7C9065 into S6;T>R6S6RD;T&90I M9D&F: the 86@>&0ST;0C6 tuple with the condition: $ F8&@CT>0UM16R?F09 ;0D 86@>0UM16R?809 ;0D D;T6?DT% by setting 0UM16R>9F>;D;&8;186>S6;TS ? 0UM16R>9F>;D;&8;186>S6;TS - .I These operations should be repeated for each 86@ of the flight on which a reser"ation is Eade This assuEes that the reser"ation has only one seat More coEpleF operations will be needed for a Eore realistic reser"ation that Eay reser"e se"eral seats at once $b% <e would checB that 0UM16R>9F>;D;&8;186>S6;TS on each 86@>&0ST;0C6 of the flight is greater than . before doing any reser"ation $unless o"erbooBing is perEitted%# and that the S6;T>0UM16R being reser"ed in S6;T>R6S6RD;T&90 is a"ailable $c% The &0S6RT operation into S6;T>R6S6RD;T&90 will checB all the Bey# entity integrity# and referential integrity constraints for the relation The checB that 0UM16R>9F>;D;&8;186>S6;TS on each 86@>&0ST;0C6 of the flight is greater than . does not fall into any of the abo"e types of constraints $it is a general seEantic integrity constraint% $d% <e will write a referential integrity constraint as R ; --5 S $or R $2% --5 T% whene"er attribute ; $or the set of attributes 2% of relation R forE a foreign Bey that references the priEary Bey of relation S $or T% F8&@CT>86@ F8&@CT>0UM16R --5 F8&@CT F8&@CT>86@ D67;RTUR6>;&R79RT>C9D6 --5 ;&R79RT F8&@CT>86@ ;RR&D;8>;&R79RT>C9D6 --5 ;&R79RT 86@>&0ST;0C6 $F8&@CT>0UM16R#86@>0UM16R% --5 F8&@CT>86@ 86@>&0ST;0C6 D67;RTUR6>;&R79RT>C9D6 --5 ;&R79RT 86@>&0ST;0C6 ;RR&D;8>;&R79RT>C9D6 --5 ;&R79RT 86@>&0ST;0C6 ;&R78;06>&D --5 ;&R78;06 F;R6S F8&@CT>0UM16R --5 F8&@CT C;0>8;0D ;&R78;06>T:76>0;M6 --5 ;&R78;06>T:76 C;0>8;0D ;&R79RT>C9D6 --5 ;&R79RT ;&R78;06 ;&R78;06>T:76 --5 ;&R78;06>T:76 S6;T>R6S6RD;T&90 $F8&@CT>0UM16R#86@>0UM16R#D;T6% --5 86@>&0ST;0C6 3. 3 - Consider the relation C8;SS$CourseJ# Uni">SectionJ# &nstructor0aEe# SeEester# 1uildingCode# RooEJ# TiEe7eriod# <eeBdays# CreditCours% This represents classes taught in a uni"ersity with uniGue Uni">SectionJ @i"e what you thinB should be "arious candidate Beys and write in your own words under what constraints each candidate Bey would be "alid Answer: 7ossible candidate Beys include the following $0ote: <e assuEe that the "alues of the SeEester attribute include the yearI for eFaEple KSpringL)*K or KFallL)3K could be "alues for SeEester%: . MSeEester# 1uildingCode# RooEJ# TiEe7eriod# <eeBdaysN if the saEe rooE cannot be used at the saEe tiEe by Eore than one course during a particular seEester - MUni">SectionJN if it is uniGue across all seEesters 3 M&nstructor0aEe# SeEesterN if an instructor can teach at Eost one course during each seEester * &f Uni">SectionJ is not uniGue# which is the case in Eany uni"ersities# we ha"e to
Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.

Chapter 3: The Relational Data Model and Relational Database Constraints

eFaEine the rules that the uni"ersity uses for section nuEbering For eFaEple# if the sections of a particular course during a particular seEester are nuEbered .# -# 3# # then a candidate Bey would be MCourseJ# Uni">SectionJ# SeEesterN &f# on the other hand# all sections $of any course% ha"e uniGue nuEbers during a particular seEester only# then the candidate Bey would be MUni">SectionJ# SeEesterN 3. " - Consider the following siF relations for an order-processing database application in a coEpany: CUST9M6R $CustJ# CnaEe# City% 9RD6R $9rderJ# 9date# CustJ# 9rd>;Et% 9RD6R>&T6M $9rderJ# &teEJ# Oty% &T6M $&teEJ# Unit>price% SC&7M60T $9rderJ# <arehouseJ# Ship>date% <;R6C9US6 $<arehouseJ# City% Cere# 9rd>;Et refers to total dollar aEount of an orderI 9date is the date the order was placedI Ship>date is the date an order $or part of an order% is shipped froE the warehouse ;ssuEe that an order can be shipped froE se"eral warehouses Specify the foreign Beys for this scheEa# stating any assuEptions you EaBe <hat other constraints can you thinB of for this databaseH Answer: Strictly speaBing# a foreign Bey is a set of attributes# but when that set contains only one attribute# then that attribute itself is often inforEally called a foreign Bey The scheEa of this Guestion has the following fi"e foreign Beys: . the attribute CustJ of relation 9RD6R that references relation CUST9M6R# - the attribute 9rderJ of relation 9RD6R>&T6M that references relation 9RD6R# 3 the attribute &teEJ of relation 9RD6R>&T6M that references relation &T6M# * the attribute 9rderJ of relation SC&7M60T that references relation 9RD6R# and , the attribute <arehouseJ of relation SC&7M60T that references relation <;R6C9US6 <e now gi"e the Gueries in relational algebra:

Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.

Chapter 3: The Relational Data Model and Relational Database Constraints

The abo"e Guery lists all orders for which no PtiEelyQ shipEent was Eade# including orders for which no shipEent was e"er Eade &t is instructi"e to coEpare the abo"e Guery with the one below that lists those orders for which at least one PlateQ shipEent was Eade

3. # - Consider the following relations for a database that Beeps tracB of business trips of salespersons in a sales office: S;86S76RS90 $SS0# 0aEe# Start>:ear# Dept>0o% TR&7 $SS0# FroE>City# To>City# Departure>Date# Return>Date# Trip>&D% 62760S6 $Trip>&D# ;ccountJ# ;Eount% Specify the foreign Beys for this scheEa# stating any assuEptions you EaBe Answer: The scheEa of this Guestion has the following two foreign Beys: . the attribute SS0 of relation TR&7 that references relation S;86S76RS90# and - the attribute Trip>&D of relation 62760S6 that references relation TR&7 &n addition# the attributes Dept>0o of relation S;86S76RS90 and ;ccountJ of relation 62760S6 are probably also foreign Beys referencing other relations of the database not Eentioned in the Guestion <e now gi"e the Gueries in relational algebra:

3. $ - Consider the following relations for a database that Beeps tracB of student enrollEent in courses and the booBs adopted for each course: STUD60T $SS0# 0aEe# MaAor# 1date% C9URS6 $CourseJ# Ouarter# @rade% 60R988 $SS0# CourseJ# Ouarter# @rade% 199=>;D97T&90 $CourseJ# Ouarter# 1ooB>&S10%
Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.

Chapter 3: The Relational Data Model and Relational Database Constraints

T62T $1ooB>&S10# 1ooB>Title# 7ublisher# ;uthor% Specify the foreign Beys for this scheEa# stating any assuEptions you EaBe Answer: The scheEa of this Guestion has the following four foreign Beys: 3 the attribute SS0 of relation 60R988 that references relation STUD60T# * the attribute CourseJ in relation 60R988 that references relation C9URS6# , the attribute CourseJ in relation 199=>;D97T&90 that references relation C9URS6# and ! the attribute 1ooB>&S10 of relation 199=>;D97T&90 that references relation T62T <e now gi"e the Gueries in relational algebra:

3. % - Database design often in"ol"es decisions about the storage of attributes For eFaEple a Social Security 0uEber can be stored as a one attribute or split into three attributes $one for each of the three hyphen-deliniated groups of nuEbers in a Social Security 0uEberR222-22-2222% Cowe"er# Social Security 0uEber is usually stored in one attribute The decision is usually based on how the database will be used This eFercise asBs you to thinB about specific situations where di"iding the SS0 is useful Answer: a <e need the area code $also Bnow as city code in soEe countries% and perhaps the country code $for dialing international phone nuEbers% b & would recoEEend storing the nuEbers in a separate attribute as they ha"e their own independent eFistence For eFaEple# if an area code region were split into two regions# it would change the area code associated with certain nuEbers# and ha"ing area code in a separate attribute will EaBe it is easier to update the area code attribute by itself
Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.

Chapter 3: The Relational Data Model and Relational Database Constraints

c & would recoEEend splitting first naEe# Eiddle naEe# and last naEe into different attributes as it is liBely that the naEes Eay be sorted andLor retrie"ed by the last naEe# etc d &n general# if the each attribute has an independent logical eFistence based on the application# it would EaBe sense to store it in a separate coluEn otherwise there is no clear ad"antage For eFaEple# SS0 need not be split into its coEponent unless we are using the subseGuences to EaBe deductions about "alidity# geography# etc &n the two cases abo"e# it Eade logical and business sense to split the attributes 3. & - Consider a STUD60T relation in a U0&D6RS&T: database with the following attributes $0aEe# SS0# 8ocal>phone# ;ddress# Cell>phone# ;ge# @7;% 0ote that the cell phone Eay be froE a different city and state $or pro"ince% froE the local phone ; possible tuple of the relation is shown below: 0aEe @eorge Shaw <illiaE 6dwards SS0 .-3-*,!+3) 8ocal7hone ,,,-.-3* ;ddress .-3 Main St # ;nytown# C; )*,3) Cell7hon e ,,,-*3-. ;ge .) @7; 3 +,

a &dentify the critical Eissing inforEation froE the 8ocal7hone and Cell7hone attributes as shown in the eFaEple abo"e $Cint: Cow do call soEeone who li"es in a different state or pro"inceH% b <ould you store this additional inforEation in the 8ocal7hone and Cell7hone attributes or add new attributes to the scheEa for STUD60TH c Consider the 0aEe attribute <hat are the ad"antages and disad"antages of splitting this field froE one attribute into three attributes $first naEe# Eiddle naEe# and last naEe%H d <hat general guideline would you recoEEend for deciding when to store inforEation in a single attribute and when to split the inforEation Answer: a ; coEbination of first naEe# last naEe# and hoEe phone Eay address the issue assuEing that there are no two students with identical naEes sharing a hoEe phone line &t also assuEes that e"ery student has a hoEe phone nuEber ;nother solution Eay be to use first naEe# last naEe# and hoEe Sip code This again has a potential for duplicates# which would be "ery rare within one uni"ersity ;n eFtreEe solution is to use a coEbination of characters froE last naEe# EaAor# house nuEber etc b &f we use naEe in a priEary Bey and the naEe changes then the priEary Bey changes Changing the priEary Bey is acceptable but can be inefficient as any references to this Bey in the database need to be appropriately updated# and that can taBe a long tiEe in a large database ;lso# the new priEary Bey Eust reEain uniGue TFootnote: 0aEe change is an eFaEple of where our database Eust be able to Eodel the natural world &n this case# we recogniSe that the naEe change can occur regardless of whether it is due to Earriage# or a conseGuence of a religious andLor spiritual con"ersion# or for any other reason U c The challenge of choosing an in"ariant priEary Bey froE the natural data iteEs leads to the concept of generated Beys# also Bnown as surrogate Beys Specifically# we can use surrogate Beys instead of Beys that occur naturally in the database SoEe database professionals belie"e that it is best to use Beys that are uniGuely generated by the database# for eFaEple each row Eay ha"e a priEary Bey that is generated in the seGuence of creation of rows $tuples% There are Eany ad"antages and disad"antages that are often been argued in design sessions The Eain ad"antage is that it gi"es us an in"ariant Bey without any worries about choosing a uniGue priEary Bey The Eain disad"antages of surrogate Beys are
Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.

Chapter 3: The Relational Data Model and Relational Database Constraints

that they do not ha"e a business Eeaning $EaBing soEe aspects of database EanageEent challenging% and that they are slightly less efficient $because they reGuire another pass when inserting a row because the Bey often needs to be returned to the application after a row is inserted% 3.!' - Recent changes in pri"acy laws ha"e disallowed organiSations froE using SS0 to identify indi"iduals unless certain restrictions are satisfied ;s a result# Eost US uni"ersities cannot use SS0s as priEary Beys $eFcept for financial data% &n practice# Student&D# a uniGue &D# a uniGue identifier# assigned to e"ery student# is liBely to be used as the priEary Bey rather than SS0 since Student&D is usable across all aspects of the systeE a SoEe database designers are reluctant to use generated Beys $also Bnown as surrogate Beys% for priEary Beys $such as Student&D% because they are artificial Can you propose any natural choices of Beys that can be used to store the student record in a U0&D6RS&T: databaseH b Suppose that you were able to guarantee uniGueness of a natural Bey that included last naEe ;re you guaranteed that the last naEe will not change during the lifetiEe of the databaseH &f the last naEe last naEe can change# what solutions can you propose for creating a priEary Bey that still includes last naEe but reEains uniGueH c <hat are the ad"antages and disad"antages of using generated $surrogate% BeysH Answer: a 1y Beeping the naEe attributes separated# we allow the possibility of looBing these pieces of their naEe &n a practical use# it is not liBely that the user will Bnow the correct priEary Bey for a gi"en student and so we Eust consider how a user will locate the correct row without this inforEation &f we were to collapse the naEe into a single attribute# then we ha"e coEplicated any sort of PlooBup by naEeQ GueryI such a Guery would then reGuire partial string Eatching and any results Eight not disaEbiguate between First0aEe and 8ast0aEe Therefore# a practical systeE should allow naEe searches by First0aEe and 8ast0aEeI we Eust lea"e Middle&nitial separated still to a"oid aEbiguities froE coEbining these pieces together b ; single attribute 7honeJ would no longer suffice if a student were able to ha"e Eultiple phone nuEbers <e could possibly ha"e Eultiple rows for a single student to allow this to happen# but then we ha"e "iolated Bey principles of database design $e g ha"ing redundant data% ; better solution would be to include the additional attributes CoEe7hone# Cell7hone# and 9ffice7hone and allow the possibility of these attributes to ha"e no "alue ;gain# this is not Eost desirable because Eost students will not ha"e all three of these attributes# and we will ha"e Eany "alueless BeyLattribute pairs ;n eFcellent solution would be add an additional relation 7honeJ $SS0# Type# 0uEber% while reEo"ing 7hone0uEber froE the Student relationship This new relationship would allow the one-to-Eany relationship froE students to phone nuEbers without creating redundant data or wasting space on sparse# "alueless attributes

Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.

You might also like