August
1999 QUESTION 5 Total Marks: 20 Marks |
Click here to access other
questions
SUGGESTED SOLUTIONS |
Consider the
following four tables.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Customer
Order
PART
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Based on the tables
above, write SQL statements to carry out the following tasks:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(a) | Create the PART table, using the following
table definition. PART
|
[4] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CREATE TABLE PART (PARTNUMB CHAR(4), PARTDESC CHAR(10), UNONHAND NUMERIC(3,0), ITEMCLSS CHAR(2), WRHSNUMB NUMERIC(1,0) UNITPRCE NUMERIC(7,2));
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(b) | From the CUSTOMER table, which records can be removed without introducing any anomaly? Justify your answer. Give an SQL statement to remove one of these records. | [3] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Customers with
CUSTNUMB 405,412,567,587 and 622 NB: all 5 records needed! This is possible as these customers do not have any order transactions. Removal of these records will therefore not cause any anomaly. DELETE FROM CUSTOMER
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(c) | Find the number, name, current balance and sales rep number of those customers whose balance is greater than the balance of every customer of sales rep 12. | [6] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SELECT CUSTNUM,
CUSTNAME, BALANCE, REPNUMB FROM CUSTOMER WHERE BALANCE > ALL (SELECT BALANCE FROM CUSTOMER WHERE REPNUMB = 12);
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(d) | Create a view LISTING with headers ORDER_NUMBER and ORDER_TOTAL to list the order total for those orders whose total is more than $200. | [5] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CREATE VIEW LISTING
(ORDER_NUMBER, ORDER_TOTAL) AS SELECT ORDNUMB, SUM(NUMBORD * QUOTPRCE) FROM ORDLNE GROUP BY ORDNUMB HAVING SUM(NUMBORD * QUOTPRCE) > 200;
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(e) | Give two advantages of using the VIEW command. | [2] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Use of a view can
allow someone access to just the rows and columns that they need from the database tables,
particularly restricting them from irrelevant and sensitive information. By assigning a view name, users can retrieve the required information just by selecting the already created view by its assigned name, without having to explicitly query the database using the data manipulation language component of SQL.
|