0% found this document useful (0 votes)
10 views

Library Database

The document contains SQL queries to create tables for a library database including tables for publishers, books, book authors, library programs, book copies, and book lending. Data is inserted into the tables and sample queries are provided to retrieve data from the tables. Additional queries are provided to: retrieve book details with publisher, author and copies; find borrowers who borrowed more than 3 books in a period; delete a book and update related tables; partition the book table by year; and create a view of available book copies.

Uploaded by

who knows
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Library Database

The document contains SQL queries to create tables for a library database including tables for publishers, books, book authors, library programs, book copies, and book lending. Data is inserted into the tables and sample queries are provided to retrieve data from the tables. Additional queries are provided to: retrieve book details with publisher, author and copies; find borrowers who borrowed more than 3 books in a period; delete a book and update related tables; partition the book table by year; and create a view of available book copies.

Uploaded by

who knows
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

create table Publisher(Publisher_name varchar(30), Address varchar(30), Phone

varchar(30),
constraint pkyPublisher primary key(Publisher_name));

create table Book(Book_id integer,Title varchar(30),Publisher_name


varchar(30),Publish_year integer,
constraint pkyBook primary key(Book_id),
constraint fkyBook foreign key(Publisher_name) references Publisher(Publisher_name)
on delete cascade);

create table Book_Authors(Book_id integer,Author_name varchar(30),constraint


pkyBookAuthors primary key(Book_id),
constraint fkyBookAuthors foreign key(Book_id) references Book(Book_id) on delete
cascade);

create table Library_programme(programme_id integer,programme_name


varchar(30),Address varchar(30),
constraint pkyLibprogramme primary key(programme_id));

create table Book_Copies(Book_id integer,programme_id integer,No_of_copies integer,


constraint pkyBookCopies primary key(Book_id,programme_id),
constraint fkyBookCopies1 foreign key(Book_id) references Book(Book_id) on delete
cascade,
constraint fkyBookCopies2 foreign key(programme_id) references
Library_programme(programme_id) on delete cascade);

create table Book_Lending(Book_id integer,programme_id integer,Card_no


varchar(30),Date_Out date,Due_Date date,
constraint pkyBookLen primary key(Book_id,programme_id,Card_no),
constraint fkyBookLen1 foreign key(Book_id) references Book(Book_id) on delete
cascade,
constraint fkyBookLen2 foreign key(programme_id) references
Library_branch(programme_id) on delete cascade);

(1)

insert into Publisher values('PHI','India',9880066666);


insert into Publisher values('McGraw Hill','India',9880066777);
insert into Publisher values('Sapna','India',9880066888);
insert into Publisher values('Andearson','India',9880066999);
insert into Publisher values('Tata','India',9880066555);
insert into Publisher values('Dreamtech','India',9880066444);
insert into Publisher values('MHI','India','9880077755');

mysql> SELECT * FROM PUBLISHER;


+----------------+---------+------------+
| Publisher_name | Address | Phone |
+----------------+---------+------------+
| Andearson | India | 9880066999 |
| Dreamtech | India | 9880066444 |
| McGraw Hill | India | 9880066777 |
| MHI | India | 9880077755 |
| PHI | India | 9880066666 |
| Sapna | India | 9880066888 |
| Tata | India | 9880066555 |
+----------------+---------+------------+
7 rows in set (0.00 sec)

(2)
insert into Book values(101,'Programming in C','PHI',2000);
insert into Book values(102,'Programming in C#','MHI',2002);
insert into Book values(103,'Computer Netwokrs','Dreamtech',2006);
insert into Book values(104,'Unix Shell Programming','PHI',2000);
insert into Book values(105,'Compiler Design','PHI',2010);
insert into Book values(106,'Database Management Systems','PHI',2008);

mysql> SELECT * FROM BOOK;


+---------+-----------------------------+----------------+--------------+
| Book_id | Title | Publisher_name | Publish_year |
+---------+-----------------------------+----------------+--------------+
| 101 | Programming in C | PHI | 2000 |
| 102 | Programming in C# | MHI | 2002 |
| 103 | Computer Netwokrs | Dreamtech | 2006 |
| 104 | Unix Shell Programming | PHI | 2000 |
| 105 | Compiler Design | PHI | 2010 |
| 106 | Database Management Systems | PHI | 2008 |
+---------+-----------------------------+----------------+--------------+
6 rows in set (0.00 sec)

(3)
insert into Book_Authors values(101,'Balaguruswamy');
insert into Book_Authors values(102,'Andearson');
insert into Book_Authors values(103,'Ferouzan');
insert into Book_Authors values(104,'Sumitabha Das');
insert into Book_Authors values(105,'Kumar');
insert into Book_Authors values(106,'Tanenbaum');

mysql> SELECT * FROM BOOK_AUTHORS;


+---------+---------------+
| Book_id | Author_name |
+---------+---------------+
| 101 | Balaguruswamy |
| 102 | Andearson |
| 103 | Ferouzan |
| 104 | Sumitabha Das |
| 105 | Kumar |
| 106 | Tanenbaum |
+---------+---------------+
6 rows in set (0.00 sec)

(4)
insert into Library_programme values(1,'Technical','Jayanagar');
insert into Library_programme values(2,'Technical','Kuvempunagar');
insert into Library_programme values(3,'Advanced
Technology','Rajarajeshwarinagar');
insert into Library_programme values(4,'Technical','Vijaynagar');
insert into Library_programme values(5,'Advanced Technology','Vijaynagar');

mysql> SELECT * FROM Library_Branch;


+-----------+---------------------+---------------------+
| Branch_id | Branch_name | Address |
+-----------+---------------------+---------------------+
| 1 | Technical | Jayanagar |
| 2 | Technical | Kuvempunagar |
| 3 | Advanced Technology | Rajarajeshwarinagar |
| 4 | Technical | Vijaynagar |
| 5 | Advanced Technology | Vijaynagar |
+-----------+---------------------+---------------------+
5 rows in set (0.00 sec)

(5)
insert into Book_Copies values(101,1,100);
insert into Book_Copies values(102,1,150);
insert into Book_Copies values(101,2,200);
insert into Book_Copies values(103,3,400);
insert into Book_Copies values(105,4,140);
insert into Book_Copies values(104,5,150);

mysql> SELECT * FROM Book_Copies;


+---------+-----------+--------------+
| Book_id | Branch_id | No_of_copies |
+---------+-----------+--------------+
| 101 | 1 | 100 |
| 101 | 2 | 200 |
| 102 | 1 | 150 |
| 103 | 3 | 400 |
| 104 | 5 | 150 |
| 105 | 4 | 140 |
+---------+-----------+--------------+
6 rows in set (0.00 sec)

(6)
insert into Book_Lending values(101,1,11,'2017-01-22','2017-02-07');
insert into Book_Lending values(102,1,22,'2017-01-22','2017-04-07');
insert into Book_Lending values(103,3,33,'2017-02-22','2017-04-07');
insert into Book_Lending values(104,5,44,'2017-01-22','2017-06-07');
insert into Book_Lending values(105,4,55,'2017-03-22','2017-05-07');
insert into Book_Lending values(101,2,66,'2017-04-22','2017-05-07');
insert into Book_Lending values(103,3,11,'2017-06-20','2017-07-07');
insert into Book_Lending values(104,5,11,'2017-03-12','2017-05-07');
insert into Book_Lending values(105,4,11,'2017-05-10','2017-05-07');

mysql> SELECT * FROM Book_Lending;


+---------+-----------+---------+------------+------------+
| Book_id | Branch_id | Card_no | Date_Out | Due_Date |
+---------+-----------+---------+------------+------------+
| 101 | 1 | 11 | 2017-01-22 | 2017-02-07 |
| 101 | 2 | 66 | 2017-04-22 | 2017-05-07 |
| 102 | 1 | 22 | 2017-01-22 | 2017-04-07 |
| 103 | 3 | 11 | 2017-06-20 | 2017-07-07 |
| 103 | 3 | 33 | 2017-02-22 | 2017-04-07 |
| 104 | 5 | 11 | 2017-03-12 | 2017-05-07 |
| 104 | 5 | 44 | 2017-01-22 | 2017-06-07 |
| 105 | 4 | 11 | 2017-05-10 | 2017-05-07 |
| 105 | 4 | 55 | 2017-03-22 | 2017-05-07 |
+---------+-----------+---------+------------+------------+
9 rows in set (0.00 sec)

QUERIES

Write SQL queries to


1. Retrieve details of all books in the library – id, title, name of publisher,
authors,
number of copies in each Programme, etc.

2. Get the particulars of borrowers who have borrowed more than 3 books, but
from Jan 2017 to Jun 2017.

3. Delete a book in BOOK table. Update the contents of other tables to reflect this
data manipulation operation.

4. Partition the BOOK table based on year of publication. Demonstrate its working
with a simple query.

5. Create a view of all books and its number of copies that are currently available
in the Library.

1)select B.Book_id,Title,Publisher_name,Author_name,No_of_copies
from Book B,Book_Authors A,Book_Copies C,Library_programme L
where B.Book_id=A.Book_id and
B.Book_id=C.Book_id and
C.programme_id=L.programme_id;

+---------+------------------------+----------------+---------------+--------------
+
| Book_id | Title | Publisher_name | Author_name | No_of_copies
|
+---------+------------------------+----------------+---------------+--------------
+
| 101 | Programming in C | PHI | Balaguruswamy | 100
|
| 101 | Programming in C | PHI | Balaguruswamy | 200
|
| 102 | Programming in C# | MHI | Andearson | 150
|
| 103 | Computer Netwokrs | Dreamtech | Ferouzan | 400
|
| 104 | Unix Shell Programming | PHI | Sumitabha Das | 150
|
| 105 | Compiler Design | PHI | Kumar | 140
|
+---------+------------------------+----------------+---------------+--------------
+

2)select Card_no,count(*)
from Book_Lending
where Date_Out between '2017-01-01' and '2017-06-30'
Group by Card_no
Having count(*) > 3;

+---------+----------+
| Card_no | count(*) |
+---------+----------+
| 11 | 4 |
+---------+----------+

3)delete from Book_Lending where Book_ID=102;


{use on delete cascade for all tables while creating}

delete from Book where Book_ID=102;

SELECT * FROM Book_Lending;


+---------+-----------+---------+------------+------------+
| Book_id | Branch_id | Card_no | Date_Out | Due_Date |
+---------+-----------+---------+------------+------------+
| 101 | 1 | 11 | 2017-01-22 | 2017-02-07 |
| 101 | 2 | 66 | 2017-04-22 | 2017-05-07 |
| 103 | 3 | 11 | 2017-06-20 | 2017-07-07 |
| 103 | 3 | 33 | 2017-02-22 | 2017-04-07 |
| 104 | 5 | 11 | 2017-03-12 | 2017-05-07 |
| 104 | 5 | 44 | 2017-01-22 | 2017-06-07 |
| 105 | 4 | 11 | 2017-05-10 | 2017-05-07 |
| 105 | 4 | 55 | 2017-03-22 | 2017-05-07 |
+---------+-----------+---------+------------+------------+
8 rows in set (0.00 sec)

4)create view Book_Details as


select B.Book_id,Title,No_of_copies,L.programme_id
from Book B,Book_Copies C,Library_programme L
where B.Book_id=C.Book_id and
C.programme_id=L.programme_id;

select * from Book_Details;

+---------+------------------------+--------------+--------------+
| Book_id | Title | No_of_copies | programme_id |
+---------+------------------------+--------------+--------------+
| 101 | Programming in C | 100 | 1 |
| 102 | Programming in C# | 150 | 1 |
| 101 | Programming in C | 200 | 2 |
| 103 | Computer Netwokrs | 400 | 3 |
| 105 | Compiler Design | 140 | 4 |
| 104 | Unix Shell Programming | 150 | 5 |
+---------+------------------------+--------------+--------------+

5)create table b2(book_id int,title varchar(20),pname varchar(20),pyear


int,constraint pkyb2 primary key(book_id,pyear))
partition by range(pyear)
( partition p0 values less than(2000),
partition p1 values less than(2005),
partition p2 values less than(2010));

insert into b2 values(2,'dbms','pearson',2000);


insert into b2 values(2,'cn','pearson',2010);
insert into b2 values(2,'cn','pearson',2009);
insert into b2 values(2,'cn','pearson',1998);
insert into b2 values(3,'se','navathe',2005);

select *from b2;

+---------+-------+---------+-------+
| book_id | title | pname | pyear |
+---------+-------+---------+-------+
| 2 | cn | pearson | 1998 |
| 2 | dbms | pearson | 2000 |
| 2 | cn | pearson | 2009 |
| 3 | se | navathe | 2005 |
+---------+-------+---------+-------+
4 rows in set (0.00 sec)
use information_schema;
select * from partitions where table_name="b2";

+---------------+--------------+------------+----------------+-------------------
+----------------------------+-------------------------------+------------------
+---------------------+----------------------+-------------------------
+-----------------------+------------+----------------+-------------
+-----------------+--------------+-----------+---------------------
+---------------------+------------+----------+-------------------+-----------
+-----------------+
| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | PARTITION_NAME | SUBPARTITION_NAME |
PARTITION_ORDINAL_POSITION | SUBPARTITION_ORDINAL_POSITION | PARTITION_METHOD |
SUBPARTITION_METHOD | PARTITION_EXPRESSION | SUBPARTITION_EXPRESSION |
PARTITION_DESCRIPTION | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH | MAX_DATA_LENGTH
| INDEX_LENGTH | DATA_FREE | CREATE_TIME | UPDATE_TIME | CHECK_TIME
| CHECKSUM | PARTITION_COMMENT | NODEGROUP | TABLESPACE_NAME |
+---------------+--------------+------------+----------------+-------------------
+----------------------------+-------------------------------+------------------
+---------------------+----------------------+-------------------------
+-----------------------+------------+----------------+-------------
+-----------------+--------------+-----------+---------------------
+---------------------+------------+----------+-------------------+-----------
+-----------------+
| def | library | b2 | p0 | NULL |
1 | NULL | RANGE | NULL |
`pyear` | NULL | 2000 | 1
| 16384 | 16384 | 0 | 0 | 0 | 2020-
10-29 12:58:30 | 2020-10-29 12:58:45 | NULL | NULL | |
default | NULL |
| def | library | b2 | p1 | NULL |
2 | NULL | RANGE | NULL |
`pyear` | NULL | 2005 | 1
| 16384 | 16384 | 0 | 0 | 0 | 2020-
10-29 12:58:30 | 2020-10-29 12:58:45 | NULL | NULL | |
default | NULL |
| def | library | b2 | p2 | NULL |
3 | NULL | RANGE | NULL |
`pyear` | NULL | 2010 | 2
| 8192 | 16384 | 0 | 0 | 0 | 2020-
10-29 12:58:30 | 2020-10-29 12:58:49 | NULL | NULL | |
default | NULL |
+---------------+--------------+------------+----------------+-------------------
+----------------------------+-------------------------------+------------------
+---------------------+----------------------+-------------------------
+-----------------------+------------+----------------+-------------
+-----------------+--------------+-----------+---------------------
+---------------------+------------+----------+-------------------+-----------
+-----------------+
3 rows in set (0.11 sec)

You might also like