You are on page 1of 27

DBMS LAB

CSE, CITECH Page 1



PROGRAM -1
Consider the Insurance database given below. The primary keys are underlined and the data types are
specified:
PERSON (driver id #: String, name: string, address: string)
CAR (regno: string, model: string, year: int)
ACCIDENT (report-number: int, accd-date: date, location: string)
OWNS (driver-id #:string, Regno:string)
PARTICIPATED (driver-id: string, Regno:string, report-number:int, damage amount:int)
(i) Create the above tables by properly specifying the primary keys and the foreign keys.
(ii) Enter at least five tuples for each relation.
(iii) Demonstrate how you
a. Update the damage amount to 25000 for the car with a specific Regno in the ACCIDENT table
with report number 12.
b. Add a new accident to the database.
(iv) Find the total number of people who owned cars that were involved in accidents in 2008.
(v) Find the number of accidents in which cars belonging to a specific model were involved.
(vi) Generate suitable reports.
(vii) Create suitable front end for querying and displaying the results.

CREATE TABLE person
(
driverid VARCHAR(5),
name VARCHAR(30),
address VARCHAR(50),
CONSTRAINT pk_dridper PRIMARY KEY (driverid)
);

CREATE TABLE car
(
regno VARCHAR(5),
model VARCHAR(25),
year INTEGER,
CONSTRAINT pk_rgnocar PRIMARY KEY (regno)
);

CREATE TABLE accident
(
reportno INTEGER,
DBMS LAB

CSE, CITECH Page 2

date1 date,
location VARCHAR(30),
CONSTRAINT pk_rptnoacc PRIMARY KEY (reportno)
);

CREATE TABLE owns
(
driverid VARCHAR(5),
regno VARCHAR(5),
CONSTRAINT fk_dridown FOREIGN KEY(driverid) REFERENCES person(driverid) ON DELETE
CASCADE,
CONSTRAINT fk_rgnoown FOREIGN KEY(regno) REFERENCES car(regno) ON DELETE CASCADE,
CONSTRAINT pk_own PRIMARY KEY(driverid,regno)
);

CREATE TABLE participated
(
driverid VARCHAR(5),
regno VARCHAR(5),
reportno INTEGER,
damages INTEGER,
CONSTRAINT fk_dridpart FOREIGN KEY(driverid) REFERENCES person(driverid) ON DELETE
CASCADE,
CONSTRAINT fk_rgnopart FOREIGN KEY(regno) REFERENCES car(regno) ON DELETE CASCADE,
CONSTRAINT fk_rptno FOREIGN KEY(reportno) REFERENCES accident(reportno) ON DELETE
CASCADE,
CONSTRAINT pk_part PRIMARY KEY(driverid,regno,reportno)
);


======================================
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INSERT INTO person VALUES('&driverid','&name','&address');
INSERT INTO car VALUES('&regno','&model','&year');
INSERT INTO accident VALUES('&reportno','&date1','&location');
INSERT INTO owns VALUES('&driverid','&regno');
INSERT INTO participated VALUES('&driverid','&regno','&reportno','&damages');
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

INSERT INTO person VALUES (1,'House','Carolina');
INSERT INTO person VALUES (2,'Dexter','Hampshire');
INSERT INTO person VALUES (3,'Elena','USA');
INSERT INTO person VALUES (4,'Steffan','Boston');
INSERT INTO person VALUES (5,'Daemon','Scotland');
DBMS LAB

CSE, CITECH Page 3



INSERT INTO car VALUES ('reg1','Mercedes',2007);
INSERT INTO car VALUES ('reg2','Porsche',2005);
INSERT INTO car VALUES ('reg3','Rolls Royce',2006);
INSERT INTO car VALUES ('reg4','Ferrari',1999);
INSERT INTO car VALUES ('reg5','Bentley',2000);
INSERT INTO car VALUES ('reg6','Maybach',2007);


INSERT INTO accident VALUES (1,'1-Jan-2008','Dakota');
INSERT INTO accident VALUES (2,'28-Mar-2008','London');
INSERT INTO accident VALUES (3,'2-Dec-2008','Nagpur');
INSERT INTO accident VALUES (4,'5-Jan-2007','Bangalore');
INSERT INTO accident VALUES (5,'26-Jan-2007','China');
INSERT INTO accident VALUES (12,'4-Feb-2007','Japan');


INSERT INTO owns VALUES (1,'reg1');
INSERT INTO owns VALUES (2,'reg2');
INSERT INTO owns VALUES (2,'reg5');
INSERT INTO owns VALUES (3,'reg3');
INSERT INTO owns VALUES (4,'reg4');
INSERT INTO owns VALUES (5,'reg6');


INSERT INTO participated VALUES (1,'reg1',1,500);
INSERT INTO participated VALUES (2,'reg2',1,2000);
INSERT INTO participated VALUES (3,'reg3',1,1000);
INSERT INTO participated VALUES (1,'reg1',2,1500);
INSERT INTO participated VALUES (4,'reg5',2,800);
INSERT INTO participated VALUES (5,'reg6',3,750);
INSERT INTO participated VALUES (2,'reg5',3,600);
INSERT INTO participated VALUES (1,'reg1',3,200);
INSERT INTO participated VALUES (5,'reg6',4,1000);
INSERT INTO participated VALUES (2,'reg2',5,1200);
INSERT INTO participated VALUES (3,'reg3',12,10000);
INSERT INTO participated VALUES (2,'reg1',12,5000);

=====================================

UPDATE PARTICIPATED
SET DAMAGES=25000
WHERE REGNO='reg1' AND REPORTNO=12
DBMS LAB

CSE, CITECH Page 4

--------------------
INSERT INTO accident VALUES (7,'16-mar-2007','Chicago');
INSERT INTO participated VALUES (2,'reg1',7,15000);

=========================================
SELECT COUNT(DISTINCT p.driverid) AS "Ppl Owning Cars of Accidnt-08" FROM accident a,owns
o,participated p
WHERE date1 LIKE '%08' AND a.reportno=p.reportno
AND p.regno=o.regno AND o.driverid=p.driverid;


==============================================
SELECT COUNT(*) FROM car c,participated p WHERE model='Mercedes' AND c.regno=p.regno;

********************

SELECT CAR.MODEL AS "Model",COUNT(CAR.REGNO) AS "No. of Accidents"
FROM CAR,PARTICIPATED
WHERE CAR.REGNO=PARTICIPATED.REGNO
GROUP BY CAR.MODEL;

~~~~~~~~~~~~~~~~~~~~~~~~
DROP TABLE PARTICIPATED;
DROP TABLE OWNS;
DROP TABLE ACCIDENT;
DROP TABLE CAR;
DROP TABLE PERSON;





PROGRAM -2
Consider the following relations for an order processing database application in a company:
CUSTOMER (cust #: int , cname: string, city: string)
ORDER (order #: int, odate: date, cust #: int, ord-Amt: int)
ORDER ITEM (order #: int, item #: int, qty: int)
ITEM (item # : int, unit price: int)
DBMS LAB

CSE, CITECH Page 5

SHIPMENT (order #: int, warehouse#: int, ship-date: date)
WAREHOUSE (warehouse #: int, city: string)
(i) Create the above tables by properly specifying the primary keys and the foreign keys.
(ii) Enter at least five tuples for each relation.
(iii) Produce a listing: CUSTNAME, #oforders, AVG_ORDER_AMT, where the middle column is the
total numbers of orders by the customer and the last column is the average order amount for
that customer.
(iv) List the order# for orders that were shipped from all the warehouses that the company has in
a specific city.
(v) Demonstrate the deletion of an item from the ITEM table and demonstrate a method of
handling the rows in the ORDER_ITEM table that contain this particular item.
(vi) Generate suitable reports.
(vii) Create suitable front end for querying and displaying the results.

CREATE TABLE customer
(
custnum INTEGER,
custname VARCHAR2(10) NOT NULL,
city VARCHAR2(15) NOT NULL,
CONSTRAINT pk_cust PRIMARY KEY(custnum)
);

CREATE TABLE custorder
(
ordrnum INTEGER,
ordrdate DATE NOT NULL,
custnum INTEGER NOT NULL,
ordramt INTEGER,
CONSTRAINT fk_cusnumco FOREIGN KEY(custnum) REFERENCES customer(custnum) ON DELETE
CASCADE,
CONSTRAINT pk_custordr PRIMARY KEY(ordrnum)
);

CREATE TABLE item
(
itemnum INTEGER,
unitprice INTEGER NOT NULL,
CONSTRAINT pk_item PRIMARY KEY(itemnum)
);
DBMS LAB

CSE, CITECH Page 6


CREATE TABLE order_item
(
ordrnum INTEGER,
itemnum INTEGER,
qty INTEGER check(qty>0),
CONSTRAINT fk_ordnmoi FOREIGN KEY(ordrnum) REFERENCES custorder(ordrnum) ON DELETE
CASCADE,
CONSTRAINT fk_itmnmoi FOREIGN KEY(itemnum) REFERENCES item(itemnum) ON DELETE
CASCADE,
CONSTRAINT pk_oi PRIMARY KEY(ordrnum,itemnum)
);

CREATE TABLE warehouse
(
warehousenum INTEGER,
city VARCHAR2(15) NOT NULL,
CONSTRAINT pk_wrehs PRIMARY KEY(warehousenum)
);

CREATE TABLE shipment
(
ordrnum INTEGER,
warehousenum INTEGER,
shipdate DATE NOT NULL,
CONSTRAINT fk_ordrnmsm FOREIGN KEY(ordrnum) REFERENCES custorder(ordrnum) ON DELETE
CASCADE,
CONSTRAINT fk_whnmsm FOREIGN KEY(warehousenum) REFERENCES warehouse(warehousenum) ON
DELETE CASCADE,
CONSTRAINT pk_shpmt PRIMARY KEY(ordrnum,warehousenum)
);

====================================================
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INSERT INTO customer VALUES('&custnum','&custname','&city');
INSERT INTO custorder(ordrnum,ordrdate,custnum) VALUES('&ordrnum','&ordrdate','&custnum');
INSERT INTO item VALUES('&itemnum','&unitprice');
INSERT INTO order_item VALUES('&ordrnum','&itemnum','&qty');
INSERT INTO shipment VALUES('&ordrnum','&warehousenum','&shipdate');
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

INSERT INTO customer VALUES(1,'Mayuri','MP');
INSERT INTO customer VALUES(2,'Sagar','Mysore');
INSERT INTO customer VALUES(3,'Strider','chennai');
DBMS LAB

CSE, CITECH Page 7

INSERT INTO customer VALUES(4,'Mallika','Mumbai');
INSERT INTO customer VALUES(5,'Harsh','Kolkata');

INSERT INTO custorder(ordrnum,ordrdate,custnum) VALUES(1,'1-Jan-2006',1);
INSERT INTO custorder(ordrnum,ordrdate,custnum) VALUES(2,'26-Mar-2006',2);
INSERT INTO custorder(ordrnum,ordrdate,custnum) VALUES(3,'12-Jun-2006',1);
INSERT INTO custorder(ordrnum,ordrdate,custnum) VALUES(4,'15-Sep-2006',3);
INSERT INTO custorder(ordrnum,ordrdate,custnum) VALUES(5,'5-Jan-2007',4);
INSERT INTO custorder(ordrnum,ordrdate,custnum) VALUES(6,'10-Jan-2007',4);
INSERT INTO custorder(ordrnum,ordrdate,custnum) VALUES(7,'3-Mar-2007',5);

INSERT INTO item VALUES(1,500);
INSERT INTO item VALUES(2,300);
INSERT INTO item VALUES(3,2500);
INSERT INTO item VALUES(4,800);
INSERT INTO item VALUES(5,700);

INSERT INTO order_item VALUES(1,1,40);
INSERT INTO order_item VALUES(2,1,20);
INSERT INTO order_item VALUES(3,3,2);
INSERT INTO order_item VALUES(5,3,1);
INSERT INTO order_item VALUES(4,2,30);
INSERT INTO order_item VALUES(6,4,3);
INSERT INTO order_item VALUES(7,5,5);

UPDATE CUSTORDER SET ORDRAMT
=(SELECT SUM(OI.qty*I.unitprice)
FROM ORDER_ITEM OI,ITEM I
WHERE OI.ORDRNUM=CUSTORDER.ORDRNUM
AND I.itemnum=OI.itemnum);


INSERT INTO warehouse VALUES(100,'Pune');
INSERT INTO warehouse VALUES(101,'Chennai');
INSERT INTO warehouse VALUES(102,'Mumbai');
INSERT INTO warehouse VALUES(103,'Kolkata');
INSERT INTO warehouse VALUES(104,'Mysore');

INSERT INTO shipment VALUES(1,100,'3-Jan-2006');
INSERT INTO shipment VALUES(2,100,'28-Mar-2006');
INSERT INTO shipment VALUES(3,101,'13-Jun-2006');
INSERT INTO shipment VALUES(4,102,'18-Sep-2006');
INSERT INTO shipment VALUES(5,103,'11-Jan-2007');
INSERT INTO shipment VALUES(6,104,'13-Jan-2007');
DBMS LAB

CSE, CITECH Page 8

INSERT INTO shipment VALUES(7,103,'3-Mar-2007');


========================================================
SELECT c.CUSTNAME AS "Name of Cust",COUNT(CO.ORDRNUM) AS "Number of Orders",AVG(CO.ORDRAMT) AS
"Avg Amount"
FROM CUSTOMER c,CUSTORDER CO
WHERE c.CUSTNUM=CO.CUSTNUM
GROUP BY c.CUSTNAME;

========================================================

SELECT ordrnum AS "Order Number",warehousenum AS "Warehouse Number"
FROM SHIPMENT WHERE warehousenum IN
(SELECT warehousenum FROM WAREHOUSE WHERE city='Pune');

=========================================================

DELETE FROM ITEM WHERE itemnum=1;

=========================================================



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DROP TABLE SHIPMENT;
DROP TABLE WAREHOUSE;
DROP TABLE ORDER_ITEM;
DROP TABLE ITEM;
DROP TABLE CUSTORDER;
DROP TABLE CUSTOMER;


Program -3
Consider the following database of student enrollment in courses & books adopted for each course:
STUDENT (regno: string, name: string, major: string, bdate:date)
COURSE (course #:int, cname:string, dept:string)
ENROLL ( regno:string, course#:int, sem:int, marks:int)
BOOK _ ADOPTION (course# :int, sem:int, book-ISBN:int)
DBMS LAB

CSE, CITECH Page 9

TEXT (book-ISBN:int, book-title:string, publisher:string, author:string)
(i) Create the above tables by properly specifying the primary keys and the foreign keys.
(ii) Enter at least five tuples for each relation.
(iii) Demonstrate how you add a new text book to the database and make this book be adopted
by some department.
(iv) Produce a list of text books (include Course #, Book-ISBN, Book-title) in the alphabetical
order for courses offered by the CS department that use more than two books.
(v) List any department that has all its adopted books published by a specific publisher.
(vi) Generate suitable reports.
(vii) Create suitable front end for querying and displaying the results.

CREATE TABLE student
(
regno VARCHAR2(5),
name VARCHAR2(10) NOT NULL,
major VARCHAR2(10) NOT NULL,
bdate date NOT NULL,
CONSTRAINT pk_rgnostu PRIMARY KEY(regno)
);

CREATE TABLE course
(
coursenum INTEGER,
cname VARCHAR2(10) NOT NULL,
dept VARCHAR2(10) NOT NULL,
CONSTRAINT pk_coucourse PRIMARY KEY(coursenum)
);

CREATE TABLE enroll
(
regno VARCHAR2(5),
coursenum INTEGER,
sem INTEGER,
marks INTEGER NOT NULL,
CONSTRAINT fk_rgnoenro FOREIGN KEY(regno) REFERENCES student(regno) ON DELETE
CASCADE,
CONSTRAINT fk_couenro FOREIGN KEY(coursenum) REFERENCES course(coursenum) ON DELETE
CASCADE,
CONSTRAINT pk_enroll PRIMARY KEY(regno,coursenum,sem)
);
DBMS LAB

CSE, CITECH Page 10


CREATE TABLE text
(
isbn INTEGER,
title VARCHAR2(15) NOT NULL,
publisher VARCHAR2(10) NOT NULL,
author VARCHAR2(10) NOT NULL,
CONSTRAINT pk_isbntex PRIMARY KEY(isbn)
);

CREATE TABLE book_adopt
(
coursenum INTEGER,
sem INTEGER,
isbn INTEGER NOT NULL,
CONSTRAINT fk_coubooadpt FOREIGN KEY(coursenum) REFERENCES course(coursenum) ON
DELETE CASCADE,
CONSTRAINT fk_isbnboadpt FOREIGN KEY(isbn) REFERENCES text(isbn) ON DELETE CASCADE,
CONSTRAINT pk_booadpt PRIMARY KEY(coursenum,sem)
);



=======================================================
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INSERT INTO student VALUES('&regno','&name','&major','&bdate');
INSERT INTO course VALUES('&coursenum','&cname','&dept');
INSERT INTO enroll VALUES('&regno','&coursenum','&sem','&marks');
INSERT INTO text VALUES('&isbn','&title','&publisher','&author');
INSERT INTO book_adopt VALUES('&coursenum','&sem','&isbn');
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

INSERT INTO student VALUES('cs42','Robby','cse','17-dec-1991');
INSERT INTO student VALUES('cs48','Payal','cse','02-sep-1990');
INSERT INTO student VALUES('ec26','Christina','cse','16-aug-1991');
INSERT INTO student VALUES('ee37','Sam','ise','28-may-1991');
INSERT INTO student VALUES('is48','Ricky','ise','28-may-1991');

INSERT INTO course VALUES(1,'.net','Computer');
INSERT INTO course VALUES(2,'J2EE','Computer');
INSERT INTO course VALUES(3,'OpenSource','Infosci');
INSERT INTO course VALUES(4,'FS','Infosci');
INSERT INTO course VALUES(5,'Oracle','Computer');

DBMS LAB

CSE, CITECH Page 11

INSERT INTO enroll VALUES('cs42',1,6,98);
INSERT INTO enroll VALUES('cs48',2,6,97);
INSERT INTO enroll VALUES('ec26',5,5,50);
INSERT INTO enroll VALUES('is48',3,7,90);
INSERT INTO enroll VALUES('ee37',4,4,80);
INSERT INTO enroll VALUES('cs48',1,6,35);

INSERT INTO text VALUES(101,'Progmer Padgm','Apress','Marcus');
INSERT INTO text VALUES(102,'Think in C++','Apress','Barbara');
INSERT INTO text VALUES(103,'Oracle','McGraw','Emily Chen');
INSERT INTO text VALUES(104,'.net','Apress','Martin');
INSERT INTO text VALUES(105,'J2EE','Pearson','Smith');

INSERT INTO book_adopt VALUES(1,6,101);
INSERT INTO book_adopt VALUES(1,3,104);
INSERT INTO book_adopt VALUES(1,5,105);
INSERT INTO book_adopt VALUES(2,3,104);
INSERT INTO book_adopt VALUES(2,5,105);
INSERT INTO book_adopt VALUES(2,6,103);
INSERT INTO book_adopt VALUES(3,5,102);
INSERT INTO book_adopt VALUES(4,4,104);
INSERT INTO book_adopt VALUES(5,7,105);



=======================================================
INSERT INTO TEXT VALUES(106,'Digit','Spinster','Rab');
INSERT INTO BOOK_ADOPT VALUES(5,3,106);

=======================================================

SELECT c.coursenum AS "Course Num",t.ISBN,t.title AS "Book Title"
FROM COURSE c,TEXT t,book_adopt b
WHERE c.COURSEnum=b.COURSEnum AND
t.ISBN=b.ISBN AND
c.DEPT='Computer' AND c.COURSEnum IN(SELECT b.COURSEnum
FROM book_adopt b
GROUP BY b.COURSEnum
HAVING COUNT(*)>2)
GROUP BY c.COURSEnum ,t.ISBN,t.TITLE
ORDER BY t.TITLE;

=========================================================

DBMS LAB

CSE, CITECH Page 12

SELECT DISTINCT c.dept AS "Dept. All Books Same Publisher"
FROM course c,book_adopt b, text t
WHERE c.coursenum=b.COURSEnum AND
b.ISBN=t.ISBN AND
t.PUBLISHER='Apress' AND
c.COURSEnum IN( SELECT COURSEnum
FROM book_adopt b,text t
WHERE b.ISBN=t.ISBN
GROUP BY COURSEnum
HAVING count(DISTINCT publisher)=1);

===========================================================

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DROP TABLE BOOK_ADOPT;
DROP TABLE TEXT;
DROP TABLE ENROLL;
DROP TABLE COURSE;
DROP TABLE STUDENT;

PROGRAM -4
The following tables are maintained by a book dealer:
AUTHOR (author-id:int, name:string, city:string, country:string)
PUBLISHER (publisher-id:int, name:string, city:string, country:string)
CATALOG (book-id:int, title:string, author-id:int, publisher-id:int, category-id:int, year:int, price:int)
CATEGORY (category-id:int, description:string)
ORDER-DETAILS (order-no:int, book-id:int, quantity:int)
(i) Create the above tables by properly specifying the primary keys and the foreign keys.
(ii) Enter at least five tuples for each relation.
(iii) Give the details of the authors who have 2 or more books in the catalog and the price of the
books is greater than the average price of the books in the catalog and the year of
publication is after 2000.
(iv) Find the author of the book which has maximum sales.
(v) Demonstrate how you increase the price of books published by a specific publisher by 10%.
(vi) Generate suitable reports.
(vii) Create suitable front end for querying and displaying the results.

DBMS LAB

CSE, CITECH Page 13

CREATE TABLE author
(
authorid INTEGER,
name VARCHAR2(15) NOT NULL,
city VARCHAR2(15) NOT NULL,
country VARCHAR2(15) NOT NULL,
CONSTRAINT pk_authidauth PRIMARY KEY(authorid)
);

CREATE TABLE publisher
(
publisherid INTEGER,
name VARCHAR2(15) NOT NULL,
city VARCHAR2(15) NOT NULL,
country VARCHAR2(15) NOT NULL,
CONSTRAINT pk_pubidpub PRIMARY KEY(publisherid)
);

CREATE TABLE category
(
categoryid INTEGER,
description VARCHAR2(15) NOT NULL,
CONSTRAINT pk_catidcat PRIMARY KEY(categoryid)
);

CREATE TABLE catalog1
(
bookid INTEGER,
title VARCHAR2(15) NOT NULL,
authorid INTEGER NOT NULL,
publisherid INTEGER NOT NULL,
categoryid INTEGER NOT NULL,
year INTEGER NOT NULL,
price INTEGER NOT NULL,
CONSTRAINT fk_authidcat FOREIGN KEY(authorid) REFERENCES author(authorid) ON DELETE
CASCADE,
CONSTRAINT fk_pubidcat FOREIGN KEY(publisherid) REFERENCES publisher(publisherid) ON
DELETE CASCADE,
CONSTRAINT fk_catidcat FOREIGN KEY(categoryid) REFERENCES category(categoryid) ON
DELETE CASCADE,
CONSTRAINT pk_booidcat PRIMARY KEY(bookid)
);

CREATE TABLE orderdetails
DBMS LAB

CSE, CITECH Page 14

(
orderno INTEGER,
bookid INTEGER,
qty INTEGER check (qty>0),
CONSTRAINT fk_booidordet FOREIGN KEY(bookid) REFERENCES catalog1(bookid) ON DELETE
CASCADE,
CONSTRAINT pk_ordrdet PRIMARY KEY(orderno,bookid)
);



=================================================================
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INSERT INTO author VALUES('&authorid','&name','&city','&country');
INSERT INTO publisher VALUES('&publisherid','&name','&city','&country');
INSERT INTO category VALUES('&categoryid','&description');
INSERT INTO catalog1
VALUES('&bookid','&title','&authorid','&publisherid','&categoryid','&year','&price');
INSERT INTO orderdetails VALUES('&orderno','&bookid','&qty');
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

INSERT INTO author VALUES (1,'Uerialc','Bangalore','India');
INSERT INTO author VALUES (2,'Reshu','Bangalore','India');
INSERT INTO author VALUES (3,'Kumar','Taiwan','Japan');
INSERT INTO author VALUES (4,'Gudiya','Washington','USA');
INSERT INTO author VALUES (5,'Hansa','California','Europe');


INSERT INTO publisher VALUES (101,'Pearson','Bangalore','India');
INSERT INTO publisher VALUES (102,'Tata','Mumbai','Australia');
INSERT INTO publisher VALUES (103,'Sapna','Chansity','Europe');
INSERT INTO publisher VALUES (104,'Eco Buddy','Bhilai','Amazon');
INSERT INTO publisher VALUES (105,'OReiley','UK','Kingdom');


INSERT INTO category VALUES (1001,'Computer');
INSERT INTO category VALUES (1002,'Electronics');
INSERT INTO category VALUES (1003,'Maths');
INSERT INTO category VALUES (1004,'Science');
INSERT INTO category VALUES (1005,'Electrical');


INSERT INTO catalog1 VALUES (111,'lib1',1,101,1001,2002,500);
INSERT INTO catalog1 VALUES (112,'lib2',2,102,1002,2000,800);
DBMS LAB

CSE, CITECH Page 15

INSERT INTO catalog1 VALUES (113,'lib3',3,103,1003,2003,200);
INSERT INTO catalog1 VALUES (114,'lib4',4,104,1001,2006,350);
INSERT INTO catalog1 VALUES (115,'lib5',5,105,1004,2007,100);
INSERT INTO catalog1 VALUES (116,'lib6',2,103,1005,2007,600);
INSERT INTO catalog1 VALUES (117,'lib7',2,105,1002,2007,450);
INSERT INTO catalog1 VALUES (118,'lib8',1,101,1001,2002,500);


INSERT INTO orderdetails VALUES (1,111,2);
INSERT INTO orderdetails VALUES (2,112,3);
INSERT INTO orderdetails VALUES (3,111,5);
INSERT INTO orderdetails VALUES (4,113,1);
INSERT INTO orderdetails VALUES (5,114,2);
INSERT INTO orderdetails VALUES (6,115,1);
INSERT INTO orderdetails VALUES (7,114,2);
INSERT INTO orderdetails VALUES (8,113,2);

===================================================
SELECT name AS "Author Name",city AS "City",country AS "Country"
FROM author
WHERE AUTHORID IN (SELECT AUTHORID FROM catalog1 WHERE
YEAR>2000 AND price>(SELECT AVG(price) FROM catalog1)
GROUP BY AUTHORID
HAVING COUNT(*)>=2);
===================================================
SELECT a.AUTHORID AS "Author ID",name AS "Author Name"
FROM author a,catalog1 c
WHERE a.AUTHORID=c.AUTHORID AND
c.BOOKID=(SELECT BOOKID FROM orderdetails GROUP BY BOOKID HAVING
SUM(qty)=(select MAX(SUM(qty)) from orderdetails
GROUP BY BOOKID));

======================================================
UPDATE CATALOG1 SET PRICE=PRICE*1.10
WHERE PUBLISHERID=101

==================================================

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DROP TABLE ORDERDETAILS;
DROP TABLE CATALOG1;
DROP TABLE CATEGORY;
DROP TABLE PUBLISHER;
DROP TABLE AUTHOR;
DBMS LAB

CSE, CITECH Page 16




PROGRAM -5

CREATE TABLE customer
(
custname VARCHAR2(10),
street VARCHAR2(10) not null,
city VARCHAR2(10) not null,
CONSTRAINT pk_cusnmecus PRIMARY KEY(custname)
);

CREATE TABLE branch
(
branchname VARCHAR2(10),
city VARCHAR2(10),
assets REAL,
CONSTRAINT pk_brnamebr PRIMARY KEY(branchname)
);

CREATE TABLE account
(
accno INTEGER,
branchname VARCHAR2(10),
balance REAL,
CONSTRAINT fk_brnameacc FOREIGN KEY(branchname) REFERENCES branch(branchname),
CONSTRAINT pk_accnoacc PRIMARY KEY(accno)
);

CREATE TABLE loan
(
loanno INTEGER,
branchname VARCHAR2(10) NOT NULL,
amt REAL,
CONSTRAINT fk_brnamelon FOREIGN KEY(branchname) REFERENCES branch(branchname) ON
DELETE CASCADE,
CONSTRAINT pk_loalon PRIMARY KEY(loanno)
);

DBMS LAB

CSE, CITECH Page 17

CREATE TABLE depositor
(
custname VARCHAR2(10),
accno INTEGER,
CONSTRAINT fk_accnodepo FOREIGN KEY(accno) REFERENCES account(accno) ON DELETE
CASCADE,
CONSTRAINT fk_cusnmedepo FOREIGN KEY(custname) REFERENCES customer(custname) ON
DELETE CASCADE,
CONSTRAINT pk_depo PRIMARY KEY(custname,accno)
);

CREATE TABLE borrower
(
custname VARCHAR2(10),
loanno INTEGER,
CONSTRAINT fk_cusnmeborr FOREIGN KEY(custname) REFERENCES customer(custname) ON
DELETE CASCADE,
CONSTRAINT fk_lonnoborr FOREIGN KEY(loanno) REFERENCES loan(loanno) ON DELETE
CASCADE,
CONSTRAINT pk_borr PRIMARY KEY(custname,loanno)
);
=============================================================

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INSERT INTO branch VALUES('&branchname','&city','&assets');
INSERT INTO customer VALUES('&custname','&street','&city');
INSERT INTO account VALUES('&accno','&branchname','&balance');
INSERT INTO loan VALUES('&loanno','&branchname','&amt');
INSERT INTO depositor VALUES('&custname','&accno');
INSERT INTO borrower VALUES('&custname','&loanno');
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


INSERT INTO customer VALUES('Ted','Dharampet','Nagpur');
INSERT INTO customer VALUES('Robin','Sadar','Nagpur');
INSERT INTO customer VALUES('Barney','Palce road','Chennai');
INSERT INTO customer VALUES('Lily','Street','Delhi');
INSERT INTO customer VALUES('Marshall','Miramar','Mumbai');
INSERT INTO customer VALUES('Zoey','Jewel','Hyderabad');


INSERT INTO branch VALUES('Children','Nagpur',1200000);
INSERT INTO branch VALUES('Main','Chennai',2000000);
INSERT INTO branch VALUES('Regional','Mumbai',330000);
DBMS LAB

CSE, CITECH Page 18

INSERT INTO branch VALUES('Farmer','Hyderabad',555555);
INSERT INTO branch VALUES('District','Nagpur',9999999);

INSERT INTO account VALUES(1,'Children',25000);
INSERT INTO account VALUES(2,'Main',12000);
INSERT INTO account VALUES(3,'Main',1000);
INSERT INTO account VALUES(4,'Regional',10000);
INSERT INTO account VALUES(5,'District',600000);
INSERT INTO account VALUES(6,'Farmer',50000);

INSERT INTO loan VALUES(1,'Children',5000);
INSERT INTO loan VALUES(2,'Main',1500);
INSERT INTO loan VALUES(3,'Regional',10000);
INSERT INTO loan VALUES(4,'Farmer',3500);
INSERT INTO loan VALUES(5,'District',20000);

INSERT INTO depositor VALUES('Ted',2);
INSERT INTO depositor VALUES('Robin',1);
INSERT INTO depositor VALUES('Robin',5);
INSERT INTO depositor VALUES('Marshall',4);
INSERT INTO depositor VALUES('Barney',3);
INSERT INTO depositor VALUES('Lily',6);
INSERT INTO depositor VALUES('Ted',3);

INSERT INTO borrower VALUES('Ted',2);
INSERT INTO borrower VALUES('Robin',1);
INSERT INTO borrower VALUES('Marshall',3);
INSERT INTO borrower VALUES('Barney',4);
INSERT INTO borrower VALUES('Lily',5);

======================================================

SELECT custname AS "Cust with 2+ Acc Main Branch"
FROM account a,depositor d
WHERE a.accno=d.accno AND branchname='Main'
GROUP BY custname
HAVING COUNT(*)>=2;

=====================================================

SELECT DISTINCT custname AS "Acc At All Branches of City" FROM depositor
WHERE accno IN(SELECT accno FROM account
WHERE branchname IN(SELECT branchname FROM branch
WHERE city='Nagpur'));
DBMS LAB

CSE, CITECH Page 19


=====================================================

DELETE FROM account WHERE branchname IN (SELECT branchname FROM branch WHERE city='Nagpur');

=====================================================


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DROP TABLE BORROWER;
DROP TABLE DEPOSITOR;
DROP TABLE LOAN;
DROP TABLE ACCOUNT;
DROP TABLE CUSTOMER;
DROP TABLE BRANCH;




CREATE TABLE STUDENT(
SNUM INT PRIMARY KEY,
NAME VARCHAR(10),
MAJOR VARCHAR(10),
LEVELS VARCHAR(5),
AGE INT
);

CREATE TABLE FACULTY
(
FID VARCHAR(4) PRIMARY KEY,
FNAME VARCHAR(10),
DEPT_ID INT
);

CREATE TABLE CLASS (
CNAME VARCHAR(10) PRIMARY KEY,
MEETS_AT VARCHAR(10),
ROOM VARCHAR(10),
FID VARCHAR(4),
FOREIGN KEY(FID) REFERENCES FACULTY(FID)
);
DBMS LAB

CSE, CITECH Page 20


CREATE TABLE ENROLLED (
SNUM INT,
CNAME VARCHAR(10) REFERENCES CLASS(CNAME),
FOREIGN KEY(SNUM) REFERENCES STUDENT(SNUM)
);

INSERT IN TO STUDENT VALUES :::
INSERT INTO STUDENT VALUES (1, 'BLAKE', 'CS', 'JR', 20);
INSERT INTO STUDENT VALUES (2, 'JIM', 'EC', 'SR', 19);
INSERT INTO STUDENT VALUES (3, 'JOHN', 'EC', 'SR', 21);
INSERT INTO STUDENT VALUES (4, 'CHRIS', 'EE', 'JR', 20);
INSERT INTO STUDENT VALUES (5, 'JAKE', 'ME', 'SR', 20);


INSERT IN TO FACULTY VALUES :
INSERT INTO FACULTY VALUES (1, 'HARSHITH', 10);
INSERT INTO FACULTY VALUES (2, 'KAMBER', 20);
INSERT INTO FACULTY VALUES (3, 'NAVATHE', 30);

INSERT IN TO CLASS VALUES :
INSERT INTO CLASS VALUES ('2SEM', '10AM', '401', 1);
INSERT INTO CLASS VALUES ('3SEM', '12PM', '128', 2);
INSERT INTO CLASS VALUES ('4SEM', '12PM', '601', 3);
INSERT INTO CLASS VALUES ('6SEM', '2PM', '128', 1);
INSERT INTO CLASS VALUES ('5SEM', '8AM', '401', 1);
INSERT INTO CLASS VALUES ('7SEM', '9AM', '601', 1);

INSERT IN TO ENROLLED VALUES :
INSERT INTO ENROLLED VALUES (1, '4SEM');
INSERT INTO ENROLLED VALUES (2, '6SEM');
INSERT INTO ENROLLED VALUES (3, '3SEM');
INSERT INTO ENROLLED VALUES (4, '2SEM');
INSERT INTO ENROLLED VALUES (5, '5SEM');
INSERT INTO ENROLLED VALUES (2, '4SEM');
INSERT INTO ENROLLED VALUES (3, '4SEM');
INSERT INTO ENROLLED VALUES (1, '3SEM');
INSERT INTO ENROLLED VALUES (1, '2SEM');
INSERT INTO ENROLLED VALUES (4, '6SEM');
INSERT INTO ENROLLED VALUES (5, '3SEM');
INSERT INTO ENROLLED VALUES (1, '5SEM');
INSERT INTO ENROLLED VALUES (5, '4SEM');
INSERT INTO ENROLLED VALUES (4, '4SEM');

Queries:
DBMS LAB

CSE, CITECH Page 21


1. Find the names of all Juniors (level = JR) who are enrolled in a class taught by Prof.
HARSHITH

SELECT DISTINCT S.NAME
FROM STUDENT S, CLASS C, ENROLLED E, FACULTY F
WHERE S.SNUM = E.SNUM AND E.CNAME = C.CNAME AND C.FID = F.FID AND
F.FNAME = 'HARSHITH' AND S.LEVELS = 'JR';

NAME
--------
CHRIS
BLAKE

2. Find the names of all classes that either meet in room R128 or have five or more
Students enrolled.

SELECT C.CNAME
FROM CLASS C
WHERE C.ROOM = 128
OR C.CNAME IN (SELECT E.CNAME
FROM ENROLLED E
GROUP BY E.CNAME
HAVING COUNT (*) > 4);

CNAME
-------
3SEM
4SEM
6SEM

3. Find the names of all students who are enrolled in two classes that meet at the same
time.

SELECT DISTINCT S.NAME
FROM STUDENT S
WHERE S.SNUM IN (SELECT E1.SNUM
FROM ENROLLED E1, ENROLLED E2, CLASS C1, CLASS C2
WHERE E1.SNUM = E2.SNUM AND E1.CNAME != E2.CNAME
AND E1.CNAME = C1.CNAME
AND E2.CNAME = C2.CNAME AND C1.MEETS_AT = C2.MEETS_AT);

NAME
------
JAKE
DBMS LAB

CSE, CITECH Page 22

JOHN
BLAKE

4. Find the names of faculty members who teach in every room in which some class is
taught.

SELECT DISTINCT F.FNAME , FID
FROM FACULTY F
WHERE NOT EXISTS (
( SELECT DISTINCT C.ROOM FROM CLASS C )
MINUS
( SELECT DISTINCT C1.ROOM FROM CLASS C1 WHERE C1.FID = F.FID ));

FNAME FID
---------------
HARSHITH 1

5. Find the names of faculty members for whom the combined enrollment of the courses
that they teach is less than five.

SELECT DISTINCT F.FNAME
FROM FACULTY F
WHERE 5 > (SELECT COUNT (E.SNUM)
FROM CLASS C, ENROLLED E
WHERE C.CNAME = E.CNAME
AND C.FID = F.FID);

FNAME
--------
KAMBER


2.VTU 5th SEM DBMS ::FLIGHT INFORMATION

create table aircraft
(
aid integer primary key,
aname varchar(15),
crange integer
);

create table flights
(
flno integer primary key references aircraft(aid),
DBMS LAB

CSE, CITECH Page 23

fromplace varchar(15),
toplace varchar(15),
distance integer,
departs timestamp,
arrives timestamp,
price integer
);

create table employees
(
eid integer primary key,
ename varchar(15),
salary integer
);

create table certified
(
eid references employees(eid),
aid references aircraft(aid),
primary key(eid,aid)
);


INSERT IN TO AIRCRAFT VALUES::
insert into aircraft values(101,'747',3000);
insert into aircraft values(102,'Boeing',900);
insert into aircraft values(103,'647',800);
insert into aircraft values(104,'Dreamliner',10000);
insert into aircraft values(105,'Boeing',3500);
insert into aircraft values(106,'707',1500);
insert into aircraft values(107,'Dreamliner2',12000);


INSERT IN TO EMPLOYEES VALUES::
insert into employees values(701,'A',50000);
insert into employees values(702,'B',100000);
insert into employees values(703,'C',150000);
insert into employees values(704,'D',90000);
insert into employees values(705,'E',40000);
insert into employees values(706,'F',60000);
insert into employees values(707,'G',70000);


INSERT INTO CERTIFIED VALUES::
insert into certified values(701,101);
DBMS LAB

CSE, CITECH Page 24

insert into certified values(701,102);
insert into certified values(701,106);
insert into certified values(701,105);
insert into certified values(702,104);
insert into certified values(703,104);
insert into certified values(704,104);
insert into certified values(702,107);
insert into certified values(703,107);
insert into certified values(704,107);
insert into certified values(702,101);
insert into certified values(703,105);
insert into certified values(704,105);
insert into certified values(705,103);


insert into flights values(101,'Bangalore','Dehi',2500,TIMESTAMP '2005-05-13
07:15:31',TIMESTAMP '2005-05-13 07:15:31',5000);
insert into flights values(102,'Bangalore','Lucknow',3000,TIMESTAMP '2005-05-13
07:15:31',TIMESTAMP '2005-05-13 07:15:31',6000);
insert into flights values(103,'Lucknow','Dehi',500,TIMESTAMP '2005-05-13
07:15:31',TIMESTAMP '2005-05-13 07:15:31',3000);
insert into flights values(107,'Bangalore','Frankfurt',8000,TIMESTAMP '2005-05-13
07:15:31',TIMESTAMP '2005-05-13 07:15:31',60000);
insert into flights values(104,'Bangalore','Frankfurt',8500,TIMESTAMP '2005-05-13
07:15:31',TIMESTAMP '2005-05-13 07:15:31',75000);
insert into flights values(105,'Kolkata','Dehi',3400,TIMESTAMP '2005-05-13
07:15:31',TIMESTAMP '2005-05-13 07:15:31',7000);


i. Find the names of aircraft such that all pilots certified to operate them have salaries more
than Rs.80, 000.

SELECT DISTINCT A.ANAME
FROM AIRCRAFT A WHERE A.AID NOT IN (SELECT C.AID
FROM CERTIFIED C, EMPLOYEES E
WHERE C.EID = E.EID AND E.SALARY<80000);
or

SELECT DISTINCT A.ANAME
FROM AIRCRAFT a,CERTIFIED C, EMPLOYEES E
WHERE a.aid=c.aid and c.eid=e.eid AND E.SALARY<80000;

ANAME
---------------
Dreamliner
DBMS LAB

CSE, CITECH Page 25

Dreamliner2


ii. For each pilot who is certified for more than three aircrafts, find the eid and the maximum
cruisingrange of the aircraft for which she or he is certified.

SELECT C.EID, MAX (A.CRANGE)
FROM CERTIFIED C, AIRCRAFT A
WHERE C.AID = A.AID
GROUP BY C.EID
HAVING COUNT (*) >= 3;


EID MAX(A.CRANGE)
----- -------------
701 3500
703 12000
704 12000
702 12000


iii. Find the names of pilots whose salary is less than the price of the cheapest route from
Bengaluru to Frankfurt.

select distinct ename
FROM EMPLOYEES E
WHERE E.SALARY < ( SELECT MIN (F.PRICE)
FROM FLIGHTS F
WHERE F.fromplace ='Bangalore' AND F.toplace = 'Frankfurt');

ENAME
----------
A
E

iv. For all aircraft with cruisingrange over 1000 Kms,. Find the name of the aircraft and the
average salary of all pilots certified for this aircraft.

select name,avgsalary
from (select a.aid,a.aname as name,avg(e.salary) as avgsalary
from aircraft a,certified c,employees e where a.aid=c.aid and e.eid=c.eid and
a.crange>1000 group by a.aid,a.aname);

NAME AVGSALARY
--------------- ----------
DBMS LAB

CSE, CITECH Page 26

747 75000
Dreamliner 113333.333
Dreamliner2 113333.333
707 50000
Boeing 96666.6667

5. Find the names of pilots certified for some Boeing aircraft.

SELECT DISTINCT E.ENAME
FROM EMPLOYEES E, CERTIFIED C, AIRCRAFT A
WHERE E.EID = C.EID AND
C.AID = A.AID AND
A.ANAME = 'Boeing';

ENAME
----------
D
A
C

vi. Find the aids of all aircraft that can be used on routes from Bengaluru to New Delhi.

SELECT A.AID
FROM AIRCRAFT A
WHERE A.CRANGE >= (SELECT MIN (F.DISTANCE)
FROM FLIGHTS F
WHERE F.FROMplace = 'Bangalore' AND F.TOplace = 'Dehi');

AID
----------
101
104
105
107
Instructions:
1. The exercises are to be solved in an RDBMS environment like Oracle or DB2.
2. Suitable tuples have to be entered so that queries are executed correctly.
3. Front end may be created using either VB or VAJ or any other similar tool.
4. The student need not create the front end in the examination. The results of the queries may be
displayed directly.
DBMS LAB

CSE, CITECH Page 27

5. Relevant queries other than the ones listed along with the exercises may also be asked in the
examination.
6. Questions must be asked based on lots.

You might also like