PL SQL1
PL SQL1
2.1) Write a PL/SÌL block which will accept a value from the user and it will
insert the factorial result into a table called Result.
PL/SQL Code:
declare
i number(5);
fnum number(5);
result number(10);=1;
begin
i-l;
fnum:= &fnum;
while(i<=fnum);
result:=result*i;
i:=it1;
end loop;
insert into fact values(fnum, result);
dbms output.put line("fact=" |result);
end;
PL/SQL Code:
declare
nnumber(10);
i number(10):=1;
sm number(20):=0;
begin
n:=&n;
while(i<=n)
loop
sm:=smti;
i:=it1;
end loop;
dbms_output.put_line('sum=" sm);
end;
3) Create abus table with the
Bus id number, no of seats number,
following
attributes
remarks varchar.
Insert first five records for bus id and no of
scats.
Writeea PL/SOL block to put the remarks in the following
1) If no_ of seats<50, remarks fairly smal. way
2) If no of seats<100, remarks little bigger.
3) If no of seats>100, remarks-lots of sets.
Ans.:
>create table BUS values(BUS ID number(2), seats number(5),Remarks varchar(15);
i)
>insert into BUS(BUS _ID,seats)
ii) values(1,40);
>insert into BUS(BUS D,seats) values(2.60):
>insert into BUS(BUS D,seats) values(3,70):
>insert into BUS(BUS_D,seats) values(4,110);
PL/SQL code:
declare
mbus id BUS054.BUS IDotype; .
mseats BUSO54.seats%type:
mremarks BUS054.remarks%type;
begin
mbus id :=&mbus id:;
select seats into mseats from BUS054 where BUS ID=mbus id:
if mseats<50 then
mremarks:= 'Fairly Small';
mremarks ='Little Bigger';
elsif mseats<100 then
else
mremarks:='Lots of seats';
end if;
update BUSO54 set Remarks=mremarks where BUS ID=mbus id;
Exception
when no data found then
dbms_output.put line(mbus_id ||'is not present in the
table');
end;
Q.1) AHRD Manager decided to raise the salary of employee by 15%. Write a
PL/SQL block which accept employee number and update
the the record
table. Display the appropriate message on nonexistence of salary in Employee
in the
employee table.
PL/SQL code:
Declare
Employee no number(4);
Begin
Employee no;=&employee_no;
Update employee set sal-sal+sal*.15 where empno-employee no;
if sq2% not found then raise no data found:
end if;
Exception
When no data found then
Dbms_output.put_line(»employee number is not found' ):
End;
Assignment::03 (Trigger)
[Making security of Database)
A trigger is a database object which resides in database that fires or execute implicitly when an
event Occurs.
A trigger has three components:
1) The trigger statement
2) Trigger restriction
3) Trigger action
The event for which trigger fires is defined in the trigger statement and it's restriction.
Trigger is of two types:
1. Before trigger (Before event occurs).
2. After trigger (After event or commit occurs).
Two categories of trigger:
1. Row trigger (For each row trigger 1s fired)
2. Statement trigger (It is independent of number of rows. Only one time trigger is fired
and it is independent of no of rows).
Q.1: Create tables client_master and audit master and write a before trigger for
updation, deletion and insertion for each row on client_master, i.e. the system must
keep track of the records that are being inserted, deleted or uploaded on
client master. The original record detail and date of operation along with user jd
are stored in audit master table and then the insertion, updation and deletion is
allowed to go througlh.
Ans. 1
client master
>create table client master(client no varchar2(5) primary key, name varchar2(10).
balance due number(10,2);
Singert in to client master(client no,name.balance due) values('c11'.Abhishek,7000.20)
Sinsert in to client_master(client no,name,balance due) values('c12,Komal,8000);
binsert in to client_master(client no,name,balance due) values('c13", 'Raju', 10000);
Audit master
>create table audit master(client no varchar2(5),name varchar2(10), balance due
'DELETE;
END IF;
insert into audit master903 values(mcl no,mname,mbalance,mop,sysdate);
end;