34
Database Applications Lab Manual Department of ISE Page 1 BANGALORE INSTITUTE OF TECHNOLOGY K.R. ROAD, V.V PURAM, BANGALORE – 560 004 DEPARTMENT OF INFORMATION SCIENCE AND ENGINEERING (AFFILIATED TO VTU, BELGUAM) CODE: 10CSL57 DATABASE APPLICATIONS LABORATORY MANUAL FOR V SEMESTER CSE/ISE AS PRESCRIBED BY VTU Academic year 2012-2013 Prepared By: Prof. Anupama K. C, M.Tech Assistant Professor Dept of ISE, BIT

Dbms Lab Manual 10CSL58

Embed Size (px)

DESCRIPTION

DBMS lab manual for 5th sem IS/CS

Citation preview

Page 1: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 1

BANGALORE INSTITUTE OF TECHNOLOGY K.R. ROAD, V.V PURAM, BANGALORE – 560 004

DEPARTMENT OF

INFORMATION SCIENCE AND ENGINEERING

(AFFILIATED TO VTU, BELGUAM)

CODE: 10CSL57

DATABASE APPLICATIONS LABORATORY MANUAL

FOR V SEMESTER CSE/ISE AS PRESCRIBED BY VTU

Academic year 2012-2013

Prepared By:

Prof. Anupama K. C, M.Tech Assistant Professor

Dept of ISE, BIT

Page 2: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 2

Subject Code: 10CSL57 I.A. Marks : 25 Hours/Week : 03 Exam Hours: 03 Total Hours : 42 Exam Marks: 50 I . 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.

1. Find the names of all Juniors (level = 'JR') who are enrolled in a class taught by Prof. Harshith. 2. Find the names of all classes that either meet in room R128 or have five or more Students

enrolled. 3. Find the names of all students who are enrolled in two classes that meet at the same time. 4. Find the names of faculty members who teach in every room in which some class is taught. 5. Find the names of faculty members for whom the combined enrollment of the courses that they

teach is less than five. (Page 4 – 9)

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

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

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

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

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

5. Find the names of pilots certified for some Boeing aircraft. 6. Find the aids of all aircraft that can be used on routes from Bengaluru to New Delhi.

(Page 10 – 16)

Page 3: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 3

III. 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. 6. (vii) Create suitable front end for querying and displaying the results. (Page 17 – 22)

IV. 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. 7. (vii) Create suitable front end for querying and displaying the results. (Page 22 – 28)

V. 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. (Page 29 – 34)

Page 4: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 4

Instructions:

1. The exercises are to be solved in an RDBMS environment like Oracle or DB2. 2. Suitable tuples have to be entered so that queries are executed correctly. 3. Front end may be created using either VB or VAJ or any other similar tool. 4. The student need not create the front end in the examination. The results of the queries may be displayed directly. 5. Relevant queries other than the ones listed along with the exercises may also be asked in the examination. 6. Questions must be asked based on lots.

STUDENT-FACULTY DATABASE I . 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.

1. Find the names of all Juniors (level = 'JR') who are enrolled in a class taught by Prof. Harshith. 2. Find the names of all classes that either meet in room R128 or have five or more Students

enrolled. 3. Find the names of all students who are enrolled in two classes that meet at the same time. 4. Find the names of faculty members who teach in every room in which some class is taught. 5. Find the names of faculty members for whom the combined enrollment of the courses that they

teach is less than five.

Page 5: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 5

Creating tables: CREATE TABLE STUDENT( SNUM INT PRIMARY KEY, NAME VARCHAR(10), MAJOR VARCHAR(10), LEVELS VARCHAR(5), AGE INT ); CREATE TABLE FACULTY ( FID VARCHAR(4) PRIMARY KEY, FNAME VARCHAR(10), DEPT_ID INT ); CREATE TABLE CLASS ( CNAME VARCHAR(10) PRIMARY KEY, MEETS_AT VARCHAR(10), ROOM VARCHAR(10), FID VARCHAR(4), FOREIGN KEY(FID) REFERENCES FACULTY(FID) ); CREATE TABLE ENROLLED ( SNUM INT, CNAME VARCHAR(10) REFERENCES CLASS(CNAME), FOREIGN KEY(SNUM) REFERENCES STUDENT(SNUM) ); Inserting values: INSERT INTO STUDENT VALUES (1, 'BLAKE', 'CS', 'JR', 20); INSERT INTO STUDENT VALUES (2, 'JIM', 'EC', 'SR', 19); INSERT INTO STUDENT VALUES (3, 'JOHN', 'EC', 'SR', 21); INSERT INTO STUDENT VALUES (4, 'CHRIS', 'EE', 'JR', 20); INSERT INTO STUDENT VALUES (5, 'JAKE', 'ME', 'SR', 20);

SNUM NAME MAJOR LEVEL AGE 1 BLAKE CS JR 20 2 JIM EC SR 19 3 JOHN EC SR 21 4 CHRIS EE JR 20 5 JAKE ME SR 20

Page 6: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 6

INSERT INTO FACULTY VALUES (1, 'HARSHITH', 10); INSERT INTO FACULTY VALUES (2, 'KAMBER', 20); INSERT INTO FACULTY VALUES (3, 'NAVATHE', 30);

FID FNAME DEPT_ID 1 HARSHITH 10 2 KAMBER 20 3 NAVATHE 30

INSERT INTO CLASS VALUES ('2SEM', '10AM', '401', 1); INSERT INTO CLASS VALUES ('3SEM', '12PM', '128', 2); INSERT INTO CLASS VALUES ('4SEM', '12PM', '601', 3); INSERT INTO CLASS VALUES ('6SEM', '2PM', '128', 1); INSERT INTO CLASS VALUES ('5SEM', '8AM', '401', 1); INSERT INTO CLASS VALUES ('7SEM', '9AM', '601', 1);

CNAME MEETS AT ROOM FID 2SEM 10AM 401 1 3SEM 12PM 128 2 4SEM 12PM 601 3 6SEM 2PM 128 1 5SEM 8AM 401 1 7SEM 9AM 601 1

Page 7: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 7

INSERT INTO ENROLLED VALUES (1, '4SEM'); INSERT INTO ENROLLED VALUES (2, '6SEM'); INSERT INTO ENROLLED VALUES (3, '3SEM'); INSERT INTO ENROLLED VALUES (4, '2SEM'); INSERT INTO ENROLLED VALUES (5, '5SEM'); INSERT INTO ENROLLED VALUES (2, '4SEM'); INSERT INTO ENROLLED VALUES (3, '4SEM'); INSERT INTO ENROLLED VALUES (1, '3SEM'); INSERT INTO ENROLLED VALUES (1, '2SEM'); INSERT INTO ENROLLED VALUES (4, '6SEM'); INSERT INTO ENROLLED VALUES (5, '3SEM'); INSERT INTO ENROLLED VALUES (1, '5SEM'); INSERT INTO ENROLLED VALUES (5, '4SEM'); INSERT INTO ENROLLED VALUES (4, '4SEM');

SNUM CNAME

1 4SEM 2 6 SEM 3 3 SEM 4 2 SEM 5 5 SEM 2 4 SEM 3 4 SEM 1 3 SEM 1 2 SEM 4 6 SEM 5 3 SEM 1 5 SEM 5 4 SEM 4 4 SEM

Page 8: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 8

Queries: 1. Find the names of all Juniors (level = JR) who are enrolled in a class taught by Prof. HARSHITH SELECT DISTINCT S.NAME FROM STUDENT S, CLASS C, ENROLLED E, FACULTY F WHERE S.SNUM = E.SNUM AND E.CNAME = C.CNAME AND C.FID = F.FID AND F.FNAME = 'HARSHITH' AND S.LEVELS = 'JR';

NAME CHRIS BLAKE

2. Find the names of all classes that either meet in room R128 or have five or more Students enrolled. SELECT C.CNAME FROM CLASS C WHERE C.ROOM = 128 OR C.CNAME IN (SELECT E.CNAME FROM ENROLLED E GROUP BY E.CNAME HAVING COUNT (*) > 4);

CNAME 3SEM 4SEM 6SEM

3. Find the names of all students who are enrolled in two classes that meet at the same time. SELECT DISTINCT S.NAME FROM STUDENT1 S WHERE S.SNUM IN (SELECT E1.SNUM FROM ENROLLED E1, ENROLLED E2, CLASS C1, CLASS C2 WHERE E1.SNUM = E2.SNUM AND E1.CNAME != E2.CNAME AND E1.CNAME = C1.CNAME AND E2.CNAME = C2.CNAME AND C1.MEETS_AT = C2.MEETS_AT);

NAME JAKE JOHN

BLAKE

Page 9: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 9

4. Find the names of faculty members who teach in every room in which some class is taught. SELECT DISTINCT F.FNAME , FID FROM FACULTY F WHERE NOT EXISTS ( ( SELECT DISTINCT C.ROOM FROM CLASS C ) MINUS ( SELECT DISTINCT C1.ROOM FROM CLASS C1 WHERE C1.FID = F.FID ));

FNAME FID HARSHITH 1

5. Find the names of faculty members for whom the combined enrollment of the courses that they teach is less than five. SELECT DISTINCT F.FNAME FROM FACULTY F WHERE 5 > (SELECT COUNT (E.SNUM) FROM CLASS C, ENROLLED E WHERE C.CNAME = E.CNAME AND C.FID = F.FID);

FNAME KAMBER

Page 10: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 10

AIRLINE FLIGHT INFORMATION

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

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

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

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

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

5. Find the names of pilots certified for some Boeing aircraft. 6. Find the aids of all aircraft that can be used on routes from Bengaluru to New Delhi.

Page 11: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 11

Creation of Tables

CREATE TABLE AIRCRAFT ( AID INTEGER PRIMARY KEY, ANAME VARCHAR(10), CRANGE INTEGER ); CREATE TABLE FLIGHTS ( FLNO INTEGER PRIMARY KEY REFERENCES AIRCRAFT(AID), FROMPLACE VARCHAR(10), TOPLACE VARCHAR(10), DISTANCE INTEGER, DEPARTS TIMESTAMP, ARRIVES TIMESTAMP, PRICE INTEGER ); CREATE TABLE EMPLOYEES ( EID INTEGER PRIMARY KEY, ENAME VARCHAR(10), SALARY INTEGER ); CREATE TABLE CERTIFIED ( EID REFERENCES EMPLOYEES(EID), AID REFERENCES AIRCRAFT(AID), PRIMARY KEY(EID,AID) );

Page 12: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 12

Insertion INSERT INTO AIRCRAFT VALUES(101,'747',3000); INSERT INTO AIRCRAFT VALUES(102,'BOEING',900); INSERT INTO AIRCRAFT VALUES(103,'647',800); INSERT INTO AIRCRAFT VALUES(104,'DREAMLINER',10000); INSERT INTO AIRCRAFT VALUES(105,'EURO',3500); INSERT INTO AIRCRAFT VALUES(106,'707',1500); INSERT INTO AIRCRAFT VALUES(107,'DREAMER',12000);

AIRCRAFT

AID ANAME CRUISINGRANGE 101 747 3000 102 BOEING 900 103 647 800 104 DREAMLINER 10000 105 EURO 3500 106 707 1500 107 DREAMER 12000

INSERT INTO EMPLOYEES VALUES(701,'A',50000); INSERT INTO EMPLOYEES VALUES(702,'B',100000); INSERT INTO EMPLOYEES VALUES(703,'C',150000); INSERT INTO EMPLOYEES VALUES(704,'D',90000); INSERT INTO EMPLOYEES VALUES(705,'E',40000); INSERT INTO EMPLOYEES VALUES(706,'F',60000); INSERT INTO EMPLOYEES VALUES(707,'G',70000);

EMPLOYEES

EID ENAME SALARY 701 A 50000 702 B 100000 703 C 150000 704 D 90000 705 E 40000 706 F 60000 707 G 70000

Page 13: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 13

INSERT INTO CERTIFIED VALUES(701,101); INSERT INTO CERTIFIED VALUES(701,102); INSERT INTO CERTIFIED VALUES(701,106); INSERT INTO CERTIFIED VALUES(701,105); INSERT INTO CERTIFIED VALUES(702,104); INSERT INTO CERTIFIED VALUES(703,104); INSERT INTO CERTIFIED VALUES(704,104); INSERT INTO CERTIFIED VALUES(702,107); INSERT INTO CERTIFIED VALUES(703,107); INSERT INTO CERTIFIED VALUES(704,107); INSERT INTO CERTIFIED VALUES(702,101); INSERT INTO CERTIFIED VALUES(703,105); INSERT INTO CERTIFIED VALUES(704,105); INSERT INTO CERTIFIED VALUES(705,103);

CERTIFIED

EID AID 701 101 701 102 701 106 701 105 702 104 703 104 704 104 702 107 703 107 704 107 702 101 703 105 704 105 705 103

INSERT INTO FLIGHTS VALUES(101,'BANGALORE','DEHI',2500,TIMESTAMP '2005-05-13 07:15:31',TIMESTAMP '2005-05-13 07:15:31',5000); INSERT INTO FLIGHTS VALUES(102,'BANGALORE','LUCKNOW',3000,TIMESTAMP '2005-05-13 07:15:31',TIMESTAMP '2005-05-13 07:15:31',6000); INSERT INTO FLIGHTS VALUES(103,'LUCKNOW','DEHI',500,TIMESTAMP '2005-05-13 07:15:31',TIMESTAMP '2005-05-13 07:15:31',3000); INSERT INTO FLIGHTS VALUES(107,'BANGALORE','FRANKFURT',8000,TIMESTAMP '2005-05-13 07:15:31',TIMESTAMP '2005-05-13 07:15:31',60000); INSERT INTO FLIGHTS VALUES(104,'BANGALORE','FRANKFURT',8500,TIMESTAMP '2005-05-13 07:15:31',TIMESTAMP '2005-05-13 07:15:31',75000); INSERT INTO FLIGHTS VALUES(105,'KOLKATA','DEHI',3400,TIMESTAMP '2005-05-13 07:15:31',TIMESTAMP '2005-05-13 07:15:31',7000);

Page 14: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 14

Queries i. Find the names of aircraft such that all pilots certified to operate them have salaries more than Rs.80, 000. SELECT DISTINCT A.ANAME FROM AIRCRAFT A WHERE A.AID IN (SELECT C.AID FROM CERTIFIED C, EMPLOYEES E WHERE C.EID = E.EID AND NOT EXISTS (SELECT * FROM EMPLOYEES E1 WHERE E1.EID = E.EID AND E1.SALARY < 80000)); OUTPUT:

ANAME

DREAMLINER BOEING

DREAMER 747

FLNO FROMPLACE TOPLACE DISTANCE DEPARTS ARRIVES PRICE

101 BANGALORE DELHI 2500 13-MAY-05 07.15.31.000000 AM

13-MAY-05 07.15.31.000000 AM 5000

102 BANGALORE LUCKNOW 3000 13-MAY-05 07.15.31.000000 AM

13-MAY-05 07.15.31.000000 AM 6000

103 LUCKNOW DELHI 500 13-MAY-05 07.15.31.000000 AM

13-MAY-05 07.15.31.000000 AM 3000

104 BANGALORE FRANKFURT 8500 13-MAY-05 07.15.31.000000 AM

13-MAY-05 07.15.31.000000 AM 75000

105 KOLKATA DELHI 3400 13-MAY-05 07.15.31.000000 AM

13-MAY-05 07.15.31.000000 AM 7000

107 BANGALORE FRANKFURT 8000 13-MAY-05 07.15.31.000000 AM

13-MAY-05 07.15.31.000000 AM 60000

Page 15: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 15

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. SELECT C.EID, MAX (A.CRANGE) FROM CERTIFIED C, AIRCRAFT A WHERE C.AID = A.AID GROUP BY C.EID HAVING COUNT (*) >= 3; OUTPUT:

ANAME MAX(A.CRANGE)

701 3500 iii. Find the names of pilots whose salary is less than the price of the cheapest route from Bengaluru to Frankfurt. SELECT DISTINCT ENAME FROM EMPLOYEES E WHERE E.SALARY < ( SELECT MIN (F.PRICE) FROM FLIGHTS F WHERE F.fromplace ='Bangalore' AND F.TOplace = 'Frankfurt'); OUTPUT:

ENAME A E

iv. For all aircraft with cruisingrange over 1000 Kms, Find the name of the aircraft and the average salary of all pilots certified for this aircraft. SELECT NAME, AVGSALARY FROM (SELECT A.AID, A.ANAME AS NAME, AVG(E.SALARY) AS AVGSALARY FROM EMPLOYEES E, CERTIFIED C, AIRCRAFT A WHERE A.AID=C.AID and E.EID=C.EID and A.CRANGE>1000 GROUP BY A.AID, A.ANAME); OUTPUT:

NAME AVGSALARY 747 75000

DREAMLINER 113333.333 DREAMER 113333.333

707 50000 BOEING 96666.6667

Page 16: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 16

v. Find the names of pilots certified for Boeing aircraft. SELECT DISTINCT E.ENAME FROM EMPLOYEES E, CERTIFIED C, AIRCRAFT A WHERE E.EID = C.EID AND C.AID = A.AID AND A.ANAME = 'Boeing'; OUTPUT:

ENAME D A C

vi. Find the aids of all aircraft that can be used on routes from Bengaluru to New Delhi. SELECT A.AID FROM AIRCRAFT A WHERE A.CRANGE >= (SELECT MIN (F.DISTANCE) FROM FLIGHTS F WHERE F.FROMplace = 'Bangalore' AND F.TOplace = 'Dehi'); OUTPUT:

AID 101 104 105 107

Page 17: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 17

STUDENT ENROLLMENT DATABASE III. 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.

Page 18: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 18

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

COURSE INTEGER PRIMARY KEY, CNAME VARCHAR(10) NOT NULL, DEPT VARCHAR(10) NOT NULL

); SQL>CREATE TABLE ENROLL ( REG_NO VARCHAR(10) NOT NULL,

COURSE INTEGER NOT NULL, SEM INTEGER NOT NULL,

MARKS INTEGER NOT NULL, PRIMARY KEY (REG_NO,COURSE,SEM), FOREIGN KEY (REG_NO) REFERENCES STUDENT (REGNO) , FOREIGN KEY (COURSE) REFERENCES COURSE (COURSE) ); SQL>CREATE TABLE TEXT ( BOOKISBN INTEGER PRIMARY KEY, BOOKTITLE VARCHAR(20) NOT NULL, PUBLISHER VARCHAR(12) NOT NULL, AUTHOR VARCHAR(15) NOT NULL );

SQL>CREATE TABLE BOOK_ADOPTION (

COURSE NUMBER(4) NOT NULL, SEM NUMBER(4) NOT NULL,

BOOK_ISBN INTEGER NOT NULL, PRIMARY KEY (COURSE,SEM,BOOK_ISBN), FOREIGN KEY (COURSE) REFERENCES COURSE(COURSE) , FOREIGN KEY (BOOK_ISBN) REFERENCES TEXT(BOOKISBN) );

Page 19: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 19

INSERTING TUPLES SQL>INSERT INTO STUDENT VALUES('1BI10IS010', 'ANU','DBMS', '15-JAN-92'); SQL>INSERT INTO STUDENT VALUES('1BI10CS010','MANNU','OS ','15-FEB-92'); SQL>INSERT INTO STUDENT VALUES('1BI10TC010','BHANU','SSDT','15-MAR-92'); SQL>INSERT INTO STUDENT VALUES('1BI10EE010','SONU','ELECTRICAL','15-SEP-92'); SQL>INSERT INTO STUDENT VALUES('1BI10EC010','DHANU','ELECTRONICS','15-DEC-92'); SQL> SELECT * FROM STUDENT;

SQL>INSERT INTO COURSE VALUES('1','BE','IS'); SQL>INSERT INTO COURSE VALUES('2','MCA','CS'); SQL>INSERT INTO COURSE VALUES('3','M.TECH','TC'); SQL>INSERT INTO COURSE VALUES('4','BSC','EE'); SQL>INSERT INTO COURSE VALUES('5','ME',EC'); SQL>SELECT * FROM COURSE;

SQL>INSERT INTO ENROLL VALUES('1BI10IS010',1,5,98); SQL>INSERT INTO ENROLL VALUES('1BI10CS010',2,3,88); SQL.INSERT INTO ENROLL VALUES('1BI10TC010',3,5,86); SQL>INSERT INTO ENROLL VALUES('1BI10EE010',4,5,90); SQL>INSERT INTO ENROLL VALUES('1BI10EC010',5,4,83); SQL>SELECT * FROM ENROLL;

REGNO NAME MAJOR BDATE 1BI10IS010 ANU DBMS 15-JAN-92 1BI10CS010 MANNU OS 15-FEB-92 1BI10TC010 BHANU SSDT 15-MAR-92 1BI10EE010 SONU ELECTRICAL 15-SEP-92 1BI10EC010 DHANU ELECTRONICS 15-DEC-92

COURSE CNAME DEPT 1 BE IS 2 MCA CS 3 M.TECH TC 4 BSC EE 5 ME EC

REG_NO COURSE SEM MARKS 1BI10IS010 1 5 98 1BI10CS010 2 3 88 1BI10TC010 3 5 86 1BI10EE010 4 5 90 1BI10EC010 5 4 83

Page 20: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 20

SQL>INSERT INTO TEXT VALUES('1','DATABASE','MEGRAW','FOROUZAN'); SQL>INSERT INTO TEXT VALUES('2',' COMPILER','PEARSON','NAVATHE'); SQL>INSERT INTO TEXT VALUES('3','LEX&YACC','PEARSON','PADMA'); SQL>INSERT INTO TEXT VALUES('4','S/W ENGG','PEARSON','SUMITHA'); SQL>INSERT INTO TEXT VALUES('5','ELECT CKTS','SUBHAS','FOROUZAN'); SQL>INSERT INTO TEXT VALUES('6','PWR GEN','TMH','SURYA'); SQL>INSERT INTO TEXT VALUES('7','PWR ELEC','TMH','SHIVA'); SQL>INSERT INTO TEXT VALUES('8','OS','MEGRAW','SUMITHA') SQL>SELECT * FROM TEXT;

BOOKISBN BOOKTITLE PUBLISHER AUTHOR

1 DATABASE MEGRAW FOROUZAN 2 COMPILER PEARSON NAVATHE 3 LEX&YACC PEARSON PADMA 4 S/W ENGG PEARSON SUMITHA 5 ELECT CKTS SUBHAS FOROUZAN 6 PWR GEN TMH SURYA 7 PWR ELEC TMH SHIVA 8 OS MEGRAW SUMITHA

SQL>INSERT INTO BOOKADOPTION VALUES(1,5,1); SQL>INSERT INTO BOOKADOPTION VALUES(1,5,8); SQL>INSERT INTO BOOKADOPTION VALUES(2,3,2); SQL>INSERT INTO BOOKADOPTION VALUES(2,3,3); SQL>INSERT INTO BOOKADOPTION VALUES(2,3,4); SQL>INSERT INTO BOOKADOPTION VALUES(3,5,5); SQL>INSERT INTO BOOKADOPTION VALUES(4,5,6); SQL>INSERT INTO BOOKADOPTION VALUES(5,4,7); SQL>SELECT * FROM BOOK_ADOPTION;

COURSE SEM BOOK_ISBN 1 5 1 1 5 8 2 3 2 2 3 3 2 3 4 3 5 5 4 5 6 5 4 7

Page 21: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 21

QUERIES 1. Demonstrate how you add a new text book to the database and make this book be adopted by some department SQL>INSERT INTO TEXT VALUES('1111','MULTIMEDIA','PEARSON','NAVATHE'); SQL>INSERT INTO BOOK_ADOPTION VALUES(5,6,1111); SQL> SELECT * FROM TEXT;

BOOKISBN BOOKTITLE PUBLISHER AUTHOR

1 DATABASE MEGRAW FOROUZAN 2 COMPILER PEARSON NAVATHE 3 LEX&YACC PEARSON PADMA 4 S/W ENGG PEARSON SUMITHA 5 ELECT CKTS SUBHAS FOROUZAN 6 PWR GEN TMH SURYA 7 PWR ELEC TMH SHIVA 8 OS MEGRAW SUMITHA

1111 MULTIMEDIA PEARSON NAVATHE SQL> SELECT * FROM BOOK_ADOPTION; COURSE SEM BOOK_ISBN

1 5 1 1 5 8 2 3 2 2 3 3 2 3 4 3 5 5 4 5 6 5 4 7 5 6 1111

EXPLANATION INSERT can be used to add a single tuple to a relation. Thus information about a new text book can be added to the TEXT entity using INSERT command. The new text book can be made to be adopted by some department using INSERT. The values which are added to the BOOK_ADOPTION contain COURSE and SEM of the department and semester which uses the textbook, along with the BOOK_ISBN of the textbook through which other information of the textbook can be obtained.

Page 22: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 22

2. Produce a list of textbooks in the alphabetic order for courses offered by the ‘CSE’ department that use more than two books SQL>SELECT C.COURSE, T.BOOKISBN, T.BOOKTITLE FROM COURSE C, BOOK_ADOPTION BA,TEXT T WHERE C.COURSE=BA.COURSE AND C.DEPT='CS' AND BA.BOOK_ISBN=T.BOOKISBN AND BA.COURSE IN (SELECT C.COURSE FROM COURSE C, BOOK_ADOPTION B WHERE C.COURSE=B.COURSE HAVING COUNT(*)>2 GROUP BY C.COURSE) ORDER BY T.BOOKTITLE;

COURSE BOOKISBN BOOKTITLE 2 2 COMPILER 2 3 LEX&YACC 2 4 S/W ENGG

Page 23: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 23

BOOK DEALER DATABASE

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

Page 24: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 24

Creating tables: AUTHOR CREATE TABLE AUTHOR ( AID INT PRIMARY KEY, NAME VARCHAR(10), CITY VARCHAR(10), COUNTRY VARCHAR(10) ); PUBLISHER CREATE TABLE PUBLISHER ( PID INT PRIMARY KEY, NAME VARCHAR(10), CITY VARCHAR(10), COUNTRY VARCHAR(10) ); CATEGORY CREATE TABLE CATEGORY ( CID INT PRIMARY KEY, DESCRIPTION VARCHAR(15) ); CATALOG CREATE TABLE CATALOG ( BOOKID INT PRIMARY KEY, TITLE VARCHAR(10) , AID INT, CID INT, PID INT, YEAR INT, PRICE INT, FOREIGN KEY(AID) REFERENCES AUTHOR(AID), FOREIGN KEY(CID) REFERENCES CATEGORY(CID), FOREIGN KEY(PID) REFERENCES PUBLISHER(PID) );

Page 25: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 25

ORDER-DETAILS CREATE TABLE ODETAILSETAILS ( ONO INT PRIMARY KEY, BID INT REFERENCES CATALOG(BID), QTY INT ); Inserting values: AUTHOR INSERT INTO AUTHOR VALUES(10,'PREDDY','BANGLORE','INDIA'); INSERT INTO AUTHOR VALUES(20,'NAVATHE','BANGLORE','INDIA'); INSERT INTO AUTHOR VALUES(30,'SOMERVILLE','PUNE','INDIA'); INSERT INTO AUTHOR VALUES(40,'BARRYBREY','NEWYORK','USA'); INSERT INTO AUTHOR VALUES(50,'FOROUZEN','MADRAS','INDIA');

AID NAME CITY COUNTRY 10 PREDDY BANGLORE INDIA 20 NAVATHE BANGLORE INDIA 30 SOMERVILLE PUNE INDIA 40 BARYBREY NEWYORK USA 50 FOROUZAN MADRAS INDIA

PUBLISHER INSERT INTO PUBLISHER VALUES(100,'NANDI','BANGLORE','INDIA'); INSERT INTO PUBLISHER VALUES(200,'CHANDRA','BANGLORE','INDIA'); INSERT INTO PUBLISHER VALUES(300,'PEARSON','PUNE','INDIA'); INSERT INTO PUBLISHER VALUES(400,'MCGRAWHILL','NEWYORK','USA'); INSERT INTO PUBLISHER VALUES(500,'TATA','MADRAS','INDIA');

PID NAME CITY COUNTRY 100 NANDI BANGLORE INDIA 200 CHANDRA BANGLORE INDIA 300 PEARSON PUNE INDIA 400 MCGRAWHILL NEWYORK USA 500 TATA MADRAS INDIA

Page 26: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 26

CATEGORY INSERT INTO CATEGORY VALUES(1000,'INTRO TO C++'); INSERT INTO CATEGORY VALUES(2000,'INTRO TO DBMS'); INSERT INTO CATEGORY VALUES(3000,'INTRO TO SE'); INSERT INTO CATEGORY VALUES(4000,'INTRO TO EC'); INSERT INTO CATEGORY VALUES(5000,'INTRO TO CN');

CID DESCRIPTION 1000 INTRO TO C++ 2000 INTRO TO DBMS 3000 INTRO TO SE 4000 INTRO TO EC 5000 INTRO TO CN

CATALOG INSERT INTO CATALOG VALUES(111,'C++',10,100,1000,2011,500); INSERT INTO CATALOG VALUES(112,'DBMS',20,200,2000,2012,350); INSERT INTO CATALOG VALUES(113,'BASIC C',10,100,1000,2014,450); INSERT INTO CATALOG VALUES(114,'S/W ENGG',30,300,3000,2013,250); INSERT INTO CATALOG VALUES(115,'EC',40,400,4000,2015,200); INSERT INTO CATALOG VALUES(116,'CN',50,500,5000,2010,375);

BID TITLE AID PID CID YEAR PRICE 111 C++ 10 100 1000 2011 500 112 DBMS 20 200 2000 2012 350 113 BASIC C 10 100 1000 2014 450 114 S/W ENGG 30 300 3000 2013 250 115 EC 40 400 4000 2015 200 116 CN 50 500 5000 2010 375

ORDER-DETAILS INSERT INTO ODETAILS VALUES(101,111,200); INSERT INTO ODETAILS VALUES(102,112,300); INSERT INTO ODETAILS VALUES(103,113,400); INSERT INTO ODETAILS VALUES(104,114,250); INSERT INTO ODETAILS VALUES(105,115,300); INSERT INTO ODETAILS VALUES(106,116,500);

ONO BID QTY 101 111 200 102 112 300 103 113 400 104 114 250 105 115 300 106 116 500

Page 27: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 27

Queries:

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

SELECT A.AID,A.NAME,A.CITY,COUNT(*) AS COUNT FROM AUTHOR A,CATALOG C WHERE A.AID=C.AID AND C.YEAR>=2010 AND C.PRICE>=(SELECT AVG(PRICE) FROM CATALOG) GROUP BY (A.AID,A.NAME,A.CITY)

HAVING COUNT(*)>=2;

AID NAME CITY COUNT 10 PREDDY BANGLORE 2

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

SELECT A.NAME FROM AUTHOR A,CATALOG C,OD O WHERE A.AID=C.AID AND O.BID=C.BID AND O.QTY=(SELECT MAX(QTY) FROM OD);

NAME FOROUZAN

Page 28: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 28

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

UPDATE CATALOG SET PRICE=1.1*PRICE WHERE PID IN (SELECT PID FROM PUBLISHER WHERE NAME=’CHANDRA’);

Before updating

BID TITLE AID PID CID YEAR PRICE 111 C++ 10 100 1000 2011 500 112 DBMS 20 200 2000 2012 350 113 BASIC C 10 100 1000 2014 450 114 S/W ENGG 30 300 3000 2013 250 115 EC 40 400 4000 2015 200 116 CN 50 500 5000 2010 375

After updating

BID TITLE AID PID CID YEAR PRICE 111 C++ 10 100 1000 2011 500 112 DBMS 20 200 2000 2012 385 113 BASIC C 10 100 1000 2014 450 114 S/W ENGG 30 300 3000 2013 250 115 EC 40 400 4000 2015 200 116 CN 50 500 5000 2010 375

Page 29: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 29

BANKING DATABASE V. 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.

Page 30: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 30

Create tables: CREATE TABLE BRANCH ( BRANCH_NAME VARCHAR(25) PRIMARY KEY, BRANCH_CITY VARCHAR(25) NOT NULL, ASSETS DECIMAL(10,2) NOT NULL ); CREATE TABLE ACCOUNT ( ACCNO INT PRIMARY KEY, BRANCH_NAME VARCHAR(25) NOT NULL, BALANCE DECIMAL(10,2) NOT NULL, FOREIGN KEY(BRANCH_NAME) REFERENCES BRANCH(BRANCH_NAME) ); CREATE TABLE CUSTOMER1 ( CUSTOMER_NAME VARCHAR(25) PRIMARY KEY, CUSTOMER_STREET VARCHAR(25), CUSTOMER_CITY VARCHAR(25) ); CREATE TABLE DEPOSITOR ( CUSTOMER_NAME VARCHAR(25) NOT NULL, ACCNO INT NOT NULL, FOREIGN KEY(CUSTOMER_NAME) REFERENCES CUSTOMER1(CUSTOMER_NAME), FOREIGN KEY(ACCNO) REFERENCES ACCOUNT(ACCNO) ON DELETE CASCADE, PRIMARY KEY(CUSTOMER_NAME,ACCNO) ); CREATE TABLE LOAN ( LOAN_NUMBER INT PRIMARY KEY, BRANCH_NAME VARCHAR(25), AMOUNT DECIMAL(10,2), FOREIGN KEY(BRANCH_NAME) REFERENCES BRANCH(BRANCH_NAME) ); CREATE TABLE BORROWER ( CUSTOMER_NAME VARCHAR(10) NOT NULL, LOAN_NUMBER INT NOT NULL, FOREIGN KEY(CUSTOMER_NAME) REFERENCES CUSTOMER1(CUSTOMER_NAME), FOREIGN KEY(LOAN_NUMBER) REFERENCES LOAN(LOAN_NUMBER) PRIMARY KEY(CUSTOMER_NAME,LOAN_NUMBER) );

Page 31: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 31

Inserting values into tables: INSERT INTO BRANCH VALUES(‘JAYANAGAR’,’BANGALORE’,’15000000); INSERT INTO BRANCH VALUES(‘BASAVANAGUDI’,’BANGALORE’,’25000000); INSERT INTO BRANCH VALUES(‘NOIDA’,’DELHI’,’50000000); INSERT INTO BRANCH VALUES(‘MARINE DRIVE’,’MUMBAI’,’40000000); INSERT INTO BRANCH VALUES(‘GREEN PARK’,’DELHI’,’30000000);

BRANCH_NAME BRANCH_CITY ASSETS JAYANAGAR BANGALORE 15000000

BASAVANGUDI BANGALORE 25000000 NOIDA DELHI 50000000

MARINE DRIVE MUMBAI 40000000 GREEN PARK DELHI 30000000

INSERT INTO ACCOUNT VALUES(123,'JAYANAGAR',25000); INSERT INTO ACCOUNT VALUES(156,'JAYANAGAR',30000); INSERT INTO ACCOUNT VALUES(456,'BASAVANGUDI',15000); INSERT INTO ACCOUNT VALUES(789,'NOIDA',25000); INSERT INTO ACCOUNT VALUES(478,'MARINE DRIVE',48000); INSERT INTO ACCOUNT VALUES(189,'BASAVANGUDI',48888); INSERT INTO ACCOUNT VALUES(778,'GREEN PARK',60000);

ACCNO BRANCH_NAME BALANCE 123 JAYANAGAR 25000 156 JAYANAGAR 30000 456 BASAVANGUDI 15000 789 NOIDA 25000 478 MARINE DRIVE 48000 189 BASAVANAGUDI 48888 778 GREEN PARK 60000

Page 32: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 32

INSERT INTO CUSTOMER1 VALUES('RAMU','JAYANAGAR','BANGALORE'); INSERT INTO CUSTOMER1 VALUES('KUMAR','BASAVANGUDI','BANGALORE'); INSERT INTO CUSTOMER1 VALUES('JOHN','NOIDA','DELHI'); INSERT INTO CUSTOMER1 VALUES('MIKE','MARINE DRIVE','MUMBAI'); INSERT INTO CUSTOMER1 VALUES('SACHIN','GREEN PARK','DELHI'); CUSTOMER_NAME CUSTOMER_STREET CUSTOMER_CITY

RAMU JAYANAGAR BANGALORE KUMAR BASAVANGUDI BANGALORE

JOHN NOIDA DELHI MIKE MARINE DRIVE MUMBAI

SACHIN GREEN PARK DELHI INSERT INTO DEPOSITOR VALUES('RAMU',123); INSERT INTO DEPOSITOR VALUES('RAMU',156); INSERT INTO DEPOSITOR VALUES('RAMU',189); INSERT INTO DEPOSITOR VALUES('KUMAR',456); INSERT INTO DEPOSITOR VALUES('JOHN',789); INSERT INTO DEPOSITOR VALUES('MIKE',478); INSERT INTO DEPOSITOR VALUES('SACHIN',778);

CUSTOMER_NAME ACCNO RAMU 123 RAMU 156 RAMU 189

KUMAR 456 JOHN 789 MIKE 478

SACHIN 778 INSERT INTO LOAN VALUES(2222,'JAYANAGAR',350000); INSERT INTO LOAN VALUES(3333,'NOIDA',150000); INSERT INTO LOAN VALUES(4444,'MARINE DRIVE',15000000); INSERT INTO LOAN VALUES(4444,'MARINE DRIVE',15000000); INSERT INTO LOAN VALUES(5555,'GREEN PARK',7500000); LOAN_NUMBER BRANCH_NAME AMOUNT

1111 JAYANAGAR 250000 2222 BASAVANGUDI 350000 3333 NOIDA 150000 4444 MARINE DRIVE 1500000 5555 GREEN PARK 7500000

Page 33: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 33

INSERT INTO BORROWER TABLE: INSERT INTO BORROWER VALUES(‘JOHN’,3333); INSERT INTO BORROWER VALUES(‘KUMAR’,2222); INSERT INTO BORROWER VALUES(‘MIKE’,4444); INSERT INTO BORROWER VALUES(‘RAMU’,1111); INSERT INTO BORROWER VALUES(‘SACHIN’,5555); CUSTOMER_NAME LOAN_NUMBER

JOHN 3333 KUMAR 2222

MIKE 4444 RAMU 1111

SACHIN 5555 Queries: 1) Find all the customers who have at least two accounts at the Main branch. SELECT DISTINCT(CUSTOMER_NAME),COUNT(*) FROM ACCOUNT A,DEPOSITOR D WHERE A.ACNO=D.ACCNO AND D.ACCNO IN(SELECT ACCNO FROM ACCONT WHERE BRANCH_NAME=’JAYANAGAR’) GROUP BY D.CUSTOMER_NAME HAVING COUNT9*)>=2;

CUSTOMER_NAME COUNT(*) RAMU 2

2) Find all the customers who have an account at all the branches located in a specific city. SELECT D.CUSTOMER_NAME FROM ACCOUNT A,DEPOSITOR D,BRANCH B WHERE B.BRANCH_NAME=A.BRANCH_NAME AND A.ACCNO=D.ACCNO AND B.BRANCH_CITY=’BANGALORE’ HAVING COUNT(DISTINCT B.BRANCH_NAME) =(SELECT COUNT(BRANCH_NAME) FROM BRANCH WHERE BRANCH_CITY=’BANGALORE’) GROUP BY CUSTOMER_NAME;

CUSTOMER_NAME RAMU

Page 34: Dbms Lab Manual 10CSL58

Database Applications Lab Manual

Department of ISE Page 34

3) Demonstrate how you delete all account tuples at every branch located in a specific city. DELETE FROM ACCOUNT WHERE BRANCH_NAME IN (SELECT BRANCH_NAME FROM BRANH WHERE BRANCH_CITY=’DELHI’); Output: 2 rows deleted SELECT * FROM ACCOUNT;

ACCNO BRANCH_NAME BALANCE

123 JAYANAGAR 25000

156 JAYANAGAR 30000

456 BASAVANGUDI 15000

478 MARINE DRIVE 48000

189 BASAVANAGUDI 48888 SELECT * FROM DEPOSITOR;

CUSTOMER_NAME ACCNO

RAMU 123

RAMU 156

RAMU 189

KUMAR 456

MIKE 478