MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
MERIT INSTITUTE OF BUSINESS MGMT &TECH (Approved by AICTE, & Affiliated to OU) Manneguda ‘X’ Roads, Turkayamzal, Hayathnagar Mandal, Ranga Reddy District.
Department of Master of Computer Applications
Certificate Hall Ticket No.: __________________
This is to certify that the bonafide record of the practical work done in the _____________________________Laboratory by Mr. / Ms. _____________________________________ of M.C.A. ________ year ______________ semester in during the academic year 2009-2010.
Lecturer-in-charge
Head of the Department
Internal Examiner
External Examiner
Date: __________________ PAGE - 1 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
MERIT INSTITUTE OF BUSINESS MGMT & TECH [M.C.A.] INDEX S. No. 1
2
3
4
5
6
7
8
Page No.
Experiment Name Create a table to represent sb-account of a bank consisting of account-no, customername, balance-amount. A] Write a PL/SQL block to implement deposit and withdraw. B] Withdraws should not be allowed if the balance goes below Rs.1000. Create The following two tables: College-info; Faculty-info. College-info consists of fields: college-code, college-name, address. Faculty-info consists of fields: college-code, faculty-code, faculty-name, qualification, experience-in-no-of-years, and address. The field collegecode is foreign key. A] Design a form to accept the data from the user. B] List all those faculty members whose experience is greater than or equal to 10 years and have M. Tech degree. C] List all those faculty members, who have at least 10 years of experience but do not have M. Tech degree. Create the following tables for Library Information System: Book: (accession-no, title, publisher, author, status) Status could be issued, present in the library, sent for binding, and cannot be issued. A] Write a trigger which sets the status of a book to "cannot be issued", if it is published 20 years back. Create the following tables for Library Information System: Book (accession-no, title, publisher, author, status, date-of-purchase) Status could be issued, present in the library, sent for binding, and account be issued. A] Create a form to accept the data from the user create a form to accept the data from the user with appropriate validation checks. B] Generate queries to list all those books which are new arrivals. The books which are acquired during the last 6 months are categorized as new arrivals. (i) List all those books that cannot be issued and purchased 20 years ago. Create the following tables: Student (roll-no, name, date-of-birth, course-id) Course (Course-id, name, fee, duration) A] Create a form to accept the data from the user with appropriate validation checks. B] Generate queries to list all those students who are greater than 18 years of age and have opted for MCA course. (i) List all those courses whose fee is greater than that of MCA course. Create the following table: Student (roll-no, name, subject-name, subject-opted) Subject (faculty-code, faculty-name, specialization) A] Create a form to accept the data from the user with appropriate validation checks. B] Generate queries to find the number of students who have enrolled for the subject "DBMS". (i) Find all those faculty members who have not offered any subject. Create the following table: Item (item-code, item-name, qty-in-stock, reorder-level); Supplier (supplier-code, supplier-name, address); Can-supply(supplier-code, itemcode) A] Create a form to accept the data from the user with appropriate validation checks. B] Generate queries to list all those suppliers who can supply the given item. (i) List all those items which cannot be supplied by given company. Create the following tables: Student (roll-no, marks, category, district, state) Student-rank(roll-no, marks, rank) A] Create a form to accept the data from the user with appropriate validation checks. B] Generate queries to list all those students who have come from Tamilnadu state and secured a rank above 100. (i) List all those students who come from Andhra Pradesh state and belong to given
PAGE - 2 -
Date
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
9
10
11
category who have secured a rank above 100. Create the following tables: Branch (branch-id, branch-name, customer-city, branch-id) Customer (customer-id, customer-name, customer-city, branch-id) A] Create a form to accept the data from the user with appropriate validation checks. B] Generate queries to list all those customers who live in the same city as the branch in which they have account. (i) List all those customers who have an account in a given branch city. Create the following tables: Book (accession-no, title, publisher, year, date-of-purchase, status) Member(member-id, name, number-of-books-issued, max-limit) Book-issue(accession-no, member-id, date-of-issue) A] Create a form to accept the data from the user with appropriate validation checks. B] Generate queries list all those books which are due from the students to be returned. A book is considered to be due if it has been issued 15 days back and yet not returned. Create the following tables: Book (accession-no, title, publisher, year, date-of-purchase, status) Member(member-id, name, number-of-books-issued, max-limit) Book-issue(accession-no, member-id, date-of-issue) A] Create a form to accept the data from the user with appropriate validation checks. B] Write a PL/SQL procedure to issue the book.
12
13
Create the following tables : Book(accession-no, title, publisher, year, date-of-purchase, status) Book-Place(accession-no, rack-id, rack-position) Member(member-id, name, number-of-books-issued, max-limit, status) Book-issue(accession-no, member-id, date-of-issue) A] Create a form to accept the data from the user with appropriate validation checks. B] Write a PL/SQL procedure to issue the book. Create the following table: Item (item-code, item-name, qty-in-stock, reorder-level) Supplier (supplier-code, supplier-name, address) Can-supply(supplier-code, item-code) A] Create a form to accept the data from the user with appropriate validation checks. B] Write PL/SQL procedure to generate a report to list the items whose qty-in-stock is less than to their reorder-levels. Create the following tables: Branch (branch-id, branch-name, branch-city) Customer (customer-id, customer-name, customer-city, branch-id)
14 A] Create a form to accept the data from the user with appropriate validation checks. B] Generate queries to list all those customers who live in the same city as the branch in which they have account. 15. Create the following tables: Branch (branch-id, branch-name, customer-city) Customer (customer-id, customer-name, customer-city, branch-id) 15 A] Create a form to accept the data from the user with appropriate validation checks. B] Generate queries to list all those customers who have more than a given number (here ‘4’) of customers. Create the following table: Student (roll-no, name, category, district, state) Student –rank (roll-no, marks, rank) 16 A] Create a form to accept the data from the user with appropriate validation checks. B] Generate queries to do list names of the students who are having same rank.
17
Create the following tables: Student(roll-no, name, date-of-birth, course-id) Course (Course-id, name, fee, duration)
PAGE - 3 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
A] Create a form to accept the data from the user with appropriate validation checks. B] Generate queries to list all those students who are between 18-19 years of age and have opted for MCA course. Create the following tables: Student(roll-no, name, date-of-birth, course-id) Course (Course-id, name, fee, duration, status) 18 A] Create a form to accept the data from the user with appropriate validation checks. B] Write PL/SQL procedure to set the status of course to "not offered" in which the number of candidates is less than 5. Create the following tables: Student (roll-no, name, date-of-birth, course-id) Course (Course-id, name, fee, duration, status)
A] Create a form to accept the data from the user with appropriate validation checks. 19
B] Write PL/SQL procedure to set the status of course to "not offered" in which the number of candidates is less than 5.
Create the following tables: Student (roll-no, name, date-of-birth, course-id) Course (Course-id, name, fee, duration, status) 20
A] Create a form to accept the data from the user with appropriate validation checks. B] Write PL/SQL procedure to set the status of course to "offered" in which the number of candidates is at least 10 otherwise set it to "not offered". Create the following tables: Book (accession-no, title, publisher, year, date-of-purchase, status) Member(member-id, name, number-of-books-issued, max-limit, status) Book-issue(accession-no, member-id, date-of-issue, due-date)
21
A] Create a form to accept the data from the user with appropriate validation checks. B] Write a PL/SQL to list all those students who are defaulters. A student is considerer to be a defaulter if he has not returned a book even after due-date.
22
Create the following table: Item (item-code, item-name, qty-in-stock, reorder-level) Supplier (supplier-code, supplier-name, address, status) Can-supply(supplier-code, item-code). A] Create a form to accept the data from the user with appropriate validation checks. B] Write PL/SQL procedure to set the status of the supplier to "important" if the supplier can supply more than five items
PAGE - 4 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
Create the following tables: Item (item-code, item-name, qty-in-stock, reorder-level) Supplier (supplier-code, supplier-name, address, status) Can-supply(supplier-code, item-code) 23
A] Create a form to accept the data from the user with appropriate validation checks. B] Write PL/SQL procedure to generate a report of those items that are supplied by those suppliers whose status is "important".
Create the following tables: Student (roll-no, name, category, district, state) Student –rank (roll-no, marks, rank)
24
A] Create a form to accept the data from the user with appropriate validation checks. B] Write PL/SQL procedure to generate a report to list of those districts from which the first hundred rankers come from.
Create the following tables: Student (roll-no, name, subject-opted) Subject –rank (subject-code, subject-name, faculty-code, specialization) Faculty (faculty-code, faculty-name, specialization) 25
A] Create a form to accept the data from the user with appropriate validation checks. B] Write PL/SQL procedure to set the status of the subject to "not offered" if the subject is not opted by at least 5 students.
Create the following tables: Student (roll-no, name, subject-opted) Subject –rank (subject-code, subject-name, faculty-code, specialization) Faculty (faculty-code, faculty-name, specialization) 26
A] Create a form to accept the data from the user with appropriate validation checks. B] Write PL/SQL procedure to set the status of the subject to "not offered" if the subject is not offered by any of the faculty members.
PAGE - 5 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
Create the following tables: Student (roll-no, name, subject-opted) Subject –rank (subject-code, subject-name, faculty-code) Faculty (faculty-code, faculty-name, specialization) 27
A] Create a form to accept the data from the user with appropriate validation checks. B] Generate queries to find the number of students who have enrolled for the subject "DBMS"
Create the following tables: Student (roll-no, name, subject-opted) Subject –rank (subject-code, subject-name, faculty-code) Faculty (faculty-code, faculty-name, specialization) 28
A] Create a form to accept the data from the user with appropriate validation checks. B] Generate queries to find the number of students who have enrolled for the subject "DBMS"
Create the following tables: Student (roll-no, name, subject-opted) Subject –rank (subject-code, subject-name, faculty-code) Faculty (faculty-code, faculty-name, specialization) 29
A] Create a form to accept the data from the user with appropriate validation checks. B] Generate queries to find the number of students who have enrolled for the subject "OS"
Create the following tables: Student (roll-no, name, subject-opted) Subject –rank (subject-code, subject-name, Faculty (faculty-code, faculty-name, specialization) 30
faculty-code)
A] Create a form to accept the data from the user with appropriate validation checks. B] Generate queries to find the number of students who have not enrolled for the subject "DBMS"
PAGE - 6 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
1. (A) Create A Table To Represent Sb-Account Of A Bank Consisting Of Account-No, Customer- Name, Balance-Amount. (B) Write A Pl/SQL Block To Implement Deposit And Withdraw. Withdraws Should Not Be Allowed If The Balance Goes Below Rs.1000.
CREATING SB_ACCOUNT TABLE
SQL > CREATE TABLE sb_account(account_no number(30)PRIMARY KEY ,customer_name varchar2(30),balance_amount number(8,2));
table created.
INSERTING RECORDS INTO SB_ACCOUNT TABLE
SQL >INSERT into sb_account values(&acno,'&acname',&balamt); enter value for acno: 10001 enter value for acname: raj enter value for balamt: 3000 old 1:INSERT into sb_account values(&acno,'&acname',&balamt) new 1:INSERT into sb_account values(10001,'raj',3000) 1 row created. SQL > / enter value for acno: 10002 enter value for acname: rani enter value for balamt: 6000 old 1:INSERT into sb_account values(&acno,'&acname',&balamt) new 1:INSERT into sb_account values(10002,'rani',6000) 1 row created. SQL > commit;
VIEW SB_ACCOUNT RECORDS
SQL > SELECT * from sb_account;
account_no customer_name balance_amount ---------- ------------------------------ -------------10001 raj 2800 10002 rani 6000 10003 sri 8000 10004 mohan 9000 10005 rohini 1000 commit complete.
PAGE - 7 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
(b)write a pl/SQL block to implement deposit and withdraw. withdraws should not be allowed if the balance goes below rs.1000. declare v_acno number:=&acno; v_amt number:=&amt; v_bal number; v_rembal number; begin SELECT balance_amount into v_bal from sb_account where account_no=v_acno; v_rembal:=v_bal-v_amt; if (v_rembal>1000) then update sb_account set balance_amount=v_rembal where account_no=v_acno; commit; else dbms_output.put_line('withdraw not possible'); end if; end; / enter value for acno: 10001 old 2: v_acno number:=&acno; new 2: v_acno number:=10001; enter value for amt: 100 old 3: v_amt number:=&amt; new 3: v_amt number:=100; pl/SQL procedure successfully completed.
SQL > set serveroutput on SQL > / enter value for acno: 10001 old 2: v_acno number:=&acno; new 2: v_acno number:=10001; enter value for amt: 100 old 3: v_amt number:=&amt; new 3: v_amt number:=100; pl/SQL procedure successfully completed. SQL > SELECT * from sb_account; account_no customer_name balance_amount ---------- ------------------------------ -------------10001 raj 2800 10002 rani 6000 10003 sri 8000 10004 mohan 9000 10005 rohini 1000
PAGE - 8 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
declare v_acno number:=&acno; v_amt number:=&amt; v_bal number; begin SELECT balance_amount into v_bal from sb_account where account_no=v_acno; v_bal:=v_bal+v_amt; update sb_account set balance_amount=v_bal where account_no=v_acno; commit; end; / enter value for acno: 10001 old 2: v_acno number:=&acno; new 2: v_acno number:=10001; enter value for amt: 1000 old 3: v_amt number:=&amt; new 3: v_amt number:=1000; pl/SQL procedure successfully completed. SQL > set serveroutput on SQL > / enter value for acno: 10001 old 2: v_acno number:=&acno; new 2: v_acno number:=10001; enter value for amt: 100 old 3: v_amt number:=&amt; new 3: v_amt number:=100; pl/SQL procedure successfully completed. SQL > SELECT * from sb_account; account_no customer_name balance_amount ---------- ------------------------------ -------------10001 raj 3900 10002 rani 6000 10003 sri 8000 10004 mohan 9000 10005 rohini 1000
PAGE - 9 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
2.
Create the following table : faculty-info faculty-info consists of fields : college-code, faculty-code, faculty-name, qualification, experience-in-no-of-years, address. Generate queries to do the following : (i) List all those faculty members whose experience is greater than or equal to 10 years and have m. Tech degree. (ii) List all those faculty members, who have at least 10 years of experience but do not have m. Tech degree.
SQL > CREATE TABLE faculty(faccode char(5), name varchar2(20),experience number, qualification varchar2(20)); table created. SQL >INSERT into faculty values('f05','devender',13,'m.tech,m.s'); 1 row created. SQL >INSERT into faculty values('f03 ','radhika',5,' b.tech'); 1 row created. SQL >INSERT into faculty values('f07',' rahul ',10,' m.sc,stats'); 1 row created.
list all those faculty whose experience is greater than or equal to 10 yrs and have m.tec h degree. SQL > SELECT name from faculty where experience>=10 and qualification like '%m.tech%'; name -------------------devender
list all those faculty members, who have at least 10 years of experience but do not have m.tech degree. SQL > SELECT name from faculty where experience>=10 and qualification not like '%m.tech%'; name -------------------rahul
PAGE- 10 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
3. Create the following tables for library information system : book : (accession-no, title, publisher, author, status) status could be issued, present in the library, sent for binding, and cannot be issued. Write atrigger which sets the status of a book to "cannot be issued", if it is published 20 years back. SQL > CREATE TABLE book( accno char(10) PRIMARY KEY , title varchar2(30),publisher varchar2(20),author 2 varchar2(10),status char(10),dtpur date); table created.
SQL > ALTER TABLE book add( constraint status_ck check (status in ('issued','present','binding','cantissue')) ); table altered. SQL >INSERT into book values('1','hp-chamber of secrets','london','jkrowling','','20-aug-2006'); 1 row created. SQL >INSERT into book values('2','hp-philisopher stone','london','jkrowling','','20-aug-2008'); 1 row created. write a trigger which sets the status of a book to “can’t issue” if it is published 20 years back. SQL > create or replace trigger book_stat_trig afterINSERT on book begin update book set status='cantissue' where (months_between(sysdate,dtpur)/12)>=20; end; / trigger created. SQL >INSERT into book values('4','negotiation possible','hyderabad','','','16-oct-1987'); 1 row created. SQL > SELECT * from book; accno title publisher author status dtpur ---------- ------------------------------ -------------------- ---------- ---------- --------1 hp-chamber of secrets london jkrowling 20-aug-06 2 hp-philisopher stone london jkrowling 20-aug-08 4 negotiation possible hyderabad cantissue 16-oct-87
PAGE- 11 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
4. Create the following tables for library information system : book(accession-no, title, publisher, author, status, date-of-purchase)status could be issued, presentin the library, sent for binding, and account be issued. Generate queries to do the following : (i) (ii)
List all those books which are new arrivals. The books which are acquired during the last 6 months are categorized as new arrivals. List all those books that cannot be issued and purchased 20 years ago.
SQL > CREATE TABLE book(accno number PRIMARY KEY ,title varchar2(50),publisher varchar(25),author varchar2(25),dop date); table created. SQL > ALTER TABLE book add (status varchar2(50)); table altered. SQL > ALTER TABLE book add constraint c1 check (status in ('issued','present in library','sent for binding','a/c be issued'))
table altered.
SQL > desc book name null? type ----------------------------------------- -------- ---------------------------accno not null number title varchar2(50) publisher varchar2(25) author varchar2(25) dop date status varchar2(50)
SQL >INSERT into book(accno,publisher,status,title,author,dop)values(&acno,'&pub','&st','&title','&author','&dop'); enter value for acno: 100 enter value for pub: tata enter value for st: issued enter value for title: networka enter value for author: stallings enter value for dop: 01-aug-84 old 1:INSERT into book(accno,publisher,status,title,author,dop)values(&acno,'&pub','&st','&title','&author','&dop') new 1:INSERT into book(accno,publisher,status,title,author,dop)values(100,'tata','issued','networka','stallings','01-aug-84') 1 row created. SQL > / enter value for acno: 180 enter value for pub: oracle enter value for st: abcd enter value for title: ad PAGE- 12 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
enter value for author: william enter value for dop: 01-feb-08 old 1:INSERT into book(accno,publisher,status,title,author,dop)values(&acno,'&pub','&st','&title','&author','&dop') new 1:INSERT into book(accno,publisher,status,title,author,dop)values(180,'oracle','abcd','ad','william','01feb-08') insert into book(accno,publisher,status,title,author,dop)values(180,'oracle','abcd','ad','william','01-feb-08') * error at line 1: ora-02290: check constraint (neerus.c1) violated
SQL > commit; commit complete. SQL >INSERT into book(accno,publisher,status,title,author,dop)values(&acno,'&pub','&st','&title','&author','&dop'); enter value for acno: 150 enter value for pub: chand enter value for st: sent for binding enter value for title: numerical enter value for author: schand enter value for dop: 01-sep-08 old 1:INSERT into book(accno,publisher,status,title,author,dop)values(&acno,'&pub','&st','&title','&author','&dop') new 1:INSERT into book(accno,publisher,status,title,author,dop)values(150,'chand','sent for binding','numerical','schand','01-sep-08') 1 row created. SQL > commit; list all those books which are new arrivals. the books which are acquired during the last 6 months are categorized as new arrivals.
commit complete. SQL > SELECT * from book where dop<=add_months(sysdate,-6); accno title publisher author -------------------------------------------------100 networka tata stallings
dop 01-aug-84
SQL >SELECT * from book where add_months(sysdate,-6)<=dop;
accno title ---------- -------------------------------------------------publisher author dop ------------------------- ------------------------- --------status -------------------------------------------------150 numerical chand schand 01-sep-08 sent for binding PAGE- 13 -
status issued
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
LIST ALL THOSE BOOKS THAT CANNOT BE ISSUED AND PURCHASED 20 YEARS AGO.
SQL >SELECT * from book where (sysdate-dop)/365>=20 accno title ---------- -------------------------------------------------publisher author dop ------------------------- ------------------------- --------status -------------------------------------------------100 networka tata stallings 01-aug-84 issued
SQL > commit; commit complete.
PAGE- 14 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
5. Create the following tables : student(roll-no, name, date-of-birth, course-id) course (course-id, name, fee, duration) (a) create a form to accept the data from the user with appropriate validation checks. (b) generate queries to do the following : (i) list all those students who are greater than 18 years of age and have opted for mca course. (ii) list all those courses whose fee is greater than that of mca course.
SQL > CREATE TABLE student( roll char(10) PRIMARY KEY , dob date, name varchar2(10),courseid char(10)); table created. SQL > CREATE TABLE course(courseid char(10) PRIMARY KEY , name varchar2(10), fee number, duration number); table created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created.
course values ('c001','mca',75000,3); course values ('m003','mba',200000,2); student values ('04808015','06-aug-1978','suchitra','c001'); student values ('04708015','07-sep-1991','radhika','c001');
(b) (i) list all student names who have opted for mca course and are above 18 yrs of age. SQL >SELECT s.name "student list" from student "s",course "c" where (months_between(sysdate,dob)/12) >=18 and c.name='mca' and c.courseid=s.courseid; student li ---------suchitra (b) (ii) list all the course whose fee is more than course’mca’. SQL > SELECT c.name from course "c" 2 where fee > (SELECT fee from course where name='mca'); name ---------mba
PAGE- 15 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
6.
Create the following table : student (roll-no, name, subject-name, subject-opted) subject(faculty-code, faculty-name, specialization) generate queries to do the following : (i) find the number of students who have enrolled for the subject "dbms". (ii) find all those faculty members who have not offered any subject.
SQL > CREATE TABLE student( roll char(5), name varchar2(20),subjopt varchar2(30)); table created.
SQL >CREATE TABLE subject(subcode char(3),name varchar2(10), faccode char(5), specialization varchar2(10),status varchar2(15)); table created. SQL > CREATE TABLE faculty( faccode char(5),name varchar2(20),experience number, 2 specialization varchar2(20)); table created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created.
student values('s8010','rekha','m01,s01,s02'); student values('s8021','ravi','s01,m02,d01'); subject values('m01','dismaths','f10','',''); subject values('m02',' geomaths','','',''); subject values('s01',' stats',' f07','',''); subject values('s02','opresearch','f07','',''); subject values('d01','dbms',' f03','',''); faculty values('f10','lavanya',5,' m.sc,matha'); faculty values('f07',' rahul ',5,' m.sc,stats'); faculty values('f05','devender',3,'m.tech,m.s'); faculty values('f03 ','radhika',10,' b.tech');
(i) list the number of students who have enrolled for the subject ‘dbms’. SQL > SELECT count(*) from student,subject where (subcode=substr(subjopt,0,3) 2 or subcode=substr(subjopt,5,3) or subcode=substr(subjopt,9,3)) and subject.name='dbms'; count(*) --------1
PAGE- 16 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
(ii) list those faculty members who have not offered any subject. SQL > SELECT name from faculty where faccode not in (SELECT faculty.faccode from 2 subject,faculty where subject.faccode=faculty.faccode); name -------------------devender radhika
PAGE- 17 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
7.
Create the following table : item (item-code, item-name, qty-in-stock, reorder-level) supplier (supplier-code, supplier-name, address) can-supply(supplier-code, item-code) Generate queries to do the following : (i) List all those suppliers who can supply the given item. (ii) List all those items which cannot be supplied by given company.
SQL > CREATE TABLE item( itcode char(10) PRIMARY KEY , itname varchar2(20),qtysk number,reorder number); table created. SQL > CREATE TABLE supplier( supcode char(10) PRIMARY KEY ,supname varchar2(20),address varchar2(20), status varchar2(10)); table created. SQL > CREATE TABLE cansupply(supcode char(10),itcode char(10)); table created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created.
item values('i001','sugar',5,3); item values('i002','salt',2,6); item values('i003','lentils',10,5); supplier values('s001','ashok','patna',''); cansupply values('s001','i001'); cansupply values('s001','i002');
(i) list all those suppliers who can supply the given item. SQL > SELECT item.itname, supplier.supname from item, supplier, cansupplywhere cansupply.itcode=item.itcode and cansupply.supcode=supplier.supcode; itname supname -------------------- -------------------sugar ashok salt ashok
(ii) list all those items which cannot be supplied by given company. SQL > SELECT item.itname from item where item.itcode not in (SELECT itcode from cansupply); itname -------------------lentils
PAGE- 18 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
8.
Create the following tables: student (roll-no, marks, category, district, state) student-rank(roll-no, marks, rank) generate queries to do the following : (i) list all those students who have come from tamilnadu state and secured a rank above 100. (ii)list all those students who come from andhra pradesh state and belong to given category who have secured a rank above 100.
SQL > CREATE TABLE student( roll char(10) PRIMARY KEY , name varchar2(20),category char(2), district varchar2(20),state varchar2(20)); table created. SQL > CREATE TABLE rank(roll char(10),marks number, rank number); table created. SQL SQL SQL SQL
>INSERT >INSERT >INSERT >INSERT
into into into into
student student student student
SQL SQL SQL SQL
>INSERT >INSERT >INSERT >INSERT
into into into into
rank rank rank rank
values('s003','siddhu','oc','karimnagar','ap'); values('s004','rekha','bc','chennai','tamilnadu'); values('s005','hema','bc','pondichery','tamilnadu'); values('s006','sudheer','oc','karimnagar','ap');
values('s003',650,135); values('s004',600,101); values('s005',600,102); values('s006',700,120);
list all those students who have come from tamilnadu state and secured a rank above 100. SQL > SELECT student.roll,student.name from student,rank where student.roll=rank.roll and rank>100 and state= 'tamilnadu'; roll name ---------- -------------------s004 rekha s005 hema list all students who come from andhrapradesh state and belong to given category who have secured a rank above 100. SQL > SELECT student.roll,name,category,rank from student,rank where student.roll=rank.roll and rank>100 and state='ap' order by category; roll name ca rank ---------- -------------------- -- --------s003 siddhu oc 135 s006 sudheer oc 120
PAGE- 19 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
9.create the following tables : branch (branch-id, branch-name, customer-city, branch-id) customer (customer-id, customer-name, customer-city, branch-id) generate queries to do the following : (i) list all those customers who live in the same city as the branch in which they have account. (ii) list all those customers who have an account in a given branch city. SQL > CREATE TABLE branch( branchid char(5) PRIMARY KEY ,name varchar2(10),city varchar2(10)); table created. SQL > CREATE TABLE customer(custid char(5) PRIMARY KEY , name varchar2(20),city varchar2(10), branchid char(5)); table created. SQL >INSERT into branch values('b002','chikkad','hyd'); 1 row created. SQL >INSERT into branch values('b003','marred','sec'); 1 row created. SQL >INSERT into customer values('c002','suchitra2','hyd','b002'); 1 row created. SQL >INSERT into customer values('c003','suchitra3','hyd','b002'); 1 row created. SQL >INSERT into customer values('c004','suchitra4','hyd','b003'); 1 row created. list all those customers who live in the same city as the branch in which they have account. SQL > SELECT customer.name, branch.name from branch,customer where branch.branchid=customer.branchid and customer.city=branch.city; name name -------------------- ---------suchitra2 chikkad suchitra3 chikkad list all customers who have an account in a given branch city. SQL > SELECT customer.name from customer,branch where customer.branchid=branch.branchid and branch.city='sec'; name -------------------suchitra4
PAGE- 20 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
10.
Create the following tables : book(accession-no, title, publisher, year, date-of-purchase, status) member(member-id, name, number-of-books-issued, max-limit) book-issue(accession-no, member-id, date-of-issue) Generate queries to do the following : (i) list all those books which are due from the students to be returned. A book is considered to be due if it has been issued 15 days back and yet not returned. (ii) list all those members who cannot be issued any more books.
SQL > CREATE TABLE book( accno char(10) PRIMARY KEY , title varchar2(30),publisher varchar2(20), 2 author varchar2(10),status char(10),dtpur date); table created. SQL > ALTER TABLE book add( constraint status_ck check (status in ('issued','present','binding','cantissue')) ); table altered. SQL > CREATE TABLE member( memid char(10) PRIMARY KEY , name varchar2(20),no_bk_issued number, 2 maxlt number, status varchar2(10)); table created. SQL > CREATE TABLE issued(accno char(10), memid char(10),dt date); table created. SQL >INSERT into book values('1','hp-chamber of secrets','london','jkrowling','','20-aug-2006'); 1 row created. SQL >INSERT into book values('2','hp-philisopher stone','london','jkrowling','','20-aug-2008'); 1 row created. SQL >INSERT into member values('h001','suchitra',1,10,''); 1 row created. SQL >INSERT into member values('s001','siddharth',1,1,''); 1 row created. SQL >INSERT into issued values('1','h001','01-feb-2008'); 1 row created. SQL >INSERT into issued values('2','s001','20-oct-2008'); 1 row created. list all the book which are due from the students to be returned. a book is considered due if it has been issued 15 days back and yet not returned. SQL > SELECT book.title, book.accno from book , issued where issued.accno=book.accno 2 and (months_between(sysdate,issued.dt)*30)>15; title accno ------------------------------ ---------hp-chamber of secrets 1 list all those members who cannot be issued any more books. SQL > SELECT name from member where no_bk_issued=maxlt; name -------------------siddharth
PAGE- 21 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
11.
Create the following tables : book(accession-no, title, publisher, year, date-of-purchase, status) member(member-id, name, number-of-books-issued, max-limit) book-issue(accession-no, member-id, date-of-issue) (a) create a form to accept the data from the user with appropriate validation checks. (b) write a pl/sql procedure to issue the book. write a trigger to set the status of students to "back listed" if they have taken book but not returned even after one year.
SQL > CREATE TABLE book(accno char(10) PRIMARY KEY , title varchar2(30),publisher varchar2(20), 2 author varchar2(10),status char(10),dtpur date); table created. SQL > ALTER TABLE book add( constraint status_ck check (status in ('issued','present','binding','cantissue')) ); table altered. SQL > CREATE TABLE member(memid char(10) PRIMARY KEY ,name varchar2(20),no_bk_issued number, 2 maxlt number,status varchar2(10)); table created. SQL > CREATE TABLE issued(accno char(10),memid char(10),dt date); table created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created.
book values('1','hp-cham of secrets','london','jkrowling','issued','20-aug-2007'); book values('2','hp-philis stone','london','jkrowling','issued','20-aug-2008'); book values('5','databaseappl','hyderabad','ramakrish','','17-jan-2007'); member values('h001','suchitra',1,10,''); member values('s001','siddharth',1,5,''); issued values('1','h001','01-feb-2007'); issued values('2','s001','20-oct-2008');
write pl/SQL procedure to issue the book. SQL > edit issue.SQL ; declare bkno char(10); memid char(10); flag number; begin bkno:=&accessionno; memid:=&memberid; flag:=0; SELECT count(*) into flag from book,member where bkno=accno and memid=member.memid; if flag>0 then insert into issued values(bkno,memid,sysdate); update book set status= ‘issued’ where accno=bkno; end if; end;
PAGE- 22 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
SQL > start issue.SQL ; 19 / enter value for accessionno: 5 old 7: bkno:=&accessionno; new 7: bkno:=5; enter value for memberid: 's001' old 8: memid:=&memberid; new 8: memid:='s001'; pl/SQL procedure successfully completed. SQL > SELECT * from issued; accno memid dt ---------- ---------- --------1 h001 01-feb-07 2 s001 20-oct-08 5 s001 23-oct-08 SQL > SELECT * from book where accno=5; accno title publisher author status dtpur ---------- ------------------------------ -------------------- ---------- ---------- --------5 databaseappl hyderabad ramakrish issued 17-jan-07 write a trigger to set the status of students to ‘backlisted’ if they have taken book but not returned even after one year. SQL > create or replace trigger mem_stat_trig 2 afterINSERT on member 3 declare 4 begin 5 update member 6 set status='backlist' 7 where memid in (SELECT memid from issued 8 where (months_between(sysdate,dt)/12)>=1); 9 end; 10 / trigger created. SQL >INSERT into member values('s002','sadhana',0,5,''); 1 row created. SQL > SELECT * from member; memid name no_bk_issued maxlt status ---------- -------------------- ------------ --------- ---------h001 suchitra 1 10 backlist s001 siddharth 1 5 s002 sadhana 0 5
PAGE- 23 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
12.
Create the following tables : book(accession-no, title, publisher, year, date-of-purchase, status) book-place(accession-no, rack-id, rack-position) member(member-id, name, number-of-books-issued, max-limit, status) book-issue(accession-no, member-id, date-of-issue) (a) create a form to accept the data from the user with appropriate validation checks. (b) write a pl/sql procedure to issue the book. write a trigger to set the status of a book neither to "lost" which is neither issued nor in the library.
SQL > CREATE TABLE book( accno char(10) PRIMARY KEY , title varchar2(30),publisher varchar2(20), 2 author varchar2(10),status char(10),dtpur date); table created. SQL > ALTER TABLE book add( constraint status_ck check (status in ('issued','present','binding','cantissue')) ); table altered. SQL > CREATE TABLE member( memid char(10) PRIMARY KEY , name varchar2(20), no_bk_issued number, 2 maxlt number, status varchar2(10)); table created. SQL > CREATE TABLE issued(accno char(10),memid char(10),dt date); table created. SQL > CREATE TABLE bookpl( accno char(10), rackid char(3),rackposition char(5)); table created. SQL >INSERT into book values('1','hp-cham of secrets','london','jkrowling','issued','20-aug-2007'); 1 row created. SQL >INSERT into book values('2','hp-philisr stone','london','jkrowling','issued','20-aug-2008'); 1 row created. SQL >INSERT into book values('5','databaseappl','hyderabad','ramakrish','','17-jan-2007'); 1 row created. SQL >INSERT into member values('h001','suchitra',1,10,''); 1 row created. SQL >INSERT into member values('s001','siddharth',1,5,''); 1 row created. SQL >INSERT into issued values('1','h001','01-feb-2007'); 1 row created. SQL >INSERT into issued values('2','s001','20-oct-2008'); 1 row created. SQL >INSERT into bookpl values ('3','005','south'); 1 row created. write pl/SQL procedure to issue the book. SQL > edit issue.SQL ; declare bkno char(10); memid char(10); flag number;
PAGE- 24 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
begin bkno:=&accessionno; memid:=&memberid; flag:=0; SELECT count(*) into flag from book,member where bkno=accno and memid=member.memid; if flag>0 then insert into issued values(bkno,memid,sysdate); update book set status= ‘issued’ where accno=bkno; end if; end; SQL > start issue.SQL ; 19 / enter value for accessionno: 5 old 7: bkno:=&accessionno; new 7: bkno:=5; enter value for memberid: 's001' old 8: memid:=&memberid; new 8: memid:='s001'; pl/SQL procedure successfully completed. SQL > SELECT * from issued; accno memid dt ---------- ---------- --------1 h001 01-feb-07 2 s001 20-oct-08 5 s001 23-oct-08 SQL > SELECT * from book where accno=5; accno title publisher author status dtpur ---------- ------------------------------ -------------------- ---------- ---------- --------5 databaseappl hyderabad ramakrish issued 17-jan-07
write a trigger to set the status of the book to ‘lost’ which is neither issued not in the library. SQL > create or replace trigger book_stat_trig2 2 afterINSERT on book 3 begin 4 update book 5 set status='lost' 6 where accno not in (SELECT accno from issued) 7 and accno not in (SELECT accno from bookpl); 8 end; 9 / trigger created.
PAGE- 25 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
SQL >INSERT into book values('3','negotaing','secun','','','18-mar-2007'); 1 row created.
SQL > SELECT * from book; accno title publisher author status dtpur ---------- ------------------------------ -------------------- ---------- ---------- --------1 hp-cham of secrets london jkrowling issued 20-aug-07 2 hp-philis stone london jkrowling issued 20-aug-08 5 databaseappl hyderabad ramakrish issued 17-jan-07 3 negotaing secun lost 18-mar-07 SQL > SELECT * from bookpl; accno rac rackp ---------- --- ----1 005 south SQL > SELECT * from issued; accno memid dt ---------- ---------- --------1 h001 01-feb-07 2 s001 20-oct-08 5 s001 23-oct-08
PAGE- 26 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
13.
Create the following tables : book(accession-no, title, publisher, year, date-of-purchase, status) member(member-id, name, number-of-books-issued, max-limit, status) book-issue(accession-no, member-id, date-of-issue, due-date) (a) create a form to accept the data from the user with appropriate validation checks. (b) write a pl/sql to list all those students who are defaulters. A student is considerer to be a defaulter if he has not returned a book even after due-date. write a trigger to set the status of students to "back listed" if they have taken book but not returned even after one year.
SQL > CREATE TABLE book( accno char(10) PRIMARY KEY , title varchar2(30),publisher varchar2(20), 2 author varchar2(10),status char(10),dtpur date); table created. SQL > ALTER TABLE book add( constraint status_ck check (status in ('issued','present','binding','cantissue')) ); table altered. SQL > CREATE TABLE member(memid char(10) PRIMARY KEY , name varchar2(20), no_bk_issued number, 2 maxlt number, status varchar2(10)); table created. SQL > CREATE TABLE issued( accno char(10), memid char(10),dt date); table created. SQL > CREATE TABLE bookpl(accno char(10), rackid char(3),rackposition char(5)); table created. SQL >INSERT into book values('1','hp-cham of secrets','london','jkrowling','issued','20-aug-2007'); 1 row created. SQL >INSERT into book values('2','hp-philis stone','london','jkrowling','issued','20-aug-2008'); 1 row created. SQL >INSERT into book values('5','databaseappl','hyderabad','ramakrish','','17-jan-2007'); 1 row created.
SQL >INSERT into member values('h001','suchitra',1,10,''); 1 row created. SQL >INSERT into member values('s001','siddharth',1,5,''); 1 row created. SQL >INSERT into issued values('1','h001','01-feb-2007'); 1 row created. SQL >INSERT into issued values('2','s001','20-oct-2008'); 1 row created. SQL >INSERT into bookpl values ('3','005','south'); 1 row created.
PAGE- 27 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
write a pl/SQL to list all those students who are defaulters. a student is considered to be defaulter if he has not returned a book even after due date. SQL > SELECT member.name from member 2 where memid in (SELECT memid from issued 3 where (months_between(sysdate,dt)/12)>=1 4 and accno not in (SELECT accno from bookpl)); name -------------------suchitra write a trigger to set the status of students to ‘backlisted’ if they have taken book but not returned even after one year. SQL > create or replace trigger mem_stat_trig 2 afterINSERT on member 3 declare 4 begin 5 update member 6 set status='backlist' 7 where memid in (SELECT memid from issued 8 where (months_between(sysdate,dt)/12)>=1); 9 end; 10 / trigger created. SQL >INSERT into member values('s002','sadhana',0,5,''); 1 row created. SQL > SELECT * from member; memid name no_bk_issued maxlt status ---------- -------------------- ------------ --------- ---------h001 suchitra 1 10 backlist s001 siddharth 1 5 s002 sadhana 0 5
PAGE- 28 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
14.
Create the following tables : branch (branch-id, branch-name, branch-city) customer (customer-id, customer-name, customer-city, branch-id) generate queries to do the following : (i) list all those customers who live in the same city as the branch in which they have account. (ii) list all those customers who have an account in more than one branch.
SQL > CREATE TABLE branch(branchid char(5) PRIMARY KEY , name varchar2(10),city varchar2(10)); table created. SQL > CREATE TABLE customer(custid char(5),name varchar2(20),city varchar2(10),branchid char(5)); table created. SQL >INSERT into branch values('b002','chikkad','hyd'); 1 row created. SQL >INSERT into branch values('b003','marred','sec'); 1 row created. SQL >INSERT into branch values('b004','patni','sec'); 1 row created. SQL >INSERT into customer values('c002','suchitra2','hyd','b002'); 1 row created. SQL >INSERT into customer values('c003','suchitra3','sec','b002'); 1 row created. SQL >INSERT into customer values('c002','suchitra2','hyd','b003'); 1 row created. SQL >INSERT into customer values('c002','suchitra2','hyd','b004'); 1 row created. list all those customers who live in the same city as the branch in which they have account. SQL > SELECT customer.name, branch.name 2 from branch,customer 3 where branch.branchid=customer.branchid 4 and customer.city=branch.city; name name -------------------- ---------suchitra2 chikkad list all customers who have an account in more than one branch. SQL > SELECT unique customer.name 2 from customer 3 where custid in( 4 SELECT custid from customer group by custid having count(*)>=2); name -------------------suchitra2
PAGE- 29 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
15.
Create the following tables : branch (branch-id, branch-name, customer-city) customer (customer-id, customer-name, customer-city, branch-id) generate queries to do the following : (i) list all those customers who have more than 100 customer. (ii) list all those customers who have an account in more than one branch
SQL > CREATE TABLE branch(branchid char(5) PRIMARY KEY , name varchar2(10),city varchar2(10)); table created. SQL > CREATE TABLE customer(custid char(5) PRIMARY KEY , name varchar2(20),city varchar2(10), 2 branchid char(5)); table created. SQL >INSERT into branch values('b002','chikkad','hyd'); 1 row created. SQL >INSERT into branch values('b003','marred','sec'); 1 row created. SQL >INSERT into branch values('b004','patni','sec'); 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created. SQL >INSERT into 1 row created.
customer values('c002','suchitra2','hyd','b002'); customer values('c003','suchitra3','sec','b002'); customer values('c002','suchitra2','hyd','b003'); customer values('c002','suchitra2','hyd','b004'); customer values('c004','suchitra4','hyd','b002'); customer values('c005','suchitra5','hyd','b002'); customer values('c006','suchitra6','hyd','b002');
list all the branch who have more than a given number(here 4) of customers. SQL > SELECT unique branch.name from branch,customer 2 where branch.branchid=customer.branchid 3 and customer.branchid in (SELECT branchid from customer 4 group by branchid having count(*)>=4); name ---------chikkad list all customers who have an account in more than one branch. SQL > SELECT unique customer.name 2 from customer 3 where custid in( 4 SELECT custid from customer group by custid having count(*)>=2); name -------------------suchitra2
PAGE- 30 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
16.
Create the following table : student (roll-no, name, category, district, state) student –rank (roll-no, marks, rank) generate queries to do the following : 1. List names of the students who are having same rank but they should reside in different districts. 2.list details of students they belongs to same category with same rank.
SQL > CREATE TABLE student( roll char(10) PRIMARY KEY , name varchar2(20),category char(2), district varchar2(20), state varchar2(20)); table created. SQL > CREATE TABLE rank(roll char(10),marks number, rank number); table created. SQL >INSERT into student values('s002','suchitra','oc','patna','bihar'); 1 row created. SQL >INSERT into student values('s003','siddhu','oc','karimnagar','ap'); 1 row created. SQL >INSERT into student values('s006','sudheer','bc','karimnagar','ap'); 1 row created. SQL >INSERT into rank values('s002',650,35); 1 row created. SQL >INSERT into rank values('s003',650,35); 1 row created. SQL >INSERT into rank values('s006',700,35); 1 row created.
PAGE- 31 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
17.
Create the following tables : student(roll-no, name, date-of-birth, course-id) course (course-id, name, fee, duration) generate queries to do the following : 1. list all those students who are between 18-19 years of age and have opted for mca course. 2. List all those courses in which number of students are less than 10.
SQL > CREATE TABLE student(roll char(10) PRIMARY KEY , dob date, name varchar2(10),courseid char(10)); table created. SQL > CREATE TABLE course(courseid char(10) PRIMARY KEY , name varchar2(10),fee number, 2 duration number); table created. SQL > insert insert insert insert insert insert insert insert insert insert insert insert insert insert
into into into into into into into into into into into into into into
course values ('c001','mca',75000,3); course values ('m003','mba',200000,2); student values ('04808015', '07-sep-1990','suchitra','c001'); student values ('04708015', '07-sep-1990','radhika','c001'); student values ('04708012','12-jan-1988','hemasri','c001'); student values ('04807010','26-dec-1990','jayasri','c001'); student values ('04708016','07-jul-1985','radha','c001'); student values ('04708013','12-jun-1988','hemalatha','c001'); student values ('04807011','26-dec-1990','vasuda','c001'); student values ('04808018','06-nov-1978','sadhana','c001'); student values ('04708019','07-oct-1985','ragini','c001'); student values ('04708020','21-feb-1988','anmol','m003'); student values ('04807021','20-dec-1990','teja','m003'); student values('0460909','17-jul-1990','kareena','c001');
list all those students who are between 18-19 yrs of age and have opted for mca course. SQL 2 3 4
> SELECT s.name "student list" from student "s",course "c" where (months_between(sysdate,dob)/12) between 18 and 19 and c.name='mca' and c.courseid=s.courseid;
student li ---------suchitra radhika kareena list all those courses in which number of students are less than 10. SQL > SELECT name from course 2 where courseid in ( 3 SELECT courseid from student group by courseid having count(*)<10); name ---------mba
PAGE- 32 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
18.
Create the following tables : student(roll-no, name, date-of-birth, course-id) course (course-id, name, fee, duration, status) write pl/sql procedure to do the following : set the status of course to "not offered" in which the number of candidates is less than 5.
SQL > CREATE TABLE student(roll char(10) PRIMARY KEY , dob date, name varchar2(10), courseid char(10)); table created. SQL > CREATE TABLE course( courseid char(10) PRIMARY KEY , name varchar2(10),fee number, 2 duration number status varchar2(10)); table created. insert into course values ('c001','mca',75000,3,''); insert into course values ('m003','mba',200000,2,''); insert into student values ('04808015', '07-sep-1990','suchitra','c001'); insert into student values ('04708015', '07-sep-1990','radhika','c001'); insert into student values ('04708012','12-jan-1988','hemasri','c001'); insert into student values ('04807010','26-dec-1990','jayasri','c001'); insert into student values ('04708016','07-jul-1985','radha','c001'); insert into student values ('04708013','12-jun-1988','hemalatha','c001'); insert into student values ('04807011','26-dec-1990','vasuda','c001'); insert into student values ('04808018','06-nov-1978','sadhana','c001'); insert into student values ('04708019','07-oct-1985','ragini','c001'); insert into student values ('04708020','21-feb-1988','anmol','m003'); insert into student values ('04807021','20-dec-1990','teja','m003'); insert into student values('0460909','17-jul-1990','kareena','c001'); pl/SQL procedure to set the status of course to ‘not offered’ in which the number of candidates is less than 5. SQL > edit status.SQL ; declare id char(10); num number; cursor course_cr is SELECT * from course; course_val course_cr%rowtype; begin open course_cr; loop fetch course_cr into course_val; exit when course_cr%notfound; id:=course_val.courseid; SELECT count(*) into num from student where courseid=id; where courseid=id; if num < 3 then update course set status='not-off' where courseid=id; end if; if num >= 3 then update course set status='offered' where courseid=id; end if; end loop; close course_cr;
PAGE- 33 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
end;
SQL > SELECT * from course; courseid name fee duration status ---------- ---------- --------- --------- -------------------c001 mca 75000 3 m003 mba 200000 2 SQL > start status.SQL ; 31 / pl/SQL procedure successfully completed. SQL > SELECT * from course; courseid name fee duration status ---------- ---------- --------- --------- --------------c001 mca 75000 3 offered m003 mba 200000 2 not-off
PAGE- 34 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
19.
Create the following tables : student(roll-no, name, date-of-birth, course-id) course (course-id, name, fee, duration, status) write pl/sql procedure to do the following : set the status of course to "not offered" in which the number of candidates is less than 5.
SQL > CREATE TABLE student(roll char(10) PRIMARY KEY , dob date, name varchar2(10), courseid char(10)); table created. SQL > CREATE TABLE course(courseid char(10) PRIMARY KEY , name varchar2(10),fee number, 2 duration number); table created. insert into course values ('c001','mca',75000,3); insert into course values ('m003','mba',200000,2); insert into student values ('04808015', '07-sep-1990','suchitra','c001'); insert into student values ('04708015', '07-sep-1990','radhika','c001'); insert into student values ('04708012','12-jan-1988','hemasri','c001'); insert into student values ('04807010','26-dec-1990','jayasri','c001'); insert into student values ('04708016','07-jul-1985','radha','c001'); insert into student values ('04708013','12-jun-1988','hemalatha','c001'); insert into student values ('04807011','26-dec-1990','vasuda','c001'); insert into student values ('04808018','06-nov-1978','sadhana','c001'); insert into student values ('04708019','07-oct-1985','ragini','c001'); insert into student values ('04708020','21-feb-1988','anmol','m003'); insert into student values ('04807021','20-dec-1990','teja','m003'); insert into student values('0460909','17-jul-1990','kareena','c001'); pl/SQL procedure to set the status of course to ‘not offered’ in which the number of candidates is less than 5. SQL > edit status.SQL ; declare id char(10); num number; cursor course_cr is SELECT * from course; course_val course_cr%rowtype; begin open course_cr; loop fetch course_cr into course_val; exit when course_cr%notfound; id:=course_val.courseid; SELECT count(*) into num from student where courseid=id; if num < 5 then update course set status='not-off' where courseid=id; end if; end loop; close course_cr; end; SQL > SELECT * from course; courseid name fee duration status ---------- ---------- --------- --------- -------------------c001 mca 75000 3 m003 mba 200000 2
PAGE- 35 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
SQL > start status.SQL ; 31 / pl/SQL procedure successfully completed. SQL > SELECT * from course; courseid name fee duration status ---------- ---------- --------- --------- --------------c001 mca 75000 3 m003 mba 200000 2 not-off
PAGE- 36 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
20.
Create the following tables : student(roll-no, name, date-of-birth, course-id) course (course-id, name, fee, duration, status) (a) create a form to accept the data from the user with appropriate validation checks. (b) write pl/sql procedure to do the following : set the status of course to "offered" in which the number of candidates is at least 10 otherwise set it to "not offered".
SQL > CREATE TABLE student(roll char(10) PRIMARY KEY ,dob date, name varchar2(10),courseid char(10)); table created. SQL > CREATE TABLE course(courseid char(10) PRIMARY KEY , name varchar2(10), 2 fee number, duration number); table created. insert insert insert insert insert insert insert insert insert insert insert insert insert insert
into into into into into into into into into into into into into into
course values ('c001','mca',75000,3); course values ('m003','mba',200000,2); student values ('04808015', '07-sep-1990','suchitra','c001'); student values ('04708015', '07-sep-1990','radhika','c001'); student values ('04708012','12-jan-1988','hemasri','c001'); student values ('04807010','26-dec-1990','jayasri','c001'); student values ('04708016','07-jul-1985','radha','c001'); student values ('04708013','12-jun-1988','hemalatha','c001'); student values ('04807011','26-dec-1990','vasuda','c001'); student values ('04808018','06-nov-1978','sadhana','c001'); student values ('04708019','07-oct-1985','ragini','c001'); student values ('04708020','21-feb-1988','anmol','m003'); student values ('04807021','20-dec-1990','teja','m003'); student values('0460909','17-jul-1990','kareena','c001');
pl/SQL procedure to set the status of course to 'offered' in which the number of candidates is at least 10 otherwise set to ‘not offered’ . SQL > edit status.SQL ; declare id char(10); num number; cursor course_cr is SELECT * from course; course_val course_cr%rowtype; begin open course_cr; loop fetch course_cr into course_val; exit when course_cr%notfound; id:=course_val.courseid; SELECT count(*) into num from student_all where courseid=id; if num < 10 then update course set status='not-off' where courseid=id; end if; if num >= 10 then update course set status='offered' where courseid=id; end if;
PAGE- 37 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
end loop; close course_cr; end; SQL > SELECT * from course; courseid name fee duration status ---------- ---------- --------- --------- -------------------c001 mca 75000 3 m003 mba 200000 2 SQL > start status.SQL ; 31 / pl/SQL procedure successfully completed. SQL > SELECT * from course; courseid name fee duration status ---------- ---------- --------- --------- --------------c001 mca 75000 3 offered m003 mba 200000 2 not-off
PAGE- 38 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
21.
create the following table : item (item-code, item-name, qty-in-stock, reorder-level) supplier (supplier-code, supplier-name, address) can-supply(supplier-code, item-code) write pl/SQL procedure to do the following : generate a report to list the items whose qty-in-stock is less than or equal to their reorderlevels.
SQL > CREATE TABLE item( itcode char(10) PRIMARY KEY , itname varchar2(20), qtysk number,2 reorder number); table created.
SQL > CREATE TABLE supplier(supcode char(10) PRIMARY KEY , 3 supname varchar2(20), 4 address varchar2(20), 5 status varchar2(10)); table created.
SQL > CREATE TABLE cansupply( supcode char(10), itcode char(10)); table created. insert insert insert insert insert insert insert insert insert insert insert insert insert insert insert insert insert insert insert
into into into into into into into into into into into into into into into into into into into
item values(‘i001’,’sugar’,5,3); item values(‘i002’,’salt’,2,6); item values(‘i003’,’lentils’,10,5); item values(‘i004’,’turmeric’,3,2); item values(‘i005’,’chilli-dry’,7,5); item values(‘i006’,’chilli-whole’,2,5); item values(‘i007’,’oil’,10,5); item values(‘i008’,’cummin’,5,7); item values(‘i009’,’coriander’,5,5); supplier values(‘s001’,’ashok’,’patna’,’’); supplier values(‘s002’,’ramu’,’hyd’); cansupply values(‘s001’,’i001’); cansupply values(‘s001’,’i002’); cansupply values(‘s001’,’i005’); cansupply values(‘s001’,’i006’); cansupply values(‘s001’,’i009’); cansupply values(‘s002’,’i002’); cansupply values(‘s002’,’i003’); cansupply values(‘s002’,’i004’);
pl/SQL procedure to generate a report to list the items whose quantity in stock is less than the reorder level. SQL > edit report.SQL ; set heading on column itname heading 'item name' column supname heading 'supplier name' SELECT supplier.supname, item.itname from item,supplier where item.itcode in (SELECT itcode from cansupply where supplier.supcode=supcode) and supplier.supcode in (SELECT supcode from cansupply where item.itcode=itcode);
PAGE- 39 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
SQL > start report.SQL ; supplier name item name -------------------- -------------------ashok sugar ashok salt ashok chilli-dry ashok chilli-whole ashok coriander ramu salt ramu lentils ramu turmeric 8 rows SELECT ed.
PAGE- 40 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
22.
create the following table : item (item-code, item-name, qty-in-stock, reorder-level) supplier (supplier-code, supplier-name, address, status) can-supply(supplier-code, item-code) write pl/SQL procedure to do the following : set the status of the supplier to "important" if the supplier can supply more than five items.
SQL > CREATE TABLE item(itcode char(10) PRIMARY KEY , itname varchar2(20),qtysk number, 2 reorder number); table created. SQL > CREATE TABLE supplier(supcode char(10) PRIMARY KEY , supname varchar2(20), 2 address varchar2(20),status varchar2(10)); table created. SQL > CREATE TABLE cansupply(supcode char(10), itcode char(10)); table created. insert into item values(‘i001’,’sugar’,5,3); insert into item values(‘i002’,’salt’,2,6); insert into item values(‘i003’,’lentils’,10,5); insert into item values(‘i004’,’turmeric’,3,2); insert into item values(‘i005’,’chilli-dry’,7,5); insert into item values(‘i006’,’chilli-whole’,2,5); insert into item values(‘i007’,’oil’,10,5); insert into item values(‘i008’,’cummin’,5,7); insert into item values(‘i009’,’coriander’,5,5);
insert into supplier values(‘s001’,’ashok’,’patna’,’’); insert into supplier values(‘s002’,’ramu gs’,’hyd,’’);
insert insert insert insert insert insert insert insert
into into into into into into into into
cansupply cansupply cansupply cansupply cansupply cansupply cansupply cansupply
values(‘s001’,’i001’); values(‘s001’,’i002’); values(‘s001’,’i005’); values(‘s001’,’i006’); values(‘s001’,’i009’); values(‘s002’,’i002’); values(‘s002’,’i003’); values(‘s002’,’i004’);
pl/SQL procedure to set the status of the supplier to ‘important’ if the supplier can supply more than 5 items. SQL > edit status.SQL ; begin update supplier set status='important' where supcode in ( SELECT supcode from cansupply group by supcode having count(*)>=5); end; SQL > SELECT * from supplier; supcode supplier name address status ---------- -------------------- -------------------- ---------s001 ashok patna s002 ramu hyd
PAGE- 41 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
SQL > start status.SQL ; 8 / pl/SQL procedure successfully completed. SQL > SELECT * from supplier; supcode supplier name address status ---------- -------------------- -------------------- ---------s001 ashok patna important s002 ramu hyd
PAGE- 42 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
23.
create the following tables : item (item-code, item-name, qty-in-stock, reorder-level) supplier (supplier-code, supplier-name, address, status) can-supply(supplier-code, item-code) write pl/SQL procedure to do the following : generate a report of those items that are supplied by those suppliers whose status is "important".
SQL > CREATE TABLE item(itcode char(10) PRIMARY KEY , itname varchar2(20),qtysk number, 2 reorder number); table created. SQL > CREATE TABLE supplier(supcode char(10) PRIMARY KEY , supname varchar2(20), 2 address varchar2(20),status varchar2(10)); table created. SQL > CREATE TABLE cansupply(supcode char(10),itcode char(10)); table created.
insert insert insert insert insert insert insert insert insert
into into into into into into into into into
item item item item item item item item item
values(‘i001’,’sugar’,5,3); values(‘i002’,’salt’,2,6); values(‘i003’,’lentils’,10,5); values(‘i004’,’turmeric’,3,2); values(‘i005’,’chilli-dry’,7,5); values(‘i006’,’chilli-whole’,2,5); values(‘i007’,’oil’,10,5); values(‘i008’,’cummin’,5,7); values(‘i009’,’coriander’,5,5);
insert into supplier values(‘s001’,’ashok’,’patna’,’’); insert into supplier values(‘s002’,’ramu gs’,’hyd,’’); insert into supplier values(‘s003’,’spencer’,’hyd’,’’);
insert insert insert insert insert insert insert insert
into into into into into into into into
cansupply cansupply cansupply cansupply cansupply cansupply cansupply cansupply
values(‘s001’,’i001’); values(‘s001’,’i002’); values(‘s001’,’i005’); values(‘s001’,’i006’); values(‘s001’,’i009’); values(‘s002’,’i002’); values(‘s002’,’i003’); values(‘s002’,’i004’);
pl/SQL procedure to generate a report on those items that are supplied by those suppliers whose status is ‘important’. SQL > edit report.SQL ; set heading on column itname heading 'item name' column supname heading 'supplier name' SELECT supplier.supname, item.itname from item,supplier where status= 'important' and item.itcode in (SELECT itcode from cansupply where supplier.supcode=supcode) and supplier.supcode in (SELECT supcode from cansupply where item.itcode=itcode);
PAGE- 43 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
SQL > SELECT * from supplier; supcode supplier name address status ---------- -------------------- -------------------- ---------s001 ashok patna important s002 ramu hyd SQL > start report.SQL ; supplier name item name -------------------- -------------------ashok sugar ashok salt ashok chilli-dry ashok chilli-whole ashok coriander
PAGE- 44 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
24.
create the following tables : student (roll-no, name, category, district, state) student –rank (roll-no, marks, rank) write pl/SQL procedure to the following : generate a report to list of those districts from which the first hundred rankers come from.
SQL > CREATE TABLE student( roll char(10) PRIMARY KEY , name varchar2(20),category char(2), 2 district varchar2(20),state varchar2(20)); table created. SQL > CREATE TABLE rank(roll char(10),marks number,rank number); table created. insert into student values(‘s002’,’suchitra’,’oc’,’patna’,’bihar’); insert into student values(‘s003’,’siddhu’,’oc’,’karimnagar’,’ap’); insert into student values(‘s004’,’rekha’,’bc’,’chennai’,’tamilnadu’); insert into student values(‘s005’,’hema’,’bc’,’pondichery’,’tamilnadu’); insert into student values(‘s006’,’sudheer’,’oc’,’karimnagar’,’ap’); insert insert insert insert insert
into into into into into
rank rank rank rank rank
values(‘s002’,650,35); values(‘s003’,650,35); values(‘s004’,600,101); values(‘s005’,600,102); values(‘s006’,700,20);
pl/SQL procedure to generate a report to list those districts from which the first hundred rankers come from. SQL > edit report.SQL ; set heading on column district heading '-district-' SELECT unique district from studentr,rank where studentr.roll=rank.roll and rank.rank<=100; / district -------------------karimnagar patna
PAGE- 45 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
25.
create the following tables : student (roll-no, name, subject-opted) subject –rank (subject-code, subject-name, faculty-code, specialization) faculty (faculty-code, faculty-name, specialization)
write pl/SQL procedure to the following : set the status of the subject to "not offered" if the subject is not opted by at least 5 students. SQL > CREATE TABLE student(roll char(5), 3 name varchar2(20),subjopt varchar2(30)); table created. SQL > CREATE TABLE subject(subcode char(3),name varchar2(10),faccode char(5), 2 specialization varchar2(10), status varchar2(15)); table created. SQL > CREATE TABLE faculty(faccode char(5),name varchar2(20),experience number, 2 specialization varchar2(10)); table created. insert insert insert insert insert insert insert insert insert insert insert insert
into into into into into into into into into into into into
student values('s8015','suchitra','m01,m02,s01'); student values('s8012',' siddu','m01,s02,p01'); student values('s8010','rekha','m01,s01,m02,'); student values('s8021','ravi','s01,m02,d01'); student values('s8016','chitragada','m01,m02,s01’); student values('s8013',' sid roy','m01,m02,s01’); subject values('m01','dismaths','f07','',''); subject values('m02',' geomaths','','',''); subject values('s01',' stats',' f07','',''); subject values('p01','physics',' f05','',''); subject values('s02','opresearch','f07','',''); subject values('d01','dbms',' f03','','');
pl/SQL procedure to set the status of the subject to’not offered’ if the subject is not opted by at least 5 students. SQL > edit status.SQL ; declare sub varchar2(10); num_student number; cursor sub_cr is SELECT * from subject; sub_val sub_cr%rowtype; begin open sub_cr; loop fetch sub_cr into sub_val; exit when sub_cr%notfound; sub:=sub_val.name; SELECT count(*) into num_student from student,subject where (subcode=substr(subjopt,0,3) or subcode=substr(subjopt,5,3) or subcode=substr(subjopt,9,3)) and subject.name=sub;
PAGE- 46 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
if num_student<5 then update subject set status=''||num_student where name=sub; end if; end loop; close sub_cr; end; SQL > SELECT * from subject; sub name facco specializa status --- ---------- ----- ---------- --------------m01 dismaths f07 m02 geomaths s01 stats f07 p01 physics f05 s02 opresearch f07 d01 dbms f03 6 rows SELECT ed. SQL > start status.SQL ; 32 / pl/SQL procedure successfully completed. SQL > SELECT * from subject; sub name facco specializa status --- ---------- ----- ---------- --------------m01 dismaths f07 m02 geomaths s01 stats f07 p01 physics f05 notoffered s02 opresearch f07 notoffered d01 dbms f03 notoffered 6 rows SELECT ed.
PAGE- 47 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
26.
create the following tables : student (roll-no, name, subject-opted) subject –rank (subject-code, subject-name, faculty-code, specialization) faculty (faculty-code, faculty-name, specialization) write pl/SQL procedure to the following : set the status of the subject to "not offered" if the subject is not offered by any of the faculty members.
SQL > CREATE TABLE student( roll char(5), name varchar2(20),subjopt varchar2(30)); table created. SQL > CREATE TABLE subject( subcode char(3),name varchar2(10),faccode char(5), 2 specialization varchar2(10), status varchar2(15));
table created. SQL > CREATE TABLE faculty(faccode char(5), name varchar2(20),experience number, 2 specialization varchar2(10)); table created.
insert insert insert insert insert insert
into into into into into into
student student student student student student
values('s8015','suchitra','m01,m02,s01'); values('s8012',' siddu','m01,s02,p01'); values('s8010','rekha','m01,s01,m02,'); values('s8021','ravi','s01,m02,d01'); values('s8016','chitragada','m01,m02,s01’); values('s8013',' sid roy','m01,m02,s01’);
insert insert insert insert insert insert
into into into into into into
subject subject subject subject subject subject
values('m01','dismaths','f07','',''); values('m02',' geomaths','','',''); values('s01',' stats',' f07','',''); values('p01','physics',' f05','',''); values('s02','opresearch','f07','',''); values('d01','dbms',' f03','','');
insert insert insert insert
into into into into
faculty faculty faculty faculty
values('f10','lavanya','', 5,' m.sc,matha'); values('f07',' rahul ','', 5,' m.sc,stats'); values('f05','devender','',3,'m.tech,m.s'); values('f03 ','radhika','',10,' b.tech');
pl/SQL procedure to set the status of the subject to ‘not offered’ if the subject is not offered by any of the faculty members. begin update subject set status='notoffered' where faccode is null; end; SQL > SELECT * from subject; sub name facco specializa status --- ---------- ----- ---------- --------------m01 dismaths f07 m02 geomaths s01 stats f07 p01 physics f05 s02 opresearch f07
PAGE- 48 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
d01 dbms f03 6 rows SELECT ed. SQL > start ex.SQL ; 6 / pl/SQL procedure successfully completed. SQL > SELECT * from subject; sub name facco specializa status --- ---------- ----- ---------- --------------m01 dismaths f07 m02 geomaths notoffered s01 stats f07 p01 physics f05 s02 opresearch f07 d01 dbms f03 6 rows SELECT ed.
PAGE- 49 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
27.
create the following tables : student (roll-no, name, subject-opted) subject –rank (subject-code, subject-name, faculty-code) faculty (faculty-code, faculty-name, specialization)
generate queries to do the following : • find the number of students who have enrolled for the subject "dbms" • find all those subjects which are not offered by any faculty members. SQL > CREATE TABLE student( roll char(5),name varchar2(20),subjopt varchar2(30)); table created. SQL > CREATE TABLE subject( subcode char(3), name varchar2(10),faccode char(5), 2 specialization varchar2(10),status varchar2(15)); table created. SQL > CREATE TABLE faculty(faccode char(5), name varchar2(20),experience number, 2 specialization varchar2(10)); table created. insert insert insert insert
into into into into
student student student student
values('s8015','suchitra','m01,m02,s01'); values('s8012',' siddu','m01,s02,p01'); values('s8010','rekha','m01,s01,s02'); values('s8021','ravi','s01,m02,d01');
insert insert insert insert insert insert
into into into into into into
subject subject subject subject subject subject
values('m01','dismaths','f07','',''); values('m02',' geomaths','','',''); values('s01',' stats',' f07','',''); values('p01','physics',' f05','',''); values('s02','opresearch','f07','',''); values('d01','dbms',' f03','','');
insert insert insert insert
into into into into
faculty faculty faculty faculty
values('f10','lavanya','', 5,' m.sc,matha'); values('f07',' rahul ','', 5,' m.sc,stats'); values('f05','devender','',3,'m.tech,m.s'); values('f03 ','radhika','',10,' b.tech');
list the number of students who have enrolled for the subject ‘dbms’. SQL > SELECT count(*) 2 from student,subject 3 where 4 (subcode=substr(subjopt,0,3) 5 or subcode=substr(subjopt,5,3) 6 or subcode=substr(subjopt,9,3)) 7 and subject.name='dbms'; count(*) --------1 list all those subjects which are not offered by any of the faculty members. SQL > SELECT name from subject where faccode is null; name ---------geomaths
PAGE- 50 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
28.
create the following tables : student (roll-no, name, subject-opted) subject –rank (subject-code, subject-name, faculty-code) faculty (faculty-code, faculty-name, specialization) generate queries to do the following : (i) find the number of students who have enrolled for the subject "dbms" (ii) find all those subjects which are offered by more than one faculty member.
SQL > CREATE TABLE student(roll char(5),name varchar2(20),subjopt varchar2(30)); table created. SQL > CREATE TABLE subject(subcode char(3), name varchar2(10),faccode char(5), 2 specialization varchar2(10),status varchar2(15)); table created. SQL > CREATE TABLE faculty( faccode char(5), name varchar2(20),experience number, 2 specialization varchar2(10)); table created. insert insert insert insert
into into into into
student student student student
values('s8015','suchitra','m01,m02,s01'); values('s8012',' siddu','m01,s02,p01'); values('s8010','rekha','m01,s01,s02'); values('s8021','ravi','s01,m02,d01');
insert insert insert insert insert insert
into into into into into into
subject subject subject subject subject subject
values('m01','dismaths','f07','',''); values('m02',' geomaths','','',''); values('s01',' stats',' f07','',''); values('p01','physics',' f05','',''); values('s02','opresearch','f07','',''); values('d01','dbms',' f03','','');
insert insert insert insert
into into into into
faculty faculty faculty faculty
values('f10','lavanya', 5,' m.sc,matha'); values('f07',' rahul ', 5,' m.sc,stats'); values('f05','devender', 3,'m.tech,m.s'); values('f03 ','radhika',10,' b.tech');
list the students who have enrolled for the subject ‘dbms’. SQL > SELECT student.name 2 from student,subject 3 where 4 (subcode=substr(subjopt,0,3) 5 or subcode=substr(subjopt,5,3) 6 or subcode=substr(subjopt,9,3)) 7 and subject.name='dbms'; name -------------------ravi find those subjects which are offered by more than one faculty member. SQL > SELECT name from subject 2 where faccode =(SELECT faccode from subject group by faccode having count(*)>1); name ---------dismaths opresearch
PAGE- 51 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
29.
create the following tables : student (roll-no, name, subject-opted) subject –rank (subject-code, subject-name, faculty-code) faculty (faculty-code, faculty-name, specialization) generate queries to do the following : (i) find the number of students who have enrolled for the subject "os" (ii) find all those students who opted for more than 5 subjects.
SQL > CREATE TABLE student( roll char(5), name varchar2(20),subjopt varchar2(30)); table created. SQL > CREATE TABLE subject( subcode char(3), name varchar2(10),faccode char(5), 2 specialization varchar2(10), status varchar2(15)); table created. SQL > CREATE TABLE faculty( faccode char(5), name varchar2(20),experience number, 2 specialization varchar2(10)); table created. insert into student values('s8015','suchitra','m01,m02,s01'); insert into student values('s8012',' siddu','m01,s02,p01'); insert into student values('s8010','rekha','m01,s01,d01'); insert into student values('s8021','ravi','s01,m02,d01,m01,s02,p01'); insert into subject values('m01','dismaths','f07','',''); insert into subject values('m02',' geomaths','','',''); insert into subject values('s01',' stats',' f07','',''); insert into subject values('p01','physics',' f05','',''); insert into subject values('s02','os','f07','',''); insert into subject values('d01','dbms',' f03','',''); find all those students who have enrolled for the subject “os”. SQL > SELECT student.name 2 from student,subject 3 where 4 (subcode=substr(subjopt,0,3) 5 or subcode=substr(subjopt,5,3) 6 or subcode=substr(subjopt,9,3)) 7 and subject.name='os'; name -------------------siddu find all those students who opted for more than 5 subjects. SQL > SELECT name from student where instr(subjopt,',',1,5)>0; name -------------------ravi
PAGE- 52 -
MERIT INST. OF BUSINESS MANAGEMENT & TECHNOLOGY
30. create the following tables : student (roll-no, name, subject-opted) subject –rank (subject-code, subject-name, faculty-code) faculty (faculty-code, faculty-name, specialization) generate queries to do the following : (i) find the number of students who have not enrolled for the subject "dbms" (ii) find all those subjects which are offered by more than one faculty member. SQL > CREATE TABLE student( roll char(5),name varchar2(20),subjopt varchar2(30)); table created. SQL > CREATE TABLE subject( subcode char(3),name varchar2(10),faccode char(5), 2 specialization varchar2(10), status varchar2(15)); table created. SQL > CREATE TABLE faculty(faccode char(5), name varchar2(20), experience number, 2 specialization varchar2(10)); table created. insert insert insert insert
into into into into
student student student student
values('s8015','suchitra','m01,m02,s01'); values('s8012',' siddu','m01,s02,p01'); values('s8010','rekha','m01,s01,s02'); values('s8021','ravi','s01,m02,d01');
insert insert insert insert insert insert
into into into into into into
subject subject subject subject subject subject
values('m01','dismaths','f07','',''); values('m02',' geomaths','','',''); values('s01',' stats',' f07','',''); values('p01','physics',' f05','',''); values('s02','opresearch','f07','',''); values('d01','dbms',' f03','','');
insert insert insert insert
into into into into
faculty faculty faculty faculty
values('f10','lavanya','', 5,' m.sc,matha'); values('f07',' rahul ','', 5,' m.sc,stats'); values('f05','devender','',3,'m.tech,m.s'); values('f03 ','radhika','',10,' b.tech');
list the number of students who have not enrolled for the subject ‘dbms’. SQL > SELECT count(*) 2 from student 3 where substr(subjopt,0,3) not like 4 (SELECT subcode from subject 5 where name='dbms') 6 and substr(subjopt,5,3) not like 7 (SELECT subcode from subject 8 where name='dbms') 9 and substr(subjopt,9,3) not like 10 (SELECT subcode from subject 11 where name='dbms'); count(*) --------3 find those subjects which are offered by more than one faculty member. SQL > SELECT name from subject 2 where faccode =(SELECT faccode from subject group by faccode having count(*)>1); name ---------dismaths os
PAGE- 53 -