0% found this document useful (0 votes)
58 views

Basic PL/SPL Programs and Triggers Experiment No: 1: Program

The document describes several PL/SQL programs and triggers: 1. Five programs that calculate the sum of odd numbers, generate the Fibonacci sequence, check if a string is a palindrome, debit an account, and display student marks and percentage. 2. A trigger is created to audit changes to client records by inserting updated and deleted records into an audit table. 3. Tables are created for bank accounts and loans. A trigger moves a negative balance from an account into the loans table when an account is updated.

Uploaded by

davis_sebastian
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Basic PL/SPL Programs and Triggers Experiment No: 1: Program

The document describes several PL/SQL programs and triggers: 1. Five programs that calculate the sum of odd numbers, generate the Fibonacci sequence, check if a string is a palindrome, debit an account, and display student marks and percentage. 2. A trigger is created to audit changes to client records by inserting updated and deleted records into an audit table. 3. Tables are created for bank accounts and loans. A trigger moves a negative balance from an account into the loans table when an account is updated.

Uploaded by

davis_sebastian
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

BASIC PL/SPL PROGRAMS AND TRIGGERS

EXPERIMENT NO: 1

To find the sum of odd numbers upto a given limit.

PROGRAM

DECLARE
num number(4);
s number(4):=0;
i number(4);
a number(4):=1;
BEGIN
num:=#
FOR i IN 1..num
LOOP
s:=s+a;
a:=a+2;
END LOOP;
DBMS_OUTPUT .PUT_LINE('sum of first'||num||'number is:'||s);
END;

OUTPUT

Enter value for num: 3

sum of first 3 number is:9


EXPERIMENT NO: 2

Fibonacci series of n numbers.

PROGRAM

DECLARE
limit number(4);
a number(4):=0;
b number(4):=1;
c number(4);
BEGIN
limit:=&limit;
DBMS_OUTPUT.PUT_LINE(a);
FOR i IN 1..limit-1
LOOP
DBMS_OUTPUT.PUT_LINE(b);
c:=b;
b:=a+b;
a:=c;
END LOOP;
END;

OUTPUT

Enter value for limit: 5


old 7: limit:=&limit;
new 7: limit:=5;
0
1
1
2
3
EXPERIMENT NO: 3

To check whether a string is palindrome or not.

PROGRAM

DECLARE
string varchar(30);
l number(4);
a number(4);
flag number(2):=0;
BEGIN
string:='&string';
l:=length(string);
a:=l;
FOR i IN 1..l
LOOP
IF(substr(string,i,1)=substr(string,a,1)and flag=0)
THEN
flag:=0;
else
flag:=1;
END IF;
a:=a-1;
END LOOP;
IF(flag=1)
THEN
DBMS_OUTPUT.PUT_LINE('NOT PALINDROME');
ELSE
DBMS_OUTPUT.PUT_LINE('PALINDROME');
END IF;
END;

OUTPUT

Enter value for string: malayalam


old 7: string:='&string';
new 7: string:='malayalam';
PALINDROME
EXPERIMENT NO: 4

To debit 2000 from an account in the account table.

SQL> create table account(acc_id number(4),name varchar(10),balance number(10,2));

SQL> desc account;


Name Null? Type
----------------------------------------- -------- --------------------
ACC_ID NUMBER(4)
NAME VARCHAR2(10)
BALANCE NUMBER(10,2)

SQL> select * from account;

ACC_ID NAME BALANCE


---------- ---------- ----------
1 davi 1000
2 rafy 1000
3 kevin 9000
4 lax 2600
5 johny 3800.5
6 kaza 4000

PROGRAM

DECLARE
accno number(4);
amount number(10,2);
BEGIN
accno:=&accno;
select balance into amount from account where acc_id=accno;
amount:=amount-2000;
IF(amount<500)
THEN
DBMS_OUTPUT.PUT_LINE('NOT SUFFICIENT BALANCE');
ELSE
update account set balance=amount where acc_id=accno;
DBMS_OUTPUT.PUT_LINE('balance of acc_id='||accno||'has been debited');
END IF;
EXCEPTION
when no_data_found then
dbms_output.put_line('no data found');
END;
OUTPUT

Enter value for accno: 1


old 5: accno:=&accno;
new 5: accno:=1;
NOT SUFFICIENT BALANCE

Enter value for accno: 3


old 5: accno:=&accno;
new 5: accno:=3;
balance of acc_id=3 has been debited

After updation

SQL> select * from account;

ACC_ID NAME BALANCE


---------- ---------- ----------
1 davi 1000
2 rafy 1000
3 kevin 7000
4 lax 2600
5 johny 3800.5
6 kaza 4000
EXPERIMENT NO: 5

To find the total marks and percentage of marks from student table

SQL> create table student(rno number(5),name varchar(10),m1 number(4),m2 number(4),m3


number(4));

SQL> desc student;


Name Null? Type
----------------------------------------- -------- ----------------------------
RNO NUMBER(5)
NAME VARCHAR2(10)
M1 NUMBER(4)
M2 NUMBER(4)
M3 NUMBER(4)

SQL> select * from student;

RNO NAME M1 M2 M3
---------- ---------- ---------- ---------- ----------
1 arjun 56 46 42
2 davis 40 57 42
3 ren 30 40 59
4 ted 70 20 56
5 lily 74 93 85

PROGRAM

DECLARE
total number(5);
percentage number(7,2);
sname varchar(10);
roll number(2);
m1 number(4);
m2 number(4);
m3 number(4);
BEGIN
roll:=&roll;
select rno,name,(m1+m2+m3),(m1+m2+m3)/3,m1,m2,m3 into
roll,sname,total,percentage,m1,m2,m3 from student
where rno=roll;
dbms_output.put_line('Roll no:'||roll);
dbms_output.put_line('Name:'||sname);
dbms_output.put_line(‘Mark1:’||m1);
dbms_output.put_line(‘Mark2:’||m2);
dbms_output.put_line(‘Mark3:’||m3);
dbms_output.put_line('Total mark:'||total);
dbms_output.put_line('Percentage'||percentage);
EXCEPTION
when no_data_found then
dbms_output.put_line('no data found');
END;

OUTPUT

Enter value for roll: 3


old 7: roll:=&roll;
new 7: roll:=3;
Roll no:3
Name:ren
Total mark:129
Percentage 43
EXPERIMENT NO: 6

Audit System

SQL> create table client_master(cno number(5),name varchar(10),balance_due number(10,2),address


varchar(30));

SQL> desc client_master;


Name Null? Type
----------------------------------------- -------- ------------------
CNO NUMBER(5)
NAME VARCHAR2(10)
BALANCE_DUE NUMBER(10,2)
ADDRESS VARCHAR2(30)

SQL> select * from client_master;

CNO NAME BALANCE_DUE ADDRESS


---------- ---------- ----------- --------------
1 ted 7000 mclarens
2 barney 3000 newyork
3 robin 1520 canada
4 marshall 9040 hills
5 lily 10500 newyork

SQL> create table audit_client(cno number(5),balance_due number(10,2),operation


varchar(10),op_date date);

SQL> desc audit_client;


Name Null? Type
----------------------------------------- -------- -----------------
CNO NUMBER(5)
BALANCE_DUE NUMBER(10,2)
OPERATION VARCHAR2(10)
OP_DATE DATE

PROGRAM

create or replace trigger trig after update or delete on client_master


for each row
begin
if updating then
insert into audit_client values(:old.cno,:old.name,:old.balance_due,'update',sysdate);
end if;
if deleting then
insert into audit_client values(:old.cno,:old.name,:old.balance_due,'delete',sysdate);
end if;
end;

Output

SQL> select * from client_master;

CNO NAME BALANCE_DUE ADDRESS


---------- --------- - ----------- --------------
1 ted 7000 mclarens
2 barney 3000 newyork
3 robin 1520 canada
4 marshall 9040 hills
5 lily 10500 newyork

SQL> update client_master set balance_due=10000 where cno=5;

1 row updated.

SQL> delete client_master where cno=4;

1 row deleted.

SQL> select * from audit_client;

CNO NAME BALANCE_DUE OPERATION OP_DATE


---------- ---------- ----------- -------- ------------ ---------
5 lily 10500 update 16-SEP-10
4 marshall 9040 delete 16-SEP-10

SQL> select * from client_master

CNO NAME BALANCE_DUE ADDRESS


---------- ---------- ----------- ----------
1 ted 7000 mclarens
2 barney 3000 newyork
3 robin 1520 canada
5 lily 10000 newyork
EXPERIMENT NO: 7

Account and Loan Table

SQL> create table account2(accno number(3),name varchar(10),balance number(10,2),branch_name


varchar (20));

Table created.

SQL> desc account2;


Name Null? Type
----------------------------------------- -------- ---------------
ACCNO NUMBER(3)
NAME VARCHAR2(10)
BALANCE NUMBER(10,2)
BRANCH_NAME VARCHAR2(20)

SQL> select * from account2;

ACCNO NAME BALANCE BRANCH_NAME


---------- ---------- ---------- ------------
100 house 8000 princeton
101 chase 5000 plainsbro
102 foreman 3000 nigeria
103 cameron 2000 newyork

SQL> create table loan(loan_no number(3),amount number(10,2),branch_name varchar(20));

SQL> desc loan;


Name Null? Type
----------------------------------------- -------- ---------------
LOAN_NO NUMBER(3)
AMOUNT NUMBER(10,2)
BRANCH_NAME VARCHAR2(20)

PROGRAM

create or replace trigger actrig after update on account2


for each row
declare
amt number(10,2);
begin
if updating then
if(:new.balance<0) then
amt:=-(:new.balance);
insert into loan values(:old.accno,amt,:old.branch_name);
end if;
end if;
end;

OUTPUT

SQL> update account2 set balance=2500 where accno=103;

SQL> select * from loan;

LOAN_NO AMOUNT BRANCH_NAME


---------- ---------- -------------
103 500 newyork

SQL> select * from account2;

ACCNO NAME BALANCE BRANCH_NAME


---------- ---------- ---------- ------------
100 house 8000 princeton
101 chase 5000 plainsbro
102 foreman 3000 nigeria
103 cameron 2500 newyork
EXPERIMENT NO: 8

create table act_tran(accno varchar(5) references acct_master(ano),dot date,


amount number(10,2),debit_credit varchar(6) constraint dc check(debit_credit in ('debit','credit')),
pro_notpro varchar(30) constraint np check(pro_notpro in ('processed','notprocessed')));

SQL> desc act_tran;


Name Null? Type
----------------------------------------- -------- --------------
ACCNO VARCHAR2(5)
DOT DATE
AMOUNT NUMBER(10,2)
DEBIT_CREDIT VARCHAR2(6)
PRO_NOTPRO VARCHAR2(30)

DQL>create table acct_master(ano varchar(5) primary key,name varchar(10),balance number(10,2);

SQL> desc acct_master;


Name Null? Type
------------------------------ ----------- ------- -------------
ANO NOT NULL VARCHAR2(5)
NAME VARCHAR2(10)
BALANCE NUMBER(10,2)

PROGRAM

create or replace trigger trg after insert on act_tran


for each row
declare
value number(10,2);
amt number(7);
begin
if inserting then
if(:new.pro_notpro='processed')then
select balance into value from acct_master where
ano=:new.accno;
if(:new.debit_credit='debit')then
update acct_master set balance=(value-:new.amount) where ano=:new.accno;
elsif(:new.debit_credit='credit')then
update acct_master set balance=(value+:new.amount) where ano=:new.accno;
else
dbms_output.put_line('process not completed');
end if;
end if;
end if;
end;

OUTPUT

SQL>select * from acct_master;

ANO NAME BALANCE


----- ---------- - ---------
a1 davis 2500
a2 ted 2000
a3 barney 3000

SQL> insert into act_tran values('&accno','&dot',&amount,'&debit_credit','&pro_notpro');


Enter value for accno: a1
Enter value for dot: 24-sep-2010
Enter value for amount: 500
Enter value for debit_credit: credit
Enter value for pro_notpro: processed
old 1: insert into act_tran values('&accno','&dot',&amount,'&debit_credit','&pro_notpro')
new 1: insert into act_tran values('a1','24-sep-2010',500,'credit','processed')

1 row created.

SQL> select * from act_tran;

ACCNO DOT AMOUNT DEBIT_ PRO_NOTPRO


----- --------- ---------- ------ ------------------------------
a1 24-SEP-10 500 credit processed

SQL> select * from acct_master;

ANO NAME BALANCE


----- ---------- ----------
a1 davis 3000
a2 ted 2000
a3 barney 3000

You might also like