0% found this document useful (0 votes)
45 views2 pages

Library

The document contains SQL commands that create tables for a basic library database including tables for publishers, books, book authors, library branches, book copies in each branch, user cards, and book lending. Data is inserted into each table including publishers, books, library branches, book copies, user cards, and book lending records. Queries are provided to select related data from the joined tables and find heavy users by card number with more than 3 books checked out in a date range.

Uploaded by

Pruthvi Raj bl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views2 pages

Library

The document contains SQL commands that create tables for a basic library database including tables for publishers, books, book authors, library branches, book copies in each branch, user cards, and book lending. Data is inserted into each table including publishers, books, library branches, book copies, user cards, and book lending records. Queries are provided to select related data from the joined tables and find heavy users by card number with more than 3 books checked out in a date range.

Uploaded by

Pruthvi Raj bl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

create table publisher

(
name varchar(20),
address varchar(20),
phone int,
primary key(name)
);

create table book


(
book_id int,
title varchar(20),
pub_year varchar(20),
publisher_name varchar(20) references publisher(name),
primary key(book_id)
);

create table book_authors


(
author_name varchar(20),
book_id int references book(book_id) on delete cascade,
primary key(author_name,book_id)
);

create table library_branch


(
branch_id int,
branch_name varchar(20),
address varchar(20),
primary key(branch_id)
);

create table book_copies


(
no_of_copies int,
book_id int references book(book_id) on delete cascade,
branch_id int references library_branch(branch_id),
primary key(book_id,branch_id)
);

create table card


(
card_no int,
primary key(card_no)
);

create table book_lending


(
date_out date,
due_date date,
book_id int references book(book_id) on delete cascade,
branch_id int references library_branch(branch_id),
card_no int references card(card_no),
primary key(book_id,branch_id,card_no)
);

insert into publisher values('macgraw_hill','bangalore',9980182485);


insert into publisher values('pearson','mysore',9901699854);
insert into publisher values('grupo_planeta','mumbai',9980856598);
insert into publisher values('legendary','chennai',6363265499);
insert into publisher values('gardenhouse','bangalore',8989565623);

insert into book values(1,'dbms','jan-2017','macgraw_hill');


insert into book values(2,'adbms','apr-2016','macgraw_hill');
insert into book values(3,'co','mar-2016','pearson');
insert into book values(4,'cn','may-2017','grupo_planeta');
insert into book values(5,'os','jun-2017','pearson');

insert into library_branch values(10,'rajaji_nagar','bangalore');


insert into library_branch values(11,'rnsit','bangalore');
insert into library_branch values(12,'bms','bangalore');
insert into library_branch values(13,'nitte','udupi');
insert into library_branch values(14,'manipal','mangalore');

insert into book_copies values(10,1,10);


insert into book_copies values(5,1,11);
insert into book_copies values(2,2,12);
insert into book_copies values(5,2,13);
insert into book_copies values(7,3,14);
insert into book_copies values(1,4,10);
insert into book_copies values(3,5,11);

insert into card values(100);


insert into card values(101);
insert into card values(102);
insert into card values(103);
insert into card values(104);

insert into book_lending values('01-jan-2017','02-jan-2017',1,10,101);


insert into book_lending values('14-feb-2017','22-feb-2017',2,12,101);
insert into book_lending values('11-mar-2017','10-apr-2017',3,14,101);
insert into book_lending values('23-may-2017','07-may-2017',4,10,101);
insert into book_lending values('15-jun-2017','09-jun-2017',5,11,102);

select
B.book_id,B.title,B.publisher_name,A.author_name,C.no_of_copies,L.branch_id,L.branc
h_name
from book B,book_authors A,book_copies C,library_branch L
where B.book_id=A.book_id and
B.book_id=C.book_id and
C.branch_id=L.branch_id;

select card_no
from book_lending
where date_out between '01-jan-2017' and '01-jun-2017'
group by card_no
having count(*)>3;

select *from book;


select *from book_authors;
select *from book_copies;
select *from book_lending;

You might also like