index

advertisement
www.bookspar.com | Website for students | VTU NOTES
INDEX
SERIAL
NO.
CONTENTS
PAGE
NO.
1.
INSURANCE DATABASE
2
2.
ORDER PROCESSING DATABASE
12
3.
STUDENT DATABASE
23
4.
BOOK DATABASE
34
5.
BANKING DATABASE
44
1
MARKS
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
PROBLEM 1
PROBLEM STATEMENT:
Consider the Insurance Database given below. The primary keys are
underlined and the datatypes 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 for the car with a specific regno in the
accident with report number 12 to 25000.
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 a suitable front end for querying and displaying the results.
2
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
SCHEMA DESCRIPTION:
PERSON
CAR
DRIVER_ID NAME
REG_NO
ADDRESS
MODEL
YEAR
PARTICIPATED
DRIVER_ID
-----------------
REG_NO
-------------
REPORTNO
-----------------
DAMAGEAMT
ACCIDENT
REPORTNO
DATE LOCATION
OWNS
DRIVER_ID
-----------------
REG_NO
-----------
3
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
ER DIAGRAM:
MODEL
REGNO
YEAR
CAR
OWNS
DRIVER_ID
NAME
PERSON
ADDRESS
DAMAGEAMT
PARTICIPATED
ACCIDENT
REPORT_NO
LOCATION
DATE
4
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table person
(
driver_id varchar(10) primary key,
name varchar(10),
address varchar(10)
);
Table created.
TABLE DESCRIPTION
SQL> describe person;
Name
Null?
Type
--------------------------------------------------------------------------------------DRIVER_ID
NOT NULL
VARCHAR(10)
NAME
VARCHAR(10)
ADDRESS
VARCHAR(10)
INSERTING TUPLES
SQL> insert into person values('&driver_id','&name','&address');
Enter value for driver_id: 1
Enter value for name: aaa
Enter value for address: jbnagar
old 1: into person values ('&driver_id','&name','&address')
new 1: into person values ('1','aaa','jbnagar')
1 row created.
SQL> insert into person values(‘2’,’bbb’,’jaynagar’);
SQL> insert into person values(‘3’,’ccc’,’basvangudi’);
SQL> insert into person values(‘4’,’ddd’,’jpnagar’);
SQL> insert into person values(‘5’,’eee’,’coxtown’);
SQL> select * from person;
DRIVER_ID NAME
ADDRESS
----------------------------------------------1
aaa
jbnagar
2
bbb
jaynagar
3
ccc
basvangudi
4
ddd
jpnagar
5
eee
coxtown
5
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table car
(
regno varchar(10) primary key,
model varchar(10),
year number(4)
);
Table created.
TABLE DESCRIPTION
SQL> describe car;
Name
Null?
Type
-------------------------------------------------------------------------------------REGNO
NOT NULL
VARCHAR2(10)
MODEL
VARCHAR2(10)
YEAR
NUMBER(4)
INSERTING TUPLES
SQL> insert into car values ('&regno','&model','&year');
Enter value for regno: 1000
Enter value for model: zen
Enter value for year: 2002
old 1: insert into car values ('&regno','&model','&year')
new 1: insert into car values ('1000','zen','2002')
1 row created.
SQL> insert into car values(‘2000’,’innova’,’2005’);
SQL> insert into car values(‘3000’,’omni’,’1999’);
SQL> insert into car values(‘4000’,’zen’,’2004’);
SQL> insert into car values(‘5000’,’scorpio’,’2008’);
SQL> select * from car;
REGNO MODEL
YEAR
--------------------------------------------1000
zen
2002
2000
innova
2005
3000
omni
1999
4000
zen
2004
5000
scorpio
2008
6
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table accident
(
repno number(4) primary key,
ddate date,
location varchar(10)
);
Table created.
TABLE DESCRIPTION
SQL> describe accident;
Name
Null?
Type
--------------------------------------------------------------------------------------REPNO
NOT NULL
NUMBER(4)
DDATE
DATE
LOCATION
VARCHAR2(10)
INSERTING TUPLES
SQL> insert into accident values('&repno','&ddate','&location');
Enter value for repno: 10
Enter value for ddate: 06-jul-2008
Enter value for location: rjngr
old 1: ('&repno','&ddate','&location')
new 1: ('10','06-jul-2008','rjngr')
1 row created.
SQL> insert into accident values(‘20’,’26-mar-2006’,’aaa’);
SQL> insert into accident values(‘30’,’24-apr-2008’,’bbb’);
SQL> insert into accident values(‘40’,’7-jan-2008’,’ccc’);
SQL> insert into accident values(‘50’,’17-feb-2009’,’ddd’);
SQL> select * from accident;
REPNO
DDATE
LOCATION
----------------------------------------------------10
06-JUL-08
rjngr
20
26-MAR-06
aaa
30
24-APR-08
bbb
40
07-JAN-08
ccc
50
17-FEB-09
ddd
7
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table owns
(
driver_id varchar(10) references person(driver_id),
regno varchar(10) references car(regno)
);
Table created.
TABLE DESCRIPTION
SQL> describe owns;
Name
Null?
Type
---------------------------------------------------------------------------------------DRIVER_ID
VARCHAR2(10)
REGNO
VARCHAR2(10)
INSERTING TUPLES
SQL> insert into owns values('&driver_id','&regno');
Enter value for driver_id: 1
Enter value for regno: 1000
old 1: insert into owns values ('&driver_id','&regno')
new 1: insert into owns values ('1','1000')
1 row created.
SQL> insert into owns values(‘2’,’2000’);
SQL> insert into owns values(‘3’,’3000’);
SQL> insert into owns values(‘4’,’4000’);
SQL> insert into owns values(‘5’,’5000’);
SQL> select * from owns;
DRIVER_ID REGNO
----------------------------1
1000
2
2000
3
3000
4
4000
5
5000
8
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table participated
(
driver_id varchar(10) references person(driver_id),
regno varchar(10) references car(regno),
repno number(4) references accident(repno),
damage number(6)
);
Table created.
TABLE DESCRIPTION
SQL> describe participated;
Name
Null?
Type
-------------------------------------------------------------------------------------------DRIVER_ID
VARCHAR2(10)
REGNO
VARCHAR2(10)
REPNO
NUMBER(4)
DAMAGE
NUMBER(6)
INSERTING TUPLES
SQL> insert into participated values('&driver_id','&regno','&repno','&damage');
Enter value for driver_id: 1
Enter value for regno: 1000
Enter value for repno: 10
Enter value for damage: 2000
old 1: insert into participated values ('&driver_id','&regno','&repno','&damage')
new 1: insert into participated values ('1','1000','10','2000')
1 row created.
SQL> insert into participated values(‘2’,’2000’,’20’,’3000’);
SQL> insert into participated values(‘3’,’3000’,’30’,’4000’);
SQL> insert into participated values(‘4’,’4000’,’40’,’5000’);
SQL> insert into participated values(‘5’,’5000’,’50’,’6000’);
SQL> select * from participated;
DRIVER_ID REGNO
REPNO
DAMAGE
--------------------------------------------------------------------------1
1000
10
2000
2
2000
20
3000
3
3000
30
4000
4
4000
40
5000
5
5000
50
6000
9
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
QUERIES
1. Demonstrate how you
a. Update the damage amount for the car with a specific Regno in the accident with
report number 12 to 25000.
SQL> update participated set damage=7777 where regno=3000 and repno between 12
and 25000;
1 row updated.
RESULT
SQL> select * from participated;
DRIVER_ID REGNO
REPNO
DAMAGE
--------------------------------------------------------------------------1
1000
10
2000
2
2000
20
3000
3
3000
30
7777
4
4000
40
5000
5
5000
50
6000
EXPLANATION
The SQL command Update is used to change the value of attribute damage in table participated.
The where clause provides the condition which selects the tuples in where regno= 3000 and repno
lies between 12 and 25000.
b. Add a new accident to the database.
SQL> insert into accident values(‘77’,'12-oct-2009','sevangr');
1 row created.
RESULT
SQL> select * from accident;
REPNO
DDATE
LOCATION
----------------------------------------------------10
06-JUL-08
rjngr
20
26-MAR-06
aaa
30
24-APR-08
bbb
40
07-JAN-08
ccc
50
17-FEB-09
ddd
77
12-OCT-09
sevangr
10
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
EXPLANATION
The insert command is used here to insert the tuples into the accident tables. The attributes are
specified in the braces.
2. Find the total number of people who owned cars that were involved in accidents
in 2008
SQL> select count(p.driver_id)
from person p,participated pa,accident a
where p.driver_id=pa.driver_id and pa.repno=a.repno and ddate like ‘%08’;
RESULT
COUNT(P.DRIVER_ID)
---------------------------------3
EXPLANATION
The condition pa.repno and a.repno selects appropriate tuples with date ending in ’08’.The count
function counts the number of such tuples.
3. Find the number of accidents in which cars belonging to a specific model were
involved
SQL> select count(a.repno)
from accident a,participated p,car c
where a.repno=p.repno and p.regno=c.regno and c.model='zen';
RESULT
COUNT(A.REPNO)
-------------------------2
EXPLANATION
The three relations are joined at their attributes correspondingly and the tuples with model name
zen are selected and count function counts the number of such tuples.
11
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
PROBLEM-2
PROGRAM STATEMENT:
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)
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, NO_OF_ORDERS,
AVG_ORDER_AMT, where the middle column is the total number of
orders by the customer and the last column is the average order amount for
that customer.
iv) List the order# for the orders that were shipped from all the warehouses
that the company has in a specific city.
v) Demonstrate how you delete an item from the ITEM table and make that
field null in the ORDER-ITEM table.
vi) Generate suitable reports.
vii) Create a suitable front end for querying and displaying the results.
12
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
SCHEMA DESCRIPTION:
CUSTOMER
CUST_NO
CNAME
CITY
ORDER
WAREHOUSE
ORDER_NO ODATE CUST_NO ORD_AMT
--------------
WARE_NO CITY
ORDER_ITEM
ORDER_NO ITEM_NO QUANTITY
----------------- --------------
ITEM
ITEM_NO
UNIT_PRICE
SHIPMENT
ORDER_NO
------------------
WARE_NO
---------------
13
SHIP_DATE
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
ER DIAGRAM:
CNAME
C_CITY
CUST_ID
CUSTOMER
PLACES
ORD_AMT
ORDER_NO
O_DATE
ORDER
QTY
SHIPMENT
ORDER_ITEM
ORD_AMT
WAREHOUSE
ITEM
WARE_NO
CITY
UNIT_PRICE
ITEM_NO
14
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table customer
(
custno number(4) primary key,
cname varchar(10) not null,
city varchar(10) not null
);
Table created.
TABLE DESCRIPTION
SQL> describe customer;
Name
Null?
Type
----------------------------------------------------------------------------CUSTNO
NOT NULL
NUMBER(4)
CNAME
NOT NULL
VARCHAR2(10)
CITY
NOT NULL
VARCHAR2(10)
INSERTING TUPLES
SQL> insert into customer values('&custno','&cname','&city');
Enter value for custno: 1
Enter value for cname: ddd
Enter value for city: chennai
old 1:insert into customer values('&custno','&cname','&city')
new 1:insert into customer values('1','ddd','chennai')
1 row created.
SQL> insert into customer values('2','aaa','bangalore');
SQL> insert into customer values(‘3’,’bbb’,’delhi’);
SQL> insert into customer values(‘4’,’eee’,’kolkata’);
SQL> insert into customer values(‘5’,’fff’,’mumbai’);
SQL> select * from customer;
CUSTNO
CNAME
CITY
------------------------------------------------------------------------1
ddd
chennai
2
aaa
banglore
3
bbb
delhi
4
eee
kolkata
5
fff
mumbai
15
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table orders
(
ordno number(4) primary key,
odate date not null,
custno number(4) not null references customer(custno) on delete cascade,
amount number(4) not null
);
Table created.
TABLE DESCRIPTION
SQL> describe orders;
Name
Null?
Type
----------------------------------------------------------------------------ORDNO
NOT NULL
NUMBER(4)
ODATE
NOT NULL
DATE
CUSTNO
NOT NULL
NUMBER(4)
AMOUNT
NOT NULL
NUMBER(4)
INSERTING TUPLES
SQL> insert into orders values ('&ordno','&odate','&custno','&amount');
Enter value for ordno: 11
Enter value for odate: 11-sep-2009
Enter value for custno: 1
Enter value for amount: 100
old 1: insert into orders values ('&ordno','&odate','&custno','&amount')
new 1: insert into orders values ('11','11-sep-2009','1','100')
SQL>insert into orders values (‘12’,’12-JAN-2004’,’2’,’200’);
SQL>insert into orders values (‘13’,’20-FEB-2005’,’1’,’300’);
SQL>insert into orders values (‘14’,’15-AUG-99’,’4’,’400’);
SQL>insert into orders values (‘15’,’12-DEC-2000’,’2’,’500’);
SQL> select * from orders;
ORDNO
ODATE
CUSTNO
AMOUNT
-------------------------------------------------------------------------------------------11
11-SEP-09
1
100
12
12-JAN-04
2
200
13
20-FEB-05
1
300
14
15-AUG-99
4
400
15
12-DEC-00
2
500
16
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table items
(
itemno number(4) primary key ,
price number(4) not null
);
Table created.
TABLE DESCRIPTION
SQL> describe items;
Name
Null?
Type
----------------------------------------------------------------------------ITEMNO
NOT NULL
NUMBER(4)
PRICE
NOT NULL
NUMBER(4)
INSERTING TUPLES
SQL> insert into items values('&itemno','&price');
Enter value for itemno: 1
Enter value for price: 2000
old 1: ('&itemno','&price')
new 1: ('1','2000')
1 row created.
SQL> insert into items values(‘2’,’2000’);
SQL> insert into items values(‘3’,’1320’);
SQL> insert into items values(‘4’,’2300’);
SQL> insert into items values(‘5’,’1200’);
SQL> select * from items;
ITEMNO PRICE
-------------------------------1
2000
2
2000
3
1320
4
2300
5
1200
17
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table order_item
(
ordno number(4) references orders(ordno) on delete cascade,
itemno number(4) references items(itemno) on delete set null,
qty number(4),
primary key(ordno)
);
Table created.
TABLE DESCRIPTION
SQL> describe order_item;
Name
Null?
Type
----------------------------------------------------------------------------ORDNO
NOT NULL
NUMBER(4)
ITEMNO
NUMBER(4)
QTY
NUMBER(4)
INSERTING TUPLES
SQL> insert into order_item values ('&ordno','&itemno','&qty');
Enter value for ordno: 11
Enter value for itemno: 1
Enter value for qty: 8
old 1: insert into order_item values ('&ordno','&itemno','&qty')
new 1: insert into order_item values ('11','1','8')
1 row created.
SQL> insert into order_item values(‘12’,’2’,’1’);
SQL> insert into order_item values(‘13’,’3’,’2’);
SQL> insert into order_item values(‘14’,’4’,’5’);
SQL> insert into order_item values(‘15’,’3’,’9’);
SQL> select * from order_item;
ORDNO
ITEMNO QTY
---------------------------------------------11
1
8
12
2
1
13
3
2
14
4
5
15
3
9
18
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table warehouse
(
wareno number(4) primary key,
city varchar(10)
);
Table created.
TABLE DESCRIPTION
SQL> describe warehouse;
Name
Null?
Type
----------------------------------------------------------------------------WARENO
NOT NULL NUMBER(4)
CITY
VARCHAR2(10)
INSERTING TUPLES
SQL> insert into warehouse values('&wareno','&city');
Enter value for wareno: 1
Enter value for city: delhi
old 1: insert into warehouse values('&wareno','&city')
new 1: insert into warehouse values ('1','delhi')
1 row created.
SQL> insert into warehouse values(‘2’,’poona’);
SQL> insert into warehouse values(‘3’,’delhi’);
SQL> insert into warehouse values(‘4’,’madras’);
SQL> insert into warehouse values(‘5’,’hyd’);
SQL> select * from warehouse;
WARENO
CITY
--------------------------------1
delhi
2
poona
3
delhi
4
madras
5
hyd
19
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table shipment
(
ordno number(4) references orders(ordno),
wareno number(4) references warehouse(wareno),
shipdate date,
primary key(ordno,wareno)
);
Table created.
TABLE DESCRIPTION
SQL> describe shipment;
Name
Null?
Type
----------------------------------------------------------------------------ORDNO
NOT NULL
NUMBER(4)
WARENO
NOT NULL
NUMBER(4)
SHIPDATE
DATE
INSERTING TUPLES
SQL> insert into shipment values('&ordno','&wareno','&sdate');
Enter value for ordno: 11
Enter value for wareno: 1
Enter value for sdate: 16-jul-1998
old 1: insert into shipment values ('&ordno','&wareno','&sdate')
new 1: insert into shipment values ('11','1','16-jul-1998')
1 row created.
SQL> insert into shipment values(‘12’,’2’,’22-NOV-03’);
SQL> insert into shipment values(‘13’,’3’,’16-FEB-95’);
SQL> insert into shipment values(‘14’,’4’,’31-JAN-08’);
SQL> insert into shipment values(‘15’,’5’,’16-AUG-00’);
SQL> select * from shipment;
ORDNO
WARENO
SHIPDATE
------------------------------------------------------------------11
1
16-JUL-98
12
2
22-NOV-03
13
3
16-FEB-95
14
4
31-JAN-08
15
5
16-AUG-00
20
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
QUERIES
1.Produce a listing: CUSTNAME, NO_OF_ORDERS, 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.
SQL> select c.cname as custname,count(*) as no_of_orders,avg(oi.qty*i.price) as avg_order_amt
from customer c,orders o,order_item oi,items i
where c.custno=o.custno and o.ordno=oi.ordno and i.itemno=oi.itemno
group by(c.cname);
RESULT
CUSTNAME
NO_OF_ORDERS
AVG_ORDER_AMT
----------------------------------------------------------------------------aaa
2
6940
eee
1
11500
ddd
2
9320
EXPLANATION
This query produces the list of customers with no of orders. The three relations are joined at
corresponding attributes. The count function is used to display the names. The average is the
product of quantity and price.
2. List the order# for orders that were shipped from all the warehouses that the
company has in a specific city.
SQL> select s.ordno
from shipment s,warehouse w
where w.wareno=s.wareno and w.city='delhi';
RESULT
ORDNO
---------11
13
EXPLANATION
The two tables warehouse and shipment are joined and the tuples with given city name are
selected and orderno is selected.
21
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
3. Demonstrate how you delete item# from the ITEM table and make that field null
in the ORDER_ITEM table
SQL> delete
from items
where itemno=1;
1 row deleted.
RESULT
SQL> select * from item;
ITEMNO PRICE
-------------------------------2
2000
3
1320
4
2300
5
1200
SQL> select *from order_item;
ORDNO ITEMNO
QTY
---------------------------------------------11
8
12
2
1
13
3
2
14
4
5
15
3
9
EXPLANATION
Since the attribute itemno is a primary key, we have eliminated that constraint and after that the
tuples with the indicated itemno are deleted. Also in the order_item table the tuple which
contained itemno 10 has been made null.
22
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
PROBLEM 3
PROBLEM STATEMENT:
Consider the following database of student enrollment in courses and 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)
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 atleast 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.
23
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
SCHEMA DESCRIPTION:
STUDENT
REG_NO
COURSE
NAME MAJOR BDATE
COURSE_NO CNAME
CITY
ENROLL
REG_NO
-------------
COURSE_NO
------------------
SEM
MARKS
BOOK ADOPTION
COURSE_NO
BOOK_ISBN
SEM
TEXT
BOOK_ISBN
BOOK_TITLE
PUBLISHER
24
AUTHOR
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
ER DIAGRAM:
NAME
MAJOR
B_DATE
STUDENT
REG_NO
ENROLL
MARKS
SEM
CNAME
COURSE
DEPT
COURSE
BOOK_ADOPTION
SEM
TEXT
ISBN
TITLE
AUTHOR
PUBLISHER
25
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table student
(
regno varchar(10) primary key,
name varchar(15) not null,
major varchar(30) not null,
bdate date not null
);
Table created.
TABLE DESCRIPTION
SQL> describe student;
Name
Null?
Type
----------------------------------------------------------------------------REGNO
NOT NULL
VARCHAR2(10)
NAME
NOT NULL
VARCHAR2(15)
MAJOR
NOT NULL
VARCHAR2(30)
BDATE
NOT NULL
DATE
INSERTING TUPLES
SQL> insert into student values ('&regno','&name','&major','&bdate');
Enter value for regno: 101
Enter value for name: akash
Enter value for major: dbms
Enter value for bdate: 11-jan-1989
old 2: ('&regno','&name','&major','&bdate')
new 2: ('101','akash','dbms','11-jan-1989')
1 row created.
SQL> insert into student values('102','arvind','algorithms','22-feb-1988');
SQL> insert into student values('103','anveesh','unix','23-mar-1989');
SQL> insert into student values('104','virinchi','game theory','24-apr-90');
SQL> insert into student values('105','vageesh','game theory','25-may-90');
SQL> select * from student;
REGNO NAME
MAJOR
BDATE
---------------------------------------------------------------------------101
akash
dbms
11-JAN-89
102
arvind
algorithms
22-FEB-88
103
anveesh
unix
23-MAR-89
104
virinchi
game theory
24-APR-90
105
vageesh
game theory
25-MAY-90
26
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table course
(
course integer primary key,
cname varchar(30) not null,
dept varchar(30) not null
);
Table created.
TABLE DESCRIPTION
SQL> describe course;
Name
Null?
Type
----------------------------------------------------------------------------COURSE
NOT NULL
NUMBER(38)
CNAME
NOT NULL
VARCHAR2(30)
DEPT
NOT NULL
VARCHAR2(30)
INSERTING TUPLES
SQL> insert into course values ('&course','&cname','&dept');
Enter value for course: 1
Enter value for cname: mtech
Enter value for dept: cse
old 2: ('&course','&cname','&dept')
new 2: ('001','mtech','cse')
1 row created.
SQL> insert into course values('2','me','cse');
SQL> insert into course values('3','mtech','ise');
SQL> insert into course values('4','bsc','cse');
SQL> insert into course values('5','mca','ise');
SQL> select * from course;
COURSE
CNAME
DEPT
---------------------------------------------------------------------1
mtech
cse
2
me
cse
3
mtech
ise
4
bsc
cse
5
mca
ise
27
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table text
(
book_isbn integer primary key,
book_title varchar(30) not null,
publisher varchar(30) not null,
author varchar(30) not null
);
Table created.
TABLE DESCRIPTION
SQL> describe text;
Name
Null?
Type
----------------------------------------------------------------------------BOOK_ISBN
NOT NULL
NUMBER(38)
BOOK_TITLE
NOT NULL
VARCHAR2(30)
PUBLISHER
NOT NULL
VARCHAR2(30)
AUTHOR
NOT NULL VARCHAR2(30)
INSERTING TUPLES
SQL>insert into text values ('&book_isbn','&book_title','&publisher','&author');
Enter value for book_isbn: 111
Enter value for book_title: linux
Enter value for publisher: pearson
Enter value for author: sumithaba das
old 2: ('&book_isbn','&book_title','&publisher','&author')
new 2: ('111','linux','pearson','sumithaba das')
1 row created.
SQL> insert into text values('222','compiler design','tmh','tenenbaum');
SQL> insert into text values('333','computer graphics','pearson','micheal folk');
SQL> insert into text values('444','data structures','venugopal','tmh');
SQL> insert into text values('555','file structures','godse','himalaya');
SQL> select * from text;
BOOK_ISBN
BOOK_TITLE
PUBLISHER
---------------------------------------------------------------------------------------AUTHOR
-----------------------------111
linux
pearson
sumithaba das
222
tenenbaum
compiler design
tmh
28
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
333
micheal folk
computer graphics
pearson
BOOK_ISBN
BOOK_TITLE
PUBLISHER
---------------------------------------------------------------------------------------AUTHOR
-----------------------------444
data structures
venugopal
tmh
555
file structures
godse
himalaya
TABLE CREATION
SQL> create table enroll
(
regno varchar(10) not null,
course integer not null,
sem integer not null,
marks integer not null,
primary key(regno,course,sem),
foreign key (regno) references student (regno) on delete cascade,
foreign key (course) references course (course) on delete cascade
);
Table created.
TABLE DESCRIPTION
SQL> describe enroll;
Name
Null?
Type
-------------------------------------------------------------------------REGNO
NOT NULL
VARCHAR2(10)
COURSE
NOT NULL
NUMBER(38)
SEM
NOT NULL
NUMBER(38)
MARKS
NOT NULL
NUMBER(38)
INSERTING TUPLES
SQL>insert into enroll values ('&regno','&course','&sem','&marks');
Enter value for regno: 103
Enter value for course: 3
Enter value for sem: 5
Enter value for marks: 590
old 2: ('&regno','&course','&sem','&marks')
new 2: ('103','003','5','590')
SQL> insert into enroll values('105','5','6','789');
SQL> insert into enroll values('102','1','4','678');
29
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
SQL> insert into enroll values('101','1','6','430');
SQL> insert into enroll values('104','4','4','556');
SQL> select * from enroll;
REGNO COURSE
SEM MARKS
---------------------------------------------------103
3
5
590
105
5
6
789
102
1
4
678
101
1
6
430
104
4
4
556
TABLE CREATION
SQL> create table book_adoption
(
course integer not null,
sem integer not null,
book_isbn integer not null,
primary key(course,sem,book_isbn),
foreign key (course) references course (course) on delete cascade,
foreign key (book_isbn) references text (book_isbn) on delete cascade
);
Table created.
TABLE DESCRIPTION
SQL> describe book_adoption;
Name
Null?
Type
--------------------------------------------------------------------COURSE
NOT NULL
NUMBER(38)
SEM
NOT NULL
NUMBER(38)
BOOK_ISBN
NOT NULL
NUMBER(38)
INSERTING TUPLES
SQL>insert into book_adoption values
('&course','&sem','&book_isbn');
Enter value for course: 1
Enter value for sem: 5
Enter value for book_isbn: 111
old 2: ('&course','&sem','&book_isbn')
new 2: ('001','5','111')
1 row created.
SQL> insert into book_adoption values('1','6','222');
SQL> insert into book_adoption values('2','4','333');
SQL> insert into book_adoption values('3','6','444');
30
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
SQL> insert into book_adoption values('4','4','555');
SQL> select * from book_adoption;
COURSE
SEM BOOK_ISBN
----------------------------------------------------1
5
111
1
6
222
1
4
333
3
6
444
4
4
555
QUERIES
1. 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
('666','computer networks','venugopal','ferouzon');
1 row created.
SQL> insert into book_adoption values
('5','6','666');
1 row created.
RESULT:
SQL> select * from text;
BOOK_ISBN
BOOK_TITLE
PUBLISHER
---------- ------------------------------ -----------------------------AUTHOR
-----------------------------111
linux
pearson
sumithaba das
222
compiler design
tmh
computer graphics
pearson
tenenbaum
333
micheal folk
31
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
BOOK_ISBN
BOOK_TITLE
PUBLISHER
---------- ------------------------------ -----------------------------AUTHOR
-----------------------------444
data structures
venugopal
tmh
555
file structures
godse
666
computer networks
venugopal
himalaya
ferouzon
6 rows selected.
SQL> select * from book_adoption;
COURSE SEM
BOOK_ISBN
------------------------------------------------1
5
111
1
6
222
1
4
333
3
6
444
4
4
555
5
6
666
6 rows selected.
EXPLANATION
INSERT can be used to add a single tuple to a relation. Thus information about a new text book
can be added to the TEXT entity using INSERT command. The new text book can be made to be
adopted by some department using INSERT. The values which are added to the
BOOK_ADOPTION contain COURSE and SEM of the department and semester which uses the
textbook, along with the BOOK_ISBN of the textbook through which other information of the
textbook can be obtained.
2.Produce a list of textbooks in the alphabetic order for courses offered by the
‘CSE’ department that use more than two books
SQL> select c.course, t.book_isbn, t.book_title
from course c, book_adoption ba, text t
where c.course=ba.course and c.dept='cse' and ba.book_isbn=t.book_isbn
and exists (select count(course)
from book_adoption
where course=c.course
group by course
having count(course)>2)
order by book_title;
32
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
RESULT
COURSE BOOK_ISBN BOOK_TITLE
------------------------------------------------------------1
222
compiler design
1
333
computer graphics
1
111
linux
EXPLANATION
The first nested query selects the Departments which use more than two books. To select the
departments EXIST function is used to check whether there are any department which use more
than two books. In the outer query, we select the courseno, book_isbn and book_title of the
textbooks for courses offered by the ‘CSE’ department. In the outer query, the join condition
C.COURSE= BA.COURSE relates the COURSE and BOOK_ADOPTION and the condition
DEPT= ‘CSE’ is a selection condition. ORDER-BY clause is used to order the tuples in the result
of query according to the BOOK_TITLE
3. List any department that has all its adopted books published by a specific
publisher.
SQL> select distinct c.dept
from course c,text t,book_adoption ba
where c.course=ba.course and t.book_isbn=ba.book_isbn and t.publisher='venugopal'
and t.publisher=all(select t1.publisher
from course c1,book_adoption ba1,text t1
where ba1.book_isbn=t1.book_isbn and ba1.course=c1.course
and c.dept=c1.dept);
RESULT
DEPT
-------------------------ise
EXPLANATION
The outer query selects the distinct departments for the specified publisher ‘venugopal’. The inner
query selects all the publishers for all the books adopted by the selected department. The keyword
‘all’ is used to check whether whether the department has all its adopted books publisher by
‘venugopal’.
33
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
PROGRAM -4
PROGRAM STATEMENT:
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 a suitable front end for querying and displaying the results.
34
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
SCHEMA DESCRIPTION:
AUTHOR
AUTHOR_ID NAME CITY COUNTRY
PUBLISHER
PUBLISHER_ID
NAME CITY
COUNTRY
CATALOG
BOOK_ID
TITLE
AUTHOR_ID
------------------
PUBLISHER_ID CATEGORY_ID YEAR PRICE
--------------------- --------------------
CATEGORY
` CATEGORY_ID
DESCRIPTION
ORDER_DETAILS
ORDER_NO BOOK_ID
35
QUANTITY
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
ER DIAGRAM:
PUBLISHER_ID
ANAME
AUTHOR_ID
PNAME
TEXT
ACITY
PCITY
AUTHOR
PCOUNTRY
PUBLISHES
ACOUNTRY
WRITES
CATEGORY_ID
ORDER_NO
CATEGORY
QTY
DESC
ORDER_DETAILS
BELONGS_TO
CONTAINS
CATALOG
BOOK_ID
PRICE
YEAR
TITLE
36
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table author
(
authid number(4) primary key,
name varchar(10),
city varchar(10),
country varchar(10)
);
Table created.
TABLE DESCRIPTION
SQL> describe author;
Name
Null?
Type
----------------------------------------------------------------------------------------AUTHID
NOT NULL
NUMBER(4)
NAME
VARCHAR2(10)
CITY
VARCHAR2(10)
COUNTRY
VARCHAR2(10)
INSERTING TUPLES
SQL> insert into author values ('&authid','&name','&city','&country');
Enter value for authid: 11
Enter value for name: aaa
Enter value for city: delhi
Enter value for country: india
old 2: ('&authid','&name','&city','&country')
new 2: ('11','aaa','delhi','india')
1 row created.
SQL> insert into author values ('12','bbb','banglore','india');
SQL> insert into author values ('13','ccc','luxar','africa');
SQL> insert into author values ('14','ddd','paris','france');
SQL> insert into author values ('15','eee','poland','germany');
SQL> select * from author;
AUTHID
NAME
CITY
COUNTRY
---------------------------------------------------------------------------------11
aaa
delhi
india
12
bbb
banglore
india
13
ccc
luxar
africa
14
ddd
paris
france
15
eee
poland
germany
37
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table publisher
(
pubid number(4) primary key,
name varchar(10),
city varchar(10),
country varchar(10)
);
Table created.
TABLE DESCRIPTION
SQL> describe publisher;
Name
Null?
Type
------------------------------------------------------------------------------------------PUBID
NOT NULL
NUMBER(4)
NAME
VARCHAR2(10)
CITY
VARCHAR2(10)
COUNTRY
VARCHAR2(10)
INSERTING TUPLES
SQL> insert into publisher values ('&pubid','&name','&city','&country');
Enter value for pubid: 21
Enter value for name: macgraw
Enter value for city: delhi
Enter value for country: india
old 2: ('&pubid','&name','&city','&country')
new 2: ('21','macgraw','delhi','india')
1 row created.
SQL> insert into publisher values ('22','subhash','paris','france');
SQL> insert into publisher values ('23','annai','banglore','india');
SQL> insert into publisher values ('24','DSC','delhi','india');
SQL> insert into publisher values ('25','KSC','poland',’germany');
SQL> select * from publisher;
PUBID
NAME
CITY
COUNTRY
--------------------------------------------------------------------------------------------------------21
macgraw
delhi
india
22
subash
paris
france
23
annai
banglore
india
24
DSC
delhi
india
25
KSC
poland
germany
38
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table category
(
catid number(4) primary key,
des varchar(20)
);
Table created.
TABLE DESCRIPTION
SQL> describe category;
Name
Null?
Type
---------------------------------------------------------------------------------------------------CATID
NOT NULL
NUMBER(4)
DES
VARCHAR2(20)
INSERTING TUPLES
SQL> insert into category values ('&catid','&des');
Enter value for catid: 31
Enter value for des: sys s/w
old 2: ('&catid','&des')
new 2: ('31','sys s/w')
1 row created.
SQL> insert into category values ('32','os')
SQL> insert into category values ('33','dbms')
SQL> insert into category values ('34','gt')
SQL> insert into category values ('35','oops')
SQL> select * from category;
CATID
DES
----------------------------------31
sys s/w
32
os
33
dbms
34
gt
35
oops
TABLE CREATION
SQL> create table catalog
(
bookid number(4) primary key,
title varchar(20),
authid number(4) references author(authid),
pubid number(4) references publisher(pubid),
39
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
catid number(4) references category(catid),
year number(4),
price number(4)
);
Table created.
TABLE DESCRIPTION
SQL> describe catalog;
Name
Null?
Type
---------------------------------------- -----------------------------------BOOKID
NOT NULL
NUMBER(4)
TITLE
VARCHAR2(20)
AUTHID
NUMBER(4)
PUBID
NUMBER(4)
CATID
NUMBER(4)
YEAR
NUMBER(4)
PRICE
NUMBER(4)
INSERTING TUPLES
SQL> insert into catalog values ('&bookid','&title','&authid','&pubid','&catid','&year','&price');
Enter value for bookid: 41
Enter value for title: logic design
Enter value for authid: 11
Enter value for pubid: 21
Enter value for catid: 31
Enter value for year: 2003
Enter value for price: 300
old 2: ('&bookid','&title','&authid','&pubid','&catid','&year','&price')
new 2: ('41','logic design','11','21','31','2003','300')
1 row created.
SQL> insert into catalog values ('42','graph theory','11','22','32','2002','480');
SQL> insert into catalog values ('43','c++ appl','13','23','33','1992','330');
SQL> insert into catalog values ('44','dbms mang','14','24','34','2008','225');
SQL> insert into catalog values ('45','c prog','14','25','35','2000','150');
SQL> select * from catalog;
BOOKID
TITLE
AUTHID PUBID CATID
YEAR PRICE
----------------------------------------------------------------------------------------------------------41
logic design
11
21
31
2003
300
42
graph theory
11
22
32
2002
480
43
c++ appl
13
23
33
1992
330
44
dbms mang
14
24
34
2008
225
45
c prog
14
25
35
2000
150
40
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table order_details
(
ordno number(4) primary key,
bookid number(4) references catalog(bookid),
quantity number(4)
);
Table created.
TABLE DESCRIPTION
SQL> describe order_details;
Name
Null?
Type
----------------------------------------------------------------------------ORDNO
NOT NULL
NUMBER(4)
BOOKID
NUMBER(4)
QUANTITY
NUMBER(4)
INSERTING TUPLES
SQL> insert into order_details values ('&ordno','&bookid','&quantity');
Enter value for ordno: 91
Enter value for bookid: 41
Enter value for quantity: 50
old 2: ('&ordno','&bookid','&quantity')
new 2: ('91','41','50')
1 row created.
SQL> insert into order_details values ('92','42','100')
SQL> insert into order_details values ('93','43','150')
SQL> insert into order_details values ('94','43','25')
SQL> insert into order_details values ('95','45','160')
SQL> select * from order_details;
ORDNO BOOKID QUANTITY
-----------------------------------------------------------------91
41
50
92
42
100
93
43
150
94
43
25
95
45
160
41
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
QUERIES
1. 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.
SQL> select a.authid, a.name ,a.city, count(*) as count
from author a,catalog c
where a.authid=c.authid and c.year>2000
and c.price>=(select avg (price)
from catalog)
group by(a.authid, a.name, city)
having count(*)>=2;
RESULT
AI
NAME
CITY
COUNT
--------------------------------------------------------------11
aaa
delhi
2
EXPLANATION
The nested query determines the average of all the book prices present in the catalog. Then the
two tables author and catalog are joined. The tuples with year greater than 2000 and the price
greater than the average are selected.
2. Find the author of the book which has maximum sales.
SQL> select distinct(a.name)
from author a,catalog c,order_details odm
where a.authid=c.authid and odm.bookid=c.bookid
and exists (select od.bookid,sum(od.quantity)
from order_details od
where od.bookid=odm.bookid
group by bookid
having sum(od.quantity)>=all(select sum(quantity)
from order_details
group by bookid));
RESULT
NAME
---------ccc
42
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
EXPLANATION
The nested query selects the maximum value of quantity in the attribute quantity in table
order_details. Then the two tables catalog and order_details are joined. The tuple with the
maximum value of quantity is selected and the name of the author is displayed.
3. Demonstrate how you increase the price of books published by a specific
publisher by 10%.
SQL> update catalog
set price = 1.1 * price
where pubid in (select pubid
from publisher
where name=’DSC’);
RESULT
SQL> select * from catalog;
BOOKID
TITLE
AUTHID
PUBID CATID
YEAR PRICE
----------------------------------------------------------------------------------------------------------41
logic design
11
21
31
2003
300
42
graph theory
11
22
32
2002
480
43
c++ appl
13
23
33
1992
330
44
dbms mang
14
24
34
2008
248
45
c prog
14
25
35
2000
150
EXPLANATION
The nested query selects all the tuples with the publisher name as explicitly given by the
user.Then using the update command,the price is incremented by 10% in these tuples.
43
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
PROBLEM – 5
PROBLEM STATEMENT:
Consider the following database for a banking enterprise
BRANCH (branch_name: string, branch_city: string, assets: real)
ACCOUNT (accno: int, branch_name: string, balance: real)
CUSTOMER (customer_name: string, customer_street: string,
customer_city: string)
DEPOSITOR (customer_name: string, accno: int)
LOAN (loan_number: int, branch_name: string, amount: real)
BORROWER (customer_name: string, loan_number: int)
i) Create the above tables by properly specifying the primary keys
and the foreign keys.
ii) Enter atleast five tuples for each relation.
iii) Find all the customers who have atleast two accounts at the Main branch.
iv) Find all the customers who have an account at all the branches located in
a specific city.
v) Demonstrate how you delete all account tuples at every branch located in
a specific city.
vi) Generate suitable reports.
vii) Create suitable front end for querying and displaying the results.
44
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
SCHEMA DESCRIPTION:
BRANCH
BRANCH_NAME
BRANCH_CITY ASSETS
ACCOUNT
ACC_NO BRANCH_NAME BALANCE
DEPOSITOR
CUSTOMER_NAME
---------------------------
ACCNO
-----------
_
CUSTOMER
CUSTOMER_NAME CUSTOMER_STREET CUSTOMER_CITY
LOAN
LOAN_NO
BRANCH_NAME
AMOUNT
BORROWER
CUSTOMER_NAME
----------------------------
LOAN_NO
---------------
45
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
ER DIAGRAM:
CITY
LNO
GIVES
OUT
BNAME
LNAME
BRANCH
LOAN
ASSETS
MANAGES
BORROWS
CNAME
CSTREET
AACCNO
CUSTOMER
ACCOUNT
CCITY
BALANCE
DEPOSITS
ACCNO
NAME
46
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table branch
(
branch_name varchar(25) primary key,
branch_city varchar(20) not null,
assets decimal(10,2) not null
);
Table created.
TABLE DESCRIPTION
SQL> describe branch;
Name
Null?
Type
----------------------------------------------------------------------------BRANCH_NAME
NOT NULL
VARCHAR2(25)
BRANCH_CITY
NOT NULL
VARCHAR2(20)
ASSETS
NOT NULL
NUMBER(10,2)
INSERTING TUPLES
SQL> insert into branch values('jaynagar’,'bangalore','15000000');
SQL>insert into branch values(‘basavanagudi’,’bangalore’,’25000000’);
SQL>insert into branch values(‘noida’,’delhi’,’50000000’);
SQL>insert into branch values(‘marine drive’,’mumbai’,’40000000’);
SQL>insert into branch values(‘grren park’,’delhi’,’30000000’);
BRANCH_NAME
BRANCH_CITY
ASSETS
-----------------------------------------------------------------------------------jaynagar
basavanagudi
noida
marine drive
green park
bangalore
bangalore
delhi
mumbai
delhi
15000000
25000000
50000000
40000000
30000000
TABLE CREATION
SQL> create table account
(
accno int primary key,
branch_name varchar(25) not null,
balance decimal(10,2) not null,
foreign key(branch_name) references branch(branch_name)
);
Table created.
47
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE DESCRIPTION
SQL> describe account;
Name
Null?
Type
-----------------------------------------------------------------------------------ACCNO
NOT NULL
NUMBER(38)
BRANCH_NAME
NOT NULL
VARCHAR2(25)
BALANCE
NOT NULL
NUMBER(10,2)
INSERTING TUPLES
SQL>insert into account values(‘123’,’jaynagar’,’25000’);
SQL>insert into account values(‘156’,’jaynagar’,’30000’);
SQL>insert into account values(‘456’,‘basavanagudi’,’15000’);
SQL>insert into account values(‘789’,‘noida’,’25000’);
SQL>insert into account values(‘478’,’marine drive’,’48000’);
SQL>insert into account values(‘778’,’green park’,’60000’);
SQL>insert into account values(‘189’,’basavanagudi’,’48888’);
SQL> select * from account;
ACCNO
BRANCH_NAME
BALANCE
---------- ------------------------- -----------------------------------123
jaynagar
25000
156
jaynagar
30000
456
basavanagudi
15000
789
noida
25000
478
marine drive
48000
778
green park
60000
189
basavanagudi
48888
TABLE CREATION
SQL> create table customer
(
customer_name varchar(25) primary key,
customer_street varchar(25) not null,
customer_city varchar(25) not null
);
Table created.
TABLE DESCRIPTION
SQL> describe customer;
Name
Null?
Type
----------------------------------------------------------------------------CUSTOMER_NAME
NOT NULL
VARCHAR2(25)
CUSTOMER_STREET
NOT NULL
VARCHAR2(25)
CUSTOMER_CITY
NOT NULL
VARCHAR2(25)
48
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
INSERTING TUPLES
SQL> insert into customer values(‘ramu’,’jaynagar’,’bangalore’);
SQL> insert into customer values(‘kumar’,’basavanagudi’,’bangalore’);
SQL> insert into customer values(‘john’,’noida’,’delhi’);
SQL> insert into customer values(‘mike’,’marine drive’,’mumbai’);
SQL> insert into customer values(‘sachin’,’green park’,’delhi’);
SQL> select * from customer;
CUSTOMER_NAME
CUSTOMER_STREET
CUSTOMER_CITY
------------------------------------------------------------------------------------------------------ramu
jaynagar
bangalore
kumar
basavanagudi
bangalore
john
noida
delhi
mike
marine drive
mumbai
sachin
green park
delhi
TABLE CREATION
SQL> create table depositor
(
customer_name varchar(25) not null,
accno int not null,
foreign key (customer_name) references customer (customer_name)
foreign key(accno) references account(accno) on delete cascade
);
Table created.
TABLE DESCRIPTION
SQL> describe depositor;
Name
Null?
Type
----------------------------------------------------------------------------CUSTOMER_NAME
NOT NULL
VARCHAR2(25)
ACCNO
NOT NULL
NUMBER(38)
INSERTING TUPLES
SQL> insert into depositor values(‘ramu’,’123’);
SQL> insert into depositor values(‘ramu’,’156’);
SQL> insert into depositor values(‘ramu’,’189’);
SQL> insert into depositor values(‘kumar’,’456’);
SQL> insert into depositor values(‘john’,’789’);
SQL> insert into depositor values(‘mike’,’478’);
SQL> insert into depositor values(‘sachin’,’778’);
49
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
SQL> select *from depositor;
CUSTOMER_NAME
ACCNO
-------------------------------------------------ramu
123
ramu
156
ramu
189
kumar
456
john
789
mike
478
sachin
778
TABLE CREATION
SQL> create table loan
(
loan_number int primary key,
branch_name varchar(25) not null,
amount decimal(10,2) not null,
foreign key(branch_name) references branch (branch_name)
);
Table created.
TABLE DESCRIPTION
SQL> describe loan;
Name
Null?
Type
-------------------------------------------------------------------------LOAN_NUMBER
NOT NULL NUMBER(38)
BRANCH_NAME
NOT NULL VARCHAR2(25)
AMOUNT
NOT NULL
NUMBER(10,2)
INSERTING TUPLES
SQL> insert into loan values(‘1111’,’jaynagar’,’250000’);
SQL> insert into loan values(‘2222’,’basavanagudi’,’350000’);
SQL> insert into loan values(‘3333’,’noida’,’150000’);
SQL> insert into loan values(‘4444’,’marine drive’,’1500000’);
SQL> insert into loan values(‘5555’,’green park’,’7500000’);
SQL> select *from loan;
LOAN_NUMBER
BRANCH_NAME
AMOUNT
----------------------------------------------------------------------------------1111
jaynagar
250000
2222
basavanagudi
350000
3333
noida
150000
4444
marine drive
1500000
5555
green park
7500000
50
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
TABLE CREATION
SQL> create table borrower
(
customer_name varchar(25) not null,
loan_number int not null,
foreign key(customer_name) references customer (customer_name),
foreign key (loan_number) references loan (loan_number),
primary key(customer_name,loan_number)
);
Table created.
TABLE DESCRIPTION
SQL> describe borrower;
Name
Null?
Type
----------------------------------------------------------------------------CUSTOMER_NAME
NOT NULL
VARCHAR2(25)
LOAN_NUMBER
NOT NULL
NUMBER(38)
INSERTING TUPLES
SQL> insert into borrower values(‘ramu’,’1111’);
SQL> insert into borrower values(‘kumar’,’2222’);
SQL> insert into borrower values(‘john’,’3333’);
SQL> insert into borrower values(‘mike’,’4444’);
SQL> insert into borrower values(‘sachin’,’5555’);
SQL> select *from borrower;
CUSTOMER_NAME
LOAN_NUMBER
------------------------------------------------------------ramu
1111
kumar
2222
john
3333
mike
4444
sachin
5555
QUERIES
1. Find all the customers who have atleast two accounts at the Main branch.
SQL> select distinct(customer_name), count(*)
from account a, depositor d
where a.accno=d.accno
and d.accno in (select accno from account
where branch_name=’jaynagar’)
group by d.customer_name
having count(*)>=2;
51
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
RESULT
CUSTOMER_NAME
COUNT(*)
-----------------------------------------------------ramu
2
EXPLANATION
The above query is similar to a SELECT-JOIN-PROJECT sequence of relational algebra
operations and such queries are called select-join queries. In the WHERE clause, branch_name =
‘jaynagar’ specifies the main branch and a.accno = d.accno is a join condition for the join
operation on the two relations account and depositor. Then the GROUP BY clause is used to subgroup the tuples based on the grouping attributes branch_name and customer_name. The
HAVING clause provides a condition count (*) >= 2 on the groups of tuples. Only the groups that
satisfy the condition are retrieved in the result of the query.
2. Find all the customers who have an account at all the branches located in a
specific city.
SQL> select d.customer_name
from account a, depositor d, branch b
where b.branch_name=a.branch_name and a.accno=d.accno
and b.branch_city=’bangalore’
having count(distinct b.branch_name)=(select count(branch_name)
from branch
where branch_city=’bangalore’)
group by customer_name;
RESULT
CUSTOMER_NAME
------------------------ramu
EXPLANATION
The inner query counts the number of branches in ‘bangalore’ which is used to compare with the
number of branches in Bangalore in which a customer has accounts. We join the account,
depositor and branch tables by specifying the appropriate join conditions and selecting only those
tuples having branches in Bangalore grouped by the customer name.
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='delhi');
2 rows deleted.
52
www.bookspar.com
www.bookspar.com | Website for students | VTU NOTES
RESULT
SQL> select *from account;
ACCNO BRANCH_NAME
BALANCE
----------------------------------------------------------------------123
jaynagar
25000
156
jaynagar
30000
456
basavanagudi
15000
478
marine drive
48000
189
basavanagudi
48888
SQL> select *from depositor;
CUSTOMER_NAME
ACCNO
-----------------------------------------------ramu
123
ramu
156
ramu
189
kumar
456
mike
478
EXPLANATION
The nested query selects the tuples that satisfy the selection condition branch_city = ‘delhi’ from
the relation branch .The IN operator compares the subtuple of value branch_name for each tuple
in account relation with the tuples produced by the nested query. Finally, the selected tuples are
deleted from the account relation. Here the account tuples containing the branches in delhi, i.e, in
noida and green park are deleted. Also, since depositor references the accno from the account
relation, for the accno deleted from the account table, corresponding tuples containing the same
accno in depositor relation are deleted.
53
www.bookspar.com
Download