Answers

advertisement

CS530

Winter 2002 (Answers)

Question 1 a) Discuss the following terms from Extended-Entity-Relationship (EER) modelling. Show how each of the elements you describe is drawn in an EER model. i. Entity types . What is an entity type? What is the difference between a strong entity type and a weak entity type?

[1 marks]

( knowledge and understanding )

Group of “objects” (real or abstract) with the same properties and having an independent existence. Strong = not dependent on some other entity type for its existence. Weak = dependent on some other entity type for its existence. ii. Relationship type . What is a relationship type? What is the degree of a relationship type? What is a recursive relationship?

[2 marks]

( knowledge and understanding )

A set of meaningful associations among entity types. Degree = number of participating entity types. Recursive = same entity participates more than once in different roles. iii. Attributes . What is an attribute? What is an attribute domain? What is a derived attribute? What is a discriminator attribute? What is the difference between simple attributes and composite attributes and the difference between single-valued attributes and multi-valued attributes?

[3 marks]

( knowledge and understanding )

Property of an entity or relationship type. Domain is set of allowable values. Simple has single independent component, composite made up of multiple independent components. Derived is derivable from value(s) of related attribute(s), not necessarily of same entity. Discriminator is used to distinguish instances of a weak entity type (part of composite primary key). Single-valued has only one value for each instance of entity type, multi-valued may have more than one. iv. Keys . What is the difference between a candidate key, a primary key and a composite key? What considerations might influence the choice of a primary key?

(

[4 marks] knowledge and understanding )

Candidate is minimal set of attributes that uniquely identifies instances of entity. Primary is selected candidate key. Composite is candidate consisting of more than one attribute. b) Figure 1 shows a fragment of an Extended-Entity-Relationship (EER) model describing operations (surgical procedures) in a hospital database. name

Procedure date name name

Patient operation Nurse patient_id nurse_id

Doctor name doctor_id

Figure 1 i. Draw a new entity relationship model in which the operation relation is replaced by a weak entity and which expresses the additional constraints that an operation consists of exactly one surgical procedure being performed on exactly one patient by one or more doctors and one or more nurses. It should also be assumed that a particular procedure can be performed on a particular patient at most once in a single day, but many different procedures could be performed on the same patient in a single day.

[7 marks]

( knowledge and understanding plus intellectual skills ) ii. A Doctor may be either a Consultant or a Registrar, but not both, and there are some Doctors who are neither Consultants nor Registrars. Show how the entity relationship can be extended to express this additional information.

[3 marks]

( knowledge and understanding plus intellectual skills )

name

Patient

1 patient_id name

Procedure

1 on date perform m m operation m m by with name n

Nurse nurse_id n

Doctor name doctor_id d

Consultant Registrar

Figure 1 (ANSWER)

Question 2 a) Figure 2 shows a fragment of an Extended-Entity-Relationship (EER) model describing products and their distribution.

price description

Product m has-part code supplier-address n

Part weight stock account-id cust-code contact address supplier-name id

Figure 2 i. Map the EER model to a relational schema without performing any normalisation . Clearly indicate all primary keys and foreign keys (also stating into which relations they are keys.

[7 marks]

( knowledge and understanding plus intellectual skills )

Distributor (account-id, contact, address)

Stock (account-id, code, cust-code) account-id FK into Distributor, code FK into Product

Product (code, price, description)

Has-part (code, id) code FK into Product, id FK into Part

Part (id, weight, supplier-name, supplier-address) ii. Given that suppliers have a unique name and exactly one address, say why the Part relation is not in Boyce-Codd normal form (BCNF).

[2 marks]

( knowledge and understanding plus intellectual skills )

Because supplier-address is functionally dependent on suppliername, which is not a candidate key. iii. Show how the Part relation can be decomposed into 2 relations that are both in BCNF.

[2 marks]

( knowledge and understanding plus intellectual skills )

Part (id, weight, supplier-name) supplier-name FK into Supplier

Supplier (supplier-name, supplier-address) iv. Show how the EER model could be modified to directly represent the normalised relations.

[3 marks]

( knowledge and understanding plus intellectual skills ) weight

Part n id has-part supplier-address

1

Supplier supplier-name

Add Supplier entity with name and address attributes related to

Part by 1:m relationship. b) Consider the following relational schema for a database describing accommodation and activities available in national parks:

Park (name, region)

Hotel (id, name, tname)

Town (name, pname)

Activity (pname, activity) i. Write an SQL to create a view called “Climbing” that contains the id, name and tname of all Hotels situated in a park where “climbing” is an available activity. Is this view updatable, and why?

[3 marks]

( knowledge and understanding plus intellectual skills ) tname is a foreign key into Town pname is a foreign key into Park pname is a foreign key into Park

CREATE VIEW Climbing AS

(SELECT h.id, h.name, h.tname

FROM Park p, Hotel h, Town t, Activity a

WHERE h.tname=t.name and t.pname=p.name

and a.pname=p.name and a.activity=`climbing’); ii. Write an SQL query that lists the name of each Town, with the name of the Park it is in and the number of hotels it contains. The list should be ordered first by Park name and then by Town name in ascending alphabetical order.

[3 marks]

( knowledge and understanding plus intellectual skills )

SELECT p.name, t.name, COUNT(*) as hotels

FROM Park p, Hotel h, Town t

WHERE h.tname=t.name and t.pname=p.name

GROUP BY t.name

ORDER BY p.name, t.name;

Question 3

(a) Which of the restrictions below are enforced, and which are not enforced, by the following schema:

[6 marks]

( knowledge and understanding )

CREATE TABLE Stores( storeID INT CHECK(storeID IN (SELECT store FROM Sales)), location CHAR(30),

PRIMARY KEY (storeID) );

CREATE TABLE Items( name CHAR(30) PRIMARY KEY, price REAL CHECK(price >= 0.0) );

CREATE TABLE Sales( store INT, item CHAR(30) REFERENCES Items(name), totalSales REAL,

PRIMARY KEY (store, item) );

CREATE ASSERTION BigSales CHECK(NOT EXISTS(

SELECT * FROM Stores, Sales WHERE Stores.location =

'M21'

AND Stores.storeID = Sales.store

AND Sales.totalSales != 1000000) );

(i) No item has a price of 0.

(ii) Every storeID that appears in the Stores relation must also appear as a store value in the relation sales.

(iii) Every store has total sales of at least 1,000,000 pounds.

(iv) None of the above; i.e., each could be false in some database that satisfies the constraints.

(i) is not enforced; the CHECK only requires the price be nonnegative, which allows 0. (2 marks)

(ii) is not enforced. Remember that attribute-based checks cannot enforce a foreign-key constraint, such as (b), because they are not triggered on a deletion.

Yet a deletion can cause a foreign-key violation. (2 marks)

(iii) is not enforced. The assertion only requires that stores in Chorlton (M21) have sales of at least a million. (2 marks)

(iv) is enforced.

(b) Which of the restrictions below are enforced, and which are not enforced, by the schema above (explain why):

[6 marks]

( knowledge and understanding )

(i) Every name appearing in relation Items also appears in the item column of

Sales.

(ii) Every item appearing in Sales also appears in the name column of Items.

(iii) No two tuples (i.e., rows) of Sales can agree (i.e., have the same value) on either store or item.

(iv) None of the above; i.e., each could be false in some database that satisfies the constraints.

(i) is not enforced, since it is the opposite of the foreign-key constraint. (2 marks)

(ii) is enforced, since this constraint is exactly the foreign-key constraint in Sales.

(2 marks)

(iii) is not enforced, since the primary key is on both attributes. (2 marks)

(c) Suppose we have the following table declarations:

CREATE TABLE Empl (ECode INT PRIMARY KEY);

CREATE TABLE Manager (MCode INT PRIMARY KEY

REFERENCES Empl(ECode) ON DELETE SET NULL;

CREATE TABLE PartTime (PRef INT REFERENCES Empl(ECode));

CREATE TABLE Boss

(MCode INT REFERENCES Manager(MCode) ON DELETE SET

NULL,

ECode INT REFERENCES Empl(ECode) ON UPDATE CASCADE);

Consider the following scripts:

I. DELETE ; DELETE FROM

Manager

; DELETE

FROM

Empl

; DELETE FROM

Boss

;

II. DELETE

PartTime

; DELETE FROM

Boss

; DELETE FROM

Empl

; DELETE FROM B

Manager

III. DELETE FROM

Manager

; DELETE FROM

PartTime

; DELETE

FROM

Boss

; DELETE FROM

Empl

;

Which of the above scripts will empty all four tables, without error (explain why):

(i) III only, (ii) I only, (iii) II and III only, (iv) I and III only.

[8 marks]

( knowledge and understanding )

The answer is (iii). (2 marks)

The foreign-key constraint from

Manager to

Empl doesn't cause problems if we delete from

Empl first, since there is a clause that tells how to handle dangling tuples in

Manager

. (1 mark)

However, the foreign-key constraint from

PartTime to

Empl is a problem, since the default policy is to reject a deletion from

Empl that causes a dangling tuple in

PartTime

. (1 mark)

Thus, in a valid sequence,

PartTime must precede

Empl

. (1 mark)

The foreign-key constraint from

Boss

to

Manager is not a problem, but the one from

Boss

to

Empl is, again because the default policy will cause a rejection of certain deletions from

Empl

. (1 mark)

Thus,

Boss

must also come before

Empl

. (1 mark)

Of the three sequences, only I has

PartTime or

Boss

before

Empl

, so II and

III are OK; I is not. (1 mark)

Question 4

Consider the following relational schema:

LECTURER(LCode, FirstName, LastName)

STUDENT(SCode, FirstName, LastName)

TOPIC(TCode, Description)

LECTURE(TCode, Date, LCode, StudNumber)

Tcode FK into

TOPIC

LCode FK into

LECTURER

(

StudNumber

is the number of students attending the lecture)

ASSESSMENT(Tcode, Date, Scode, Mark)

Tcode, Date FK into

LECTURE

Scode FK into

STUDENT

(Assessments are done during lectures)

Write the SQL expressions for the following queries:

(a) Select the code number, the first name and the last name of the students who where never assessed on a topic with a description “Logic”.

[4 marks]

( knowledge and understanding plus intellectual skill )

(b) Select the code number, of the lecturer who gave lectures on all the topics with description “Logic”.

[4 marks]

( knowledge and understanding plus intellectual skill )

(c) Select the code number of the lecturer who made continuous assessments, i.e. who made at least an assessment for each of her/his lectures.

[4 marks]

( knowledge and understanding plus intellectual skill )

(d) Select, for each topic, the average of the marks assessed on the topic, considering only the students who where assessed at least three times on the topic.

[4 marks]

( knowledge and understanding plus intellectual skill )

(e) Select, for each student, the code number of the lecturer with whom she/he got the greatest number of assessments.

[4 marks]

( knowledge and understanding plus intellectual skill )

(a)

SELECT *

FROM STUDENT

WHERE NOT EXISTS

( SELECT *

FROM ASSESSMENT, TOPIC

WHERE STUDENT.Scode=ASSESSMENT.Scode

AND TOPIC.Tcode=ASSESSMENT.Tcode

AND TOPIC.Description=’Logic’)

(b)

SELECT DISTINCT LCode

FROM LECTURE X

WHERE NOT EXISTS

( SELECT *

FROM TOPIC

WHERE Description=’Logic’

AND NOT EXISTS

( SELECT *

FROM LECTURE Y

WHERE X.LCode=Y.LCode

AND TOPIC.Tcode=Y.Tcode))

(c)

SELECT LX.LCode

FROM LECTURE LX

WHERE NOT EXISTS

( SELECT *

FROM LECTURE LY

WHERE LX.LCode=LY.LCode

AND NOT EXISTS

( SELECT *

FROM ASSESSMENT A

WHERE LY.Date=A.Date

AND LY.Tcode=A.Tcode))

(d)

SELECT X.Tcode, AVG(X.Mark)

FROM ASSESSMENT X

WHERE 3 <= ( SELECT COUNT(*)

FROM ASSESSMENT Y

WHERE Y.Tcode=X.Tcode

AND Y.Scode=X.Scode)

(e)

SELECT AX.Scode, LECTURE.LCode

FROM LECTURE, ASSESSMENT AX

WHERE AX.Tcode=LECTURE.Tcode

AND AX.Date=LECTURE.Date

GROUP BY AX.Scode, LECTURE.LCode

HAVING COUNT(*) >= ALL ( SELECT COUNT(*)

FROM LECTURE L, ASSESSMENT A

WHERE L.Tcode=A.Tcode

AND L.Date=A.Date

AND AX.Scode=A.Scode

GROUP BY A.Scode, L.LCode)

Download