Chapter 5: The Relational Data Model and Relational Database Constraints
1
CHAPTER 5: THE RELATIONAL DATA MODEL AND RELATIONAL DATABASE
CONSTRAINTS
Answers to Selected Exercises
5.11 – Suppose each of the following Update operations is applied directly to the database of
Figure 3.6. Discuss all integrity constraints violated by each operation, if any, and the
different ways of enforcing these constraints:
(a) Insert < ‘Robert’, ‘F’, ‘Scott’, ‘943775543′, ’21-JUN-42′, ‘2365 Newcastle Rd,
Bellaire, TX’, M, 58000, ‘888665555′, 1 > into EMPLOYEE.
(b) Insert < ‘ProductA’, 4, ‘Bellaire’, 2 > into PROJECT.
(c) Insert < ‘Production’, 4, ‘943775543′, ’01-OCT-88′ > into DEPARTMENT.
(d) Insert < ‘677678989’, null, ‘40.0’ > into WORKS_ON.
(e) Insert < ‘453453453’, ‘John’, M, ’12-DEC-60′, ‘SPOUSE’ > into DEPENDENT.
(f) Delete the WORKS_ON tuples with ESSN= ‘333445555′.
(g) Delete the EMPLOYEE tuple with SSN= ‘987654321′.
(h) Delete the PROJECT tuple with PNAME= ‘ProductX’.
(i) Modify the MGRSSN and MGRSTARTDATE of the DEPARTMENT tuple with
DNUMBER=5 to ‘123456789′ and ’01-OCT-88′, respectively.
(j) Modify the SUPERSSN attribute of the EMPLOYEE tuple with SSN= ‘999887777’ to
‘943775543‘.
(k) Modify the HOURS attribute of the WORKS_ON tuple with ESSN= ‘999887777′ and
PNO= 10 to ‘5.0’.
Answers:
(a) No constraint violations.
(b) Violates referential integrity because DNUM=2 and there is no tuple in the
DEPARTMENT relation with DNUMBER=2. We may enforce the constraint by: (i) rejecting
(c) Violates both the key constraint and referential integrity. Violates the key constraint
because there already exists a DEPARTMENT tuple with DNUMBER=4. We may enforce
this constraint by: (i) rejecting the insertion, or (ii) changing the value of DNUMBER in the
new DEPARTMENT tuple to a value that does not violate the key constraint. Violates
Chapter 5: The Relational Data Model and Relational Database Constraints
2
(d) Violates both the entity integrity and referential integrity. Violates entity integrity because
PNO, which is part of the primary key of WORKS_ON, is null. We may enforce this constraint
by: (i) rejecting the insertion, or (ii) changing the value of PNO in the new WORKS_ON tuple
(e) No constraint violations.
(f) No constraint violations.
(g) Violates referential integrity because several tuples exist in the WORKS_ON,
DEPENDENT, DEPARTMENT, and EMPLOYEE relations that reference the tuple being
(h) Violates referential integrity because two tuples exist in the WORKS_ON relations that
reference the tuple being deleted from PROJECT. We may enforce the constraint by: (i)
(i) No constraint violations.
(j) Violates referential integrity because the new value of SUPERSSN=’943775543‘ and there
(k) No constraint violations.
5.12 – Consider the AIRLINE relational database schema shown in Figure 3.8, which
describes a database for airline flight information. Each FLIGHT is identified by a flight
NUMBER, and consists of one or more FLIGHT_LEGs with LEG_NUMBERs 1, 2, 3, etc.
Each leg has scheduled arrival and departure times and airports, and has many
LEG_INSTANCEsone for each DATE on which the flight travels. FARES are kept for each
flight. For each leg instance, SEAT_RESERVATIONs are kept, as is the AIRPLANE used in
the leg, and the actual arrival and departure times and airports. An AIRPLANE is identified
by an AIRPLANE_ID, and is of a particular AIRPLANE_TYPE. CAN_LAND relates
AIRPLANE_TYPEs to the AIRPORTs in which they can land. An AIRPORT is identified by
an AIRPORT_CODE. Consider an update for the AIRLINE database to enter a reservation
on a particular flight or flight leg on a given date.
(a) Give the operations for this update.
(b) What types of constraints would you expect to check?
(c) Which of these constraints are key, entity integrity, and referential integrity constraints
and which are not?
Chapter 5: The Relational Data Model and Relational Database Constraints
3
(d) Specify all the referential integrity constraints on Figure 3.8.
Answers:
(a) One possible set of operations for the following update is the following:
INSERT <FNO,LNO,DT,SEAT_NO,CUST_NAME,CUST_PHONE> into
operations should be repeated for each LEG of the flight on which a reservation is made.
This assumes that the reservation has only one seat. More complex operations will be
needed for a more realistic reservation that may reserve several seats at once.
(b) We would check that NUMBER_OF_AVAILABLE_SEATS on each LEG_INSTANCE of
(c) The INSERT operation into SEAT_RESERVATION will check all the key, entity integrity,
and referential integrity constraints for the relation. The check that
(d) We will write a referential integrity constraint as R.A > S (or R.(X) > T) whenever
attribute A (or the set of attributes X) of relation R form a foreign key that references the
primary key of relation S (or T). FLIGHT_LEG.FLIGHT_NUMBER > FLIGHT
FLIGHT_LEG.DEPARTURE_AIRPORT_CODE > AIRPORT
FLIGHT_LEG.ARRIVAL_AIRPORT_CODE > AIRPORT
LEG_INSTANCE.(FLIGHT_NUMBER,LEG_NUMBER) > FLIGHT_LEG
LEG_INSTANCE.DEPARTURE_AIRPORT_CODE > AIRPORT
5.13 – Consider the relation CLASS(Course#, Univ_Section#, InstructorName, Semester,
BuildingCode, Room#, TimePeriod, Weekdays, CreditHours). This represents classes taught
in a university with unique Univ_Section#. Give what you think should be various candidate
keys and write in your own words under what constraints each candidate key would be valid.
Answer:
Possible candidate keys include the following (Note: We assume that the values of the
Semester attribute include the year; for example “Spring/94″ or “Fall/93″ could be values for
Semester):
2. {Univ_Section#} if it is unique across all semesters.
4. If Univ_Section# is not unique, which is the case in many universities, we have to
examine the rules that the university uses for section numbering. For example, if the
Chapter 5: The Relational Data Model and Relational Database Constraints
4
sections of a particular course during a particular semester are numbered 1, 2, 3, …then
5.14 – Consider the following six relations for an order-processing database application in a
company:
CUSTOMER (Cust#, Cname, City)
ORDER (Order#, Odate, Cust#, Ord_Amt)
ORDER_ITEM (Order#, Item#, Qty)
ITEM (Item#, Unit_price)
SHIPMENT (Order#, Warehouse#, Ship_date)
WAREHOUSE (Warehouse#, City)
Here, Ord_Amt refers to total dollar amount of an order; Odate is the date the order was
placed; Ship_date is the date an order (or part of an order) is shipped from the warehouse.
Assume that an order can be shipped from several warehouses. Specify the foreign keys for
this schema, stating any assumptions you make. What other constraints can you think of for
this database?
Answer:
Strictly speaking, a foreign key is a set of attributes, but when that set contains only one
attribute, then that attribute itself is often informally called a foreign key. The schema of this
question has the following five foreign keys:
1. the attribute Cust# of relation ORDER that references relation CUSTOMER,
3. the attribute Item# of relation ORDER_ITEM that references relation ITEM,
5. the attribute Warehouse# of relation SHIPMENT that references relation WAREHOUSE.
We now give the queries in relational algebra:
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
5.15 – Consider the following relations for a database that keeps track of business trips of
salespersons in a sales office:
SALESPERSON (SSN, Name, Start_Year, Dept_No)
TRIP (SSN, From_City, To_City, Departure_Date, Return_Date, Trip_ID)
EXPENSE (Trip_ID, Account#, Amount)
Specify the foreign keys for this schema, stating any assumptions you make.
Answer:
The schema of this question has the following two foreign keys:
1. the attribute SSN of relation TRIP that references relation SALESPERSON, and
2. the attribute Trip_ID of relation EXPENSE that references relation TRIP.
5.16 – Consider the following relations for a database that keeps track of student enrollment
in courses and the books adopted for each course:
STUDENT (SSN, Name, Major, Bdate)
COURSE (Course#, Quarter, Grade)
Specify the foreign keys for this schema, stating any assumptions you make.
Chapter 5: The Relational Data Model and Relational Database Constraints
6
Answer:
The schema of this question has the following four foreign keys:
4. the attribute Course# in relation ENROLL that references relation COURSE,
6. the attribute Book_ISBN of relation BOOK_ADOPTION that references relation TEXT.
We now give the queries in relational algebra:
5.18 – Database design often involves decisions about the storage of attributes. For example
a Social Security Number can be stored as a one attribute or split into three attributes (one
for each of the three hyphen-deliniated groups of numbers in a Social Security Number
XXX-XX-XXXX). However, Social Security Number is usually stored in one attribute. The
decision is usually based on how the database will be used. This exercise asks you to think
about specific situations where dividing the SSN is useful.
Answer:
a. We need the area code (also know as city code in some countries) and perhaps the
country code (for dialing international phone numbers).
b. I would recommend storing the numbers in a separate attribute as they have their own
independent existence. For example, if an area code region were split into two regions, it
Copyright © 2016 Pearson Education, Inc., Hoboken NJ
5.19 – Consider a STUDENT relation in a UNIVERSITY database with the following attributes
(Name, SSN, Local_phone, Address, Cell_phone, Age, GPA). Note that the cell phone may
be from a different city and state (or province) from the local phone. A possible tuple of the
relation is shown below:
Name
SSN
LocalPhone
CellPhone
Age
GPA
George Shaw
William Edwards
12345
6789
555-1234
555-4321
19
3.75
a. Identify the critical missing information from the LocalPhone and CellPhone attributes as
shown in the example above. (Hint: How do call someone who lives in a different state or
province?)
b. Would you store this additional information in the LocalPhone and CellPhone attributes or
add new attributes to the schema for STUDENT?
c. Consider the Name attribute. What are the advantages and disadvantages of splitting this
field from one attribute into three attributes (first name, middle name, and last name)?
d. What general guideline would you recommend for deciding when to store information in a
single attribute and when to split the information?
Answer:
a. A combination of first name, last name, and home phone may address the issue assuming
that there are no two students with identical names sharing a home phone line. It also
assumes that every student has a home phone number. Another solution may be to use first
b. If we use name in a primary key and the name changes then the primary key changes.
Changing the primary key is acceptable but can be inefficient as any references to this key in
the database need to be appropriately updated, and that can take a long time in a large
c. The challenge of choosing an invariant primary key from the natural data items leads to
the concept of generated keys, also known as surrogate keys. Specifically, we can use
surrogate keys instead of keys that occur naturally in the database. Some database
professionals believe that it is best to use keys that are uniquely generated by the database,