Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

21CSL58 – DBMS LAB WITH MINI PROJECT

Entity Relationship Diagram

◻ An Entity–relationship model (ER model) describes the


structure of a database with the help of a diagram, which is
known as Entity Relationship Diagram (ER Diagram).
◻ An ER model is a design or blueprint of a database that can later
be implemented as a database.
◻ The main components of E-R model are: entity set, attributes and
relationship set.
Consider the following schema for a Library Database:
BOOK (Book_id, Title, Publisher_Name, Pub_Year)
BOOK_AUTHORS (Book_id, Author_Name)
PUBLISHER (Name, Address, Phone)
BOOK_COPIES (Book_id, Branch_id, No-of_Copies)
BOOK_LENDING (Book_id, Branch_id, Card_No,
Date_Out, Due_Date)
LIBRARY_BRANCH (Branch_id, Branch_Name, Address)
create table publisher
(
name varchar(12),
address varchar(25),
phone int,
primary key(name)
);
◻ insert into publisher values('mcgrawhill','Bangalore',9480506312);
◻ insert into publisher values('pearson','New Delhi',9785642365);
◻ insert into publisher values('random house','Hydrabad',8796452368);
◻ insert into publisher values('sapna','Chennai',8947589632);
◻ insert into publisher values('oxford','Bangalore',9785642315);
create table book
(
book_id int,
title varchar(15),
publisher_name varchar(10),
pub_year int,
primary key(book_id),
foreign key(publisher_name) references
publisher(name) on delete cascade
);
insert into book values(1,'DBMS','mcgrawhill',2017);
insert into book values(2,'ADBMS','mcgrawhill',2016);
insert into book values(3,'CN','pearson',2016);
insert into book values(4,'CG','oxford',2015);
insert into book values(5,'OS','pearson',2016);
create table book_authors
(
book_id int,
author_name varchar(15),
primary key(book_id,author_name),
foreign key(book_id) references book(book_id)
on delete cascade
);
insert into book_authors values(1,'navathe');
insert into book_authors values(2,'navathe');
insert into book_authors values(3,'tenenboum');
insert into book_authors values(4,'edward');
insert into book_authors values(5,'galvin');
create table library_branch
(
branch_id int,
branch_name varchar(10),
address varchar(15),
primary key(branch_id)
);
insert into library_branch values(10,'RR nagar','Bangalore');
insert into library_branch values(11,'Manipal','Bangalore');
insert into library_branch values(12,'ACIT','Bangalore');
insert into library_branch values(13,'Rajajnagar','Bangalore');
insert into library_branch values(14,'Acharya','Mangalore');
create table book_copies
(
book_id int,
branch_id int,
no_of_copies int,
primary key(book_id, branch_id),
foreign key(book_id) references book(book_id) on delete cascade,
foreign key(branch_id) references library_branch(branch_id) on
delete cascade
);
insert into book_copies values(1,10,10);
insert into book_copies values(1,11,5);
insert into book_copies values(2,12,2);
insert into book_copies values(2,13,5);
insert into book_copies values(3,14,7);
insert into book_copies values(5,10,1);
insert into book_copies values(4,11,3);
create table book_lending1
(
book_id int,
branch_id int,
card_no int,
date_out date,
due_date date,
primary key(book_id,branch_id,card_no),
foreign key(book_id) references book(book_id) on delete
cascade,
foreign key(branch_id) references
library_branch(branch_id) on delete cascade
);
insert into book_lending1 values(1,10,101,'2017-01-01','2017-06-01');
insert into book_lending1 values(3,14,101,'2017-01-11','2017-03-11');
insert into book_lending1 values(2,13,101,'2017-02-17','2017-04-21');
insert into book_lending1 values(4,11,101,'2017-03-15','2017-07-15');
insert into book_lending1 values(1,11,104,'2017-04-12','2017-05-12');
1. Retrieve details of all books in the library –
id, title, name of publisher, authors, number of
copies in each Programme, etc.
select b.book_id, b.title, b.publisher_name, a.author_name,
c.no_of_copies, c.branch_id
from book b,book_authors a,book_copies c
where b.book_id=a.book_id and
b.book_id=c.book_id;
2.Get the particulars of borrowers who have borrowed more than

three books, but from Jan 2017 to Jun 2017.


select card_no
from book_lending
where date_out between '2017-01-01' and '2017-06-30'
group by card_no
having count(*)>3;
3.Delete a book in BOOK table. Update the contents of other
tables to reflect this data manipulation operation.
delete from book
where book_id=3;

Additonal Query:

Write a SQL command to list the name of the


publisher who have published more than 2 books.
4. Partition the BOOK table based on year of publication.
Demonstrate its working with a simple query.
create table bookpartition
(
book_id int,
pub_year int,
publisher_name varchar(10))
partition by range(pub_year)
(partition p0 values less than(2000),
partition p1 values less than(2005),
partition p2 values less than(2010));

insert into bookpartition values('1',1998, 'vikas');


insert into bookpartition values('3',1999, 'subash');
insert into bookpartition values('2',2002, 'pearson');
insert into bookpartition values('5',2005, 'vikas');
insert into bookpartition values('7',2008, 'subash');
insert into bookpartition values('8',2007, 'pearson');
select * from bookpartition;
select * from bookpartition partition(p0);
select * from bookpartition partition(p1);
select * from bookpartition partition(p2);
5.Create a view of all books and its number of copies
that are currently available in the Library.
create view book_available as
select b.book_id,b.title,c.no_of_copies
from book_copies c,book b,library_branch l
where b.book_id=c.book_id and
c.branch_id=l.branch_id;

You might also like