99
10MCA38 DBMS LABORATORY Sl. no. Title of the program Pag e no. 1 Consider the following relations: 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) 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. 4 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. 13 Department of MCA Page 1 JCE, Belgaum

Dbms Lab Manual

Embed Size (px)

Citation preview

Page 1: Dbms Lab Manual

10MCA38

Sl.no. Title of the program Page no.

1 Consider the following relations:

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)

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.

4

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.

13

Department of MCA Page 1 JCE, Belgaum

Page 2: Dbms Lab Manual

10MCA38

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

25

Department of MCA Page 2 JCE, Belgaum

Page 3: Dbms Lab Manual

10MCA38

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

36

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.

49

Department of MCA Page 3 JCE, Belgaum

Page 4: Dbms Lab Manual

10MCA38

ASSIGNMENT N0. – 1

COLLEGE DATABASE

Consider the following relations:

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.

Department of MCA Page 4 JCE, Belgaum

Page 5: Dbms Lab Manual

10MCA38

SQL> CREATE TABLE STUDENTT(SNUM NUMBER(5) PRIMARY KEY, 2 SNAME VARCHAR(10), 3 MAJOR VARCHAR(10), 4 LEVELS VARCHAR(2), 5 AGE NUMBER(2));

Table created.

SQL> DESC STUDENTT Name Null? Type ----------------------------------------- ------------------- ----------------------------

SNUM NOT NULL NUMBER(5) SNAME VARCHAR2(10) MAJOR VARCHAR2(10) LEVELS VARCHAR2(2) AGE NUMBER(2)

SQL> INSERT INTO STUDENTT VALUES(&SNUM,'&SNAME','&MAJOR','&LEVELS',&AGE);Enter value for snum: 901Enter value for sname: AJITEnter value for major: MATHSEnter value for levels: SREnter value for age: 25old 1: INSERT INTO STUDENTT VALUES(&SNUM,'&SNAME','&MAJOR','&LEVELS',&AGE)new 1: INSERT INTO STUDENTT VALUES(901,'AJIT','MATHS','SR',25)

1 row created.

SQL> SELECT * FROM STUDENTT;

SNUM SNAME MAJOR LE AGE--------------- --------------- ------------------- -- ---------- 901 AJIT MATHS SR 25 902 AMIR COMP. SC JR 21 903 BINDU OR SR 27 904 DIYA PHYSICS JR 22 905 ESHA CHEMISTRY SR 24

Department of MCA Page 5 JCE, Belgaum

Page 6: Dbms Lab Manual

10MCA38

SQL> CREATE TABLE FACULTY(FID NUMBER(5) PRIMARY KEY, 2 FNAME VARCHAR(10), 3 DEPTID NUMBER(5));

Table created.

SQL> DESC FACULTY; Name Null? Type ----------------------------------------- ----------------- ---------------------------- FID NOT NULL NUMBER(5) FNAME VARCHAR2(10) DEPTID NUMBER(5)

SQL> INSERT INTO FACULTY VALUES(&FID,'&FNAME',&DEPTID);Enter value for fid: 11Enter value for fname: RAHULEnter value for deptid: 4441old 1: INSERT INTO FACULTY VALUES(&FID,'&FNAME',&DEPTID)new 1: INSERT INTO FACULTY VALUES(11,'RAHUL',4441)

1 row created.

SQL> SELECT * FROM FACULTY;

FID FNAME DEPTID--------- ---------------- -------------- 11 RAHUL 4441 22 RANJIT 4442 33 HARSHITH 4443 44 SACHIN 4444 55 SACHA 4445 66 SRIDEVI 4446 77 TINA 4447 88 TANMAY 4448

8 rows selected.

Department of MCA Page 6 JCE, Belgaum

Page 7: Dbms Lab Manual

10MCA38

SQL> CREATE TABLE CLASS(CNAME VARCHAR(10) PRIMARY KEY, 2 MEETS_AT VARCHAR(5), 3 ROOM VARCHAR(5), 4 FID NUMBER(5), 5 FOREIGN KEY(FID) REFERENCES FACULTY(FID));

Table created.

SQL> DESC CLASS Name Null? Type ----------------------------------------- ----------------- ---------------------------- CNAME NOT NULL VARCHAR2(10) MEETS_AT VARCHAR2(5) ROOM VARCHAR2(5) FID NUMBER(5)

SQL> INSERT INTO CLASS VALUES('&CNAME','&MEETS_AT','&ROOM',&FID);Enter value for cname: ELECTRICALEnter value for meets_at: 9.00Enter value for room: A101Enter value for fid: 11old 1: INSERT INTO CLASS VALUES('&CNAME','&MEETS_AT','&ROOM',&FID)new 1: INSERT INTO CLASS VALUES('ELECTRICAL','9.00','A101',11)

1 row created.

SQL> SELECT * FROM CLASS;

CNAME MEETS ROOM FID-------------------- ----------- ---------- ----------ELECTRICAL 9.00 R128 11E & C 10.00 A202 22CS01 11.00 204 11CS02 12.00 B101 33MECH 10.00 C101 55MCA 12.00 B312 66MBA 10.00 R128 33MCA02 11.00 B313 88

8 rows selected.

Department of MCA Page 7 JCE, Belgaum

Page 8: Dbms Lab Manual

10MCA38

SQL> CREATE TABLE ENROLLED(SNUM NUMBER(5) NOT NULL, 2 CNAME VARCHAR(10) NOT NULL, 3 FOREIGN KEY(SNUM) REFERENCES STUDENTT(SNUM), 4 FOREIGN KEY(CNAME) REFERENCES CLASS(CNAME));

Table created.

SQL> DESC ENROLLED; Name Null? Type ----------------------------------------- ----------------- ---------------------------- SNUM NOT NULL NUMBER(5) CNAME NOT NULL VARCHAR2(10)

SQL> INSERT INTO ENROLLED VALUES(&SNUM,'&CNAME');Enter value for snum: 901Enter value for cname: ELECTRICALold 1: INSERT INTO ENROLLED VALUES(&SNUM,'&CNAME')new 1: INSERT INTO ENROLLED VALUES(901,'ELECTRICAL')

1 row created.

SQL> SELECT * FROM ENROLLED;

SNUM CNAME------------- ------------------- 901 ELECTRICAL 902 CS01 903 CS02 901 MCA 901 MCA02 901 MBA 901 E & C 903 MCA 903 MBA 901 MECH 905 CS01

11 rows selected.

Department of MCA Page 8 JCE, Belgaum

Page 9: Dbms Lab Manual

10MCA38

QUERIES

Query I : Find the names of all Juniors who are enrolled in a class start by prof .Harshit.

SQL> SELECT DISTINCT S.SNAME 2 FROM STUDENTT S, ENROLLED E, CLASS C, FACULTY F 3 WHERE S.SNUM = E.SNUM AND 4 C.FID =F.FID AND 5 FNAME = 'HARSHITH' AND 6 LEVELS = 'JR';

SNAME----------AMIR

Department of MCA Page 9 JCE, Belgaum

Page 10: Dbms Lab Manual

10MCA38

Query II: Find the names of all classes that either meet in room R128 or have five or moreStudents enrolled

SQL> SELECT * FROM CLASS;CNAME MEETS ROOM FID-------------------- ----------- ---------- ----------ELECTRICAL 9.00 R128 11E & C 10.00 A202 22CS01 11.00 A204 11CS02 12.00 B101 33MECH 10.00 C101 55MCA 12.00 B312 66MBA 10.00 R128 33MCA02 11.00 B313 88

8 rows selected.

SQL> SELECT * FROM ENROLLED; SNUM CNAME------------- ------------------- 901 ELECTRICAL 902 CS01 903 CS02 901 MCA 901 MCA02 901 MBA 901 E & C 903 MCA 903 MBA 901 MECH 905 CS01

11 rows selected.

SQL> SELECT DISTINCT C.CNAME 2 FROM CLASS C, ENROLLED E 3 WHERE C.CNAME = E.CNAME AND 4 C.ROOM = 'R128' OR C.CNAME IN(SELECT CNAME 5 FROM ENROLLED 6 GROUP BY CNAME 7 HAVING COUNT(SNUM)>=5);

CNAME----------ELECTRICALMBA

Department of MCA Page 10 JCE, Belgaum

Page 11: Dbms Lab Manual

10MCA38

Query III : Find the names of all students who are enrolled in two classes that meet at the sametime.

SQL> SELECT DISTINCT S.SNAME 2 FROM STUDENTT S 3 WHERE S.SNUM IN(SELECT E1.SNUM 4 FROM ENROLLED E1, ENROLLED E2, CLASS C1, CLASS C2 5 WHERE E1.CNAME = C1.CNAME AND 6 E2.CNAME = C2.CNAME AND 7 E1.SNUM = E2.SNUM AND 8 E1.CNAME<>C2.CNAME AND 9 C1.MEETS_AT = C2.MEETS_AT);

SNAME----------BINDUAJIT

Query IV : Find the names of faculty members who teach in every room in which some class istaught

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

no rows selected.

Department of MCA Page 11 JCE, Belgaum

Page 12: Dbms Lab Manual

10MCA38

Query V : Find the names of faculty members for whom the combined enrollment of the coursesthat 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--------------RAHULTANMAYSRIDEVISACHINHARSHITHSACHATINARANJIT

8 rows selected.

Department of MCA Page 12 JCE, Belgaum

Page 13: Dbms Lab Manual

10MCA38

ASSIGNMENT N0. – 2

AIRCRAFT DATABASE

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.

Department of MCA Page 13 JCE, Belgaum

Page 14: Dbms Lab Manual

10MCA38

SQL> create table flight(no number(5) primary key, 2 frm varchar(20), 3 toe varchar(20), 4 dist number(4), 5 departs date, 6 arrives date, 7 price real);

Table created.

SQL> desc flight

Name Null? Type -------------------------- --------------- ------ -- ----------------------- NO NOT NULL NUMBER(5) FRM VARCHAR2(20) TOE VARCHAR2(20) DIST NUMBER(4) DEPARTS DATE ARRIVES DATE PRICE FLOAT(63)

SQL> insert into flight values(&no,'&frm','&toe','&dist','&departs','&arrives','&price');

Enter value for no: 101Enter value for frm: belgaumEnter value for toe: bangloreEnter value for dist: 550Enter value for departs: 10-oct-12Enter value for arrives: 10-oct-12Enter value for price: 4500old 2: &no,'&frm','&toe','&dist','&departs','&arrives','&price')new 2: 101,'belgaum','banglore','550','10-oct-12','10-oct-12','4500')

1 row created.

Department of MCA Page 14 JCE, Belgaum

Page 15: Dbms Lab Manual

10MCA38

SQL> select * from flight;

NO FRM TOE DIST DEPARTS ARRIVES PRICE---------- -------------- --- ----------------- -- ---------- --------- ------ --- ------------- ------------- 101 belgaum banglore 550 10-OCT-12 10-OCT-12 4500 102 banglore Frankfurt 550 11-OCT-12 11-OCT-12 2000 103 mumbai new york 1255 05-OCT-11 06-OCT-11 32010 104 banglore new delhi 1337 14-NOV-12 15-NOV-12 35000 105 banglore london 1037 12-DEC-12 13-DEC-12 34000

5 rows selected.

SQL> create table aircraft(aid number(5) primary key, 2 aname varchar2(15), 3 crange number(5));

Table created.

SQL> desc aircraft Name Null? Type ----------------------- ------------------ - ------- --------------------------

AID NOT NULL NUMBER(5) ANAME VARCHAR2(15) CRANGE NUMBER(5)

SQL> insert into aircraft values(&aid,'&aname','&crange');Enter value for aid: 201Enter value for aname: airbus375Enter value for crange: 3000old 1: insert into aircraft values(&aid,'&aname','&crange')new 1: insert into aircraft values(201,'airbus375','3000')

1 row created.

Department of MCA Page 15 JCE, Belgaum

Page 16: Dbms Lab Manual

10MCA38

SQL> select * from aircraft; AID ANAME CRANGE---------- --------------- ------------- 201 airbus375 3000 202 boeing 2000 203 emirates 3500 204 qatar 1000 205 Air India 2000

5 rows selected.

SQL> create table employees2(eid number(5) primary key, 2 ename varchar2(15), 3 salary number(6));

Table created.

SQL> desc employees2 Name Null? Type --------------------------- -------------- --- ----- ------------------------- EID NOT NULL NUMBER(5) ENAME VARCHAR2(15) SALARY NUMBER(6)

SQL> insert into employees2 values(&eid,'&ename','&salary');Enter value for eid: 301Enter value for ename: abhijitEnter value for salary: 65000old 1: insert into employees2 values(&eid,'&ename','&salary')new 1: insert into employees2 values(301,'abhijit','65000')

1 row created.

SQL> select * from employees2;

EID ENAME SALARY---------- --------------- ---------- 301 abhijit 85000 302 alicia 63000 303 mahesh 45961 304 smith 78961 305 jeni 1000

Department of MCA Page 16 JCE, Belgaum

Page 17: Dbms Lab Manual

10MCA38

Department of MCA Page 17 JCE, Belgaum

Page 18: Dbms Lab Manual

10MCA38

SQL> create table certified(eid number(5), 2 aid number(5), 3 foreign key(eid) references employees2(eid), 4 foreign key(aid) references aircraft(aid));

Table created.

SQL> desc certified Name Null? Type ----------------------------------------- -------- --------------------------

EID NUMBER(5) AID NUMBER(5)

SQL> insert into certified values(&eid,&aid);Enter value for eid: 301Enter value for aid: 201old 1: insert into certified values(&eid,&aid)new 1: insert into certified values(301,201)

1 row created.

SQL> select * from certified;

EID AID---------- ---------- 301 201 301 202 301 203 301 204 302 205 301 204 304 205

7 rows selected.

Department of MCA Page 18 JCE, Belgaum

Page 19: Dbms Lab Manual

10MCA38

QUERIES

Query i: Find the names of aircraft such that all pilots certified to operate them have salariesmore than Rs.80, 000.

SQL> select * from aircraft; AID ANAME CRANGE---------- --------------- ------------- 201 airbus375 3000 202 boeing 2000 203 emirates 3500 204 qatar 1000 205 Air India 2000

SQL> select * from certified;

EID AID---------- ---------- 301 201 301 202 301 203 301 204 302 205 301 204 304 205

SQL> select * from employees2;

EID ENAME SALARY---------- --------------- ---------- 301 abhijit 85000 302 alicia 63000 303 mahesh 45961 304 smith 78961 305 jeni 1000

SQL> select distinct a.aname 2 from aircraft a 3 where a.aid in(select c.aid 4 from certified c,employees2 e 5 where c.eid=e.eid and 6 e.salary>80000);

ANAME---------------emiratesboeingairbus375

Department of MCA Page 19 JCE, Belgaum

Page 20: Dbms Lab Manual

10MCA38

Query ii: For each pilot who is certified for more than three aircrafts, find the eid and themaximum cruisingrange of the aircraft for which she or he is certified.

SQL> select * from aircraft; AID ANAME CRANGE---------- --------------- ------------- 201 airbus375 3000 202 boeing 2000 203 emirates 3500 204 qatar 1000 205 Air India 2000

SQL> select * from certified;

EID AID---------- ---------- 301 201 301 202 301 203 301 204 302 205 301 204 304 205

SQL> select c.eid, max(a.crange) 2 from certified c,aircraft a 3 where c.aid =a.aid 4 group by c.eid 5 having count(c.aid)>3;

EID MAX(A.CRANGE)---------- ----------------------- 301 3500

Department of MCA Page 20 JCE, Belgaum

Page 21: Dbms Lab Manual

10MCA38

Query iii: Find the names of pilots whose salary is less than the price of the cheapest route fromBengaluru to Frankfurt.

SQL> select * from employees2;

EID ENAME SALARY---------- --------------- ---------- 301 abhijit 85000 302 alicia 63000 303 mahesh 45961 304 smith 78961 305 jeni 1000

SQL> select * from flight;

NO FRM TOE DIST DEPARTS ARRIVES PRICE---------- -------------- --- ----------------- -- ---------- --------- ------ --- ------------- ------------- 101 belgaum banglore 550 10-OCT-12 10-OCT-12 4500 102 banglore Frankfurt 550 11-OCT-12 11-OCT-12 2000 103 mumbai new york 1255 05-OCT-11 06-OCT-11 32010 104 banglore new delhi 1337 14-NOV-12 15-NOV-12 35000 105 banglore london 1037 12-DEC-12 13-DEC-12 34000

SQL> select e.ename 2 from employees2 e 3 where e.salary < (select min(price) 4 from flight 5 where frm = 'banglore' and 6 toe = 'Frankfurt');

ENAME---------------jeni

Department of MCA Page 21 JCE, Belgaum

Page 22: Dbms Lab Manual

10MCA38

Query 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 * from employees2; EID ENAME SALARY---------- --------------- ---------- 301 abhijit 85000 302 alicia 63000 303 mahesh 45961 304 smith 78961 305 jeni 1000

SQL> select * from certified; EID AID---------- ---------- 301 201 301 202 301 203 301 204 302 205 301 204 304 205

SQL> select * from aircraft;

AID ANAME CRANGE---------- --------------- --------------- 201 airbus375 3000 202 boeing 2000 203 emirates 3500 204 qatar 1000 205 Air India 2000

SQL> select a.aid,a.aname as name,avg(e.salary) as avgsal 2 from aircraft a, certified c,employees2 e 3 where a.aid=c.aid and 4 c.eid=e.eid and 5 crange>1000 6 group by a.aid,a.aname;

AID NAME AVGSAL---------- --------------- ------------- 203 emirates 85000 202 boeing 85000 201 airbus375 85000 205 Air India 70980.5

Department of MCA Page 22 JCE, Belgaum

Page 23: Dbms Lab Manual

10MCA38

Department of MCA Page 23 JCE, Belgaum

Page 24: Dbms Lab Manual

10MCA38

Query vi: Find the aid s of all aircraft that can be used on routes from Bengaluru to New Delhi.

SQL> select * from aircraft;

AID ANAME CRANGE---------- --------------- --------------- 201 airbus375 3000 202 boeing 2000 203 emirates 3500 204 qatar 1000 205 Air India 2000

SQL> select * from flight;

NO FRM TOE DIST DEPARTS ARRIVES PRICE---------- -------------- --- ----------------- -- ---------- --------- ------ --- ------------- ------------- 101 belgaum banglore 550 10-OCT-12 10-OCT-12 4500 102 banglore Frankfurt 550 11-OCT-12 11-OCT-12 2000 103 mumbai new york 1255 05-OCT-11 06-OCT-11 32010 104 banglore new delhi 1337 14-NOV-12 15-NOV-12 35000 105 banglore london 1037 12-DEC-12 13-DEC-12 34000

SQL> select a.aid 2 from aircraft a 3 where a.crange>(select min(dist) 4 from flight 5 where frm = 'banglore' and toe = 'delhi');

AID---------- 201 202 203 204 205

Department of MCA Page 24 JCE, Belgaum

Page 25: Dbms Lab Manual

10MCA38

Query v: Find the names of pilots certified for some Boeing aircraft.

SQL> select * from employees2;

EID ENAME SALARY---------- --------------- ---------- 301 abhijit 85000 302 alicia 63000 303 mahesh 45961 304 smith 78961 305 jeni 1000

SQL> select * from certified;

EID AID---------- ---------- 301 201 301 202 301 203 301 204 302 205 301 204 304 205

SQL> select * from aircraft;

AID ANAME CRANGE---------- --------------- --------------- 201 airbus375 3000 202 boeing 2000 203 emirates 3500 204 qatar 1000 205 Air India 2000

SQL> SELECT DISTINCT E.ENAME 2 FROM EMPLOYEES2 E, CERTIFIED C, AIRCRAFT A 3 WHERE A.AID = C.AID AND 4 C.EID = E.EID AND A.ANAME LIKE '%boeing%';

ENAME---------------abhijit

Department of MCA Page 25 JCE, Belgaum

Page 26: Dbms Lab Manual

10MCA38

Query vi: Find the aid s of all aircraft that can be used on routes from Bengaluru to New Delhi.

SQL> select * from aircraft;

AID ANAME CRANGE---------- --------------- --------------- 201 airbus375 3000 202 boeing 2000 203 emirates 3500 204 qatar 1000 205 Air India 2000

SQL> select * from flight;

NO FRM TOE DIST DEPARTS ARRIVES PRICE---------- -------------- --- ----------------- -- ---------- --------- ------ --- ------------- ------------- 101 belgaum banglore 550 10-OCT-12 10-OCT-12 4500 102 banglore Frankfurt 550 11-OCT-12 11-OCT-12 2000 103 mumbai new york 1255 05-OCT-11 06-OCT-11 32010 104 banglore new delhi 1337 14-NOV-12 15-NOV-12 35000 105 banglore london 1037 12-DEC-12 13-DEC-12 34000

SQL> select a.aid 2 from aircraft a 3 where a.crange>(select min(dist) 4 from flight 5 where frm = 'banglore' and toe = 'delhi');

AID---------- 201 202 203 204 205

Department of MCA Page 26 JCE, Belgaum

Page 27: Dbms Lab Manual

10MCA38

ASSIGNMENT N0. – 3

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

Department of MCA Page 27 JCE, Belgaum

Page 28: Dbms Lab Manual

10MCA38

SQL> CREATE TABLE STUDENT(REGNO VARCHAR(10) PRIMARY KEY, NAME VARCHAR(15) NOT NULL, MAJOR VARCHAR(15) NOT NULL, BDATE DATE NOT NULL);

Table created.

SQL> desc student Name Null? Type ----------------------------------------- -------- ---------------------------- REGNO NOT NULL VARCHAR2(10) NAME NOT NULL VARCHAR2(15) MAJOR NOT NULL VARCHAR2(15) BDATE NOT NULL DATE

SQL> INSERT INTO STUDENT VALUES('&REGNO', '&NAME','&MAJOR','&BDATE');Enter value for regno: 2JI11MCA01Enter value for name: ABHIEnter value for major: UNIXEnter value for bdate: 11-JAN-1990old 1: INSERT INTO STUDENT VALUES('&REGNO', '&NAME','&MAJOR','&BDATE')new 1: INSERT INTO STUDENT VALUES('2JI11MCA01', 'ABHI','UNIX','11-JAN-1990')

1 row created.

SQL> SELECT * FROM STUDENT;

REGNO NAME MAJOR BDATE----------------- ------------------ -------------------- -------------2JI11MCA01 ABHI UNIX 11-JAN-902JI11MCA02 ABHIS WEB PGM 12-JAN-912JI11MCA03 ROHAN JAVA 13-JAN-922JI11MCA04 RAJ DOT NET 14-JAN-932JI11MCA05 SACHIN PHP 15-JAN-95

Department of MCA Page 28 JCE, Belgaum

Page 29: Dbms Lab Manual

10MCA38

SQL> CREATE TABLE COURSE( COURSE NUMBER(5) PRIMARY KEY, CNAME VARCHAR(15) NOT NULL, DEPT VARCHAR(20) NOT NULL);

Table created.

SQL> DESC COURSE; Name Null? Type ----------------------------------------- -------------------------------------------- COURSE NOT NULL NUMBER(5) CNAME NOT NULL VARCHAR2(15) DEPT NOT NULL VARCHAR2(20)

SQL> INSERT INTO COURSE VALUES('&COURSE','&CNAME','&DEPT');Enter value for course: 1Enter value for cname: WEB DESIGNEnter value for dept: MCAold 1: INSERT INTO COURSE VALUES('&COURSE','&CNAME','&DEPT')new 1: INSERT INTO COURSE VALUES('1','WEB DESIGN','MCA')

1 row created.

SQL> SELECT * FROM COURSE;

COURSE CNAME DEPT---------------------------------------------------------------------------- 1 WEB DESIGN MCA 2 DOT NET MCA 3 JAVA COMPUTER SCIENCE 4 C COMPUTER SCIENCE 5 NETWORKING ELECTRONICS

Department of MCA Page 29 JCE, Belgaum

Page 30: Dbms Lab Manual

10MCA38

SQL> CREATE TABLE ENROLL(REG_NO VARCHAR(10) NOT NULL, COURSE NUMBER(5) NOT NULL, SEM NUMBER(1) NOT NULL CHECK(SEM>0), MARKS NUMBER(3) NOT NULL CHECK(MARKS>0), FOREIGN KEY (REG_NO) REFERENCES STUDENT(REGNO) ON DELETE CASCADE, FOREIGN KEY (COURSE) REFERENCES COURSE(COURSE) ON DELETE CASCADE);

Table created.

SQL> DESC ENROLL

Name Null? Type ----------------------------------------- ----------------- ---------------------------- REG_NO NOT NULL VARCHAR2(10) COURSE NOT NULL NUMBER(5) SEM NOT NULL NUMBER(1) MARKS NOT NULL NUMBER(3)

SQL> INSERT INTO ENROLL VALUES('&REG_NO','&COURSE', &SEM, &MARKS);Enter value for reg_no: 2JI11MCA05Enter value for course: 5Enter value for sem: 4Enter value for marks: 86old 1: INSERT INTO ENROLL VALUES('&REG_NO','&COURSE', &SEM, &MARKS)new 1: INSERT INTO ENROLL VALUES('2JI11MCA05','5', 4, 86)

1 row created.

SQL> SELECT * FROM ENROLL;

REG_NO COURSE SEM MARKS----------------- ---------------- -------------- --------------2JI11MCA05 5 4 862JI11MCA03 3 2 902JI11MCA01 1 3 912JI11MCA02 1 1 94

Department of MCA Page 30 JCE, Belgaum

Page 31: Dbms Lab Manual

10MCA38

SQL> CREATE TABLE TEXT(BOOKISBN NUMBER(10) PRIMARY KEY, BOOKTITLE VARCHAR(20) NOT NULL, PUBLISHER VARCHAR(20) NOT NULL, AUTHOR VARCHAR(25) NOT NULL);

Table created.

SQL> DESC TEXT; Name Null? Type ------------------------------------------------------------------------------------ BOOKISBN NOT NULL NUMBER(10) BOOKTITLE NOT NULL VARCHAR2(20) PUBLISHER NOT NULL VARCHAR2(20) AUTHOR NOT NULL VARCHAR2(25)

SQL> INSERT INTO TEXT VALUES('&BOOKISBN','&BOOKTITLE','&PUBLISHER','&AUTHOR');Enter value for bookisbn: 1234Enter value for booktitle: CEnter value for publisher: TATA MHEnter value for author: HERBERTold 1: INSERT INTO TEXT VALUES('&BOOKISBN','&BOOKTITLE','&PUBLISHER','&AUTHOR')new 1: INSERT INTO TEXT VALUES('1234','C','TATA MH','HERBERT')

1 row created.

SQL> SELECT * FROM TEXT;

BOOKISBN BOOKTITLE PUBLISHER AUTHOR------------------ --------------------------- ----------------------------------------------- 1234 C TATA MH HERBERT 5678 WEB PROGM WILEY CHRIS B 2587 COMP. NETWORKS ANDREW JHONTY 7896 JAVA TATA MH HERBERT

Department of MCA Page 31 JCE, Belgaum

Page 32: Dbms Lab Manual

10MCA38

SQL> CREATE TABLE BOOK_ADOPTION(COURSE NUMBER(5) NOT NULL, SEM NUMBER(1) NOT NULL,

BOOK_ISBN NUMBER(10) NOT NULL, FOREIGN KEY(BOOK_ISBN) REFERENCES TEXT(BOOKISBN) ON DELETE CASCADE, FOREIGN KEY (COURSE) REFERENCES COURSE(COURSE) ON DELETE CASCADE);

Table created.

SQL> DESC BOOK_ADOPTION Name Null? Type ----------------------------------------- ---------------- ---------------------------- COURSE NOT NULL NUMBER(5) SEM NOT NULL NUMBER(1) BOOK_ISBN NOT NULL NUMBER(10)

SQL> INSERT INTO BOOK_ADOPTION VALUES(&COURSE, &SEM, &BOOK_ISBN);Enter value for course: 1Enter value for sem: 4Enter value for book_isbn: 1234old 1: INSERT INTO BOOK_ADOPTION VALUES(&COURSE, &SEM, &BOOK_ISBN)new 1: INSERT INTO BOOK_ADOPTION VALUES(1, 4, 1234)

1 row created.

SQL> SELECT * FROM BOOK_ADOPTION;

COURSE SEM BOOK_ISBN----------------------------------------------- 1 4 1234 1 2 5678 3 4 2587 4 3 5678 3 5 7896 3 1 2587 5 5 7896

Department of MCA Page 32 JCE, Belgaum

Page 33: Dbms Lab Manual

10MCA38

QUERIES

Query iii: Demonstrate how you add a new text book to the database and make this book be adopted by some department

SQL> SELECT * FROM TEXT;

BOOKISBN BOOKTITLE PUBLISHER AUTHOR------------------ --------------------------- ----------------------------------------------- 1234 C TATA MH HERBERT 5678 WEB PROGM WILEY CHRIS B 2587 COMP. NETWORKS ANDREW JHONTY 7896 JAVA TATA MH HERBERT

SQL> SELECT * FROM BOOK_ADOPTION;

COURSE SEM BOOK_ISBN----------------------------------------------- 1 4 1234 1 2 5678 3 4 2587 4 3 5678 3 5 7896 3 1 2587 5 5 7896

Department of MCA Page 33 JCE, Belgaum

Page 34: Dbms Lab Manual

10MCA38

SQL> INSERT INTO TEXT VALUES(7956, 'J2ME','PEARSON','HERBERT');

1 row created.

SQL> SELECT * FROM TEXT;

BOOKISBN BOOKTITLE PUBLISHER AUTHOR------------------ --------------------------- --------------------------------------------- 1234 C TATA MH HERBERT 5678 WEB PROGM WILEY CHRIS B 2587 COMP. NETWORKS ANDREW JHONTY 7896 JAVA TATA MH HERBERT 7956 J2ME PEARSON HERBERT

SQL> INSERT INTO BOOK_ADOPTION VALUES(4,4,7956);

1 row created.

SQL> SELECT * FROM BOOK_ADOPTION;

COURSE SEM BOOK_ISBN----------------- -------------- ----------------- 1 4 1234 1 2 5678 3 4 2587 4 3 5678 3 5 7896 3 1 2587 5 5 7896 4 4 79568 rows selected.

Department of MCA Page 34 JCE, Belgaum

Page 35: Dbms Lab Manual

10MCA38

Query iv: produce a list of text book (include Course#, Book_ISBN, Book_title) in alphabetical order for courses offered by the 'CS' department that use more than two books.

SQL> SELECT * FROM COURSE; COURSE CNAME DEPT---------------------------------------------------------------------------- 1 WEB DESIGN MCA 2 DOT NET MCA 3 JAVA COMPUTER SCIENCE 4 C COMPUTER SCIENCE 5 NETWORKING ELECTRONICS

SQL> SELECT * FROM TEXT; BOOKISBN BOOKTITLE PUBLISHER AUTHOR------------------ --------------------------- ----------------------------------------------- 1234 C TATA MH HERBERT 5678 WEB PROGM WILEY CHRIS B 2587 COMP. NETWORKS ANDREW JHONTY 7896 JAVA TATA MH HERBERT

SQL> SELECT * FROM BOOK_ADOPTION; COURSE SEM BOOK_ISBN----------------------------------------------- 1 4 1234 1 2 5678 3 4 2587 4 3 5678 3 5 7896 3 1 2587 5 5 7896

Department of MCA Page 35 JCE, Belgaum

Page 36: Dbms Lab Manual

10MCA38

SQL> SELECT C.COURSE, T.BOOKISBN, T.BOOKTITLE FROM COURSE C, TEXT T, BOOK_ADOPTION B WHERE C.DEPT = 'COMPUTER SCIENCE' AND C.COURSE = B.COURSE AND B.BOOK_ISBN = T.BOOKISBN AND C.COURSE IN(SELECT B1.COURSE FROM BOOK_ADOPTION B1 GROUP BY B1.COURSE HAVING COUNT(*)>2) ORDER BY T.BOOKTITLE;

COURSE BOOKISBN BOOKTITLE----------------- ------------------------------------------- 3 2587 COMP. NETWORKS 3 2587 COMP. NETWORKS 3 7896 JAVA

Department of MCA Page 36 JCE, Belgaum

Page 37: Dbms Lab Manual

10MCA38

Query v: List any department that has all its adopted books published by a specific Publisher.

SQL> SELECT C.DEPT FROM COURSE C, TEXT T WHERE NOT EXISTS((SELECT BOOKISBN FROM TEXT WHERE PUBLISHER = 'PEARSON') MINUS (SELECT BOOK_ISBN FROM COURSE O, BOOK_ADOPTION B WHERE B.COURSE = O.COURSE AND O.DEPT = C.DEPT));

DEPT---------------------------------COMPUTER SCIENCECOMPUTER SCIENCE

Department of MCA Page 37 JCE, Belgaum

Page 38: Dbms Lab Manual

10MCA38

ASSIGNMENT N0. – 4

BOOK DEALER DATABASE

The following tables are maintained by a book dealer.

AUTHOR (author-id:int, name:string, city:string, country:string)

PUBLISHER (publisher-id:int, name:string, city:string, country:string)

CATALOG (book-id:int, title:string, author-id:int, publisher-id:int, category-id:int, year:int, price:int)

CATEGORY (category-id:int, description:string)

ORDER-DETAILS (order-no:int, book-id:int, quantity:int)

i. Create the above tables by properly specifying the primary keys and the foreign keys.

ii. Enter at least five tuples for each relation.

iii. Give the details of the authors who have 2 or more books in the catalog and the price of the books is greater

than the average price of the books in the catalog and the year of publication is after 2000.

iv. Find the author of the book which has maximum sales.

v. Demonstrate how you increase the price of books published by a specific publisher by 10%.

vi. Generate suitable reports.

vii. Create suitable front end for querying and displaying the results.

Department of MCA Page 38 JCE, Belgaum

Page 39: Dbms Lab Manual

10MCA38

SQL> create table author(author_id number(5) primary key, author_name varchar(15),

city varchar(15), country varchar(15));

Table created.

SQL> desc author Name Null? Type ----------------------------------------- ------------------- ---------------------------- AUTHOR_ID NOT NULL NUMBER(5) AUTHOR_NAME VARCHAR2(15) CITY VARCHAR2(15) COUNTRY VARCHAR2(15)

SQL> INSERT INTO AUTHOR VALUES('&AUTHOR_ID','&AUTHOR_NAME','&CITY','&COUNTRY');Enter value for author_id: 101Enter value for author_name: NAVATHEEnter value for city: DELHIEnter value for country: INDIAold 1: INSERT INTO AUTHOR VALUES('&AUTHOR_ID','&AUTHOR_NAME','&CITY','&COUNTRY')new 1: INSERT INTO AUTHOR VALUES('101','NAVATHE','DELHI','INDIA')

1 row created.

SQL> SELECT * FROM AUTHOR;

AUTHOR_ID AUTHOR_NAME CITY COUNTRY------------------ ------------------------ --------------------- -------------------- 101 NAVATHE DELHI INDIA 102 KANITKAR NAGPUR INDIA 103 SALIM DHAKA BANGLADESH 104 HERBERT NEW YORK AMERICA 105 JAWADEKAR PUNE INDIA 106 BALGURUSWAMY BANGLORE INDIA 107 LIPPMAN NEW JERSY INDIA

7 rows selected.

Department of MCA Page 39 JCE, Belgaum

Page 40: Dbms Lab Manual

10MCA38

SQL> create table publisher(publisher_id NUMBER(5) primary key, 2 publisher_name varchar(15), 3 city varchar(15), 4 country varchar(15));

Table created.

SQL> DESC PUBLISHER; Name Null? Type ----------------------------------------- ----------------- ---------------------------- PUBLISHER_ID NOT NULL NUMBER(5) PUBLISHER_NAME VARCHAR2(15) CITY VARCHAR2(15) COUNTRY VARCHAR2(15)

SQL> INSERT INTO PUBLISHER VALUES(&PUBLISHER_ID,'&PUBLISHER_NAME','&CITY','&COUNTRY');Enter value for publisher_id: 201Enter value for publisher_name: BPBEnter value for city: DELHIEnter value for country: INDIAold 1: INSERT INTO PUBLISHER VALUES(&PUBLISHER_ID,'&PUBLISHER_NAME','&CITY','&COUNTRY')new 1: INSERT INTO PUBLISHER VALUES(201,'BPB','DELHI','INDIA')

1 row created.

SQL> SELECT * FROM PUBLISHER;

PUBLISHER_ID PUBLISHER_NAME CITY COUNTRY---------------------- --------------------------- -------------------- ---------------------- 201 BPB DELHI INDIA 202 PEARSON HONGKONG CHINA 203 WILEY LOS VEGAS USA 204 TATA MH SIDNEY AUSTRALIA 205 WATSON HARARE ZIMBABWE 206 MAULANA DHAKA BANGLADESH 207 PEP BANGLORE INDIA

7 rows selected.

Department of MCA Page 40 JCE, Belgaum

Page 41: Dbms Lab Manual

10MCA38

Department of MCA Page 41 JCE, Belgaum

Page 42: Dbms Lab Manual

10MCA38

SQL> create table category(category_id number(5) primary key, description varchar(15));

Table created.

SQL> desc category Name Null? Type ----------------------------------------- ---------------- ---------------------------- CATEGORY_ID NOT NULL NUMBER(5) DESCRIPTION VARCHAR2(15)

SQL> INSERT INTO CATEGORY VALUES(&CATEGORY_ID, '&DESCRIPTION');Enter value for category_id: 301Enter value for description: COMPUTERSold 1: INSERT INTO CATEGORY VALUES(&CATEGORY_ID, '&DESCRIPTION')new 1: INSERT INTO CATEGORY VALUES(301, 'COMPUTERS')

1 row created.

SQL> SELECT * FROM CATEGORY ;

CATEGORY_ID DESCRIPTION---------------------- ----------------------- 301 COMPUTERS 302 SCIENCE 303 ARTS 304 AERONATICS 305 SPORTS 306 HEALTH 307 COMMERCE 308 ROBOTICS

8 rows selected.

Department of MCA Page 42 JCE, Belgaum

Page 43: Dbms Lab Manual

10MCA38

SQL>create table catalog(book_id number(5) primary key, tittle varchar(15), author_id number(5), publisher_id number(5), category_id number(5), year number(5), price number(7,2), foreign key(author_id) references author(author_id), foreign key(publisher_id) references publisher(publisher_id), foreign key(category_id) references category(category_id));

SQL> desc catalog Name Null? Type ----------------------------------------- ----------------- ---------------------------- BOOK_ID NOT NULL NUMBER(5) TITTLE VARCHAR2(15) AUTHOR_ID NUMBER(5) PUBLISHER_ID NUMBER(5) CATEGORY_ID NUMBER(5) YEAR NUMBER(5) PRICE NUMBER(7,2)

SQL> insert into catalog values(&book_id,'&title',&author_id,&publisher_id,&category_id, &year, &price);Enter value for book_id: 501Enter value for title: c programmingEnter value for author_id: 101Enter value for publisher_id: 201Enter value for category_id: 301Enter value for year: 2001Enter value for price: 800old 1: insert into catalog values(&book_id,'&title',&author_id,&publisher_id,&category_id, &year, &price)new 1: insert into catalog values(501,'c programming',101,201,301, 2001, 800)

1 row created.

Department of MCA Page 43 JCE, Belgaum

Page 44: Dbms Lab Manual

10MCA38

SQL> select * from catalog;

BOOK_ID TITTLE AUTHOR_ID PUBLISHER_ID CATEGORY_ID YEAR PRICE---------------- ---------------------- ----------------------- ----------------------- ---------------------- ----------- ------------- 501 c programming 101 201 301 2001 800 502 ASP 102 202 303 2005 7452 503 c++ 103 203 303 2011 450 504 soft. engg. 104 203 303 2009 789 505 c graphics 105 204 305 2007 4123 506 java 104 205 304 2002 450 507 economics 105 206 306 2008 412 508 RUBY 104 201 303 2005 412 509 PYTHON 103 202 302 2011 560

9 rows selected.

Department of MCA Page 44 JCE, Belgaum

Page 45: Dbms Lab Manual

10MCA38

SQL>create table order_details(order_no number(5) primary key, book_id number(5),

quantity number(3), foreign key(book_id) references catalog(book_id));

Table created.

SQL> desc order_details Name Null? Type ----------------------------------------- ---------------- ----------------------------

ORDER_NO NOT NULL NUMBER(5) BOOK_ID NUMBER(5) QUANTITY NUMBER(3)

SQL> insert into order_details values(&order_no,&book_id,&quantity);Enter value for order_no: 601Enter value for book_id: 501Enter value for quantity: 10old 1: insert into order_details values(&order_no,&book_id,&quantity)new 1: insert into order_details values(601,501,10)

1 row created.

SQL> select * from order_details;

ORDER_NO BOOK_ID QUANTITY------------------- ----------------- ----------------- 601 501 10 602 503 4 603 504 8 604 505 40 605 501 15 606 506 3 607 505 12

7 rows selected.

Department of MCA Page 45 JCE, Belgaum

Page 46: Dbms Lab Manual

10MCA38

QUERIES

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

SQL> select * from catalog;

BOOK_ID TITTLE AUTHOR_ID PUBLISHER_ID CATEGORY_ID YEAR PRICE---------------- ---------------------- ----------------------- ----------------------- ---------------------- ----------- ------------- 501 c programming 101 201 301 2001 800 502 ASP 102 202 303 2005 7452 503 c++ 103 203 303 2011 450 504 soft. engg. 104 203 303 2009 789 505 c graphics 105 204 305 2007 4123 506 java 104 205 304 2002 450 507 economics 105 206 306 2008 412 508 RUBY 104 201 303 2005 412 509 PYTHON 103 202 302 2011 560

9 rows selected.

SQL> SELECT * FROM AUTHOR;

AUTHOR_ID AUTHOR_NAME CITY COUNTRY------------------ ------------------------ --------------------- -------------------- 101 NAVATHE DELHI INDIA 102 KANITKAR NAGPUR INDIA 103 SALIM DHAKA BANGLADESH 104 HERBERT NEW YORK AMERICA 105 JAWADEKAR PUNE INDIA 106 BALGURUSWAMY BANGLORE INDIA 107 LIPPMAN NEW JERSY INDIA

7 rows selected.

Department of MCA Page 46 JCE, Belgaum

Page 47: Dbms Lab Manual

10MCA38

SQL> select distinct(a.author_id), a.author_name, a.city, a.country from author a, catalog c where a.author_id in(select author_id from catalog where year>2000 group by author_id having count(*)>=2 and sum(price)>(select avg(price) from catalog));

AUTHOR_ID AUTHOR_NAME CITY COUNTRY

------------------ ------------------------ --------------- ------------------

105 JAWADEKAR PUNE INDIA

Department of MCA Page 47 JCE, Belgaum

Page 48: Dbms Lab Manual

10MCA38

QUERY IV: Find the author of the book which has maximum sales.

SQL> select * from catalog;

BOOK_ID TITTLE AUTHOR_ID PUBLISHER_ID CATEGORY_ID YEAR PRICE---------------- ---------------------- ----------------------- ----------------------- ---------------------- ----------- ------------- 501 c programming 101 201 301 2001 800 502 ASP 102 202 303 2005 7452 503 c++ 103 203 303 2011 450 504 soft. engg. 104 203 303 2009 789 505 c graphics 105 204 305 2007 4123 506 java 104 205 304 2002 450 507 economics 105 206 306 2008 412 508 RUBY 104 201 303 2005 412 509 PYTHON 103 202 302 2011 560

9 rows selected.

SQL> SELECT * FROM AUTHOR;

AUTHOR_ID AUTHOR_NAME CITY COUNTRY------------------ ------------------------ --------------------- -------------------- 101 NAVATHE DELHI INDIA 102 KANITKAR NAGPUR INDIA 103 SALIM DHAKA BANGLADESH 104 HERBERT NEW YORK AMERICA 105 JAWADEKAR PUNE INDIA 106 BALGURUSWAMY BANGLORE INDIA 107 LIPPMAN NEW JERSY INDIA

7 rows selected.

SQL> select * from order_details;

ORDER_NO BOOK_ID QUANTITY------------------- ----------------- ----------------- 601 501 10 602 503 4 603 504 8 604 505 40 605 501 15 606 506 3 607 505 12

7 rows selected.

Department of MCA Page 48 JCE, Belgaum

Page 49: Dbms Lab Manual

10MCA38

SQL> SELECT A.AUTHOR_NAME, O.BOOK_ID FROM AUTHOR A, CATALOG C, ORDER_DETAILS O WHERE A.AUTHOR_ID=C.AUTHOR_ID AND C.BOOK_ID = O.BOOK_ID AND O.QUANTITY IN (SELECT MAX(QUANTITY) FROM ORDER_DETAILS);

AUTHOR_NAME BOOK_ID------------------------- ---------------JAWADEKAR 505

Department of MCA Page 49 JCE, Belgaum

Page 50: Dbms Lab Manual

10MCA38

QUERY V: Demonstrate how you increase the price of book published by a specific publisher by 10%.

SQL> select * from catalog;

BOOK_ID TITTLE AUTHOR_ID PUBLISHER_ID CATEGORY_ID YEAR PRICE---------------- ---------------------- ----------------------- ----------------------- ---------------------- ----------- ------------- 501 c programming 101 201 301 2001 800 502 ASP 102 202 303 2005 7452 503 c++ 103 203 303 2011 450 504 soft. engg. 104 203 303 2009 789 505 c graphics 105 204 305 2007 4123 506 java 104 205 304 2002 450 507 economics 105 206 306 2008 412 508 RUBY 104 201 303 2005 412 509 PYTHON 103 202 302 2011 560

9 rows selected.

SQL> SELECT * FROM PUBLISHER;

PUBLISHER_ID PUBLISHER_NAME CITY COUNTRY---------------------- --------------------------- -------------------- ---------------------- 201 BPB DELHI INDIA 202 PEARSON HONGKONG CHINA 203 WILEY LOS VEGAS USA 204 TATA MH SIDNEY AUSTRALIA 205 WATSON HARARE ZIMBABWE 206 MAULANA DHAKA BANGLADESH 207 PEP BANGLORE INDIA

7 rows selected.

Department of MCA Page 50 JCE, Belgaum

Page 51: Dbms Lab Manual

10MCA38

SQL> UPDATE CATALOG SET PRICE = (PRICE*0.10)+PRICE WHERE PUBLISHER_ID IN(SELECT P.PUBLISHER_ID FROM PUBLISHER P WHERE P.PUBLISHER_NAME ='TATA MH');

1 row updated.

SQL> select * from catalog;

BOOK_ID TITTLE AUTHOR_ID PUBLISHER_ID CATEGORY_ID YEAR PRICE---------------- ---------------------- ----------------------- ----------------------- ---------------------- ----------- ------------- 501 c programming 101 201 301 2001 800 502 ASP 102 202 303 2005 7452 503 c++ 103 203 303 2011 450 504 soft. engg. 104 203 303 2009 789 505 c graphics 105 204 305 2007 4535.3 506 java 104 205 304 2002 450 507 economics 105 206 306 2008 412 508 RUBY 104 201 303 2005 412 509 PYTHON 103 202 302 2011 560

9 rows selected.

Department of MCA Page 51 JCE, Belgaum

Page 52: Dbms Lab Manual

10MCA38

ASSIGNMENT N0. – 5

BANKING ENTERPRISE DATABASE

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.

Department of MCA Page 52 JCE, Belgaum

Page 53: Dbms Lab Manual

10MCA38

SQL> CREATE TABLE BRANCH(branch_name varchar(10) PRIMARY KEY, branch_city varchar(10) NOT NULL, assets decimal(10, 2)NOT NULL);

Table created.

SQL> desc branch Name Null? Type ----------------------------------------- ---------------- ---------------------------- BRANCH_NAME NOT NULL VARCHAR2(10) BRANCH_CITY NOT NULL VARCHAR2(10) ASSETS NOT NULL NUMBER(10,2)

SQL> insert into branch values('&branch_name','&branch_city',&assets);Enter value for branch_name: SBIEnter value for branch_city: BrooklynEnter value for assets: 7000000old 1: insert into branch values('&branch_name','&branch_city',&assets)new 1: insert into branch values('SBI','Brooklyn',7000000)

1 row created.

SQL> SELECT * FROM BRANCH;

BRANCH_NAM BRANCH_CIT ASSETS---------------------- ------------------- ---------------- SBI Brooklyn 7000000 MAIN Banglore 400000 SYNDICATE Manglore 4500000 VIJAYA HASSAN 852134 HDFC BELGAUM 600012 AXIS HUBLI 1230001

6 rows selected.

Department of MCA Page 53 JCE, Belgaum

Page 54: Dbms Lab Manual

10MCA38

SQL> CREATE TABLE ACCOUNT(accno number(6) PRIMARY KEY, branch_name varchar(10) NOT NULL, balance decimal(10, 2)NOT NULL, FOREIGN KEY(branch_name ) references BRANCH(branch_name));

Table created.

SQL> desc account; Name Null? Type ----------------------------------------- ----------------- ---------------------------- ACCNO NOT NULL NUMBER(6) BRANCH_NAME NOT NULL VARCHAR2(10) BALANCE NOT NULL NUMBER(10,2)

SQL> insert into account values(&accno,'&branch_name',&balance);Enter value for accno: 1501Enter value for branch_name: MAINEnter value for balance: 23146old 1: insert into account values(&accno,'&branch_name',&balance)new 1: insert into account values(1501,'MAIN',23146)

1 row created.

SQL> SELECT * FROM ACCOUNT;

ACCNO BRANCH_NAM BALANCE---------------- --------------------- ----------------- 1501 MAIN 23146 1502 MAIN 50000 1503 SBI 6100 1504 SYNDICATE 750 1505 HDFC 150

Department of MCA Page 54 JCE, Belgaum

Page 55: Dbms Lab Manual

10MCA38

SQL> CREATE TABLE CUSTOMER(customer_name varchar(10) primary key, 2 customer_street varchar(10), 3 customer_city varchar(10));

Table created.

SQL> desc customer Name Null? Type ----------------------------------------- --------------- ---------------------------- CUSTOMER_NAME NOT NULL VARCHAR2(10) CUSTOMER_STREET VARCHAR2(10) CUSTOMER_CITY VARCHAR2(10)

SQL> insert into customer values('&customer_name','&customer_street','&customer_city');Enter value for customer_name: AnilEnter value for customer_street: WalnutEnter value for customer_city: italyold 1: insert into customer values('&customer_name','&customer_street','&customer_city')new 1: insert into customer values('Anil','Walnut','italy')

1 row created.

SQL> select * from customer;

CUSTOMER_N CUSTOMER_S CUSTOMER_C--------------------- --------------------- -------------------- Anil Walnut italy Bimal Gandhi South AF Civil Golf USA dinil Khade BZR Belgaum Eifel BTM Banglore final Los Vegas US

6 rows selected.

Department of MCA Page 55 JCE, Belgaum

Page 56: Dbms Lab Manual

10MCA38

SQL> CREATE TABLE DEPOSITOR(customer_name varchar(10) not null, accno number(6) not null, foreign key(customer_name)references customer(customer_name), foreign key(accno) references account(accno));

Table created.

SQL> desc depositor Name Null? Type ----------------------------------------- ----------------- ---------------------------- CUSTOMER_NAME NOT NULL VARCHAR2(10) ACCNO NOT NULL NUMBER(6)

SQL> insert into depositor values('&customer_name','&accno');Enter value for customer_name: AnilEnter value for accno: 1501old 1: insert into depositor values('&customer_name','&accno')new 1: insert into depositor values('Anil','1501')

1 row created.

SQL> select * from depositor;

CUSTOMER_N ACCNO---------------------- ----------------Anil 1501dinil 1502Anil 1502Bimal 1503Civil 1505

Department of MCA Page 56 JCE, Belgaum

Page 57: Dbms Lab Manual

10MCA38

SQL>CREATE TABLE LOAN(loan_number number(5) PRIMARY KEY, branch_name varchar(10) NOT NULL, amount decimal(10, 2) NOT NULL, FOREIGN KEY(branch_name) references BRANCH(branch_name));Table created.

SQL> desc loan

Name Null? Type ----------------------------------------- ----------------- ---------------------------- LOAN_NUMBER NOT NULL NUMBER(5) BRANCH_NAME NOT NULL VARCHAR2(10) AMOUNT NOT NULL NUMBER(10,2)

SQL> insert into loan values(&loan_number,'&branch_name',&amount);Enter value for loan_number: 11Enter value for branch_name: MAINEnter value for amount: 15000old 1: insert into loan values(&loan_number,'&branch_name',&amount)new 1: insert into loan values(11,'MAIN',15000)

1 row created.

SQL> SELECT * FROM LOAN;

LOAN_NUMBER BRANCH_NAM AMOUNT----------------------- ---------------------- ----------------- 11 MAIN 15000 12 SBI 1500 13 SYNDICATE 900 14 HDFC 1200 15 MAIN 8000

Department of MCA Page 57 JCE, Belgaum

Page 58: Dbms Lab Manual

10MCA38

SQL>CREATE TABLE BORROWER(customer_name varchar(10) NOT NULL, loan_number number(5) NOT NULL, FOREIGN KEY(customer_name) references CUSTOMER(customer_name),

FOREIGN KEY(loan_number) references LOAN(loan_number));

Table created.

SQL> desc borrower Name Null? Type ----------------------------------------- ------------------ ---------------------------- CUSTOMER_NAME NOT NULL VARCHAR2(10) LOAN_NUMBER NOT NULL NUMBER(5)

SQL> insert into borrower values('&customer_name',&loan_number);Enter value for customer_name: AnilEnter value for loan_number: 12old 1: insert into borrower values('&customer_name',&loan_number)new 1: insert into borrower values('Anil',12)

1 row created.

SQL> select * from borrower;

CUSTOMER_N LOAN_NUMBER--------------------- -----------------------Anil 12Civil 13Anil 14final 15Eifel 12

Department of MCA Page 58 JCE, Belgaum

Page 59: Dbms Lab Manual

10MCA38

QUERIES

Query III : Find all the customers who have at least two accounts at the Main branch.

SQL> select * from depositor;

CUSTOMER_N ACCNO---------------------- ----------------Anil 1501dinil 1502Anil 1502Bimal 1503Civil 1505

SQL> SELECT * FROM ACCOUNT;

ACCNO BRANCH_NAM BALANCE---------------- --------------------- ----------------- 1501 MAIN 23146 1502 MAIN 50000 1503 SBI 6100 1504 SYNDICATE 750 1505 HDFC 150

SQL> SELECT D.CUSTOMER_NAME FROM DEPOSITOR D, ACCOUNT A WHERE D.ACCNO = A.ACCNO AND A.BRANCH_NAME = 'MAIN' GROUP BY D.CUSTOMER_NAME HAVING COUNT(D.ACCNO)>=2;

CUSTOMER_N--------------------Anil

Department of MCA Page 59 JCE, Belgaum

Page 60: Dbms Lab Manual

10MCA38

Query IV: Find all the customers who have an account at all the branches located in a specific city.

SQL> select * from depositor;CUSTOMER_N ACCNO---------------------- ----------------Anil 1501dinil 1502Anil 1502Bimal 1503Civil 1505

SQL> SELECT * FROM ACCOUNT; ACCNO BRANCH_NAM BALANCE---------------- --------------------- ----------------- 1501 MAIN 23146 1502 MAIN 50000 1503 SBI 6100 1504 SYNDICATE 750 1505 HDFC 150

SQL> SELECT * FROM BRANCH;BRANCH_NAM BRANCH_CIT ASSETS---------------------- ------------------- ---------------- SBI Brooklyn 7000000 MAIN Banglore 400000 SYNDICATE Manglore 4500000 VIJAYA HASSAN 852134 HDFC BELGAUM 600012 AXIS HUBLI 1230001

SQL> SELECT D.CUSTOMER_NAME, A.BRANCH_NAME FROM DEPOSITOR D, ACCOUNT A, BRANCH B WHERE D.ACCNO = A.ACCNO AND A.BRANCH_NAME = B.BRANCH_NAME AND B.BRANCH_CITY = 'Brooklyn';

CUSTOMER_N BRANCH_NAM---------------------- --------------------Bimal SBI

Department of MCA Page 60 JCE, Belgaum

Page 61: Dbms Lab Manual

10MCA38

Query V: Demonstrate how you delete all account tuples at every branch located in a specific city.

SQL> SELECT * FROM ACCOUNT; ACCNO BRANCH_NAM BALANCE---------------- --------------------- ----------------- 1501 MAIN 23146 1502 MAIN 50000 1503 SBI 6100 1504 SYNDICATE 750 1505 HDFC 150

SQL> SELECT * FROM BRANCH;BRANCH_NAM BRANCH_CIT ASSETS---------------------- ------------------- ---------------- SBI Brooklyn 7000000 MAIN Banglore 400000 SYNDICATE Manglore 4500000 VIJAYA HASSAN 852134 HDFC BELGAUM 600012 AXIS HUBLI 1230001

SQL> delete 2 from account 3 where branch_name in(select branch_name 4 from branch 5 where branch_city='Manglore');

1 row deleted.

SQL> SELECT * FROM ACCOUNT;

ACCNO BRANCH_NAM BALANCE---------------- --------------------- ----------------- 1501 MAIN 23146 1502 MAIN 50000 1503 SBI 6100 1505 HDFC 150

Department of MCA Page 61 JCE, Belgaum