The document is a lab manual for a database applications course. It contains instructions for students on completing lab exercises using various database relations to practice SQL queries and create front-end interfaces. The manual includes 5 sections, with each section containing sample database relations and multiple queries for students to write and test using an RDBMS like Oracle or DB2. It also provides guidance on building front-end applications to display query results.
The document is a lab manual for a database applications course. It contains instructions for students on completing lab exercises using various database relations to practice SQL queries and create front-end interfaces. The manual includes 5 sections, with each section containing sample database relations and multiple queries for students to write and test using an RDBMS like Oracle or DB2. It also provides guidance on building front-end applications to display query results.
The document is a lab manual for a database applications course. It contains instructions for students on completing lab exercises using various database relations to practice SQL queries and create front-end interfaces. The manual includes 5 sections, with each section containing sample database relations and multiple queries for students to write and test using an RDBMS like Oracle or DB2. It also provides guidance on building front-end applications to display query results.
The document is a lab manual for a database applications course. It contains instructions for students on completing lab exercises using various database relations to practice SQL queries and create front-end interfaces. The manual includes 5 sections, with each section containing sample database relations and multiple queries for students to write and test using an RDBMS like Oracle or DB2. It also provides guidance on building front-end applications to display query results.
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: J unior: J R, etc.) Write the following queries in SQL. No duplicates should be printed in any of the answers.
1. Find the names of all J uniors (level ='J R') who are enrolled in a class taught by Prof. Harshith. 2. Find the names of all classes that either meet in roomR128 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 roomin which some class is taught. 5. Find the names of faculty members for whomthe 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 fromBengaluru 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 fromBengaluru to New Delhi. (Page 10 16)
Database Applications Lab Manual
Department of ISE Page 3
III. Consider the following database of student enrollment in courses & books adopted for each course.
(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.
(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
(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) 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.
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: J unior: J R, etc.) Write the following queries in SQL. No duplicates should be printed in any of the answers.
1. Find the names of all J uniors (level ='J R') who are enrolled in a class taught by Prof. Harshith. 2. Find the names of all classes that either meet in roomR128 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 roomin which some class is taught. 5. Find the names of faculty members for whomthe combined enrollment of the courses that they teach is less than five.
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 );
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);
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
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 J AKE JOHN BLAKE
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
Database Applications Lab Manual
Department of ISE Page 10
AIRLINE FLIGHT INFORMATION
II. The following relations keep track of airline flight information:
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 fromBengaluru 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 fromBengaluru to New Delhi.
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
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);
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);
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 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
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
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.
(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.
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, MAJ OR 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)
);
Database Applications Lab Manual
Department of ISE Page 19
INSERTING TUPLES
SQL>INSERT INTO STUDENT VALUES('1BI10IS010', 'ANU','DBMS', '15-J AN-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 MAJ OR BDATE 1BI10IS010 ANU DBMS 15-J AN-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 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>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);
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.
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;
(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.
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) );
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) );
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
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);
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);
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 maximumsales.
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
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);
(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.
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 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) );
Database Applications Lab Manual
Department of ISE Page 31
Inserting values into tables:
INSERT INTO BRANCH VALUES(J AYANAGAR,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);
INSERT INTO ACCOUNT VALUES(123,'J AYANAGAR',25000); INSERT INTO ACCOUNT VALUES(156,'J AYANAGAR',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);
INSERT INTO CUSTOMER1 VALUES('RAMU','J AYANAGAR','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 J AYANAGAR 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);
INSERT INTO LOAN VALUES(2222,'J AYANAGAR',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);
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=J AYANAGAR) 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
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);