Upload
hadien
View
228
Download
2
Embed Size (px)
Citation preview
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
1
PROGRAM NO: 1
Consider the following relations for College Database:
Student (snum: integer, sname: string, major: string, level: string, age:
integer)
Class (name: string, meets at: string, room: string, fid: 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.
Table Creation:
SQL>CREATE TABLE GSTUDENT(snum number(5) primary key,sname varchar2(10),major
varchar2(10),
levels varchar2(2),age number(2));
SQL>CREATE TABLE FACULTY(fid number(5) primary key,fname varchar2(10),deptid
references dept(deptid));
SQL>CREATE TABLE CLASS(cname varchar2(10) primary key,meetsat varchar2(10),room
varchar2(5), fid references faculty(fid));
SQL>CREATE TABLE ENROLLED(snum references student(snum),cname references
class(cname));
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
2
Inserting Values:
INSERT INTO STUDENT VALUES(101,'adi','maths','jr','21');
SELECT * FROM STUDENT;
SNUM SNAME MAJOR LEVELS AGE
------- --------- --------- ------- -----
101 adi maths jr 21
102 arul math sr 22
103 bhavya se sr 27
104 divya or jr 20
105 kota se sr 27
INSERT INTO FACULTY VALUES(501,'Harshith','401');
SELECT * FROM FACULTY;
FID FNAME DEPTID
--------- ---------- ---------- ---
501 Harshith 401
502 sathya 402
503 kc 401
504 illangos 405
505 nirmala 403
506 asha 404
507 usha 402
INSERT INTO CLASS VALUES('3a','10.00','r128','501');
SELECT * FROM CLASS;
CNAME MEETSAT ROOM FID
--------- ---------- --- ------- --------- -----
3a 10.00 r128 501
3b 10.00 ro2 502
4a 11.00 r03 503
4b 11.00 r128 502
5a 12.00 r04 504
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
3
INSERT INTO ENROLLED VALUES(101,'3a');
SELECT * FROM ENROLLED;
SNUM CNAME
--------- ---------- ------
101 3a
101 3b
101 4a
101 4b
102 3a
102 3b
103 3a
104 4a
105 5a
101 5b
Queries:
I) Create the above tables by properly specifying the primary keys and the
foreign keys.
SQL> SELECT SNAME FROM STUDENT S,ENROLLEDE,CLASSC,FACULTY F WHERE
S.SNUM=E.SNUM AND E.CNAME=C.CNAME AND C.FID=F.FID AND
FNAME='HARSHITH' AND LEVELS='JR';
SNAME
--------- ---
ADI
II) Find the names of all classes that either meet in room R128 or have five or
more Students enrolled.
SQL> SELECT DISTINCT C.CNAME FROM CLASS C,ENROLLED E WHERE C.CNAME=E.CNAME
AND ROOM='R128' OR C.CNAME IN (SELECT CNAME FROM ENROLLED GROUP BY
CNAME HAVING COUNT(SNUM)>=5);
CNAME
--------- ---
3A
4B
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
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 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 C1.CNAME<>C2.CNAME AND C1.MEETSAT=C2.MEETSAT);
SNAME
--------- ----
ADI
ARUN
IV) Find the names of faculty members who teach in every room in which some
class is taught.
SQL>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
--------- ---
MANJU
KALA
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.CNAME=E.CNAME AND F.FID=C.FID);
FNAME
--------- --
HARSHITH
KALA
SANTOSH
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
5
PROGRAM NO:2
The following relations keep track of airline flight information:
Flights (aid: 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
eidand the maximum cruisingrangeof 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 cruisingrangeover 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 AIRCRAFT(aid number(5) primary key,aname varchar2(10),cruisrange
number(10));
Table created.
SQL> CREATE TABLE EMPLOYEES(eid number(5) primary key,ename varchar2(10),salary
number(10));
Table created.
SQL> CREATE TABLE CERTIFIED(eid references employees(eid),aid references
aircraft(aid));
Table created.
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
6
SQL>CREATE TABLE FLIGHTS(aid references aircraft(aid),source varchar2(10),destn
varchar2(10),distance number(10),departs varchar2(5),arrives varchar2(5),price
number(10,2));
Table created.
Inserting Values:
INSERT INTO AIRCRAFT VALUES('1','Boeing',’500');
SELECT * FROM AIRCRAFT;
AID ANAME CRUISRANGE
--------- ---------- ----------
1 Boeing 500
2 Combat 20000
3 Invader 500
4 Mitchell 500
5 yale 30000
INSERT INTO EMPLOYEES VALUES('100','Arun','2000');
SELECT * FROM EMPLOYEES;
EID ENAME SALARY
--------- ---------- ---------
100 Arun 20000
200 Vijay 45000
300 Bruno 30000
400 Mithun 20000
500 Kunal 55000
600 Jack 85000
700 Jane 15000
INSERT INTO CERTIFIED VALUES('100','1');
SELECT * FROM CERTIFIED;
EID AID
--------- ---------
100 1
100 2
200 3
700 2
300 5
400 5
500 1
500 4
600 1
600 4
600 5
600 2
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
7
INSERT INTO FLIGHTS ALUES('1','Bangalore','Frankfurt','3000','10.00',’15.00’,’20000’);
SELECT * FROM FLIGHTS;
AID SOURCE DESTN DISTANCE DEPAR ARRIV PRICE
---- ------------ -------- ---------- ------- -------- ------
1 Bangalore Frankfurt 3000 10.00 15.00 20000
2 Delhi New York 5000 6.00 16.00 40000
3 Bangalore Newdelhi 5000 06.35 10.30 4000
4 Bangalor Chennai 1000 12.00 13.00 1200
5 Chennai new delhi 3000 13.00 15.30 4300
2 Bangalore Frankfurt 3000 12.30 18.00 18000
5 Bangalore Newdelhi 5000 13.00 15.30 3500
Queries:
I) Find the names of aircraft such that all pilots certified to operate them have
salaries more than Rs.80, 000.
SQL> SELECT ANAME FROM AIRCRAFT A,CERTIFIEDC,EMPLOYEES E WHERE A.AID=C.AID
AND C.EID=E.EID AND SALARY>80000;
ANAME
----------
BOEING
MITCHELL
COMBAT
II) For each pilot who is certified for more than three aircrafts, find the eidand the
maximum cruisingrangeof the aircraft for which she or he is certified.
SQL> SELECT EID,CRUISRANGE FROM AIRCRAFT A,CERTIFIED C WHERE A.AID=C.AID AND
EID IN (SELECT EID FROM CERTIFIED GROUP BY EID HAVING COUNT(AID)>3) AND
CRUISRANGE IN (SELECT MAX(CRUISRANGE) FROM AIRCRAFT A,CERTIFIED C WHERE
A.AID=C.AID GROUP BY EID HAVING COUNT(C.AID)>3);
EID CRUISRANGE
---- ----------
600 30000
III) Find the names of pilots whose salary is less than the price of the cheapest
route from Bengaluru to Frankfurt
SQL>SELECT DISTINCT ENAME FROM EMPLOYEES E,CERTIFIED C,FLIGHTS F WHERE
E.EID=C.EID AND C.AID=F.AID AND SALARY<(SELECT MIN(PRICE)
FROM FLIGHTS WHERE SOURCE='BANGALORE' AND DESTN='FRANKFORT');
ENAME
----------
JANE
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
8
IV) For all aircraft with cruisingrangeover 1000 Kms, .find the name of the aircraft
and the average salary of all pilots certified for this aircraft.
SQL>SELECT ANAME,AVG(SALARY) FROM AIRCRAFT A,EMPLOYEES E,CERTIFIED C WHERE
E.EID=C.EID AND C.AID=A.AID
AND CRUISRANGE>1000 GROUP BY ANAME;
ANAME AVG(SALARY)
---------- -----------
COMBAT 40000
YALE 45000
V) Find the names of pilots certified for some Boeing aircraft.
SQL> SELECT ENAME FROM AIRCRAFT A,EMPLOYEES E,CERTIFIED C WHERE E.EID=C.EID
AND C.AID=A.AID AND ANAME='BOIENG';
ENAME
----------
ARUN
KUNAL
JACK
VI) Find the aids of all aircraft that can be used on routes from Bengaluru to New
Delhi.
SQL> SELECT AID FROM FLIGHTS WHERE SOURCE=’BANGALORE’ AND
DESTN=’NEWDELHI’;
AID
---------
3
5
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
9
PROGRAM NO:3
Consider the following database of student enrollment in courses and books
adapted 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.
SOLUTION:
(i) Creating Tables:
CREATE TABLE STUDENT(regno varchar(10) primary key,name varchar(10),
major varchar(10),bdate date);
CREATE TABLE COURSE(cno number(4) primary key, cname varchar(20)
dept varchar(20));
CREATE TABLE ENROLL( regno varchar (10) references STUDENT,
cno number(5) references COURSE,
sem number(1) , marks number(8));
CREATE TABLE TEXT(book_isbn number(10) primary key,book_title varchar(20),
publisher varchar(20),author varchar(15));
CREATE TABLE BOOK_ADOPTION(cno number(5) references COURSE,
sem number(3) , book_isbn number(10) references TEXT);
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
10
(ii) Inserting Values:
INSERT INTO STUDENT VALUES(‘1MV10MCA09’,’JOHN’,’DATASTRUCTURES’,’20-JAN-88’);
SELECT * FROM STUDENT;
REGNO NAME MAJOR BDATE
--------------- -------------------- --------------------- ---------
1MV10MCA01 JOHN DATA STRUCTURES 20-JAN-88
1MV10MCA02 JOYCE NETWORKS 31-DEC-87
1MV10MCA03 ALEN OS 29-MAY-87
IMV10MCA04 SMITH SYSTEM SOFTWARE 25-NOV-87
IMV10MCA05 JAMES OS 2-DEC-86
INSERT INTO COURSE VALUES(1, 'MCA', 'CS');
SELECT * FROM COURSE;
CNO CNAME DEPT
-------- --------- ----------
1 MCA CS
2 MCA CS
3 MBA BUSINESS
4 CIVIL BE
5 MECH BE
INSERT INTO ENROLL VALUES(1MV10MCA01, 1,3, 90);
SELECT * FROM ENROLL;
REGNO CNO SEM MARKS
----------------- --------- --------- ------------
1MV10MCA01 1 3 90
1MV10MCA02 2 4 90
1MV10MCA03 3 3 90
IMV10MCA04 4 2 97
IMV10MCA05 5 2 93
INSERT INTO TEXT VALUES(111675,’DATA STRUCTURES’,’PEARSON’,’CHRIS);
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
11
SELECT * FROM TEXT;
BOOK_ISBN BOOK_TITLE PUBLISHER AUTHOR
--------------- ----------------------- ---------------- ------------------
111675 DATA STRUCTURES PEARSON CHRIS
111654 NETWORKS MC GRAW TANENBAUM
111425 OPERATING SYS PEARSON SOMER
111980 DATABASE PEARSON ELMASRI
111990 DS-2 SCHAND PADMA REDDY
INSERT INTO BOOK-ADOPTION VALUES(1,3,111675);
SELECT * FROM BOOK_ADOPTION;
CNO SEM BOOK_ISBN
-------- -------- ------------
1 3 111675
2 4 111654
3 3 111425
1 4 111980
4 2 111990
III) Demonstrate how you add a new text book to the database and make this
book to be adopted by some department
INSERT INTO TEXT VALUES (&book-isbn, '&book-title', '&publisher', '&author');
Enter value for book-isbn: 111449
Enter value for book-title: SYSTEM SOFTWARE
Enter value for publisher: MC GRAW
Enter value for author: LELAND
INSERT INTO BOOK_ADOPTION VALUES(&cno, &sem, &book-isbn);
Enter value for courseno: 5
Enter value for sem: 2
Enter value for book-isbn: 111449
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
12
SELECT * FROM TEXT;
BOOK_ISBN BOOK_TITLE PUBLISHER AUTHOR
--------------- ----------------------- ---------------- ------------------
111675 DATA STRUCTURES PEARSON CHRIS
111654 NETWORKS MC GRAW TANENBAUM
111425 OPERATING SYS PEARSON SOMER
111980 DATABASE PEARSON ELMASRI
111990 DS-2 SCHAND PADMA REDDY
111449 SYSTEM SOFTWARE MC GRAW LELAND
SELECT * FROM BOOK-ADOPTION;
CNO SEM BOOK_ISBN
------ ------ --------------
1 3 111675
2 4 111654
3 3 111425
1 4 111980
4 2 111990
5 2 111449
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.
SELECT COURSE.CNO,TEXT.BOOK_ISBN,BOOK_TITLE FROM COURSE, TEXT,
BOOK_ADOPTION WHERE COURSE.CNO=BOOK_ADOPTION.CNO AND
TEXT.BOOK_ISBN=BOOK_ADOPTION.BOOK_ISBN AND COURSE.DEPT=’CS’
GROUP BY COURSE.CNO,TEXT.BOOK_ISBN,BOOK_TITLE HAVING
COUNT(DEPT)>=1;
CNO BOOK_ISBN BOOK_TITLE
----- -------------- ----------------------
1 111675 DATA STRUCTURES
1 111980 DATABASE
2 111654 NETWORKS
V) List any department that has all its adopted books published by a specific
publisher.
SELECT DEPT FROM COURSE C,BOOK_ADOPTION B,TEXT T WHERE C.CNO=B.CNO
AND T.BOOK_ISBN=B.BOOK_ISBN AND T.PUBLISHER='SUMUKHA' GROUP BY DEPT;
DEPT PUBLISHER
-------------- ----------------
CS PEARSON
BUSINESS PEARSON
CS PEARSON
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
13
(vi) Generate suitable reports
SQL> SET LINESIZE 70
SQL> TTITLE 'REPORT GENERATION|STUDENT DETAILS';
SQL> BTITLE 'END OF REPORT';
SQL> COLUMN REGNO HEADING 'REGISTER|NUMBER';
SQL> COLUMN NAME HEADING 'STUDENT|NAME';
SQL> COLUMN MAJOR HEADING 'MAJOR';
SQL> COLUMN BDATE HEADING 'BIRTH DATE';
SQL> SPOOL REPORT3;
SQL> SELECT * FROM STUDENT;
Thu Nov 5 page 1
REPORT GENERATION
STUDENT DETAILS
REGISTER STUDENT
NUMBER NAME MAJOR BIRTH DATE
----------------- ------------- ----------------------- ---------------
1MV10MCA01 JOHN DATA STRUCTURES 20-JAN-88
1MV10MCA02 JOYCE NETWORKS 31-DEC-87
1MV10MCA03 ALEN OS 29-MAY-87
1MV10MCA04 SMITH SYSTEM SOFTWARE 25-NOV-86
1MV10MCA05 JAMES OS 02-DEC-86
END OF REPORT
SQL> SPOOL OFF;
SQL> CLEAR COLUMN;
columns cleared
SQL> TTITLE OFF;
SQL> BTITLE OFF;
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
14
PROGRAM NO: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
the foreign keys.
(ii) Enter at least five tuples for each relation.
(iii) Give the details of authors who have two 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 publisherby10%.
(vi) Generate suitable reports.
(vii) Create suitable front end for querying and displaying the results.
SOLUTION:
(i) Creating Tables:
CREATE TABLE AUTHOR(author_id number(5) primary key,
name varchar(20),city varchar(20),country varchar(20));
CREATE TABLE PUBLISHER(pub_id number(5) primary key,
name varchar(20),city varchar(20),country varchar(20));
CREATE TABLE CATEGORY(category_id number(5) primary key,
description varchar(20));
CREATE TABLE CATALOG(book_id number(5) primary key,
author_id number(5) references AUTHOR,
pub_id number(5) references PUBLISHER,
category_id number(5) references CATEGORY,
year number(4),
price number(10));
CREATE TABLE ORDER_DET(order_no number(5) primary key,
Book_id number(5) references CATALOG,quantity number(5));
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
15
(ii) Inserting Values:
INSERT INTO AUTHOR VALUES(1, ' BECK ', 'LONDON’,’UK’);
SELECT * FROM AUTHOR ;
AUTHOR_ID NAME CITY COUNTRY
------------- -------- ------------ -----------
1 BECK LONDON UK
2 JAVADEKAR BANGALORE INDIA
3 K V RAO BANGALORE INDIA
4 SOMERVILLE NEW YORK USA
5 TANENBAUM WASHINGTON USA
INSERT INTO PUBLISHER VALUES(11,’PHI’,’LONDON’,’UK’);
SELECT * FROM PUBLISHER ;
PUB_ID NAME CITY COUNTRY
---------- ----------- ------- -------------
11 PHI LONDON UK
12 MGHILL NEW DELHI INDIA
13 TMH BOMBAY INDIA
14 PEARSON WASINGTON USA
15 MBD NEW DELHI INDIA
INSERT INTO CATEGORY VALUES(‘1001’,’COMPUTER SCIENCE’);
SELECT * FROM CATEGORY ;
CATEGORY_ID DESCRIPTION
---------------- --------------------
1001 COMPUTER SCIENCE
1002 ALGORITHM DESIGN
1003 MECHANICS
1004 COMPUTER GRAPHICS
1005 DATA STRUCTURE
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
16
INSERT INTO CATALOG VALUES(21,1,11,1001,2002,300);
SELECT * FROM CATALOG ;
BOOK_ID AUTHOR_ID PUB_ID CATEGORY_ID YEAR PRICE
----------- -------------- --------- ---------------- ------- --------
21 1 11 1001 2002 300
22 1 11 1002 2003 250
23 2 11 1003 2000 400
24 3 12 1004 1999 500
25 4 14 1004 2004 540
26 5 15 1005 2001 399
27 4 11 1001 2004 300
28 1 11 1004 1998 850
INSERT INTO ORDER_DET VALUES(51,21,100);
SELECT * FROM ORDER-DET ;
ORDER_NO BOOK_ID QUANTITY
------------- ----------- -------------
51 21 100
52 21 150
53 21 200
54 22 39
55 23 35
56 23 49
57 24 35
58 25 100
III) Give the details of authors who have two 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.
SELECT * FROM AUTHOR WHERE AUTHOR_ID IN ( SELECT AUTHOR_ID FROM
CATALOG GROUP BY AUTHOR_ID HAVING COUNT(*)>2 AND AUTHOR_ID IN (
SELECT AUTHOR_ID FROM CATALOG
WHERE PRICE> (SELECT AVG(PRICE) FROM CATALOG) AND YEAR>2000));
AUTHOR_ID NAME CITY COUNTRY
------------- ------------ ---------- --------------
1 BECK LONDON UK
4 SOMERVILLE NEW YORK USA
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
17
IV) Find the author of the book which has maximum sales.
SELECT NAME FROM AUTHOR WHERE AUTHOR_ID IN ( SELECT AUTHOR_ID FROM
CATALOG WHERE BOOK_ID =
(SELECT BOOK_ID FROM ORDER_DETAILS WHERE QUANTITY =
(SELECT MAX (QUANTITY) FROM ORDER_DETAILS)));
NAME
-------
BECK
V) Demonstrate how you increase the price of books published by a specific
publisher by 10%.
UPDATE CATALOG SET PRICE=PRICE+PRICE*0.1 WHERE PUB_ID IN ( SELECT
PUB_ID FROM PUBLISHER WHERE NAME='PHI');
BOOK_ID AUTHOR_ID PUB_ID CATEGORY_ID YEAR PRICE
----------- ------------- --------- ---------------- -------- ---------
21 1 11 1001 2002 330
22 1 11 1002 2003 275
23 2 11 1003 2000 440
27 4 11 1001 2004 330
28 1 11 1004 1998 935
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
18
(vi) Generate suitable reports.
SQL> SET LINESIZE 70
SQL> SET PAGESIZE 20
SQL> TTITLE 'REPORT GENERATION|AUTHOR DETAILS'
SQL> BTITLE CENTER 'END REPORT'
SQL> COLUMN AUTHOR_ID HEADING ‘AUTHOR|IDENTITY’;
SQL> COLUMN NAME HEADING ‘AUTHOR|NAME’;
SQL> COLUMN CITY HEADING ‘CITY’;
SQL> COLUMN COUNTRY HEADING ‘COUNTRY’;
SQL> SPOOL REPORT4;
SQL> SELECT * FROM AUTHOR;
Thu Nov 5 page 1
REPORT GENERATION
AUTHOR DETAILS
AUTHOR AUTHOR
IDENTITY NAME CITY COUNTRY
------------ ---------- ---------- ------------
1 BECK LONDON UK
2 JAVADEKAR BANGALORE INDIA
3 K V RAO BANGALORE INDIA
4 SOMERVILLE NEW YORK USA
5 TANENBAUM WASHINGTON USA
END REPORT
SQL> SPOOL OFF;
SQL> CLEAR COLUMNS;
Columns cleared
SQL> TTITLE OFF;
SQL> BTITLE OFF;
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
19
PROGRAM NO: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-steet: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 an
the foreign keys.
(ii) Enter at least five tuples for each relation.
(iii) Find all the customer who have at least two accounts at the main
branch.
(iv) Find all the customers who have an account tuples at every branch
located in a specific city.
(v) Demonstrate how you delete all account tuples at every branch
located in a specified city.
(vi) Generate suitable reports.
(vi) Create suitable front end for querying and displaying the results.
SOLUTION:
(i) Creating Tables:
CREATE TABLE BRANCH(branch_name varchar(30) primary key,
branch_city varchar(30),asset number(20));
CREATE TABLE ACCOUNT(acc_no number(10) primary key,
branch_name varchar(30) references BRANCH,balance number(10));
CREATE TABLE CUSTOMER(cust_name varchar(30) primary key,
cust_street varchar(30),cust_city varchar(30));
CREATE TABLE DEPOSITOR(cust_name varchar(30) references CUSTOMER,
acc_no number(10) references ACCOUNT);
CREATE TABLE LOAN(loan_number number(5) primary key,
branch_name varchar(30) references BRANCH,amount number(10));
CREATE TABLE BORROWER(cust_name varchar(30) references CUSTOMER,
loan_number number (5) references LOAN unique);
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
20
(ii) Inserting values:
INSERT INTO BRANCH VALUES(' SBM BULL TEMPLE’,’BANGALORE’,500000);
SELECT * FROM BRANCH ;
BRANCH_NAME BRANCH_CITY ASSET
----------------- --------------------- ----------
SBM BULL TEMPLE BANGALORE 500000
SBM VIJAY NAGAR BANGALORE 200000
SBM JAY NAGAR BANGALORE 150000
SBM VIDYPEETHA BANGALORE 350000
SBI MAIN MYSORE 450000
INSERT INTO ACCOUNT VALUES(‘133550,’SBM BULL TEMPLE’,’45000);
SELECT * FROM ACCOUNT;
ACC_NO BRANCH_NAME BALANCE
--------- ----------------------- ----------
133550 SBM BULL TEMPLE 45000
133551 SBM VIJAY NAGAR 10000
25551 SBM BULL TEMPLE 10000
2551 SBI MAIN 45000
2552 SBI MAIN 5000
2245 SBM VIDYPEETHA 5600
2246 SBM JAY NAGAR 20000
2257 SBM JAY NAGAR 8000
22351 SBM VIDYPEETHA 3000
24466 SBM JAY NAGAR 5000
23451 SBI MAIN 4500
23455 SBM VIJAY NAGAR 5400
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
21
INSERT INTO CUSTOMER VALUES('PUSHPA', 'BULL TEMPLE ROAD', 'BANGALORE');
SELECT * FROM CUSTOMER;
CUST_NAME CUST_STREET CUST_CITY
-------------- ------------------------ --------------
PUSHPA BULL TEMPLE ROAD BANGALORE
RAMYA M G ROAD BANGALORE
SUDHA N R COLONY BANGALORE
SHYAMALA HANUMANTH NAGAR BANGALORE
ROOPA JALLAHALLI BANGALORE
UMA MAIN ROAD MYSORE
SANDYA COMMERCIAL STREET BANGALORE
INSERT INTO DEPOSITOR VALUES('PUAHPA',2551);
SELECT * FROM DEPOSITOR;
CUST_NAME ACC_NO
--------------- ----------
PUSHPA 2551
PUSHPA 2552
PUSHPA 23451
SANDYA 133550
SANDYA 2552
ROOPA 133551
SUDHA 25551
SUDHA 2245
RAMYA 2246
SHYAMALA 2257
SHYAMALA 22351
UMA 24466
UMA 23455
INSERT INTO LOAN VALUES(101,’ SBM BULL TEMPLE’, 30000’);
SELECT * FROM LOAN ;
LOAN_NUMBER BRANCH_NAME AMOUNT
---------------- --------------------- ----------
101 SBM BULL TEMPLE 30000
102 SBM VIJAY NAGAR 10000
103 SBI MAIN 50000
104 SBM VIDYPEETHA 20000
105 SBM JAY NAGAR 45000
INSERT INTO BORROWER VALUES(‘PUSHPA’,101);
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
22
SELECT * FROM BORROWER ;
CUST_NAME LOAN_NUMBER
-------------- -----------------
PUSHPA 101
SUDHA 102
ROOPA 103
SHYAMALA 104
UMA 105
III) Find all the customer who have at least two accounts at the main branch.
SELECT CUST_NAME FROM DEPOSITOR WHERE ACC_NO IN (SELECT ACC_NO
FROM ACCOUNT WHERE BRANCH_NAME = 'SBI MAIN') GROUP BY CUST_NAME
HAVING COUNT(*)>1;
CUST_NAME
------------------
PUSHPA
IV) Find all the customers who have an account tuples at every branch located ina
specific city.
SELECT DISTINCT CUST_NAME FROM DEPOSITOR WHERE ACC_NO IN (SELECT
ACC_NO FROM ACCOUNT WHERE BRANCH_NAME IN (SELECT BRANCH_NAME FROM
BRANCH WHERE BRANCH_CITY='MYSORE'));
CUST_NAME
--------------
SANDYA
PUSHPA
V) Demonstrate how you delete all account tuples at every branch located in a
specified city.
DELETE FROM ACCOUNT WHERE BRANCH_NAME IN (SELECT BRANCH_NAME
FROMBRANCH WHERE BRANCH_CITY='MYSORE');
SELECT * FROM ACCOUNT;
ACC_NO BRANCH_NAME BALANCE
--------- ----------------------- ----------
133550 SBM BULL TEMPLE 45000
133551 SBM VIJAY NAGAR 10000
25551 SBM BULL TEMPLE 10000
2245 SBM VIDYPEETHA 5600
2246 SBM JAY NAGAR 20000
2257 SBM JAY NAGAR 8000
22351 SBM VIDYPEETHA 3000
24466 SBM JAY NAGAR 5000
23455 SBM VIJAY NAGAR 5400
(vi) Generate suitable reports.
Database Management System Laboratory (10MCA38)
Ganesh Hegde [email protected] facebook.com/hegde.ganesh7
23
SQL> SET LINESIZE 70;
SQL> TTITLE ‘REPORT GENERATION|ACCOUNT DETAILS’;
SQL> BTITLE ‘END OF REPORT’;
SQL> COLUMN ACC_NO HEADING ‘ACCOUNT|NUMBER’;
SQL> COLUMN BRANCH_NAME HEADING ‘BRANCH|NAME’;
SQL> SPOOL REPORT5;
SQL> SELECT * FROM ACCOUNT;
Thu Nov 1 page 1
REPORT GENERATION
ACCOUNT DETAILS
ACCOUNT BRANCH
NUMBER NAME BALANCE
--------- ----------------------- ----------
133550 SBM BULL TEMPLE 45000
133551 SBM VIJAY NAGAR 10000
25551 SBM BULL TEMPLE 10000
2245 SBM VIDYPEETHA 5600
2246 SBM JAY NAGAR 20000
2257 SBM JAY NAGAR 8000
22351 SBM VIDYPEETHA 3000
24466 SBM JAY NAGAR 5000
23455 SBM VIJAY NAGAR 5400
END OF REPORT
SQL> SPOOL OFF;
SQL> CLEAR COLUMN;
Columns cleared
SQL> TTITLE OFF;
SQL> BTITLE OFF;