Вы находитесь на странице: 1из 17

Sara Irfan

Assignment 3/4 SQL


Total points: 100
This assignment should be completed individually. For each problem, submit your
SQL statements and a screen shot of the SQL results in a single Word document or
pdf file. Submit the file via eLearning.

I recommend creating a new user and workspace named after your netid, log in as that user and load the
database script facts.sql (provided in this week's assignment folder). Before you attempt to write any SQL
queries, familiarize yourself with the database structure and data. I have provided a relational diagram
and sample data for this database.
Write queries to address each of the problems below. Submit both the SQL statements and the
screen prints of the outputs from Oracle. (2 points for the screen shots). Be sure the workspace
name is included in your screen shots!!!

FACT Description and ERD


The CIS Department at Tiny College maintains the Free Access to Current Technology (FACT) library of
eBooks.
FACT is a collection of current technology eBooks for use by faculty and students.
Agreements with the publishers allow patrons to electronically check out a book, which gives them
exclusive access to the book online through the FACT website, but only one patron at a time can have
access to a book. A book must have at least one author but can have many. An author must have
written at least one book to be included in the system, but may have written many. A book may have
never been checked out, but can be checked out many times by the same patron or different patrons
over time. Because all faculty and staff in the department are given accounts at the online library, a
patron may have never checked out a book or they may have checked out many books over time. To
simplify determining which patron currently has a given book checked out, a redundant relationship
between BOOK and PATRON is maintained.

Sara Irfan

Sara Irfan

1. Create a new table to track the Library location. Screen shot not required
(6pts)

LIBRARY (lib_id, lib_name, lib_address, lib_city, lib_state, lib_zip)


LIB_ID is the library id it is an auto generated number. (you should
create a sequence number called lib_id_seq, start with 1001 and
increment by 1.)
LIB_ID is the primary key.
LIB_NAME, LIB_ADDRESS, and LIB_CITY is between 1 and 35
characters.
LIB_STATE is 2 characters default to TX.
LIB_ZIP is 5 numbers. Check for one of the following zip codes
75081, 75080, 75082, 75079, 75078
CREATE TABLE LIBRARY (
LIB_ID VARCHAR(4) PRIMARY KEY,
LIB_NAME VARCHAR(35),
LIB_ADDRESS VARCHAR(35),
LIB_CITY VARCHAR(35),
LIB_STATE VARCHAR(2) DEFAULT 'TX',
LIB_ZIP VARCHAR(5),
CHECK(LIB_ZIP IN('75081', '75080', '75082', '75079', '75078'))
);
CREATE SEQUENCE
LIB_ID_SEQ
START WITH 1001
INCREMENT BY 1;
2. Insert the following records into the LIBRARY table Display the rows after
they have been insert into the table. (5 pts)
1000,
1001,
1002,
1003,
1004,

JFK Library, 800 West Campbell Road, Richardson, TX, 75080


MLK Library, 105 King Blvd., Richardson, TX, 75081
Hoover Library, 932 Arapaho St., Richardson, TX, 75080
Perot Library, 1523 Perot St., Plano, TX, 75082
Bush Library, 2456 W St., Dallas, TX, 75079

Sara Irfan

INSERT INTO LIBRARY (LIB_ID, LIB_NAME, LIB_ADDRESS,


LIB_CITY, LIB_STATE, LIB_ZIP)
VALUES('1000', 'JFK Library', '800 West Campbell Road',
'Richardson', 'TX', '75080')
INSERT INTO LIBRARY (LIB_ID, LIB_NAME, LIB_ADDRESS,
LIB_CITY, LIB_STATE, LIB_ZIP)
VALUES('1001', 'MLK Library', '105 King Blvd.',
'Richardson', 'TX', '75081')
INSERT INTO LIBRARY (LIB_ID, LIB_NAME, LIB_ADDRESS,
LIB_CITY, LIB_STATE, LIB_ZIP)
VALUES('1002', 'Hoover Library', '932 Arapaho St.',
'Richardson', 'TX', '75080')
INSERT INTO LIBRARY (LIB_ID, LIB_NAME, LIB_ADDRESS,
LIB_CITY, LIB_STATE, LIB_ZIP)
VALUES('1003', 'Perot Library', '1523 Perot St.', 'Plano',
'TX', '75082')
INSERT INTO LIBRARY (LIB_ID, LIB_NAME, LIB_ADDRESS,
LIB_CITY, LIB_STATE, LIB_ZIP)
VALUES('1004', 'Bush Library', '2456 W St.', 'Dallas', 'TX',
'75079')

3. There is a 1:M relationship between LIBRARY and BOOK. Alter the BOOK table,
add lib_id as a foreign key. (4 pts) Screen shot not required

ALTER TABLE BOOK


ADD (LIB_ID VARCHAR(4))
ALTER TABLE BOOK
4

Sara Irfan
ADD CONSTRAINT LIB_ID_FK FOREIGN KEY (LIB_ID) REFERENCES LIBRARY
(LIB_ID)
4. Update all the Checkout records by updating the year to 2016. (HINT: Use the
ADD_MONTHS function). (5 pts) Screen shot not required

UPDATE CHECKOUT
SET CHECK_OUT_DATE = ADD_MONTHS(CHECK_OUT_DATE, 12)
UPDATE CHECKOUT
SET CHECK_DUE_DATE = ADD_MONTHS(CHECK_DUE_DATE, 12)
UPDATE CHECKOUT
SET CHECK_IN_DATE = ADD_MONTHS(CHECK_IN_DATE, 12)

5. Due to inflation, the cost of all books published in 2014 will increase by 6%.
Write a single SQL command to increase all book costs by 6%. (4 pts) Screen
shot not required

UPDATE BOOK
SET BOOK_COST = BOOK_COST *1.06
WHERE BOOK_YEAR = '2014'

6. Write a query that will display all the Books that were published in 2016.
Display the book year, book title, book subject, author last name, and author
first. Sort the records by book title. The output should be formatted as follows
(Book Title (year), Book Subject, Author Name) see output below. (5 pts)

SELECT BOOK_TITLE, BOOK_SUBJECT, AU_LNAME ||', ' || AU_FNAME


AS "AUTHOR_NAME"
FROM BOOK B, AUTHOR A, WRITES W
5

Sara Irfan
WHERE A.AU_ID = W.AU_ID AND W.BOOK_NUM = B.BOOK_NUM AND
BOOK_YEAR = 2016
ORDER BY BOOK_TITLE

7. Write a query to display the book title, book year, book subject , and book cost
for all books that are either Database or Programming books and that have a
replacement cost that is greater than $50. Sort the results in ascending order
by Subject then cost. (4 pts)

SELECT BOOK_TITLE, BOOK_YEAR, BOOK_SUBJECT, BOOK_COST FROM BOOK


WHERE BOOK_SUBJECT = 'Database' OR BOOK_SUBJECT = 'Programming'
AND BOOK_COST > 50.00
ORDER BY BOOK_SUBJECT, BOOK_COST ASC

Sara Irfan

8. Write a query to show the min, max, and average replacement cost for all
books. (4 pts)

SELECT MIN(BOOK_COST), MAX(BOOK_COST), AVG(BOOK_COST) FROM


BOOK

Sara Irfan
9. Write a query to display the book subject and the number of books as #
Books in each subject, order by the subject. (5 pts)

SELECT BOOK_SUBJECT, COUNT(BOOK_SUBJECT) AS "# BOOKS" FROM BOOK


GROUP BY BOOK_SUBJECT
ORDER BY BOOK_SUBJECT

10.Write a query to display the patron type and the number of patrons as # of
Patrons in each type, order by the number of patrons in descending order.
What are some of the problems with the way the data is entered? How could
you avoid those problems at the DBMS level? (5 pts)

SELECT PAT_TYPE, COUNT(PAT_TYPE) AS "# of Patrons" FROM PATRON


GROUP BY PAT_TYPE
ORDER BY 2 DESC

Sara Irfan

The person who entered the patron type used a combination of


uppercase and lowercase letters for each entry, resulting in 8
records of patron type when in reality there should only be 2;
Student and Faculty. To avoid this, you can use the %student
% and %faculty% as well as UPPER(PAT_TYPE) LIKE %STUDENT%
OR %FACULTY% to group all patron types that have the same
name into 2 categories. It is important to establish rules
from the beginning in order to avoid redundancy like this.
11.Write a query to calculate the age of each author display the author name as
follows (Lastname, Firstname), their birth year, and their age as Age. Round
the age to 0 places order by age in descending order. Do not display those
authors who do not have a birthyear recorded. (HINT: Use sysdate to get the
current date, and to_char to extract out the current year). (5 pts)

SELECT AU_LNAME || ', ' || AU_FNAME AS "AUTHOR_NAME",


AU_BIRTHYEAR, (TO_CHAR(SYSDATE, 'YYYY') - AU_BIRTHYEAR) AS
"Age" FROM AUTHOR
WHERE AU_BIRTHYEAR IS NOT NULL
ORDER BY "Age" DESC

Sara Irfan

12.Which books have been checked out more than 7 times? Show the book_title,
bok_num and the # of times checked out. Show the query and the result set.
(6 pts)

SELECT B.BOOK_TITLE, B.BOOK_NUM, COUNT(C.CHECK_OUT_DATE) AS "#


OF CHECKOUTS" FROM BOOK B, CHECKOUT C
WHERE B.BOOK_NUM = C.BOOK_NUM
GROUP BY B.BOOK_NUM, B.BOOK_TITLE
HAVING COUNT(C.CHECK_OUT_DATE) > 7

10

Sara Irfan

13.Write a query to display books that have the word database in the title
regardless of how it is capitalized. (4 pts)

SELECT BOOK_TITLE FROM BOOK


WHERE BOOK_TITLE LIKE '%database%'
OR UPPER(BOOK_TITLE) LIKE '%DATABASE%'

14.Write a query to display the total number of books as # of Books. (4 pts)

SELECT COUNT(BOOK_TITLE) AS "# of Books" FROM BOOK


11

Sara Irfan

15.Which patrons have checked out more than 2 books? Display the patron name,
patron type, and number of books. (6 pts)

SELECT PAT_LNAME ||', '|| PAT_FNAME AS "PAT_NAME", PAT_TYPE,


count(BOOK_NUM) as "# of Books"
FROM PATRON P LEFT JOIN CHECKOUT C
ON P.PAT_ID = C.PAT_ID
GROUP BY PAT_TYPE, PAT_FNAME, PAT_LNAME
HAVING COUNT (BOOK_NUM) > 2
ORDER BY "# of Books"

12

Sara Irfan

16.Write a query to display the patron id, book title, and days kept for each
checkout. Days Kept is the difference from the date on which the book is
returned to the date it was checked out. (6 pts)

SELECT DISTINCT P.PAT_ID, BOOK_TITLE, (CHECK_OUT_DATE CHECK_IN_DATE) AS "DAYS_KEPT"


FROM BOOK B,PATRON P,CHECKOUT C
WHERE C.BOOK_NUM = B.BOOK_NUM AND C.PAT_ID = P.PAT_ID

13

Sara Irfan

17.Write a query to display all the books that are currently checked out. Display
the book title, patron last name, due date, and # of days remaining until it is
due. (6 pts)

SELECT B.BOOK_TITLE, P.PAT_LNAME, CHECK_OUT_DATE, CHECK_DUE_DATE,


TRUNC(CHECK_DUE_DATE - CHECK_OUT_DATE) AS "DAYS LEFT UNTIL BOOK
DUE"
FROM CHECKOUT C, BOOK B, PATRON P
WHERE C.PAT_ID = P.PAT_ID AND C.BOOK_NUM = B.BOOK_NUM AND
CHECK_IN_DATE IS NULL

14

Sara Irfan

18.Write a query that will display all the books that are overdue. Display the book
title, patron last name, due date, and days overdue. (6 pts)

SELECT BOOK_TITLE, PAT_LNAME, CHECK_DUE_DATE, CHECK_IN_DATE,


TRUNC(CHECK_IN_DATE - CHECK_OUT_DATE) AS "DAYS OVERDUE"
FROM BOOK B, PATRON P, CHECKOUT C
WHERE C.PAT_ID = P.PAT_ID AND C.BOOK_NUM
(CHECK_IN_DATE > CHECK_DUE_DATE)

B.BOOK_NUM

AND

19.Write a query that will which authors have written more than 2 books.
Display the author first name, last name, and # of Books Written. (6 pts)

15

Sara Irfan

SELECT AU_ID, COUNT(*)


FROM WRITES
GROUP BY AU_ID
HAVING COUNT(*) > 1

20.Write a query that will list all the books that have never been checked out. List
the book number, book title and subject. (4 pts)

SELECT B.BOOK_NUM, B.BOOK_TITLE, B.BOOK_SUBJECT,


C.CHECK_OUT_DATE
FROM BOOK B FULL JOIN CHECKOUT C
ON C.BOOK_NUM = B.BOOK_NUM
WHERE C.CHECK_OUT_DATE IS NULL

16

Sara Irfan

17

Вам также может понравиться