You are on page 1of 58

10MCA38

Sl.no. 1 Consider the following relations: Title of the program

DBMS LABORATORY
Page no. 4

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.

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 1

13

JCE, Belgaum

10MCA38
3

DBMS LABORATORY
25

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.

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

Department of MCA

Page 2

JCE, Belgaum

10MCA38
5 Consider the following database for a banking enterprise

DBMS LABORATORY
49

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 3

JCE, Belgaum

10MCA38

DBMS LABORATORY

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

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 SNAME MAJOR LEVELS AGE NOT NULL NUMBER(5) VARCHAR2(10) VARCHAR2(10) VARCHAR2(2) NUMBER(2)

DBMS LABORATORY

SQL> INSERT INTO STUDENTT VALUES(&SNUM,'&SNAME','&MAJOR','&LEVELS',&AGE); Enter value for snum: 901 Enter value for sname: AJIT Enter value for major: MATHS Enter value for levels: SR Enter value for age: 25 old 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 --------------- --------------- ------------------901 AJIT MATHS 902 AMIR COMP. SC 903 BINDU OR 904 DIYA PHYSICS 905 ESHA CHEMISTRY LE AGE -- ---------SR 25 JR 21 SR 27 JR 22 SR 24

Department of MCA

Page 5

JCE, Belgaum

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: 11 Enter value for fname: RAHUL Enter value for deptid: 4441 old 1: INSERT INTO FACULTY VALUES(&FID,'&FNAME',&DEPTID) new 1: INSERT INTO FACULTY VALUES(11,'RAHUL',4441) 1 row created.

DBMS LABORATORY

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

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: ELECTRICAL Enter value for meets_at: 9.00 Enter value for room: A101 Enter value for fid: 11 old 1: INSERT INTO CLASS VALUES('&CNAME','&MEETS_AT','&ROOM',&FID) new 1: INSERT INTO CLASS VALUES('ELECTRICAL','9.00','A101',11) 1 row created.

DBMS LABORATORY

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

Department of MCA

Page 7

JCE, Belgaum

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: 901 Enter value for cname: ELECTRICAL old 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.

DBMS LABORATORY

Department of MCA

Page 8

JCE, Belgaum

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

DBMS LABORATORY

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

10MCA38

DBMS LABORATORY

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 11 E&C 10.00 A202 22 CS01 11.00 A204 11 CS02 12.00 B101 33 MECH 10.00 C101 55 MCA 12.00 B312 66 MBA 10.00 R128 33 MCA02 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 ---------ELECTRICAL MBA

Department of MCA

Page 10

JCE, Belgaum

10MCA38

DBMS LABORATORY

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

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

10MCA38

DBMS LABORATORY

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 -------------RAHUL TANMAY SRIDEVI SACHIN HARSHITH SACHA TINA RANJIT 8 rows selected.

Department of MCA

Page 12

JCE, Belgaum

10MCA38 ASSIGNMENT N0. 2 AIRCRAFT DATABASE

DBMS LABORATORY

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 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. Rs.80,

Department of MCA

Page 13

JCE, Belgaum

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? -------------------------- --------------- -----NO NOT NULL FRM TOE DIST DEPARTS ARRIVES PRICE Type -- ----------------------NUMBER(5) VARCHAR2(20) VARCHAR2(20) NUMBER(4) DATE DATE FLOAT(63)

DBMS LABORATORY

SQL> insert into flight values(&no,'&frm','&toe','&dist','&departs','&arrives','&price'); Enter value for no: 101 Enter value for frm: belgaum Enter value for toe: banglore Enter value for dist: 550 Enter value for departs: 10-oct-12 Enter value for arrives: 10-oct-12 Enter value for price: 4500 old 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

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.

DBMS LABORATORY

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 ANAME CRANGE NOT NULL NUMBER(5) VARCHAR2(15) NUMBER(5)

SQL> insert into aircraft values(&aid,'&aname','&crange'); Enter value for aid: 201 Enter value for aname: airbus375 Enter value for crange: 3000 old 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

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: 301 Enter value for ename: abhijit Enter value for salary: 65000 old 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

DBMS LABORATORY

Department of MCA

Page 16

JCE, Belgaum

10MCA38

DBMS LABORATORY

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 AID NUMBER(5) NUMBER(5)

SQL> insert into certified values(&eid,&aid); Enter value for eid: 301 Enter value for aid: 201 old 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 17

JCE, Belgaum

10MCA38 QUERIES

DBMS LABORATORY

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 --------------emirates boeing airbus375 Department of MCA Page 18 JCE, Belgaum

10MCA38

DBMS LABORATORY

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 19

JCE, Belgaum

10MCA38

DBMS LABORATORY

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 20

JCE, Belgaum

10MCA38

DBMS LABORATORY

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 21 JCE, Belgaum

10MCA38

DBMS LABORATORY

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 22

JCE, Belgaum

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

DBMS LABORATORY

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 23

JCE, Belgaum

10MCA38

DBMS LABORATORY

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

10MCA38 ASSIGNMENT N0. 3 STUDENT ENROLLMENT DATABASE

DBMS LABORATORY

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 25

JCE, Belgaum

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.

DBMS LABORATORY

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: 2JI11MCA01 Enter value for name: ABHI Enter value for major: UNIX Enter value for bdate: 11-JAN-1990 old 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-90 2JI11MCA02 ABHIS WEB PGM 12-JAN-91 2JI11MCA03 ROHAN JAVA 13-JAN-92 2JI11MCA04 RAJ DOT NET 14-JAN-93 2JI11MCA05 SACHIN PHP 15-JAN-95

Department of MCA

Page 26

JCE, Belgaum

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: 1 Enter value for cname: WEB DESIGN Enter value for dept: MCA old 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

DBMS LABORATORY

Department of MCA

Page 27

JCE, Belgaum

10MCA38

DBMS LABORATORY

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: 2JI11MCA05 Enter value for course: 5 Enter value for sem: 4 Enter value for marks: 86 old 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 86 2JI11MCA03 3 2 90 2JI11MCA01 1 3 91 2JI11MCA02 1 1 94

Department of MCA

Page 28

JCE, Belgaum

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)

DBMS LABORATORY

SQL> INSERT INTO TEXT VALUES('&BOOKISBN','&BOOKTITLE','&PUBLISHER','&AUTHOR'); Enter value for bookisbn: 1234 Enter value for booktitle: C Enter value for publisher: TATA MH Enter value for author: HERBERT old 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 29

JCE, Belgaum

10MCA38

DBMS LABORATORY

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: 1 Enter value for sem: 4 Enter value for book_isbn: 1234 old 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 30

JCE, Belgaum

10MCA38 QUERIES

DBMS LABORATORY

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 31

JCE, Belgaum

10MCA38

DBMS LABORATORY

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 7956 8 rows selected.

Department of MCA

Page 32

JCE, Belgaum

10MCA38

DBMS LABORATORY

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 33

JCE, Belgaum

10MCA38

DBMS LABORATORY

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 34

JCE, Belgaum

10MCA38

DBMS LABORATORY

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

Department of MCA

Page 35

JCE, Belgaum

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)

DBMS LABORATORY

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 36

JCE, Belgaum

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)

DBMS LABORATORY

SQL> INSERT INTO AUTHOR VALUES('&AUTHOR_ID','&AUTHOR_NAME','&CITY','&COUNTRY'); Enter value for author_id: 101 Enter value for author_name: NAVATHE Enter value for city: DELHI Enter value for country: INDIA old 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 37

JCE, Belgaum

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)

DBMS LABORATORY

SQL> INSERT INTO PUBLISHER VALUES(&PUBLISHER_ID,'&PUBLISHER_NAME','&CITY','&COUN TRY'); Enter value for publisher_id: 201 Enter value for publisher_name: BPB Enter value for city: DELHI Enter value for country: INDIA old 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 38

JCE, Belgaum

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)

DBMS LABORATORY

SQL> INSERT INTO CATEGORY VALUES(&CATEGORY_ID, '&DESCRIPTION'); Enter value for category_id: 301 Enter value for description: COMPUTERS old 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 39

JCE, Belgaum

10MCA38

DBMS LABORATORY

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,&cate gory_id, &year, &price); Enter value for book_id: 501 Enter value for title: c programming Enter value for author_id: 101 Enter value for publisher_id: 201 Enter value for category_id: 301 Enter value for year: 2001 Enter value for price: 800 old 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 40

JCE, Belgaum

10MCA38

DBMS LABORATORY

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 41

JCE, Belgaum

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 BOOK_ID QUANTITY NOT NULL NUMBER(5) NUMBER(5) NUMBER(3)

DBMS LABORATORY

SQL> insert into order_details values(&order_no,&book_id,&quantity); Enter value for order_no: 601 Enter value for book_id: 501 Enter value for quantity: 10 old 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 42

JCE, Belgaum

10MCA38 QUERIES

DBMS LABORATORY

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 43

JCE, Belgaum

10MCA38

DBMS LABORATORY

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 44

JCE, Belgaum

10MCA38
QUERY IV: Find the author of the book which has maximum sales. SQL> select * from catalog;

DBMS LABORATORY

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 45

JCE, Belgaum

10MCA38

DBMS LABORATORY

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 46

JCE, Belgaum

10MCA38

DBMS LABORATORY

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 47

JCE, Belgaum

10MCA38

DBMS LABORATORY

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 48

JCE, Belgaum

10MCA38 ASSIGNMENT N0. 5 BANKING ENTERPRISE DATABASE

DBMS LABORATORY

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 49

JCE, Belgaum

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)

DBMS LABORATORY

SQL> insert into branch values('&branch_name','&branch_city',&assets); Enter value for branch_name: SBI Enter value for branch_city: Brooklyn Enter value for assets: 7000000 old 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 50

JCE, Belgaum

10MCA38

DBMS LABORATORY

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: 1501 Enter value for branch_name: MAIN Enter value for balance: 23146 old 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 51

JCE, Belgaum

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)

DBMS LABORATORY

SQL> insert into customer values('&customer_name','&customer_street','&customer_city'); Enter value for customer_name: Anil Enter value for customer_street: Walnut Enter value for customer_city: italy old 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 52

JCE, Belgaum

10MCA38

DBMS LABORATORY

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: Anil Enter value for accno: 1501 old 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 1501 dinil 1502 Anil 1502 Bimal 1503 Civil 1505

Department of MCA

Page 53

JCE, Belgaum

10MCA38

DBMS LABORATORY

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: 11 Enter value for branch_name: MAIN Enter value for amount: 15000 old 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 54

JCE, Belgaum

10MCA38

DBMS LABORATORY

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: Anil Enter value for loan_number: 12 old 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 12 Civil 13 Anil 14 final 15 Eifel 12

Department of MCA

Page 55

JCE, Belgaum

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

DBMS LABORATORY

SQL> select * from depositor; CUSTOMER_N ACCNO ---------------------- ---------------Anil 1501 dinil 1502 Anil 1502 Bimal 1503 Civil 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 56

JCE, Belgaum

10MCA38

DBMS LABORATORY

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 1501 dinil 1502 Anil 1502 Bimal 1503 Civil 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 57

JCE, Belgaum

10MCA38

DBMS LABORATORY

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 58

JCE, Belgaum

You might also like