Sie sind auf Seite 1von 125

S.

No
1.

INDEX Program
(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. Withdraw should not be allowed if the balance goes below Rs.1000.

Page No
8

2.

Create the following two tables: 12 College-info College-info College-info consists of fields : college-code, college-name, address Faculty-info consists of fields : college-code, faculty-code, faculty-name, qualification, college-code is foreign key. (a)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. Create the following tables for library-information systems : Book: (Accession -no, title, publisher, author, status) Status could be issued, present, in the library, dent 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 systems : Book : (Accession- no, title, publisher, author, status, date-of -purchase) Status could be issued, present, in the library, dent for binding, and cannot be issued. Generate queries to do the following : (i) List all those books which are new arrivals. The books which are requires during the last 6 months are categorized as new arrivals. (ii) 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)Generate the queries to do the following: (i) List all those students who are greater than 18 years of age and 15

3.

4.

17

5.

19

have opted or MCA course. (ii) List all those courses whose fee is grater than that of MCA course.

S.No
6.

Program
Create the following table : Student(roll-no,name subject-opted) Subject(faculty-code, faculty-name, specialization) (a)Generate queries to do the following : (i) Find the number of students who haveenrolles for the subject DBMS (ii) 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, item-code) (a) 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. Create the following table: Student(roll-no,marks,category, district, state) Student-rank(roll-no, marks,rank) (a)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 have come from Anshra Pradesh state and belong togiven category who have secured a rank above 100. Create the following table: Branch (branch-id, branch-name, branch-city) Customer (customer-id, customer-name, customer-city, branch-id) (a) Generate queries to do the following: (i) List all those customers who live in the same city as the branch in which theyhave account. (ii) 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, number-of-books-issued, max-limit)Book-issue (accession-no, member-id, date-of-issue) (a) Generate the following to do the following: (i) List all those books which are due from the students to be returned. A book is considered to be issued 15 days back and yet not returned. (ii) List all those members who cannot be issued any more books Create the following tables: 2

Page No
22

7.

25

8.

28

9.

31

10.

34

11.

38

12.

13.

14.

15.

Book (accession no, title, publisher, year, date-of-purchase, status) Member (member-id, number-of-books-issued, max-limit) Book-issue (accession-no, member-id, date-of-issue) (a) Write a PL/SQL procedure to issue the book. (b)Write a trigger to set the status of student to back listed if they have taken book but not returned even one year. Create the following Book (accession-no, title, publisher, year, date-of-purchase) 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-purchase) (a) Write a PL/SQL procedure to issue the book (b) Write a trigger to set the status of the book neither to lost which is neither issued nor in the library. Create the following Book (accession-no, title, publisher, year, date-of-purchase) Member (member-id, name, number-of-books-issued, max-limit, status) Book-issue (accession-no, member-id, date-of-purchase ,due-date) (a) Write a PL/SQL to list all those students who are defaulters. Astudent is considered to be a defaulter if he has not returned a book even after the due-date (b) Write a trigger to set the status of the student to back listed if they have taken book but not returned even after one year. Create the following tables: Branch (branch-id, branch-name, branch_-city) Customer (customer-id, customer-name, customer-city, branch-id) (b) 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. Create the following tables: Branch (branch-id, branch-name, branch_-city) Customer (customer-id, customer-name, customer-city, branch-id) (a) Generate queries to do the following: (i)List all those branches who have more than 2 customers. (ii) List all those customers who have an account in more than one branch. Create the following table: Student (roll-no, name, category, district, state)

43

49

53

57

16.

61

17.

18.

19.

20.

21.

22.

Student-rank (roll-no, marks, rank) (b) Generate queries to do the following : (i) List names of the students who are having same rank bur they should reside in different districts. (ii) List details of students they belongs to same category with same tank. Create the following tables: Student (roll-no, name, date-of-birth, course-id) Course (course-id, name, fee, duration) (a) Generate queries to do the following: (i) List all those student who are between 10-19 years of age and have opted for MCAcourse. (ii) List all those courses in which number of students are less than 10. Create the following tables: Student (roll_no, name,date_of_birth,course-id) Course (course-id, name, fee, duration, status) (a) Write PL/SQL procedure to do the following: Set the status of course to not offered in which the number of candidate 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) Write PL/SQL procedure to do the following: Set the status of course to not offered in which the number of candidate 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) Write PL/SQL procedure to do the following: Set the status of course to offered in which the number of candidate are at least 10 otherwise not offered. Create the following tables 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)Create a report to list the items whose qty_in_stock is less than or equal to their reorder levels. Create the following table : Item (item-code, item-name, qty-in-stock, reorder-level) Supplier (supplier-code, supplier-name, address)

65

69

73

77

81

84

23.

24.

25.

26.

27.

Can-Supply (supplier-code, item-code) (a)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. 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) (a)Create a form to accept the data from the user with appropriate validation checks (b)Write PL/SQL procedure to do the following (i) Generate a report of those items that are supplied by those suppliers whose status isimportant. 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)Write a PL/SQL procedure to do the following (i)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,status) Faculty(faculty_code,faculty_name,specialization) (a)Write a 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. create the following tables Student(roll_no,name,subject_opted) Subject_rank(subject_code,subject_name,faculty_code,status) Faculty(faculty_code,faculty_name,specialization) (a)Write a 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. create the following tables Student(roll_no,name,subject_opted) Subject_rank(subject_code,subject_name,faculty_code) Faculty(faculty_code, faculty_name, specialization) (a)Generate the queries to do the following (i) Find the number of students who have enrolled for the subject DBMS.

88

92

94

97

100

28.

29.

30.

(ii)Find all those subjects which are not offered by any faculty member. create the following tables 103 Student(roll_no,name,subject_opted) Subject_rank(subject_code,subject_name,faculty_code) Faculty(faculty_code, faculty_name, specialization) (a)Generate the 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. create the following tables 106 Student(roll_no,name,subject_opted) Subject_rank(subject_code,subject_name,faculty_code) Faculty(faculty_code, faculty_name, specialization) (a)Generate the queries to do the following (i)Find the number of students who have enrolled for the subject OS. (ii)Find all those students who have opted for more than 5 subjects. create the following tables 109 Student(roll_no,name,subject_opted) Subject_rank(subject_code,subject_name,faculty_code) Faculty(faculty_code, faculty_name, specialization) (a)Generate the 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.

S.No
1. 2. 3. Steps To Create A Form

Program
Steps For Creating A Data Source

Page No
112 114 121

Steps To Generate A Report

1. (a)Create a table to represent sb_account of a bank consisting of account_no, customer-name , balance-amount. 6

(b)Write a PL/SQL block to implement deposit and withdraw. Withdraw should not be allowed if the balance goes below Rs.1000. PROGRAM: Creating Tables: SQL> create table sb_account 2 ( ACC_NO varchar(10) primary key, 3 cust_name varchar(15), 4 balance number(10)); Table created. Inserting values in to table: SQL> insert into sb_account values 2 ('&ACC_NO','&cust_name',&balance); Enter value for acc_no: 101 Enter value for cust_name: abcd Enter value for balance: 15000 old 2: ('&ACC_NO','&CUST_NAME','&BALANCE') new 2: ('101','abcd','15000') 1 row created. SQL> / Enter value for acc_no: 102 Enter value for cust_name: defg Enter value for balance: 1000 old 2: ('&ACC_NO','&CUST_NAME','&BALANCE') new 2: ('102','defg','1000') 1 row created. SQL> select * from sb_account; ACC_NO -----------101 102 103 104 CUST_NAME ----------------abcd defg fhdkd dfhdl BALANCE -------------15000 1000 2000 25000

(b) PL/SQL Code: SQL> declare

2 bal number(10); 3 acno varchar(10); 4 min_bal constant number(7,2):=1000.00; 5 amount number(10); 6 ch number(1); 7 begin 8 dbms_output.put_line(to_char(1)||'.deposit'); 9 dbms_output.put_line(to_char(2)||'.Withdraw'); 10 dbms_output.put_line('enter choice'); 11 ch:=&ch; 12 acno:=&acno; 13 select balance into bal from sb_account where acc_no=acno; 14 amount:=&amount; 15 if(ch=1) then 16 update sb_account set balance=balance+amount where acc_no=acno; 17 dbms_output.put_line('deposited'); 18 else 19 if (bal-amount)<min_bal then 20 dbms_output.put_line('cant withdraw'); 21 else 22 update sb_account set balance=balance-amount where acc_no=acno; 23 dbms_output.put_line('withdrawn'); 24 end if; 25 end if; 26* end; SQL> / Enter value for ch: 1 old 11: ch:=&ch; new 11: ch:=1; Enter value for acno: 101 old 12: acno:=&acno; new 12: acno:=101; Enter value for amount: 1000 old 14: amount:=&amount; new 14: amount:=1000; 1.deposit 2.Withdraw enter choice deposited PL/SQL procedure successfully completed. SQL> select * from sb_account;

ACC_NO CUST_NAME ---------- --------------- ---------101 abcd 16000 102 defg 1000 103 fhdkd 2000 104 dfhdl 25000

BALANCE

SQL>/ (reexecuting yhe same PL/SQL code) Enter value for ch: 2 old 11: ch:=&ch; new 11: ch:=2; Enter value for acno: 102 old 12: acno:=&acno; new 12: acno:=102; Enter value for amount: 1000 old 14: amount:=&amount; new 14: amount:=1000; enter choice 1.deposit 2.Withdraw cant withdraw PL/SQL procedure successfully completed. SQL> / (reexecuting yhe same PL/SQL code) Enter value for ch: 2 old 11: ch:=&ch; new 11: ch:=2; Enter value for acno: 103 old 12: acno:=&acno; new 12: acno:=103; Enter value for amount: 1000 old 14: amount:=&amount; new 14: amount:=1000; enter choice 1.deposit 2.Withdraw withdrawn PL/SQL procedure successfully completed. SQL> select * from sb_account;

ACC_NO CUST_NAME ---------- --------------- ---------101 abcd 16000 102 defg 1000 103 fhdkd 1000 104 dfhdl 25000

BALANCE

2. Create the following two tables: College-info

10

College-info College-info consists of fields : college-code, college-name, address Faculty-info consists of fields : college-code, faculty-code, faculty-name, qualification, college-code is foreign key. (a) 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. Program: Creating tables: SQL>create table college_info. (college_code number(5) primary key, college_name varchar(15), address varchar(15)); Table created. SQL>create table faculty_info 2 ( faculty_code number(5) primary key, 3 college_code number(5), 4 faculty_name varchar(15), 5 qualification varchar(15), 6 experience number(2), 7 address varchar(15), 8constraints fk foreign key(college_code) references college_info( college_code )); Table created. Inserting Values Into Tables: SQL> insert into college_info values 2 (&college_code,'&college_name','&address'); Enter value for college_code: 101 Enter value for college_name: xyz Enter value for address: old alwal 1; old 2: (&college_code,'&college_name','&address') new 2: (101,'xyz','old alwal 1;') 1 row created. SQL> / Enter value for college_code: 202

11

Enter value for college_name: abc Enter value for address: old alwal 2; old 2: (&college_code,'&college_name','&address') new 2: (202,'abc','old alwal 2;') 1 row created SQL> insert into faculty_info values (&faculty_code,&college_code,'&faculty_name','&qualification','&experience','&ad dress') Enter value for faculty_code: 1111 Enter value for college_code: 101 Enter value for faculty_name: ssss Enter value for qualification: M.Tech Enter value for experience: 10 Enter value for address: zzzzzzz old 2: (&faculty_code,&college_code,'&faculty_name','&qualification','&experience','&address' ) new 2: (1111,101,'ssss','M.Tech','10','zzzzzzz') 1 row created. SQL> / Enter value for faculty_code: 2222 Enter value for college_code: 202 Enter value for faculty_name: yyyy Enter value for qualification: M.Phil Enter value for experience: 20 Enter value for address: xxxxxx old 2: (&faculty_code,&college_code,'&faculty_name','&qualification','&experience','&address' ) new 2: (2222,202,'yyyy','M.Phil','20','xxxxxx') 1 row created.

SQL> select * from college_info;

12

COLLEGE_CODE ---------------------101 202 303 404 505

COLLEGE_NAME --------------------xyz abc def ghi klm

ADDRESS ----------------old alwal 1; old alwal 2; old alwal 3; old alwal 4; old alwal 5;

SQL> select * from faculty_info; FACTY_C0 COLLE_CODE FATY_NAME QUALIF -------------- ------------------ --------------------------1111 101 ssss M.Tech 2222 202 yyyy M.Phil 3333 303 dddd M.Tech 4444 404 ffff B.Sc 55555 505 rrrr M.Tech (a) Query: (i)SQL> select faculty_name from faculty_info where experience>=10 and qualification='M.Tech); Output: FACULTY_NAME -------------------ssss dddd (ii) Query: SQL> select faculty_name from faculty_info where experience>10 and qualification!= 'M.Tech'); Output: FACULTY_NAME -----------------------yyyy EXPERI -------10 20 15 5 5 ADDRESS ------------zzzzzzz xxxxxx ccccc vvvvv qqqqq

3. Create the following tables for library-information systems : Book: (Accession -no, title, publisher, author, status)

13

Status could be issued, present, in the library, dent for binding, and cannot be issued. Write a trigger which sets the status of a book to cannot be issued , if it is published 20 years back. PROGRAM: Creating Tables: SQL> create table libr_info 2 ( accession_no varchar(4) primary key, 3 title varchar(8), 4 published date, 5 author varchar(5), 6 status varchar(14)); Table created Inserting Values Into Table: . SQL> insert into libr_info values 2 ('&accession_no','&title','&publisher','&author','&status'); Enter value for accession_no: mc24 Enter value for title: icg Enter value for published: 06-mar-1985 Enter value for author: ghij Enter value for status: issued old 2: ('&accession_no','&title','&publisher','&author','&status') new 2: ('mc24','icg','06-mar-1985','ghij','issued') 1 row created. SQL> / Enter value for accession_no: mc25 Enter value for title: dcn Enter value for published: 06-apr-2005 Enter value for author: ijkl Enter value for status: cannot issue old 2: ('&accession_no','&title','&publisher','&author','&status') new 2: ('mc25','dcn','06-apr-2005','ijkl','cannot issue') 1 row created.

SQL> select * from libr_info;

14

ACCE -------mc21 mc22 mc23 mc24 mc25

TITLE -------dbms or os icg dcn

PUBLISHED ---------------06-JAN-86 06-FEB-00 06-SEP-99 06-MAR-85 06-APR-05

AUTHO --------abcd cdef efgh ghij ijkl

STATUS ----------binding present issued issued cannot issue

SQL> create or replace trigger t03 before insert or update on libr_info for each row begin if (:new.published<'01-jan-1989') then update libr_info set status='cant issue'; end if; end; SQL> / Trigger created. insert into libr_info * ERROR at line 1: ORA-04098: trigger 'SCOTT. LIBR_TRIGGER' is invalid and failed re-validation ORA-06512: at "SCOTT.T03.line 3 ORA-04088: error during execution of trigger 'SCOTT.T03

4. Create the following tables for library-information systems : Book : (Accession- no, title, publisher, author, status, date-of -purchase)

15

Status could be issued, present, in the library, dent for binding, and cannot be issued. (a) Generate queries to do the following : (i) List all those books which are new arrivals. The books which are requires during the last 6 months are categorized as new arrivals. (ii) List all those books that cannot be issued and purchased 20 years ago. PROGRAM: Creating table: SQL> create table library_info 2 (accession_no varchar(7) primary key, 3 title varchar(15), 4 publisher varchar(10), 5 author varchar(10), 6 status varchar(10) 7 date_of_purcahse date; Table created. Inserting into table: SQL>insert into library_info values ('&accession_no','& title','&publisher','&author','&status','&date_of_purcahse' ); Enter value for accession_no: mc1122 Enter value for title: dbms Enter value for publisher: tata Enter value for author: ramakrsih Enter value for status: new Enter value for date_of_purcahse: 06-mar-08 old 2: ('&accession_no','& title','&publisher','&author','&status','&date_of_purcahse' ) new 2: ('mc1122','dbms','tata','ramakrsih','new','06-mar-08' ) 1 row created. SQL> / Enter value for accession_no: mc5566 Enter value for title: c++ Enter value for publisher: hhhhh Enter value for author: aaaaa Enter value for status: isuued Enter value for date_of_purcahse: 12-sep-1986 old 2: ('&accession_no','& title','&publisher','&author','&status','&date_of_purcahse' ) new 2: ('mc5566','c++','hhhhh','aaaaa','isuued','12-sep-1986' )

16

1 row created. SQL> select * from library_info; ACCESSI TITLE ------- --------------------mc1122 dbms mc2233 os mc3344 or mc4455 icg mc5566 c++ mc6677 eit 6 rows selected. (a) Queries: (i) SQL> select * from library_info where(months_between(sysdate,date_of_purcahse)<=6) ACCESSION_NO TITLE PUBLISHER AUTHOR STATUS DATE_OF_PURCAHSE ---------------------- -------- ---------------- ----------- - --------- -----------------------------mc3344 or ssss wwwww binding 25-SEP-08 mc4455 icg ggggg xxxxx issued 01-AUG-08 SQL> select * from library_info where(months_between(sysdate,date_of_purcahse)/12>=20); ACCESSI -----------mc5566 mc6677 TITLE -------c++ eit PUBLISHER AUTHOR ----------------- -----------hhhhh aaaaa jjjjj lllll STATUS -----------isuued issued DATE_OF_P ---------- --------12-SEP-86 15-JAN-85 PUBLISHER ---------------tata Mcgrahill ssss ggggg hhhhh jjjjj AUTHOR -----------ramakrsih Galvin wwww xxxxx aaaaa lllll STATUS -----------present present binding issued issued issued DATE_OF_P ----------------06-MAR-08 05-JAN-05 25-SEP-08 01-AUG-08 12-SEP-86 15-JAN-85

5. Create the following tables: Student (roll-no, name, date-of-birth, course-id)

17

Course (course-id, name, fee, duration) (a) Create a form to accept the data from the user with appropriate validation checks (b) Generate the 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 grater than that of MCA course. PROGRAM: Creating tables: SQL> create table course 2 (course_id varchar(6) primary key, 3 name varchar(10), 4 fee number(6), 5 duration number(2)); Table created. Inserting into values: SQL> insert into course values 2 ('&course_id','& name ',&fee ,&duration ); Enter value for course_id: 123 Enter value for name: M.C.A Enter value for fee: 25400 Enter value for duration: 3 old 2: ('&course_id','& name ',&fee ,&duration ) new 2: ('123','M.C.A ',25400 ,3 ) 1 row created. SQL> / Enter value for course_id: 456 Enter value for name: B.Tech Enter value for fee: 36000 Enter value for duration: 4 old 2: ('&course_id','& name ',&fee ,&duration ) new 2: ('456','B.Tech ',36000 ,4 ) 1 row created.

SQL> select * from course;

18

COURSE -----------123 456 789 012 156

NAME -------M.C.A B.Tech M.B.A Bio-Tech Food-Tech

FEE DURATION ------- --------------25400 3 36000 4 20000 2 40000 3 10000 3

SQL> create table student 2 (roll_no varchar(5) primary key, 3 name varchar(5), 4 date_of_birth date, 5 course_id varchar(5), 6 constraints fk1 foreign key(course_id) references course(course_id)); Table created. SQL> insert into student values 2 ('&roll_no','& name','& date_of_birth','&course_id'); Enter value for roll_no: nmca1 Enter value for name: aaaa Enter value for date_of_birth: 01-jan-1986 Enter value for course_id: 123 old 2: ('&roll_no','& name','& date_of_birth','&course_id') new 2: ('nmca1','aaaa','01-jan-1986','123') 1 row created. SQL> / Enter value for roll_no: nmca2 Enter value for name: bbbb Enter value for date_of_birth: 25-feb-2000 Enter value for course_id: 456 old 2: ('&roll_no','& name','& date_of_birth','&course_id') new 2: ('nmca2','bbbb','25-feb-2000','456') 1 row created.

SQL> select * from student;

19

SQL> select * from student; ROLL_ --------nmca1 nmca2 nmca3 nmca4 nmca5 nmca6 NAME -------aaaa bbbb cccc dddd eeee ffff DATE_OF_B ---------------01-JAN-86 25-FEB-00 30-MAR-87 15-APR-99 08-MAY-87 26-JUN-83 COURS ------------123 456 123 123 789 012

6 rows selected. (a) Queries:(i) SQL>select * from student where(months_between(sysdate,date_of_birth)/12>18)and course_id='123' ROLL_ -------nmca1 nmca3 NAME --------aaaaa ccccc DATE_OF_B COURS ---------------- ---------10-JAN-86 123 30-MAR-87 123

(ii) SQL> select * from course where fee>25400; COURSE ----------456 012 NAME ---------B.Tech Bio-Tech FEE DURATION ------- --------------36000 4 40000 3

6. Create the following table : Student(roll-no,name subject-opted)

20

Subject(faculty-code, faculty-name, specialization) (a) Generate queries to do the following : (i) Find the number of students who haveenrolles for the subject DBMS (ii) Find all those faculty members who have not offered any subject. PROGRAM: Creating the table: SQL> create table subject_06 2 (faculty_code varchar(5), 3 faculty_name varchar(5), 4 specilization varchar(5) primary key); Table created. SQL> create table student_06 2 (roll_no varchar(5) primary key, 3 name varchar(5), 4 subject_opted varchar(5), 5 constraints fk2 foreign key(subject_opted) references subject_06(specilization)); Table created. Inserting the values into table: SQL> insert into subject_06 values 2 ('&faculty_code','&faculty_name','&specilization'); Enter value for faculty_code: mca01 Enter value for faculty_name: abcde Enter value for specilization: dbms old 2: ('&faculty_code','&faculty_name','&specilization') new 2: ('mca01','abcde','dbms') 1 row created. SQL> / Enter value for faculty_code: mca04 Enter value for faculty_name: pqrst Enter value for specilization: icg old 2: ('&faculty_code','&faculty_name','&specilization') new 2: ('mca04','pqrst','icg') 1 row created. SQL> insert into student_06 values 2 ('&roll_no','&name ','& subject_opted');

21

Enter value for roll_no: mc001 Enter value for name: abc Enter value for subject_opted: dbms old 2: ('&roll_no','&name ','& subject_opted') new 2: ('mc001','abc ','dbms') 1 row created. SQL> / Enter value for roll_no: mc002 Enter value for name: def Enter value for subject_opted: os old 2: ('&roll_no','&name ','& subject_opted') new 2: ('mc002','def ','os') 1 row created. SQL> select * from subject_06; FACUL ----------mca01 mca02 mca03 mca04 mca05 FACUL ----------abcde fghij klmno pqrst uvwxy SPECI --------dbms co os icg or

SQL> select * from student_06; ROLL_ ---------mc001 mc002 mc003 mc004 mc005 NAME ---------abc def ghi klm nop SUBJE ---------dbms os or dbms dbms

(a) Queries:

22

(i)SQL> select * from student_06 where subject_opted='dbms'; ROLL_ --------mc001 mc004 mc005 NAME ---------abc klm nop SUBJE ---------dbms dbms dbms

(ii) SQL> select * from subject_06 where specilization not in (select subject_opted from student_06); FACUL --------mca02 mca04 FACUL ---------fghij pqrst SPECI --------co icg (or) SQL> select * from subject_06 where specilization!= all('dbms','os','or'); FACUL ----------mca02 mca04 FACUL ----------fghij pqrst SPECI ---------co icg

7. Create the following table : Item (item-code, item-name, qty-in-stock, reorder-level)

23

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) 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. PROGRAM: Creating the tables: SQL> create table item 2 (item_code varchar(5) primary key, 3 item_name varchar(6), 4 qty_in_stock number(3), 5 reorder_level number(3)); Table created. SQL> create table supplier 2 (supplier_code varchar(5) primary key, 3 supplier_name varchar(6), 4 address varchar(10)); Table created. SQL> create table can_supply (supplier_code varchar(5), item_code varchar(5), constraints fk3 foreign key(supplier_code) references supplier(supplier_code), constraints fk4 foreign key(item_code) references item(item_code)); Table created. Inserting the values into tables: SQL> insert into item values 2 ('&item_code','& item_name ',&qty_in_stock,&reorder_level); Enter value for item_code: i303 Enter value for item_name: drives Enter value for qty_in_stock: 30 Enter value for reorder_level: 18 old 2: ('&item_code','&item_name','&qty_in_stock','&reorder_level') new 2: ('i303','drives','30','18') 1 row created.

24

SQL> / Enter value for item_code: i404 Enter value for item_name: keys Enter value for qty_in_stock: 35 Enter value for reorder_level: 20 old 2: ('&item_code','&item_name','&qty_in_stock','&reorder_level') new 2: ('i404','keys','35','20') 1 row created. SQL> insert into supplier values 2 ('&SUPPLIER_CODE','&SUPPLIER_NAME','&ADDRESS'); Enter value for supplier_code: s111 Enter value for supplier_name: abcd Enter value for address: xxxx old 2: ('&supplier_code','&supplier_name','&address') new 2: ('s111','abcd','xxxx') 1 row created. SQL> / Enter value for supplier_code: s222 Enter value for supplier_name: efgh Enter value for address: yyyy old 2: ('&supplier_code','&supplier_name','&address') new 2: ('s222','efgh','yyyy') 1 row created. SQL> insert into can_supply values 2 ('&supplier_code','&item_code'); Enter value for supplier_code: s222 Enter value for item_code: i202 old 2: ('&supplier_code','&item_code') new 2: ('s222','i202') 1 row created. SQL> / Enter value for supplier_code: s333 Enter value for item_code: i303 old 2: ('&supplier_code','&item_code') new 2: ('s333','i303') 1 row created. SQL> select * from item;

25

ITEM_ -------i101 i202 i303 i404 i505

ITEM_N ----------cpu mouse drives keys pens

QTY_IN_STOCK --------------------25 20 30 35 21

REORDER_LEVEL ----------------------5 15 18 20 13

SQL> select * from supplier; SUPPL -------s111 s222 s333 s444 s555 (a) Queries: (i) SQL> select * from supplier where supplier_code = (select supplier_code from can_supply where item_code = (select item_code from item where item_name='drives')); SUPPL --------s333 SUPPLI ---------ijkl ADDRESS ------------zzzz SUPPLI ----------abcd efgh ijkl mnop rstu ADDRESS -------------xxxx yyyy zzzz wwww vvvv

(ii) SQL> select * from item where item_code not in (select item_code from can_supply where supplier_code = ( select supplier_code from supplier where supplier_name='ijkl')); ITEM_ ------i101 i202 i404 i505 ITEM_N --------cpu mouse keys pens QTY_IN_STOCK ---------------------25 20 35 21 REORDER_LEVEL ------------------------5 15 20 13

8. Create the following table:

26

Student(roll-no,marks,category, district, state) Student-rank(roll-no, marks,rank) (a) Generate queries to do the following : (i) List all those students who have come from tamilnadu state ans secured a rank above 100. (ii) List all those students who have come from Anshra Pradesh state and belong to given category who have secured a rank above 100. PROGRAM: Creating the tables: SQL>create table student_07 2 (roll_no varchar(6) primary key, 3 marks number(3), 4 category varchar(15), 5 district varchar(10), 6 state varchar(18)); Table created. SQL> create table student_rank 2 (roll_no varchar(6), 3 marks number(3), 4 rank number(5), 5 constraints fk_7 foreign key(roll_no) references student_07(roll_no)); Table created. Inserting values into tables: SQL> insert into student_07 values 2 ('&roll_no','&marks','&category','&district','&state'); Enter value for roll_no: nmca01 Enter value for marks: 150 Enter value for category: apptitude Enter value for district: chennai Enter value for state: tamilnadu old 2: ('&roll_no','&marks','&category','&district','&state') new 2: ('nmca01','150','apptitude','chennai','tamilnadu') 1 row created. SQL>/ Enter value for roll_no: nmca05 Enter value for marks: 350 Enter value for category: reasoning

27

Enter value for district: kurnool Enter value for state: andhrapradesh old 2: ('&roll_no','&marks','&category','&district','&state') new 2: ('nmca05','350',' reasoning','kurnool','andhrapradesh') 1 row created. SQL> insert into student_rank values 2 ('&roll_no','&marks','&rank'); Enter value for roll_no: nmca01 Enter value for marks: 150 Enter value for rank: 50 old 2: ('&roll_no','&marks','&rank') new 2: ('nmca01','150','7') 1 row created. SQL> / Enter value for roll_no: nmca02 Enter value for marks: 200 Enter value for rank: 150 old 2: ('&roll_no','&marks','&rank') new 2: ('nmca02','200','150') 1 row created SQL> select * from student_07; ROLL_N ----------nmca01 nmca02 nmca03 nmca04 nmca05 nmca06 nmca07 MARKS ----------150 200 250 300 350 400 450 CATEGORY ----------------apptitude apptitude reasoning reasoning reasoning apptitude apptitude DISTRICT ------------chennai mailadu trivan hyd kurnool guntur vizag STATE ---------tamilnadu tamilnadu tamilnadu andhrapradesh andhrapradesh andhrapradesh andhrapradesh

7 rows selected.

SQL> select * from student_rank;

28

ROLL_N ----------nmca01 nmca02 nmca03 nmca04 nmca05 nmca06 nmca07

MARKS ----------150 200 250 300 350 400 450

RANK ---------400 350 300 250 200 150 50

7 rows selected. (a) Queries: (i) select * from student_07 where state='tamilnadu' and roll_no in (select roll_no from student_rank where rank>100); ROLL_N ----------nmca02 nmca03 MARKS ---------200 250 CATEGORY ---------------apptitude reasoning DISTRICT -------------mailadu trivan STATE --------tamilnadu tamilnadu

(ii) SQL> select * from student_07 where state='andhrapradesh' and category='apptitude' and roll_no in (select roll_no from student_rank where rank>100); ROLL_N ----------nmca06 MARKS ---------400 CATEGORY ---------------apptitude DISTRICT -------------guntur STATE -----------andhrapradesh

9. Create the following table:

29

Branch (branch-id, branch-name, branch-city) 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 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. PROGRAM: Creating the tables: SQL> create table branch_09 2 (branch_id varchar(6) primary key, 3 branch_name varchar(8), 4 branch_city varchar(15)): Table created. SQL> create table customer_09 2 (customer_id varchar(6) primary key, 3 customer_name varchar(10), 4 customer_city varchar(15), 5 branch_id varchar(6), 6constraints fk09 foreign key(branch_id) references branch_09(branch_id)); Table created. SQL> insert into branch_09 values 2 ('&branch_id','&branch_name','&branch_city'); Enter value for branch_id: sbh01 Enter value for branch_name: sbh Enter value for branch_city: hyd old 2: ('&branch_id','&branch_name','&branch_city') new 2: ('sbh01','sbh','hyd') 1 row created. SQL> / Enter value for branch_id: sbh02 Enter value for branch_name: sbh Enter value for branch_city: hyd old 2: ('&branch_id','&branch_name','&branch_city') new 2: ('sbh02','sbh','hyd')

30

SQL> insert into customer_09 values 2 ('&customer_id','&customer_name','&customer_city','&branch_id'); Enter value for customer_id: cus01 Enter value for customer_name: abcde Enter value for customer_city: hyd Enter value for branch_id: sbh01 old 2: ('&customer_id','&customer_name','&customer_city','&branch_id') new 2: ('cus01','abcde','hyd','sbh01') 1 row created. SQL> / Enter value for customer_id: cuso2 Enter value for customer_name: fghij Enter value for customer_city: kurnool Enter value for branch_id: sbh02 old 2: ('&customer_id','&customer_name','&customer_city','&branch_id') new 2: ('cuso2','fghij','kurnool','sbh02') 1 row created. SQL> select * from branch_09; BRANCH ------------sbh01 sbh02 sbh03 icci01 icci02 icci03 6 rows selected. SQL> select * from customer_09; CUSTOM -----------cus01 cuso2 cus03 cus04 cus05 cus06 CUSTOMER_N --------------------abcde fghij klmno pqrst uvwxy sjfkdl CUSTOMER_CITY -----------------------hyd kurnool hyd xxxx hyd kurnool BRANCH --------------sbh01 sbh02 sbh03 icci01 icci02 icci03 BRANCH_N ---------------sbh sbh sbh icci icci icci BRANCH_CITY --------------------hyd hyd kurnool hyd hyd kurnool

6 rows selected. (a) Queries:

31

(i) select * from customer_09 where branch_id in (select branch_id from branch_09 where branch_city=customer_city); CUSTOM -----------cus01 cus05 cus06 CUSTOMER_N -------------------abcde uvwxy sjfkdl CUSTOMER_CITY -----------------------hyd hyd kurnool BRANCH ------------sbh01 icci02 icci03

(ii) SQL> select * from customer_09 where branch_id in (select branch_id from branch_09 where branch_city='kurnool'); CUSTOM -----------cus03 cus06 CUSTOMER_N ------------------klmno sjfkdl CUSTOMER_CITY -----------------------hyd kurnool BRANCH -----------sbh03 icci03

10. Create the following tables:

32

Book (accession no, title, publisher, year, date-of-purchase, status) Member (member-id, number-of-books-issued, max-limit) Book-issue (accession-no, member-id, date-of-issue) (b) Generate the following to do the following: (i) List all those books which are due from the students to be returned. A book is considered to be isuued 15 days back and yet not returned. (ii) List all those members who cannot be issued any more books PROGRAM: Creating the tables: SQL> create table book_10 2 (accession_no varchar(7) primary key, 3 title varchar(10), 4 publisher varchar(10), 5 year date, 6 date_of_purchase date, 7 status varchar(8)); Table created. SQL> create table memeber 2 (memeber_id varchar(5) primary key, 3 name varchar(8), 4 number_of_books_issued number(2), 5 max_limit number(2)); Table created. SQL> create table book_issue 2 (accession_no varchar(7), 3 memeber_id varchar(5), 4 date_of_issue date, 5 constraints fk_10 foreign key(accession_no) references book_10(accession_no), 6 constraints fk10 foreign key(memeber_id) references memeber(memeber_id)); Table created. Inserting values into table: SQL> insert into book_10 values 2 ('&ACCESSION_NO','&TITLE','&PUBLISHER','&YEAR','& DATE_OF_PURCHASE','&STATUS'); Enter value for accession_no: mca2110 Enter value for title: dbms Enter value for publisher: tata

33

Enter value for year: 06-sep-2005 Enter value for date_of_purchase: 08-oct-2006 Enter value for status: issued old 2: ('&ACCESSION_NO','&TITLE','&PUBLISHER','&YEAR','& DATE_OF_PURCHASE','&STATUS') new 2: ('mca2110','dbms','tata','06-sep-2005','08-oct-2006','issued') 1 row created. SQL> / Enter value for accession_no: mca2115 Enter value for title: c++ Enter value for publisher: lll Enter value for year: 19-dec-2000 Enter value for date_of_purchase: 20-apr-2001 Enter value for status: notissue old 2: ('&ACCESSION_NO','&TITLE','&PUBLISHER','&YEAR','& DATE_OF_PURCHASE','&STATUS') new 2: ('mca2115','c++','lll','19-dec-2000','20-apr-2001','notissue') 1 row created. SQL> insert into memeber values 2 ('&MEMEBER_ID','&NAME','&NUMBER_OF_BOOKS_ISSUED','&MAX_LIMIT'); Enter value for memeber_id: mc21 Enter value for name: xxxxx Enter value for number_of_books_issued: 2 Enter value for max_limit: 3 old 2: ('&MEMEBER_ID','&NAME','&NUMBER_OF_BOOKS_ISSUED','&MAX_LIMIT') new 2: ('mc21','xxxxx','2','3') 1 row created SQL> / Enter value for memeber_id: mc22 Enter value for name: yyyyy Enter value for number_of_books_issued: 3 Enter value for max_limit: 3 old 2: ('&MEMEBER_ID','&NAME','&NUMBER_OF_BOOKS_ISSUED','&MAX_LIMIT') new 2: ('mc22','yyyyy','3','3') 1 row created. SQL> insert into book_issue values

34

2 ('&ACCESSION_NO','&MEMEBER_ID','&DATE_OF_ISSUE'); Enter value for accession_no: mca2114 Enter value for memeber_id: mc25 Enter value for date_of_issue: 27-sep-2008 old 2: ('&ACCESSION_NO','&MEMEBER_ID','&DATE_OF_ISSUE') new 2: ('mca2114','mc25','27-sep-2008') 1 row created. SQL> / Enter value for accession_no: mca2115 Enter value for memeber_id: mc26 Enter value for date_of_issue: 15-sep-2008 old 2: ('&ACCESSION_NO','&MEMEBER_ID','&DATE_OF_ISSUE') new 2: ('mca2115','mc26','15-sep-2008') 1 row created. SQL> select * from book_10; ACCESSI -----------mca2110 mca2111 mca2112 mca2113 mca2114 mca2115 TITLE -------dbms os or icg dcn c++ PUBLISHER YEAR DATE_OF_PUR --------------------------------------------tata 06-SEP-05 08-OCT-06 galvin 01-JAN-01 01-FEB-01 sharma 02-MAR-99 06-DEC-00 xxxxx 05-APR-95 06-MAY-96 yyyyy 06-OCT-96 08-NOV-97 ll 19-DEC-00 20-APR-01 STATUS ---------issued issued notissue issued issued notissue

6 rows selected. SQL> select * from memeber; MEMEB ---------mc21 mc22 mc33 mc24 mc25 mc26 NAME NUMBER_OF_BOOKS_ISSUED --------- -----------------------------------------xxxxx 2 yyyyy 3 llllll 2 mmmmm 3 rrrrr 3 pppp 3 MAX_LIMIT -----------------3 3 3 3 3 4

6 rows selected. SQL> select * from book_issue;

35

ACCESSI MEMEB ------- ----- --------mca2110 mc21 mca2111 mc22 mca2112 mc33 mca2113 mc24 mca2114 mc25 mca2115 mc26 6 rows selected. (b) Queries:

DATE_OF_I 01-OCT-08 20-SEP-08 06-OCT-08 25-SEP-08 27-SEP-08 15-SEP-08

(i)SQL> select * from book_issue where (months_between(sysdate,date_of_issue)*30>15); ACCESSI -----------mca2111 mca2115 MEMEB ----------mc22 mc26 DATE_OF_ISSUE ----------------------20-SEP-08 15-SEP-08

(ii) SQL> select * from memeber where number_of_books_issued=max_limit; MEMEB ----------mc22 mc24 mc25 NAME NUMBER_OF_BOOKS_ISSUED -------- -----------------------------------------yyyyy 3 mmmmm 3 rrrrr 3 MAX_LIMIT ----------------3 3 3

11. Create the following tables: Book (accession no, title, publisher, year, date-of-purchase, status)

36

Member (member-id, number-of-books-issued, max-limit) Book-issue (accession-no, member-id, date-of-issue) (b) Write a PL/SQL procedure to issue the book. Write a trigger to set the status of student to back listed if they have taken book but not returned even one year. PROGRAM: Creating Tables: SQL> create table book_11 2 (accession_no varchar(7) primary key, 3 title varchar(10), 4 publisher varchar(10), 5 year date, 6 date_of_purchase date, 7 status varchar(8)); Table created. SQL> create table member 2 (memeber_id varchar(5) primary key, 3 name varchar(8), 4 number_of_books_issued number(2), 5 max_limit number(2)); Table created. SQL> create table book_issue 2 (accession_no varchar(7), 3 memeber_id varchar(5), 4 date_of_issue date, 5 constraints fk_10 foreign key(accession_no) references book_10(accession_no), 6 constraints fk10 foreign key(memeber_id) references memeber(memeber_id)); Table created Inserting values into tables: SQL> insert into book_11 values 2 ('&accession_no','&title','&publisher','&year','&date_of_purchase','&status'); Enter value for accession_no: mca2110 Enter value for title: dbms Enter value for publisher: tata Enter value for year: 06-sep-2005 Enter value for date_of_purchase: 08-oct-2001 Enter value for status: issued

37

old 2: ('&accession_no','&title','&publisher','&year','&date_of_purchase','&status') new 2: ('mca2110','dbms','tata','06-sep-2005','08-oct-2001','issued') 1 row created. SQL> / Enter value for accession_no: mca2111 Enter value for title: or Enter value for publisher: galvin Enter value for year: 01-jan-2001 Enter value for date_of_purchase: 01-feb-2001 Enter value for status: issued old 2: ('&accession_no','&title','&publisher','&year','&date_of_purchase','&status') new 2: ('mca2111','or','galvin','01-jan-2001','01-feb-2001','issued') 1 row created. SQL> insert into member values 2 ('&MEMBER_ID','&NAME','&NUMBER_OF_BOOKS_ISSUED','&MAX_LIMIT'); Enter value for member_id: mc21 Enter value for name: xxxxx Enter value for number_of_books_issued: 2 Enter value for max_limit: 3 old 2: ('&MEMBER_ID','&NAME','&NUMBER_OF_BOOKS_ISSUED','&MAX_LIMIT') new 2: ('mc21','xxxxx','2','3') 1 row created. SQL> / Enter value for member_id: mc22 Enter value for name: yyyyy Enter value for number_of_books_issued: 3 Enter value for max_limit: 3 old 2: ('&MEMBER_ID','&NAME','&NUMBER_OF_BOOKS_ISSUED','&MAX_LIMIT') new 2: ('mc22','yyyyy','3','3') 1 row created. SQL> insert into book_issue_11 values 2 ('&ACCESSION_NO','&MEMBER_ID','&DATE_OF_ISSUE'); Enter value for accession_no: mca2110 Enter value for member_id: mc21 Enter value for date_of_issue: 01-oct-2007

38

old 2: ('&ACCESSION_NO','&MEMBER_ID','&DATE_OF_ISSUE') new 2: ('mca2110','mc21','01-oct-2007') 1 row created. SQL> / Enter value for accession_no: mca2111 Enter value for member_id: mc22 Enter value for date_of_issue: 20-sep-2008 old 2: ('&ACCESSION_NO','&MEMBER_ID','&DATE_OF_ISSUE') new 2: ('mca2111','mc22','20-sep-2008') SQL> select * from book_11; ACCESSI -----------mca2110 mca2111 mca2112 mca2113 mca2114 mca2115 TITLE ------dbms or os icg dcn c++ PUBLISHER ----------------tata galvin sharma xxxxx yyyyy llll YEAR DATE_OF_P STATUS ----------------------- ----------06-SEP-05 08-OCT-01 issued 01-JAN-01 01-FEB-01 issued 02-MAR-99 06-DEC-00 notissue 05-APR-95 06-MAY-96 issued 06-OCT-96 08-NOV-97 issued 19-DEC-00 20-APR-01 notissue

6 rows selected. SQL> select * from member; MEMBE ---------mc21 mc22 mc23 mc24 mc25 mc26 NAME NUMBER_OF_BOOKS_ISSUED -----------------------------------------------xxxxx 2 yyyyy 3 lllll 2 mmmmm 3 rrrrr 3 ppppp 3 MAX_LIMIT ---------------3 3 3 3 3 4

6 rows selected

SQL> select * from book_issue_11;

39

ACCESSI -----------mca2110 mca2111 mca2112 mca2113 mca2114 mca2115

MEMBE ----------mc21 mc22 mc23 mc23 mc24 mc25

DATE_OF_I ---------------01-OCT-07 20-SEP-08 06-OCT-08 25-SEP-07 27-SEP-07 15-SEP-08

6 rows selected. PROCEDURE: SQL>create or replace procedure p11 is ac_no book_11.accession_no%type; sta book_11.status%type; bookissued exception; begin ac_no:='&ac_no'; select status into sta from book_11 where accession_no=ac_no; if(sta='notissue') then update book_11 set status='issued' where accession_no=ac_no; dbms_output.put_line('book issued'); else raise bookissued; end if; exception when bookissued then dbms_output.put_line('already book has been issued'); end; SQL>/ Enter value for ac_no: mca2112 old 6: ac_no:='&ac_no'; new 6: ac_no:='mca2112'; book issued SQL>exec p11; Book issued. 3L/SQL procedure successfully completed.

40

select * from book_11; ACCESSI -----------mca2110 mca2111 mca2112 mca2113 mca2114 mca2115 TITLE ---------dbms or os icg dcn c++ PUBLISHER ---------------tata galvin sharma xxxxx yyyyy llll YEAR DATE_OF_P -------- ----------------06-SEP-05 08-OCT-01 01-JAN-01 01-FEB-01 02-MAR-99 06-DEC-00 05-APR-95 06-MAY-96 06-OCT-96 08-NOV-97 19-DEC-00 20-APR-01 STATUS ----------issued issued issued issued issued notissue

6 rows selected. Trigger: SQL> create or replace trigger t12 before insert or update on book_issue for each row begin if(:new.date_of_issue<'01-jan-2007') then update book_11 set status='back listed'; end if; end; SQL> / Trigger created. Execution of trigger: SQL> insert into book_issue values ('mca2156','mc27','01-dec-2006') SQL> / insert into book_issue * ERROR at line 1: ORA-04098: trigger 'SCOTT.BOOKTRIGGER' is invalid and failed re-validation ORA-06512: at "SCOTT.T12", line 3 ORA-04088: error during execution of trigger 'SCOTT.T12'

12. Create the following Book (accession-no, title, publisher, year, date-of-purchase)

41

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-purchase) (a) Write a PL/SQL procedure to issue the book (b) Write a trigger to set the status of the book neither to lost which is neither issued nor in the library. PROGRAM: Creating Tables: SQL> create table book_12 2 (accession_no varchar(7) primary key, 3 title varchar(10), 4 publisher varchar(8), 5 year number(4), 6 date_of_purchase date, 7 status varchar(11)); Table created. SQL> create table book_place_12 2 (accession_no varchar(7), 3 rack_id varchar(5), 4 rack_position varchar(5), 5 constraints fkk foreign key(accession_no) references book_12(accession_no)); Table created. SQL> 2 3 4 5 6 create table member_12 (member_id varchar(7) primary key, name varchar(5), no_of_bokks_issued number(2), max_limit number(2), status varchar(11));

Table created. SQL> create table book_issue_12 2 (accession_no varchar(7), 3 member_id varchar(7), 4 date_of_issue date, 5 constraints fk12 foreign key(accession_no) references book_12(accession_no), 6 constraints fk_12 foreign key(member_id) references member_12(member_id)); Table created.

42

Inserting Values into table: (i) Into table book_12 SQL> insert into book_12 values 2 ('&accession_no','&title','&publisher',&year,'&date_of_purchase','&status'); Enter value for accession_no: mca2113 Enter value for title: icg Enter value for publisher: wwwww Enter value for year: 2005 Enter value for date_of_purchase: 13-oct-2006 Enter value for status: issued old 2: ('&accession_no','&title','&publisher',&year,'&date_of_purchase','&status') new 2: ('mca2113','icg','wwwww',2005,'13-oct-2006','issued') 1 row created. SQL> / Enter value for accession_no: mca2114 Enter value for title: dcn Enter value for publisher: gggg Enter value for year: 2007 Enter value for date_of_purchase: 25-jan-2007 Enter value for status: not issued old 2: ('&accession_no','&title','&publisher',&year,'&date_of_purchase','&status') new 2: ('mca2114','dcn','gggg',2007,'25-jan-2007','not issued') 1 row created. SQL> select * from book_12; ACCESSI -----------mca2110 mca2111 mca2112 mca2113 mca2114 mca2115 TITLE -------dbms os or icg dcn c++ PUBLISHE -------------tata xxxx yyyy wwwww gggg jjjj YEAR -------2000 1995 2001 2005 2007 1995 DATE_OF_P ---------------06-OCT-01 15-NOV-96 14-OCT-01 13-OCT-06 25-JAN-07 09-SEP-96 STATUS ---------issued not issued not issued issued not issued not issued

6 rows selected.

43

(ii) Into book_place_12 SQL> insert into book_place_12 values 2 ('&ACCESSION_NO','&RACK_ID','&RACK_POSITION'); Enter value for accession_no: mca2110 Enter value for rack_id: 01 Enter value for rack_position: 11 old 2: ('&ACCESSION_NO','&RACK_ID','&RACK_POSITION') new 2: ('mca2110','01','11') 1 row created. SQL> / Enter value for accession_no: mca2111 Enter value for rack_id: 02 Enter value for rack_position: 12 old 2: ('&ACCESSION_NO','&RACK_ID','&RACK_POSITION') new 2: ('mca2111','02','12') 1 row created. SQL> select * from book_place_12; ACCESSI RACK_ ID RACK_POSI --------------------------------------mca2110 01 11 mca2111 02 12 mca2112 03 00 mca2113 04 14 mca2114 05 01 mca2115 06 15 6 rows selected. (iii) Into table member_12 SQL> insert into member_12 values ('&MEMBER_ID','&NAME',&NO_OF_BOKKS_ISSUED,&MAX_LIMIT,'&STAT US'); Enter value for member_id: m1211 Enter value for name: abcd Enter value for no_of_bokks_issued: 2 Enter value for max_limit: 4 Enter value for status: can issue

44

old 2: ('&MEMBER_ID','&NAME',&NO_OF_BOKKS_ISSUED,&MAX_LIMIT,'&STATUS') new 2: ('m1211','abcd',2,4,'can issue') 1 row created. SQL> / Enter value for member_id: mc1212 Enter value for name: efgh Enter value for no_of_bokks_issued: 4 Enter value for max_limit: 4 Enter value for status: cant issue old 2: ('&MEMBER_ID','&NAME',&NO_OF_BOKKS_ISSUED,&MAX_LIMIT,'&STATUS') new 2: ('mc1212','efgh',4,4,'cant issue') 1 row created. SQL> select * from member_12; MEMBER_ NAME NO_OF_BOKKS_ISSUED MAX_LIMIT STATUS ------- ----- ------------------ ---------- ----------mc1211 abcd 2 4 can issue mc1212 defg 4 4 cant issue mc1213 efgh 4 4 cant issue mc1214 ghij 1 3 can issue mc1215 jklm 0 4 can issue mc1216 mnop 3 4 can issue 6 rows selected.. (iv) Into book-issue SQL> insert into book_issue_12 values 2 ('&accession_no','&member_id','&date_of_issue'); Enter value for accession_no: mca2110 Enter value for member_id: m1211 Enter value for date_of_issue: 01-sep-2008 old 2: ('&accession_no','&member_id','&date_of_issue') new 2: ('mca2110','m1211','01-sep-2008') 1 row created.

45

SQL> / Enter value for accession_no: mca2111 Enter value for member_id: mc1212 Enter value for date_of_issue: 05-sep-2008 old 2: ('&accession_no','&member_id','&date_of_issue') new 2: ('mca2111','mc1212','05-sep-2008') 1 row created. SQL> select * from book_issue_12; ACCESSI -----------mca2110 mca2111 mca2112 mca2113 mca2114 mca2115 MEMBER_ -------------m1211 mc1212 mc1213 mc1214 mc1215 mc2116 DATE_OF_I ---------------01-SEP-08 05-SEP-08 01-OCT-08 08-SEP-08 05-OCT-08 25-SEP-08

6 rows selected. (b) PROCEDURE: SQL> create or replace procedure p12 is 2 ac_no book_12.accession_no%type; 3 sta book_12.status%type; 4 bookissued exception; 5 begin 6 ac_no:='&ac_no'; 7 select status into sta from book_12 where accession_no=ac_no; 8 if(sta='not issued') then 9 update book_12 set status='issued' where accession_no=ac_no; 10 dbms_output.put_line('book issued'); 11 else 12 raise bookissued; 13 end if; 14 exception 15 when bookissued then 16 dbms_output.put_line('already book has been issued'); 17 end; 18 / Enter value for ac_no: mca2111 old 6: ac_no:='&ac_no'; new 6: ac_no:='mca2111';

46

Procedure created. SQL> exec p12; book issued PL/SQL procedure successfully completed. SQL> select * from book_12; ACCESSI TITLE PUBLISHE ------------- -------- --------------mca2110 dbms tata mca2111 os xxxx mca2112 or yyyy mca2113 icg wwwww mca2114 dcn gggg mca2115 c++ jjjj 6 rows selected. SQL>create or replace trigger t122 before insert or update on book_place_12 for each row begin if((:new.rack_position=00)) then update book_12 set status='lost'; end if; end; SQL> / Trigger created. YEAR -------2000 1995 2001 2005 2007 1995 DATE_OF_P ----------------06-OCT-01 15-NOV-96 14-OCT-01 13-OCT-06 25-JAN-07 09-SEP-96 STATUS -----------issued issued not issued issued not issued not issued

13. Create the following Book (accession-no, title, publisher, year, date-of-purchase)

47

Member (member-id, name, number-of-books-issued, max-limit, status) Book-issue (accession-no, member-id, date-of-purchase ,due-date) (a) Write a PL/SQL to list all thos estudents who are defaulters. A student is considered to be a defaulter if he has not returned a book even after the due-date (b) Write a trigger to set the status of the student toback listedif they have taken book but not returned even after one year. PROGRAM: Creating Tables: SQL> create table book_13 2 (accession_no varchar(7) primary key, 3 title varchar(10), 4 publisher varchar(8), 5 year number(4), 6 date_of_purchase date, 7 status varchar(11)); Table created. SQL> create table member_13 2 (member_id varchar(7) primary key, 3 name varchar(5), 4 no_of_books_issued number(2), 5 max_limit number(2), 6 status varchar(11)); Table created. SQL> create table book_issue_13 2 (accession_no varchar(7), 3 member_id varchar(7), 4 date_of_issue date, 5 Due_date date, 6 constraints fk13 foreign key(accession_no) references book_13(accession_no), 7 constraints fk_13 foreign key(member_id) references member_13(member_id)); Table created.

48

SQL> select * from book_13; ACCESSI TITLE PUBLISHE YEAR DATE_OF_P STATUS ------- ---------- -------- ---------- --------- ----------mca2110 dbms tata 2000 06-OCT-01 issued mca2111 os xxxx 1995 15-NOV-96 not issued mca2112 or yyyy 2001 14-OCT-01 not issued mca2113 icg wwwww 2005 13-OCT-06 issued mca2114 dcn gggg 2007 25-JAN-07 not issued mca2115 c++ jjjj 1995 09-SEP-96 not issued 6 rows selected. SQL> select * from member_13; MEMBER_ NAME NO_OF_BOOKS MAX_LIMIT STATUS ------- ----- ----------- ---------- ----------mc1211 abcd 2 4 can issue mc1212 defg 4 4 cant issue mc1213 efgh 4 4 cant issue mc1214 ghij 1 3 can issue mc1215 jklm 0 4 can issue mc1216 mnop 3 4 can issue 6 rows selected. SQL> select * from book_issue_13; ACCESSI MEMBER_ DATE_OF_I DUE_DATE ------- ------- --------- --------mca2110 mc1211 01-SEP-07 01-SEP-08 mca2111 mc1212 05-SEP-08 10-OCT-08 mca2112 mc1213 01-OCT-05 30-OCT-07 mca2113 mc1214 08-SEP-08 28-SEP-08 mca2114 mc1215 05-OCT-08 28-OCT-08 mca2115 mc1216 25-SEP-07 25-SEP-08 6 rows selected.

Procedure:

49

SQL> declare 2 cursor c13 is select * from book_issue_13 where months_between(sysdate,date_of_isue)/12>1; 3 str_ac_no book_issue_13.accession_no%type; 4 str_m_id book_issue_13.member_id%type; 5 dt_issuedate book_issue_13.date_of_isue%type; 6 dt_due book_issue_13.due_date%type; 7 begin 8 dbms_output.put_line('DEFAULTERS'); 9 dbms_output.put_line('acession_no member_id date_of_issue due_date '); 10 open c13; 11 loop 12 fetch c13 into str_ac_no,str_m_id,dt_issuedate,dt_due ; 13 dbms_output.put_line(str_ac_no ||str_m_id ||dt_issuedate ||dt_due); 14 exit when c13 % notfound; 15 end loop; 16 close c13; 17* end; SQL> / DEFAULTERS acession_no member_id mca2110 mc1211 mca2112 mc1213 mca2115 mc1216 date_of_issue 01-SEP-07 01-OCT-05 25-SEP-07 due_date 01-SEP-08 30-OCT-07 25-SEP-08

PL/SQL procedure successfully completed. ] Trigger: SQL> create or replace trigger t13 before insert or update on book_issue for each row begin if(:new.date_of_issue<'01-jan-2007') then update book_13 set status='back listed'; end if; end; SQL> / Trigger created. Execution of trigger:

50

SQL> insert into book_issue values ('mca2156','mc27','01-dec-2006') SQL> / insert into book_issue * ERROR at line 1: ORA-04098: trigger 'SCOTT.BOOKTRIGGER' is invalid and failed re-validation ORA-06512: at "SCOTT.T12", line 3 ORA-04088: error during execution of trigger 'SCOTT.T12'

14. Create the following tables: Branch (branch-id, branch-name, branch_-city)

51

Customer (customer-id, customer-name, customer-city, branch-id) (b) 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. PROGRAM: Creation of tables: SQL> create table branch15 (branch_id number(5) primary key, branch_name varchar(15), branch_city varchar(15)); Table created. SQL> create table customer15 (customer_id number(5) primary key, customer_name varchar(15), customer_city varchar(15)); Table created. SQL> create table branchcust15 (branch_id number(5), customer_id number(5), constraint fk_15 foreign key(branch_id) references branch15(branch_id), constraint fk_1555 foreign key(customer_id) references customer15(customer_id)); Table created. Inserting values: SQL> insert into branch15 values (&branch_id,'&branch_name','&branch_city'); Enter value for branch_id: 001 Enter value for branch_name: sbh Enter value for branch_city: hyd old 2: (&branch_id,'&branch_name','&branch_city') new 2: (001,'sbh','hyd') 1 row created. SQL> / Enter value for branch_id: 002 Enter value for branch_name: andhra

52

Enter value for branch_city: vizag old 2: (&branch_id,'&branch_name','&branch_city') new 2: (002,'andhra','vizag') 1 row created. SQL> insert into customer15 values (&customer_id,'&customer_name','&customer_city'); Enter value for customer_id: 101 Enter value for customer_name: abcd Enter value for customer_city: khamam old 2: (&customer_id,'&customer_name','&customer_city') new 2: (101,'abcd','khamam') 1 row created. SQL> / Enter value for customer_id: 102 Enter value for customer_name: defg Enter value for customer_city: hyd old 2: (&customer_id,'&customer_name','&customer_city') new 2: (102,'defg','hyd') 1 row created SQL> insert into branchcust15 values (&branch_id,&customer_id); Enter value for branch_id: 10 Enter value for customer_id: 101 old 2: (&branch_id,&customer_id) new 2: (10,101) 1 row created. SQL> / Enter value for branch_id: 10 Enter value for customer_id: 102 old 2: (&branch_id,&customer_id) new 2: (10,102) 1 row created.

SQL> select * from branch15;

53

BRANCH_ID BRANCH_NAME ---------- --------------- --------------10 sbh 11 icici 12 andhra 13 indian 14 hdfc SQL> select * from customer15; CUSTOMER_ID -------------------101 102 103 104 105 106 107 108 109 9 rows selected. SQL> select * from branchcust15; BRANCH_ID -----------------10 101 11 102 12 105 13 106 14 108 11 101 12 101 13 105 14 105 12 105 10 rows selected. CUSTOMER_ID -------------------CUSTOMER_NAME ------ ------------------abcd defg fghi ijkl lmno opqr stuv wxyz afrg

BRANCH_CITY khamam hyd secun vizag kurnool

CUSTOMER_CITY ----------------------khamam hyd secun kurnool vizag hyd secun secun secun

(b) Queries:

54

(i) select * from customer15 where customer_id in (select customer_id from branchcust15 where branch_id in (select branch_id from branch15 where customer_city=branch_city)); CUSTOMER_ID CUSTOMER_NAME CUSTOMER_CITY -------------------------------------------------------------------= 101 abcd khamam 102 defg hyd 105 lmno vizag (ii) select * from customer15 where customer_id in (select customer_id from branchcust15 group by customer_id having count(branch_id)>1); CUSTOMER_ID -------------------101 105 CUSTOMER_NAME --------------------------abcd lmno CUSTOMER_CITY -----------------------khamam vizag

15. Create the following tables: Branch (branch-id, branch-name, branch_-city)

55

Customer (customer-id, customer-name, customer-city, branch-id) (b) Generate queries to do the following: (i)List all those branches who have more than 2 customers. (ii) List all those customers who have an account in more than one branch. PROGRAM: Creation of tables: SQL> create table branch15 (branch_id number(5) primary key, branch_name varchar(15), branch_city varchar(15)); Table created. SQL> create table customer15 (customer_id number(5) primary key, customer_name varchar(15), customer_city varchar(15)); Table created. SQL> create table branchcust15 (branch_id number(5), customer_id number(5), constraint fk_15 foreign key(branch_id) references branch15(branch_id), constraint fk_1555 foreign key(customer_id) references customer15(customer_id)); Table created. Inserting values: SQL> insert into branch15 values (&branch_id,'&branch_name','&branch_city'); Enter value for branch_id: 001 Enter value for branch_name: sbh Enter value for branch_city: hyd old 2: (&branch_id,'&branch_name','&branch_city') new 2: (001,'sbh','hyd') 1 row created. SQL> / Enter value for branch_id: 002 Enter value for branch_name: andhra Enter value for branch_city: vizag

56

old 2: (&branch_id,'&branch_name','&branch_city') new 2: (002,'andhra','vizag') 1 row created. SQL> insert into customer15 values (&customer_id,'&customer_name','&customer_city'); Enter value for customer_id: 101 Enter value for customer_name: abcd Enter value for customer_city: khamam old 2: (&customer_id,'&customer_name','&customer_city') new 2: (101,'abcd','khamam') 1 row created. SQL> / Enter value for customer_id: 102 Enter value for customer_name: defg Enter value for customer_city: hyd old 2: (&customer_id,'&customer_name','&customer_city') new 2: (102,'defg','hyd') 1 row created SQL> insert into branchcust15 values (&branch_id,&customer_id); Enter value for branch_id: 10 Enter value for customer_id: 101 old 2: (&branch_id,&customer_id) new 2: (10,101) 1 row created. SQL> / Enter value for branch_id: 10 Enter value for customer_id: 102 old 2: (&branch_id,&customer_id) new 2: (10,102) 1 row created.

SQL> select * from branch15; BRANCH_ID BRANCH_NAME BRANCH_CITY

57

---------- --------------- --------------10 sbh 11 icici 12 andhra 13 indian 14 hdfc SQL> select * from customer15; CUSTOMER_ID -------------------101 102 103 104 105 106 107 108 109 9 rows selected. SQL> select * from branchcust15; BRANCH_ID -----------------10 101 11 102 12 105 13 106 14 108 11 101 12 101 13 105 14 105 12 105 CUSTOMER_ID -------------------CUSTOMER_NAME ------ ------------------abcd defg fghi ijkl lmno opqr stuv wxyz afrg

khamam hyd secun vizag kurnool

CUSTOMER_CITY ----------------------khamam hyd secun kurnool vizag hyd secun secun secun

(i)select * from branch15 where branch_id in (select branch_id from branchcust15 group by branch_id having count(customer_id)>2); BRANCH_ID ---------- ------12 BRANCH_NAME BRANCH_CITY --------- -------------- --------------andhra secun

58

(ii) select * from customer15 where customer_id in (select customer_id from branchcust15 group by customer_id having count(branch_id)>1); CUSTOMER_ID -------------------101 105 CUSTOMER_NAME --------------------------abcd lmno CUSTOMER_CITY -----------------------khamam vizag

16. Create the following table:

59

Student (roll-no, name, category, district, state) Student-rank (roll-no, marks, rank) (b) Generate queries to do the following : (i) List names of the students who are having same rank bur they should reside in different districts. (ii) List details of students they belongs to same category with same tank. PROGRAM: Creation of tables: SQL> create table student16 2 (roll_no number(5) primary key, 3 name varchar(15), 4 category varchar(10), 5 district varchar(15), 6 state varchar(5)); Table created. SQL> create table student_rank16 2 (roll_no number(5), 3 marks number(5), 4 rank number(3), 5 constraint fk_16 foreign key(roll_no)references student16(roll_no)); Table created. Inserting Values: SQL> insert into student16 values (&roll_no,'&name','&category','&district','&state'); Enter value for roll_no: 09 Enter value for name: abc Enter value for category: maths Enter value for district: rrr Enter value for state: vvv old 2: (&roll_no,'&name','&category','&district','&state') new 2: (09,'abc','maths','rrr','vvv') 1 row created. SQL> / Enter value for roll_no: 10 Enter value for name: def

60

Enter value for category: maths Enter value for district: xxx Enter value for state: yyy old 2: (&roll_no,'&name','&category','&district','&state') new 2: (10,'def','maths','xxx','yyy') 1 row created. . SQL> insert into student_rank16 values (&roll_no,&marks,&rank); Enter value for roll_no: 09 Enter value for marks: 200 Enter value for rank: 25 old 2: (&roll_no,&marks,&rank) new 2: (09,200,25) 1 row created. SQL> / Enter value for roll_no: 10 Enter value for marks: 150 Enter value for rank: 50 old 2: (&roll_no,&marks,&rank) new 2: (10,150,50) 1 row created. SQL> select * from student16; ROLL_NO ------------9 10 11 12 13 14 15 NAME -------abc def ghi jkl mno pqr stu CATEGORY ---------------maths maths apptitude apptitude reasoning reasoning apptitude DISTRICT ------------rrr xxx zzz ttt xxx sss www STATE --------vvv yyy bbb ppp yyy ccc qqq

7 rows selected

61

SQL> select * from student_rank16; ROLL_NO ------------9 10 11 12 13 14 15 MARKS ---------200 150 150 300 150 400 150 RANK -------25 50 50 20 50 10 50

create view A as select category,count(category) from student group by category having count(category)>1; create view B as select student.rollno,A.category from student,A where student.category=a.category; create view C as select rank,count(rank) from student_rank group by rank having count(rank)>1; create view D as select student_rank.rollno,C.rank from student,C where student_rank.rank=C.rank; create view E as select B.rollno,B.category,D.rank where B.rollno=D.rollno; select student.rollno,student.name,student.district,student.state,E.category,E.rank where student.rollno=E.rollno;

62

17. Create the following tables:

63

Student (roll-no, name, date-of-birth, course-id) Course (course-id, name, fee, duration) (b) Generate queries to do the following: (i) List all those student who are between 10-19 years of age and have opted for MCA course. (ii) List all those courses in which number of students are less than 10. PROGRAM: Creation of tables: SQL> create table course17 2 ( course_id number(5) primary key, 3 name varchar(15), 4 fee number(10), 5 duration number(3)); Table created. SQL> create table student17 2 ( roll_no number(5) primary key, 3 name varchar(15), 4 date_of_birth date, 5 course_id number(5), 6 constraints fk17 foreign key(course_id)references course17(course_id)); Table created. Inserting values into tables: SQL> insert into course17 values 2 (&course_id,'&name',&fee,&duration); Enter value for course_id: 101 Enter value for name: MCA Enter value for fee: 24700 Enter value for duration: 3 old 2: (&course_id,'&name',&fee,&duration) new 2: (101,'MCA',24700,3) 1 row created.

SQL> / Enter value for course_id: 102

64

Enter value for name: MBA Enter value for fee: 20000 Enter value for duration: 2 old 2: (&course_id,'&name',&fee,&duration) new 2: (102,'MBA',20000,2) 1 row created. SQL> / Enter value for course_id: 103 Enter value for name: MSc Enter value for fee: 15000 Enter value for duration: 2 old 2: (&course_id,'&name',&fee,&duration) new 2: (103,'MSc',15000,2) 1 row created. SQL> insert into student17 values 2 (&roll_no,'&name','&date_of_birth',&course_id); Enter value for roll_no: 27 Enter value for name: rs Enter value for date_of_birth: 06-oct-1986 Enter value for course_id: 103 old 2: (&roll_no,'&name','&date_of_birth',&course_id) new 2: (27,'rs','06-oct-1986',103) 1 row created. SQL> / Enter value for roll_no: 28 Enter value for name: st Enter value for date_of_birth: 06-JUN-95 Enter value for course_id: 103 old 2: (&roll_no,'&name','&date_of_birth',&course_id) new 2: (28,'st','06-JUN-95',103) 1 row created.

SQL> select * from course17;

65

COURSE_ID ---------------101 102 103

NAME --------MCA MBA MSc

FEE ----24700 20000 15000

DURATION --------------3 2 2

SQL> select * from student17; ROLL_NO ------------1 2 3 11 12 13 19 20 21 22 23 24 25 26 27 28 16 rows selected. SQL> select * from course17; COURSE_ID ---------------101 102 103 QUERIES: (i) SQL> select * from student17 where roll_no in 2 (select roll_no from student17 where 3 (months_between(sysdate,date_of_birth)/12 between 18 and 19) ) 4* and course_id = (select course_id from course17 where name='MCA'); ROLL_NO NAME DATE_OF_B COURSE_ID NAME --------MCA MBA MSc FEE -----24700 20000 15000 DURATION ---------------3 2 2 NAME --------ab bc cd ef fg gh ij kl lm mn no op pq qr rs st DATE_OF_B ----------------01-JAN-90 02-FEB-02 03-MAR-88 04-APR-85 05-MAY-02 06-JUN-95 07-JUL-86 08-AUG-89 07-JUL-86 08-AUG-89 01-JAN-99 02-FEB-00 03-MAR-98 04-APR-96 06-OCT-86 06-JUN-95 COURSE_ID --------------101 101 101 101 102 102 103 103 103 103 103 103 103 103 103 103

66

-------------1 11

---------ab ef

----------------01-JAN-90 04-APR-90

---------------101 101

(ii) select * from course17 where course_id in (select course_id from student17 group by course_id having count(roll_no)<10); COURSE_ID ---------------101 102 NAME --------MCA MBA FEE ------24700 20000 DURATION --------------3 2

67

18. Create the following tables: Student (roll_no, name,date_of_birth,course-id) Course (course-id, name, fee, duration, status) (b) Write PL/SQL procedure to do the following: Set the status of course to not offered in which the number of candidate is less than 5. PROGRAM: Creation of tables: SQL> create table course18 2(course_id number(5) primary key, 3 name varchar(15), 4 fee number(10), 5 duration number(3), 6 status varchar(12)); Table created. SQL> create table student18 2( roll_no number(5) primary key, 3 name varchar(15), 4 date_of_birth date, 5 course_id number(5), 6 constraints fk18 foreign key(course_id)references course17(course_id)); Table created. Inserting values into tables: SQL> insert into course18 values 2 (&course_id,'&name',&fee,&duration); Enter value for course_id: 101 Enter value for name: MCA Enter value for fee: 24700 Enter value for duration: 3 old 2: (&course_id,'&name',&fee,&duration) new 2: (101,'MCA',24700,3) 1 row created.

68

SQL> / Enter value for course_id: 102 Enter value for name: MBA Enter value for fee: 20000 Enter value for duration: 2 old 2: (&course_id,'&name',&fee,&duration) new 2: (102,'MBA',20000,2) 1 row created.

SQL> insert into student17 values 2 (&roll_no,'&name','&date_of_birth',&course_id); Enter value for roll_no: 27 Enter value for name: rs Enter value for date_of_birth: 06-oct-1986 Enter value for course_id: 103 old 2: (&roll_no,'&name','&date_of_birth',&course_id) new 2: (27,'rs','06-oct-1986',103) 1 row created. SQL> / Enter value for roll_no: 28 Enter value for name: st Enter value for date_of_birth: 06-JUN-95 Enter value for course_id: 103 old 2: (&roll_no,'&name','&date_of_birth',&course_id) new 2: (28,'st','06-JUN-95',103) 1 row created. SQL> select * from course18; COURSE_ID ---------------101 102 103 NAME --------MCA MBA MSc FEE ------24700 20000 15000 DURATION --------------3 2 2 STATUS ----------offered offered offered

SQL> select * from student18;

69

ROLL_NO ------------1 2 3 11 12 13 19 20 21 22 23 24 25 26 27 28 16 rows selected. PROCEDURE:

NAME --------ab bc cd ef fg gh ij kl lm mn no op pq qr rs st

DATE_OF_B ----------------01-JAN-90 02-FEB-02 03-MAR-88 04-APR-85 05-MAY-02 06-JUN-95 07-JUL-86 08-AUG-89 07-JUL-86 08-AUG-89 01-JAN-99 02-FEB-00 03-MAR-98 04-APR-96 06-OCT-86 06-JUN-95

COURSE_ID --------------101 101 101 101 102 102 103 103 103 103 103 103 103 103 103 103

SQL> declare 2 c_id course18.course_id%type; 3 ctr number; 4 cursor c18 is select course_id from course18; 5 begin 6 open c18; 7 loop 8 fetch c18 into c_id; 9 select count(roll_no) into ctr from student18 where course_id=c_id; 10 if(ctr<5) then 11 update course18 set status='not offered' where course_id=c_id; 12 dbms_output.put_line('tables has been updated'); 13 end if; 14 exit when c18 % notfound; 15 end loop; 16 close c18; 17 end; 18 /

70

OUTPUT: tables has been updated tables has been updated PL/SQL procedure successfully completed. SQL>select * from course18; COURSE_ID ---------------101 102 103 NAME --------MCA MBA MSc FEE -------24700 20000 15000 DURATION --------------3 2 2 STATUS -----------not offered not offered offered

19. Create the following tables:

71

Student (roll_no, name,date_of_birth,course-id) Course (course-id, name, fee, duration, status) (b) Write PL/SQL procedure to do the following: Set the status of course to not offered in which the number of candidate is less than 5. PROGRAM: Creation of tables: SQL> create table course19 2(course_id number(5) primary key, 3 name varchar(15), 4 fee number(10), 5 duration number(3), 6 status varchar(12)); Table created. SQL> create table student19 2( roll_no number(5) primary key, 3 name varchar(15), 4 date_of_birth date, 5 course_id number(5), 6 constraints fk18 foreign key(course_id)references course17(course_id)); Table created. Inserting values into tables: SQL> insert into course19 values 2 (&course_id,'&name',&fee,&duration); Enter value for course_id: 101 Enter value for name: MCA Enter value for fee: 24700 Enter value for duration: 3 old 2: (&course_id,'&name',&fee,&duration) new 2: (101,'MCA',24700,3) 1 row created.

SQL> /

72

Enter value for course_id: 102 Enter value for name: MBA Enter value for fee: 20000 Enter value for duration: 2 old 2: (&course_id,'&name',&fee,&duration) new 2: (102,'MBA',20000,2) 1 row created.

SQL> insert into student19 values 2 (&roll_no,'&name','&date_of_birth',&course_id); Enter value for roll_no: 27 Enter value for name: rs Enter value for date_of_birth: 06-oct-1986 Enter value for course_id: 103 old 2: (&roll_no,'&name','&date_of_birth',&course_id) new 2: (27,'rs','06-oct-1986',103) 1 row created. SQL> / Enter value for roll_no: 28 Enter value for name: st Enter value for date_of_birth: 06-JUN-95 Enter value for course_id: 103 old 2: (&roll_no,'&name','&date_of_birth',&course_id) new 2: (28,'st','06-JUN-95',103) 1 row created. SQL> select * from course18; COURSE_ID ---------------101 102 103 NAME --------MCA MBA MSc FEE ------24700 20000 15000 DURATION --------------3 2 2 STATUS ----------offered offered offered

SQL> select * from student18;

73

ROLL_NO ------------1 2 3 11 12 13 19 20 21 22 23 24 25 26 27 28 16 rows selected. PROCEDURE:

NAME --------ab bc cd ef fg gh ij kl lm mn no op pq qr rs st

DATE_OF_B ----------------01-JAN-90 02-FEB-02 03-MAR-88 04-APR-85 05-MAY-02 06-JUN-95 07-JUL-86 08-AUG-89 07-JUL-86 08-AUG-89 01-JAN-99 02-FEB-00 03-MAR-98 04-APR-96 06-OCT-86 06-JUN-95

COURSE_ID --------------101 101 101 101 102 102 103 103 103 103 103 103 103 103 103 103

SQL> declare 2 c_id course18.course_id%type; 3 ctr number; 4 cursor c18 is select course_id from course18; 5 begin 6 open c18; 7 loop 8 fetch c18 into c_id; 9 select count(roll_no) into ctr from student18 where course_id=c_id; 10 if(ctr<5) then 11 update course18 set status='not offered' where course_id=c_id; 12 dbms_output.put_line('tables has been updated'); 13 end if; 14 exit when c18 % notfound; 15 end loop; 16 close c18; 17 end; 18 /

74

OUTPUT: tables has been updated tables has been updated PL/SQL procedure successfully completed. SQL>select * from course18; COURSE_ID ---------------101 102 103 NAME --------MCA MBA MSc FEE -------24700 20000 15000 DURATION --------------3 2 2 STATUS -----------not offered not offered offered

20. Create the following tables:

75

Student (roll-no, name, date_of_birth, course-id) Course (course-id, name, fee, duration, status) (b) Write PL/SQL procedure to do the following: Set the status of course to offered in which the number of candidate ist atleast 10 otherwise not offered. PROGRAM: Creation of tables : SQL> create table course20 2 (course_id number(5) primary key, 3 name varchar(15), 4 fee number(10), 5 duration number(3), 6 status varchar(12)); Table created. SQL> SQL> create table student20 2 ( roll_no number(5) primary key, 3 name varchar(15), 4 date_of_birth date, 5 course_id number(5), 6 constraints fk18 foreign key(course_id)references course17(course_id)); Table created. Inserting values into tables: SQL> insert into course20 values 2 (&course_id,'&name','&fee','&duration','&status') 3 ; Enter value for course_id: 101 Enter value for name: MCA Enter value for fee: 24700 Enter value for duration: 3 Enter value for status: old 2: (&course_id,'&name','&fee','&duration','&status') new 2: (101,'MCA','24700','3','') 1 row created. SQL> / Enter value for course_id: 102

76

Enter value for name: MBA Enter value for fee: 20000 Enter value for duration: 2 Enter value for status: old 2: (&course_id,'&name','&fee','&duration','&status') new 2: (102,'MBA','20000','2','') 1 row created. SQL> insert into student20 values 2 (&roll_no,'&name','&date_of_birth',&course_id); Enter value for roll_no: 27 Enter value for name: rs Enter value for date_of_birth: 06-oct-1986 Enter value for course_id: 103 old 2: (&roll_no,'&name','&date_of_birth',&course_id) new 2: (27,'rs','06-oct-1986',103) 1 row created. SQL> / Enter value for roll_no: 28 Enter value for name: st Enter value for date_of_birth: 06-JUN-95 Enter value for course_id: 103 old 2: (&roll_no,'&name','&date_of_birth',&course_id) new 2: (28,'st','06-JUN-95',103) 1 row created. SQL> select * from course20; COURSE_ID ---------------101 102 103 NAME --------MCA MBA MSc FEE ------24700 20000 15000 DURATION --------------3 2 2 STATUS -----------

77

SQL> select * from student20; ROLL_NO ------------1 2 3 11 12 13 19 20 21 22 23 24 25 26 27 28 16 rows selected. PROCEDURE: SQL> declare 2 c_id course18.course_id%type; 3 ctr number; 4 cursor c20 is select course_id from course20; 5 begin 6 open c20; 7 loop 8 fetch c20 into c_id; 9 select count(roll_no) into ctr from student20 where course_id=c_id; 10 if(ctr>=10) then 11 update course20 set status='offered' where course_id=c_id; 12 else 13 update course20 set status=' not offered' where course_id=c_id; 14 end if; 15 exit when c20 % notfound; 16 dbms_output.put_line('table has been updated'); 17 end loop; 18 close c20; 19 end; NAME --------ab bc cd ef fg gh ij kl lm mn no op pq qr rs st DATE_OF_B ----------------01-JAN-90 02-FEB-02 03-MAR-88 04-APR-85 05-MAY-02 06-JUN-95 07-JUL-86 08-AUG-89 07-JUL-86 08-AUG-89 01-JAN-99 02-FEB-00 03-MAR-98 04-APR-96 06-OCT-86 06-JUN-95 COURSE_ID --------------101 101 101 101 102 102 103 103 103 103 103 103 103 103 103 103

78

OUTPUT: table has been updated table has been updated table has been updated PL/SQL procedure successfully completed. SQL> select * from course20; COURSE_ID --------------------------101 102 103 NAME --------MCA MBA MSc FEE ------24700 20000 15000 DURATION ---------------3 2 2 STATUS not offered not offered offered

79

21. create the following tables 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)Create a report to list the items whose qty_in_stock is less than or equal to their reorder levels. Program: Creating Tables: SQL> create table item 2 (item_code varchar(5) primary key, 3 item_name varchar(6), 4 qty_in_stock number(3), 5 reorder_level number(3)); Table created. SQL> create table supplier 2 (supplier_code varchar(5) primary key, 3 supplier_name varchar(6), 4 address varchar(10)); Table created. SQL> create table can_supply (supplier_code varchar(5), item_code varchar(5), constraints fk3 foreign key(supplier_code) references supplier(supplier_code), constraints fk4 foreign key(item_code) references item(item_code)); Table created. SQL> select * from item; ITEM_ ITEM_N QTY_IN_STOCK REORDER_LEVEL ----- ------ ------------ ------------i101 cpu 25 5 i202 mouse 20 15 i303 drives 30 18 i404 keys 35 20 i505 pens 21 13 SQL> select * from supplier;

80

SUPPL SUPPLI ADDRESS ----- ------ ---------s111 abcd xxxx s222 efgh yyyy s333 ijkl zzzz s444 mnop wwww s555 rstu vvvv SQL> select * from can_supply; SUPPL ITEM_ ----- ----s111 i101 s222 i202 s333 i303 s444 i404 s555 i505

Creation of form *Steps for creation of form refer index The resulting form is as shown below

Creation of report

81

*Steps for creation of report refer index The resulting reportis as shown below

82

22.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) b) 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. PROGRAM: Creating tables: SQL> create table item22 2 (item_code number(5) primary key, 3 item_name varchar(15), 4 qty_in_stock number(5), 5 record_level number(5)); Table created. SQL> create table supplier22 2 (supp_code number(5) primary key, 3 supp_name varchar(15), 4 address varchar(15), 5 status varchar(15)); Table created. SQL> create table can_supply22 2 (supp_code number(5), 3 item_code number(5), 4 constraint fk22 foreign key(supp_code) references supplier22(supp_code), 5 constraint fh_22 foreign key(item_code) references item22(item_code)); Table created Inserting Values Into Tables: SQL> insert into item22 values 2 (&item_code,'&item_name',&qty_in_stock,&record_level); Enter value for item_code: 1111 Enter value for item_name: xxxx Enter value for qty_in_stock: 20 Enter value for record_level: 15

83

old 2: (&item_code,'&item_name',&qty_in_stock,&record_level) new 2: (1111,'xxxx',20,15) 1 row created. SQL> / Enter value for item_code: 2222 Enter value for item_name: yyyy Enter value for qty_in_stock: 15 Enter value for record_level: 10 old 2: (&item_code,'&item_name',&qty_in_stock,&record_level) new 2: (2222,'yyyy',15,10) 1 row created. SQL> insert into supplier22 values 2 (&supp_code,'&supp_name','&address','&status'); Enter value for supp_code: 1001 Enter value for supp_name: abcd Enter value for address: 1234 Enter value for status: processing old 2: (&supp_code,'&supp_name','&address','&status') new 2: (1001,'abcd','1234','processing') 1 row created. SQL> / Enter value for supp_code: 2002 Enter value for supp_name: defg Enter value for address: 4567 Enter value for status: processing old 2: (&supp_code,'&supp_name','&address','&status') new 2: (2002,'defg','4567','processing') 1 row created. SQL> select * from item22; ITEM_CODE -----------------1111 2222 3333 4444 5555 6666 ITEM_NAME ------------------xxxx yyyy zzzz aaaa bbbb cccc QTY_IN_STOCK --------------------20 15 10 25 15 7 RECORD_LEVEL ----------------------15 10 5 15 15 5

84

SQL> select * from supplier22; SUPP_CODE ----------------1001 2002 3003 SUPP_NAME -----------------abcd defg hijk ADDRESS -------------1234 4567 7890 STATUS -------------processing processing processing

SQL> insert into can_supply22 values 2 (&supp_code,&item_code); Enter value for supp_code: 1001 Enter value for item_code: 1111 old 2: (&supp_code,&item_code) new 2: (1001,1111) 1 row created. SQL> / Enter value for supp_code: 2002 Enter value for item_code: 2222 old 2: (&supp_code,&item_code) new 2: (2002,2222) 1 row created. SQL> select * from can_supply22; SUPP_CODE -----------------1001 2002 1001 3003 1001 3003 1001 2002 1001 1001 9 rows selected. ITEM_CODE -----------------1111 2222 2222 1111 3333 2222 4444 4444 5555 6666

85

PROCEDURE: SQL> declare 2 s_c supplier22.supp_code%type; 3 ctr number; 4 cursor c22 is select supp_code from supplier22; 5 begin 6 open c22; 7 loop 8 fetch c22 into s_c; 9 select count(item_code) into ctr from can_supply22 where supp_code=s_c; 10 if(ctr>5) then 11 update supplier22 set status='important' where supp_code=s_c; 12 dbms_output.put_line('table updated'); 13 end if; 14 exit when c22 % notfound; 15 end loop; 16 close c22; 1 17 end; 18 / Output: table updated PL/SQL procedure successfully completed. SQL> select * from supplier22; SUPP_CODE ---------------1001 2002 3003 SUPP_NAME ----------------abcd defg hijk ADDRESS --------------1234 4567 7890 STATUS ----------important processing processing

23.Create the following tables

86

Item(item_code,item_name,qty_in_stock,reorder_level) Supplier(supplier_code,supplier_name,address,status) Can_supply(supplier_code,item_code) Generate a report of those items that are supplied by those suppliers whose status is important Program: SQL> create table item22(item_code number(5) primary key,item_name varchar(15), qty_in_stock number(5),record_level number(5)); Table created. SQL> create table supplier22(supp_code number(5) primary key,supp_name varchar(15), address varchar(15),status varchar(15)); Table created. SQL> create table can_supply22(supp_code number(5),item_code number(5),constraint fk22 foreign key(supp_code) references supplier22(supp_code),constraint fh_22 foreign key(item_code) references item22(item_code)); Table created Inserting Values Into Tables SQL> insert into item22 values(&item_code,'&item_name',&qty_in_stock,&record_level); Enter value for item_code: 1111 Enter value for item_name: xxxx Enter value for qty_in_stock: 20 Enter value for record_level: 15 old 2: (&item_code,'&item_name',&qty_in_stock,&record_level) new 2: (1111,'xxxx',20,15) 1 row created.

SQL> / Enter value for item_code: 2222 Enter value for item_name: yyyy Enter value for qty_in_stock: 15 Enter value for record_level: 10 old 2: (&item_code,'&item_name',&qty_in_stock,&record_level)

87

new 2: (2222,'yyyy',15,10) 1 row created. SQL> insert into supplier22 values(&supp_code,'&supp_name','&address','&status'); Enter value for supp_code: 1001 Enter value for supp_name: abcd Enter value for address: 1234 Enter value for status: processing old 2: (&supp_code,'&supp_name','&address','&status') new 2: (1001,'abcd','1234','processing') 1 row created. SQL> / Enter value for supp_code: 2002 Enter value for supp_name: defg Enter value for address: 4567 Enter value for status: processing old 2: (&supp_code,'&supp_name','&address','&status') new 2: (2002,'defg','4567','processing') 1 row created. SQL> select * from item22; ITEM_CODE ITEM_NAME QTY_IN_STOCK RECORD_LEVEL ---------- --------------- ------------ -----------1111 xxxx 20 15 2222 yyyy 15 10 3333 zzzz 10 5 4444 aaaa 25 15 5555 bbbb 15 15 6666 cccc 7 5 SQL> select * from supplier22; SUPP_CODE SUPP_NAME ADDRESS ---------- --------------- --------------- --------------1001 abcd 1234 processing 2002 defg 4567 processing 3003 hijk 7890 processing SQL> insert into can_supply22 values 2 (&supp_code,&item_code); STATUS

88

Enter value for supp_code: 1001 Enter value for item_code: 1111 old 2: (&supp_code,&item_code) new 2: (1001,1111) 1 row created. SQL> / Enter value for supp_code: 2002 Enter value for item_code: 2222 old 2: (&supp_code,&item_code) new 2: (2002,2222) 1 row created. SQL> select * from can_supply22; SUPP_CODE ITEM_CODE ---------- ---------1001 1111 2002 2222 1001 2222 3003 1111 1001 3333 3003 2222 1001 4444 2002 4444 1001 5555 1001 6666 9 rows selected.

Procedure SQL> declare 2 s_c supplier23.supp_code%type; 3 ctr number; 4 cursor c23 is select supp_code from supplier23; 5 begin 6 open c23; 7 loop 8 fetch c23 into s_c;

89

9 select count(item_code) into ctr from can_supply23 where supp_code=s_c; 10 if(ctr>5) then 11 update supplier23 set status='important' where supp_code=s_c; 12 dbms_output.put_line('table updated'); 13 end if; 14 exit when c23 % notfound; 15 end loop; 16 close c23; 1 17 end; 18 / Output: table updated PL/SQL procedure successfully completed. SQL> select * from supplier22; SUPP_CODE SUPP_NAME -------------------------------1001 abcd 2002 defg 3003 hijk Creation of Report ADDRESS --------------1234 4567 7890 STATUS ----------important processing processing

*Steps for creation of Report same as given in Problem21. The report generated is as follows.

24.Create the following tables

90

Student(roll_no,marks,category,district,state) Student_rank(roll_no,marks,rank) Generate a report to list of those districts from which the first hundred rankers come from. Creating the tables SQL> Create table student8(roll_no number(4) primary key,marks number(4),category varchar(2),district varchar(15),state varchar(15));

ROLL_NO
6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798

MARKS
200 180 176 175 173 170 168 167 165 164 160 159 158 157 156 153 152 150 148 A A A A A A B B B B B B B B B B B C C

CATEGORY

DISTRICT
warangal ranga reddy nizambad nalgonda khammam adilabad tiruchhi kanchi kanchi drwad hubli erode erode hubli krishna guntur kadapa prakasham nellore

STATE
andhra pradesh andhra pradesh andhra pradesh andhra pradesh andhra pradesh andhra pradesh tamilnadu tamilnadu tamilnadu karnataka karnataka Tamilnadu Tamilnadu karnataka andhra pradesh andhra pradesh andhra pradesh andhra pradesh andhra pradesh

SQL> create table student_rank(roll_no number(4),marks number(4),rank number(3),constraints fk81 foreign key(roll_no) references student8(roll_no));

91

ROLL_NO
6781 6780 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 180 200 176 175 173 170 168 167 165 164 160 159 158 157 156 153 152 150 148

MARKS
2 1 8 10 25 32 41 59 72 84 100 110 129 143 164 170 179 184 197

RANK

Creation of Report Steps for creation of Report refer index The report generated is as follows.

25.Create the following tables Student(roll_no,name,subject_opted) Subject_rank(subject_code,subject_name,faculty_code,status) Faculty(faculty_code,faculty_name,specialization)

92

Write a 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.

Creating the table

SQL> Create table faculty25(faculty_code number(4) primary key,faculty_name varchar(25),specialization varchar(10));


FACULTY_CODE
5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692

FACULTY_NAME
Syed Nazia Begum M Suhasini S Zareena B Sujana Kumari K Balaramudu G Gopinath M Rama Krishna B Ramesh D.Anilkumar Reddy J.Haribabu S.Mahammad Goush L.Sudhakar O.Sivaprasad G.Manesh M.Nagaraju

SPECIALIZATION
M.Tech M.Sc M.com MCA MBA MA B.Sc B.Com M.Tech M.Sc M.com MCA BA B.Tech BE

SQL> Crete table student25(roll_no varchar(19),subject_opted varchar(15));


ROLL_NO
5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698

number(4)

primary

key,name

NAME
A.Nagedra B.Adinarayana B.Ayyawaru B.Dastagiri B.Ramakrishna B.Ramesh C.Jayaraju C.Rajashekar C.Subramanyam C.Venkatasiva D.Anilkumar Reddy D.Udaykiran

SUBJECT_OPTED
DBMS History Physics History Physics Physics Physics Physics DBMS Physics History History

93

5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735

G.Ganganna G.Gurappa G.Manesh G.Suresh G.Venkataramana J.Haribabu K.Diwakar Reddy K.Gourisankar K.Ramakrishna K.Ramakrishna K.Ramesh K.Ramesh K.Sankar Reddy K.Sivasankar L.Sudhakar M.Nagaraju M.Nagarjuna M.Raghavendra Reddy O.Sivaprasad P.Bashakar P.Moulali P.Nagaraju P.Rafi P.Sreeramulu P.Tirupalu S.Anwar Basha S.Baba S.Gangaraju S.Ibrahim S.Jilani Basha S.Mahammad Goush T.Babaiah T.Balagangi Reddy T.Sreenivasulu Y.Vijayaram N.jagan K.Hari Babu

DBMS Accounts Accounts History Accounts DBMS Physics DBMS OS History OS DBMS DBMS History DBMS Accounts Physics DBMS OS Accounts OS Physics OS Accounts OS OS OS OS DBMS Accounts OS History Accounts DBMS OS Electronics Chemistry

SQL> create table subject_rank25(subject_code number(4) primary key,subject_name varchar(15),faculty_code number(4),status varchar(18));
SUBJECT_CODE
9110 9111 9112 9113

SUBJECT_NAME
Accounts DBMS History OS

FACULTY_CODE
5688 5681 5683 5678

STATUS
Offered Offered Offered Offered

94

9114 9115 9116

Physics Electronics Chemistry

5687 0 0

Offered Offered Offered

Procedure
SQL> create or replace procedure PRO25

is z1 subject_rank25.subject_code%type; ctr number; cursor c1 is select subject_code from subject_rank25; begin open c1; loop fetch c1 into z1; select count(roll_no) into ctr from student25 where subject_opted=z1; if(ctr<5) then update subject_rank25 set status='Not offered' where subject_code=z1; end if; exit when c1%notfound; end loop; close c1; end; PL/SQL procedure successfully completed SQL> select * from subject_rank25;
SUBJECT_CODE
9110 9111 9112 9113 9114 9115 9116

SUBJECT_NAME
Accounts DBMS History OS Physics Electronics Chemistry

FACULTY_CODE
5688 5681 5683 5678 5687 0 0

STATUS
Offered Offered Offered Offered Offered Not offered Not offered

26.Create the following tables Student(roll_no,name,subject_opted) Subject_rank(subject_code,subject_name,faculty_code,status) Faculty(faculty_code,faculty_name,specialization) Write a PL/SQL procedure to the following: Set the status of the subject to not offered if the subject is not offered by any faculty 95

Creating the table

SQL> Create table faculty25(faculty_code number(4) primary key,faculty_name varchar(25),specialization varchar(10));


FACULTY_CODE
5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692

FACULTY_NAME
Syed Nazia Begum M Suhasini S Zareena B Sujana Kumari K Balaramudu G Gopinath M Rama Krishna B Ramesh D.Anilkumar Reddy J.Haribabu S.Mahammad Goush L.Sudhakar O.Sivaprasad G.Manesh M.Nagaraju

SPECIALIZATION
M.Tech M.Sc M.com MCA MBA MA B.Sc B.Com M.Tech M.Sc M.com MCA BA B.Tech BE

SQL> Crete table student25(roll_no varchar(19),subject_opted varchar(15));


ROLL_NO
5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703

number(4)

primary

key,name

NAME
A.Nagedra B.Adinarayana B.Ayyawaru B.Dastagiri B.Ramakrishna B.Ramesh C.Jayaraju C.Rajashekar C.Subramanyam C.Venkatasiva D.Anilkumar Reddy D.Udaykiran G.Ganganna G.Gurappa G.Manesh G.Suresh G.Venkataramana

SUBJECT_OPTED
DBMS History Physics History Physics Physics Physics Physics DBMS Physics History History DBMS Accounts Accounts History Accounts

96

5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735

J.Haribabu K.Diwakar Reddy K.Gourisankar K.Ramakrishna K.Ramakrishna K.Ramesh K.Ramesh K.Sankar Reddy K.Sivasankar L.Sudhakar M.Nagaraju M.Nagarjuna M.Raghavendra Reddy O.Sivaprasad P.Bashakar P.Moulali P.Nagaraju P.Rafi P.Sreeramulu P.Tirupalu S.Anwar Basha S.Baba S.Gangaraju S.Ibrahim S.Jilani Basha S.Mahammad Goush T.Babaiah T.Balagangi Reddy T.Sreenivasulu Y.Vijayaram N.jagan K.Hari Babu

DBMS Physics DBMS OS History OS DBMS DBMS History DBMS Accounts Physics DBMS OS Accounts OS Physics OS Accounts OS OS OS OS DBMS Accounts OS History Accounts DBMS OS Electronics Chemistry

SQL> create table subject_rank25(subject_code number(4) primary key,subject_name varchar(15),faculty_code number(4),status varchar(18));
SUBJECT_CODE
9110 9111 9112 9113 9114 9115 9116

SUBJECT_NAME
Accounts DBMS History OS Physics Electronics Chemistry

FACULTY_CODE
5688 5681 5683 5678 5687 0 0

STATUS
Offered Offered Offered Offered Offered Offered Offered

97

Procedure
SQL> create or replace procedure PRO25

is z1 subject_rank25.subject _code%type; fac subject_rank25.faculty_code%type; ctr number; cursor c1 is select faculty_code as fac from subject_rank25; begin open c1; loop fetch c1 into z1; if(fac=0) then update subject_rank25 set status='Not offered' where subject_code=z1; end if; exit when c1%notfound; end loop; close c1; end; PL/SQL procedure successfully completed SQL> select * from subject_rank25; Subject_code Subject_namefaculty_code staus
9115 Electronics 9116 Chemistry 0 0 Offered Offered

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 the queries to do the following a. Find the number of students who have enrolled for the subject DBMS. b. Find all those subjects which are not offered by any faculty member.

98

SQL> create table subject_rank(subject_code number(4),subject_name varchar(15),faculty_code number(4));


SUBJECT_CODE
1 2 3 4 5 6 7 8 9 9113 9114 9110 9111 9112 9114 9110 9111 9112

SUBJECT_NAME
OS Physics Accounts DBMS History Physics Accounts DBMS History Electronics Chemistry

FACULTY_CODE
5678 5679 5680 5681 5683 5687 5688 5689 5690 0 0

10 9115 11 9116

SQL> Create table faculty27(faculty_code number(4) primary key,faculty_name varchar(25),specialization varchar(10));


FACULTY_CODE
5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692

FACULTY_NAME
Syed Nazia Begum M Suhasini S Zareena B Sujana Kumari K Balaramudu G Gopinath M Rama Krishna B Ramesh D.Anilkumar Reddy J.Haribabu S.Mahammad Goush L.Sudhakar O.Sivaprasad G.Manesh M.Nagaraju

SPECIALIZATION
M.Tech M.Sc M.com MCA MBA MA B.Sc B.Com M.Tech M.Sc M.com MCA BA B.Tech BE

SQL> create table student27(roll_no varchar(19),subject_opted varchar(15));


ROLL_NO
11001 11002 11003 11004 11005

number(4)

primary

key,name

NAME
A.Nagedra B.Adinarayana B.Ayyawaru B.Dastagiri B.Ramakrishna

SUBJECT_OPTED
DBMS History Physics History Physics

99

11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049

B.Ramesh C.Jayaraju C.Rajashekar C.Subramanyam C.Venkatasiva D.Anilkumar Reddy D.Udaykiran G.Ganganna G.Gurappa G.Manesh G.Suresh G.Venkataramana J.Haribabu K.Diwakar Reddy K.Gourisankar K.Ramakrishna K.Ramakrishna K.Ramesh K.Ramesh K.Sankar Reddy K.Sivasankar L.Sudhakar M.Nagaraju M.Nagarjuna M.Raghavendra Reddy O.Sivaprasad P.Bashakar P.Moulali P.Nagaraju P.Rafi P.Sreeramulu P.Tirupalu S.Anwar Basha S.Baba S.Gangaraju S.Ibrahim S.Jilani Basha S.Mahammad Goush T.Babaiah T.Balagangi Reddy T.Sreenivasulu Y.Vijayaram N.jagan K.Hari Babu

Physics Physics Physics DBMS Physics History History DBMS Accounts Accounts History Accounts DBMS Physics DBMS OS History OS DBMS DBMS History DBMS Accounts Physics DBMS OS Accounts OS Physics OS Accounts OS OS OS OS DBMS Accounts OS History Accounts DBMS OS Electronics Chemistry

100

Queries
(a) select count(roll_no) from student27 where subject_opted='DBMS';
COUNT(ROLL_NO)
11

(b) select subject_name from subject_rank where faculty_code=0;


SUBJECT_NAME
Electronics Chemistry

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 the queries to do the following Find the number of students who have enrolled for the subject DBMS. Find all those subjects which are offered by more than one faculty member.

Creating the Tables SQL> create table subject_rank(subject_code number(4),subject_name varchar(15),faculty_code number(4));
SUBJECT_CODE SUBJECT_NAME FACULTY_CODE

101

1 2 3 4 5 6 7 8 9

9113 9114 9110 9111 9112 9114 9110 9111 9112

OS Physics Accounts DBMS History Physics Accounts DBMS History Electronics Chemistry

5678 5679 5680 5681 5683 5687 5688 5689 5690 0 0

10 9115 11 9116

SQL> Create table faculty27(faculty_code number(4) primary key,faculty_name varchar(25),specialization varchar(10));


FACULTY_CODE
5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692

FACULTY_NAME
Syed Nazia Begum M Suhasini S Zareena B Sujana Kumari K Balaramudu G Gopinath M Rama Krishna B Ramesh D.Anilkumar Reddy J.Haribabu S.Mahammad Goush L.Sudhakar O.Sivaprasad G.Manesh M.Nagaraju

SPECIALIZATION
M.Tech M.Sc M.com MCA MBA MA B.Sc B.Com M.Tech M.Sc M.com MCA BA B.Tech BE

SQL> create table student27(roll_no varchar(19),subject_opted varchar(15));


ROLL_NO
11001 11002 11003 11004 11005 11006

number(4)

primary

key,name

NAME
A.Nagedra B.Adinarayana B.Ayyawaru B.Dastagiri B.Ramakrishna B.Ramesh

SUBJECT_OPTED
DBMS History Physics History Physics Physics

102

11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049

C.Jayaraju C.Rajashekar C.Subramanyam C.Venkatasiva D.Anilkumar Reddy D.Udaykiran G.Ganganna G.Gurappa G.Manesh G.Suresh G.Venkataramana J.Haribabu K.Diwakar Reddy K.Gourisankar K.Ramakrishna K.Ramakrishna K.Ramesh K.Ramesh K.Sankar Reddy K.Sivasankar L.Sudhakar M.Nagaraju M.Nagarjuna M.Raghavendra Reddy O.Sivaprasad P.Bashakar P.Moulali P.Nagaraju P.Rafi P.Sreeramulu P.Tirupalu S.Anwar Basha S.Baba S.Gangaraju S.Ibrahim S.Jilani Basha S.Mahammad Goush T.Babaiah T.Balagangi Reddy T.Sreenivasulu Y.Vijayaram N.jagan K.Hari Babu

Physics Physics DBMS Physics History History DBMS Accounts Accounts History Accounts DBMS Physics DBMS OS History OS DBMS DBMS History DBMS Accounts Physics DBMS OS Accounts OS Physics OS Accounts OS OS OS OS DBMS Accounts OS History Accounts DBMS OS Electronics Chemistry

Queries

103

(i)SQL> select count(roll_no) from student27 where subject_opted='DBMS';


COUNT(ROLL_NO)
11

(ii)SQL> select subject_code,subject_name,count(subject_code) from subject_rank group by subject_code having count(subject_code)>1;


SUBJECT_CODE
9114 9110 9111 9112

SUBJECT_NAME
Physics Accounts DBMS History

COUNT
2 2 2 2

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 the queries to do the following (i)Find the number of students who have enrolled for the subject OS. (ii)Find all those students who have opted for more than 5 subjects. SQL> create table subject_rank(subject_code number(4),subject_name varchar(15),faculty_code number(4)); Select * from subject_rank;
SUBJECT_CODE
1 2 3 9113 9114 9110 OS Physics Accounts

SUBJECT_NAME

FACULTY_CODE
5678 5679 5680

104

4 5 6 7 8 9 10 11

9111 9112 9114 9110 9111 9112 9115 9116

DBMS History Physics Accounts DBMS History Electronics Chemistry

5681 5683 5687 5688 5689 5690 0 0

SQL> Create table faculty27(faculty_code number(4) primary key,faculty_name varchar(25),specialization varchar(10));


FACULTY_CODE
5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692

FACULTY_NAME
Syed Nazia Begum M Suhasini S Zareena B Sujana Kumari K Balaramudu G Gopinath M Rama Krishna B Ramesh D.Anilkumar Reddy J.Haribabu S.Mahammad Goush L.Sudhakar O.Sivaprasad G.Manesh M.Nagaraju

SPECIALIZATION
M.Tech M.Sc M.com MCA MBA MA B.Sc B.Com M.Tech M.Sc M.com MCA BA B.Tech BE

SQL> create table student27(roll_no varchar(19),subject_opted varchar(15)); select * from student27;


ROLL_NO
11001 11002 11003 11004 11005 11006 11007

number(4)

primary

key,name

NAME
A.Nagedra B.Adinarayana B.Ayyawaru B.Dastagiri B.Ramakrishna B.Ramesh C.Jayaraju

SUBJECT_OPTED
DBMS History Physics History Physics Physics Physics

105

11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049

C.Rajashekar C.Subramanyam C.Venkatasiva D.Anilkumar Reddy D.Udaykiran G.Ganganna G.Gurappa G.Manesh G.Suresh G.Venkataramana J.Haribabu K.Diwakar Reddy K.Gourisankar K.Ramakrishna K.Ramakrishna K.Ramesh K.Ramesh K.Sankar Reddy K.Sivasankar L.Sudhakar M.Nagaraju M.Nagarjuna M.Raghavendra Reddy O.Sivaprasad P.Bashakar P.Moulali P.Nagaraju P.Rafi P.Sreeramulu P.Tirupalu S.Anwar Basha S.Baba S.Gangaraju S.Ibrahim S.Jilani Basha S.Mahammad Goush T.Babaiah T.Balagangi Reddy T.Sreenivasulu Y.Vijayaram N.jagan K.Hari Babu

Physics DBMS Physics History History DBMS Accounts Accounts History Accounts DBMS Physics DBMS OS History OS DBMS DBMS History DBMS Accounts Physics DBMS OS Accounts OS Physics OS Accounts OS OS OS OS DBMS Accounts OS History Accounts DBMS OS Electronics Chemistry

Queries

106

(i)select count(subject_opted) from student27 group by subject_opted having subject_opted =OS;

COUNT(SUBJECT_OPTED)
10

(ii)select rollno,student_name,count(rollno) from subject_opted group by rollno having count(rollno)>5;


COUNT(ROLLNO)
0

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 the queries to do the following a. Find the number of students who have not enrolled for the subject DBMS. b. Find all those subjects which are offered by more than one faculty member.

Creating Tables SQL>create table subject_rank(subject_code number(4),subject_name varchar(15),faculty_code number(4)); Select * from subject_rank;
SUBJECT_CODE
1 2 3 9113 9114 9110 OS Physics Accounts

SUBJECT_NAME

FACULTY_CODE
5678 5679 5680

107

4 5 6 7 8 9 10 11

9111 9112 9114 9110 9111 9112 9115 9116

DBMS History Physics Accounts DBMS History Electronics Chemistry

5681 5683 5687 5688 5689 5690 0 0

SQL> Create table faculty27(faculty_code number(4) primary key,faculty_name varchar(25),specialization varchar(10));


FACULTY_CODE
5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692

FACULTY_NAME
Syed Nazia Begum M Suhasini S Zareena B Sujana Kumari K Balaramudu G Gopinath M Rama Krishna B Ramesh D.Anilkumar Reddy J.Haribabu S.Mahammad Goush L.Sudhakar O.Sivaprasad G.Manesh M.Nagaraju

SPECIALIZATION
M.Tech M.Sc M.com MCA MBA MA B.Sc B.Com M.Tech M.Sc M.com MCA BA B.Tech BE

SQL> Crete table student27(roll_no varchar(19),subject_opted varchar(15));


ROLL_NO
11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 A.Nagedra B.Adinarayana B.Ayyawaru B.Dastagiri B.Ramakrishna B.Ramesh C.Jayaraju C.Rajashekar C.Subramanyam C.Venkatasiva

number(4)

primary

key,name

NAME

SUBJECT_OPTED
DBMS History Physics History Physics Physics Physics Physics DBMS Physics

108

11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024

D.Anilkumar Reddy D.Udaykiran G.Ganganna G.Gurappa G.Manesh G.Suresh G.Venkataramana J.Haribabu K.Diwakar Reddy K.Gourisankar K.Ramakrishna K.Ramakrishna K.Ramesh K.Ramesh

History History DBMS Accounts Accounts History Accounts DBMS Physics DBMS OS History OS DBMS

Queries (i)SQL> select count(roll_no) from student27 having subject_opted!='DBMS';


COUNT(ROLL_NO)
38

(ii)SQL> select subject_code,subject_name,count(subject_code) from subject_rank group by subject_code having count(subject_code)>1;


SUBJECT_CODE
9114 9110 9111 9112

SUBJECT_NAME
Physics Accounts DBMS History

COUNT
2 2 2 2

109

Steps For Creating A Data Source


STEPS FOR CREATING DATA SOURCE Step1: Go to control panel select administrative tools->Data Source (ODBC) icon . Step2: Then a dialogue box appears naming ODBC Data Source then click on Add and then select the option Microsoft ODBC for Oracle and click finish then a dialogue appear naming Microsoft ODBC for Oracle SetUp Step 3: Now enter the values as Data Source Name as nmca10 User Name as scott/tiger(password) (is SQL is opened with host string is given as oracle then scott/tiger@oracle) Then click ok Step4: Then that data source name is entered into the ODBC Data Source Administrator. In this way we connect the data source to database

110

111

Steps To Create A Form


Steps To Create A Form : Step1: Open Microsoft Visual Basic 6.0 Step2: Open standard EXE Step 3: It generates an empty form as shown. Step 3: Select the required no of labels by selecting icon Label and enter the label names in the properties table in Caption row Step 4: Select the required no of text fields by selecting the icon Text Step 5: create an adodc icon by selecting Project ->Components then a dialogue box appears then a dialogue appears Step 6: Tick on Microsoft ADO Data Control 6.0 (OLEDB) the click apply and then ok. Step 7: choose the icon adodc create the adodc1 box as shown Step 8: Select the required command fields by selecting the icon Command Button and And enter the command names like add,save,delete in the properties table in Caption row. Step 8: Now select the adodc1 box and enter the command type as table in properties table 112

Step 9: Now select the command string the a dialogue box appears in the box select the use ODBC Data source Name and enter the data source name i.e nmca10 as shown. Step 10: Now select the Resource type in properties table then a dialodue box appears select the table for which the form being created as shown an then select ok. Step 11: Now select the Data Source as adodc1 and Data Field as necessary (Eg: Course_id); Step 12: In order to write code for the command buttons double click the command button then a window appears the write the code as follows (i) for add: Private Sub Command1_Click() Adodc1.Recordset.AddNew End Sub (ii) for save: Private Sub Command1_Click() Adodc1.Recordset.update End Sub (iii) for delete: Private Sub Command1_Click() Adodc1.Recordset.update End Sub Step 13: In order to give Validation checks select required text box on which we have to define validate and same as step11and write the code as follows. Fro Eg to text 3 Private Sub Text3_Change() If (Text3.Text) = 0 Then MsgBox ("fee cant be zero") Text3.SetFocus End If End but the following must be used Step 14: In order to execute the form click start button the menu bar and the form is created as follows: Step 15: For example we have generated the for and have add the values as follows Course_id as 1111 Name as Commerce Fee as 15000 Duration as 3 Step 16: In order to see the added recored go to SQL promt and gine the following command SQL> select * from course; COURSE -----------1111 123 456 789 012 NAME -------Commerce M.C.A B.Tech M.B.A Bio-Tech FEE ----15000 25400 36000 20000 40000 DURATION --------------3 3 4 2 3

113

156

Food-Tech

10000

6 rows selected.

Output of Step 3:

114

Output of Step 5:

Output of Step 6:

115

Output of Step 7:

Output of Step 9:

116

Output of step10:

Output of step11:

117

Output of step12 & 13:

Output of step 14:

118

Steps To Generate A Report


STEPS TO GENERATE REPORT 119

Step1: Create form on the table for which a report has to generated. Step2: Now select option projects->add from->VB Wizard form. Step3: a dialogue box appears naming Data Form wizard-Introduction click on Next. Step4: A dialogue box appears naming Data Form wizard-Database Type select the option remote(ODBC) and click Next. Step5:A dialogue box appears naming Data Form wizard-Connection Information and enter the values for ODBC Connect Data as DSN : nmca10(name of the data source) UID : scott PWD :tiger Then click on Next. Step6: A dialogue box appears naming Data Form wizard-Forms select the Form Layout as Grid(database) and Binding code as AOD Data control if we want to select all the rows or if we want to retrieve data based upon a condition then select the ption as AOD Code and click on Next. Step 7: A dialogue box appears naming Data Form wizard-Record Source then select the record source as table name (eg item22) then it displays available fields select all the fields if we want to retrive entire data if not select those columns on which a condition should be specified. Step8: A dialogue box appears naming Data Form wizard-Control Section delete all the available controls and click on Next Step9: A dialogue box appears naming Data Form wizard-Finished then select finish. Step10: Then report is generated Step11: In order to give a condition double click the report and insert the SQL query and then run the report.

120

Step2:

: Step3:

121

Step4:

Step5:

Step6:

122

Step7:

123

Step8:

Step9:

Step10: 124

125

Das könnte Ihnen auch gefallen