Академический Документы
Профессиональный Документы
Культура Документы
Problem 1
All questions in this problem are based on the data shown in Figure R5.1. This database table is
named EMP_1. The table structure is summarized in Table R5.1. Have in mind that this table is
in relation with another table JOBS, which primary key is JOB_CODE.
FIGURE R5.1 The Contents of the EMP_1 Table
1. Write the SQL code that will create the table structure. Pay particular attention to referential
and entity integrities. (9p)
Since the EMP_NUM is a primary key, it is best to declare it NOT NULL. In fact,
attributes are usually declared NOT NULL for efficiency reasons, unless there is reason
to believe that the attribute is expected to have nulls occasionally. (For example, not
everyone has a middle initial, nor does everyone necessarily have a job code
assignment.) Also, to enforce referential and entity integrity, you should make use of the
primary and foreign key declarations shown in the following sequence. (We're assuming
that the JOB_CODE is the foreign key to a JOB table.)
Naturally, if your software's SQL does not allow such enhancements, delete the NOT
NULL and primary key and foreign key references.
2. Having created the table structure in question 1, write the SQL code that will enter the first
data row into the EMP_l table. (7p)
3. Assuming that the remaining data have been entered, write the SQL code that will list all
attributes for a job code of 502. (7p)
SELECT *
FROM EMP_1
WHERE JOB_CODE = '502';
4. Write the SQL code that will save the EMP_l table after all the tuples have been inserted.
(7p)
COMMIT EMP_1;
5. Write the SQL code to change the job code to 501 for the person whose personnel number is
106. After you have completed the task, examine the results, and then reset the job code to its
original value. (7p)
UPDATE EMP_1
SET JOB_CODE = '501'
WHERE EMP_NUM = '106';
SELECT *
FROM EMP_1
WHERE EMP_NUM = '106';
UPDATE EMP_1
SET JOB_CODE = '500'
WHERE EMP_NUM = '106';
You may also reset the attribute values to their original state with the ROLLBACK
command, assuming that the original database table values had been saved to disk with
COMMIT.
6. Write the SQL code to delete the row for the person named William Smithfield, who was
hired on 06/23/90 and whose job code classification is 500. Assume that the personnel
number is not known (Hint: Use logical operators to include all the information given in this
problem.) (7p)
7. Create a copy of EMP_1, name it EMP_2, then add the attributes EMP_PCT and
PROJ_NUM to its structure. The EMP_PCT is the bonus percentage to be paid to each
employee. The new attribute characteristics are shown next:
EMP_PCT NUMBER(4,2)
PROJ_NUM CHAR(3)
(HINT: alter the table). (7p)
Consider the LIBRARY relational schema shown at Figure bellow, which is used to keep track of
books, borrowers and book loans. Referential integrity constraints are shown in directed arcs (e.g.
table BOOK has a foreign key PublisherName that points to the table PUBLISHER). Primary
keys are underlined (e.g. BOOK_LOANS has a composity key consisting of three attributes:
BookID, BranchId and CardNo). As we can see, each author may be an author of several books,
each book may have several copies, book is determined by its BookId, so that is possible to exist
two books of the same title but different BookIds. If a borrower has borrowed a book and did not
return it, the data about that are maintained in BOOK_LOANS table. However, when the book is
returned, the corresponding record in BOOK_LOANS is deleted.
a) How many copies of the book titled ‘Titanic’ are owned by the library branch whose
name is ‘Cottman’? (HINT: In this and some of the following queries, you should
perform join on multiple tables). (7p)
SELECT SUM(No_Of_Copies)
FROM BOOK B, BOOK_COPIES C, LIBRARY_BRANCH L
WHERE B.BookId=C.BookId
AND C.BranchId=L.BranchId
AND B.Title=’Titanic’
AND L.Branch=’Cottman’;
b) How many copies of the book titled ‘Titanic’ are owned by each library branch? (HINT:
it is possible to exist two books of the same title but different BookIds). (7p)
c) Retrieve the names of all the borrowers who do not have any book checked out. (HINT:
first, create view containing the result of an outer join). (7p)
SELECT Name
FROM PROBLEM_C
WHERE BOOK_LOANS.CardNo IS NULL;
d) For each book that is loaned from the ‘Cottman’ branch and whose DueDate is
6/21/2002, retrieve the book title, the borrower’s name and the borrower’s address. (7p)
e) For each library branch, retrieve the branch name and the total number of books loaned
out from that branch. (7p)
f) Retrieve the names, addresses, and number of books checked out for all borrowers who
have more than five books checked out. (HINT: Use HAVING) (7p)
g) For each book authored by ‘Ivo Andric’ retrieve the title and the number of copies owned
by the library branch whose name is ‘Central’. (7p)