0% found this document useful (0 votes)
30 views

2011 2012 Normalization SQL Sol

The document discusses a database design problem and provides a solution. It describes a transport company that manages trucks and bases. The current database design has issues like redundancy and multi-valued attributes. The question asks to normalize the database into third normal form and provide dependency diagrams of the new tables.

Uploaded by

diristi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

2011 2012 Normalization SQL Sol

The document discusses a database design problem and provides a solution. It describes a transport company that manages trucks and bases. The current database design has issues like redundancy and multi-valued attributes. The question asks to normalize the database into third normal form and provide dependency diagrams of the new tables.

Uploaded by

diristi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Semester 1, 2012 Final Exam

Questions on Normalization and SQL with Solutions

Question 22 – Normalization (18 marks)

Use the following description and the three tables to answer parts (A) and (B) of this question.

A transport company provides a number of trucks for cartage purposes. These trucks are hired by
people for a number of different reasons, for example, for carting household items when moving from
one house to another.

The trucks operate from a base, which is managed by at least one employee of the company. A truck
may operate from one or multiple bases over a period of time. A base will have at least one truck at any
given time.

The company hired a database designer to create a MS Access database that could be used by the
company to store details about the trucks. The implemented MS Access database contains three tables,
which are provided together with the relational schema in Figure 1.

Base
Base City State Area Code Phone Manager
501 Murfreesboro TN 615 123-4567 or 123-4577 Andrea D. Gallager
502 Lexington KY 568 234-5678 or 234-5699 Andrea D. Gallager
and
Maria J. Talindo
503 Cape Girardeau MO 456 345-6789 Maria J. Talindo
504 Dalton GA 901 456-7890 Peter F. McAvee

Truck
Truck # Base Base City Base Phone Truck Miles Serial Num
Type
1001 501 Murfreesboro 123-4567 or 123- 1 32123.5 AA-322-12212-W11
4577
1002 501 Murfreesboro 123-4567 or 123- 1 76984.3 AC-342-22134-Q23
4577
1002 502 Lexington 234-5678 or 234- 1 76984.3 AC-342-22134-Q23
5699
1003 501 Murfreesboro 123-4567 or 123- 2 12346.6 AC-445-78656-Z99
4577
1003 503 Cape Girardeau 345-6789 2 12346.6 AC-445-78656-Z99
1005 503 Cape Girardeau 345-6789 2 45673.1 FR-998-32245-W12
1006 501 Murfreesboro 123-4567 or 123- 2 93245.7 AD-456-00845-R45
4577
1006 503 Cape Girardeau 345-6789 2 93245.7 AD-456-00845-R45

1
Truck Type
Type Description
1 Single box, double-axle
2 Single box, single-axle
3 Tandem trailer, single-axle

Figure 1

(A) Discuss some problems with the design of above database and provide examples of the
problems that you have identified. (4 marks).

Ans:

1. Redundancy:
a. The truck details are repeating the truck table because a truck can operate
from multiple bases.
b. The base details are repeating in base and truck tables.
2. Multi-valued attributes: Base phone and Base Manager are both mult-values attributes
3. Attribute Naming: The names of the attributes do not follow a particular format.

2
(B) The current database has three tables. Redesign, that is normalize, the database so that all the
tables are in 3NF and create the dependency diagrams for all the tables in your re-designed
database. Use the primary keys identified in the relational schema for the normalization
process. (14 marks)

Employee (Emp_Num, Emp_FName, Emp_LName)

Base (Base_Num, Base_City, Base_State, Base_Areacode)

Base_Phone (Base_Num, Base_Phone_Num)

Base_Manager (Base_Num, Emp_Num)

Truck_Type (Type_ID, Type_Desc)

Truck (Truck_Num, Truck_Serial, Truck_Miles, Type_ID)

Base_Assign (Truck_Num, Base_Num)

3
Question 24 – Structured Query Language (17 marks)

Use the following description and the ERD given below (see Figure 2) to answer parts (A) to (E) of this
question. Write SQL statements to answer each part and your SQL statement should be written for a
MySQL database.

You are given a database that is used by a video library to store data about movies, copies of each
movie, borrowers, and renting of the movies. A movie can have one or more copies. Each copy can be
rented by one of more borrowers.

Figure 2

4
(A) List the details of all borrowers, sorted by their last name in increasing order. (2
marks)

select *

from borrower

order by brw_lname;

(B) List the number and names of all borrowers that have not borrowed any movies. (3
marks)

select b.brw_num,

concat_ws(" ", brw_fname, brw_lname) as 'Name'

from borrower as b

where b.brw_num not in(select distinct(brw_num) from

rental);

(C) List the code(s), name(s) and the price of the most expensive movie(s) [Hint: use the
movie_charge column]. (3 marks)

select movie_code, movie_name, movie_charge

from movie

where movie_charge in

(select max(movie_charge) from movie);

5
(D) List the movie code, name and the number of copies of each movie that has more than two
copies. (4 marks)

select m.movie_code, movie_name, count(copy_code) as 'copies'

from movie as m, copy as c

where m.movie_code = c.movie_code

group by m.movie_code, movie_name

having count(copy_code) > 2;

select m.movie_code, movie_name, count(copy_code) as 'copies'

from movie as m, copy as c

where m.movie_code = c.movie_code

group by m.movie_code, movie_name

having copies > 2;

select movie_code, movie_name, count(copy_code) as 'copies'

from movie natural join copy

group by movie_code, movie_name

having count(copy_code) > 2;

(E) List the movie code and name of all movies where at least one copy of the movie has never
been borrowed. (5 marks)

select movie_code, movie_name

from movie

where movie_code in

(select distinct movie_code from copy where copy_code not in

(select distinct copy_code from rent_line));

6
SELECT distinct movie_code,movie_name

FROM movie NATURAL JOIN copy

WHERE copy_code NOT IN

(SELECT distinct(copy_code) FROM rent_line);

7
Semester 1, 2011Final Exam

Questions on Normalization and SQL with Solutions

Question 22 – Normalization (12 marks)

Use the following description and the two tables to answer parts (A) to (C) of this question.

A small software development firm uses the following two tables to store details about the software
development projects the firm undertakes, the employees who worked on the projects, and the project
manager. For each employee that worked on the project, the employee’s details, the dates when the
employee worked on the project, and the total hours, hourly rate and total payment is recorded for
each day the employee worked on a particular project. For the project manager, only the manager’s
employee number is recorded as the Manager #, and the manager’s name is not recorded.

The employee details are stored in a separate table. The two tables are shown below. The first table is
the Project table, while the second one is the Employee table.

Project
Project Number Title Emp Name Work Date Hours Hourly Total Manager
# Worked Rate Payment #
15 Evergreen 103 June 04-Mar-04 2.60 $84.50 $219.70 105
Arbough
15 Evergreen 101 John News 05-Mar-04 3.60 $105.00 $378.00 105
15 Evergreen 103 June 05-Mar-04 1.90 $84.50 $160.55 105
Arbough
15 Evergreen 105 Alice 05-Mar-04 2.00 $105.00 $210.00 105
Johnson
15 Evergreen 102 David Senior 06-Mar-04 3.80 $96.75 $367.65 105
15 Evergreen 101 John News 06-Mar-04 2.30 $105.00 $241.50 105
15 Evergreen 103 June 06-Mar-04 3.00 $84.50 $253.50 105
Arbough
22 Rolling 113 Delbert 05-Mar-04 2.50 $48.10 $120.25 113
Tide Joenbrood
22 Rolling 105 Alice 05-Mar-04 5.20 $105.00 $546.00 113
Tide Johnson
22 Rolling 104 Anne 06-Mar-04 2.60 $96.75 $251.55 113
Tide Ramoras
22 Rolling 111 Geoff 06-Mar-04 4.00 $26.87 $107.48 113
Tide Wabash
22 Rolling 105 Alice 07-Mar-04 2.70 $105.00 $283.50 113
Tide Johnson
22 Rolling 106 William 07-Mar-04 2.40 $35.75 $85.80 113
Tide Smithfield
25 Starflight 101 John News 05-Mar-04 1.70 $105.00 $178.50 101
25 Starflight 114 Annelise 06-Mar-04 1.80 $48.10 $86.58 101
Jones
25 Starflight 114 Annelise 07-Mar-04 3.40 $48.10 $163.54 101
Jones

8
Employee
Emp # Name Hire Date Job Code Job Desc Job Hourly
Rate
106 William Smithfield 22-Jun-03 500 Programmer $35.75
107 Maria Alonzo 10-Oct-91 500 Programmer $35.75
102 David Senior 12-Jul-87 501 Systems Analyst $96.75
104 Anne Ramoras 15-Nov-85 501 Systems Analyst $96.75
108 Ralph Washington 22-Aug-89 501 Systems Analyst $96.75
109 Larry Smith 18-Jul-95 501 Systems Analyst $96.75
115 Travis Bawangi 25-Jan-90 501 Systems Analyst $96.75
101 John News 08-Nov-98 502 Database Designer $105.00
105 Alice Johnson 01-Feb-91 502 Database Designer $105.00
103 June Arbough 01-Dec-94 503 Electrical Engineer $84.50
110 Gerald Olenko 11-Dec-93 505 Civil Engineer $55.78
111 Geoff Wabash 04-Apr-89 506 Clerical Support $26.87
112 Darlene Smithson 23-Oct-92 507 DSS Analyst $45.95
113 Delbert Joenbrood 15-Nov-94 508 Applications Designer $48.10
114 Annelise Jones 20-Aug-91 508 Applications Designer $48.10
117 Angie Williamson 19-Jun-94 509 Bio Technician $34.55
116 Gerald Pratt 05-Mar-95 510 General Support $18.36
118 James Frommer 04-Jan-04 510 General Support $18.36

(A) When is normalization process used? (2 marks)

When you are required to analyze and correct an existing database.

9
(B) The current database has two tables. Redesign, that is normalize, the database so that all the
tables are in 3NF and create the dependency diagrams for all the tables in your re-designed
database. A database designer has identified the following primary keys for the above two
tables:

Table Primary Key


Project Project Num, Emp #, Work Date
Employee Emp #

Use these primary keys for the normalization process. (8 marks)

10
(C) Briefly explain when 4NF is violated. (2 marks)
Fourth normal form is violated when the table has multiple independent multi-valued attributes.
However, these multi-valued attributes can be determined by a common key.

11
Question 23 – Structured Query Language (28 marks)

Use the following description and the ERD given below to answer parts (A) to (I) of this question. Write
SQL statements to answer each part and your SQL statement should be written for a MySQL database.

This database is used by a video library to store data about movies, copies of each movie (video),
membership, and rental records of the movies. A movie can have one or more copies. Each copy can be
rented by one or more members. The movie copy record is stored in the Video table. Each movie has a
particular price scheme and all price schemes are included in the Price table.

Price Movie
PK Price_Code SMALLINT PK Movie_Num INTEGER

Price_Description VARCHAR(50) Movie_Title VARCHAR(255)


Price_RentFee DECIMAL(5,2) Movie_Year SMALLINT
Price_DailyLateFee DECIMAL(5,2) Movie_Cost DECIMAL(5,2)
Movie_Genre VARCHAR(12)
FK1 Price_Code SMALLINT
RentalDetail

PK,FK1 Rent_Num INTEGER


PK,FK2 Vid_Num INTEGER

Detail_Fee DECIMAL(5,2)
Detail_DueDate DATETIME Video
Detail_ReturnDate DATETIME
Detail_DailyLateFee DECIMAL(5,2) PK Vid_Num INTEGER

Vid_InDate DATETIME
FK1 Movie_Num INTEGER
Rental

PK Rent_Num INTEGER Membership

Rent_Date DATETIME PK Mem_Num INTEGER


FK1 Mem_Num INTEGER
Mem_FName VARCHAR(50)
Mem_LName VARCHAR(50)
Mem_Street VARCHAR(255)
Payment Mem_City VARCHAR(255)
PK Payment_Num INTEGER Mem_State VARCHAR(2)
Mem_Zip VARCHAR(5)
Payment_Date DATETIME Mem_Balance DECIMAL(5,2)
Payment_Amount DECIMAL(5,2)
FK1 Mem_Num INTEGER

12
(A) List the details of all members, sorted by their last name in descending order. (2 marks)

SELECT *

FROM membership

ORDER BY mem_lname DESC

(B) List the number of rental transactions where the video was returned late that is after the
due date. (2 marks)

SELECT COUNT(*) FROM rentaldetail

WHERE detail_returndate > detail_duedate

(C) List the movie number, title, rental fee and late return fee for each movie. Sort the list by
rental fee in increasing order. (3 marks)

SELECT movie_num, movie_title, price_rentfee, price_dailylatefee

FROM movie NATURAL JOIN price

ORDER BY price_rentfee

(D) List the movie number and title of the most expensive movie(s). (3 marks)

SELECT movie_num, movie_title

FROM movie NATURAL JOIN price

WHERE price_rentfee IN (SELECT DISTINCT MAX(price_rentfee) from


price)

13
(E) List the number, name (combine first and last names together) and state of all members
that live in the same state as members with member numbers 107 and 106. Do not
include members 106 and 107 in the output. (5 marks)

SELECT mem_num, mem_state, CONCAT( mem_lname, " ", mem_fname ) AS


'member name'
FROM membership
WHERE mem_state IN (
SELECT mem_state
FROM membership
WHERE mem_num IN (107, 106))
AND mem_num NOT IN (107, 106)

(F) List the video number, movie number and movie title of all movies that have not been
rented out. (3 marks)

SELECT vid_num, movie_num, movie_title


FROM movie natural join video
WHERE vid_num NOT IN ( SELECT DISTINCT (vid_num) FROM
rentaldetail )

(G) Create a view named moviesales that lists the movie number, title and the total amount of
sales for each movie. Include those movies that have not been rented. Exclude any late
return penalty that may have been incurred as a result of the member returning the movie
late. (5 marks)

CREATE VIEW moviesales AS


SELECT movie_num, movie_title, sum( detail_fee ) AS 'Sales
Amount'
FROM rentaldetail
RIGHT JOIN (
movie NATURAL JOIN video
)
USING (vid_num)
GROUP BY movie_num, movie_title

14
(H) Write the UPDATE statement to update the first name of the member with number 106
from Jane to Jene. (2 marks)

UPDATE membership SET mem_fname = ‘Jene’ WHERE mem_num = 106;

(I) Create a trigger that will automatically update the members balance when the payment
table is updated with the payment records. You can use the following partially complete
trigger definition. (3 marks)

CREATE TRIGGER memberbalanceupdate

AFTER INSERT ON payment

FOR EACH ROW BEGIN

UPDATE membership SET mem_balance = mem_balance –


NEW.payment_amount

WHERE membership.mem_num = NEW.mem_num;

END

15

You might also like