You are on page 1of 69

1.

Create database schema for a customer-sale

scenario customer(custid,cust_name)

item(itemid,item_name,price)

sale(bill_no,billdate,custid,itemid,qtysolid)

SQL> create table customer(custid int not null primary key,cust_name varchar(20));

Table created.

SQL> create table item(itemid int not null primary key,item_name

varchar(20),price number(10));

Table created.

SQL> create table sale(billno int not null primary key,billdate date,custid int,

foreign key(custid) references customer(custid),itemid int,foreign key(itemid)

references item(itemid),qtysld number(10));

Table created.

SQL> insert into customer values(101,'abc');

1 row created.

SQL> insert into customer

values(102,'def'); 1 row created.

1
SQL> insert into customer

values(103,'gh'); 1 row created.

SQL> insert into customer

values(104,'ijk'); 1 row created.

SQL> insert into customer

values(105,'lm'); 1 row created.

SQL> insert into customer values(106,'no');

1 row created. SQL> insert into customer values(107,'pq'); 1 row created.

SQL> insert into customer values(108,'rs');

1 row created.

SQL> select * from customer;

SQL> insert into item values(1001,'pencils',5); 1 row created.

SQL> insert into item values(1002,'erasers',5); 1 row created.

2
SQL> insert into item

values(1003,'sharpeners',5); 1 row

created.

SQL> insert into item

values(1004,'scales',10); 1 row

created.

SQL> insert into item values(1005,'pens',10);

1 row created.
SQL> insert into item

values(1006,'sketches',250); 1 row

created.

SQL> insert into item values(1007,'color

pens',220); 1 row created. SQL> insert into

item values(1008,'fevicol',10); 1 row

created.

3
SQL> select * from item;

SQL> insert into sale values(2000,'03-aug-

2023',101,1001,20);

1 row created.

SQL> insert into sale values(2001,'04-aug-2023',102,1003,40);

1 row created.

SQL> insert into sale values(2002,'sysdate',103,1002,30);

1 row created.

SQL> insert into sale values(2003,'06-aug-2023',104,1004,45);

1 row created.

SQL> insert into sale values(2004,'sysdate',105,1005,25);


1 row created.

SQL> insert into sale values(2005,'08-aug-2023',106,1006,25);

1 row created.

SQL> insert into sale values(2006,'sysdate',107,1007,15);

1 row created.

4
SQL> insert into sale values(2007,'10-aug-

2023',108,1008,35); 1 row created.

SQL> select * from sale;

i. List all the bills for the currentdate with the customer names and item numbers

SQL> select s.billno,i.item_name,i.itemid,c.custid,c.cust_name from sale s

2 join item i on i.itemid=s.itemid

3 join customer c on c.custid=s.custid

4 where billdate=trunc(sysdate)';

ii. List the total bill details with the quantity sold,price of the item and the final
amount

SQL> select s.billno,c.cust_name,i.itemid,i.price,s.qtysld,(s.qtysld*i.price) as


final_amount from sale s

2 join customer c on s.custid=c.custid

5
3 join item i on s.itemid=i.itemid
4 where s.billdate=trunc(sysdate);

iii.List the details of the customer who have bought a product which has a
price>200

SQL> select c.custid,c.cust_name,i.item_name from customer c

2 join sale s on c.custid=s.custid

3 join item i on s.itemid=i.itemid


4 where i.price>200;

6
2. create database schema for a student library

scernario student(stud_no,stu_name)

membership(mem_no,stud_no)

book((book_no,book_name,author)

iss_rec(iss_no,iss_date,memo_no,book_no)

1 list all the student names with their membership numbers

2.list all the issues for the current date with student and book names
SQL> CREATE TABLE Students(stud_no INT PRIMARY KEY,stu_name
VARCHAR(10));

Table created.
SQL> insert into Students values (1,'vinod');
1 row created.
SQL> insert into Students values (2,'ramu');
1 row created.
SQL> insert into Students values (3,'navaneeth');
1 row created.
SQL> insert into Students values (4,'ramesh');
1 row created.
SQL> insert into Students values (5,'suresh');
1 row created.
SQL> insert into Students values (6,'teja');
1 row created.
SQL> insert into Students values (7,'raveen');
1 row created.
SQL> insert into Students values (8,'praveen');
1 row created.
SQL> CREATE TABLE Membership (mem_no INT PRIMARY KEY,stud_no
INT,FOREIGN
KEY (stud_no) REFERENCES Students(stud_no));
Table created. \
7
SQL> insert into Membership values (101,1);
1 row created.
SQL> insert into Membership values (102,2);
1 row created.
SQL> insert into Membership values (103,3);
1 row created.
SQL> insert into Membership values (104,4);
1 row created.
SQL> insert into Membership values (105,5);
1 row created.
SQL> insert into Membership values (106,6);
1 row created.
SQL> insert into Membership values (107,7);
1 row created.
SQL> insert into Membership values (108,8);
1 row created.
SQL> CREATE TABLE Books (book_no INT PRIMARY KEY,book_name
VARCHAR(255),author VARCHAR(100));
Table created.
SQL> insert into Books values (201, 'Introduction to Computer Science', 'John
Smith');
1 row created.
SQL> insert into Books values (202, 'The Catcher in the Rye', 'J.D.Salinger');
1 row created.
SQL> insert into Books values (203, 'To Kill a Mockingbird', 'Harper Lee'); 1 row
created.
SQL> insert into Books values (204, 'The India Story', 'Bimal jalal');
1 row created.
SQL> insert into Books values (205, 'A Place Called Home', 'Preeti Shenoy');
1 row created.
SQL> insert into Books values (206, 'Lal Salam', 'Smriti Irani');
1 row created.
SQL> insert into Books values (207, 'Queen of fire', 'Devika Rangachari');

8
1 row created.
SQL> insert into Books values (208, 'Hear Yourself', 'Indian Author Prem
Rawat');
1 row created.
SQL> CREATE TABLE IssueRecords (iss_no INT PRIMARY KEY,iss_date
DATE,mem_no INT,book_no INT,
FOREIGN KEY (mem_no) REFERENCES Membership(mem_no), FOREIGN KEY
(book_no) REFERENCES Books(book_no));

Table created.

SQL> insert into IssueRecords values (301,'24-sep-2023',101,201);

1 row created.

SQL> insert into IssueRecords values (302,'24-sep-2023',102,202);


1 row created.
SQL> insert into IssueRecords values (303,'25-sep-2023',103,203);
1 row created.
SQL> insert into IssueRecords values (304,'23-sep-2023',104,204);
1 row created.
SQL> insert into IssueRecords values (305,'23-sep-2023',105,205);
1 row created.
SQL> insert into IssueRecords values (306,'22-sep-2023',106,206);
1 row created.
SQL> insert into IssueRecords values (307,'21-sep-2023',107,207);
1 row created.
SQL> insert into IssueRecords values (308,'20-sep-2023',108,208);
1 row created.
1) SQL> SELECT s.stu_name, m.mem_no FROM Students

9
2) JOIN Membership m ON s.s

t 2)SQL> SELECT
s.stu_name AS student_name,
b.book_name AS book_name,

ir.iss_date AS issue_date

FROM

Students s
JOIN

Membership m ON s.stud_no = m.stud_no


JOIN

IssueRecords ir ON m.mem_no = ir.mem_no


JOIN

Books b ON ir.book_no = b.book_no


WHERE

ir.iss_date = TRUNC(SYSDATE);

10
3)SQL> SELECT

s.stu_name AS student_name,

m.stud_no AS student_id,

COUNT(ir.book_no) AS total_books_bought

FROM

Students s
JOIN

Membership m ON s.stud_no = m.stud_no


LEFT JOIN

IssueRecords ir ON m.mem_no = ir.mem_no

GROUP BY

s.stu_name, m.stud_no

ORDER BY

total_books_bought DESC;

11
3.database schema for a employee-pay scenario
employee(empid,empname)
department(deptid,deptname)
paydetails(empid,deptid,basic,deduction,addition,dateofjoin)
payroll(empid,paydate date)
CREATE TABLE Employee1(empid INT PRIMARY KEY,empname VARCHAR(20));
SQL> insert into employee1 values(101,'aaa');
1 row created.
SQL> insert into employee1 values(102,'bbb');
1 row created.
SQL> insert into employee1 values(103,'ccc');
1 row created.
SQL> insert into employee1 values(104,'ddd');
1 row created.
SQL> insert into employee1 values(105,'eee');
1 row created.
CREATE TABLE Department1(deptid INT PRIMARY KEY,deptname VARCHAR(30));
SQL> insert into department1 values(201,'acct');
1 row created.
SQL> insert into department1 values(202,'admin');
1 row created.
SQL> insert into department1 values(203,'tech');
1 row created.
SQL> insert into department1 values(204,'hr');
1 row created.
SQL> insert into department1 values(205,'support');
1 row created.
CREATE TABLE PayDetails1(
empid INT,
deptid INT,
basic DECIMAL(10, 2),
deduction DECIMAL(10, 2),
addition DECIMAL(10, 2),
dateofjoin DATE,
FOREIGN KEY (empid) REFERENCES Employee1(empid),
FOREIGN KEY (deptid) REFERENCES Department1(deptid));

12
SQL> insert into paydetails1 values(101,201,20000,2000,3000,'02-aug-2020');
1 row created.
SQL> insert into paydetails1 values(102,202,30000,2000,3000,'03-aug-2020');
1 row created.
SQL> insert into paydetails1 values(103,203,50000,2000,3000,'03-aug-2019');
1 row created.
SQL> insert into paydetails1 values(104,204,10000,2000,3000,'03-aug-2023');
1 row created.
SQL> insert into paydetails1 values(105,205,60000,2000,3000,'03-aug-2018');
1 row created.
CREATE TABLE Payroll (empid INT, paydate DATE, FOREIGN KEY (empid)
REFERENCES Employee1(empid));
SQL> insert into payroll values(101,'20-aug-2023');
1 row created.
SQL> insert into payroll values(102,'20-aug-2023');
1 row created.
SQL> insert into payroll values(103,'20-aug-2023');
1 row created.
SQL> insert into payroll values(104,'20-aug-2023');
1 row created.
SQL> insert into payroll values(105,'20-aug-2023');
1 row created.
list all the employee names who joined after particular date
SQL> select empname from employee1 where empid in (select empid from
2 paydetails1 where dateofjoin<'02-aug-2022');

2 list the details of employess whose basic salary is between 10000 and 20000
SQL> select e.empid,e.empname,p.basic from employee1 e join paydetails1 p
on e.empid=p.empid where p.basic between 10000 and 20000;

13
14
4.Create the following tables :
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 students who are greater than 18 years of age and have opted for
MCA course.
(ii) List all those courses whose fee is greater than that of MCA course.
SQL>create table student(roll_no number(2),name varchar2(20),dob date,course_id
number(3));
Table Created
SQL> insert into student values(1,’Vignesh’,’15-MAR-90’,101);
1 row inserted
SQL> insert into student values(2,’Ramesh’,’17-APR-90’,101);
1 row inserted
SQL> insert into student values(3,’Naveen’,’21-MAY-91’,102);
1 row inserted
SQL> insert into student values(4,’Ashok’,’01-JAN-91’,103);
1 row inserted
SQL> insert into student values(5,’Sandeep’,’23-AUG-90’,104);
1 row inserted
SQL>create table course(course_id number(3), name varchar2(20),fee
number(5),duration number(1));
Table Created
SQL> insert into course values(101,’MCA’,30000,3);
1 row inserted
SQL> insert into course values(102,’M.Tech’,35000,2);
1 row inserted
SQL> insert into course values(103,’MBA’,33000,2);
1 row inserted
SQL> insert into course values(104,’B.Tech’,27000,4);
1 row inserted
(b) Generate queries to do the following :
(i) List all those students who are greater than 18 years of age and have opted for
MCA course.
SQL>select s.roll_no,s.name,s.dob,c.name "Course" from student s,
course c where s.course_id=c.course_id and (sysdate-dob)/365>18 and
c.name='MCA';

15
(ii) List all those courses whose fee is greater than that of MCA course.

SQL> select name "Course",fee from course where fee>(select fee from course
where name='MCA');

16
5.Create the following table :
Student (roll-no, name, subject-OPTED)
Subject_rank(subject-code, subject-name, faculty_code)
Faculty(faculty_code,faculty_name,specilization)
Generate queries to do the following :
(i) Find the number of students who have enrolled for the subject "DBMS".
(ii)Find all those faculty members who have not offered any subject.
SQL> create table student5(roll_no number(10),name varchar(20),subject_code
number(10));
Table created.
SQL> insert into student5 values(1,'aaa',101);
1 row created.
SQL> insert into student5 values(2,'bbb',102);
1 row created.
SQL> insert into student5 values(3,'ccc',103);
1 row created.
SQL> insert into student5 values(3,'ddd',104);
1 row created.
SQL> insert into student5 values(4,'eee',105);
1 row created.
SQL> create table subject_rank(subject_code number(10),subject_name varchar(
10),faculy_code number(10));
Table created.
SQL> insert into subject_rank values(101,'dbms',201);
1 row created.
SQL> insert into subject_rank values(102,'os',202);
1 row created.
SQL> insert into subject_rank values(103,'os',203);
1 row created.
SQL> insert into subject_rank values(104,'CN',204);
1 row created.
SQL> insert into subject_rank values(105,'sql',205);
1 row created.
SQL> delete subject_rank where subject_code = 103;
1 row deleted.
SQL> insert into subject_rank values(103,'python',203);
1 row created.

17
SQL> create table faculty(faculty_code number(10),faculty_name
varchar(20),specilization varchar(20));
Table created.
SQL> insert into faculty values(201,'srinivas','dbms');
1 row created.
SQL> insert into faculty values(202,'ismail SIR','os');
1 row created.
SQL> insert into faculty values(203,'noor','dbms');
1 row created.
SQL> insert into faculty values(204,'DEVENDER SIR','SE');
1 row created.
SQL> insert into faculty values(205,'SUBHASINI MADAM','SE');
1 row created.
a) Generate queries to do the following :
(i) Find the number of students who have enrolled for the subject "DBMS".
SQL> SELECT COUNT(*) FROM STUDENT5 WHERE SUBJECT_OPTED='DBMS';

18
6. 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.
SQL> create table item(icode number(3),iname varchar2(10),stock
number(3),reorder number(3));
Table Created
SQL> insert into item values(201,'software',20,20);
1 row created.
SQL> insert into item values(202,'tractor',20,20);
1 row created.
SQL> insert into item values(203,'product',20,20);
1 row created.
SQL> insert into item values(204,'buld',20,20);
1 row created.
SQL> insert into item values(205,'buld',20,20);
1 row created.
SQL> create table supplier6(scode number(3),sname varchar2(10),address
varchar2(10));
Table created.
SQL> insert into supplier6 values(101,'satyam','hyderabad');
1 row created.
SQL> insert into supplier6 values(102,'tata','mumbai');
1 row created.
SQL> insert into supplier6 values(103,'mahindra','mumbai');
1 row created.
SQL> insert into supplier6 values(104,'reliance','delhi');
1 row created.
SQL> insert into supplier6 values(105,'wipro','delhi');
1 row created.
SQL> create table cansupply6(scode number(3), icode number(3));
Table Created
SQL> insert into cansupply values(101,201);
1 row created.

19
SQL> insert into cansupply values(102,202);
1 row created.
SQL> insert into cansupply values(103,203);
1 row created.
SQL> insert into cansupply values(104,204);
1 row created.
SQL> insert into cansupply values(105,205);
1 row created.
a)Generate queries to do the following :

(i) List all those suppliers who can supply the given item.
SQL> select sname from supplier6 where scode=(select scode from cansupply
where icode=(select icode from item where iname='&itemname'));

(ii)List all those items which cannot be supplied by given company.


SQL> select iname from item where icode!=(select icode from cansupply where
scode=(select scode from supplier6 where sname='&sname'));

20
7.Create the following tables:
Student (roll-no, name, 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 come from Andhra Pradesh state and belong to
given category who have secured a rank above 100
SQL>create table student7(roll number(3),name varchar2(20),category
varchar2(2),
district varchar2(20),state varchar2(20));
Table Created
SQL> insert into student7 values(1,’Sai’,’OC’,’Hyd’,’AP’);
1 row created
SQL> insert into student7 values(2,’Ashok’,’OC’,’Anathapur’,’AP’);
1 row created
SQL> insert into student7 values(3,’Sharma’,’OC’,’Rampur’,’UP’);
1 row created
SQL> insert into student7 values(4,’Dheeraj’,’OC’,’RR’,’AP’);
1 row created
SQL> insert into student7 values(5,’Ramesh’,’BC’,’Medak’,’AP’);
1 row created
SQL> insert into student7 values(6,’Vignesh’,’BC’,’Chennai’,’TN’);
1 row created
SQL> insert into student7 values(7,’Narsimha’,’BC’,’Kurnool’,’AP’);
1 row created
SQL> insert into student7 values(8,’Sandeep’,’OC’,’Adilabad’,’AP’);
1 row created
SQL>create table studentrank(roll number(3),marks number(3),rank number(4));
Table Created.
SQL> insert into studentrank values(1,125,5427);
1 row created
SQL> insert into studentrank values(2,110,5899);
1 row created
SQL> insert into studentrank values(3,136,3621);
1 row created
SQL> insert into studentrank values(4,125,5427);

21
1 row created
SQL> insert into studentrank values(5,142,2431);
1 row created
SQL> insert into studentrank values(6,98,6531);
1 row created
SQL> insert into studentrank values(7,125,5427);
1 row created
SQL> insert into studentrank values(8,110,5899);
1 row created
a) Generate queries to do the following :
(i) List all those students who have come from Tamilnadu state and secured a rank
above 3000
SQL> select s.name from student7 s,studentrank r where s.roll=r.roll and r.rank >
3000 and s.state='TN';

(ii) List all those students who come from Andhra Pradesh state and belong to
given category who have secured a rank above 100.
SQL> select s.name from student7 s,studentrank r where s.roll=r.roll and r.rank > 100
and s.state='AP';

22
8.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 customers who live in the same city as the branch in which they
have account
(ii)List all those customers who have an account in a given branch city.
SQL>create table branch(bid number(3),bname varchar2(20),bcity varchar2(20));
Table Created
SQL> insert into branch values(101,’ABC’,’Hyderabad’);
1 row created
SQL> insert into branch values(102,’DEF’,’Adilabad’);
1 row created
SQL> insert into branch values(103,’MNO’,’Warangal’);
1 row created
SQL> insert into branch values(104,’PQR’,’Tirupathi’);
1 row created
SQL> insert into branch values(105,’XYZ’,’Hyderabad’);
1 row created
SQL>create table customer(cid number(3),cname varchar2(20),
ccity varchar2(20),bid number(3));
SQL> insert into customer values(201,’A’,’Hyderabad’,101);
1 row created
SQL> insert into customer values(202,’A’,’Hyderabad’,101);
1 row created
SQL> insert into customer values(203,’C’,’Warangal’,102);
1 row created
SQL> insert into customer values(204,’D’,’Warangal’,103);
1 row created
SQL> insert into customer values(205,’E’,’Tirupathi’,104);
1 row created
(a) 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.
SQL> select cname,ccity,bcity from branch b,customer c where b.bid=c.bid and
ccity=bcity;

23
(ii)List all those customers who have an account in a given branch city.
SQL> select cname from customer c,branch b where c.bid=b.bid and b.bid=(select
bid from branch where bname='&banme');

24
9.Create the following tables :
Branch (branch-id, branch-name, branch-city)
Customer (customer-id, customer-name, customer-city, branch-id)
(i) List all those customers who live in the same city as the branch in which they
have account.
(ii) List all those customers who have an account in more than one branch.
SQL>create table branch9(bid number(3),bname varchar2(20),bcity varchar2(20));
Table Created
SQL> insert into branch9 values(101,’ABC’,’Hyderabad’);
1 row created
SQL> insert into branch9 values(102,’DEF’,’Adilabad’);
1 row created
SQL> insert into branch9 values(103,’MNO’,’Warangal’);
1 row created
SQL> insert into branch9 values(104,’PQR’,’Tirupathi’);
1 row created
SQL> insert into branch9 values(105,’XYZ’,’Hyderabad’);
1 row created
SQL>create table customer9(cid number(3),cname varchar2(20),
ccity varchar2(20),bid number(3));
SQL> insert into customer9 values(201,’A’,’Hyderabad’,101);
1 row created
SQL> insert into customer9 values(202,’A’,’Hyderabad’,101);
1 row created
SQL> insert into customer9 values(203,’C’,’Warangal’,102);
1 row created
SQL> insert into customer9 values(204,’D’,’Warangal’,103);
1 row created
SQL> insert into customer9 values(205,’E’,’Tirupathi’,104);
1 row created
(i) List all those customers who live in the same city as the branch in which they
have account.
Sql> select c.cname,ccity,bcity from customer9 c,branch9 b where c.bid=b.bid and
ccity=bcity;

25
(ii) List all those customers who have an account in more than one branch.
SQL> select cname from customer9 group by cname having count(bid)>1;

26
10.Create the following tables :
Branch (branch-id, branch-name, customer-city)
Customer (customer-id, customer-name, customer-city, branch-id)
(a) Insert at least 5 records in each table.
(b) Generate queries to do the following :
(i) list all those branches who have more than 5 customer
(ii) List all those customers who have an account in more than one branch
SQL> create table branch(bid number(3),bname varchar2(20),bcity
varchar2(20));
Table created.
SQL> insert into branch values(1,'abc','hyd');
1 row created.

SQL> insert into branch values(2,'def','knl');

1 row created.
SQL> insert into branch values(3,'ghi','ndl');
1 row created.
SQL> insert into branch values(4,'jkl','khm');
1 row created.
SQL> insert into branch values(5,'mno','mnd');
1 row created.
SQL> insert into branch values(6,'pqr','pny');
1 row created.
SQL> insert into branch values(7,'xyz','wrn');
1 row created.
SQL> insert into branch values(8,'uvw','rntp');
1 row created.
SQL> create table customer(cid number(3),cname varchar2(20), ccity
varchar2(20),bid number(3));
Table created.

SQL> insert into customer values(201,’A’,’hyd’,1);


1 row created.
SQL> insert into customer values(202,’b’,’hyd’,2);

27
1 row created.
SQL> insert into customer values(203,’C’,’knl’,3);
1 row created.
SQL> insert into customer values(204,’D’,’mnd’,2);
1 row created.
SQL> insert into customer values(205,’E’,’rntp’,4);
1 row created.
SQL> insert into customer values(206,’f’,’pyn’,7);
1 row created.
SQL> insert into customer values(207,’G’,’wrn’,8);
1 row created.
SQL> insert into customer values(208,’I’,’khm’,9);
1 row created.
(b) (i) SQL>SELECT b.bname FROM branch b JOIN customer c ON b.bid = c.bid
GROUP
BY bname HAVING COUNT(c.cid) > 5;

(ii) SQL> select *From customer where bid in(select bid from customer group by
bid 2 having count(bid)>=2);

28
11. Create the following tables:
Student (roll-no, name, date-of-birth, course-id)
Course (Course-id, name, fee, duration)
A] Create a form to accept the data from the user with appropriate validation
checks.
B] Generate queries to list all those students who are greater than 18 years of age
andhave opted for MCA course
SQL> CREATE TABLE course11(courseid char(10) PRIMARY KEY , name
varchar2(10),fee number,duration number);
Table created.
insert into course11 values ('c001','mca',75000,3);
insert into course11 values ('m003','mba',200000,2);
SQL> CREATE TABLE student11(roll char(10) PRIMARY KEY , dob date, name
varchar2(10),courseid char(10));
Table created.
insert into student11 values ('04808015', '07-sep-1990','suchitra','c001');
insert into student11 values ('04708015', '07-sep-1990','radhika','c001');
insert into student11 values ('04708012','12-jan-1988','hemasri','c001');
insert into student11 values ('04807010','26-dec-1990','jayasri','c001');
insert into student11 values ('04708016','07-jul-1985','radha','c001');
insert into student11 values ('04708013','12-jun-1988','hemalatha','c001');
insert into student11 values ('04807011','26-dec-1990','vasuda','c001');
insert into student11 values ('04808018','06-nov-1978','sadhana','c001');
insert into student11 values ('04708019','07-oct-1985','ragini','c001');
insert into student11 values ('04708020','21-feb-1988','anmol','m003');
insert into student11 values ('04807021','20-dec-1990','teja','m003');
insert into student11 values('0460909','17-jul-1990','kareena',' m003');

1). list all those students who are between 18-19 yrs of age and have opted
for mca course
SQL> SELECT s.name studentlist from student s,course c where
(months_between(sysdate,dob)/12) between 18 and 35 and c.name='mca' and
c.courseid=s.courseid;

29
2.list all those courses in which number of students are less than 10.
SQL> select name from course11 where courseid in(select courseid from student11
group by courseid having count(courseid)<10);

30
12. create the following tables :
student1 (roll-no, name, subject-opted)
subject –rank (subject-code, subject-name, faculty-code)
faculty (faculty-code, faculty-name, specialization).
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 members.
SQL> CREATE TABLE student12( roll char(5),name varchar2(20),subjopt
varchar2(30));
Table created.
insert into student12 values('s8015','suchitra','m01,m02,s01');
insert into student12 values('s8012',' siddu','m01,s02,p01');
insert into student12 values('s8010','rekha','m01,s01,s02');
insert into student12 values('s8021','ravi','s01,m02,d01');
SQL> CREATE TABLE subject12( subcode char(3), name varchar2(10),faccode
char(5),specialization varchar2(10),status varchar2(15));
Table created.
insert into subject12 values('m01','dismaths','f07','','');
insert into subject12 values('m02',' geomaths','','','');
insert into subject12 values('s01',' stats',' f07','','');
insert into subject12 values('p01','physics',' f05','','');
insert into subject12 values('s02','opresearch','f07','','');
insert into subject12 values('d01','dbms',' f03','','');
SQL> CREATE TABLE faculty12(faccode char(5), name varchar2(20),experience
number,specialization varchar2(10));
Table created.
insert into faculty12 values('f10','lavanya', 5,'m.sc,matha');
insert into faculty12 values('f07',' rahul ', 5,'m.sc,stats');
insert into faculty12 values('f05','devender',3,'m.tech,m.s');
insert into faculty12 values('f03 ','radhika',10,' b.tech');
I. list the number of students who have enrolled for the subject ‘dbms’
SQL> SELECT count(*) from student12,subject12 where
(subcode=substr(subjopt,0,3)subcode=substr(subjopt,5,3subcode=substr(subjopt,9,

31
2.list all those subjects which are not offered by any of the faculty members.
SQL> SELECT name from subject12 where faccode is null;

32
13. create the following tables :
student (roll-no, name, subject-opted)
subject –rank (subject-code, subject-name, faculty-code)
faculty (faculty-code, faculty-name, specialization).
• find the number of students who have enrolled for the subject "os"
• find all those subjects which are not offered by any faculty members.
SQL> CREATE TABLE student2( roll char(5), name varchar2(20),subjopt varchar2(30));
Table created.
SQL> CREATE TABLE subject2( subcode char(3), name varchar2(10),faccode
char(5),specialization varchar2(10), status varchar2(15));
Table created.
SQL> CREATE TABLE faculty2( faccode char(5), name varchar2(20),experience
number,specialization varchar2(10));
Table created.
SQL> SELECT count(*) from student12,subject12 where
(subcode=substr(subjopt,0,3) or subcode=substr(subjopt,5,3) or
subcode=substr(subjopt,9,3)) and subject12.name='os';

2.find all those students who opted for more than 5 subjects.
SQL> SELECT name from student2 where instr(subjopt,',',1,5)>0;

33
14). Create a table to represent sb-account of a bank consisting of account-no,
customer-name, balance-amount.
A] Write a PL/SQL block to implement deposit and withdraw.B] Withdraws should
not be allowed if the balance goes below Rs.1000
SQL> CREATE TABLE sb_account(account_no number(30)PRIMARY KEY
,customer_name varchar2(30),balance_amount number(8,2));
Table created.
SQL> INSERT into sb_account values(&acno,'&acname',&balamt);
Enter value for acno: 101
Enter value for acname: srinu
Enter value for balamt: 100000
old 1: INSERT into sb_account values(&acno,'&acname',&balamt)
new 1: INSERT into sb_account values(101,'srinu',100000)
1 row created.
SQL> /
Enter value for acno: 102
Enter value for acname: ravi
Enter value for balamt: 20000
old 1: INSERT into sb_account values(&acno,'&acname',&balamt)
new 1: INSERT into sb_account values(102,'ravi',20000)
1 row created.
SQL> /
Enter value for acno: 103
Enter value for acname: sriram
Enter value for balamt: 10000
old 1: INSERT into sb_account values(&acno,'&acname',&balamt)
new 1: INSERT into sb_account values(103,'sriram',10000)
1 row created.
SQL> commit
Commit complete.
SQL> declare
declare
v_acno number:=&acno;
v_amt number:=&amt;
v_bal number;

34
v_rembal number;
begin
SELECT balance_amount into v_bal from sb_account where account_no=v_acno;
v_rembal:=v_bal-v_amt;
if (v_rembal>1000) then
update sb_account set balance_amount=v_rembal where account_no=v_acno;
commit;
else
dbms_output.put_line('withdraw not possible');
end if;
end;

35
15.create the following table:
student(roll_no,date-of-birth,course-id)
course(course-id,name,fee,duration,status)
write pl/sql procedure to do following
set the status of course to "not offered" in which the number of candidates is less
than 5
SQL> create table student15(rno number(10),dob date,cid number(10));
Table created.
SQL> insert into student15 values(101,'14-jun-2010',201);
1 row created.
SQL> insert into student15 values(102,'14-jun-2009',202);
1 row created.
SQL> insert into student15 values(103,'14-jun-2009',202);
1 row created.
SQL> alter table student15 add name varchar(10);
Table altered.
SQL> update student15 set name='aaa' where rno=101;
1 row updated.
SQL> update student15 set name='bbb' where rno=102;
1 row updated.
SQL> update student15 set name='ccc' where rno=103;
1 row updated.
SQL> insert into student15 values(104,'14-jun-2009',204,'ddd');
1 row created.
SQL> insert into student15 values(105,'14-jun-2009',204,'eee');
1 row created.
SQL> create table course15(cid number(10),cname varchar(10),fee
number(10),duration varchar(10),status varchar(10));
Table created.
SQL> insert into course15 values(201,'ece',20000,'2',' ');
1 row created.
SQL> insert into course15 values(202,'eee',30000,'3',' ');
1 row created.
SQL> insert into course15 values(203,'mech',30000,'4',' ');
1 row created.

36
SQL> insert into course15 values(204,'cse',30000,'4',' ');
1 row created.
CREATE OR REPLACE PROCEDURE UpdateCourseStatus AS
BEGIN
UPDATE course15 c
SET c.status = 'not offered'
WHERE (SELECT COUNT(*) FROM student15 s WHERE s.cid = c.cid) < 5;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Course status updated successfully.');
END;
/

37
16.CONSIDER FOLLOWING COMPANY DATABASE
EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNO)
DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
DLOCATION (DNO,DLoc)
PROJECT (PNO, PName, Plocation, DNo)
WORKS ON (SSN, PNO, Hours) Write SQL queries to
1. Make a list of all project numbers for projects that involve an e 'Scott', either as
a worker or as a manager of the department that controls the project
2. show the resulting salaries if every employee working on the 'iot' project is
given a 10 percetnt rise
UPDATE EMPLOYEE3
3. FIND THE SUM OF SALARIES OF ALL EMPLOYEES OF THE 'ACCOUNTS'
DEPARTMENT AS WELL AS THE MAXIMUM SALARY,THE MINIMUM SALARY AND
AVERAGE SALARY IN THIS DEPARTMENT
CREATE TABLE EMPLOYEE16(
SSN VARCHAR(9) PRIMARY KEY,
Name VARCHAR(25),
Address VARCHAR(25),
Sex CHAR(1),
Salary DECIMAL(10, 2),
SuperSSN VARCHAR(9),
DNO INT
);
SQL> insert into employee16 values('101','aaa','hyderabad','M',20000,'s101',201);
1 row created.
SQL> insert into employee16 values('102','bbb','mumber','f',20000,'s102',202);
1 row created.
SQL>
SQL> insert into employee16 values('103','ccc','mumbai','m',20000,'s103',203);
1 row created.
SQL> insert into employee16 values('104','ddd','kurnool','f',20000,'s104',203);
1 row created.
SQL> insert into employee16 values('105','eee','kurnool','f',20000,'s105',203);
1 row created.
CREATE TABLE DEPARTMENT16(
DNO INT PRIMARY KEY,

38
DName VARCHAR(25),
MgrSSN VARCHAR(9),
MgrStartDate DATE
);
SQL> insert into department16 values(201,'hr','101','21-jun-2020');
1 row created.
SQL> insert into department16 values(202,'acct','102','21-jun-2020');
1 row created.
SQL> insert into department16 values(203,'admin','103','21-jun-2022');
1 row created.
SQL> insert into department16 values(204,'finace','104','21-jun-2020');

1 row created.
CREATE TABLE DLOCATION16(
DNO INT PRIMARY KEY,
DLoc VARCHAR(25)
);
SQL> insert into dlocation16 values(201,'hyderabad');
1 row created.
SQL> insert into dlocation16 values(202,'kurnool');
1 row created.
SQL> insert into dlocation16 values(203,'nadyal');
1 row created.
SQL> insert into dlocation16 values(204,'delhi');
1 row created.
CREATE TABLE PROJECT16 (
PNO INT PRIMARY KEY,
PName VARCHAR(25),
PLocation VARCHAR(25),
DNO INT );
SQL> insert into project16 values(401,'airline','hyderabad',201);
1 row created.
SQL> insert into project16 values(402,'railway','hyderabad',202);
1 row created.
SQL> insert into project16 values(403,'payroll','hyderabad',203);
1 row created.
CREATE TABLE WORKS_ON16 (

39
SSN VARCHAR(9),
PNO INT,
Hours DECIMAL(5, 2),
PRIMARY KEY (SSN, PNO)
);
SQL> insert into works_on16 values(101,401,120);
1 row created.
SQL> insert into works_on16 values(102,402,120);
1 row created.
SQL> insert into works_on16 values(103,403,120);
1 row created.
1. Make a list of all project numbers for projects that involve an e 'Scott', either as a
worker or as a manager of the department that controls the project
SELECT DISTINCT P.PNO
FROM PROJECT16 P
INNER JOIN WORKS_ON16 W ON P.PNO = W.PNO
INNER JOIN EMPLOYEE16 E ON W.SSN = E.SSN
WHERE E.Name = 'scott'
OR P.DNO IN (
SELECT DNO
FROM DEPARTMENT16
WHERE MgrSSN = (
SELECT SSN
FROM EMPLOYEE16
WHERE Name = 'scott'
)
);

40
2. show the resulting salaries if every employee working on the 'iot' project is given
a 10 percetnt rise
UPDATE EMPLOYEE3
Update employee16 set Salary = Salary * 1.10 WHERE SSN IN ( SELECT SSN FROM
WORKS_ON16 WHERE PNO IN ( SELECT PNO FROM PROJECT16 WHERE PName =
'iot' ));

3. FIND THE SUM OF SALARIES OF ALL EMPLOYEES OF THE 'ACCOUNTS'


DEPARTMENT AS WELL AS THE MAXIMUM SALARY,THE MINIMUM SALARY AND
AVERAGE SALARY IN THIS DEPARTMENT
SELECT
SUM(E.Salary) AS TotalSalary,
MAX(E.Salary) AS MaxSalary,
MIN(E.Salary) AS MinSalary,
AVG(E.Salary) AS AvgSalary
FROM EMPLOYEE16 E
INNER JOIN DEPARTMENT16 D ON E.DNO = D.DNO
WHERE D.DName = 'acct';

41
17. Create the following tables:
Student (roll-no, name, date-of-birth, course-id)
Course (Course-id, name, fee, duration, status)
A] Create a form to accept the data from the user with appropriate
validationchecks.
B] Write PL/SQL procedure to set the status of course to "offered" in which the
numberof candidates is at least 10 otherwise set it to "not offered"
SQL> CREATE TABLE course16(courseid char(10) PRIMARY KEY , name
varchar2(10),fee number, duration number,status varchar(10));
Table created.
insert into course16 values ('c001','mca',75000,3,' ');
insert into course16 values ('m003','mba',200000,2,' ');
SQL> CREATE TABLE student16(roll char(10) PRIMARY KEY ,dob date, name
varchar2(10),courseid char(10));
Table created.
insert into student16 values ('04808015', '07-sep-1990','suchitra','c001');
insert into student16 values ('04708015', '07-sep-1990','radhika','c001');
insert into student16 values ('04708012','12-jan-1988','hemasri','c001');
insert into student16 values ('04807010','26-dec-1990','jayasri','c001');
insert into student16 values ('04708016','07-jul-1985','radha','c001');
insert into student16 values ('04708013','12-jun-1988','hemalatha','c001');
insert into student16 values ('04807011','26-dec-1990','vasuda','c001');
insert into student16 values ('04808018','06-nov-1978','sadhana','c001');
insert into student16 values ('04708019','07-oct-1985','ragini','c001');
insert into student16 values ('04708020','21-feb-1988','anmol','m003');
insert into student16 values ('04807021','20-dec-1990','teja','m003');
insert into student16 values('0460909','17-jul-1990','kareena','c00');
pl/SQL procedure to set the status of course to 'offered' in which the number of
candidates is at least10 otherwise set to ‘not offered’
declare
id char(10);
num number;
cursor course_cr is SELECT * from course16;
course_val course_cr%rowtype;
begin
open
course_cr;

42
loop
fetch course_cr into course_val;
exit when course_cr%notfound;
id:=course_val.courseid;
SELECT count(*) into num from student16 where courseid=id;
if num < 10 then
update course16 set status='not-off'
where courseid=id;
end if;
if num >= 10 then
update course16 set status='offered'
where courseid=id;
end if;
end loop;
close course_cr;
end;
/

43
18.create the following table :
item (item-code, item-name, qty-in-stock, reorder-level)
supplier (supplier-code, supplier-name, address, status)
can-supply(supplier-code, item-code)
write pl/SQL procedure to do the following :set the status of the supplier to
"important" if the supplier can supply morethan five items
CREATE TABLE item18(itcode char(10) PRIMARY KEY , itname varchar2(20),qtysk
number, reorder number);
SQL> insert into item18 values('i003','lentils',10,5);
1 row created.
SQL> insert into item18 values('i004','turmeric',3,2);
1 row created.
SQL> insert into item18 values('i005','chilli-dry',7,5);
1 row created.
SQL> insert into item18 values('i006','chilli-whole',2,5);
1 row created.
SQL> insert into item18 values('i007','oil',10,5);
1 row created.
SQL> insert into item18 values('i008','cummin',5,7);
1 row created.
SQL> insert into item18 values('i009','coriander',5,5);
1 row created.
CREATE TABLE supplier18(supcode char(10) PRIMARY KEY , supname
varchar2(20),address varchar2(20),status varchar2(10))
SQL> insert into supplier18 values('s001','ashok','patna','');
1 row created.
SQL> insert into supplier18 values('s002','ramu gs','hyd','');
1 row created.
CREATE TABLE cansupply18(supcode char(10), itcode char(10));
SQL> insert into cansupply18 values('s001','i001');
1 row created.
SQL> insert into cansupply18 values('s001','i005');
1 row created.
SQL> insert into cansupply18 values('s001','i006');
1 row created.

SQL> insert into cansupply18 values('s001','i009');

44
1 row created.
SQL> insert into cansupply18 values('s002','i002');
1 row created.
SQL> insert into cansupply18 values('s002','i003');
1 row created.
SQL> insert into cansupply18 values('s002','i004')
1 row created.
Pl/SQL procedure to set the status of the supplier to ‘important’ if the supplier can
supply more than5 items.
SQL> SQL> begin
2 update supplier18 set status='important' where supcode
3 in(select supcode from cansupply18 group by supcode having count(*)>=2);
4 end;
5 /

45
19.create the following tables :
student (roll-no, name, subject-opted)
subject –rank (subject-code, subject-name, faculty-code, specialization)
faculty (faculty-code, faculty-name, specialization)
write pl/SQL procedure to the following :set the status of the subject to "not
offered" if the subject is not offered by anyof the faculty members
SQL> CREATE TABLE student19( roll char(5), name varchar2(20),subjopt
varchar2(30));
Table created.
insert into student19 values('s8015','suchitra','m01,m02,s01');
insert into student19 values('s8012',' siddu','m01,s02,p01');
insert into student19 values('s8010','rekha','m01,s01,m02,');
insert into student19 values('s8021','ravi','s01,m02,d01');
insert into student19 values('s8016','chitragada','m01,m02,s01’);
insert into student19 values('s8013',' sid roy','m01,m02,s01’);
SQL> CREATE TABLE subject19( subcode char(3),name varchar2(10),faccode char(5),
specialization varchar2(10), status varchar2(15));
Table created.
insert into subject19 values('m01','dismaths','f07','','');
insert into subject19 values('m02',' geomaths','','','');
insert into subject19 values('s01',' stats',' f07','','');
insert into subject19 values('p01','physics',' f05','','');
insert into subject19 values('s02','opresearch','f07','','');
insert into subject19 values('d01','dbms',' f03','','');
SQL> CREATE TABLE faculty19(faccode char(5), name varchar2(20),experience
number, specialization varchar2(10));
Table created.
insert into faculty19 values('f10','lavanya', 5,' m.sc,math');
insert into faculty19 values('f07',' rahul ', 5,' m.sc,stats');
insert into faculty19 values('f05','devender',3,'mtech,m.s');
insert into faculty19 values('f03 ','radhika',10,' b.tech');
pl/SQL procedure to set the status of the subject to ‘not offered’ if the subject is not
offered by anyof the faculty members.
Sql> begin
update subject19 set status='notoffered' where faccode is null;
end;

46
47
20.create the following tables :
student (roll-no, name, category, district, state)
student –rank (roll-no, marks, rank)
write pl/SQL procedure to the following :generate a report to list of those districts
from which the first hundred rankers come from.
SQL> CREATE TABLE student20( roll char(10) PRIMARY KEY , name
varchar2(20),category char(2), district varchar2(20),state varchar2(20));
Table created.
insert into student20 values('s002','suchitra','oc','patna','bihar');
insert into student20 values('s003','siddhu','oc','karimnagar','ap');
insert into student20 values('s004','rekha','bc','chennai','tamilnadu');
insert into student20 values('s005','hema','bc','ondichery','tamilnadu');
insert into student20 values('s006','sudheer','oc','karimnagar','ap');
SQL> CREATE TABLE rank(roll char(10),marks number,rank number);
Table created.
insert into rank values('s002',650,35);
insert into rank values('s003',650,35);
insert into rank values('s004',600,101);
insert into rank values('s005',600,102);
insert into rank values('s006',700,20);
pl/SQL procedure to generate a report to list those districts from which the first
hundred rankerscome from
sql>set heading on
column district heading '-district-'
SELECT unique district from student20,rank where student20.roll=rank.roll and
rank.rank<=100;

48
21 create the following table
Customer(customer_id,custname,customerage,address,salary)
Create a row level trigger for the customer table that would fire for insert or
update or delete operation performed on the customers table. This trigger will
display the salary difference between old values and new values.
create table customers(customer_id number(10),name varchar(10),age
number(10),address varchar(20),salary number(10));
SQL> insert into customers values(100,'ramu',32,'kurnool',50000);

SQL> insert into customers values(101,'ramu',32,'kurnool',50000);


Old salary:
New salary: 50000
Salary difference:

49
22.write a pl/sql program find the largest of three numbers
SQL> SET SERVEROUTPUT ON;
SQL>

SQL> -- Declare variables to store the three numbers and the result

SQL> DECLARE

2 num1 NUMBER := 25;

3 num2 NUMBER := 42;

4 num3 NUMBER := 18;

5 largest NUMBER;

6 BEGIN

7 -- Find the largest number among num1, num2, and num3

8 IF num1 >= num2 AND num1 >= num3 THEN

9 largest := num1;

10 ELSIF num2 >= num1 AND num2 >= num3 THEN

11 largest := num2;

12 ELSE

13 largest := num3;

14 END IF;

15

16 -- Display the result


17 DBMS_OUTPUT.PUT_LINE('The largest number among ' || num1 || ', ' || num2 || ', and '
|| num3 || ' is: ' || largest);

18 END;

19 /

b)factorial of given number.

50
declare
fac number :=1;
n number := &n;
begin
while n > 0 loop
fac:=n*fac;
n:=n-1;
end loop;
dbms_output.put_line(fac);
end;
/

51
23).write pl/sql code to accept a text and reverse the text and test whether the
given text is palindrome or not.
CREATE OR REPLACE FUNCTION checkForPalindrome(inputString VARCHAR2)
RETURN VARCHAR2
IS result VARCHAR2(75);
reversedString VARCHAR2(50);
BEGIN
SELECT REVERSE(inputString) INTO reversedString FROM DUAL;
-- Using UPPER to ignore case sensitivity.
IF UPPER(inputString) = UPPER(reversedString)
THEN
RETURN(inputString||' IS a palindrome.');
END IF;
RETURN (inputString||' IS NOT a palindrome.');
END checkForPalindrome;
/

52
24.a) Reverse of the number
SQL> DECLARE
2 num number;
3 reverse_num number:=0;
4
5 begin
6 num:=98765;
7 while num>0
8 loop
9 reverse_num:=(reverse_num*10) + mod(num,10);
10 num:=trunc(num/10);
11 end loop;
12
13 dbms_output.put_line(' Reversed number is : '|| reverse_num);
14 end;
15 /
Reversed number is : 56789
PL/SQL procedure successfully completed.

24.
b) write a procedure to check the given number is prime or not by using call
procedure
creating procedure:
CREATE OR REPLACE PROCEDURE IsPrime(p_number IN NUMBER,
p_is_prime OUT VARCHAR2) AS
v_count NUMBER := 0;
BEGIN
IF p_number <= 1 THEN
p_is_prime := 'Not Prime';
RETURN;
END IF;
FOR i IN 2..TRUNC(SQRT(p_number)) LOOP
IF MOD(p_number, i) = 0 THEN
v_count := v_count + 1;
EXIT;

53
END IF;
END LOOP;

IF v_count = 0 THEN
p_is_prime := 'Prime';
ELSE
p_is_prime := 'Not Prime';
END IF;
END;
/

Calling procedure:

DECLARE
v_result VARCHAR2(20);
BEGIN
IsPrime(17, v_result);
DBMS_OUTPUT.PUT_LINE('Result: ' || v_result);
END;
/

54
25.
A ) write a pl/sql code to update values in created tables by using implicit
cursors
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers selected ');
END IF;
END;
/

25 b. )
write pl/sql code to display employee details using explicit cursors
SQL> set serveroutput on;
SQL> DECLARE
2 CURSOR c_detail IS
3 SELECT dept_name,d.dept_id,first_name,salary
4 FROM department d JOIN emp_detail e
5 ON e.dept_id = d.dept_id
6 WHERE salary > 48000;
7 BEGIN
8 FOR item IN c_detail
9 LOOP

55
10 DBMS_OUTPUT.PUT_LINE(item.first_name||' '||item.dept_name||'
'||item.dept_id||' '||' '||item.salary);
11 END LOOP;
12 END;
13 /
mangala store 3 60000

56
26 a)
write pl/sql code in trigger not to accept the existing empno(unique no) in
emp table.
CREATE OR REPLACE TRIGGER prevent_duplicate_empno
BEFORE INSERT OR UPDATE ON emptrigger
FOR EACH ROW
DECLARE
existing_emp_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO existing_emp_count
FROM emptrigger
WHERE empno = :new.empno;
IF existing_emp_count > 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee number already exists in the
emp table');
END IF;
END;
/
26 b)
write a pl/sql code using trigger to inform if the new salary of an emp is less
than old salary
CREATE OR REPLACE TRIGGER check_salary_increase
BEFORE UPDATE ON myemp
FOR EACH ROW
DECLARE
v_old_salary number(10);
BEGIN
SELECT salary
INTO v_old_salary
FROM myemp
WHERE eno = :old.eno;
IF :new.salary < v_old_salary THEN
DBMS_OUTPUT.PUT_LINE('Warning: New salary is less than old salary for
employee ' || :old.eno);
END IF;
end;

57
27 Create the following tables for library information system :
book : (accession-no, title, publisher, author, status)
status could be issued, present in the library, sent for binding, and cannot be
issued
Write atrigger which sets the status of a book to "cannot be issued", if it is
published 20 years back
CREATE TABLE book( accno char(10) PRIMARY KEY , title
varchar2(30),publisher varchar2(20),author varchar2(10),status
char(10),dtpur date);
create or replace trigger book_stat_trig
after INSERT on book
begin
update book set status='cantissue' where
(months_between(sysdate,dtpur)/12)>=20;
end;
/

SQL> insert into book values(101,'aaa','bbbb','sriram',' ','02-jun-2001');

SQL> select *from book;

58
28.. Create the following tables :

Book(accession-no, title, publisher, year, date-of-

purchase, status) Member(member-id, name, number-

of-books-issued, max-limit) book-issue(accession-no,

member-id, date-of-issue) A)write a PL/SQL procedure

to issue the book.

Write a trigger to set the status of students to “back listed” if they

have take book but not returned even after one year.

CREATE TABLE Book6 ( accession_no NUMBER PRIMARY KEY, title


VARCHAR2(255), publisher VARCHAR2(255), year NUMBER,
date_of_purchase DATE, status VARCHAR2(50)

CREATE TABLE Member6 (

member_id NUMBER PRIMARY

KEY, name VARCHAR2(255),

number_of_books_issued

NUMBER, max_limit NUMBER );

CREATE TABLE

Book_issue6 (

accession_no NUMBER,

member_id NUMBER,

date_of_issue DATE,

FOREIGN KEY (accession_no) REFERENCES Book(accession_no),

FOREIGN KEY (member_id) REFERENCES Member(member_id)


);
59
CREATE OR REPLACE PROCEDURE

IssueBook( p_accession_no NUMBER,

p_member_id NUMBER

) AS

BEGIN

-- Check if the member has reached the maximum limit of books issued

SELECT number_of_books_issued INTO Member.number_of_books_issued

FROM Member

WHERE member_id = p_member_id;


IF Member.number_of_books_issued >= Member.max_limit THEN
RAISE_APPLICATION_ERROR(-20001, 'Member has reached the maximum
limit of books
issued.');

END IF;
-- Check if the book is available

SELECT status INTO Book.status

FROM Book

WHERE accession_no = p_accession_no;


IF Book.status = 'Issued' THEN

RAISE_APPLICATION_ERROR(-20002, 'The book is already issued.');

END IF;
- Issue the book

INSERT INTO Book_issue (accession_no, member_id, date_of_issue)

VALUES (p_accession_no, p_member_id, SYSDATE);


-- Update member's number of books issued

60
UPDATE Member

SET number_of_books_issued = number_of_books_issued + 1

WHERE member_id = p_member_id;


-- Update book status

UPDATE Book

SET status = 'Issued'

WHERE accession_no = p_accession_no;


COMMIT;

END IssueBook;
/

SELECT * FROM Book6;

SELECT * FROM Member6;

SELECT * FROM Book

_issue6;

61
Write a trigger to set the status of students to “back listed” if they have take
book but not returned even after one year.
CREATE OR REPLACE TRIGGER SetBacklistedStatus

BEFORE INSERT OR UPDATE ON Book_issue


FOR EACH ROW

DECLARE v_issue_date

DATE;

BEGIN

IF INSERTING THEN

-- New book issue

v_issue_date :=

:NEW.date_of_issue;

ELSE

-- Update to book issue (not relevant in this context)

RETURN;

END IF;
-- Check if the book issue is overdue (more than one year)

IF (SYSDATE - v_issue_date) > 365 THEN

-- Set the status of the member to "back listed"

UPDATE Member

SET status = 'Back listed'

WHERE member_id = :NEW.member_id;

END IF;

62
END SetBacklistedStatus;
/

63
29. Create the following tables :
Book(accession-no, title, publisher, year, date-of-purchase, status)
Member(member-id, name, number-of-books-issued, max-limit)
Book-issue(accession-no, member-id, date-of-issue)
Creating tables:
SQL> create table book29(accesion_no number(4) primary key,title
varchar(15),publisher varchar(15),year number(4), dop date,status varchar(20));
SQL> insert into book29 values(101,'aaa','chand','2012','04-jun-2012',' ');
1 row created.
SQL> insert into book29 values(102,'bbb','himalay','2013','04-jun-2013',' ');
SQL> create table member29(mid number(10),name varchar(10),nofbooksissue
number(10),maxlimit number(2));
Table created.
SQL> insert into member29 values(1,'sandeep',2,2);
1 row created.
SQL> insert into member29 values(2,'narasimha',1,2);
1 row created.
SQL> insert into member29 values(3,'sai',2,2);
1 row created.
SQL> insert into member29 values(4,'bang',3,3);
1 row created.
SQL> insert into member29 values(5,'kiran',2,2);
1 row created.
SQL> create table book_issue29 (ACCESION_NO number(10),mid number(10),dateof
issue date);
Table created.
SQL> insert into book_issue29 values(101,1,'12-jun-2023');
1 row created.
SQL> insert into book_issue29 values(102,1,'12-jun-2023');
1 row created.
SQL> insert into book_issue29 values(102,2,'12-jun-2023');
1 row created.
SQL> insert into book_issue29 values(103,3,'12-jun-2023');
1 row created.

(b) Write a PL/SQL procedure to issue the book

64
SQL> create or replace procedure p11
is
ac_no book29.accesion_no%type;
sta book29.status%type;
begin
ac_no:='&ac_no';
select status into sta from book29 where accesion_no=ac_no;
if(sta='not issued') then

update book29 set status='issued' where accesion_no=ac_no;


dbms_output.put_line('book issued');
else
dbms_output.put_line('already book has been issued');
end if;
end;
/
Enter value for ac_no:
old 6: ac_no:='&ac_no';
new 6: ac_no:='';
Procedure created.

(i) Write a trigger to set the status of students to "back listed" if they have taken
65
book but not returned even after one year.
Procedure:
create or replace procedure p11
is
ac_no book29.accesion_no%type;
sta book29.status%type;
begin
ac_no:='&ac_no';
select status into sta from book29 where accesion_no=ac_no;
if(sta='not issued') then

update book29 set status='issued' where accesion_no=ac_no;


dbms_output.put_line('book issued');
else
dbms_output.put_line('already book has been issued');
end if;
end;
create or replace trigger t1
before insert or update on book_issue29
for each row
begin
if(:new.dateofissue<'01-jan-07')then
update book29 set status='back listed';
end if;
end;
/

66
67
30 a)
write a pl/sql block to handle the BUILT In Exceptions.
DECLARE
c_id customers.customer_id%type := 8;
c_name customerS.Name%type;
c_addr customers.address%type;
BEGIN
SELECT name, address INTO c_name, c_addr
FROM customers
WHERE customer_id= c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);

EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
/
30 b)
write a pl/sql code to demonstrate if statement in trigger.
SQL> create table emp(eno number(10),ename varchar(10),salary
number(10),commission number(3)selec);
Table created.
SQL> insert into emp values(1,'aaa',20000);
1 row created.
SQL> insert into emp values(2,'sss',20000);
1 row created.
SQL> insert into emp values(2,'sss',60000);
1 row created.
CREATE OR REPLACE TRIGGER demonstrate_if_statement
BEFORE INSERT OR UPDATE ON emp
FOR EACH ROW
BEGIN
IF :new.salary > 50000 THEN
:new.commission := 0;

68
DBMS_OUTPUT.PUT_LINE('Commission set to 0 because the salary is greater
than 50000.');
ELSE
DBMS_OUTPUT.PUT_LINE('Commission remains unchanged.');
END IF;
END;
/

69

You might also like