chapter 1 · a mini project should be implemented by the students in teams. the maximum size of a...
TRANSCRIPT
Database Application Laboratory 1
Dr. A.I.T.,Dept of CSE
CHAPTER 1
Course objectives:
1. Provide a strong formal foundation in database concepts and technology
2. Familiarize the students with the database environments towards an information-oriented data-
processing oriented framework.
3. Understand the relational data model and systematic database design approaches covering
conceptual design, logical design.
4. To present the concepts and techniques relating to query processing by SQL
PART – A
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. Relevant queries other than the ones listed along with the exercises may also be asked in the
examinations.
4. Questions must be asked based on lots.
1. Consider the following relations:
STUDENT(snum:integer, sname:string, major:string, level:string, age:integer)
CLASS(cname:string, meets at:time, room:string, fid:integer)
ENROLLED(snum:integer, cname:string)
FACULTY(fid:integer, fname:string, deptid:integer)
The meaning of these relations is straight forward; 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 duplicate 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 tech is less than five.
Sub Title : DATABASE APPLICATIONS LABORATORY
Sub Code: CSL57 No. of Credits: 1.5= 0 :0 : 1.5 (L-T-P) No. of lecture hours / week :
3
Exam Duration : 3 hrs CIE + SEE = 50 + 50 =100
Database Application Laboratory 2
Dr. A.I.T.,Dept of CSE
(vi) Generation of suitable reports.
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
vi) Generation of suitable reports
3. 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 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) Generation of suitable reports.
4. The following tables are maintained by a book dealer.
AUTHOR (author-id: int, name: string, city: string, country: string)
PUBLISHER (publisher-id: int, name: string, city: string, country: string)
CATALOG (book-id: int, title: string, author-id: int, publisher-id: int, category-id: int, year: int,
price: int)
CATEGORY (category-id: int, description: string)
ORDER-DETAILS (order-no: int, book-id: int, quantity: int)
i) Create the above tables by properly specifying the primary keys and foreign keys.
(i) Find the names of aircraft such that all pilots certified to operate them have salaries more than
80,000.
(ii) For each pilot who is certified for more than three aircrafts, find the eid and the maximum
cruising range of the aircraft for which she or he is certified.
(iii) Find the names of the pilots whose salary is less than the price of the cheapest route from
Bengaluru to Frankfurt.
(iv) For all aircraft with cruising range over 1000 Kms, find the name of the aircraft and the
average salary of all pilots certified for this aircraft.
Database Application Laboratory 3
Dr. A.I.T.,Dept of CSE
ii) Enter atleast five tuples for each tables.
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) Generation of suitable reports.
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 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) Generation of suitable reports.
PART – B
A mini project should be implemented by the students in teams. The maximum size of a team
can be 2 from the same batch. The students have to finalize a project topic by discussing with the
faculty. The mini project must be carried out in the college only.
The tasks when implementing mini project would be:
1. Understand the complete domain knowledge of the application and derive the complete
data requirement specification for the mini project.
2. Design the ER diagram for the application
3. Design Relational Scheme diagram for the application
4. Normalization of the relational design.
5. Implement minimum 5 queries for the application
6. Documentation & submission of report.
General guidelines:
• Database for the project - Oracle / MySQL/ DB2 / SQL Server etc.
Sample Mini Projects.
• Inventory Control System.
• Material Requirement Processing.
• Hospital Management System.
• Railway Reservation System.
• Web Based User Identification System.
Database Application Laboratory 4
Dr. A.I.T.,Dept of CSE
• Timetable Management System.
• Hotel Management System
• Placement management system
• Library management system
• Personal Information System.
Note: In the examination, the marks will be evaluated based on Part A and project
demonstration, project report and viva-voce.
Course Outcomes:
1. Understand underlying concepts of database technologies.
2. Design and implement a database schema for a given problem-domain.
3. Understand, analyze, and apply common SQL Statements including DDL, DML and DCL
statements to perform different operations.
4. Design and implement a database for a given problem according to well-known design
principles that balance data retrieval performance with data consistency.
TEXT BOOK:
1. Fundamental of Database Systems by Elmasri and Navathe, 7th Edition, Addison-Wesley,
2015 ISBN-10: 0133970779, ISBN-13: 978-0133970777
REFERENCE BOOKS/WEBLINKS:
1. Database Management Systems by Raghu Ramakrishnan and Johannes Gehrke – 3rd Edition,
McGraw-Hill, 2006.
2. An Introduction to Database Systems by C.J. Date, A. Kannan, S. Swamynathan, 8th Edition,
Pearson Education,2013.
3. Data Base system Concepts by Silberschatz, Korth and Sudharshan, 5th edition McGraw Hill,
2011.
Cos Mapping with POs
CO1 PO2,PO3
CO2 PO2,PO3
CO3 PO1,PO3,PO4,PO5
CO4 PO3,PO4,PO5
Database Application Laboratory 5
Dr. A.I.T.,Dept of CSE
CHAPTER 2
INTRODUCTION
OVERVIEW
Databases store data and metadata. Data are the individual facts that are used to derive
information. Metadata describe the content, quality, condition, availability, and characteristics of
data. Database Management Systems (DBMS) is used to modify the data.
There are varieties of database types: Sequential files, Hierarchical databases, Network
databases, and Relational databases. The recent database type is Object-Relational database,
which is essentially a relational database with some Object properties.
Relational databases become popular because it was easy to modify the schema. It is very easy to
add tables and columns to the schema, and doing so does not affect the remainder of the schema,
and more important, does not affect the applications that access the database schema. Older
databases required the databases to be restructured and the applications to be modified. Avoiding
database and application maintenance is an important benefit and the reason for the switch.
Relational databases consist of independent tables. Relational Database Management Software
(RDBMS) does not know how the records in the table are related.
ORACLE
Oracle is the largest database manufacturer and the second-largest software manufacturer in the
world. The company began as a relational database manufacturer. In the beginning, Oracle touted
its software as “being able to run on any platform”. This openness has been most attractive to
companies; an Oracle has tried to maintain its image as an open product. Oracle was at a good
place when industry became extremely interested in moving away from network databases and
the mainframe.
Oracle Database 10g is an entry-level database based on the Oracle Database 10g Release 2 code
base that's free to develop, deploy, and distribute; fast to download; and simple to administer.
Oracle Database is a great starter database for:
• Developers working on PHP, Java, .NET, and Open Source applications
• DBAs who need a starter database for training and deployment
• Independent Software Vendors (ISVs) and hardware vendors who want a starter database
• Educational institutions and students who need database for their curriculum
Database Application Laboratory 6
Dr. A.I.T.,Dept of CSE
The main use of the Oracle database system is to store and retrieve data for applications. The
Oracle database includes different languages and interfaces that allow programmers to access
and manipulate the data in the database. The ANSI standard Structured Query Language (SQL)
provides basic functions for data manipulation, transaction control, and record retrieval from the
database. However, most end users interact with Oracle through applications that provide an
interface that hides the underlying SQL and its complexity.
BASICS
Structured Query Language (SQL), which is an ANSI standard language for interacting with
relational databases, is the main tool for extracting the information.
A database is a representation of a real-world thing called an Entity. Examples of entities are
vehicles, employees, customers, fish, buildings, and even things such as baseball teams. The
database stores facts about the entity in an organized framework, model, or schema. These facts
are called attributes.
An Instance is one occurrence of an entity. Each entity must have an identifier, which is one or
more attributes that make each entity instance unique from any other instance. The identifier
should contain a value that does not change. Examples of identifiers are student IDs, payroll
numbers, or social security numbers. If the entity does not have an attribute that can be used as
an identifier, an artificial identifier can be created. The identifier on an entity is often called a
primary key. A foreign key is a set of attributes of the considered table that exists as a primary
key attributes in another table. Database records are matched (joined) through the use of primary
and foreign keys.
Normalization is a process consisting of series of steps, which is used to group the database
attributes. The purpose of this design is to ensure that the tables within the database are space
efficient and performance efficient.
• Zero Normal Form—each of the relations (tables) has a unique identifier (primary key).
• First Normal Form—Separate the repeating groups of attributes or multi-valued attributes
into a relation of their own. Be sure to form composite keys.
• Second Normal Form—Establish full functional dependency by separating out attributes
that are not fully dependent on the full primary keys.
• Third Normal Form—Remove transitive dependencies by separating attributes that are
dependent on a non key attribute.
Database Application Laboratory 7
Dr. A.I.T.,Dept of CSE
CHAPTER 3
STUDENT DATABASE
Consider the following relations:
STUDENT(snum:integer, sname:string, major:string, level:string, age:integer)
CLASS(cname:string, meets at:time, room:string, fid:integer)
ENROLLED(snum:integer, cname:string)
FACULTY(fid:integer, fname:string, deptid:integer)
Schema-Diagram:
STUDENT
snum sname major level age
CLASS
cname meets_at room fid
ENROLLED
snum cname
FACULTY
fid fname deptid
Creation of Tables:
create table student
(
snum int primary key,
sname varchar2(10),
major varchar2(10),
levels varchar2(2),
age number(2)
);
create table faculty
(
fid number(5) primary key,
fname varchar2(10),
deptid number(5)
);
Database Application Laboratory 8
Dr. A.I.T.,Dept of CSE
create table class
(
cname varchar2(10) primary key,
meetsat varchar2(10),
room varchar2(5),
fid number(5) references faculty(fid)
);
create table enrolled
(
snum int references student(snum),
cname varchar2(10) references class(cname)
);
Insertion of records:
insert into student values(1,'pratima','cs','jr',20);
insert into student values(2,'jyoti','is','jr',20);
insert into student values(3,'divya','is','sr',21);
insert into student values(4,'akki','civil','ss',18);
insert into student values(5,'usha','ec','sj',20);
insert into faculty values(11,'harshith',111);
insert into faculty values(22,'harish',111);
insert into faculty values(33,'archana',222);
insert into faculty values(44,'sita',333);
insert into faculty values(55,'anchal',222);
insert into class values('1st sem','9am-10am',125,11);
insert into class values('2nd sem','10am-11am',128,11);
insert into class values('3rd sem','11am-12pm',128,33);
insert into class values('4th sem','11am-12pm',126,11);
insert into class values('5th sem','10am-11am',128,22);
insert into enrolled values(1,'1st sem');
insert into enrolled values(2,'2nd sem');
insert into enrolled values(3,'3rd sem');
insert into enrolled values(4,'4th sem');
insert into enrolled values(5,'5th sem');
select * from enrolled;
SNUM CNAME
---------- ----------
1 1st sem
2 2nd sem
3 4th sem
4 3rd sem
5 5th sem
3 3rd sem
Database Application Laboratory 9
Dr. A.I.T.,Dept of CSE
4 4th sem
select * from student;
SNUM SNAME MAJOR LEVEL AGE
---------- ---------- ---------- ---------- -------
1 pratima cs jr 20
2 shruti is jr 20
3 chetan is sr 21
4 akki civil ss 18
5 usha ec sj 20
select * from faculty;
FID FNAME DEPTID
------- ------------ ----------
11 harshith 111
22 harish 111
33 archana 222
44 sita 333
55 anchal 222
select * from class;
CNAME MEETSAT ROOM FID
---------- --------------- ---------- -----
1st sem 9am-10am 125 11
2nd sem 10am-11am 128 22
3rd sem 11am-12pm 128 33
4th sem 11am-12pm 126 11
5th sem 10am-11am 128 22
6th sem 10am-11am 128 11
Queries:
(i) Find the names of all juniors(level=jr) who are enrolled in a class taught by
Prof.Harshith
select distinct s.sname
from student s,class c,enrolled e, faculty f
where f.fid=c.fid and
c.cname=e.cname and
e.snum=s.snum and
f.fname='harshith' and levels='jr' ;
SNAME
----------
Pratima
Database Application Laboratory 10
Dr. A.I.T.,Dept of CSE
(ii)Find the names of all classes that either meet in room R128 or have five or more
students enrolled.
select distinct(c.cname)
from class c,enrolled e
where c.cname=e.cname and
room='128' or e.cname IN (select cname
from enrolled
group by cname
having count(snum)>=5);
CNAME
----------
5th sem
2nd sem
3rd sem
(iii).find the names of all students who are enrolled in two classes that meet at the same
time.
select distinct s.sname
from student s
where s.snum in (select e1.snum
from enrolled e1,enrolled e2,class c1,class c2
where e1.cname=c1.cname and
e2.cname=c2.cname and
e1.snum=e2.snum and
e1.cname<>c2.cname and
c1.meetsat=c2.meetsat);
SNAME
----------
akki
chetan
(iv) Find the names of faculty members who teach in every room in which some class is
taught.
select f.fname
from faculty f
where not exists ((select c.room
from class c)
minus
(select c1.room
from class c1
where c1.fid=f.fid));
FNAME
----------
Harshith
Database Application Laboratory 11
Dr. A.I.T.,Dept of CSE
(v) Find the names of faculty members for whom the combined enrollment of the courses
that they take is less than five.
select distinct f.fname from faculty f
where 5> (select count(e.snum)
from class c,enrolled e
where c.cname=e.cname and
f.fid=c.fid);
FNAME
----------
harish
archana
sita
anchal
harshithy
Database Application Laboratory 12
Dr. A.I.T.,Dept of CSE
CHAPTER 4
AIRCRAFT DATABASE
The following relations keep track of airline flight information:
FLIGHTS(no:integer, from:string, to:string, distance:integer, departs:date, arrives:date,
price:real)
AIRCRAFT(aid:integer, aname:string, cruisingrange:integer)
CERTIFIED(eid:integer, aid:integer)
EMPLOYEES(eid:integer, ename:string, salary:integer)
Schema-Diagram:
FLIGHTS
no from to distance departs arrives price
AIRCRAFT
aid aname cruisingrange
CERTIFIED
eid aid
EMPLOYEES
eid ename salary
Creation of Tables:
create table flights
(
no integer primary key,
frm varchar(20),
end varchar(20),
dist int,
dept date,
arr date,
price real
);
Database Application Laboratory 13
Dr. A.I.T.,Dept of CSE
create table aircraft
(
aid int primary key,
aname varchar2(15),
crange int
);
create table certified
(
eid int refernces employees(eid),
aid int references aircraft(aid),
primary key(cid,aid)
);
create table employees
(
eid int primary key,
ename varchar2(15),
salary real
);
Insertion of records:
insert into flights values('255','bangalore','frankfurt','200','01-aug-2011','01-aug-2011','5000’);
insert into flights values('256','bangalore','frankfurt','200','01-aug-2011','01-aug-2011','8000’);
insert into flights values('257','bangalore','delhi','200','01-aug-2011','01-aug-2011','5000');
insert into flights values('258','bangalore','delhi','200','01-aug-2011','01-aug-2011','6000');
insert into flights values('259','bangalore','mangalore','200','01-aug-2011','01-aug-2011','8000’);
insert into aircraft values('685','boeing15','1000');
insert into aircraft values('686','boeing10','2000');
insert into aircraft values('687','skytrain','1000');
insert into aircraft values('688','avenger','100');
insert into certified values('101','685');
insert into certified values('102','686');
insert into certified values('101','687');
insert into certified values('101','688');
insert into certified values('101','686');
insert into certified values('103','685');
insert into certified values('103','687');
insert into employees values(101,'asha',90000);
insert into employees values(102,'arun',85000);
insert into employees values(103,'anand',3000);
insert into employees values(104,'ramya',4000);
Database Application Laboratory 14
Dr. A.I.T.,Dept of CSE
select * from flights;
NO FRM END DIST DEPT ARR PRICE
----- ----------- ----------- ------ ------------- --------------- ---------
255 bangalore frankfurt 200 01-AUG-11 01-AUG-11 5000
256 bangalore frankfurt 200 01-AUG-11 01-AUG-11 8000
257 bangalore delhi 200 01-AUG-11 01-AUG-11 5000
258 bangalore delhi 200 01-AUG-11 01-AUG-11 6000
259 bangalore mangalore 200 01-AUG-11 01-AUG-11 8000
select * from certified;
CID AID
---------- ----------
101 685
101 686
101 687
101 688
102 686
103 685
103 687
select * from employees;
EID ENAME SALARY
---------- --------------- ----------
101 asha 90000
102 arun 85000
103 anand 3000
104 ramya 4000
select * from aircraft;
AID ANAME CRANGE
---------- --------------- ----------
685 boeing15 1000
686 boeing10 2000
687 skytrain 1000
688 avenger 100
Queries:
(i) Find the names of aircraft such that all pilots ceritified to operate them have salaries
more than 80,000.
select distinct a.aname
from aircraft a,certified c,employees e
where a.aid=c.aid and c.cid=e.eid and e.salary>80000;
Database Application Laboratory 15
Dr. A.I.T.,Dept of CSE
ANAME
---------------
avenger
skytrain
boeing15
boeing10
(ii) For each pilot who is certified for more than three aircrafts, find the eid and the
maximum cruising range of the aircraft for which she or he is certified.
select distinct c.cid,max(a.crange)
from certified c,aircraft a
where c.aid=a.aid
group by c.cid
having count(c.cid)>3;
CID MAX(A.CRANGE)
---------- -------------
101 2000
(iii) Find the names of the pilots whose salary is less than the price of the cheapest route
from bengaluru to frankfurt.
select e.ename
from employees e
where e.salary<(select min(price)
from flights
where frm='bangalore' and end='frankfurt');
ENAME
---------------
anand
ramya
(iv) For all aircraft with cruisingrange over 1000 Kms,find the name of the aircraft and the
average salary of all pilots certified for this aircraft.
select name, avgsal
from(select a.aid,a.aname as name,avg(e.salary)
as avgsal from aircraft a,certified c,employees e
where a.aid=c.aid and c.cid=e.eid and crange>1000
group by a.aid,a.aname);
NAME AVGSAL
--------------- ----------
boeing10 87500
Database Application Laboratory 16
Dr. A.I.T.,Dept of CSE
CHAPTER 5
STUDENT ENROLLMENT DATABASE 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)
Schema-Diagram:
STUDENT
regno name major bdate
COURSE
courseno cname dept
ENROLL
regno courseno sem marks
BOOK _ ADOPTION
courseno sem book-ISBN
TEXT
book-ISBN book-title publisher author
Database Application Laboratory 17
Dr. A.I.T.,Dept of CSE
Creation of Tables:
create table student
(
regno varchar(5) primary key,
name varchar(10),
major varchar(10),
bdate date
);
create table course
(
cno int primary key,
cname varchar(10),
dept varchar(10)
);
create table enroll
(
regno references student,
cno references course,
sem int primary key,
marks int
);
create table text
(
isbn int primary key,
title varchar(15),
publisher varchar(10),
author varchar(10)
);
create table bad
(
cno references course,
sem references enroll,
isbn references text
);
Insertion of records:
insert into student values('cs42','mikhil','cse','17-dec-1986');
insert into student values('cs48','mujeeb','cse','02-sep-1986');
insert into student values('ec26','pradeep','cse','16-aug-1987');
insert into student values('ee37','majid','ise','28-may-1986');
Database Application Laboratory 18
Dr. A.I.T.,Dept of CSE
insert into student values('is48','wajid','ise','28-may-1986');
insert into course values(1,'.net','computer');
insert into course values(2,'j2ee','computer');
insert into course values(3,'mis','infosci');
insert into course values(4,'fs','infosci');
insert into course values(5,'oracle','computer');
insert into enroll values('cs42',1,6,98);
insert into enroll values('cs48',2,3,97);
insert into enroll values('ec26',5,5,50);
insert into enroll values('is48',3,7,90);
insert into enroll values('ee37',4,4,80);
insert into text values(101,'let us c','lpe','fahad');
insert into text values(102,'c++','abc','mujeeb');
insert into text values(103,'oracle','def','othman');
insert into text values(104,'.net','lpe','naushad');
insert into text values(105,'j2ee','pearson','mikhil');
insert into bad values(1,6,101);
insert into bad values(1,6,103);
insert into bad values(1,6,102);
insert into bad values(4,4,104);
insert into bad values(5,7,105);
select * from student;
REGNO NAME MAJOR BDATE
---------- ---------- ------------- -------------
cs42 mikhil cse 17-DEC-86
cs48 mujeeb cse 02-SEP-86
ec26 pradeep cse 16-AUG-87
ee37 majid ise 28-MAY-86
is48 wajid ise 28-MAY-86
select * from course;
CNO CNAME DEPT
---------- -------------- ------------
1 .net computer
2 j2ee computer
3 mis infosci
4 fs infosci
5 oracle computer
Database Application Laboratory 19
Dr. A.I.T.,Dept of CSE
select * from enroll;
REGNO CNO SEM MARKS
------------ ---------- ---------- ------------
cs42 1 6 98
cs48 2 3 97
ec26 5 5 50
is48 3 7 90
ee37 4 4 80
select * from text;
ISBN TITLE PUBLISHER AUTHOR
---------- --------------- ------------------ ---------------
101 let us c lpe fahad
102 c++ abc mujeeb
103 oracle def othman
104 .net lpe naushad
105 j2ee pearson mikhil
select * from bad;
CNO SEM ISBN
---------- ---------- ----------
1 6 101
1 6 103
1 6 102
4 4 104
5 7 105
Queries:
iii.Demonstrate how you add a new text book to the database and make this book be
adopted by some department.
insert into text values(106,'JAVA','pearson','Avril');
insert into bad values(2,7,106);
select * from text;
ISBN TITLE PUBLISHER AUTHOR
---------- --------------- ------------------ ---------------
101 let us c lpe fahad
102 c++ abc mujeeb
103 oracle def othman
104 .net lpe naushad
105 j2ee pearson mikhil
106 JAVA pearson Avril
Database Application Laboratory 20
Dr. A.I.T.,Dept of CSE
select * from bad;
CNO SEM ISBN
---------- ---------- ----------
1 6 101
1 6 103
1 6 102
4 4 104
5 7 105
2 7 106
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 2 books.
SELECT s.cno,t.title,b.isbn
FROM course s,bad b,text t
where s.cno=b.cno
AND b.isbn=t.isbn
AND s.dept='computer'
AND b.cno in (select b.cno
from bad b,course s
where b.cno=s.cno
group by b.cno
having count(*)>2)
order by t.title;
CNO TITLE ISBN
---------- --------------- ----------
1 c++ 102
1 let us c 101
1 oracle 103
v. List any department that has all its adopted books published by specific publisher.
select s.dept, t.publisher
from course s, bad b, text t
where s.cno=b.cno
and t.isbn=b.isbn
and publisher='lpe';
DEPT PUBLISHER
------------- --------------------
computer lpe
infosci lpe
Database Application Laboratory 21
Dr. A.I.T.,Dept of CSE
CHAPTER 6
BOOK DEALER DATABASE
The following tables are maintained by a book dealer:
AUTHOR (author_id: int, author_name: string, city: string, country: string)
PUBLISHER (publ_id: int, publ_name: string, publ_city: string, publ_country: string)
CATEGORY (category_id: int, description: string)
CATALOG (book-id: int, title: string, author_id: int, publisher_id: int, category_id: int,
cyear: int, price: int)
ORDER-DETAILS (order_no: int, book_id: int, qty: int)
Schema-Diagram:
AUTHOR
author_id author_name city country
PUBLISHER
publ_id publ_name publ_city publ_country
CATEGORY
category_id description
CATALOG
book-id title author_id publ_id category_id cyear price
ORDER-DETAILS
order_no book_id qty
Database Application Laboratory 22
Dr. A.I.T.,Dept of CSE
Creation of Tables:
create table author
(
aid int primary key,
name varchar(15),
city varchar(15),
country varchar(15)
);
create table publisher
(
pid int primary key,
name varchar(15) ,
city varchar(15) ,
country varchar(15)
);
create table category
(
cid int primary key,
description varchar(15)
);
create table catalog
(
bid int primary key,
title varchar(15),
aid references author,
pid references publisher,
cid references category,
year int,
price int
);
create table odetails
(
ono int,
bid references catalog,
qty int
);
Insertion of records:
insert into author values (1,'mik','bang','ind');
insert into author values (2,'muj','bang','ind');
insert into author values (3,'prad','tri','aus');
insert into author values (4,'maj','anan','ame');
insert into author values (5,'waj','anan','euro');
Database Application Laboratory 23
Dr. A.I.T.,Dept of CSE
insert into publisher values (101,'pearson','bang','ind');
insert into publisher values (102,'tata','mumbai','aus');
insert into publisher values (103,'sapna','che','euro');
insert into publisher values (104,'abc','tri','ame');
insert into publisher values (105,'xyz','anan','ind');
insert into category values (1001,'computer');
insert into category values (1002,'electronics');
insert into category values (1003,'maths');
insert into category values (1004,'science');
insert into category values (1005,'electrical');
insert into catalog values (111,'lib1',1,101,1001,2002,500);
insert into catalog values (112,'lib2',2,102,1002,2000,800);
insert into catalog values (113,'lib3',3,103,1003,2003,200);
insert into catalog values (114,'lib4',4,104,1001,2006,350);
insert into catalog values (115,'lib5',5,105,1004,2007,100);
insert into catalog values (116,'lib6',2,103,1005,2007,600);
insert into catalog values (117,'lib7',2,105,1002,2007,450);
insert into catalog values (118,'lib8',1,101,1001,2002,500);
insert into odetails values (1,111,2);
insert into odetails values (2,112,3);
insert into odetails values (3,111,5);
insert into odetails values (4,113,1);
insert into odetails values (5,114,2);
insert into odetails values (6,115,1);
insert into odetails values (1,114,2);
insert into odetails values (2,113,2);
select * from author;
AID NAME CITY COUNTRY
---------- --------------- --------------- ---------------
1 mik bang ind
2 muj bang ind
3 prad tri aus
4 maj anan ame
5 waj anan euro
select * from publisher;
PID NAME CITY COUNTRY
---------- --------------- --------------- ---------------
101 pearson bang ind
102 tata mumbai aus
103 sapna che euro
104 abc tri ame
105 xyz anan ind
Database Application Laboratory 24
Dr. A.I.T.,Dept of CSE
select * from category;
CID DESCRIPTION
---------- ---------------------
1001 computer
1002 electronics
1003 maths
1004 science
1005 electrical
select * from catalog;
BID TITLE AID PID CID YEAR PRICE
------- ------------ --------- -------- -------- ---------- ----------
111 lib1 1 101 1001 2002 500
112 lib2 2 102 1002 2000 800
113 lib3 3 103 1003 2003 200
114 lib4 4 104 1001 2006 350
115 lib5 5 105 1004 2007 100
116 lib6 2 103 1005 2007 600
117 lib7 2 105 1002 2007 450
118 lib8 1 101 1001 2002 500
select * from odetails;
ONO BID QTY
---------- ---------- ----------
1 111 2
2 112 3
3 111 5
4 113 1
5 114 2
6 115 1
1 114 2
2 113 2
Queries:
iii. Give the detail of the author who have 2 or more books in the catalog and the price of
the book is greater than the average price of the book in the catalog and the year of
publication after 2000
select name, city, country
from author
where aid in (select aid from catalog where year>2000 and
price>(select avg(price) from catalog)
group by aid
having count(*)>1);
Database Application Laboratory 25
Dr. A.I.T.,Dept of CSE
NAME CITY COUNTRY
----------- --------------- ---------------
mik bang ind
muj bang ind
iv. Find the author of the book which has maximum sales
select a.aid, name
from author a, catalog c
where a.aid=c.aid and
c.bid=(select bid
from odetails
group by bid
having sum(qty) = (select max(sum(qty))
from odetails
group by bid));
AID NAME
---------- ---------------
1 mik
v. Demonstrate how to increase the price of books published by a specific publisher by 10%
update catalog
set price=1.1*price
where pid in (select pid
from publisher
where name='abc');
select * from catalog;
BID TITLE AID PID CID YEAR PRICE
------- ------------ --------- -------- -------- ---------- ----------
111 lib1 1 101 1001 2002 500
112 lib2 2 102 1002 2000 800
113 lib3 3 103 1003 2003 200
114 lib4 4 104 1001 2006 385
115 lib5 5 105 1004 2007 100
116 lib6 2 103 1005 2007 600
117 lib7 2 105 1002 2007 450
118 lib8 1 101 1001 2002 500
Database Application Laboratory 26
Dr. A.I.T.,Dept of CSE
CHAPTER 7
BANK DATABASE
Consider the following database for a banking enterprise
BRANCH (bname: string, bcity: string, assets: real)
ACCOUNT (accno: int, bname: string, balance: real)
DEPOSITOR (cname: string, accno: int)
BANK_CUST (cname: string, cstreet: string, ccity: string)
LOAN (loan_no: int, bname: string, amount: real)
BORROWER (cname: string, loan_no: int)
Schema-Diagram:
BRANCH
bname bcity assets
ACCOUNT
accno bname balance
DEPOSITOR
cname accno
BANK_CUST
cname cstreet ccity
LOAN
loan_no bname amount
BORROWER
cname loan_no
Database Application Laboratory 27
Dr. A.I.T.,Dept of CSE
Creation of Tables:
create table branch
(
bname varchar(10) primary key,
city varchar(10),
assets real
);
create table account
(
accno int primary key,
bname varchar(10),
balance real,
foreign key(bname) references branch(bname)
);
create table cust
(
cname varchar(10) primary key,
cstreet varchar(10) not null,
city varchar(10) not null
);
create table depositor
(
cname varchar(10),
accno int,
primary key(cname,accno),
foreign key(accno) references account(accno) on delete cascade,
foreign key(cname) references cust(cname) on delete cascade
);
create table loan
(
lno int primary key,
bname varchar(10) not null,
amt real,
foreign key(bname) references branch(bname) on delete cascade
);
Database Application Laboratory 28
Dr. A.I.T.,Dept of CSE
create table borrower
(
cname varchar(10),
lno int,
primary key(cname,lno),
foreign key(cname) references cust(cname) on delete cascade,
foreign key(lno) references loan(lno) on delete cascade
);
Insertion of records:
insert into branch values('abc','bang',1200000);
insert into branch values('def','che',2000000);
insert into branch values('abn','mum',330000);
insert into branch values('xyz','hyd',555555);
insert into branch values('mno','bang',9999999);
insert into account values(1,'abc',25000);
insert into account values(2,'def',12000);
insert into account values(3,'def',1000);
insert into account values(4,'abn',10000);
insert into account values(5,'mno',600000);
insert into account values(6,'xyz',50000);
insert into cust values('mik','ab','bang');
insert into cust values('muj','cd','bang');
insert into cust values('maj','ef','che');
insert into cust values('waj','xy','del');
insert into cust values('prad','lm','mum');
insert into cust values('now','op','hyd');
insert into depositor values('mik',2);
insert into depositor values('muj',1);
insert into depositor values('muj',5);
insert into depositor values('prad',4);
insert into depositor values('maj',3);
insert into depositor values('waj',6);
insert into depositor values('mik',3);
insert into loan values(1,'abc',5000);
insert into loan values(2,'def',1500);
insert into loan values(3,'abn',10000);
insert into loan values(4,'xyz',3500);
insert into loan values(5,'mno',20000);
Database Application Laboratory 29
Dr. A.I.T.,Dept of CSE
insert into borrower values('mik',2);
insert into borrower values('muj',1);
insert into borrower values('prad',3);
insert into borrower values('maj',4);
insert into borrower values('waj',5);
select *from branch;
BNAME CITY ASSETS
---------- ---------- ----------
abc bang 1200000
def che 2000000
abn mum 330000
xyz hyd 555555
mno bang 9999999
select *from account;
ACCNO BNAME BALANCE
---------- ---------- ----------
1 abc 25000
2 def 12000
3 def 1000
4 abn 10000
5 mno 600000
6 xyz 50000
select *from cust;
CNAME CSTREET CITY
---------- ------------ ----------
mik ab bang
muj cd bang
maj ef che
waj xy del
prad lm mum
now op hyd
select *from depositor;
CNAME ACCNO
---------- ------------
mik 2
muj 1
muj 5
prad 4
maj 3
waj 6
mik 3
Database Application Laboratory 30
Dr. A.I.T.,Dept of CSE
select *from loan;
LNO BNAME AMT
---------- ------------ ----------
1 abc 5000
2 def 1500
3 abn 10000
4 xyz 3500
5 mno 20000
select *from borrower;
CNAME LNO
------------- ----------
mik 2
muj 1
prad 3
maj 4
waj 5
Queries:
iii. Find all the customers who atleast two accounts at the MAIN branch.
select cname
from account a, depositor d
where a.accno=d.accno and
bname='def'
group by cname
having count(*)>1;
CNAME
----------
mik
iv. Find all the customers who have an account at all branches located in a specific city
select cname
from cust c
where not exists (select bname
from branch
where city='bang')
minus
(select bname
from depositor d, account a
where d.accno=a.accno and d.cname=c.cname)
and exists (select bname
from branch
where city='bang');
CNAME
------------
muj
Database Application Laboratory 31
Dr. A.I.T.,Dept of CSE
v. Demonstrate how you delete all account tuples at every branch located in a specific city.
delete from account
where bname in (select bname
from branch
where city='che');
2 rows deleted.
select *from account;
ACCNO BNAME BALANCE
----------- ------------ ---------------
1 abc 25000
4 abn 10000
5 mno 600000
6 xyz 50000