top of page

Oracle Database Practice Set | Libraries Relational Database Schema



SQL Queries

a) Use your script from HW Assignment 6 to create the LIBRARY relational database schema in the figure below, in your Oracle schema. (Alternatively, a solution script HW6_SQL_DDL_Library.sql will be provided for that assignment after the submission window closes.)


b) Use the HW7_HW8_SQL_DML_Library.sql script provided on Canvas to populate the LIBRARY tables. If you use your HW Assignment 6 script and get errors because varchar attributes are limited to less characters than used in the insert statements, please extend the allowed size for these attributes, recreate the LIBRARY tables, and run again the script to populate them. Leave the tables in your schema until your assignment is graded in Canvas.


c) Find the names of all authors of books published by "Penguin" that cost more than $10. Make sure each name is included only once in the result.


d) Find the title, publisher and price for all books with an unknown author.


e) The library is setting a summer reading program, where each book values a number of points, based on its length; the formula to determine points for a book is number of pages / 100, rounded up to the nearest integer. Calculate the number of points for each book and list it together with the book id and title. Name the calculated column "no_of_points".


f) Find the title and publisher of all books that are priced between 3 and 6 dollars. Requirements:

- All titles from each publisher must be reported consecutively, and sorted alphabetically from A to Z.

- Publisher names must be all shown in capital letters.

- The report must have the 3 columns: publisher, title, and a new calculated column that should be named "Selling price". A partial result is shown below:

g) How many copies of the book titled "The Lost Tribe" are owned by the library branch whose name is "Newport Branch"? Report the library branch id, name, and number of copies of the book. Separate join conditions from where conditions.


h) Find the card number and name of all members who have borrowed books from the Alexandria library branch. Requirements:

  • List borrowers ordered by their card number.

  • Make sure each name is included only once in the result.

  • Separate join conditions from where conditions.

i) Find the card numbers of all library members who have not borrowed any books from the library. You must use set operations to write this query.


DDL(Data Definition Language)

drop table book_loans;
drop table book_copies;
drop table book;
drop table library_branch;
drop table borrower;

create table library_branch
	(branch_id int,
	branch_name varchar2(20),
	address varchar2(50),
	primary key (branch_id));

create table book
	(book_id int,
	title varchar2(50),
	publisher varchar2(30),
	author varchar2(30),
	price numeric (6,2),
	primary key (book_id));

create table book_copies
	(book_id int,
	branch_id int,
	no_of_copies int,
	primary key (book_id, branch_id),
	foreign key (book_id) references book,
	foreign key (branch_id) references library_branch);

create table borrower
	(card_no int,
	name varchar2(30),
	address varchar2(50),
	phone varchar2(12),
	primary key (card_no));	

create table book_loans
	(book_id int,
	branch_id int,
	card_no int,
	date_out date,
	due_date date,
	primary key (book_id, branch_id, card_no),
	foreign key (book_id, branch_id) references book_copies,
	foreign key (card_no) references borrower);

alter table book add (no_of_pages int);

commit;

-- library_branch

insert into library_branch(branch_id, branch_name, address)
values (1, 'Fort Thomas Branch', '1000 Highland Ave, Fort Thomas, KY 41075');

insert into library_branch(branch_id, branch_name, address)
values (2, 'Fort Thomas Branch', '1000 Highland Ave, Fort Thomas, KY 41075');

insert into library_branch(branch_id, branch_name, address)
values (3, 'Fort Thomas Branch', '1000 Highland Ave, Fort Thomas, KY 41075');

insert into library_branch(branch_id, branch_name, address)
values (4, 'Fort Thomas Branch', '1000 Highland Ave, Fort Thomas, KY 41075');

-- book

insert into book(book_id, title, publisher, author, price, no_of_pages)
values(1,'The Lost Tribe','Penguin','John Smyth',5.50,275);

insert into book(book_id, title, publisher, author, price, no_of_pages)
values(2,'How to Sew Buttons','Kensington','Jane Do',4.35,50);

insert into book(book_id, title, publisher, author, price, no_of_pages)
values(3,'The Terrible Night','Penguin','John Smyth',7.89,280);

insert into book(book_id, title, publisher, author, price, no_of_pages)
values(4,'Mindy''s Mittens','Scholastic','Eleanor Mellors',5.50,35);

insert into book(book_id, title, publisher, author, price, no_of_pages)
values(5,'Pizza and Donuts Diet','Kensington','Jane Do',10.5,80);

-- book_copies

INSERT INTO book_copies (book_id,branch_id,no_of_copies) VALUES(1,2,2);
INSERT INTO book_copies (book_id,branch_id,no_of_copies) VALUES(1,1,3);
INSERT INTO book_copies (book_id,branch_id,no_of_copies) VALUES(2,2,2);
INSERT INTO book_copies (book_id,branch_id,no_of_copies) VALUES(3,3,2);
INSERT INTO book_copies (book_id,branch_id,no_of_copies) VALUES(4,4,2);
INSERT INTO book_copies (book_id,branch_id,no_of_copies) VALUES(5,1,5);
INSERT INTO book_copies (book_id,branch_id,no_of_copies) VALUES(6,2,2);
INSERT INTO book_copies (book_id,branch_id,no_of_copies) VALUES(7,3,1);

-- borrower

INSERT INTO Borrower(card_no,name,address,phone) values(1,'Charlie Brown','27 Main St, Newport','859-555-5123');
INSERT INTO Borrower(card_no,name,address,phone) values(2,'Rachel Rigby','101 Hwy 22, Cold Spring','859-688-7711');
INSERT INTO Borrower(card_no,name,address,phone) values(3,'Nancy Drew','5678 Oak St, Newport','859-555-3467');
INSERT INTO Borrower(card_no,name,address,phone) values(4,'Derek Jones','6789 Ritmo Cir, Cold Spring','859-222-1234');
INSERT INTO Borrower(card_no,name,address,phone) values(5,'Howie Han','111 First Ave, Anderson','513-234-5678');
INSERT INTO Borrower(card_no,name,address,phone) values(6,'Tim Tegulpas','432 Nebraska Ave, Alexandria','859-987-6543');
INSERT INTO Borrower(card_no,name,address,phone) values(7,'Sam Semel','7688 Hedge Ct, Wilder','859-777-9898');
INSERT INTO Borrower(card_no,name,address,phone) values(8,'Evan Mann','4545 Court St, Alexandria','859-899-9090');
INSERT INTO Borrower(card_no,name,address,phone) values(9,'Sally Short','323 Remington St, Alexandria','859-767-8991');
INSERT INTO Borrower(card_no,name,address,phone) values(10,'Bob Biggs','227 South St, Ft. Thomas',null);

--book_loans

INSERT INTO book_loans(book_id,branch_id,card_no,date_out,due_date) VALUES(1,1,4,to_date('2020-08-20', 'yyyy-mm-dd'),to_date('2020-09-20', 'yyyy-mm-dd'));
INSERT INTO book_loans(book_id,branch_id,card_no,date_out,due_date) VALUES(2,2,4,to_date('2020-08-20', 'yyyy-mm-dd'),to_date('2020-09-20', 'yyyy-mm-dd'));
INSERT INTO book_loans(book_id,branch_id,card_no,date_out,due_date) VALUES(3,3,4,to_date('2020-08-20', 'yyyy-mm-dd'),to_date('2020-09-20', 'yyyy-mm-dd'));
INSERT INTO book_loans(book_id,branch_id,card_no,date_out,due_date) VALUES(4,4,4,to_date('2020-08-19', 'yyyy-mm-dd'),to_date('2020-09-19', 'yyyy-mm-dd'));
INSERT INTO book_loans(book_id,branch_id,card_no,date_out,due_date) VALUES(5,1,4,to_date('2020-08-19', 'yyyy-mm-dd'),to_date('2020-09-19', 'yyyy-mm-dd'));
INSERT INTO book_loans(book_id,branch_id,card_no,date_out,due_date) VALUES(6,2,2,to_date('2020-09-19', 'yyyy-mm-dd'),to_date('2020-10-19', 'yyyy-mm-dd'));
INSERT INTO book_loans(book_id,branch_id,card_no,date_out,due_date) VALUES(7,3,2,to_date('2020-09-19', 'yyyy-mm-dd'),to_date('2020-10-19', 'yyyy-mm-dd'));
INSERT INTO book_loans(book_id,branch_id,card_no,date_out,due_date) VALUES(8,4,2,to_date('2020-09-18', 'yyyy-mm-dd'),to_date('2020-10-18', 'yyyy-mm-dd'));
INSERT INTO book_loans(book_id,branch_id,card_no,date_out,due_date) VALUES(9,1,2,to_date('2020-09-18', 'yyyy-mm-dd'),to_date('2020-10-18', 'yyyy-mm-dd'));
commit;

Solution

Requirements a) and b): Leave the LIBRARY tables in your schema until your assignment is graded in Canvas.


Query c): select author

from book

where publisher='Penguin' AND price>10;


output:


Query d):

select TITLE,PUBLISHER,PRICE from book where AUTHOR='unknown';


Query e):

Select book_id, title, (no_of_pages/100) as no_of_points from book;


Query f):

Select Upper(publisher), title,'Selling_price' || ' ' ||'$' || price as Selling_price from book where price between 3 and 6;


Query g):

select book_copies.branch_id, book.title, book_copies.no_of_copies from book

inner join book_copies on book.book_id = book_copies.book_id

inner join library_branch on library_branch.branch_id = book_copies.branch_id where library_branch.branch_name = 'Newport Branch' and book.title = 'The Lost Tribe';


Query h):

select Borrower.card_no, Borrower.name

from Borrower

inner join book_loans on book_loans.card_no = borrower.card_no

inner join book_copies on book_copies.book_id = book_loans.book_id and book_copies.branch_id = book_loans. branch_id

inner join library_branch on library_branch.branch_id = book_copies.branch_id

where library_branch.branch_name='Alexandria';


Query i):

select card_no from Borrower

minus

select Borrower.card_no

from Borrower

inner join book_loans on book_loans.card_no = borrower.card_no

inner join book_copies on book_copies.book_id = book_loans.book_id and book_copies.branch_id = book_loans. branch_id

inner join library_branch on library_branch.branch_id = book_copies.branch_id

where library_branch.branch_name='Alexandria';



Send Your Requirement Details at:

realcode4you@gmail.com

To get help in any other databases.


106 Comments


Ba Anh
Ba Anh
9 hours ago

Thường mình sẽ đọc nhanh các bài giới thiệu trên điện thoại nên khá thích kiểu viết ngắn nhưng đủ ý. Bài nói về https://www.97c77.com/  có cách sắp xếp nội dung hợp lý, thông tin được chia rõ nên theo dõi khá thoải mái. Ngoài việc giới thiệu giao diện dễ thao tác, bài còn nhắc đến nhiều danh mục quen thuộc giúp người đọc dễ nắm tổng quan.

Like

ok baoah
ok baoah
17 hours ago

Mình biết đến MU88 khi vô tình bắt gặp tên trang trong một cuộc thảo luận trên mạng nên đã ghé vào xem thử. Chủ yếu mình chỉ quan sát giao diện tổng thể và cách các nội dung được sắp xếp trên trang. Ấn tượng ban đầu là bố cục khá gọn gàng, các khu vực hiển thị được phân chia rõ ràng nên dễ theo dõi và không tạo cảm giác rối mắt. Sau khi xem qua một lúc để tham khảo cách trình bày, mình rời trang và tiếp tục công việc đang làm trước đó.

Like

Fly88 mình bắt gặp khi đang xem một vài nội dung trên mạng nên vào thử cho biết. Mình chỉ lướt nhanh qua giao diện để hình dung cách họ sắp xếp các mục, chưa tìm hiểu chi tiết. Tổng thể trang khá gọn, các phần hiển thị rõ ràng nên nhìn không bị rối

Like

Trịnh Sơn
Trịnh Sơn
2 days ago

Mình từng thấy đăng ký f168 được nhắc đến khi đang lướt qua một bài thảo luận nên cũng tò mò vào xem thử. Mình chỉ xem nhanh giao diện tổng thể và cách các nội dung hiển thị trên trang. Cảm giác ban đầu là bố cục khá dễ nhìn, mọi thứ được sắp xếp tương đối hợp lý nên không mất nhiều thời gian làm quen. Xem qua một lúc thì mình thoát ra và quay lại đọc tiếp như lúc đầu.

Like

MU88 mình có ghé vào xem thử sau khi thấy được nhắc đến trong một vài nội dung. Mình chỉ lướt nhanh để quan sát cách họ bố trí giao diện và sắp xếp các mục chính, chưa tìm hiểu sâu. Ấn tượng ban đầu là trang được thiết kế khá gọn gàng, các khu vực hiển thị rõ ràng nên dễ theo dõi. Thanh menu đặt ở vị trí dễ thấy giúp thao tác chuyển đổi giữa các mục nhanh chóng. Nhìn chung giao diện trực quan, dễ sử dụng và phù hợp để xem nhanh.

Like

REALCODE4YOU

Realcode4you is the one of the best website where you can get all computer science and mathematics related help, we are offering python project help, java project help, Machine learning project help, and other programming language help i.e., C, C++, Data Structure, PHP, ReactJs, NodeJs, React Native and also providing all databases related help.

Hire Us to get Instant help from realcode4you expert with an affordable price.

USEFUL LINKS

Discount

ADDRESS

Noida, Sector 63, India 201301

Follows Us!

  • Facebook
  • Twitter
  • Instagram
  • LinkedIn

OUR CLIENTS BELONGS TO

  • india
  • australia
  • canada
  • hong-kong
  • ireland
  • jordan
  • malaysia
  • new-zealand
  • oman
  • qatar
  • saudi-arabia
  • singapore
  • south-africa
  • uae
  • uk
  • usa

© 2023 IT Services provided by Realcode4you.com

bottom of page