Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Exp 9

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

-- 1

CREATE TABLE PUBLISHER

(NAME VARCHAR(20) PRIMARY KEY,

PHONE VARCHAR(20),

ADDRESS VARCHAR(20));

-- 1

INSERT INTO PUBLISHER VALUES('MCGRAW-HILL',9191919191,'BANGALORE');

INSERT INTO PUBLISHER VALUES('PEARSON',8181818181,'NEWDELHI');

INSERT INTO PUBLISHER VALUES('RANDOM HOUSE',7171717171,'HYDERABAD');

INSERT INTO PUBLISHER VALUES('LIVRE',6161616161,'CHENNAI');

INSERT INTO PUBLISHER VALUES('PLANETA',5151515151,'BANGALORE');

SELECT * FROM PUBLISHER;

-- 2

CREATE TABLE BOOK

(BID VARCHAR(10) PRIMARY KEY,

TITLE VARCHAR(20),

PUBYEAR DATE,

PUBNAME varchar(20),

FOREIGN KEY (PUBNAME) REFERENCES PUBLISHER(NAME) ON DELETE CASCADE);

-- 2

INSERT INTO BOOK VALUES(1,'DBMS','2017-01-10','MCGRAW-HILL');


INSERT INTO BOOK VALUES(2,'ADBMS','2016-06-12','MCGRAW-HILL');

INSERT INTO BOOK VALUES(3,'CN','2016-09-24','PEARSON');

INSERT INTO BOOK VALUES(4,'CG','2015-09-01','PLANETA');

INSERT INTO BOOK VALUES(5,'OS','2016-05-31','PEARSON');

SELECT * FROM BOOK;

-- 3

CREATE TABLE BOOK_AUTHORS

(ANAME VARCHAR(20),

BOOK_ID VARCHAR(10),

FOREIGN KEY (BOOK_ID) REFERENCES BOOK(BID) ON DELETE CASCADE);

-- 3

INSERT INTO BOOK_AUTHORS VALUES('NAVATHE',1);

INSERT INTO BOOK_AUTHORS VALUES('NAVATHE',2);

INSERT INTO BOOK_AUTHORS VALUES('TANENBAUM',3);

INSERT INTO BOOK_AUTHORS VALUES('EDWARD ANGEL',4);

INSERT INTO BOOK_AUTHORS VALUES('GALVIN',5);

SELECT * FROM BOOK_AUTHORS;

-- 4

CREATE TABLE LIBRARY_BRANCH

(BR_ID VARCHAR(10) PRIMARY KEY,


BNAME VARCHAR(50),

ADDRESS VARCHAR(50));

-- 4

INSERT INTO LIBRARY_BRANCH VALUES(10,'RR NAGAR','BANGALORE');

INSERT INTO LIBRARY_BRANCH VALUES(11,'RNSIT','BANGALORE');

INSERT INTO LIBRARY_BRANCH VALUES(12,'RAJAJI NAGAR','BANGALORE');

INSERT INTO LIBRARY_BRANCH VALUES(13,'NITTE','MANGALORE');

INSERT INTO LIBRARY_BRANCH VALUES(14,'MANIPAL','UDUPI');

SELECT * FROM LIBRARY_BRANCH;

-- 5

CREATE TABLE BOOK_COPIES

(NOOFCOPIES INTEGER,

BID VARCHAR(10),

FOREIGN KEY (BID) REFERENCES BOOK(BID) ON DELETE CASCADE,

BRANCHID VARCHAR(10),

FOREIGN KEY (BRANCHID) REFERENCES LIBRARY_BRANCH(BR_ID) ON DELETE CASCADE);

-- 5

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,5,10);

INSERT INTO BOOK_COPIES VALUES(3,4,11);

SELECT * FROM BOOK_COPIES;

-- 6

CREATE TABLE CARD

(CARDNO VARCHAR(10) PRIMARY KEY);

-- 6

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);

SELECT * FROM CARD;

-- 7

CREATE TABLE BOOKLENDING

(DATEOUT DATE,

DUEDATE DATE,

BID VARCHAR(10),

FOREIGN KEY (BID) REFERENCES BOOK(BID) ON DELETE CASCADE,


BRANCH_ID VARCHAR(10),

FOREIGN KEY (BRANCH_ID) REFERENCES LIBRARY_BRANCH(BR_ID) ON DELETE CASCADE,

CARDNO VARCHAR(10),

FOREIGN KEY (CARDNO) REFERENCES CARD(CARDNO) ON DELETE CASCADE,

PRIMARY KEY (BID,BRANCH_ID,CARDNO));

-- 7

INSERT INTO BOOKLENDING VALUES('2017-01-01','2017-06-01',1,10,101);

INSERT INTO BOOKLENDING VALUES('2017-01-11','2017-03-11',3,14,101);

INSERT INTO BOOKLENDING VALUES('2017-02-21','2017-04-21',2,13,101);

INSERT INTO BOOKLENDING VALUES('2017-03-15','2017-07-15',4,11,101);

INSERT INTO BOOKLENDING VALUES('2017-04-12','2017-05-12',1,11,104);

SELECT * FROM BOOKLENDING;

-- QUERIES

-- QUERY 1

SELECT B.BID, B.TITLE, B.PUBNAME, A.ANAME, C.NOOFCOPIES, L.BR_ID

FROM BOOK B, BOOK_AUTHORS A, BOOK_COPIES C, LIBRARY_BRANCH L

WHERE B.BID=A.BOOK_ID

AND B.BID=C.BID

AND L.BR_ID=C.BRANCHID;
-- QUERY 2

SELECT CARDNO

FROM BOOKLENDING

WHERE DATEOUT BETWEEN '2017-01-01' AND '2017-06-01'

GROUP BY CARDNO

HAVING COUNT(*)>3;

-- QUERY 3

DELETE FROM BOOK

WHERE BID = 3;

SELECT * FROM BOOK;

-- QUERY 4

CREATE VIEW VPUBLICATION AS

SELECT PUBYEAR

FROM BOOK;

SELECT * FROM VPUBLICATION;

-- QUERY 5

CREATE VIEW VBOOKS AS

SELECT B.BID, B.TITLE, C.NOOFCOPIES


FROM BOOK B, BOOK_COPIES C, LIBRARY_BRANCH L

WHERE B.BID=C.BID

AND C.BRANCHID=L.BR_ID;

SELECT* FROM VBOOKS;

You might also like