Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
INSURANCE DATABASE
Consider the Insurance database given below:
PERSON (driver id: String, name: string, address: string)
CAR (Regno: string, model: string, year: int)
ACCIDENT (report-number: int, date: date, location: string)
OWNS (driver-id: string, Regno: string)
PARTICIPATED (driver-id: string, Regno: string, report-number:int, damage amount:int)
ER-Diagram:
Driver-id
Name
Regno
N
PERSON
Model
N
OWNS
CAR
Address
Year
PARTICIPATED
N
ACCIDENT
Report-number
Date
Null?
----------
Type
------------------VARCHAR2(20)
VARCHAR2(20)
NUMBER(10)
COUNT(*)
------------------2
2
1
2
1
report_no 12 to 25000.
SQL> update participated set damage_amount=25000 where report_no=12;
Table updated.
DRIVER_ID
---------------100
400
500
100
400
123
100
100
REG_NO
----------------33333
22222
44444
33333
22222
66666
33333
33333
REPORT_NO DAMAGE_AMOUNT
------------------- -------------------------------11
10000
12
25000
13
4566
14
2443
16
3545
17
55656
15
3333324
18
366785
NAME
-----------------ahmed
ADDRESS
-----------------fine
x) Find the name and address of the person who involved in the accident at a given
location on a given date.
SQL> select p.name, p.address
from person p, participated pd, accident a
where a.ddate='11-mar-03' and a.location='spring' and a.report_no=pd.report_no
and pd.driver_id=p.driver_id;
NAME
-----------ahmed
ADDRESS
-----------------fine
COUNT(REPORT_NO)
-----------------------------2
400
xii) Find the car number that is involved in minimum number of accidents.
SQL> select reg_no, count(report_no)
from participated
group by reg_no
having count(report_no)<=all (select count(report_no)
from participated
group by reg_no);
REG_NO
-----------44444
66666
COUNT(REPORT_NO)
-----------------------------1
1
NAME
ADDRESS
---------------- --------------johny
dallas
CHAPTER 2
Cust_no
Cname
Order_no
Odate
CUSTOMER
ORDER
ORDER
N
N
Order_amt
SHPMENT
ORDER-ITEM
WAREHOUSE
Warehouse_no
ITEM
City
Item_no
Unit price
i) Create the tables by properly specifying primary key and foreign key.
NO_OF_ORDERS AVG_AMT
------------------------ ---------------1
34444
1
23323
3
17774.333
1
34534
iv) List the order_no for orders that were shipped from all the warehouses that the
company has in a specific city.
SQL> select order_no,warehouse_no
from shipment
where warehouse_no in (select warehouse_no
from warehouse
where city='tumkur');
ORDER_NO
----------------15
11
WAREHOUSE_NO
------------------------100
100
v) Demonstrate how you delete item# 10 from the ITEM table and make that field
null in the ORDER_ITEM table.
SQL> delete from item
where item_no=10;
vi) Demonstrate how you increase or decrease no. of items ordered by a customer.
SQL> update orditem set qty=qty+10 where order_no in (select order_no
from cust_order
where cust_no=2);
ORDER_NO ITEM_NO
QTY
----------------- -------------- --------12
222
100
14
333
44
15
555
789
11
111
355
13
444
600
SQL> update orditem set qty=qty-10 where order_no in (select order_no
from cust_order
where cust_no=5);
ORDER_NO ITEM_NO
QTY
----------------- -------------- --------12
222
90
14
333
44
15
555
789
CHAPTER 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 (courseno: int, cname: string, dept: string)
ENROLL (regno: string, courseno: int, sem: int, marks: int)
BOOK _ ADOPTION (courseno: int, sem: int, book-ISBN: int)
TEXT (book-ISBN: int, book-title: string, publisher: string, author: string)
ER-Diagram:
Regno
Name
STUDENT
Major
Bdate
Marks
Course_no
ENROLL
Cname
COURSE
Sem
Dept
BOOK-ADOPTION
Sem
Publisher
TEXT
Author
Book-ISBN
book-title
i) Create the above tables by properly specifying the primary keys and foreign keys.
SQL> create table student (reg_no varchar(20), name varchar(20), major varchar(20),
bdate date, primary key(reg_no));
BDATE
-----------11-JAN-02
SEM MARKS
-------- -----------1
100
johny
ramesh
girish
ahmad
23-MAY-09
03-JUN-99
14-APR-93
16-NOV-02
2
6
3
3
200
125
95
100
BDATE
------------11-JAN-02
23-MAY-09
03-JUN-99
14-APR-93
16-NOV-02
SEM
--------1
2
6
3
3
MARKS
----------100
200
125
95
100
BDATE
------------11-JAN-02
23-MAY-09
03-JUN-99
14-APR-93
16-NOV-02
12-DEC-98
SEM MARKS
--------- -----------1
100
2
200
6
125
3
95
3
100
BDATE
SEM
MARKS
-----------john
johny
ramesh
girish
ahmad
john
johny
ramesh
girish
ahmad
sunil
--------------11-JAN-02
23-MAY-09
03-JUN-99
14-APR-93
16-NOV-02
11-JAN-02
23-MAY-09
03-JUN-99
14-APR-93
16-NOV-02
12-DEC-98
--------1
2
6
3
3
1
2
6
3
3
-----------100
200
125
95
100
100
200
125
95
100
v) Demonstrate Update, Insert and Delete with select and sub select with suitable
example.
Insert
SQL> create table newbook (course_no number(10), sem number(10),ISBN
number(10));
SQL> insert into newbook (select * from bookadpt);
COURSE_NO
SEM
ISBN
------------------- --------- --------11
1
1122
22
2
2233
55
6
3344
44
3
4455
33
3
5566
33
4
1122
55
3
1122
Delete
SQL> delete from bookadpt
where ISBN in (select ISBN
from text
where publisher='sush');
COURSE_NO
-----------------11
22
55
33
33
55
SEM
ISBN
--------- --------1
1122
2
2233
6
3344
3
5566
4
1122
3
1122
Update
SQL> update enroll
set marks=marks*1.5
where course_no in (select course_no
from course
where dept='cse');
REG_NO
------------111
222
333
444
555
COURSE_NO
-----------------11
22
55
44
33
SEM MARKS
--------- ----------1
150
2
300
6
188
3
95
3
100
vi) Demonstrate how you add a new text book to the database and make this book
be adopted by some department.
SQL> insert into text values(6655,'compiler design','pearson','xyz');
SQL> insert into bookadpt values(11,3,6655);
ISBN
----------1122
2233
3344
4455
5566
6655
COURSE_NO
-----------------11
22
55
33
33
55
11
TITLE
PUBLISHER
---------------- ---------- ------------------analysis and design
sapna
data communications
shree
networks
suhas
graphics
sush
data base
vijay
compiler design
pearson
SEM
--------1
2
6
3
4
3
3
AUTHOR
-------------------balaguru
swamy
van
foley
vijaya
xyz
ISBN
--------1122
2233
3344
5566
1122
1122
6655
vii) 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
SQL> select c.course_no, t.ISBN, t.title, count(*)
ISBN TITLE
--------- ----------------------4455 graphics
3344 networks
COUNT(*)
--------------1
1
viii) List any department that has all its adopted books published by a specific
publisher.
SQL> select distinct c.dept
from course c, bookadpt ba, text t
where t.ISBN=ba.ISBN and ba.course_no=c.course_no and t.ISBN in (select
t.ISBN
from text t
where t.publisher='sapna');
DEPT
---------cse
IT
mech
CHAPTER 4
BOOK DEALER DATABASE
The following tables are maintained by a book dealer:
Author-id
Name
AUTHOR
City
Title
Year
CATALOG
Country
Category-id
Publisher-id
Price
CATEGORY
Name
PUBLISHER
city
country
Description
N
Quantity
Book-id
ORDER-DETAILS
1
ORDER
Order-no
(i)Create the above tables by properly specifying the primary keys and the foreign
keys.
SQL> create table author (author_id number(5),author_name varchar(15),city
varchar(15),country varchar(15),primary key(author_id));
Table created.
2222
rose
(iv) Find the author of the book, which has maximum sales.
SQL> create view salesdetails as
( select OD.book_id as book_no, C.price as cost, sum(OD.qty) as qty,
sum(OD.qty * C.price) as sales
from order_details OD, catalog C, author A
where OD.book_id=C.book_id and C.author_id=A.author_id
group by OD.book_id, C.price);
SQL> select a.author_id,a.AUTHOR_NAMe,s.book_no,s.sales
from author a,catalog c,salesdetails s
where a.author_id=c.author_id and s.book_no=c.book_id and
sales=(selectmax(sales) from salesdetails);
AUTHOR_ID AUTHOR_NAME BOOK_NO SALES
------------------ ---------------------- --------------- ---------2222
rose
222
100000
(v) Demonstrate how you increase the price of books published by a specific
publisher by 10%.
SQL> update catalog set price=price*1.1 where PUBL_ID=100;
3 rows updated.
(vi) Create View with a name BookObject with the information about author,
publisher, price and year of publication.
SQL> create or replace view book_object as
(select a.author_id as author_id, a.author_name as author_name,
p.publ_id as publ_id, p.publ_name as publ_name,c.price as price,c.cyear as
cyear
from author a,publisher p,catalog c
where a.author_id=c.author_id and p.publ_id=c.publ_id);
View created.
(vii) Demonstrate how do you add new book information to the database through
views.
SQL> create view book_update as (select author_id, author_name,
publ_id, publ_name, price, cyear from catalog);
view created.
SQL> insert into book_update values(888,frank,111,tom,500,2005);
(viii) Rename the publisher xyz by any new name.
BANKING ENTERPRISE
Consider the following database for a banking enterprise
BRANCH (bname: string, bcity: string, assets: real)
ACCOUNT (accno: int, bname: string, balance: real)
CHAPTER 5
Cust-street
Accno
N
CUSTOMER
Branch-name
N
DEPOSITOR
ACCOUNT
N
Balance
ACCOUNT-BRANCH
BORROWER
Branch-city
Branch-name
1
Assets
BRANCH
1
LOAN-BRANCH
N
N
LOAN
Branch-name
Loan-no
Amount
(i) Create the above tables by properly specifying the primary keys and the foreign
keys.
SQL> create table branch(bname varchar(10),bcity varchar(10),assets real,primary key
(bname));
Table created.
(vii) Demonstrate how do you test for the empty relations with the query to find
all customers who have both an account and a loan at the bank.
SQL>
select cname
from borrower
where exists ( Select *
from depositor d, borrower b where d.cname=b.cname);
CNAME
---------jack
tom
rose
john