Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (794.31 KB, 18 trang )
<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">
Topic and Description, Objectives and Usage scenario ... 3
I, Functionalities and Requirement ... 4
I.1, Functionalities ... 4
I.2, Requirement ... 4
II, Entity Relationship Diagram and Relational Schema ... 5
II.1.ERD ... 5
II.2, Relational Schema ... 5
III, Database description ... 6
IV, SQL Queries ... Error! Bookmark not defined.V, Install functionalies ... 7
1. Function to count the number of borrowing for each member ... 7
2. Trigger when modifying borrowing ... 8
3. Function set a book to available stauts when a member returns a book ... 9
4. Function to count the number of copies for each book ... 9
5. trigger when modifying book_copy ... 10
6. Function to update number_of_books for each author ... 11
7. trigger when modifying book_author ... 11
8. trigger to delete a book copy ... 12
VI, Difficulties ... 15
VII, Evaluation ... 15
VIII, Task of each member ... 15
</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">- Book Management:
• Managing detailed record of a book
• Managing all copy of the book in library: Allow the staff to manage all the copy of the book of the library
- Author Management: In case readers want to find books written by an author.
• Managing the Author’s name and his/her books
• Storing the Author’s Information such as his/her name, Date of Birth, Nationality, Description,
- Book and Author Searching: Link the Book Author to allow users to search for the book and its authors
- Category Management: Allow the reader to search the type of books they want to read.
• Storing book’s category, for example: Detective, Horror, Mathematics, Scientific, Fictional,...
- Publisher Management: Containing accurate detail about the publisher - Member Management: Storing the reader’s information
- Borrowing and returning process managing and storing: indicate whether the book has been borrowed and returned back.
- Notification: Storing all the users’s received notification, send the notification to the users when borrowing and returning books happened.
- A books can be written by multiple author and an author can write a lot of books - A book will be associated with a category. Book’s copies is distinguish with each
other by a code design by the library
- Notification depends on the member list. Each notification will be sent to one member.
- A borrowing relation depends on the member list and the book_copy list. Each borrowing can only consist of one members borrowing one book copy - A book can have multiple book_copy but a book_copy can be associated with
only one book.
</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">2. Relational Schema
</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6">• BookID: An unique ID to identify books, generated by the ISBN 13 code
• Title: the title of the book
• CategoryID: Foreign Key linked to the Categories_list Relation. Category of a book is assigned by the librarian.
• Description: The summary of a book, written by the author and the staff of the library.
• Number_of_Pages: Storing the number of pages of that book
• Number_of_copies: storing the number of copies of that book
Storing the information of the author
• AuthorID: Storing the ID of the author
• Author_name: The name of the author
• Date_of_birth: His/her birthdate
• Nationality: His/her nationality
• Description: Detailed information of the Author
• Number_of_books: storing the number of books the author wrote.
Storing Author and their book
• Book_ID: Storing unique books, reference to the Books Table
• AuthorID: Storing the Author’s ID, reference to the Author Table
Storing the categories bring specified by the library
• CategoryID: Storing the unique ID of the categories
• Name: Storing the full name of the category
Store the detail information about each copy of books in library
• book_copy_ID: The unique ID of the copy
• year_published: The Year it has been published
• publisher_ID: Foreign Key reference to the Publisher_detail relation
• book_ID: Foreign Key reference to the Book relation
• Is_available: The status of the book
The information of the publisher
• PublisherID: Unique ID of the publisher
• PublisherName: Name of the publisher
• Email: Contact email to the publisher
• phone_number: The publisher’s phone number
</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">• memberID: Unique ID is the username created by user
• DoB: The Date of birth of User
• email: the email used to recover the account in case the user forget the password
• Name: User’s Full name, typed in by the User
• Phone_number: the user’s number in case the library wants to contact the user
• Number_of_borrowing: the number of book they have borrowed
Weak entity belongs to the strong entities Book_copy and Member.
• Book_copy_id: Foreign Key links to the Book_copy relation
• Member_id: Foreign Key links to the Member relation
• Start_time: store the time they successfully borrowed a book from library
• End_time: store the time they scheduled to return the book
• Is_returned: the status attribute of a borrowed book.
Store the notification to the user
• Notif_ID: the Id of the notification.
• Member_id: Primary Key and foreign Key reference to the Member table.
• Sent_at: the time to sent the notification
• content: the message the library sent (available book, request to return books)
1. Function to count the number of borrowing for each member create or replace function count_borrowing()
returns void as $$ declare
borrowing_count int; member_record varchar(40); begin
for member_record in select memberID from member loop select count(*) into borrowing_count
from borrowing
where memberID = member_record;
update member
</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">set number_of_borrowing = borrowing_count where memberID = member_record; end loop;
end; $$
LANGUAGE plpgsql;
2. Trigger when modifying borrowing
CREATE OR REPLACE FUNCTION update_book_copy_availability() RETURNS TRIGGER AS $$
BEGIN
IF NEW.is_returned = 0 THEN UPDATE book_copy SET is_available = 0
WHERE book_copy_id = NEW.book_copy_id; END IF;
FOR EACH ROW
EXECUTE FUNCTION update_book_copy_availability();
</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">3. Function set a book to available stauts when a member returns a book create or replace function modify_checkout(in member_id varchar(40), in book_copy_id varchar(30), in return_date date)
returns void as $$ begin
update borrowing set is_returned = 1
where borrowing."memberID" = member_id and borrowing."book_copy_id" = book_copy_id and borrowing."is_returned" = 0;
update book_copy set is_available = 1
WHERE book_copy."book_copy_id" = book_copy_id;
INSERT INTO notification (notif_ID, sent_at, type, memberID)
VALUES (uuid_generate_v4(), return_date, 'Return Successful', member_id); end;
$$
language plpgsql;
4. Function to count the number of copies for each book create or replace function count_number_of_books() returns void as $$
declare
copies_count int; book_record varchar(20); begin
for book_record in select bookID from book loop select count(*) into copies_count
from book_copy
</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">where bookID = book_record;
update book
set number_of_copies = copies_count where bookID = book_record; end loop;
end; $$
language plpgsql;
5. Trigger when modifying book_copy
CREATE OR REPLACE FUNCTION increase_book_copies() RETURNS TRIGGER AS $$
BEGIN UPDATE book
SET number_of_copies = number_of_copies + 1 WHERE bookID = NEW.bookID;
RETURN NEW; END;
$$ LANGUAGE plpgsql;
CREATE or replace TRIGGER increase_copies_trigger AFTER INSERT ON book_copy
FOR EACH ROW
EXECUTE FUNCTION increase_book_copies();
</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11">6. Function to update number_of_books for each author create or replace function count_number_of_books_author() returns void as $$
declare
book_count int;
author_record varchar(20); begin
for author_record in select authorID from author loop select count(*) into book_count
end; $$
language plpgsql;
7. Trigger when modifying book_author
create or replace function update_number_of_books() returns trigger as $$
begin
update author
set number_of_books = number_of_books + 1 where authorID = new.authorID;
</div><span class="text_page_counter">Trang 12</span><div class="page_container" data-page="12">end; $$
language plpgsql;
create or replace trigger trigger_update_book_author after insert on author_book
for each row
execute function update_number_of_books();
8. trigger to delete a book copy
create or replace function decrease_book_copies() returns trigger as $$
begin update book
set number_of_copies = number_of_copies - 1 where bookID = new.bookID;
end; $$
language plpgsql;
create or replace trigger delete_book_copies after delete on book_copy
for each row
execute function decrease_book_copies();
</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13">time (without
index)
Execution time (with index) Count the number of times
the book ‘Physics Principles’ was borrowed
select b.title from book b inner join book_copy bc on b.bookID = bc.bookID inner join borrowing br on br.book_copy_id = bc.book_copy_id where b.title = 'Physics Principles';
0.938 ms 0.887 ms
Find all the member’s name with number_of_borrowing > 3
select name from member whenumber_of_borrowing > 3;
0.662 ms 0.039 ms
Count the number of notifications for member with name "Nguyen Sy Tu"
select count(*) from member minner join notification n on m.memberid = n.memberid where lower(m.name) = 'nguyesy tu' group by m.memberid;
Select * from author where Extract(year from date_of_birth) >= 1980 And Extract(year from date_of_birth<= 2000 order by(
date_of_birth) asc;
0.115 ms 0.104 ms
Find the author who has
Japanese nationality <sup>Select * from author where </sup>nationality = 'Japanese'; <sup>0.040 ms </sup> <sup>0.028 ms </sup>
Select book that has been
written by J.k Rowling <sup>Select ab.bookid, b.title from </sup>author_book ab join book b using(bookid) join author a
0.089 ms 0.088 ms
</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">using(authorid) where a.author_name like 'J.K Rowling%';
Display all the book_copy of the book 'Harry Potter'
select bc.book_copy_id, bc.year_published, p.publisherName from book_copy bc join book b using(bookid) join publisher_detail p
using(publisherid) where b.title like'Harry Potter%';
1.040 ms 0.270 m
Display publisher which published more than 10 books in the library
select p.* from publisher_detailp left join book_copy bc using(publisherid) group by p.publisherid having count(distinct bc.bookid) > 5;
2.860 ms 2.847 ms
Display categories that contains more than 15 books
select c.* from categories_list cleft join book b using (categoryid) group by(c.categoryid) having count(b.bookid) >15;
0.490 ms
0.612 ms
• Index used in this database :
➢ CREATE INDEX idx_publishername ON publisher_detail (publishername); ➢ CREATE INDEX idx_number_of_copies ON book (number_of_copies); ➢ CREATE INDEX idx_number_of_books ON author (number_of_books) ; ➢ CREATE INDEX idx_title ON book (title);
➢ CREATE INDEX idx_member_name ON member (name); • Explanation for the usage of index in this database :
➢ Because we mainly perform searching statements on tables publisher_detail, books, author, member, we use indices to enhance the queries’ performance
</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">each statement that we carry out, the DBMS will have to restructrure the index in the table not effective →
➔ Solution: Since notfication can not exist without member → It’s a weak entity. So we need to set the Foreign key reference to the memberid in Member relation, and a key in notification to form a composite primary key can store multiple →notification of members. To sent notification automatically, we need to create triggers to indicate whether a book is borrowed, sent notification to the member that have same memberID as the borrower. The same goes with the returning book process.
➢ Basically handle the problems of library management, fulfil some functionalities and all requirements
➢ Define the relationships between tables in the database, such as book and author, member and borrowing, pubisher detail
• <b>Nguyễn Đình Phúc : write reports, contribute to making ERD, input data sample, </b>
create queries and contribute to optimizing the queries
</div><span class="text_page_counter">Trang 16</span><div class="page_container" data-page="16">• <b>Vũ Thái Hưng contribute to making ERD, build up the database, input data sample, </b>: create queries and contribute to optimizing the queries
• Nguy n Tr ng Bách<b>ễọ</b> make slides for presentation, contribute to making ERD, create : queries and contribute to optimizing the queries
</div>