0% found this document useful (0 votes)
19 views7 pages

Inf3707 Tut201 2018 Assignmnet 02solution Semester 01

Uploaded by

piratesza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Inf3707 Tut201 2018 Assignmnet 02solution Semester 01

Uploaded by

piratesza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

lOMoARcPSD|6517928

INF3707 TUT201 2018 Assignmnet 02Solution semester 01

Database Design and Implementation (University of South Africa)

Studocu is not sponsored or endorsed by any college or university


Downloaded by Sootha Deea ([email protected])
lOMoARcPSD|6517928

INF3707/202/01/2018

Tutorial Letter 202/01/2018

Database Design and Implementation


INF3707

Semesters 1: Assignment 2 solutions

School of Computing

This tutorial letter contains important information


about your module.

BARCODE

Downloaded by Sootha Deea ([email protected])


lOMoARcPSD|6517928

Assignment 2 Total marks Due date


60
Note that this is a COMPULSORY assignment!
To do this assignment, you need access to the JustLee books database

Question 1: Chapter 5
Currently, the contents of the Category column in the BOOKS table are the actual names for
each category. This structure presents a problem if one user enters COMPUTER for the
Computer category and another user enters COMPUTERS. To avoid this and other problems
that might occur, the database designers have decided to create a CATEGORY table containing
a code and description for each category. The structure for CATEGORY table should be as
follows:

Column Name Datatype Width Constraints


CATCODE VARCHAR2 3 PRIMARY KEY
CATDESC VARCHAR2 11 NOT NULL

The data for the CATEGORY table is as follows:


CATCODE CATDESC
BUS BUSINESS
CHN CHILDREN
COK COOKING
COM COMPUTER
3+FAL FAMILY LIFE
FIT FITNESS
SEH SELF HELP
LIT LITERATURE

1.1 Create the CATEGORY table (3 marks).

CREATE TABLE category (


catcode VARCHAR2(3),
catdesc VARCHAR2(11) NOT NULL,
CONSTRAINT category_code_pk PRIMARY KEY (catcode) );

1.2 Populate the Category table with the given data. Save the changes permanently (8
marks)

INSERT INTO category


VALUES ('BUS', 'BUSINESS');

INSERT INTO category


VALUES ('CHN', 'CHILDREN');

INSERT INTO category


VALUES ('COK', 'COOKING');

INSERT INTO category


VALUES ('COM', 'COMPUTER');

Downloaded by Sootha Deea ([email protected])


lOMoARcPSD|6517928

INF3707

INSERT INTO category


VALUES ('FAL', 'FAMILY LIFE');

INSERT INTO category


VALUES ('FIT', 'FITNESS');

INSERT INTO category


VALUES ('SEH', 'SELF HELP');

INSERT INTO category


VALUES ('LIT', 'LITERATURE');

COMMIT;

1.3 Add a column to the BOOKS table called Catcode. Add a FOREIGN KEY constraint that
requires all category codes entered in the BOOKS table to already exist in the
CATEGORY table. (4 marks)

ALTER TABLE books


ADD (catcode VARCHAR2(3),
CONSTRAINT books_catcode_fk FOREIGN KEY (catcode)
REFERENCES category(catcode));

1.4 Set the Catcode values for the rows in the BOOKS table based on each book’s current
Category value. (8 marks)

UPDATE books
SET catcode = 'BUS'
WHERE category = 'BUSINESS';

UPDATE books
SET catcode = 'CHN'
WHERE category = 'CHILDREN';

UPDATE books
SET catcode = 'COK'
WHERE category = 'COOKING';

UPDATE books
SET catcode = 'COM'
WHERE category = 'COMPUTER';

UPDATE books
SET catcode = 'FAL'
WHERE category = 'FAMILY LIFE';

UPDATE books
SET catcode = 'FIT'
WHERE category = 'FITNESS';

UPDATE books
SET catcode = 'SEH'

Downloaded by Sootha Deea ([email protected])


lOMoARcPSD|6517928

WHERE category = 'SELF HELP';

UPDATE books
SET catcode = 'LIT'
WHERE category = 'LITERATURE';

COMMIT;

Or

UPDATE BOOKS
SET BOOKS.CATCODE = (SELECT CATEGORY.CATCODE
FROM CATEGORY
WHERE CATEGORY.CATDESC = BOOKS.CATEGORY)
WHERE CATCODE IS NULL;

1.5 Verify that the correct categories have been assigned in the BOOKS table, and save
changes permanently. (1 marks)

Select * from Books;

1.6 Delete the Category column from the BOOKS table. (1 marks)

ALTER TABLE books


DROP column category;

Question 2 Chapter 8
Give SQL statements for the following requests.

2.1 Which customers live in Georgia or New Jersey? Put the results in ascending order by last
name. List each customer’s customer number, last name and the state. (5 marks)

Answer;
SELECT customer#, lastname, state
FROM customers
WHERE state = 'GA' or state = 'NJ' //(Note that Georgia and New Jersey are
not cities)
ORDER BY lastname;

Or

SELECT customer#, lastname, state


FROM customers
WHERE state IN ('GA', 'NJ')
ORDER BY lastname;

2.2 List authors whose last name contains the letter pattern “IN”. Put the results in order of last
name, then first name. List each author’s last name and first name. (5 marks)

Answer
SELECT lname, fname
FROM author
4

Downloaded by Sootha Deea ([email protected])


lOMoARcPSD|6517928

INF3707

WHERE lname LIKE '%IN%'


ORDER BY lname, fname;

Question 3 Chapter 9
To perform this activity, refer to the tables of the JustLee Books database

The marketing Department of JustLee Books is preparing for its annual sales promotion. Each
customer who places an order during the promotion will receive a gift with each book
purchased. Each gift will be based on the book’s retail price. JustLee Books also participates in
co-op advertising programs with certain publishers. If the publisher’s name is included in the
advertisements, JustLee Books is reimbursed a certain amount of the advertisement cost.

3.1. To determine the projected cost of this year’s sales promotion, the marketing Department
needs the publisher’s name, profit amount and the gift description for each book in the JustLee
Books inventory. Use join operator to create a report for the above request. (3 marks)

Answer

SELECT b.title, pu.name, b.retail-b.cost, p.gift


FROM books b JOIN publisher pu USING (pubid)
JOIN promotion p ON b.retail BETWEEN p.minretail AND p.maxretail
ORDER BY b.retail-b.cost;

3.2 The Marketing Department is analysing books that do not sell. List the ISBN for books with
no sales recorded. Use a set operator to complete this task. (2 marks)

Answer

SELECT isbn
FROM books
MINUS
SELECT isbn
FROM orderitems;

Question 4: Chapter 10
Give SQL statements for the following requests.

4.1 Determine the amount of total profit generated by the book purchased on order 1002.
Display the book title and profit. The profit should be formatted to display a dollar sign and two
decimal places. Take into account that the customer might not pay the full retail price and each
item ordered can involve multiple copies. (5 marks)

Answer ((make sure that the formula is correct, if wrong do not give marks. Note that other
students may use different formulars, please check if they are correct )

SELECT title, TO_CHAR(quantity*(paideach-cost), '$999.99')as Profit


FROM books JOIN orderitems USING (isbn)
WHERE order# = 1002;

4.2 Display a list of all book titles and percentage of mark-up for each book. The percentage of
mark-up should be displayed as a whole number (that is, multiplied by 100) with no decimal
position, followed by a percent sign (for example, 0.2793 = 28%). The percentage of mark-up
5

Downloaded by Sootha Deea ([email protected])


lOMoARcPSD|6517928

should reflect the difference between the retail and cost amounts as a percent of the cost.).
(5 marks)
Answer (make sure that the formula is correct, if wrong do not give marks. Note that some
students may use different formulas from this but correct)

SELECT title, ROUND((retail-cost)/cost *100, 0)||'%' as "Markup %"


FROM books;

Question 5: Chapter 11
Give SQL statements for the following requests.

5.1 Determine which books cost less than the average cost of other books in the same
category. Use subqueries to answer the question. (5 marks)

Answer (make sure subqueries are used, if not do not give marks. Note that other students
may manualy calculate the average)
SELECT a.title, b.category, a.cost
FROM books a, (SELECT category, AVG(cost) averagecost
FROM books
GROUP BY category) b
WHERE a.category = b.category
AND a.cost < b.averagecost;

5.2 Determine which books were shipped to the same state as order 1014. Use subqueries to
answer the question. (5 marks)
Answer

SELECT order#
FROM orders
WHERE shipstate = (SELECT shipstate
FROM orders
WHERE order# = 1014);

Downloaded by Sootha Deea ([email protected])

You might also like