Sie sind auf Seite 1von 23

Database Lab Page 1 of 23

I. INSURANCE DATABASE

Create tables PERSON( driver_id : String, name : String, address: String )


CAR (reg_no : String, model : String, year :int )
ACCIDENT ( report_no : int, date : date, location : String )
OWNS (driver_id : String, reg_no : String )
PARTICIPATED ( driver_id : String, reg_no : String, report_no :
int,damage_amt : int);

1.
SQL> create table person (driver_id varchar(10) primary key, name varchar(10),
address varchar(20));

Table created.

SQL> insert into person values('001','deepa','bangalore');


1 row created.

SQL> select * from person;

DRIVER_ID NAME ADDRESS


---------- ----- ----- --- -----------------
001 deepa bangalore
002 jayteertha gulburga
003 guru bellary
004 verma hyderabad
005 bhargavi patna

2.
SQL> create table car (regno varchar(10) primary key, model varchar(10),year
number(4));
Table created.

SQL> insert into car values ('ka01','accent', 2000)


1 row created.

SQL> select * from car;


Database Lab Page 2 of 23

REGNO MODEL YEAR


---------- ---------- ---------
ka01 accent 2000
ka02 ford 2002
ka03 ford 2000
ka04 accent 2003
ka05 fiat 2004

3.
SQL> create table accident (report_no number (10) primary key, adate date, location
varchar(15));
Table created.

SQL> insert into accident values (11,'01-apr-02','shankarpura')


1 row created.

SQL> select * from accident;

REPORT_NO ADATE LOCATION


--------- -------- ---------- ----------------
11 01-APR-02 shankarpura
12 12-APR-02 k.g.road
13 20-JUN-02 vijayngr
14 25-JUN-02 jayngr
15 20-JUL-04 basavangudi

4.
SQL> create table owns (driver_id varchar(10), regno varchar(10),
2primary key(driver_id,regno),
3 foreign key(driver_id) references person(driver_id),
4 foreign key(regno) references car);
Table created.

SQL> insert into owns values ('001','ka01')


1 row created.

SQL> select * from owns;

DRIVER_ID REGNO
---------------- ---------
Database Lab Page 3 of 23

001 ka01
002 ka02
003 ka03
004 ka04
005 ka05

5.
SQL> create table participated (driver_id varchar(10),regno varchar(10),
2 report_no number(10), dam_amt number(7,2),
3 primary key(driver_id,regno,report_no),
4 foreign key(driver_id) references person,
5 foreign key(regno) references car,
6 foreign key(report_no) references accident);
Table created.

SQL> insert into participated values ('001','ka01',11, 1000)

SQL> select * from participated;

DRIVER_ID REGNO REPORT_NO DAM_AMT


---------------- ----- ---- ----------------- ---------------
001 ka01 11 1000
002 ka02 12 2000
003 ka03 13 3000
004 ka04 14 4000
005 ka05 15 5000

Queries

1. (a) Update the damage_amt for the car with specific reg_no in the accident with
report_no 12 to 25000.

SQL> update participated set dam_amt=25000 where report_no=12;


1 row updated.

SQL> select * from participated where report_no=12;

DRIVER_ID REGNO REPORT_NO DAM_AMT


---------- ---------- --------- --------------------------------------------
002 ka02 12 25000
Database Lab Page 4 of 23

(b) Add a new accident to database.

SQL> insert into accident values (16,'01-apr-05','nagarbavi');


1 row created.

SQL> select * from accident;

REPORT_NO ADATE LOCATION


--------- -------- ---------- ----------------
11 01-APR-02 shankarpura
12 12-APR-02 k.g.road
13 20-JUN-02 vijayngr
14 25-JUN-02 jayngr
15 20-JUL-04 basavangudi
16 01-APR-05 nagarbavi

2. Find the total number of the people who owned cars that are involved in
accident in 2004.

SQL> select count(*) as "No_of people" from owns where driver_id in


2 (select driver_id from participated where report_no in
3 (select report_no from accident where adate between '01-jan-02' and '31-dec-02'));

No_of people
----------------
4

3. Find the number of the accidents in which cars belonging to a specific model
were involved.

SQL> select count(*) as "No.of Accidents" from accident where report_no in (select
report_no from participated where regno in (select regno from car where
model='ford'));

No.of Accidents
Database Lab Page 5 of 23

---------------
2
Database Lab Page 6 of 23

II. ORDER PROCESSING DATABASE APPLICATION IN A COMPANY

Create tables CUSTOMER (cust_id : int, cname : String, city : String )


ORDER ( order_id : int, odate : date, ord_amt : int )
ORDER_ITEM ( order_id : int, item : int, qty : int )
ITEM ( item : int, unit_price : int )
SHIPMENT ( order_id : int , warehouse_id : int, ship_date : date )
WAREHOUSE ( warehouse_id : int, city : String );

1.
SQL> create table customer(custno number(3) primary key,
2 name varchar(10),city varchar(10));
Table created.

SQL> insert into customer values(&custno,'&name','&city');


Enter value for custno: 101
Enter value for name: deepa
Enter value for city: bangalore
old 1: insert into customer values(&custno,'&name','&city')
new 1: insert into customer values(101,'deepa','bangalore')
1 row created.

SQL> select * from customer;

CUSTNO NAME CITY


--------- ---------- ----------
101 deepa bangalore
102 raghu hyderabad
103 bhargavi patna
104 jayateertha raichur
105 guru bellary

2.
SQL> create table order1 (orderno number(4) primary key,date1 date, custno
number(3),ord_amt number(7,2));
Table created.

SQL> insert into order1 values (&orderno,'&date1',&custno,&ord_amt);


Database Lab Page 7 of 23

Enter value for orderno: 11


Enter value for date1: 21-mar-07
Enter value for custno: 101
Enter value for ord_amt: 1000
old 1: insert into order1 values(&orderno,'&date1',&custno,&ord_amt)
new 1: insert into order1 values(11,'21-mar-07',101,1000)
1 row created.

SQL> select * from order1;

ORDERNO DATE1 CUSTNO ORD_AMT


--------- ------- -------------- -- --------- ---------------
11 21-MAR-07 101 1000
12 22-MAR-07 102 2000
13 23-MAR-07 103 3000
14 24-MAR-07 104 4000
15 25-MAR-07 105 5000

3.
SQL> create table item (itemno number (4) primary key, unit_price number(7,2));
Table created.

SQL> insert into item values (&itemno,&unit_price);


Enter value for itemno: 201
Enter value for unit_price: 500
old 1: insert into item values(&itemno,&unit_price)
new 1: insert into item values(201,500)
1 row created.

SQL> select * from item;

ITEMNO UNIT_PRICE
--------- -------------------
201 500
202 600
203 700
204 800
205 100
4.
SQL> create table order_item(orderno number(4),itemno number(4), qty number(3),
Database Lab Page 8 of 23

primary key(orderno,itemno),foreign key(orderno) references order1,


foreign key(itemno) references item(itemno) on delete set null);
Table created.

SQL> insert into order_item values(&orderno,&itemno,&qty);


Enter value for orderno: 11
Enter value for itemno: 201
Enter value for qty: 2
old 1: insert into order_item values(&orderno,&itemno,&qty)
new 1: insert into order_item values(11,201,2)
1 row created.

SQL> select * from order_item;

ORDERNO ITEMNO QTY


--------- ---- ----- ------ ---
11 201 2
12 202 3
13 203 4
14 204 5
15 205 6
5.
SQL> create table shipment(orderno number(4),
warehouseno number(3),shipdate date,
foreign key(orderno) references order1,
primary key(orderno,warehouseno));
Table created.

SQL> insert into shipment values(&orderno,&warehouseno,'&shipdate');


Enter value for orderno: 11
Enter value for warehouseno: 111
Enter value for shipdate: 10-jan-07
old 1: insert into shipment values(&orderno,&warehouseno,'&shipdate')
new 1: insert into shipment values(11,111,'10-jan-07')
1 row created.
SQL> select * from shipment;

ORDERNO WAREHOUSENO SHIPDATE


--------- ----- ------ ---------------- --------------
11 111 10-JAN-07
Database Lab Page 9 of 23

12 112 11-JAN-07
13 113 12-JAN-07
14 114 13-JAN-07
15 115 14-JAN-07
11 112
6.
SQL> create table warehouse (warehouseno number(3) primary key, city
varchar(20));
SQL> insert into warehouse values (111,'bangalore')
1 row created.

SQL> select * from warehouse;

WAREHOUSENO CITY
----------- ----------- ---------
111 bangalore
112 bijapur
113 bellary
114 gadag
115 mumbai

Queries
1. Produce a listing, Customer name, number of orders and avg_Order_amt, for
that customer.

SQL> select name as "Customer Name",count(orderno),avg(ord_amt)


2 from customer,order1 where customer.custno=order1.custno
3 group by name;

Customer Name COUNT(ORDERNO) AVG(ORD_AMT)


---------- --------- ----- --------------------- -----------------------
guru 1 5000
bhargavi 1 3000
deepa 1 1000
jayateertha 1 4000
raghu 1 2000
2. List all order numbers for orders that were shipped from all warehouses that
the company has in a specific city.

SQL> select orderno from order1


Database Lab Page 10 of 23

Where not exists ((select warehouseno from warehouse where city=’bangalore’)


Minus
(select warehouseno from shipment where order1.orderno=shipment.orderno))

ORDERNO
---------------
11

3. Demonstrate how to delete the itemno 205 from item table.

SQL> delete from item where itemno=205;


1 row deleted.

SQL> select * from order_item;

ORDERNO ITEMNO QTY


--------- ---- ----- ------ ---
11 201 2
12 202 3
13 203 4
14 204 5
15 null 6
Database Lab Page 11 of 23

III. DATABASE OF STUDENT ENROLLMENT IN COURSES & BOOK


ADOPTED FOR EACH COURSE.

Create tables STUDENT ( reg_no : String, name : String, major : String, bdate : date )
COURSE ( course : int, cname : String, dept : string )
ENROLL ( reg_no : String, course : int, sem : int, marks : int )
BOOK_ADOPTION ( course : int, sem : int, ISBN : int )
TEXT ( book_ISBN : int, title : String, publisher : String );

1.
SQL> create table student(regno varchar(5) primary key,name varchar(10),
2 major varchar(5),bdate date);

SQL> insert into student values ('111','deepa','computers','10-may-84');


1 row created.

SQL> select * from student;

REGNO NAME MAJOR BDATE


---------- -------------------- -------------------- --------------
111 deepa computers 10-MAY-84
222 raghu animation 02-FEB-84
333 miya graphics 01-JAN-84
444 guru sp 23-AUG-84
555 sagar dbms 01-JUN-84

2.
SQL> create table course (courseno number(2) primary key,cname varchar(5),dept
varchar(5));

SQL> insert into course values (1,'mca','cs');


1 row created.

SQL> select * from course;


Database Lab Page 12 of 23

COURSENO CNAME DEPT


--------- -------------------- --------------------
1 mca cs
2 mba pg
3 be cs
4 mtech pg
5 bsc ug
6 me cs

3.
SQL> create table enroll(regno varchar(5) references student(regno),courseno
number(3) references course(courseno),sem number(2),marks number(3));

SQL> insert into enroll values('111',1,4,1000);


1 row created.

SQL> select * from enroll;

REGNO COURSENO SEM BOOK_ISBN


---------- --------- --------- --------------------------------
111 1 4 1000
222 2 5 1001
333 3 4 1002
444 4 6 1003
555 5 5 1004

4.
SQL> create table text(book_isbn number(4) primary key,title varchar(10),publisher
varchar(10),author varchar(10));

SQL> insert into text values(1000,'cn','prentice','tanenbaum');


1 row created.

SQL> select * from text;


Database Lab Page 13 of 23

BOOK_ISBN BOOK_TITLE PUBLISHER AUTHOR


--------- -------------------- -------------------- ----------------------------
1000 cn prentice tanenbaum
1001 dbms tata navathe
1002 ada prentice levitin
1003 se tata somerville
1004 wp prentice tanenbaum
1005 networks subhash dennis
1006 unix subhash richie

5.
SQL> create table book_adoption(courseno number(3) references course, sem
number(3),
2 isbn number(4) references text));

SQL> insert into book_adoption values(1,4,1000);


1 row created.

SQL> select * from book_adoption;


COURSENO SEM BOOK_ISBN
--------- --------- --------------------------
1 4 1000
2 5 1001
3 6 1002
4 4 1003
5 5 1004
6 7 1006

Queries

1. Demonstrate how to add a new text book to the database.

SQL> insert into text values( 4,'dca','pub4','kmh');

1 row created.

2. Produce a list of text_books (include courseno, book_isbn, book_title) in the


alphabetical order for course offered by ‘cs’ dept that use more than 2 books.
Database Lab Page 14 of 23

SQL> select c.courseno,b.book_isbn,t.book_title from course c,book_adoption b,text t


2 where b.courseno=c.courseno and b.book_isbn=t.book_isbn and dept like 'cs'
3 and c.courseno in (select courseno from book_adoption group by courseno
3 having count(*)>2) order by cname;

No rows selected

3. List any dept that has all its adopted books published by a specific publisher.

SQL> select dept from course c1 where not exists


((select book_adoption.book_isbn from text,course c2,book_adoption
where c2.dept=c1.dept and c2.courseno=book_adoption.courseno and
book_adoption.book_isbn=text.book_isbn)
minus
(select book_isbn from text where publisher=’prentice’))
group by dept;

DEPT
--------------------
ug
Database Lab Page 15 of 23

IV. DATABASE MAINTAINED BY A BOOK DEALER

Create tables AUTHOR ( author_id : int, name : String, city : String, country : String )
PUBLISHER ( pub_id : int, name : String, city : String, country : String )
CATALOG ( book_id : int, title : String , author_id : int, pub_id :int ,
category_id : int, year : int, price : int )
CATEGORY ( category_id : int, description : String )
ORDER_DETAILS ( order_no : int, book_id : int, qty : int );

1.
SQL> create table author (author_id number(6) primary key, name varchar(10),city
varchar(10), country varchar(10));
Table created.

SQL>insert into author values (101,’A1’,’bangalore’,’india’);


1 row created.

SQL> select * from author;

AUTHOR_ID NAME CITY COUNTRY


--------- ------- --- ----- ----- - ---------
101 A1 bangalore india
102 A2 bombay india
103 A3 newyork US
104 A4 London england
105 A5 delhi india

2.
SQL>create table publisher (publisher_id number (6) primary key, name varchar(10),
city varchar(10), country varchar(10));

SQL>insert into publisher values (1,’p1’,’bangalore’,’india’);


1 row created.

SQL> select * from publisher;


Database Lab Page 16 of 23

PUBLISHER_ID NAME CITY COUTRY


------------ -------- -- ------ ------- --------------
1 p1 bangalore india
2 p2 bombay india
3 p3 newyork us
4 p4 london england
5 p5 delhi india

3.
SQL>create table category (category_id number(4) primary key, description
varchar(10));

SQL>insert into category value s(1,’desc1’);


1 row created.

SQL> select * from category;


CATEGORY_ID DESCRIPTION
----------- --------- -------------------
1 desc1
2 desc2
3 desc3
4 desc4
5 desc5

4.
SQL>create table catalog (book_id number(6) primary key, title varchar(10),
Author_id number(6) references author,
Publisher_id number(6) refences publisher,
Category_id number(4) references category,
Year number(4), price number(7,2));

SQL>insert into catalog values (1,’cprog’,1,1,1999,150);


1 row created.

SQL> select * from catalog;


Database Lab Page 17 of 23

BOOK_ID TITLE AUTHOR_ID PUBLISHER_ID CATEGORY_ID YEAR PRICE


--------- ------------------------------------------------------------------ ----------- --------- ---------
1 cprog 101 1 1 1999 150
2 c++ 101 1 1 2002 200
3 network 103 3 3 2003 300
4 se 104 2 4 2002 330
5 ada 101 3 3 2000 250

5.
SQL>create table order_details (order_no number(6),book_id number(6),
qty number(3),
Primary key (order_no,book_id));

SQL>insert into order_details values (1,1,10);


1 row created.

SQL> select * from order_details;

ORDER_NO BOOK_ID QUANTITIY


--------- ------- ------------ ----------------
1 1 10
2 2 15
3 5 5
4 3 25
5 4 20
1 4 5
2 4 5

7 rows selected.

Queries
Database Lab Page 18 of 23

1. Give the details of author who have 2 or more books in the catalog and the price
of the books greater than average price of book in the catalog and year of
publication is after 2000.

SQL> select * from author where author_id in


(select author_id from catalog group by author_id having
count(author_id)>=2) and author_id in (select author_id from catalog where
price > (select avg(price) from catalog)) and author_id in (select author_id from
catalog where year >2000);

AUTHOR_ID NAME CITY COUNTRY


--------- ---------- ---------- ---------- ----------------
101 A1 bangalore india

2. Find author of the books which has maximum sales.

SQL> select * from author where author_id = (select author_id from catalog
2 where book_id = (select book_id from order_details group by book_id
3 having sum(quantitiy)=(select max(sum(quantitiy)) from order_details
4 group by book_id)))
5 ;
AUTHOR_ID NAME CITY COUNTRY
--------- ---------- ---------- ---------- ---------------
104 A4 London england

3. Demonstrate how to increase the price of the books published by a specific


publisher by 10%.

SQL> update catalog set price=price+price*.1 where publisher_id =2;


1 row updated.

SQL> select * from catalog where publisher_id =2;

BOOK_ID TITLE AUTHOR_ID PUBLISHER_ID CATEGORY_ID YEAR PRICE


--------- ---------- --------- ------------ ----------- --------- ---------------------------------------
4 se 104 2 4 2002 363

V. DATABASE FOR A BANKING ENTERPRISE


Database Lab Page 19 of 23

Create tables BRANCH (branch_name : String, branch_city : String, assets : real )


ACCOUNT ( acc_no : int, branch_name : String, balance : real )
DEPOSITOR ( cust_name : String, acc_no : int )
CUSTOMER ( customer_name : String, street : String, city : String )
LOAN ( loan_no : int, branch_name : String, amount : real )
BORROWER ( customer_name : String, loan_no : int );

1.
SQL> create table branch(branch_name varchar(20) primary key,branch_city
varchar(20),assets number(10,2));
Table created.

SQL> insert into branch values('&branch_name','&branch_city',&assets);


Enter value for branch_name: b1
Enter value for branch_city: bangalore
Enter value for assets: 1000000
old 1: insert into branch values('&branch_name','&branch_city',&assets)
new 1: insert into branch values('b1','bangalore',1000000)
1 row created.

SQL> select * from branch;


BRANCH_NAME BRANCH_CITY ASSETS
-------------------- -- ------------------ - ------------
b1 bangalore 1000000
b2 bangalore 1500000
b3 mysore 500000
b4 mangalore 500000
b5 mysore 1000000

2.
SQL> create table account (accno number(3) primary key,branch_name varchar(20)
references branch,bal number(10,2));
Table created.

SQL> insert into account values (101,'b1',2000);


1 row created.

SQL> select * from account;

ACCNO BRANCH_NAME BAL


Database Lab Page 20 of 23

--------- -------------------- ------- -----


101 b1 2000
102 b1 4000
103 b2 2500
104 b5 2000
105 b5 2000

3.
SQL> create table customer(customer_name varchar(10) primary
key,customer_street varchar(20),customer_city varchar(20));
Table created.

SQL>insert into customer values (‘c1’,’s1’,’bangalore’);


1 row created.

SQL>select * from customer;

CUSTOMER_NAME CUSTOMER_STREET CUSTOMER_CITY


-------------------------- ---------------------------- -------------------------
c1 s1 bangalore
c2 s2 bangalore
c3 s3 mysore
c4 s2 mangalore
c5 s2 mysore

4.
SQL>create table depositor (customer_name varchar(10) references customer, accno
number(3) references account);
Table created.

SQL>insert into depositor values (‘c1’,101);


1 row created.

SQL>select * from depositor;

CUSTOMER_NAME ACCNO
-------------------------- ----------
Database Lab Page 21 of 23

c1 101
c1 102
c2 103
c4 104
c2 105

5.
SQL>create table loan(loan_number number(6) primary key, brnch_name
vrachar(20) references branch,amount number(10,2));
Table created.

SQL>insert into loan values (1,’b1’,10000);


1 row created.

SQL>select * from loan;


LOAN_NUMBER BRANCH_NAME AMOUNT
---------------------- ----------------------- -------------
1 b1 10000
2 b2 15000
3 b3 15000
4 b1 5000
5 b5 10000

6.
SQL>create table borrower (customer_name varchar(10) references customer,
loan_number number(3) refernces loan);
Table created.

SQL>insert into borrower values (‘c1’,1);


1 row created.

SQL>select * from borrower;

CUSTOMER_NAME LOAN_NUMBER
-------------------------- ----------------------
c1 1
Database Lab Page 22 of 23

c2 3
c1 3
c4 4
c5 5

Queries

1.Find all the customers who have atleast two account in the main branch.
(assuming mainbranch=b1)

SQL>select customer_name from depositor,branch,account


Where depositor.accno=account.accno and
account.branch_name=branch.branch_name and branc.branc_name=’b1’
Group by depositor.customer_name
having count(distinct depositor.accno)>=2;

CUSTOMER_NAME
--------------------------
c1

2.Find the entire customers who have an account at all the branches in specific
city.

SQL>select customer_name from customer where not exists ((select branch_name


from branch where Branch_city=’bangalore’)
Minus
(select branch_name from (select customer_name,branch_name from
depositor, account where account.accno=depositor.accno)
where customer_name=customer.customer_name));

CUSTOMER_NAME
--------------------------
c1

3. Demonstrate how you delete all account tuples at every branch located in a
specific city.

SQL> delete from account where branch_name in


(select branch_name from branch where branch_city=’mysore’);
2 rows deleted.
Database Lab Page 23 of 23

SQL> select * from account;

ACCNO BRANCH_NAME BAL


--------- -------------------- ------- -----
101 b1 2000
102 b1 4000
103 b2 2500

Das könnte Ihnen auch gefallen