INF3707/202/02/2018
Tutorial Letter 202/02/2018
Database Design and Implementation
INF3707
Semesters 2: Assignment 02 solutions
School of Computing
This tutorial letter contains important information
about your module.
BARCODE
Assignment 2 Total marks Due date
50
Note that this is a COMPULSORY assignment!
To do this assignment, you need access to the JustLee books database
Question 1: Chapter 6
Give SQL statements for the following requests.
1.1 Create a sequence that generates integers starting with the value 5. Each value should be
three less than the previous value generated. The lowest value should be 0, and the sequence
should not be allowed to cycle. Name the sequence MY_FIRST_SEQ. (5 marks)
Answer
CREATE SEQUENCE my_first_seq
INCREMENT BY -3
START WITH 5
MAXVALUE 5
MINVALUE 0
NOCYCLE;
1.2 Issue a SELECT statement that displays NEXTVAL for MY_FIRST_SEQ three times. Since
the value is not being stored in the table, use dual table in the FROM clause of the SELECT
statement. What caused the error on the third SELECT? (5 marks)
Answer
SELECT my_first_seq.NEXTVAL
FROM DUAL;
Students should explain this error. If they copy and paste the error
from Oracle IDE, no marks
Explanation: Caused by the sequence running out of values to issue, as
the minimum value of 0 was reached and the CYCLE option is set to
NOCYCLE.
Question 2: Chapter 7
Create and execute statements to perform the following actions, using the JustLee Books
database:
2.1 Create a new user account. The account name should be a combination of your first initial
and last name. (1 marks)
Answer
CREATE USER finitiallastname
IDENTIFIED BY apassword;
2
INF3707
2.2 Attempt to log in to Oracle with the newly created account. What happened? (1 marks)
Answer
Connection will fail because the CREATE SESSION privilege is required.
2.3 Assign privileges to the new account that allow connecting to the database, creating new
tables and altering existing tables. (3 marks)
Answer
GRANT CREATE SESSION, CREATE TABLE, ALTER ANY TABLE
TO finitiallastname;
2.4 Create a role named CUSTOMERREP that allows inserting new rows in the ORDERS and
ORDERITEMS tables and deleting rows from these tables. (5 marks)
Answer
CREATE ROLE customerrep;
GRANT INSERT, DELETE
ON orders
TO customerrep;
GRANT INSERT, DELETE
ON orderitems
TO customerrep;
Question 3: Chapter 9
Give SQL statements for the following requests.
3.1 Determine which books customer Jake Lucas purchased. Perform the search using the
customer name, not the customer number. If he has purchased multiple copies of the same
book, induplicate the results. Generate the report using the JOIN keyword. (5 marks)
Some students may use tradition CROSS JOINS etc
SELECT DISTINCT b.title
FROM customers c JOIN orders USING (customer#)
JOIN orderitems USING (order#)
JOIN books b USING (isbn)
WHERE c.firstname = 'JAKE'
AND c.lastname = 'LUCAS';
3.2 Which books where written by an author whose last name is Adams? Perform the search
using the author name. Generate the report using the JOIN keyword. (5 marks)
Some students may use tradition CROSS JOINS etc
SELECT b.title
FROM books b JOIN bookauthor USING (isbn)
JOIN author a USING (authorid)
WHERE a.lname = 'ADAMS';
3
Question 4: Chapter 10
To perform this activity, refer to the tables of the JustLee Books database.
Management is proposing to increase the price of each book. The amount of the increase on
each book’s category is according to the following scale:
Computer books, 10%
Fitness books, 15%
Self-Help books, 25%
All other categories, 3%.
Create a query that displays each book’s title, category, current retail price, and revised retail
price. The format the prices to two decimal places. The column headings for the output should
be as follows: Title, category, Current Price and Revised Price. Sort the results by category. If
there is more than one book in category, perform a secondary sort on the book’s title. (10
marks)
Answer
SELECT title "Title", category "Category", retail "Current Price",
Round(decode(category,
'COMPUTER',retail*1.1,'FITNESS',retail*1.15,'SELF HELP',retail*1.25,
retail*1.03), 2) "Revised price"
FROM books
order by category, title;
or
SELECT title, category, retail AS "Current Price",
retail *
CASE category
WHEN 'COMPUTER' THEN 1.1 WHEN 'FITNESS' THEN 1.15 ELSE 1.03 END AS
"Revised Price"
FROM books
order by category,title;
Question 5: Chapter 11
Give SQL statements for the following requests.
Determine, which orders, had a higher total amount due than order 1008. Use subqueries to
answer the question (10 marks)
SELECT oi.order#, SUM(oi.quantity*oi.paideach)
FROM orderitems oi, books b
WHERE oi.isbn = b.isbn
GROUP BY oi.order#
HAVING SUM(oi.quantity*oi.paideach) >
(SELECT SUM(oi.quantity*oi.paideach)
FROM orderitems oi, books b
WHERE oi.isbn = b.isbn
AND oi.order# = 1008);
4
INF3707
Or
SELECT order#, SUM(quantity*paideach) "total Due"
From orderitems
HAVING SUM (quantity*paideach) > (SELECT SUM (quantity*paideach) FROM
orderitems Where order# = 1008)
Group by order#;