Chapter 11 Answers

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Chapter 11: Databases: Answers to

coursebook questions and to Worksheet


questions
Syllabus sections covered: 8.1, 8.2 and 8.3.

From the coursebook


Question 11.01
No. If a band had four members the Member table would have a row for each one. Each row would have the
same value for the BandName foreign key attribute.

Question 11.02
No, because each relationship comprises a forward and backward component so if the position of the entities
is reversed the relationship components are changed in accordance with this.

Extension question 11.01


Yes it is possible but it is rarely worthwhile. In this case, the four relationships could be labelled:
 One Band is booked for many Band-Bookings.
 One Band-Booking is for one Band.
 One Band-Booking requires one Booking.
 One Booking is made for one Band-Booking.
The annotation does not provide any clarification. Note, however, that the relationships could be drawn with
minimum and maximum cardinalities defined. This would be useful when a database was being implemented
from the design.

Task 11.01
The conceptual E–R diagram in its simplest form can be shown as:

The only sensible diagram has the Cruise entity at the centre.
There are some obvious arguments for one of the M:M relationships. A cruise visits more than one port and
a port is visited by more than one cruise. For the other relationship the question is whether the database should
have a permanent entity for Passenger. If this is true, then a passenger can go on more than one cruise.

© Cambridge University Press 2019


This is what is shown here. If a passenger list was drawn up for only one cruise then there would be an M:1
relationship for Passenger and Cruise.
The relationships could be annotated, for example, ‘One Passenger goes on many Cruises’, ‘One Cruise takes
many Passengers’.
As a final touch, minimum cardinalities could be included. For Passenger you would argue that to be a passenger
a person must be booked on a cruise, but that might be the only one so the minimum cardinality should be 1.
A cruise with only one passenger would not make sense so the minimum cardinality at the Cruise end of the
Passenger-Cruise relationship must be M. Decisions on the other relationship would depend on whether all
cruises visited more than one port or if a port was used only on one cruise. Minimum M cardinalities seem
sensible.
To convert to a logical E–R diagram each M:M relationship has to be replaced by a link entity with 1:M
relationships with the M ends connected to the link entity.

The key table can now be constructed as follows:

Entity name Primary key Foreign keys


Passenger PassengerID
Cruise-Passenger PassengerID + CruiseID PassengerID, CruiseID
Cruise CruiseID
Cruise-Port CruiseID + PortName CruiseID, PortName
Port PortName

Note that PortName must have unique values; if two ports have the same name they could be modified to
distinguish them so that there is no need to create a new attribute PortID.
Note the compound primary keys containing two individual foreign keys.
There is a subtlety in this scenario. If each cruise is defined to be an individual one on a certain date the above
key table is fine. However, if the database has been set up with Cruise defined as a package that is on offer more
than once, the primary key defined here no longer has unique values. To make the primary key values unique, a
date attribute could be included.

Question 11.03
It would be possible to design a database where a band could be classified permanently as a headlining one.
However, the assumption here is that a band may or may not be headlining a particular booking. This means
that the Headlining attribute must be in the Band-Booking table.

Task 11.02
The list of attributes can be represented as:
(OrderNumber, Date, CustomerNumber, CustomerName, CustomerAddress, SalesRepNumber, SalesRepName,
(ProductNumber, Description, Quantity, Price))

© Cambridge University Press 2019


Some comments are needed here. CustomerAddress and SalesRepName could be split into more than one
attribute; the use of a single attribute is just to keep things simple. Total and Total Price have been ignored.
This is because they are calculated from other data values. A database never stores values that can be calculated;
it has facilities for carrying out calculations when required.
Conversion to 1NF produces two table definitions:
 Order(OrderNumber, Date, CustomerNumber, CustomerName, CustomerAddress, SalesRepNumber,
SalesRepName)
 Order-Product(ProductNumber, OrderNumber(fk), Description, Quantity, Price)
Conversion to 2NF involves only the repeating group table. This produces two tables, the first of which is
a modified form of the repeating group table:
 Order-Product(ProductNumber(fk), OrderNumber(fk), Quantity)
The second is a new table:
 Product(ProductNumber, Description, Price)
Finally, for conversion to 3NF, we need two new tables to give the full normalised set:
 Order(OrderNumber, Date, CustomerNumber(fk), SalesRepNumber(fk))
 Customer(CustomerNumber, CustomerName, CustomerAddress)
 SalesRep(SalesRepNumber, SalesRepName)
 Product(ProductNumber, Description, Price)
 Order-Product(ProductNumber(fk), OrderNumber(fk), Quantity)

Task 11.03
This SQL matches the table designs identified at the end of Worked Example 11.02.
CREATE TABLE Venue(

VenueName varchar(25),

VenueAddress1 varchar(25),

VenueAddress2 varchar(25));

ALTER TABLE Venue ADD PRIMARY KEY (VenueName);

CREATE TABLE Band(

BandName varchar(25),

NumberOfMembers integer);

ALTER TABLE Band ADD PRIMARY KEY (BandName);

© Cambridge University Press 2019


CREATE TABLE Booking(

BookingID varchar(8),

Date date,

VenueName varchar(25));

ALTER TABLE Booking ADD PRIMARY KEY (BookingID);

ALTER TABLE Booking ADD FOREIGN KEY (VenueName REFERENCES,

Venue(VenueName));

CREATE TABLE Band-Booking(

BandName varchar(25),

BookingID varchar(8),

Headlining character);

ALTER TABLE Band-Booking ADD PRIMARY KEY (BandName, BookingID);

ALTER TABLE Band-Booking ADD FOREIGN KEY (BandName REFERENCES Band(BandName));

ALTER TABLE Band-Booking ADD FOREIGN KEY (BookingID REFERENCES,


Booking(BookingID));

Note that the order in which this SQL is presented is important. Before a foreign key constraint is added, the
reference table must have been created. Note that the ALTER command does not have to come immediately after
a table is created.

Exam-style Questions
1 a i 1 mark each for any of the following. (max 2)
Each row of the table is a tuple (1), each column an attribute (1). For each tuple an attribute has a
single value or no value (1). The same value can occur more than once for an attribute unless unique
values have been stipulated (1).
ii 1 mark each for any of the following. (max 4)
Every table must have a primary key (1). The primary key must be a single attribute or a combination
of attributes (1). The primary key must have unique values (1). There is no attribute in this table nor a
suitable combination of attributes that is suitable as a primary key (1). An attribute StudentStudyID
could be added (1) and assigned as a primary key (1).
iii No, there is a repeating group (1). For each student there is more than one value for subject, level and
subject teacher (1).
b i The Student table has a primary key StudentName (1), which is a foreign key in StudentSubject (1).
StudentSubject has a compound primary key StudentName + Subject (1).

© Cambridge University Press 2019


ii 1 mark each for any of the following. (max 2)
The problem is that StudentName might not have unique values (1). Either a StudentID has to be
created as a primary key (1) or names may need extra bits added to ensure unique values (1).
iii Tutor is dependent on TutorGroup (1), which is not the primary key (1).
2 a 1 mark each for any of the following. (max 4)
Dish (1), Venue (1), Client (1) and Staff (1) are straightforward. Catering service (1) might be considered
but Dinner (1) will probably be sufficient.
b Dinner and Client (1), Dinner and staff (1), Dinner and Venue (1), Dinner and Dish (1). Note that there is
often one entity with many relationships.
c The following diagram applies for Dinner and Client and for Dinner and Venue:

(1)
It is M:1 because a dinner is for one Client at one Venue but one Client can book many Dinners and one
Venue can be used for many Dinners. (1)
For each of the remaining relationships the following diagram is correct:

(1)
This is M:M because one Dinner needs many staff and provides many dishes. One staff works at many
Dinners and one Dish can be served at many Dinners. (1)
3 a 3 marks for all non-repeating attributes, 2 marks for all repeating OR 1 mark each for any non-repeating
and any repeating: (BookingNumber, Hotel, HotelAddress, Rating, (Date, RoomType, NumberOfRooms,
RoomRate)).
b 1 mark for two sensible tables, 1 mark for primary keys, 1 mark for other attributes:
Booking(BookingNumber, Hotel, HotelAddress, Rating)
RoomTypebooking(BookingNumber(fk), RoomType, Date, NumberOfRooms, RoomRate).
c 2 marks for keys, 1 mark for other attributes, 2 marks for explanation:
To convert to 2NF only the repeating group data has to be changed. The second table definition changes
to the following two:
RoomTypebooking(BookingNumber(fk), RoomType(fk), Date, NumberOfRooms)
RoomType(RoomType, RoomRate)
In the RoomTypebooking table a composite primary key is needed in which both attributes are foreign
keys. Note that this design assumes that the RoomRate is the same for all rooms of a given type.
You might wish to discuss how the database would be designed if this were not the case.
d HotelAddress and Rating are dependent on Hotel (1), which is not the primary key (1) so the Booking table
is not in 3NF.

4 a CREATE TABLE Student(

StudentID varchar(5),

StudentName varchar(25),

StudentOtherName varchar(25),

© Cambridge University Press 2019


DateOfBirth date);

CREATE TABLE Subject(

SubjectName varchar(25),

SubjectTeacher varchar(25),

);

CREATE TABLE Tutorial(

StudentID varchar(5),

SubjectName varchar(25),

WeekNumber integer,

Day integer,

PeriodNumber integer);

For each table 1 mark each for Create Table, the correct attributes and correct parentheses. (max 5)
b ALTER TABLE Subject ADD PRIMARY KEY (StudentID);

ALTER TABLE Tutorial ADD FOREIGN KEY (StudentID REFERENCES

Student(StudentID));

ALTER TABLE Tutorial ADD FOREIGN KEY (SubjectName REFERENCES

Subject(SubjectName));

1 mark each per ALTER command statement.


c SELECT StudentName

FROM Student

ORDER BY DateOfBirth DESC

One mark each for SELECT StudentName, FROM Student, ORDER BY, DateofBirth, DESC.
Note that the use of DESC puts the largest value for the date first, so that the ordering starts with the
lowest age. Also note that the use of ORDER BY does not imply that the attribute used is output by the
SELECT statement.
d The tutorial table needs a TutorialID as the primary key (1) because the same combination of StudentID
and SubjectName will occur for many different weeks and possibly more than one period. The primary
key must have unique values (1).

© Cambridge University Press 2019


5 This is Question 8 in 9608 Paper 11 June 2016. At the time of writing the published mark scheme is available
on the Cambridge International School Support Hub (requires registration). The Examiners Report for the June
2016 series is also available there and this may contain comments specific to this question.
The following are what the author of this chapter in the Teacher Resource would suggest as reasonable
answers with alternatives suggested where appropriate. Where a suggested answer includes bullet points,
each bullet point would be worth one mark up to the maximum mark allocation for the question.
a i Database management system
ii Detailed answers are not required. It should be noted that there are only two ways that are unique
to a database system:
 The creation of ‘views’ that restrict the access of any individual user to only some of the tables
in the database
 The handling of transactions where a user is altering some of the data. In this case the DBMS
restricts access by other users and only commits the changes when the transaction has completed.
If there is a problem in the execution of the transaction, the database is ‘rolled back’ to the state
that existed before the transaction began
All of the other answers to this question involve activities that would be handled by the DBMS but
similar activities would be performed in file-based systems:
 Encryption of data to ensure the data is unusable by an unauthorised user who does not have
the key.
 Backup of data to allow data restore if there is any problem with the live data.
 Use of usernames and passwords to help prevent unauthorised access.
 Use of access rights to restrict the activities of individual users.
 Monitoring of usage to check which users carried out certain actions.
iii This could be answered generically or by giving specific answers relating to the scenario:
 A query can be used to retrieve data or to update data.
 A query defines criteria to be applied to a data search.
 The school secretary could use a query to check for absences.
 The school secretary could use a query to find the telephone number for the parent of a pupil.
 The school secretary could use a query to record a change of home address for a pupil.
iv The following are statements that could be included in an answer:
 If data is stored in a file there cannot be security measures applying to just part of the file.
 This requires a number of files to be created each suited for a specific use.
 These files may contain the same data; there is data redundancy, which may lead to data
inconsistency if there is editing of only some of the instances of a data item.
 Files have to store data in a format and structure understood by any program that uses the file,
which is described as data dependency.
 If there is a need to change the format or structure the programs using the file have to be updated.
 In a database, data is only stored once so no redundancy or inconsistency and data update is a
simple operation.
 A program using data from the database does not need to know the format or structure of the
data storage, so there is no dependency.
 Database users can have their access to individual data items restricted.

© Cambridge University Press 2019


b i There has to be a foreign key in the CLASS-GROUP table that references the primary key in the CLASS
table. The primary key in CLASS will be ClassID. The same name could be used for the foreign key
in CLASS-GROUP.
ii Many-to-one (M:1 might be accepted as correct)
iii It is sensible to write the query on separate lines. Note that the query is completed with a semi-colon,
the individual lines do not need one.
SELECT StudentID, FirstName

FROM STUDENT

WHERE TutorGroup = "10B"

ORDER BY LastName ASC;

iv This requires an inner join. Depending on which DBMS is used, the keyword INNER JOIN might or might
not be required. The two options are illustrated here.
SELECT STUDENT.LastName

FROM STUDENT, CLASS-GROUP

WHERE ClassID = "CS1"

AND CLASS-GROUP.StudentID = STUDENT.StudentID;

SELECT STUDENT.LastName

FROM STUDENT INNER JOIN CLASS-GROUP

ON CLASS-GROUP.StudentID = STUDENT.StudentID

WHERE ClassID = "CS1"

Cambridge International AS & A Level Computer Science 9608 paper 11 Q8 June 2016

6 This is Question 9 in 9608 Paper 12 November 2016. At the time of writing the published mark scheme is
available on the Cambridge International School Support Hub (requires registration). The Examiners Report
for the November 2016 series is also available there and this may contain comments specific to this question.
The following are what the author of this chapter in the Teacher Resource would suggest as reasonable
answers with alternatives suggested where appropriate. Where a suggested answer includes bullet points,
each bullet point would be worth one mark up to the maximum mark allocation for the question
a The following are the suggested answers to a part of Question 5 above. They are equally applicable here:
 If data is stored in a file there cannot be security measures applying to just part of the file.
 This requires a number of files to be created each suited for a specific use.
 These files may contain the same data; there is data redundancy which may lead to data
inconsistency if there is editing of only some of the instances of a data item.
 Files have to store data in a format and structure that is understood by any program that uses the file
which is described as data dependency.

© Cambridge University Press 2019


 If there is a need to change the format or structure the programs using the file have to be updated.
 In a database data is only stored once so no redundancy or inconsistency and data update
is a simple operation
 A program using data from the database does not need to know the format or structure
of the data storage so no dependency
 Database users can have their access to individual data items restricted.
b Note that only two relationships are required:

c CREATE TABLE CLASS (


ClassID VARCHAR(5),
Description VARCHAR(30),
StartDate DATE,
ClassTime TIME,
NumberOfSessions INT,
AdultsOnly BOOLEAN);
ALTER TABLE CLASS ADD PRIMARY KEY (ClassID);
This illustrates one example of how the ALTER TABLE command can be used. Note that the SQL could have been
written without the commas.
Cambridge International AS & A Level Computer Science 9608 paper 12 Q9 November 2016

Worksheet 11.1: for testing


basic understanding
1 a Not the same. The primary key is an attribute chosen by the designer which must not have repeated
values.
b Not the same. A foreign key in a table will relate to a primary key in another table.
c Not the same. Tuple is the formal name for a row in a table.
d The same. Tuple is the formal, correct term but row is usually used.
e Not the same. When the term ‘attribute’ is used it really means ‘attribute name’, which is in effect
a column heading for a table. The attribute value is what is stored for that attribute in a specific row.
f Effectively the same. An instance could be called an example of the entity or table. Each instance will
have a collection of data values for the row of the table corresponding to that instance.

© Cambridge University Press 2019


2 A True, B True, C False, D False, E True, F False
It is important to understand the overriding rationale of a relational database. The relational table represents
an entity, and each instance of an entity has a specific set of data values for the attributes defined for the
table. This specific set of values can be referenced by the unique primary key value for the instance. One or
more of the attributes may refer to further relevant data stored in another table. In this case, the attribute
will be a foreign key referencing the primary key of the other table. A unique set of values is only a
requirement for the primary key in a table. To achieve unique values, an extra attribute may have to be
defined or a combination of attributes may have to be used.
3 a

c
Table name Primary key Foreign key
Chef ChefID
Cake CakeName ChefID
CakeName
Cake-Ingredient CakeName+IngredientName
IngredientName
Ingredient IngredientName
This assumes that each cake has an individual name and the same applies for ingredient names.
It allows for two chefs to have the same name.
4

With a scenario like this there is often an obvious choice for an entity to be placed first at the centre of the
diagram. This entity will have more than one connected entity and will have attribute values that will change
on a regular basis. Coach trip fits this profile. A special point to note is that the coach will not always have the
same driver so there is no direct relationship between coach and driver. Finally, note that only Destination is
involved in a M:M relationship.
5 Table name Primary key Foreign key
Film FilmID
Cast-member FilmID&ActorName FilmID, ActorName
Actor ActorName
It is worth noting that the entity-relationship diagram given in the question has a link entity because the
relationship between Actor and Film would be M:M. If there is a request to draw such a diagram, the crow’s
feet must be in the correct positions as shown in the question’s diagram. In order to answer the question, it is
important to remember that the foreign key goes at the M end of a relationship so that Cast-member must
have two foreign keys. The combination of these creates a sensible primary key so it is not necessary to
create a separate primary key. Finally, because a primary key must have unique values, FilmName is not
sensible as a primary key because films are often re-made with the same title. The assumption here is that all
film actors have unique names, which may not be true.

© Cambridge University Press 2019


6 WardRoster (WName, Location, WType,
(NurseName, TeamCode,TeamSkill, Shift))
The list has been presented in the form of a table design with a suitable name for the table. The repeating
group attributes have been identified by surrounding them with a pair of brackets.
The name of the hospital has not been included as an attribute. This is sensible if the database is used for just
this hospital. If the database were to be used for a number of hospitals then the hospital name would need to
be an attribute.
7 A True, B True, C False, D False, E True, F True
Note that it is important to be prepared for different styles of question relating to normalisation. One type
would concern a normalisation starting with a list of attributes and then following successive conversion
through 1NF to 2NF and finally to 3NF. In this case the conversion to 2NF only concerns the data with a
compound primary key. Another type might show some table definitions and require comments about which
normal form they are consistent with.
8 A suitable design would be:

a SELECT OrderID FROM Order WHERE CustomerID = 365 AND OrderDate > 2016-
01-01
b SELECT ProductID, Quantity
FROM Order-Product, Order
WHERE Order.CustomerID = 365
AND Order.OrderID = Order-Product.OrderID
AND OrderDate = 2016-07-09
9 A 5, B 1, C 8, D 7, E 3, F 9, G 6, H 2, I 4
It is important to remember that the DDL must come before the DML. A table cannot be altered until it has
been created. The ALTER TABLE commands could be switched round. Inserting data into a table is the first
stage of DML. The query command would only be used once the database was created and the data inserted.

© Cambridge University Press 2019


Worksheet 11.2: more challenging questions
1 a

b The following shows some examples:

There are no optional relationships here.

© Cambridge University Press 2019


d

Note that this assumes that the materials needed for a task do not depend on which contract they are to
be used on. However, the operatives performing the tasks will be chosen for the task on a specific contact
and will not always be the same for different contracts.
e
Table Primary key Foreign key
Customer CustomerID
Contract ContractID
ContractID
Contract-Task ContractID+TaskID
TaskID
Task TaskID
TaskID
Task-Material TaskID+MaterialID
MaterialID
Material MaterialID
ContractID+TaskID
Contract-Task-Operative ContractID+TaskID+OperativeID
OperativeID
Operative OperativeID

2 The suitable definitions for first normal form are:


 Ward ( WName, Location, WType)
 Ward-Nurse ( WName(fk), NurseID, NurseName, TeamCode, TeamSkill, Shift)
The suitable definitions for second normal form are:
 Ward ( WName, Location, WType)
 Ward-Nurse ( WName(fk), NurseID(fk), Shift)
 Nurse ( NurseID, NurseName, TeamCode, TeamSkill)
The suitable definitions for third normal form are:
 Ward ( WName, Location, WType)
 Ward-Nurse ( WName(fk), NurseID(fk), Shift)
 Nurse ( NurseID, NurseName, TeamCode(fk))
 Team ( TeamCode, TeamSkill)

© Cambridge University Press 2019

You might also like