Sei sulla pagina 1di 35

10CSL57

SRSIT/CSE/DBA/V.0.1
DATABASE APPLICATIONS LABORATORY
(COMMON TO CSE)

Subject Code: 10CSL57


1. Consider the following relations:
Student (snum: integer, sname: string, major: string, level: string, age: integer)
Class (name: string, meets at: string, room: string, d: integer)
Enrolled (snum: integer, cname: string)
Faculty (fid: integer, fname: string, deptid: integer)
The meaning of these relations is straightforward; for example, Enrolled has one record
per student-class pair such that the student is enrolled in the class. Level is a two
character code with 4 different values (example: Junior: JR etc)
Write the following queries in SQL. No duplicates should be printed in any of the
answers.
i.

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

ii.

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

iii.

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

iv.

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

v.

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

2. The following relations keep track of airline flight information:


Flights (no: integer, from: string, to: string, distance: integer, Departs: time,
arrives: time, price: real)
Aircraft (aid: integer, aname: string, cruisingrange: integer)
Certified (eid: integer, aid: integer)
Employees (eid: integer, ename: string, salary: integer)

CM/SB/S

Page 1

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

Note that the Employees relation describes pilots and other kinds of employees as well;
every pilot is certified for some aircraft, and only pilots are certified to fly.
Write each of the following queries in SQL.
i. Find the names of aircraft such that all pilots certified to operate them have
salaries more than Rs.80, 000.
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.
iii. Find the names of pilots whose salary is less than the price of the cheapest
route from Bengaluru to Frankfurt.
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.
v. Find the names of pilots certified for some Boeing aircraft.
vi. Find the aids of all aircraft that can be used on routes from Bengaluru to
New Delhi.
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)
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.

CM/SB/S

Page 2

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1
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.

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, categoryid: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.
5. 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)
DEPOSITOR(customer-name:string, accno:int)
CUSTOMER(customer-name:string, customer-street:string, customer-city:string)
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
CM/SB/S

Page 3

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1
ii. Enter at least five tuples for each relation
iii. Find all the customers who have at least 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.

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.
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.

CM/SB/S

Page 4

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1
PROBLEM 1

1. Consider the following relations:


Student (snum: integer, sname: string, major: string, level: string, age: integer)
Class (name: string, meets at: string, room: string, d: integer)
Enrolled (snum: integer, cname: string)
Faculty (fid: integer, fname: string, deptid: integer)
The meaning of these relations is straightforward; for example, Enrolled has one record
per student-class pair such that the student is enrolled in the class. Level is a two
character code with 4 different values (example: Junior: JR etc)
Write the following queries in SQL. No duplicates should be printed in any of the
answers.
vi.

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

vii.

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

viii.

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

ix.

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

Find the names of faculty members for whom the combined enrollment of the courses
that they teach is less than five
TABLE CREATION
SQL> create table student(
snum int not null primary key,
sname varchar(20),
major varchar(20),
level varchar(10),
age int not null );
Table created.

CM/SB/S

Page 5

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

INSERTING TUPLES
SQL> insert into student values(01,'raju','ise','JR',20);
Enter value for snum: 01
Enter value fro sname:raju
Enter value for major: ise
Enter value for level: JR
Enter value for age: 20
old 1: insert into student values(&snum,&sname,&major,&level,&age);
new 1: insert into student values(01,'raju','ise','JR',20);
1 row created
SQL> insert into student values(02,'ram','ise','JR',20);
SQL> insert into student values(03,'akash','ise','SR',21);
SQL> insert into student values(04,'akash','cse','SR',21);
SQL> insert into student values(05,'avinash','ise','FR',19);
SQL> insert into student values(06,'kumar','ise','FR',19);
SQL> insert into student values(07,'john','ise','SSR',22);
SQL> select * from student;
+------+---------+-------+-------+-----+
| snum | sname
| major | level | age |
+------+---------+-------+-------+-----+
|
1 | raju
| ise
| JR
| 20 |
|
2 | ram
| ise
| JR
| 20 |
|
3 | akash
| ise
| SR
| 21 |
|
4 | akash
| cse
| SR
| 21 |
|
5 | avinash | ise
| FR
| 19 |
|
6 | kumar
| ise
| FR
| 19 |
|
7 | john
| ise
| SSR
| 22 |
+------+---------+-------+-------+-----+

TABLE CREATION
SQL> create table class(
name varchar(10) primary key,
meets_at varchar(10),
room varchar(10),
fid int not null);
Table created
INSERTING TUPLES

CM/SB/S

Page 6

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

SQL> insert into class values('3','hall1','101',1);


Enter value for name: 3
Enter value fro meets_at : hall1
Enter value for room: 101
Enter value for fid: 1
old 1: insert into class values(&name,&meets_at,&room,&fid);
new 1: insert into class values('3','hall1','101',1);
1 row created
SQL> insert into class values('4','hall1','101',1);
SQL> insert into class values('5','hall2','102',2);
SQL> insert into class values('6','hall2','102',1);
SQL> insert into class values('7','hall2','103',3);
SQL> insert into class values('8','hall3','103',3);
SQL> insert into class values('1','hall3','103',3);
SQL> select * from class;
+------+----------+------+-----+
| name | meets_at | room | fid |
+------+----------+------+-----+
| 3
| hall1
| 101 |
1 |
| 4
| hall1
| 101 |
1 |
| 5
| hall2
| 102 |
2 |
| 6
| hall2
| 102 |
1 |
| 7
| hall2
| 103 |
3 |
| 8
| hall3
| 103 |
3 |
| 1
| hall3
| 103 |
3 |
+------+----------+------+-----+

TABLE CREATION
SQL> create table enrolled(
snum int not null,
cname varchar(10),
primary key(snum,cname),
foreign key(snum) references student(snum),
foreign key(cname) references class(name));
Table Created
INSERTING TUPLES
SQL> insert into enrolled values(1,3);
Enter value for snum: 1
Enter value for cname : 3
old 1: insert into class values(&snum,&cname);
CM/SB/S

Page 7

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

new 1: insert into class values(1,3);


1 row created
SQL> insert into enrolled values(1,4);
SQL> insert into enrolled values(1,5);
SQL> insert into enrolled values(2,5);
SQL> insert into enrolled values(3,5);
SQL> insert into enrolled values(4,5);
SQL> insert into enrolled values(4,1);
SQL> select * from enrolled;
+------+-------+-----+
| snum | cname |
+------+-------+
|
1 | 3
|
|
1 | 4
|
|
1 | 5
|
|
2 | 5
|
|
3 | 5
|
|
4 | 1
|
|
4 | 5
|
+------+-------+

TABLE CREATION
SQL> create table faculty(
fid int not null primary key,
fname varchar(25),
deptid int not null,
foreign key(fid) references class(fid));
Table created
INSERTING TUPLES
SQL> insert into faculty values(1,'Prof Harshit',200);
Enter value for fid: 1
Enter value for fname : Prof Harshit
Enter values for deptid: 200
old 1: insert into faculty values(&fid,&fname,&deptid);
new 1: insert into class values(1,3);
1 row created
SQL> insert into faculty values(2,'Prof Harshit',200);
SQL> insert into faculty values(3,'Prof Raghav',201);
SQL> insert into faculty values(4,'Prof Raghav',201);
CM/SB/S

Page 8

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

SQL> insert into faculty values(5,'Prof Pranav',202);


SQL> insert into faculty values(6,'Prof Pranav',202);
SQL> select * from faculty;
+-----+--------------+--------+
| fid | fname
| deptid |
+-----+--------------+--------+
|
1 | Prof Harshit |
200 |
|
2 | Prof Harshit |
200 |
|
3 | Prof Raghav |
201 |
|
4 | Prof Raghav |
201 |
|
5 | Prof Pranav |
202 |
|
6 | Prof Pranav |
202 |
+-----+--------------+--------+

QUERIES
i.

Find the names of all Juniors (level = JR) who are enrolled in a class taught by Prof.
Harshith
SQL> select distinct s.sname
from student s,class c, enrolled e, faculty f
where s.snum=e.snum and e.cname=c.name
and c.fid=f.fid and f.fname='Prof Harshit' and s.level='JR';

RESULT
+-------+
| sname |
+-------+
| raju |
| ram
|
+-------+

ii.

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

SQL> select c.name from class c where c.room='101' or c.name in(select


e.cname from enrolled e group by e.cname having count(*)>=5);
RESULT
name
--------------------3
4
iii.

Find the names of all students who are enrolled in two classes that meet at the same
time.
SQL> select distinct s.sname
from student s
where s.snum in(select a.snum
from enrolled a, enrolled b, class a1, class b1

CM/SB/S

Page 9

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1
where a.snum=a.snum and a.cname<>b.cname
and a.cname=a1.name and b.cname=b1.name
and a1.meets_at=b1.meets_at);

RESULT
+-------+
| sname |
+-------+
| raju |
+-------+

iv.

Find the names of faculty members who teach in every room in which some class is
taught
SQL> select distinct f.fname
from faculty f
where not exists(select c.room
from class c
where c.fid=f.fid);

RESULT
+-------------+
| fname
|
+-------------+
| Prof Raghav |
| Prof Pranav |
+-------------+

v.

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

SQL> select distinct f.fname


from faculty f
where 5 >(select count(e.snum)
from class c, enrolled e
where c.name=e.cname and c.fid=f.fid);
RESULT
+--------------+
| fname
|
+--------------+
| Prof Harshit |
| Prof Raghav |
| Prof Pranav |
+--------------+

CM/SB/S

Page 10

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1
PROBLEM-2

2. The following relations keep track of airline flight information:


Flights (no: integer, from: string, to: string, distance: integer, Departs: time,
arrives: time, price: real)
Aircraft (aid: integer, aname: string, cruisingrange: integer)
Certified (eid: integer, aid: integer)
Employees (eid: integer, ename: string, salary: integer)
Note that the Employees relation describes pilots and other kinds of employees as well;
every pilot is certified for some aircraft, and only pilots are certified to fly.
Write each of the following queries in SQL.
i.

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

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.

iii.

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

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.

v.

Find the names of pilots certified for some Boeing aircraft.

vi.

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

TABLE CREATION
SQL> create table flights(
flight_no int not null primary key,
flight_from varchar(20),
flight_to varchar(20),
distance int,
departure datetime null,
arrival datetime null,
price decimal(10,2));
Table created
INSERTING TUPLES
CM/SB/S

Page 11

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

SQL> insert into flights values(7,'bangalore','mumbai',500,'2012-12-06


22:34:46', 3, 5000);
1 row created
SQL> select * from flights;

TABLE CREATION
SQL> create table aircraft(
aid int not null primary key,
aname varchar(20),
cruisingrange int null);
Table Created
INSERTING TUPLES
SQL> insert into aircraft values(117,'kingfisher',10000);
1 row created
SQL> select * from aircraft;

TABLE CREATION
SQL> create table certified(
CM/SB/S

Page 12

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1
eid int not null,
aid int not null,
primary key(eid,aid),
foreign key(aid) references aircraft(aid));

Table Created
INSERTING TUPLES
SQL> insert into certified values(109,117);
1 row created
SQL> select * from certified;

TABLE CREATION
SQL> create table employees(
eid int not null,
ename varchar(20),
salary int not null,
primary key(eid),
foreign key(eid) references certified(eid));
Table Created
INSERTING TUPLES
SQL>insert into employees values(100,pranav,20000);
1 row created
SQL> select * from employees;

QUERIES
i.

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

Page 13

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

SQL> select distinct a.aname


from aircraft a
where a.aid in (select c.aid
from certified c, employees e
where c.eid=e.eid and
not exists(select * from employees e1
where e1.eid=e.eid and e.salary<80000));
RESULT
+------------+
| aname
|
+------------+
| indian
|
| kingfisher |
+------------+

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.
SQL> select c.eid,max(a.cruisingrange)
from certified c, aircraft a
where c.aid=a.aid
group by c.eid
having count(*)>3;
RESULT
+-----+----------------------+
| eid | max(a.cruisingrange) |
+-----+----------------------+
| 115 |
10000 |
+-----+----------------------+

iii.

Find the names of pilots whose salary is less than the price of the cheapest route from
Bengaluru to Frankfurt.
SQL> select distinct e.ename
from employees e
where e.salary < (select min(f.price)
from flights f
where f.flight_from='bangalore' and f.flight_to='Frankfurt');

RESULT
+----------+
| ename
|
+----------+
| pranav
|
| pranav p |
| ramesh
|

CM/SB/S

Page 14

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

| rajiv
|
| shashank |
| suraj
|
| boeing
|
+----------+

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.
SQL> select temp.name, temp.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
c.eid=e.eid and a.cruisingrange>1000 group by a.aid, a.aname) as temp;
RESULT
+--------+------------+
| name
| avgsalary |
+--------+------------+
| sahara | 30000.0000 |
| sahara | 30000.0000 |
| indian | 30000.0000 |
| indian | 65000.0000 |

v.

Find the names of pilots certified for some Kingfisher aircraft.


SQL> select distinct e.ename
from employees e, certified c, aircraft a
where e.eid=c.eid and c.aid=a.aid and a.aname like'kingfisher%';
RESULT
+----------+
| ename
|
+----------+
| pranav
|
| pranav p |
| ramesh
|
| boeing
|
+----------+

vi.

Find the aids of all aircraft that can be used on routes from Bengaluru to New Delhi.
SQL> select a.aid
from aircraft a
where a.cruisingrange>(select min(f.distance)
from flights f
where f.flight_from='bangalore' and f.flight_to='mumbai');
CM/SB/S

Page 15

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

RESULT
+-----+
| aid |
+-----+
| 111 |
| 112 |
| 113 |
| 114 |
| 115 |
| 116 |
| 117 |
| 118 |
| 119 |
| 120 |
| 121 |
| 122 |
| 123 |
+-----+

CM/SB/S

Page 16

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1
PROBLEM-3

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

TABLE CREATION
SQL> create table student
(
regno varchar(10) primary key,
name varchar(15) ,
major varchar(30) ,
bdate date
);

Table created.
INSERTING TUPLES
SQL> insert into student values ('&regno','&name','&major','&bdate');
SQL> insert into student values ('&regno','&name','&major','&bdate');
Enter value for regno: 101

CM/SB/S

Page 17

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

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

TABLE CREATION
SQL> create table course
(
course integer primary key,
cname varchar(30) ,
dept varchar(30)
);
Table created.

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> select * from course;
COURSE
CNAME
DEPT
---------------------------------------------------------------------1
mtech
cse
2
me
cse
3
mtech
ise
4
bsc
cse

5
CM/SB/S

mca

ise
Page 18

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

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.

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> select * from text;
BOOK_ISBN
BOOK_TITLE
PUBLISHER
---------------------------------------------------------------------------------------AUTHOR
-----------------------------111
linux
pearson
sumithaba das
222
tenenbaum
333
micheal folk

compiler design

tmh

computer graphics

pearson

444

data structures

555

file structures

venugopal

tmh
godse

himalaya

TABLE CREATION
SQL> create table enroll
(

CM/SB/S

Page 19

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1
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.

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> select * from enroll;
REGNO COURSE
SEM MARKS
---------------------------------------------------103
3
5
590
105
5
6
789
102
1
4
678
101
1
6
430

104

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.

INSERTING TUPLES
SQL>insert into book_adoption values
('&course','&sem','&book_isbn');
Enter value for course: 1

CM/SB/S

Page 20

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.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> select * from book_adoption;
COURSE
SEM BOOK_ISBN
----------------------------------------------------1
5
111
1
6
222
1
4
333
3
6
444

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

data structures

venugopal

tenenbaum
333
micheal folk
444
tmh

CM/SB/S

Page 21

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

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.

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;

RESULT
COURSE BOOK_ISBN BOOK_TITLE
------------------------------------------------------------1
222
compiler design
1
333
computer graphics
1
111
linux

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

CM/SB/S

Page 22

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1
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

CM/SB/S

Page 23

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1
Problem-4

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, categoryid: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
TABLE CREATION
SQL> create table author
(
authid number(4) primary key,
name varchar(10),
city varchar(10),
country varchar(10)
);
Table created.

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

CM/SB/S

Page 24

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

Enter value for country: india


old 2: ('&authid','&name','&city','&country')
new 2: ('11','aaa','delhi','india')
1 row created.
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

TABLE CREATION
SQL> create table publisher
(
pubid number(4) primary key,
name varchar(10),
city varchar(10),
country varchar(10)
);
Table created.

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> select * from publisher;
PUBID
NAME
CITY
COUNTRY
--------------------------------------------------------------------------------------------------------21
macgraw
delhi
india
22
subash
paris
france
23
annai
banglore
india
24
DSC
delhi
india

25
CM/SB/S

KSC

poland
Page 25

germany

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

TABLE CREATION
SQL> create table category
(
catid number(4) primary key,
des varchar(20)
);
Table created.

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),
catid number(4) references category(catid),
year number(4),
price number(4)
);
Table created.

INSERTING TUPLES
SQL> insert into catalog values ('&bookid','&title','&authid','&pubid','&catid','&year','&price');

CM/SB/S

Page 26

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

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> 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
TABLE CREATION

14

25

35

2000

150

SQL> create table order_details


(
ordno number(4) primary key,
bookid number(4) references catalog(bookid),
quantity number(4)
);
Table created.

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> select * from order_details;


ORDNO BOOKID QUANTITY
------------------------------------------------------------------

CM/SB/S

Page 27

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1
91
92
93
94

41
42
43
43

95

45

50
100
150
25

160

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

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


publisher by 10%.
SQL> update catalog
set price = 1.1 * price

CM/SB/S

Page 28

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1
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

CM/SB/S

Page 29

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1
PROBLEM-5

5. 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)
DEPOSITOR(customer-name:string, accno:int)
CUSTOMER(customer-name:string, customer-street:string, customer-city:string)
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 at least five tuples for each relation
iii. Find all the customers who have at least 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

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.

INSERTING TUPLES
SQL> insert into branch values('jaynagar,'bangalore','15000000');

BRANCH_NAME
BRANCH_CITY
ASSETS
-----------------------------------------------------------------------------------CM/SB/S

Page 30

2012-2013

10CSL57
jaynagar
basavanagudi
noida
marine drive
green park

SRSIT/CSE/DBA/V.0.1
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.

INSERTING TUPLES
SQL>insert into account values(123,jaynagar,25000);
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.

INSERTING TUPLES
SQL> insert into customer values(ramu,jaynagar,bangalore);
SQL> select * from customer;
CUSTOMER_NAME

CM/SB/S

CUSTOMER_STREET

Page 31

CUSTOMER_CITY

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

------------------------------------------------------------------------------------------------------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.

INSERTING TUPLES
SQL> insert into depositor values(ramu,123);
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.

INSERTING TUPLES
SQL> insert into loan values(1111,jaynagar,250000);

CM/SB/S

Page 32

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

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

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.

INSERTING TUPLES
SQL> insert into borrower values(ramu,1111);
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;

RESULT
CM/SB/S

Page 33

2012-2013

10CSL57

SRSIT/CSE/DBA/V.0.1

CUSTOMER_NAME
COUNT(*)
-----------------------------------------------------ramu
2

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

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.

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

CM/SB/S

Page 34

2012-2013

10CSL57
ramu
kumar
mike

CM/SB/S

SRSIT/CSE/DBA/V.0.1
189
456
478

Page 35

2012-2013

Potrebbero piacerti anche