+ All Categories
Home > Documents > SQL Lab Manual Final

SQL Lab Manual Final

Date post: 21-Nov-2014
Category:
Upload: expect
View: 193 times
Download: 3 times
Share this document with a friend
Popular Tags:
57
PROBLEM 1 INSURANCE DATBASE PROBLEM STATEMENT: DATA BASE TO MANAGE THE INSURANCE OF A VEHICLE . THE RELATIONS WITH THEIR RESPECTIVE ATTRIBUTES ARE SHOWN BELOW. PERSON (DRIVER ID , NAME, ADDRESS) CAR (REGNO, MODEL, YEAR) ACCIDENT (REPORTNO,DATE,LOCATION) OWNS(DRIVERID,REGNO) PARTICIPATED(DRIVERID,REGNO,REPORTNO,DAMAGEAMOUNT) SCHEMA DESCRIPTION: PERSON CAR DRIVER_ID NAME ADDRESS PARTICIPATION DRIVER_ID REGNO REPORTNO DAMAGEAMT ACCIDENT REPORTNO DATE LOCATION OWNS REGNO MODEL YEAR 1
Transcript
Page 1: SQL Lab Manual Final

PROBLEM 1INSURANCE DATBASE

PROBLEM STATEMENT:

DATA BASE TO MANAGE THE INSURANCE OF A VEHICLE . THE RELATIONS WITH THEIR RESPECTIVE ATTRIBUTES ARE SHOWN BELOW.

PERSON (DRIVER ID, NAME, ADDRESS)CAR (REGNO, MODEL, YEAR)ACCIDENT (REPORTNO,DATE,LOCATION)OWNS(DRIVERID,REGNO)PARTICIPATED(DRIVERID,REGNO,REPORTNO,DAMAGEAMOUNT)

SCHEMA DESCRIPTION:

PERSON CAR DRIVER_ID NAME ADDRESS

PARTICIPATION DRIVER_ID REGNO REPORTNO DAMAGEAMT

ACCIDENT REPORTNO DATE LOCATION

OWNS

REGNO MODEL YEAR

DRIVER_ID REGNO

1

Page 2: SQL Lab Manual Final

The table PERSON contains the attributes namely driver_id, name & address where the primary key is driver_id.

The table CAR contains the attributes such as model, year & register number where the primary key is register number.

The table ACCIDENT contains the attributes such as report number, date &location where the primary key is report number.

The table OWNS contains the attributes driver id & regno, both the attributes are foreign key &they together constitute the composite key.

The table PARTICIPATED contains the attributes driver id, regno, report number & damage amount. Here driver id , regno , report number are foreign keys and they constitute the composite key

TABLE CREATION:

2

Page 3: SQL Lab Manual Final

CREATE TABLE PERSON ( DRIVER_ID VARCHAR (10) PRIMARY KEY, NAME VARCHAR (10) NOT NULL, ADDRESS VARCHAR (20));

CREATE TABLE CAR( REG_NO VARCHAR (10) PRIMARY KEY, MODEL VARCHAR (10) NOT NULL, YEAR NUMBER (4));

CREATE TABLE ACCIDENT( REPORT_NO NUMBER (10) PRIMARY KEY, ACC_DATE DATE, LOCATION VARCHAR (10) NOT NULL);

CREATE TABLE OWNS( DRIVER_ID VARCHAR (10), REG_NO VARCHAR (10), PRIMARY KEY(DRIVER_ID, REG_NO), FOREIGN KEY(DRIVER_ID) REFERENCES PERSON (DRIVER_ID), FOREIGN KEY (REG_NO) REFERENCES CAR (REG_NO));

CREATE TABLE PARTICIPATED (DRIVER_ID VARCHAR(10),REG_NO VARCHAR(10),REPORT_NO NUMBER(10),DAMAGE_AMOUNT NUMBER(10),PRIMARY KEY(DRIVER_ID, REG_NO, REPORT_NO),FOREIGN KEY(DRIVER_ID) REFERENCES PERSON(DRIVER_ID),FOREIGN KEY (REG_NO) REFERENCES CAR(REG_NO),FOREIGN KEY(REPORT_NO) REFERENCES ACCIDENT(REPORT_NO));

VALUE INSERTION:

3

Page 4: SQL Lab Manual Final

SQL> INSERT INTO PERSON VALUES 2 ('&DRIVER_ID','&NAME','&ADDRESS');Enter value for driver_id: 001Enter value for name: AKASHEnter value for address: SHAHAPURold 2: ('&DRIVER_ID','&NAME','&ADDRESS')new 2: ('001','AKASH','SHAHAPUR')

1 row created.

SQL> /Enter value for driver_id: 002Enter value for name: ADITIEnter value for address: RJ NAGARold 2: ('&DRIVER_ID','&NAME','&ADDRESS')new 2: ('002','ADITI','RJ NAGAR')

1 row created.

SQL> INSERT INTO CAR VALUES 2 ('&REG_NO','&MODEL','&YEAR') 3 ;Enter value for reg_no: R001Enter value for model: SKODAEnter value for year: 2002old 2: ('&REG_NO','&MODEL','&YEAR')new 2: ('R001','SKODA','2002')

1 row created.

SQL> /Enter value for reg_no: R002Enter value for model: SKODAEnter value for year: 2002old 2: ('&REG_NO','&MODEL','&YEAR')new 2: ('R002','SKODA','2002')

1 row created.

SQL> INSERT INTO ACCIDENT VALUES 2 ('&REPORT_NO','&DATE','&LOCATION');Enter value for report_no: 1Enter value for date: 1-JAN-2003

4

Page 5: SQL Lab Manual Final

Enter value for location: RJ NAGARold 2: ('&REPORT_NO','&DATE','&LOCATION')new 2: ('1','1-JAN-2003','RJ NAGAR')

1 row created.

SQL> /Enter value for report_no: 2Enter value for date: 13-FEB-2003Enter value for location: TC NAGARold 2: ('&REPORT_NO','&DATE','&LOCATION')new 2: ('2','13-FEB-2003','TC NAGAR')

1 row created.

SQL> INSERT INTO OWNS VALUES 2 ('&DRIVER_ID','&REG_NO') 3 ;Enter value for driver_id: 001Enter value for reg_no: R001old 2: ('&DRIVER_ID','&REG_NO')new 2: ('001','R001')

1 row created.

SQL> /Enter value for driver_id: 002Enter value for reg_no: R002old 2: ('&DRIVER_ID','&REG_NO')new 2: ('002','R002')

1 row created.

SQL> INSERT INTO PARTICIPATED VALUES 2 ('&DRIVER_ID','&REGNO','&REPORT_NO','&DAMAGE_AMOUNT') 3 ;Enter value for driver_id: 001Enter value for regno: R001Enter value for report_no: 12Enter value for damage_amount: 15000old 2: ('&DRIVER_ID','&REGNO','&REPORT_NO','&DAMAGE_AMOUNT')new 2: ('001','R001','12','15000')

1 row created.

5

Page 6: SQL Lab Manual Final

SQL> /Enter value for driver_id: 002Enter value for regno: R002Enter value for report_no: 1Enter value for damage_amount: 20000old 2: ('&DRIVER_ID','&REGNO','&REPORT_NO','&DAMAGE_AMOUNT')new 2: ('002','R002','1','20000')

1 row created.

SQL> SELECT * FROM PERSON;

DRIVER_ID NAME ADDRESS------------------------------------------------------------------------------------------------------ 001 AKASH SHAHAPUR 002 ADITI RJ NAGAR 003 MOHAN RC NAGAR 004 AKSAY UDYAMBAG 005 ROHAN CHANNAMMA NAGAR 006 RAKES AUTO NAGAR 007 SACHIN GOA WAYS7 rows selected.

SQL> SELECT * FROM CAR;

REG_NO MODEL YEAR----------------------------------------------R001 SKODA 2002R002 SKODA 2002R003 BMW 2003R004 MARUTI 2004R005 HYUNDAI 2004R006 TOYOTA 2005R007 TOYOTA 2006

7 rows selected.

SQL> SELECT * FROM ACCIDENT;

6

Page 7: SQL Lab Manual Final

REPORT_NO ACC_DATE LOCATION-------------------------------------------------------------------------------------------- 1 01-JAN-03 RJ NAGAR 2 13-FEB-03 TC NAGAR 12 25-JUL-05 JAYA NAGAR 3 15-AUG-04 PEENYA 4 04-MAR-06 SHAHAPUR 6 05-MAR-04 AUTO NAGAR 7 02-JAN-02 JP NAGAR 9 13-MAR-02 JK NAGAR

8 rows selected.

SQL> SELECT * FROM OWNS;

DRIVER_ID REG_NO----------------------------------- 001 R001 002 R002 003 R003 004 R004 005 R005 006 R006 007 R007

7 rows selected.

SQL> SELECT * FROM PARTICIPATED;

DRIVER_ID REG_NO REPORT_NO DAMAGE_AMOUNT---------- ---------- ---------- ---------------------------------------------------------------------001 R001 12 15000002 R002 1 20000003 R003 2 21000004 R004 3 10000005 R005 4 210000006 R006 7 52000007 R007 9 16000

SQL COMMANDS FOR QUERY:

7

Page 8: SQL Lab Manual Final

Query to update the damage amount for the car with a specific register number in the accident with report number 12 to 25000

SQL> UPDATE PARTICIPATED SET DAMAGE_AMOUNT=25000 WHERE REPORT_NO=12 AND REG_NO='R001';

SQL> SELECT * FROM PARTICIPATED;

DRIVER_ID REG_NO REPORT_NO DAMAGE_AMOUNT---------- ---------- ---------- ---------------------------------------------------------------------001 R001 12 25000002 R002 1 20000003 R003 2 21000004 R004 3 10000005 R005 4 210000006 R006 7 52000007 R007 9 16000

7 rows selected.

Query to find the total number of people who owned the cars that were involved in accidents in 2002.

SQL> SELECT COUNT (P.DRIVER_ID) AS TOT_NUM_PEOPLE FROM PERSON P, PARTICIPATED PA, ACCIDENT A WHERE P.DRIVER_ID=PA.DRIVER_ID AND PA.REPORT_NO=A.REPORT_NO AND ACC_DATE LIKE '%02';

TOT_NUM_PEOPLE--------------------------- 2

Query to find the number of accidents in which cars belonging to a specific model were involved.

SELECT MODEL,COUNT (P.REPORT_NO) AS NUM_OF_ACCIDENTS FROM ACCIDENT A, PARTICIPATED P, CAR CWHERE A.REPORT_NO=P.REPORT_NO AND P.REG_NO=C.REG_NO GROUP BY MODEL ;

MODEL NUM_OF_ACCIDENTS---------- -----------------------------------------------BMW 1

8

Page 9: SQL Lab Manual Final

HYUNDAI 1MARUTI 1SKODA 2TOYOTA 2

OR

1. SELECT COUNT (P.REPORT_NO) AS NUM_OF_ACCIDENTS FROM 2 ACCIDENT1 A, PARTICIPATED1 P, CAR C 3 WHERE A.REPORT_NO=P.REPORT_NO AND P.REG_NO=C.REG_NO 4 and MODEL='TOYOTA';

NUM_OF_ACCIDENTS--------------------------------- 2

PROBLEM-2

Consider the following relations for an order processing database application in a company.

CUSTOMER (Cust #: int, Cname: string, City: string)ORDER (Order #: int, Odate: date, Cust #: int, Ord-Amt: int)ORDER-ITEM (Order #: int, Item #: int, qty: int)ITEM (Item #: int, Unit Price: int)SHIPMENT (Order #: int, Warehouse #: int, Ship-Date: date)WAREHOUSE (Warehouse #: int, City: string)

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

9

Page 10: SQL Lab Manual Final

ii) Enter at least five tuples for each relation.iii) Produce a listing: CUSTNAME, NO_OF_ORDERS, AVG_ORDER_AMT, where

the middle column is the total number of orders by the customer and the last column is the average order amount for that customer.

iv) List the Order# for the orders that were shipped from all the warehouses that the company has in a specific city.

v) Demonstrate how you delete Item# 10 from the ITEM table and make that field null in the ORDER-ITEM table.

vi) Generation of suitable reports.vii) Create a suitable front end for querying and displaying the results.

I) TABLE CREATION

CREATE TABLE CUSTOMER(CUSTID INT, CNAME VARCHAR2 (10), CITY VARCHAR2 (10), PRIMARY KEY (CUSTID));

CREATE TABLE ORDERTAB(ORDNO INT, ODATE DATE, CUSTID INT, ORD_AMT INT, PRIMARY KEY (ORDNO), FOREIGN KEY (CUSTID) REFERENCES CUSTOMER (CUSTID));

CREATE TABLE ITEM(ITEMNO INT,

10

Page 11: SQL Lab Manual Final

PRICE INT, PRIMARY KEY (ITEMNO));

CREATE TABLE ORDER_ITEM(ORDNO INT, ITEMNO INT, QTY INT, PRIMARY KEY (ORDNO, ITEMNO), FOREIGN KEY (ORDNO) REFERENCES ORDERTAB (ORDNO), FOREIGN KEY (ITEMNO) REFERENCES ITEM (ITEMNO) ON DELETE CASCADE);

CREATE TABLE WAREHOUSE(WARENO INT, CITY VARCHAR2 (10), PRIMARY KEY (WARENO));

CREATE TABLE SHIPMENT(ORDNO INT, WARENO INT, SHIPDATE DATE, PRIMARY KEY (ORDNO, WARENO), FOREIGN KEY (ORDNO) REFERENCES ORDERTAB (ORDNO), FOREIGN KEY (WARENO) REFERENCES WAREHOUSE (WARENO));

II) VALUE INSERTION

CUSTOMER:SQL> INSERT INTO CUSTOMER VALUES('&CUSTID','&CNAME','&CITY');Enter value for custid: 001

Enter value for cname: MOHANEnter value for city: BGMold 1: INSERT INTO CUSTOMER VALUES('&CUSTID','&CNAME','&CITY')new 1: INSERT INTO CUSTOMER VALUES('001','MOHAN','BGM')

ORDERTAB:SQL> INSERT INTO ORDERTAB VALUES ('&ORDNO','&ODATE','&CUSTID','&ORD_AMT');Enter value for ordno: 001Enter value for odate: 12-JAN-2006

11

Page 12: SQL Lab Manual Final

Enter value for custid: 1Enter value for ord_amt: 20000old 1: INSERT INTO ORDERTAB VALUES ('&ORDNO','&ODATE','&CUSTID','&ORD_AMT')new 1: INSERT INTO ORDERTAB VALUES ('001','12-JAN-2006','1','20000')

ITEMTAB:SQL> INSERT INTO ITEM VALUES('&ITEMNO','&PRICE');Enter value for itemno: 001Enter value for price: 20000old 1: INSERT INTO ITEM VALUES('&ITEMNO','&PRICE')new 1: INSERT INTO ITEM VALUES('001','20000')1 row created.

ORDER_ITEM:SQL> INSERT INTO ORDER_ITEM VALUES('&ORDNO','&ITEMNO','&QTY');Enter value for ordno: 1Enter value for itemno: 3Enter value for qty: 6old 1: INSERT INTO ORDER_ITEM VALUES('&ORDNO','&ITEMNO','&QTY',new 1: INSERT INTO ORDER_ITEM VALUES('1','3','6',

WAREHOUSE:INSERT INTO WAREHOUSE VALUES('&WARENO','&CITY');Eg.INSERT INTO WAREHOUSE VALUES(15, 'SYD');

SHIPMENT:INSERT INTO SHIPMENT VALUES('&ORDNO','&WARENO','&SHIPDATE');Eg.INSERT INTO SHIPMENT VALUES(10000, 15, '10-JUN-2002');

SQL> SELECT * FROM CUSTOMER;

CUSTID CNAME CITY---------- ---------- ----------------------------------------------------------- 1 MOHAN BGM 2 ROHAN BLR 3 AKASH MSR 4 AKSHAY SHAHAPUR 5 ANIL MANGALORE 6 LOKESH PUTTUR

SQL>SELECT * FROM ORDERTAB;

12

Page 13: SQL Lab Manual Final

ORDNO ODATE CUSTID ORD_AMT----- --------- ---------- -------------------------------------------- 1 12-JAN-06 1 20000 2 15-FEB-06 1 350000 3 16-MAR-06 2 14500 4 23-MAY-06 2 13000 5 14-MAR-06 3 50000 6 27-JUN-07 4 26000

SQL> SELECT * FROM ITEM;

ITEMNO PRICE---------- -------------------------- 1 20000 2 150000 3 14000 4 6000 5 18000 6 31000 10 15000

SQL> SELECT * FROM ORDER_ITEM;

ORDNO ITEMNO QTY ---------- ---------- ---------- ------------------------ 1 1 5 1 3 6 2 10 9 3 4 12 4 6 15

SQL> SELECT * FROM WAREHOUSE;

WARENO CITY---------- ----------------------------------- 1 BGM 2 MSR 3 BLR 4 DWD 5 HUBLI 6 BGM

7 BGM

13

Page 14: SQL Lab Manual Final

SQL> SELECT * FROM SHIPMENT;

ORDNO WARENO SHIPDATE---------- ---------- -------------------------------- 1 1 10-JAN-08 5 5 12-JAN-08 4 6 13-JAN-08 2 7 14-JAN-08 3 3 15-JAN-08

QUERIES

III)

Produce a listing: CUSTNAME, NO_OF_ORDERS, AVG_ORDER_AMT, where the middle column is the total number of orders by the customer and the last column is the average order amount for that customer.

SQL> SELECT CNAME, COUNT(ORDNO) AS NO_OF_ORDERS,AVG(ORD_AMT) AS AVG_ORD_AMT FROM CUSTOMER C,ORDERTAB O WHERE C.CUSTID=O.CUSTID GROUP BY C.CNAME,O.CUSTID;

CNAME NO_OF_ORDERS AVG_ORD_AMT---------- ------------ --------------------------------------------------AKASH 1 50000AKSHAY 1 26000MOHAN 2 185000ROHAN 2 13750

IV)

14

Page 15: SQL Lab Manual Final

List the Order# for the orders that were shipped from all the warehouses that the company has in a specific city.

SQL> SELECT ORDNO FROM SHIPMENT WHERE WARENO IN (SELECT WARENO FROM WAREHOUSE WHERE CITY='&CITY');Enter value for city: BGMold 4: WHERE CITY='&CITY')new 4: WHERE CITY='BGM')

ORDNO----------------- 1 4 2

V)

Demonstrate the deletion of an item from ITEM table and demonstrate a metod of handling the rows in the ORDER_ITEM table that contain this particular item.

SQL> DELETE FROM ITEM WHERE ITEMNO=10;

1 row deleted.

SQL> SELECT * FROM ORDER_ITEM;

ORDNO ITEMNO QTY---------- ---------- ---------------------------------- 1 1 5 1 3 6 3 4 12 4 6 15

SQL> SELECT * FROM ITEM;

ITEMNO PRICE---------- ------------------- 1 20000

15

Page 16: SQL Lab Manual Final

2 150000 3 14000 4 6000 5 18000 6 31000

6 rows selected.We can observe that in ORDER_ITEM table the row containing ITEMNO=10 has been deleted by deleting the row containing ITEMNO=10 from ITEM table. This is constraint ON DELETE CASCADE set during the creation ORDER_ITEM of table

PROBLEM 3 CONSIDER THE FOLLOWING DATABASE OF STUDENT ENROLLEMENT IN COURSES AND BOOKS ADOPTED FOR EACH COURSE .

STUDENT (REGNO :STRING , NAME : STRING , MAJOR : STRING , BDATE : INT)COURSE (COURSE# : INT , CNAME : STRING , DEPT : STRING)ENROLL ( REGNO : STRING , COURSE#: INT , SEM : INT , MARKS : INT )BOOK_ADAPTION ( COURSE#: INT , SEM : INT , BOOK_ISBN :INT)TEXT( BOOK_ISBN : INT , BOOK-TITLE : STRING , PUBLISHER : STRING , AUTHOR : STRING).

1> CREATE THE ABOVE TABLES BY PROPERLY SPECIFYING THE PRIMARY KEYS AND THE FOREIGN KEY .

2> ENTER ATLEAST FIVE TUPLES FOR EACH RELATION . 3> DEMONSTRATE HOW YOU ADD A NEW TEXT BOOK TO THE

DATABASE AND MAKE THIS BOOK BE ADOPTED BY SOME DEPARTMENT.

4> PRODUCE A LIST OF TEXT BOOKS( INCLUDE COURSE # ,BOOK_ISBN,BOOK-TITLE) IN THE ALPHABETICAL ORDER FOR COURSES OFFERED BY THE CS DEPARTMENT THAT USE MORE THAN 2 BOOKS.

5> LIST ANY DEPARTMENT THAT HAS ALL ITS ADOPTED BOOKS PUBLISHED BY SPECIFIC PUBLISHER.

16

Page 17: SQL Lab Manual Final

6> GENERATION OF SUITABLE REPORTS.

TABLE CREATION:

CREATE TABLE STUDENT ( REGNO VARCHAR(30), NAME VARCHAR(30) NOT NULL, MAJOR VARCHAR(30) NOT NULL, BDATE DATE NOT NULL,PRIMARY KEY(REGNO));

CREATE TABLE COURSE( COURSE INTEGER, CNAME VARCHAR(30) NOT NULL, DEPT VARCHAR(30) NOT NULL,PRIMARY KEY(COURSE));

CREATE TABLE ENROLL( REG_NO VARCHAR(30) NOT NULL, COURSE INTEGER NOT NULL,

17

Page 18: SQL Lab Manual Final

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

CREATE TABLE TEXT( BOOKISBN INTEGER, BOOKTITLE VARCHAR(30) NOT NULL, PUBLISHER VARCHAR(30) NOT NULL, AUTHOR VARCHAR(30) NOT NULL,PRIMARY KEY(BOOKISBN));

CREATE TABLE BOOK_ADOPTION( COURSE INTEGER NOT NULL, SEM INTEGER NOT NULL, BOOKISBN INTEGER NOT NULL, PRIMARY KEY(COURSE,SEM), FOREIGN KEY (COURSE) REFERENCES COURSE(COURSE) ,FOREIGN KEY(BOOKISBN) REFERENCES TEXT(BOOKISBN));

SQL> INSERT INTO STUDENT VALUES('&REGNO','&NAME','&MAJOR','&BDATE');Enter value for regno: 2GIO6IS001Enter value for name: HARISHEnter value for major: ISEEnter value for bdate: 15-JAN-1983

SQL> INSERT INTO COURSE VALUES('&COURSE','&CNAME','&DEPT');Enter value for course: 1Enter value for cname: M.TECH(CSE)Enter value for dept: CSE

SQL> INSERT INTO ENROLL VALUES('&REGNO','&COURSE','&SEM','&MARKS');

18

Page 19: SQL Lab Manual Final

Enter value for regno: 2GI06CS001Enter value for course: 2Enter value for sem: 3Enter value for marks: 690

SQL> INSERT INTO TEXT VALUES('&BOOKISBN','&BOOKTITLE','&PUBLISHER','&AUTHOR');Enter value for bookisbn: 001Enter value for booktitle: DBMSEnter value for publisher: TMHEnter value for author: NAVATHE

SQL> INSERT INTO BOOK_ADOPTION VALUES('&COURSE','&SEM','&BOOKISBN');Enter value for course: 2Enter value for sem: 3Enter value for bookisbn: 1

SQL> SELECT * FROM STUDENT;

REGNO NAME MAJOR BDATE------------------------------ ------------------------------------------------------------------2GIO6IS001 HARISH ISE 15-JAN-832GI06CS001 AKASH CSE 17-FEB-842GI06CS100 SHRUTI CSE 18-MAR-852GI06EC089 AMOGH ECE 17-JUN-84

SQL> SELECT * FROM COURSE;

COURSE CNAME DEPT---------- ------------------------------ --------------------------------- 1 M.TECH(CSE) CSE 2 B.E.(CSE) CSE 3 B.E.(ISE) ISE 4 M.TECH(IT) ISE 5 B.E.(E&C) ECE 6 M.TECH(DE) ECE

SQL> SELECT * FROM TEXT;

BOOKISBN BOOKTITLE PUBLISHER AUTHOR---------- ------------------------------ ------------------------------------------------------------------ 1 DBMS TMH NAVATHE

19

Page 20: SQL Lab Manual Final

2 DMS PHI GRIMALDI 3 DS VK PUBLISHERS TENENBAUM 4 POWER ELECTRONICS PHI XYZ 5 DSP TMH ABC 6 ADA LEVITIN TMH 8 USP TMH DAS

SQL> SELECT * FROM BOOK_ADOPTION;

COURSE SEM BOOKISBN---------- ---------- ----------------------------- 2 3 1 2 4 5 2 5 8 1 3 1 3 3 1 3 4 2 3 5 6 5 3 4 6 3 5

SQL> SELECT * FROM ENROLL;

REG_NO COURSE SEM MARKS------------------------------ ---------- ---------- -----------------------2GIO6IS001 3 3 7852GI06CS001 2 3 6902GI06CS100 1 1 7802GI06EC089 5 3 756

QUERIES

(i)Demonstrate how you add a new text book to the database and make this book be adopted by some department

Query

20

Page 21: SQL Lab Manual Final

INSERT INTO TEXT VALUES (1111,’MULTIMEDIA’,’PHI’, ‘NAVATHE’);

INSERT INTO BOOK_ADOPTION VALUES(6,6,1111);

Explanation

INSERT can be used to add a single tuple to a relation. Thus a 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 contains COURSENO and SEM of the department and semester which has uses the textbook, along with the BOOK_ISBN of the textbook through which other information of the textbook can be obtained.

(ii) Produce a list of textbooks in the alphabetic order for courses offered by the ‘CS’ department that use more than two books

SQL> SELECT * FROM COURSE;

COURSE CNAME DEPT---------- ------------------------------ --------------------------------- 1 M.TECH(CSE) CSE 2 B.E.(CSE) CSE 3 B.E.(ISE) ISE 4 M.TECH(IT) ISE 5 B.E.(E&C) ECE 6 M.TECH(DE) ECE

SQL> SELECT * FROM TEXT;

BOOKISBN BOOKTITLE PUBLISHER AUTHOR---------- ------------------------------ ------------------------------------------------------------------ 1 DBMS TMH NAVATHE 2 DMS PHI GRIMALDI 3 DS VK PUBLISHERS TENENBAUM 4 POWER ELECTRONICS PHI XYZ 5 DSP TMH ABC

21

Page 22: SQL Lab Manual Final

6 ADA LEVITIN TMH 8 USP TMH DAS

SQL> SELECT * FROM BOOK_ADOPTION;

COURSE SEM BOOKISBN---------- ---------- ----------------------------- 2 3 1 2 4 5 2 5 8 1 3 1 3 3 1 3 4 2 3 5 6 5 3 4 6 3 5

SQL> SELECT C.COURSE,T.BOOKISBN,T.BOOKTITLE 2 FROM COURSE C,BOOK_ADOPTION B,TEXT T 3 WHERE C.COURSE=B.COURSE AND 4 B.BOOKISBN=T.BOOKISBN AND 5 C.COURSE IN 6 ( 7 SELECT B1.COURSE FROM BOOK_ADOPTION B1 8 GROUP BY B1.COURSE HAVING COUNT(*)>2 9 ) 10 AND C.DEPT='CSE';

COURSE BOOKISBN BOOKTITLE---------- ---------- ------------------------------ 2 1 DBMS 2 5 DSP 2 8 USP

(iii) List any department that has all its adopted books published by a specific

22

Page 23: SQL Lab Manual Final

Publisher.

SQL> SELECT * FROM TEXT;

BOOKISBN BOOKTITLE PUBLISHER AUTHOR---------- ------------------------------ ------------------------------------------------------------------ 1 DBMS TMH NAVATHE 2 DMS PHI GRIMALDI 3 DS VK PUBLISHERS TENENBAUM 4 POWER ELECTRONICS PHI XYZ 5 DSP TMH ABC 6 ADA LEVITIN TMH 8 USP TMH DAS

SQL> SELECT * FROM BOOK_ADOPTION;

COURSE SEM BOOKISBN---------- ---------- ----------------------------- 2 3 1 2 4 5 2 5 8 1 3 1 3 3 1 3 4 2 3 5 6 5 3 4 6 3 5

SQL> SELECT * FROM COURSE;

COURSE CNAME DEPT---------- ------------------------------ --------------------------------- 1 M.TECH(CSE) CSE 2 B.E.(CSE) CSE 3 B.E.(ISE) ISE 4 M.TECH(IT) ISE 5 B.E.(E&C) ECE 6 M.TECH(DE) ECE

23

Page 24: SQL Lab Manual Final

Query

SQL> SELECT DISTINCT C.DEPT, T.PUBLISHER 2 FROM COURSE C,TEXT T, BOOK_ADOPTION BA 3 WHERE C.COURSE = BA.COURSE AND 4 T.BOOKISBN = BA.BOOKISBN AND 5 T.PUBLISHER = '&PUBLISHER' AND 6 T.PUBLISHER = ALL 7 ( 8 SELECT T1.PUBLISHER 9 FROM COURSE C1, BOOK_ADOPTION BA1, TEXT T1 10 WHERE BA1.BOOKISBN=T1.BOOKISBN AND 11 BA1.COURSE=C1.COURSE 12 AND C.DEPT=C1.DEPT 13 ) 14 ;

DEPT PUBLISHER------------------------------ ------------------------------CSE TMH

OR

SQL> SELECT DEPT FROM COURSE C 2 WHERE NOT EXISTS 3 ( 4 SELECT BOOKISBN FROM TEXT 5 WHERE PUBLISHER='&PUBLISHER' 6 MINUS 7 SELECT BOOKISBN 8 FROM BOOK_ADOPTION B 9 WHERE C.COURSE=B.COURSE 10 ) 11 ;Enter value for publisher: TMHold 5: WHERE PUBLISHER='&PUBLISHER'new 5: WHERE PUBLISHER='TMH'

DEPT-----------CSE

Explanation

24

Page 25: SQL Lab Manual Final

The nested query selects publishers from relations COURSE, BOOK_ADOPTION and TEST using the join conditions BA1.BOOK_ISBN=T1.BOOK_ISBN which relates book adoption to the text entity,BA1.COURSENO=C1.COURSENO which relates BOOK_ADOPTION and COURSE entities. The keyword ALL returns true if T.PUBLISHER value is equal to all the results of the nested query. The department name and the publisher whose books have been adopted by the department are projected from the entities mentioned in the FROM clause which are joined using the join conditions given in the WHERE clause.

PROGRAM -4

Consider the following relations for the details 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)

viii) Create the above tables by properly specifying the primary keys and the foreign keys.ix) Enter at least five tuples for each relation.x) 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.

xi) Find the author of the book which has maximum sales.xii) Demonstrate how you increase the price of books published by a specific publisher by

10%.xiii) Generation of suitable reports.xiv) Create a suitable front end for querying and displaying the results.

25

Page 26: SQL Lab Manual Final

SCHEMA DESCRIPTION

The following schema describes a database maintained by a Book-Dealer. It describes the following relations: Author, Publisher, Catalog, Category, Order-Details.

Author:

This relation contains necessary information about the authors. It keeps track of each author’s I.D.(AID), his/her name (ANAME) , the city (ACITY) in which he/she lives, and the country (ACOUNTRY) to which he/she belongs.

ATTRIBUTE TYPEAUTHOR_ID INTEGER

ANAME VARCHAR (10) {String}ACITY VARCHAR (10) {String}

ACOUNTRY VARCHAR(10) {String}

Primary Key : AID (Author_I.D)Constraints : None

Publisher:

This relation maintains all the details of the publisher. It contains the publisher-id (PID), name of the publisher (PNAME), the city in which the publisher lives (PCITY) and the country in which the publisher lives (PCOUNTRY).

ATTRIBUTE TYPEPID INTEGER

PNAME VARCHAR (10) {String}PCITY VARCHAR (10) {String}

PCOUNTRY VARCHAR (10) {String}

26

Page 27: SQL Lab Manual Final

Primary Key : PID (Publisher’s I.D)Constraints : None

Category:

This relation category contains the category-i.d (CID) along with the description for all the books (DESCR).

ATTRIBUTE TYPECID INTEGER

DESCR VARCHAR (10) {String}

Primary Key : CID (Category I.D)Constraints : None.

Catalog :

This relation contains information about all the books i.e. book-id (BID), title (BTITLE), along with the details of the author of the respective books i.e. author-Id (AID), publisher (PID) and other details of the books such as category-id (CID), ,price (PRICE), and year (YEAR).

ATTRIBUTE TYPEBID INTEGER

BTITLE VARCHAR (10) {String}AID INTEGERPID INTEGERCID INTEGER

PRICE INTEGERYEAR INTEGER

Primary Keys : BID (Book I.D.) Constraints: The Author I.d (AID) of this relation references the Author-i.d (AID) of the Author relation. Similarly the Publisher id (PID) of this relation references the Publisher id (PID) of the relation Publisher. Also the Category- i.d (CID) of this relation references the Category-i.d (CID) of the relation Category.

Order-Details :This relation contains information about all the orders that have been placed i.e. Order-No (ONO), book-id (BID) and the quantity (QTY).

ATTRIBUTE TYPE ONO INTEGER

27

Page 28: SQL Lab Manual Final

BID INTEGERQTY INTEGER

Primary Key : ONO (Order No.), BID (Book id.)Constraints : The Book-id (BID) of this relation references the Book-id (BID) of the Catalog relation.

TABLE CREATION:

1. create table author( author_id int , author_name varchar(30), city varchar(15), country varchar(15),primary key(author_id));Table created.

2. create table publisher( publisher_id int,publisher_name varchar(30),city varchar(15),country varchar(15),primary key(publisher_id));Table created.

3. create table category(category_id int,description varchar(30),primary key(category_id));

Table created.

4. create table catalog( book_id int primary key, tittle varchar(30),author_id int,publisher_id int,category_id int, year int,price int, foreign key(author_id) references author(author_id),

28

Page 29: SQL Lab Manual Final

foreign key(publisher_id) references publisher(publisher_id), foreign key(category_id) references category(category_id));

Table created.

5. create table order_details( order_no int primary key, book_id int, quantity int, foreign key(book_id) references catalog(book_id));Table created.

INSERTION OF VALUES INTO THE TABLES:

INSERT INTO AUTHOR VALUES('&author_id','&author_name','&city','&country');INSERT INTO CATEGORY VALUES('&CATEGORY','&DESCRIPTION');INSERT INTO CATALOG VALUES ('&book_id','&tittle','&author_id','&publisher_id','&category_id','&year','&price');INSERT INTO ORDER_DETAILS VALUES ('&ORDER_NO','&BOOK_ID','&QUANTITY');

SQL> SELECT * FROM AUTHOR;

AUTHOR_ID AUTHOR_NAME CITY COUNTRY---------- --------------- --------------- ------------------------------------------------------ 1 RAMESH BGM INDIA 2 DSC BLR INDIA 3 GRIMALDI SYDNEY AUSTRALIA 4 BECK NEW YORK USA 5 NARENDRA CHENNAI INDIA

SQL> SELECT * FROM PUBLISHER;

PUBLISHER_ID PUBLISHER_NAME CITY COUNTRY------------ --------------- --------------- -------------------------------------------------------- 100 SKY PUBLISHERS BLR INDIA 101 PRINT PUBLISHER BGM INDIA 102 PEARSON DHARWAD INDIA 103 ABC PUBLISHERS MADRAS INDIA 104 XYZ PUBLISHERS DELHI INDIA

29

Page 30: SQL Lab Manual Final

SQL> SELECT * FROM CATEGORY;

CATEGORY_ID DESCRIPTION----------- ----------------------------------------- 200 ISE 201 CSE 202 EC 203 MECHANICAL

204 IP

SQL> SELECT * FROM CATALOG;

BOOK_ID TITTLE AUTHOR_ID PUBLISHER_ID CATEGORY_ID YEAR PRICE------------------------------------------------------------------------------------------------------------------------ 501 CNE 1 100 200 2000 450 502 DATA STRUCTURE 2 101 200

2001 520 503 DBMS 1 102 203 2001 500 504 ADA 1 104 204 2002 500 505 LOGIC DESIGN 2 104 203 2002 780 506 FAFL 5 103 201 2005 530 507 CO 4 102 202 2003 150 508 SS 5 104 201 2005

450

Note: Average Price of the Books= 4857 rows selected.

SQL> SELECT * FROM ORDER_DETAILS;

ORDER_NO BOOK_ID QUANTITYx---------- ---------- ------------------------------------------- 1 501 5

30

Page 31: SQL Lab Manual Final

2 502 5 3 502 10 4 503 10 5 501 8 6 504 12 7 501 5

7 rows selected.

ALGORITHM FOR QUERIES

QUERY III

This query gives 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 COMMAND

SELECT DISTINCT A.AUTHOR_ID,A.AUTHOR_NAME,A.CITY,A.COUNTRY

FROM AUTHOR A,CATALOG C

WHERE A.AUTHOR_ID=C.AUTHOR_ID AND

A.AUTHOR_ID IN

(

SELECT AUTHOR_ID FROM CATALOG

WHERE YEAR>2000 AND

(PRICE>(SELECT AVG(PRICE) FROM CATALOG))

HAVING COUNT(BOOK_ID)>=2 GROUP BY AUTHOR_ID

);

Result:

AUTHOR_ID AUTHOR_NAME CITY COUNTRY

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

1 RAMESH BGM INDIA

2 DSC BLR INDIA

Note: Price of book_id 508 is Rs.450, which is less than Average Price of books

Rs.485. So Auhtor_id 5 is not selected in the result of the query even though the

author has 2 books in the catalog.

31

Page 32: SQL Lab Manual Final

ALGORITHM

Step1: select average price of all books from catalog relation.

Step2: select author id from catalog whose book(s) price is greater than the price retrieved from step1.

Step3: select author id,author name,city and count of books from author,

catalog and rename them as AI,NAM,city,CNT respectively where author id

in author relation is equal to author id in catalog relation and the year of

publication is after 2000 and that author id exists in the set retrieved from

step2 and group the result based on author id ,author name and city having

the count of books greater than or equal to 2.

QUERY IV

This query demonstrates how to retrieve the author of the book

which has maximum sales.

SQL COMMAND

select distinct author_name

from author a, catalog c,order_details odm

where a.author_id=c.author_id and odm.book_id=c.book_id

and exists ( select od.book_id,sum(od.quantity)

from order_details od

where od.book_id=odm.book_id

group by book_id

having sum(od.quantity)>=all(

select sum(quantity)

from order_details

group by book_id ) );

32

Page 33: SQL Lab Manual Final

Result:

AUTHOR_NAME

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

RAMESH

QUERY V

This query demonstrates how to increase the price of the books published by a specific publisher by 10%.

SQL COMMAND

UPDATE CATALOG SET PRICE=1.1*PRICE

WHERE PUBLISHER_ID IN

(SELECT PUBLISHER_ID FROM PUBLISHER WHERE

PUBLISHER_NAME='&PUBLISHER_NAME');

Enter value for publisher_name: PEARSON

2 rows updated.

SQL> SELECT * FROM CATALOG;

BOOK_ID TITTLE AUTHOR_ID PUBLISHER_ID CATEGORY_ID YEAR

PRICE

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

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

501 CNE 1 100 200

2000 450

502 DATA STRUCTURE 2 101 200 2001

520

503 DBMS 1 102 203 2001

550

504 ADA 1 104 204

2002 500

33

Page 34: SQL Lab Manual Final

505 LOGIC DESIGN 2 104 203

2002 780

506 FAFL 5 103 201

2005 530

BOOK_ID TITTLE AUTHOR_ID PUBLISHER_ID CATEGORY_ID YEAR

PRICE

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

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

507 CO 4 102 202

2003 165

508 SS 5 104 201

2005 450

8 rows selected.

PROJECT – 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)CUSTOMER (customer_name: string, customer_street: string, city: string)DEPOSITOR (customer_name: string, accno: int)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.

34

Page 35: SQL Lab Manual Final

(ii) Enter atleast 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 branches located in a specific city.

(v) Demonstrate how you delete all account tuples at every branch located in a specific city.

(vi) Generation of suitable reports.

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

SCHEMA DESCRIPTION

This database is created for a banking enterprise. This database keeps an account of all the branches in a particular city, information about the customer like details of the customer, in which branch his account exists and details of loan taken from the customer(if any).

Table name: BRANCH This table consists of details of the branch in a particular city. It consists of the following attributes: branch_name: It gives the name of the branch. Since no two branches of a city can have the same name it is taken as the primary key of this table. branch_city: It gives the name of the city where the particular branch is located. assets: It gives the assets of the particular branch.

Table name: ACCOUNT This table gives the details of the each account of a particular like the account number, balance in the account. accno: It specifies the account number. Since no two account numbers can be same it is taken as the primary key of this table.

35

Page 36: SQL Lab Manual Final

branch_name: It gives the name of the branch in which the account is existing. This attribute refers the BRANCH table to get the name of the branch, hence it is considered to be the foreign key. balance: This gives the balance present in the corresponding account number.

Table name: CUSTOMER This table customer details. The following attributes are present in this table: customer_name: This gives the name of the customer. Two customers cannot have same name, therefore it is considered to be the primary key. customer_street: This gives the name of the street given by the customer. city: This gives the name of the city in which the customer lives. Table name: DEPOSITOR This table gives details of the account number of a particular customer. customer_name: This gives the name of the customer. It refers CUSTOMER table to get the name, therefore it is considered as the foreign key. accno: It gives the account number of the customer and refers the ACCOUNT table to get the accno, therefore it is considered as the foreign key. Since a customer can have more than one account and accno can be same in different branches we consider both the attributes as the primary key. Table name: LOAN This table gives the details of the loan sanctioned in the branches. loan_number: This gives the loan number. No two loan numbers can be same, therefore it is considered as the primary key. branch_name: It gives the name of the branch in which the account is existing. This attribute refers the BRANCH table to get the name of the branch, hence it is considered to be the foreign key. amount: This attribute gives the amount for which the loan is sanctioned. Table name: BORROWER This table gives the details of which customer as taken the loan. customer_name: This gives the name of the customer. It refers CUSTOMER table to get the name, therefore it is considered as the foreign key. loan_number: This gives the loan number. It refers LOAN table to obtain the loan number, therefore it is taken as the foreign key. Here both the attributes are together taken as the primary key.

36

Page 37: SQL Lab Manual Final

CREATION OF TABLES AND INSERTION OF TUPLES

CREATE TABLE BRANCH (

branch_namevarchar2(25) PRIMARY KEY, branch_city varchar2(20) NOT NULL, assets number NOT NULL);

CREATE TABLE ACCOUNT1(accno int PRIMARY KEY, branch_name varchar2(25) NOT NULL, balance number NOT NULL,FOREIGN KEY(branch_name) references BRANCH(branch_name));

CREATE TABLE CUSTOMER1(customer_name varchar2(25) PRIMARY KEY, customer_street varchar2(20) NOT NULL, customer_city varchar2(20) NOT NULL);

CREATE TABLE DEPOSITOR(customer_name varchar2(25) NOT NULL, accno int NOT NULL,PRIMARY KEY(customer_name, accno),

37

Page 38: SQL Lab Manual Final

FOREIGN KEY(accno) references ACCOUNT1(accno),FOREIGN KEY(customer_name) references

CUSTOMER1(customer_name));

CREATE TABLE LOAN(loan_number int PRIMARY KEY, branch_name varchar2(25) NOT NULL, amount number NOT NULL, FOREIGN KEY(branch_name) references BRANCH(branch_name));

CREATE TABLE BORROWER(customer_name varchar2(25) NOT NULL, loan_number int NOT NULL,PRIMARY KEY(customer_name, loan_number), FOREIGN KEY(customer_name) references CUSTOMER1(customer_name),FOREIGN KEY(loan_number) references LOAN(loan_number));

Insertion of tuples into the table ‘BRANCH’:

INSERT INTO BRANCH VALUES('&branch_name','&branch_city','&ASSETS');

Insertion of tuples into the table ‘ACCOUNT1’:

INSERT INTO ACCOUNT1 VALUES('&accno','&branch_name','&balance');

Insertion of tuples into the table ‘DEPOSITOR’:

INSERT INTO DEPOSITOR VALUES('&customer_name','&accno');

Insertion of tuples into the table ‘CUSTOMER’:

INSERT INTO CUSTOMER1 VALUES('&customer_name','&customer_street','&customer_city');

Insertion of tuples into the table ‘LOAN’:

38

Page 39: SQL Lab Manual Final

INSERT INTO LOAN VALUES(901, ‘HDFC’, 23455.22);INSERT INTO LOAN VALUES(877, ‘Citibank’, 7887.09);

Insertion of tuples into the table ‘BORROWER’:

INSERT INTO BORROWER VALUES(‘Smith’, 877);INSERT INTO BORROWER VALUES(‘John’, 901);

SQL> SELECT * FROM BRANCH;

BRANCH_NAME BRANCH_CITY ASSETS------------------------- -------------------- -------------------------------------------------SBI-MAIN BELGAUM 1000000.25SBI-VADAGAON BELGAUM 2.0000E+11UTI-MAIN BANGALORE 250000000UTI-DELHI DELHI 62000000.5ICICI-MAIN DHARWAD 2100000ICICI-SHAHAPUR DHARWAD 2100052

SQL> SELECT * FROM ACCOUNT1;

ACCNO BRANCH_NAME BALANCE---------- ------------------------- --------------------------------------------- 100 SBI-MAIN 150000 101 SBI-VADAGAON 25000 102 SBI-MAIN 520000 103 ICICI-MAIN 45000 105 ICICI-MAIN 62000 106 UTI-MAIN 63000 107 ICICI-SHAHAPUR 410000 108 UTI-MAIN 33634

SQL> SELECT * FROM CUSTOMER1;

CUSTOMER_NAME CUSTOMER_STREET CUSTOMER_CITY------------------------- -------------------- ----------------------------------------------------ARUN SHAHAPUR BELGAUMSHRUTI SHAHAPUR BELGAUMPRAVEEN GANESHPUR GALLI BELGAUM

39

Page 40: SQL Lab Manual Final

ASHOK MG-ROAD BANGALOREADITI MG-ROAD BANGALORENATWAR RPD DHARWAD

SQL> select * from depositor;

CUSTOMER_NAME ACCNO------------------------- ---------------------------ARUN 100ARUN 102SHRUTI 103SHRUTI 105ARUN 101ADITI 107ASHOK 106ARUN 108

QUERIES

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

QUERY: select branch_name, customer_name, count(*) as no_of_acc from account1 a, depositor d where a.accno = d.accno and branch_name like ‘%MAIN%’ group by customer_name, branch_name having count (*) >= 2

EXPLANATION: The above query is similar to a SELECT-JOIN-PROJECT sequence of relational algebra operations and such queries are called select-join queries. In the where clause, b_name = ‘MAIN’ specifies the selection condition and accno = actno is a join condition for the join operation on the two relations account and depositor. Then the group by clause is used to sub-group the tuples based on the grouping attributes b_name and cust_name. The having clause provides a condition count (*) >= 2 on the groups of tuples. Only the groups that satisfy the condition are retrieved in the result of the query.

RESULT:

BRANCH_NAME CUSTOMER_NAME NO_OF_ACC-------------------- ------------------------- ----------------------------------------------SBI-MAIN ARUN 2ICICI-MAIN SHRUTI 2

40

Page 41: SQL Lab Manual Final

The result of the query is as shown above. It selects only the customers who have two or more accounts in the MAIN branch.

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

QUERY: 1 SELECT C.CUSTOMER_NAME FROM CUSTOMER1 C 2 WHERE NOT EXISTS 3 ( 4 ( 5 SELECT BRANCH_NAME FROM BRANCH B 6 WHERE B.BRANCH_CITY='&BRANCH_CITY') 7 MINUS 8 ( 9 SELECT BRANCH_NAME FROM ACCOUNT1 A,DEPOSITOR D 10 WHERE A.ACCNO=D.ACCNO AND

D.CUSTOMER_NAME=C.CUSTOMER_NAME 11 )) 12 ;Enter value for branch_city: BANGALOREold 6: WHERE B.BRANCH_CITY='&BRANCH_CITY')new 6: WHERE B.BRANCH_CITY='BANGALORE')

RESULT1: CUSTOMER_NAME--------------------------------------------ARUNASHOK

RESULT2:Enter value for branch_city: BELGAUM

41

Page 42: SQL Lab Manual Final

old 6: WHERE B.BRANCH_CITY='&BRANCH_CITY')new 6: WHERE B.BRANCH_CITY='BELGAUM')

CUSTOMER_NAME-------------------------ARUN

EXPLANATION:

The nested query selects the tuples that satisfy the selection condition branch_city = ‘Bangalore’ from the relation branch. In the outer query, a join operation is performed on the relations account and depositor with the join condition as accno=actno. Further the IN operator compares the tuples selected from account and depositor with the set of union-compatible tuples produced from the nested query for the value b_name. The outer query also uses a group by clause to group the tuples based on the grouping attributes accno, cust_name.

As demonstrated by the result, the above query selects the customers who have account at all the branches located in the city ‘Bangalore’.

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

QUERYSQL> DELETE FROM DEPOSITOR D 2 WHERE D.ACCNO IN 3 ( SELECT A.ACCNO FROM ACCOUNT1 A,BRANCH B 4 WHERE A.BRANCH_NAME=B.BRANCH_NAME 5 AND B.BRANCH_CITY='&BRANCH_CITY');Enter value for branch_city: BANGALOREold 5: AND B.BRANCH_CITY='&BRANCH_CITY')new 5: AND B.BRANCH_CITY='BANGALORE')2 rows deleted.

SQL> SELECT * FROM DEPOSITOR;

CUSTOMER_NAME ACCNO------------------------- -----------------------------ARUN 100ARUN 102SHRUTI 103SHRUTI 105ARUN 101ADITI 107

42

Page 43: SQL Lab Manual Final

6 rows selected.

43


Recommended