Library Management System Using Mysql
Library Management System Using Mysql
BE G I NNE R D AT A E NG I NE E RI NG SQL
Introduction
In this article, we will build Library Management System using MYSQL. We will build the database, which
includes tables. Imagine that you go to the library, take a book, and just enter that book into the computer
rather than entering your details and book details in a register. Isn’t it easy and convenient? Here comes
the library management system. This system is handy for people going to the library and searching for
their desired book and for the librarian to guide them and take care of these things. It ensures that
everything works in systematic order, given that each person taking or returning books needs to enter the
record in the system.
Source: Lovelycoding.org
Library Management System is a system that shows all the available books and their count and also books
taken by people, the date on which they took that particular book, expected date of return, late due fees,
membership details, and so on. Everything will be crystal clear. There will be no ambiguity. It will be
beneficial for both students and librarians.
This library management is very efficient and also cost-effective. It saves a lot of time for both librarians
and also students. With this, manual work is reduced, requiring less staff and maintenance. This system is
user-friendly and also very easy to use.
Analysis of Features
Firstly we need to take every user perspective; user means not only customers but also staff in the library.
What are all the features each user requires?
Record while issuing books- When anyone takes a book, staff should be able to scan the barcode on
the book and should be able to enter the record.
Profile editing- Staff should be able to edit the profile and the profiles of the people with membership
in that library.
They should be able to keep track of books issued by them.
Should be able to ask, request, or demand the books from the people who took that if they crossed the
due date.
They should be able to track books, their place, and so on.
If there occurs any change in the system, or if anyone else entered details or tried to access the
system, staff should get the notification.
Creating Tables
So our first step is to create tables. Let us create a database as db_LibraryManagement and then create all
the required tables inside it.
table_publisher ( PublisherName VARCHAR(50) PRIMARY KEY NOT NULL, PublisherAddress VARCHAR(100) NOT NULL,
CREATE TABLE table_book ( BookID INT PRIMARY KEY NOT NULL IDENTITY (1,1), Book_Title VARCHAR(100) NOT NULL,
CREATE TABLE table_library_branch ( library_branch_BranchID INT PRIMARY KEY NOT NULL IDENTITY ( library
SELECT FROM table_library_branch CREATE TABLE table_borrower ( CardNo INT PRIMARY KEY NOT NULL IDENTITY
(100,1), BorrowerName VARCHAR(100) NOT NULL, BorrowerAddress VARCHAR(200) NOT NULL, BorrowerPhone VARCHAR(50)
NOT MILL, );
CREATE TABLE table_book_copies ( book_copies CopiesID INT PRIMARY KEY NOT NULL book_copies BookID INT NOT
NULL book_copies BranchID INT NOT NULL book_copies No Of Copies INT NOT NULL, );
SELECT FROM table_book_copies CREATE TABLE table_book_authors ( book_authors AuthorID INT PRIMARY KEY NOT
NULL IDENTITY (1,1), book_authors BookID INT NOT NULL CONSTRAINT fk_book_id3 FOREIGN KEY REFERENCES
Inserting Data
Next is having some data inserted into the tables. Let’s do it now.
-- Table structure for table `book` CREATE TABLE IF NOT EXISTS `book` ( `isbn` char(13) NOT NULL, `title`
varchar(80) NOT NULL, `author` varchar(80) NOT NULL, `category` varchar(80) NOT NULL, `price` int(4) unsigned
INSERT INTO `book` (`isbn`, `title`, `author`, `category`, `price`, `copies`) VALUES ('9788654552277', 'X-
Men: God Loves, Man Kills', 'Chris', 'Comics', 98, 39), ('0964161484100', 'Mike Tyson : Undisputed Truth',
'Larry Sloman, Mike Tyson', 'Sports', 654, 79), ('6901142585540', 'V for Vendetta', 'Alan Moore', 'Comics',
600, 23), ('9094996245442', 'When Breath Becomes Air', 'Paul Kalanithi', 'Medical', 500, 94),
('8653491200700', 'The Great Gatsby', 'F. Scott Fitzgerald', 'Fiction', 432, 120);
CREATE TABLE IF NOT EXISTS `book_issue` ( `issue_id` int(11) NOT NULL, `member` varchar(20) NOT NULL,
`book_isbn` varchar(13) NOT NULL, `due_date` date NOT NULL, `last_reminded` date DEFAULT NULL ) ;
CREATE TRIGGER `issue_book` BEFORE INSERT ON `book_issue` FOR EACH ROW BEGIN SET NEW.due_date =
DATE_ADD(CURRENT_DATE, INTERVAL 20 DAY); UPDATE member SET balance = balance - (SELECT price FROM book WHERE
isbn = NEW.book_isbn) WHERE username = NEW.member; UPDATE book SET copies = copies - 1 WHERE isbn =
NEW.book_isbn; DELETE FROM pending_book_requests WHERE member = NEW.member AND book_isbn = NEW.book_isbn; END
CREATE TRIGGER `return_book` BEFORE DELETE ON `book_issue` FOR EACH ROW BEGIN UPDATE member SET balance =
balance + (SELECT price FROM book WHERE isbn = OLD.book_isbn) WHERE username = OLD.member; UPDATE book SET
CREATE TABLE IF NOT EXISTS `librarian` ( `id` int(11) NOT NULL, `username` varchar(20) NOT NULL, `password`
INSERT INTO `librarian` (`id`, `username`, `password`) VALUES (1, 'Vani', 'xthds97@3h$yfc*jrk0%dfg$');
CREATE TABLE IF NOT EXISTS `member` ( `id` int(11) NOT NULL, `username` varchar(20) NOT NULL, `password`
char(40) NOT NULL, `name` varchar(80) NOT NULL, `email` varchar(80) NOT NULL, `balance` int(4) NOT NULL );
CREATE TRIGGER `add_member` AFTER INSERT ON `member` FOR EACH ROW DELETE FROM pending_registrations WHERE
username = NEW.username CREATE TRIGGER `remove_member` AFTER DELETE ON `member` FOR EACH ROW DELETE FROM
pending_book_requests WHERE member = OLD.username Structure of table for pending book requests
CREATE TABLE IF NOT EXISTS `pending_book_requests` ( `request_id` int(11) NOT NULL, `member` varchar(20) NOT
NULL, `book_isbn` varchar(13) NOT NULL, `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP );
CREATE TABLE IF NOT EXISTS `pending_registrations` ( `username` varchar(30) NOT NULL, `password` char(20) NOT
NULL, `name` varchar(40) NOT NULL, `email` varchar(20) NOT NULL, `balance` int(10), `time` timestamp NOT NULL
DEFAULT CURRENT_TIMESTAMP );
INSERT INTO `pending_registrations` (`username`, `password`, `name`, `email`, `balance`, `time`) VALUES
ALTER TABLE `book` ADD PRIMARY KEY (`isbn`); ALTER TABLE `book_issue` ADD PRIMARY KEY (`issue_id`); ALTER
TABLE `librarian` ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `username` (`username`); ALTER TABLE `member` ADD
PRIMARY KEY (`id`), ADD UNIQUE KEY `username` (`username`), ADD UNIQUE KEY `email` (`email`); ALTER TABLE
`pending_book_requests` ADD PRIMARY KEY (`request_id`); ALTER TABLE `pending_registrations` ADD PRIMARY KEY
(`username`);
Testing
Now it’s time to test our database. The below code is to test whether we are getting the correct output. We
are getting to know the number of books named The Lost Tribe present in the library Sharpstown.
book.book_Title AS [Book Title] FROM table_book_copies AS copies INNER JOIN table_book AS book ON
copies.book_copies_BookID = book.book_BookID INNER JOIN table_library_branch AS branch ON
Conclusion
The library management system is essential for colleges, schools, and many more places these days. A lot
of manual work can be reduced with this library management system. And also, a lot of glitches like wrong
borrow date and miscalculation of fine amount are avoided. As it is a computer-managed system and so
these are all avoided. It is also efficient and cost-effective. The Library management system stores the
details of books and also details of persons. So overall, we have seen-
The media shown in this ar ticle is not owned by Analytics Vidhya and is used at the Author’s discretion.