Book Inventory Book Inventory System Application - Sample database lab project

28
Book Inventory 1 Book Inventory Book Inventory System Application Yelena Bytenskaya University of Maryland University College

Transcript of Book Inventory Book Inventory System Application - Sample database lab project

Book Inventory 1

Book Inventory

Book Inventory System ApplicationYelena Bytenskaya

University of Maryland University College

Book Inventory 2

ContentsStatement of work 3 Timeline 4 Entity relationship diagram 4Table design 5Data definition 6

Drop objects 6Create tables 7Create indexes 9Create views 9Create trigger 10Data dictionary query 10

Insert data 11Queries 17Conclusion 24

Book Inventory 3

Statement of Work1. This database supports the Book Inventory application and

keeps track of books, their prices, authors, publishers, and

the available number of copies of every book at each store.

The purpose of the database is to support the customers with

buying a book at the most convenient location and at the

best price, and to support the managers with book orders for

the store.

The book information includes the title, publisher,

author, edition, hard cover, and ISBN. The store

information includes the store name, street address, zip

code, and manager’s name. For each author, the database

keeps track of first, last name, nickname, and date of

birth. Each zip codes table includes city and state. An

inventory tracks the book price and the number of available

copies at each store.

Book Inventory 4

For this application, each book must have an author. If

there is more than one author, the database stores only the

first author’s name. Each author could have written zero or

more books. A book can be available at zero or many stores,

and each store could sell zero or more books. A zipcode

location may have zero or more stores, and each store must

have a location.

Assumptions: This application tracks the stores

located in the United States only. The prices for each book

vary among the stores.

2. Database: Oracle 11g on UNIX platform.

3. Software: Oracle data modeler, Oracle SQL developer, and

Putty.

4. DDL – drop and create the database objects including tables,

views, triggers, and sequences. DML – populate and query

data.

Timeline

Book Inventory 5

Task Due dateStatement of work Class 3ERD Class 5DDL Class 9Insert data Class 9Queries Class 11

Entity Relationship Diagram

Book Inventory 6

Tables Design

Author

AuthorID FirstName LastName Nickname DateOfBirth

AuthorID NUMERIC Not null - pkFirstName VARCHAR (20)LastName VARCHAR (30)Nickname VARCHAR (20)DateOfBirth DATE

ZipCode

ZipCode City State

ZipCode NUMERIC Not null - pkCity VARCHAR (20)State VARCHAR (30)

Book

ISBN Title Publisher

Edition IsHardCover

AuthorID

ISBN NUMERIC Not null - pkTitle VARCHAR (40)Publisher VARCHAR (25)Edition VARCHAR (15)IsHardCover BOOLEANAuthorID NUMERIC Not null - fk

Book Inventory 7

Store

StoreID Name StreetAddress

ManagerName

ZipCode

StoreID NUMERIC Not null - pkName VARCHAR (20)StreetAddress VARCHAR (30)ManagerName VARCHAR (35)ZipCode NUMERIC Not null - fk

Inventory

InventoryID NumCopies Price ISBN StoreID

InventoryID NUMERIC Not null - pkNumCopies NUMERICPrice DECIMAL (7, 2)ISBN NUMERIC Not null - fkStoreID NUMERIC Not null - fk

Data Definitionset echo on;set serveroutput on;

/* Drop tables and sequence*/drop table inventory;

table INVENTORY dropped.

drop table store;

table STORE dropped.

drop table book;

Book Inventory 8

table BOOK dropped.

drop table zipcode;

table ZIPCODE dropped.

drop table author;

table AUTHOR dropped..drop sequence authorID_Seq;

sequence AUTHORID_SEQ dropped.

/* Create 5 tables */

create table author( AuthorID NUMERIC, FirstName VARCHAR (20), LastName VARCHAR (30), Nickname VARCHAR (20), DateOfBirth DATE, constraint pk_author primary key (AuthorID));

table AUTHOR created.

describe author;

create table zipcode

Book Inventory 9

( ZipCode NUMERIC, City VARCHAR (20), State VARCHAR (30), constraint PKzipcode primary key (zipCode));

table ZIPCODE created.

describe zipcode;

create table book( ISBN NUMERIC, Title VARCHAR (40), Publisher VARCHAR (25), Edition VARCHAR (15), IsHardCover CHAR (1) , AuthorID NUMERIC NOT NULL, constraint PKISBN primary key (ISBN), constraint FKAuthorID foreign key (AuthorID) references author);

table BOOK created.

describe book;

Book Inventory 10

create table store( StoreID NUMERIC, Name VARCHAR (20), StreetAddress VARCHAR (30), ManagerName VARCHAR (35), ZipCode NUMERIC NOT NULL, constraint PKStoreID primary key (StoreID), constraint FKSipCode foreign key (ZipCode) references zipcode);

table STORE created.

describe store;

create table inventory( InventoryID NUMERIC, NumCopies NUMERIC, Price DECIMAL (7, 2), ISBN NUMERIC NOT NULL, StoreID NUMERIC NOT NULL, constraint PKInventory primary key (InventoryID),

Book Inventory 11

constraint FKISBN foreign key (ISBN) references book, constraint FKStoreID foreign key (StoreID) references store);

table INVENTORY created.

describe inventory;

/* Create indexes */

create index fk_MyStore on inventory (StoreID);

index FK_MYSTORE created.

create index fk_ISBN on inventory (ISBN);

index FK_ISBN created.

create index fk_zip on store (ZipCode);

index FK_ZIP created.

create index fk_AuthorID on book (AuthorID);

index FK_AUTHORID created.

/* Create views */

create or replace view HardCoverbook_view as

Book Inventory 12

select * from book where IsHardCover='1' or IsHardCover='Y' or IsHardCover='y';

view HARDCOVERBOOK_VIEW created.

describe HardCoverbook_view;

create or replace view Inventory_view as select i.ISBN, b.title, i.StoreID, i.NumCopies, i.price from inventory i inner join book b on i.ISBN=b.ISBN;

view INVENTORY_VIEW created.

describe Inventory_view;

/* Create trigger */

create or replace trigger inventory_trigafter insert on inventoryfor each rowbegin

Book Inventory 13

dbms_output.put_line ('The inventory item has been inserted.Congratualtions!!');end; /

TRIGGER inventory_trig compiled

/* Data dictionary query */

purge recyclebin;

select substr(object_name, 1, 20) as name, object_type, status from user_objects;

Insert Data /* create authorID_Seq sequence to use for the authorId values */

Book Inventory 14

create sequence authorID_Seq start with 1 increment by 1;

sequence AUTHORID_SEQ created.

select * from author;

Book Inventory 15

select * from zipcode;

Book Inventory 16

select * from book;

Book Inventory 17

SQL> select * from store;

insert into inventory (InventoryID, NumCopies, Price, ISBN, StoreID) values ('1', '100', '10.59', '5737243737', '3')1 rows inserted.The inventory item has been inserted. Congratualtions!!

Book Inventory 18

insert into inventory (InventoryID, NumCopies, Price, ISBN, StoreID) values ('2', '50', '11.59', '5737243737', '4')1 rows inserted.The inventory item has been inserted. Congratualtions!!

insert into inventory (InventoryID, NumCopies, Price, ISBN, StoreID) values ('3', '0', '9.99', '2765432197', '4')1 rows inserted.The inventory item has been inserted. Congratualtions!!

insert into inventory (InventoryID, NumCopies, Price, ISBN, StoreID) values ('4', '1', '5.99', '2727272799', '5')1 rows inserted.The inventory item has been inserted. Congratualtions!!

insert into inventory (InventoryID, NumCopies, Price, ISBN, StoreID) values ('5', '10', '8.99', '7272727272', '1')1 rows inserted.The inventory item has been inserted. Congratualtions!!

insert into inventory (InventoryID, NumCopies, Price, ISBN, StoreID) values ('6', '10', '8.99', '2827776800', '1')1 rows inserted.The inventory item has been inserted. Congratualtions!!

insert into inventory (InventoryID, NumCopies, Price, ISBN, StoreID) values ('7', '10', '8.99', '2827776800', '10')

Book Inventory 19

1 rows inserted.The inventory item has been inserted. Congratualtions!!

insert into inventory (InventoryID, NumCopies, Price, ISBN, StoreID) values ('8', '10', '8.99', '2827776802', '9')1 rows inserted.The inventory item has been inserted. Congratualtions!!

insert into inventory (InventoryID, NumCopies, Price, ISBN, StoreID) values ('9', '10', '8.99', '2827776801', '8')1 rows inserted.The inventory item has been inserted. Congratualtions!!

commit;

Commit complete.

SQL> select * from inventory;

Queries

/* 1. Select all columns and all rows from one table */select * from book;

Book Inventory 20

/* 2. Select 5 columns and all rows from one table */

select ISBN, Title, Publisher, Edition, AuthorID from book;

/* 3. Select all columns and all rows from 2 tables */

select ISBN, Title, Publisher, ishardcover, book. AUTHORID, author. AUTHORID, firstname, lastname, nickname, dateofbirthfrom book inner join author on book. AUTHORID=author. AUTHORID;

Book Inventory 21

/* 4. Select and order data retreived from 1 table */

select * from book order by ISBN;

/* 5. Select 5 columns and 10 rows from 3 tables */

select book.ISBN,title, store.name, StreetAddress, price from (inventory inner join book on inventory.isbn=book.isbn) inner join store on store.storeid=inventory.storeidwhere rownum<11;

/* 6. Select distinct rows using joins using 3 tables */

select distinct inventory.storeid, book.titlefrom (inventory inner join book on inventory.isbn=book.isbn) inner join store on store.storeid=inventory.storeid;

Book Inventory 22

/* 7. Select all columns and 10 rows from 2 tables */select ISBN, Title, Publisher, ishardcover, book. AUTHORID, author. AUTHORID, firstname, lastname, nickname, dateofbirthfrom book inner join author on book. AUTHORID=author. AUTHORIDwhere rownum<11;

/* 8. Use group by & having in a select statement – list books with available quantity above 50*/

select ISBN, avg(numcopies) AS Average_quantity from inventorygroup by ISBN having avg(numcopies)>50;

/9. *Use IN clause */ select * from book where edition in (1, 3);

Book Inventory 23

/* 10. Select lenght of a column*/

select title, length (title) from book;

/* 11. Use column alias*/

select title, length (title) as numLetters from book;

/* 12. Advanced query using dates – list the authors born before August 8, 1970 */

select *

Book Inventory 24

from authorwhere dateofbirth<to_date('08-AUG-70', 'dd-mm-yy');

/* 13 Use an aggregate function */

SELECT ISBN, avg(price) AS Average_Price FROM inventory group by ISBN;

/* 14 A query to update the data */

select * from inventory;

Book Inventory 25

update inventory set price=price+1;

10 rows updated.

select * from inventory;

rollback;

Rollback complete.

/* 15. Perform 6 advanced queries */

/* 15.1 (Type 2 subquery) List the title, anuther first and lastname for the books sold in more than one stores. */

SELECT Title, FirstName, Lastname FROM book INNER JOIN author ON book.authorID=author.AuthorIDWHERE (SELECT count(*) as count FROM inventory WHERE inventory.ISBN=Book.ISBN)>=2;

/* 15.2 (Type 2 subquery) List the ISBN and Title of the book that is not sold at any store. */

Book Inventory 26

SELECT book.ISBN, TitleFROM bookWHERE NOT EXISTS (SELECT ISBN FROM inventory WHERE inventory.ISBN=book.ISBN);

/* 15.3 (5 table join) List the book title, the author first and last name, the store name and address (street address, city, state, and Zipcode), price, and number of available copies for each book.*/

SELECT title, FirstName, LastName, Name as Store, StreetAddress, City, store.ZipCode AS ZipCode, Price, NumCopiesFROM (((inventory INNER JOIN book on inventory.ISBN=book.ISBN) INNER JOIN author on author.AuthorID=book.AuthorID) INNER JOIN store on store.StoreID=inventory.StoreID) INNER JOIN zipcode on zipcode.ZipCode=store.ZipCode;

/* 15.4 (Type 1 subquery) List the title, store name and price for all books with the greater than average price */

select title, Name as Store, price

Book Inventory 27

from (inventory INNER JOIN book on inventory.ISBN=book.ISBN) INNER JOIN store on store.StoreID=inventory.StoreIDwhere price> (select avg(price) from inventory);

/* 15.5 (Type 1 subquery) List the books titles that are unavailable for sale */

select title from book where isbn not in (select isbn from inventory);

/* 15.6 (outer join) List the first and last name of the Authors who don't have any books listed in the book table. */

SELECT FirstName, LastNameFROM author LEFT JOIN book on book.AuthorID=author.AuthorIDWHERE book.AuthorID is NULL;

ConclusionThe most important things that I learned while developing this project are:

1. The tables need to be created in a certain order because of the foreign key constraints. For the same reason, we need to populate the tables in a certain order. For example, I can’t add a data about the book if the author’s table is

Book Inventory 28

empty, unless I leave the author information blank. In addition, I can’t have a book written by author not listed in the author’s table.

2. It’s possible to use a trigger to print a user-friendly message when the new data are inserted. The customer would like this message better than the message saying “1 row created.”

3. In this database, each table has only 10 records. It’s not enough data to run some of the reports. For example, if would be nice to check which books are sold at all stores, or each store sells all books. We need to enter more records into the inventory table to test this report.

4. This database has only five tables, and it took approximately 2 months to design and to develop it. It shows that the databases in the real work environment, whichare much larger and have more data, would take longer to develop and would require more planning.

5. It’s very important to understand the database structure to be able to query the data. Before writing a query, we need to find out in what tables the desired data are located and how those tables are related. Very often, we may need to join more than 2 tables to satisfy the reporting request.