You are on page 1of 105

Week-4: PROGRAMS ON PL/SQL

1.a) write a PL/SQL program for swapping 2 numbers.

SQL> set serveroutput on

declare a
number:=&a; b
number:=&b;
begin
dbms_output.put_line('Before swapping a='||a||' and b='||b);
a:=a+b; b:=a-b; a:=a-b;
dbms_output.put_line('After swapping a='||a||' and b='||b); end;
/
Output: -
Enter value for a: 12 old 2: a
number:=&a; new 2: a
number:=12; Enter value for b:
24 old 3: b number:=&b; new
3: b number:=24; Before
swapping a=12 and b=24 After
swapping a=24 and b=12

1.b) Write a PL/SQL block to find the maximum number from given three numbers.

declare a
number:=&a; b
number:=&b; c
number:=&c;
begin
if (a>b and a>c) then
dbms_output.put_line('a is maximum ' || a); elsif
(b>a and b>c) then
dbms_output.put_line('b is maximum ' || b); else
dbms_output.put_line('c is maximum ' || c); end
if;
end;
/
Output: -
Enter value for a: 4 old
2: a number:=&a; new
2: a number:=4; Enter
value for b: 12 old 3:
b number:=&b; new
3: b number:=12;
Enter value for c: 24
old 4: c number:=&c;
new 4: c
number:=24;
c is maximum 24

2.a) Write a PL/SQL program to find the total and average of 6 subjects and display the
grade.

DECLARE sub1 NUMBER := &subject1;


sub2 NUMBER := &subject2; sub3 NUMBER :=
&subject3; sub4 NUMBER := &subject4; sub5
NUMBER := &subject5; sub6 NUMBER :=
&subject6; total NUMBER; average NUMBER;
grade VARCHAR2(10); BEGIN total := sub1 +
sub2 + sub3 + sub4 + sub5 + sub6; average :=
total/6;
CASE
WHEN average >= 90 THEN
grade := 'A+';
WHEN average >= 80 THEN
grade := 'A';
WHEN average >= 70 THEN
grade := 'B';
WHEN average >= 60 THEN
grade := 'C';
WHEN average >= 50 THEN
grade := 'D';
ELSE
grade := 'F';
END CASE;
DBMS_OUTPUT.PUT_LINE('Total marks: ' || total);
DBMS_OUTPUT.PUT_LINE('Average marks: ' || average);
DBMS_OUTPUT.PUT_LINE('Grade: ' || grade);
END;
/
Output: -
Enter value for subject1: 92 old 2:
sub1 NUMBER := &subject1; new 2:
sub1 NUMBER := 92; Enter value for
subject2: 91 old 3: sub2 NUMBER
:= &subject2; new 3: sub2
NUMBER := 91; Enter value for
subject3: 90 old 4: sub3 NUMBER
:= &subject3; new 4: sub3
NUMBER := 90; Enter value for
subject4: 94 old 5: sub4 NUMBER
:= &subject4; new 5: sub4
NUMBER := 94; Enter value for
subject5: 92 old 6: sub5 NUMBER
:= &subject5; new 6: sub5
NUMBER := 92; Enter value for
subject6: 93 old 7: sub6 NUMBER
:= &subject6; new 7: sub6
NUMBER := 93;
Total marks: 552
Average marks: 92
Grade: A+

2.b) Write a PL/SQL program to find the sum of digits in a given number.

declare n
number(5):=&n; s
number:=0; r
number(2):=0;
begin while n !=0
loop
r:=mod(n,10);
s:=s+r;
n:=trunc(n/10);
end loop;
dbms_output.put_line('Sum of digits of given number is '||s); end;
/
Output: -
Enter value for n: 2356 old 2: n
number(5):=&n; new 2: n
number(5):=2356; Sum of digits of
given number is 16

3.a) Write a PL/SQL Program to accept a number from user and print number in reverse
order.

declare
num1 number(5):=&num1;
num2 number(5); rev
number(5); begin rev:=0;
while num1>0 loop
num2:=num1 mod 10;
rev:=num2+(rev*10);
num1:=floor(num1/10);
end loop;
dbms_output.put_line('Reverse number is: '||rev); end;
/
Output: -
Enter value for num1: 256 old 2:
num1 number(5):=&num1; new 2:
num1 number(5):=256; Reverse
number is: 652

3.b) Write a PL / SQL program to check whether the given number is prime or not.

declare num
number; i
number:=1;
c number:=0;
begin
num:=#
for i in 1..num loop if((mod(num,i))=0)
then c:=c+1; end if; end loop; if(c>2)
then dbms_output.put_line(num||' not a
prime'); else dbms_output.put_line(num||'
is prime'); end if;
end;
/
Output: -
Enter value for num: 37
old 6: num:=#
new 6: num:=37; 37
is prime

4.a) Write a PL/SQL program to find the factorial of a given number.

declare i
number(4):=1; n
number(4):=&n; f
number(4):=1;
begin
for i in 1..n loop f:=f*i; end loop;
dbms_output.put_line('The factorial of '||n||' is:'||f);
end;
/
Output: - Enter value for
n: 5 old 3: n
number(4):=&n; new 3:
n number(4):=5; The
factorial of 5 is:120

4.b) Calculate the area of a circle for a value of radius varying from 3 to 7. Store the radius
and the corresponding values of calculated area in table areas. Consisting of two columns
radius and area.

declare

radius number;

area number;

begin

radius. & radius;

area: round (3.141* radius radius, 2);

EXECUTE IMMEDIATE 'create table Circle ( radius int, area int);

insert into circle Values (radius, area);

dbms_output.put_line('Radius is' || radius || ‘Area is’||area);

end;
/
Output:
Enter a value for radius :5
Radius is 5 Area is 78.55
PL/SQL procedure successfully completed
Select * from circle;
Radius Area
5 79
5.a) Write a PL/SQL program to accept a string and remove the vowels from the string.
(When ‘hello’ passed to the program it should display ‘Hll’ removing e and o from the
word Hello).

set serveroutput on set


verify off
accept vstring prompt "Please enter your string: "; declare
vnewstring varchar2(100); begin
vnewstring := regexp_replace('&vstring', '[aeiouAEIOU]','');
dbms_output.put_line('The new string is: ' || vnewstring); end;
/
Output: -
old 4: vnewstring := regexp_replace('&vstring', '[aeiouAEIOU]',''); new 4:
vnewstring := regexp_replace('DhanushVardhan', '[aeiouAEIOU]',''); The
new string is: DhnshVrdhn

5.b) Write a PL/SQL program to accept a number and a divisor. Make sure the divisor is less
than or equal to 10. Else display an error message. Otherwise Display the remainder.

insert all

into words (0, 'zero);

into words (1, 'one')

into words (2, 'two')

into words (3, 'three')

into words (4, 'four')

into words (5, 'five')

into words (6, 'Six')

into words (7, 'Seven')

into words (8, 'eight')

into words (9, 'Nine')

Inserted successfully

declare
num number;

divisor number;

rem number;

remno Varchar2(20);

begin

num = &num,

divisior = & divisior;

if divisior > 10 then

dbms_output.put-line('divisior must be less than or equal to 10);

else

rem; mod (num, division);

Select word into remwo from words


where no = rem;

end if;

dbms_output.put-line (remwo);

end;
\

Output: -

SQL> edit sqltest

Enter value for num: 10

Enter value for divisior: 10

Zero

procedure successfully completed


Week-5: PROCEDURES AND FUNCTIONS

1. Write a function to accept employee number as parameter and return Basic +HRA together
as single column?

CREATE TABLE employees (


eid NUMBER,
bsalary NUMBER,
hra NUMBER
);

SQL>Insert all
Into employee values(1001,1200,1200)
Into employee values(1002,13209,1300)
Into employee values(1003,18293,1800)
Into employee values(1004,10929,1000)
Select 1 from dual;
4 rows created.
SQL> select * from employee;

EID BSALARY HRA


---------- ---------- ----------
1001 1200 1200
1002 13209 1300
1003 18293 1800
1004 10929 1000

CREATE OR REPLACE FUNCTION emp_total_sal(empid NUMBER)


RETURN NUMBER
IS
bsalarye NUMBER;
hras number;
total number;
BEGIN
SELECT bsalary ,hra INTO bsalarye,hras
FROM employees
WHERE eid=empid;
Total:=bsalarye+hras;
RETURN total;
END;
/
Output: -
SQL>@sqltest
Function created
SQL>select
emp_total_sal(1001)
as total_from dual;
Total
13200

2. Accept year as parameter and write a Function to return the total net salary spent for a
given year?
create table employee(salary number,join_date date);
insert all
into employee values(60000,TO_DATE('2020-01-01','YYYY-MM-DD'))
into employee values(85670,TO_DATE('2021-03-15','YYYY-MM-DD'))
into employee values(95670,TO_DATE('2022-07-01','YYYY-MM-DD'))
into employee values(95970,TO_DATE('2023-02-01','YYYY-MM-DD'))
into employee values(95970,TO_DATE('2024-02-01','YYYY-MM-DD'))
select 1 from dual;
SQL>select * from employee;
SALARY JOIN_DATE
---------- ---------
60000 01-JAN-20
85670 15-MAR-21
95670 01-JUL-22
95970 14-FEB-23
95970 14-FEB-24
SQL>edit sqltest

CREATE OR REPLACE FUNCTION net_salary(year_in IN NUMBER)


RETURN NUMBER IS
sal NUMBER;
BEGIN
Select salary into sal from employee
Where EXTRACT(year from join_date)=year_in;

RETURN sal;
END;
/
Output: -
SQL>@sqltest
SQL> SELECT net_salary(2021) FROM dual;
NET_SALARY(2021)
--------------------------------
85670
3. Create a function to find the factorial of a given number and hence find NCR.
create or replace function
factorial(number)
return number
is
fact number:=1;
begin
for I in 1..n loop
facts:=fact*I;
end loop’
return facts;
end factorial;
/
Output: -
@sqltest
Function created successfully

Declare
n number;
r number;
nume number;
decn number;
res number;
begin
n:=&n;
r:=&r;
nume:=factorial(n);
decn:=factorial(n-r) * factorial(r);
res:=nume/decn;
dbms_output.put_line(res);
end;
/
OUTPUT:-
Enter value for n:12
Enter value for r:12
1
Pl/sql procedure successfully complted.
4. Print prime Fibonacci series using local functions.
Create or replace function fibo( A in number)
Return Number
is
begin
if A<=1 then
return A
else
return fibo(A-1) + fibo(A-2);
end if;
end fibo;

SQL>@sqltest
function created successfully
To execute

Output:-
Declare
N number;
Begin
N:=&N;
for i in 1..N loop
DBMS_OUTPUT.PUT_LINE(fibo(i));
End loop;
End;
/
Enter value for n:=5
1
1
2
3
5
PL/SQL procedure successfully completed

5. Create a procedure to find the lucky number of a given birthdate.


CREATE OR REPLACE PROCEDURE p_lucky(birthdate_in IN DATE,
lucky_number OUT NUMBER)
IS
total NUMBER := 0;
BEGIN
FOR i IN 1..LENGTH(TO_CHAR(birthdate_in, 'YYYYMMDD')) LOOP
total := total + TO_NUMBER(SUBSTR(TO_CHAR(birthdate_in, 'YYYYMMDD'), i,
1));
END LOOP;
lucky_num := MOD(total, 10);
DBMS_OUTPUT.PUT_LINE('Your lucky number is: ' || lucky_number_out);
END;
/
DECLARE
lucky_num NUMBER; BEGIN get_lucky_number(TO_DATE('2004-09-
13', 'YYYY-MM-DD'), lucky_num);
END;
/
Output: -
Your lucky number is: 9
Total is 19
This indicates that the lucky number for the given birthdate is 8, which is calculated by
summing the digits in the birthdate (2+0+0+3+0+7+2+4 = 18) and taking the modulus of the
sum with 10 (18 % 10 = 8).

6. Create function to the reverse of given number?


CREATE OR REPLACE FUNCTION reverse_number(num IN NUMBER) RETURN
NUMBER
IS
Rev NUMBER:=0;
temp_num NUMBER:=num;
BEGIN
WHILE temp_num>0 LOOP
rev:=rev*10+mod(temo_num,10);
temp_num:=trunc(temp_num/10);
end loop;
return rev;
end;
/
Output: -
Function created successfully
SELECT reverse_number(12345) AS reversed_num FROM dual;

REVERSED_NUM
--------------------------
54321
Week-6: TRIGGERS

1. Create a row level trigger for the customers table that would fire for INSERT or UPDATE or
DELETE operations performed on the CUSTOMERS table. This trigger will display the salary
difference between the old values and new values:

ID NAME AGE ADDRESS SALARY


1 Alive 24 Khammam 2000
2 Bob 27 Kadapa 3000
3 Catri 25 Guntur 4000
4 Dena 28 Hyderabad 5000
5 Eshwar 27 Kurnool 6000
6 Farooq 28 Nellore 7000

SQL> create table customer1(id number,name varchar2(10),age number,address


varchar2(20),salary number); Table created.
SQL> insert into customer1 values(1,'Alice',24,'Khammam',2000); 1
row created.
SQL> insert into customer1 values(2,'Bob',27,'Kadapa',3000); 1
row created.
SQL> insert into customer1 values(3,'Catri',25,'Guntur',4000); 1
row created.
SQL> insert into customer1 values(4,'Dena',28,'Hyderabad',5000); 1
row created.
SQL> insert into customer1 values(5,'Eshwar',27,'Kurnool',6000); 1
row created.
SQL> insert into customer1 values(6,'Farooq',28,'Nellore',7000); 1
row created.
SQL> select * from customer1;
ID NAME AGE ADDRESS SALARY
----------- ----------------- ---------- -------------------- --------------
1 Alice 24 Khammam 2000
2 Bob 27 Kadapa 3000
3 Catri 25 Guntur 4000
4 Dena 28 Hyderabad 5000
5 Eshwar 27 Kurnool 6000
6 Farooq 28 Nellore 7000

SQL> CREATE OR REPLACE TRIGGER customer1_salary_diff


AFTER INSERT OR UPDATE OR DELETE ON customer1
FOR EACH ROW DECLARE
salary_diff NUMBER;
BEGIN
IF INSERTING THEN
salary_diff := :NEW.salary;
ELSIF UPDATING THEN
salary_diff := :NEW.salary - :OLD.salary;
ELSIF DELETING THEN
salary_diff := - :OLD.salary;
END IF;

DBMS_OUTPUT.PUT_LINE('Salary difference: ' || salary_diff);


END;
/
Trigger Created.

Output: -

SQL> UPDATE customer1 SET salary = salary + 1000 WHERE id = 1;


Salary difference: 1000
1 row updated.
SQL> select * from customer1;
ID NAME AGE ADDRESS SALARY
----------- ----------------- ---------- -------------------- --------------
1 Alice 24 Khammam 3000
2 Bob 27 Kadapa 3000
3 Catri 25 Guntur 4000
4 Dena 28 Hyderabad 5000
5 Eshwar 27 Kurnool 6000
6 Farooq 28 Nellore 7000
2. Creation of insert trigger, delete trigger, update trigger practice triggers using the passenger
database. Passenger( Passport_ id INTEGER PRIMARY KEY, Name VARCHAR (50)
NotNULL, Age Integer Not NULL, Sex Char, Address VARCHAR (50) NotNULL);

SQL> create table passenger1 (passport_id varchar2(20),name varchar2(20),age number,sex


varchar2(2),address varchar2(25));
Table created.
SQL> insert into passenger1 values('123456','John Doe',35,'M','123 Main St');
1 row created.
SQL> insert into passenger1 values('234567','Jane Smith',28,'F','456 Oak Ave');
1 row created.
SQL> insert into passenger1 values('345678','Bob Johnson',42,'M','789 Elm St');
1 row created.
SQL> insert into passenger1 values('456789','Alice Lee',22,'F','456 Oak Ave');
1 row created.
SQL> select * from passenger1;

PASSPORT_ID NAME AGE S ADDRESS


-------------------- -------------------- ---------- -- -------------------------
123456 John Doe 35 M 123 Main St
234567 Jane Smith 28 F 456 Oak Ave
345678 Bob Johnson 42 M 789 Elm St
456789 Alice Lee 22 F 456 Oak Ave

a) Write a Insert Trigger to check the Passport_id is exactly six digits or not.
SQL> CREATE OR REPLACE TRIGGER passenger_insert
BEFORE
INSERT ON passenger1
FOR EACH ROW DECLARE
num NUMBER(2);
BEGIN
num := LENGTH(:NEW.passport_id);
IF num != 6 THEN
RAISE_APPLICATION_ERROR(-20001, 'Passport ID should be exactly 6 digits');
END IF;
END;
/
Trigger Created.
Output: -
SQL> insert into passenger1 values(11111,’MANOJ’,19,’MALE’,’kadapa’);
*
ERROR at line 1:
ORA-20001: Passport ID should be exactly 6 digits

b) Write a trigger on passenger to display messages ‘1 Record is inserted’, ‘1 record is


deleted’, ‘1 record is updated’ when insertion, deletion and updation are done on passenger
respectively.
CREATE OR REPLACE TRIGGER ludpass
AFTER INSERT OR UPDATE OR DELETE ON passenger
FOR EACH ROW
BEGIN
IF INSERTING THEN
DBMS_OUTPUT.PUT_LINE('1 Record is inserted');
ELSIF DELETING THEN
DBMS_OUTPUT.PUT_LINE('1 Record is deleted');
ELSIF UPDATING THEN
DBMS_OUTPUT.PUT_LINE('1 Record is updated');
END IF;
COMMIT;
END;
/
SQL>@sqltest
Trigger Created.
Output: -
SQL> insert into passenger values(11111,’mahesh’,20,’male’,’kadapa’);

1 Record is inserted
1 row created.
3. Insert row in employee table using Triggers. Every trigger is created with name any trigger
have same name must be replaced by new name. These triggers can raised before insert,
update or delete rows on data base. The main difference between a trigger and a stored
procedure is that the former is attached to a table and is only fired when an INSERT,
UPDATE or DELETE occurs.

CREATE TABLE employee24(


emp_id NUMBER ,
emp_name VARCHAR2(50),
emp_salary NUMBER
);

SQL>edit sqltest
CREATE OR REPLACE TRIGGER before_insert_employee24
BEFORE INSERT OR UPDATE OR DELETE ON employee24
FOR EACH ROW DECLARE
trigger_name VARCHAR2(30) := 'before_insert_employee24';
trigger_count NUMBER := 0;
BEGIN
SELECT COUNT(*) INTO trigger_count
FROM user_triggers
WHERE trigger_name = trigger_name;

IF trigger_count > 1 THEN


trigger_name := trigger_name || '_' || trigger_count;
END IF;
dbms_output.put_line('Trigger name: ' || trigger_name);
END;
/
Trigger Created.

Output: -

SQL> INSERT INTO employee24 VALUES (1, 'John Doe', 5000);


Trigger name: before_insert_employee24_5 1 row created.
SQL> INSERT INTO employee24 VALUES (2, 'Jane Smith', 6000);
Trigger name: before_insert_employee24_5 1 row created.
SQL> update employee24 set emp_salary=7000 where emp_id=2;
Trigger name: before_insert_employee24_5 1 row updated.
SQL> delete from employee24 where emp_id=2;
Trigger name: before_insert_employee24_5 1
row deleted.

4. Convert employee name into uppercase whenever an employee record is inserted or


updated. Trigger to fire before the insert or update.
SQL> create table employee25(emp_id number,emp_name varchar2(20),emp_salary
number); Table created.

SQL> CREATE OR REPLACE TRIGGER before_insert_update_employee


BEFORE INSERT OR UPDATE ON employee25
FOR EACH ROW
BEGIN
:new.emp_name := UPPER(:new.emp_name);
END;
/
SQL>@sqltest
Trigger Created.
Output: -

SQL> insert into employee25 values(1,'manoj’,50000);


1 row created.
SQL> select * from employee25;
EMP_ID EMP_NAME EMP_SALARY
-------------- -------------------- ----------------------
1 MANOJ 50000
5. Trigger before deleting a record from emp table. Trigger will insert the row to be deleted
into table called delete _emp and also record user who has deleted the record and date and
time of delete.

SQL> create table emp321 (emp_id number,emp_name varchar2(20),emp_salary number);


Table created.
SQL> insert into emp321 values(1,'John Doe',50000); 1
row created.
SQL> insert into emp321 values(2,'Jane Smith',60000); 1
row created.
SQL> insert into emp321 values(3,'Bob Johnson',70000); 1
row created.
SQL> select * from emp321;
EMP_ID EMP_NAME EMP_SALARY
---------------- -------------------- -------------------
1 John Doe 50000
2 Jane Smith 60000
3 Bob Johnson 70000

SQL> CREATE TABLE deleted_emp321 (


2 emp_id NUMBER,
3 emp_name VARCHAR2(50),
4 delete_user VARCHAR2(50),
5 delete_date DATE
6 );
Table created.

SQL> CREATE OR REPLACE TRIGGER before_delete_emp321


2 BEFORE DELETE ON emp321
3 FOR EACH ROW
4 DECLARE
5 v_user VARCHAR2(50) := USER;
6 BEGIN
7 INSERT INTO deleted_emp321 (emp_id, emp_name, delete_user, delete_date)
8 VALUES (:OLD.emp_id, :OLD.emp_name, v_user, SYSDATE);
9 END;
10 /
Trigger created.
Output: -
SQL> delete from emp321 where emp_id=2; 1
row deleted.
SQL> select * from deleted_emp321;

EMP_ID EMP_NAME DELETE_USER DELETE_DATE


---------- ------------------- -------------- ------------
2 Jane Smith SYSTEM 23-APR-23

6. Create a transparent audit system for a table CUST_MSTR. The system must keep track of
the records that are being deleted or updated

-- Sample attributes for the CUST_MSTR table


CREATE TABLE CUST_MSTR (
CUST_ID NUMBER PRIMARY KEY,
CUST_NAME VARCHAR2(50),
CUST_ADDRESS VARCHAR2(100),
CUST_PHONE VARCHAR2(20),
CUST_EMAIL VARCHAR2(50),
CUST_STATUS VARCHAR2(10)
);

-- Sample attributes for the CUST_MSTR_AUDIT table


CREATE TABLE CUST_MSTR_AUDIT (
USERNAME VARCHAR2(30),
TIMESTAMP TIMESTAMP,
ACTION VARCHAR2(10),
OLD_VALUE CLOB,
NEW_VALUE CLOB
);

CREATE OR REPLACE TRIGGER CUST_MSTR_AUDIT_TRG


AFTER DELETE OR UPDATE ON CUST_MSTR
FOR EACH ROW DECLARE
action VARCHAR2(10);
old_value CLOB;
new_value CLOB;
BEGIN
IF DELETING THEN
action := 'DELETE'; old_value := TO_CLOB(TO_CHAR(:OLD.CUST_ID) || ','
||:OLD.CUST_NAME || ','
||:OLD.CUST_ADDRESS || ',' || :OLD.CUST_PHONE || ',' || :OLD.CUST_EMAIL || ',' ||
:OLD.CUST_STATUS);
new_value := NULL;
ELSE

action := 'UPDATE'; old_value :=


TO_CLOB(TO_CHAR(:OLD.CUST_ID) || ',' ||
:OLD.CUST_NAME || ',' ||
:OLD.CUST_ADDRESS || ',' ||
:OLD.CUST_PHONE || ',' ||
:OLD.CUST_EMAIL || ',' ||
:OLD.CUST_STATUS); new_value :=
TO_CLOB(TO_CHAR(:NEW.CUST_ID) || ',' ||:NEW.CUST_NAME || ','
||:NEW.CUST_ADDRESS || ',' ||:NEW.CUST_PHONE || ',' ||:NEW.CUST_EMAIL || ',' ||
:NEW.CUST_STATUS);

END IF;

INSERT INTO CUST_MSTR_AUDIT


(USERNAME,TIMESTAMP,ACTION,OLD_VALUE,NEW_VALUE) VALUES
(USER,SYSTIMESTAMP, action,old_value,new_value);

END;
/
Output: -
SQL> insert into cust_mstr values (1,'John Smith','123 Main St,Anytown','555-
1234','jntu@remould.com','Active');

1 row created.

Assuming we have the following customer record in the CUST_MSTR table:


markdown

CUST_ID CUST_NAME CUST_ADDRESS CUST_PHONE CUST_EMAIL


CUST_STATUS
---------------------------------------------------------------------------------------
1 John Smith 123 Main St, Anytown 555-1234 john@example.com Active

Example 1: Insert a new customer record into the CUST_MSTR table

INSERT INTO CUST_MSTR (


CUST_ID,
CUST_NAME,
CUST_ADDRESS,
CUST_PHONE,
CUST_EMAIL,
CUST_STATUS
) VALUES (
2,
'Jane Doe',
'456 Maple Ave, Anytown',
'555-5678',
'jane@example.com',
'Active'
);

Since this is an insert action, no audit record will be created in the CUST_MSTR_AUDIT
table.

Example 2: Update the customer record in the CUST_MSTR table to change the phone
number and email address

UPDATE CUST_MSTR
SET
CUST_PHONE = '555-4321',
CUST_EMAIL = 'jsmith@example.com'
WHERE
CUST_ID = 1;

This update action will trigger the CUST_MSTR_AUDIT_TRG trigger, which will insert a
new audit record into the CUST_MSTR_AUDIT table with the following values:

USERNAME TIMESTAMP ACTION OLD_VALUE


NEW_VALUE
--------------------------------------------------------------------------------------------------------
SYS 2023-04-24 12:34:56.123456 PM UPDATE
CUST_ID=1^M^J...CUST_EMAIL=john@examp
CUST_ID=1^M^J...CUST_EMAIL=jsmit

Note that the OLD_VALUE column contains the entire contents of the original customer
record before the update, and the NEW_VALUE column contains the entire contents of the
customer record after the update, with the phone number and email address changed.

Example 3: Delete the customer record from the CUST_MSTR table

DELETE FROM CUST_MSTR


WHERE
CUST_ID = 1;

This delete action will trigger the CUST_MSTR_AUDIT_TRG trigger, which will insert a
new audit record into the CUST_MSTR_AUDIT table with the following values:

USERNAME TIMESTAMP ACTION OLD_VALUE


NEW_VALUE
--------------------------------------------------------------------------------------------------------
SYS 2023-04-24 12:45:12.345678 PM DELETE
CUST_ID=1^M^J...CUST_STATUS=Active <null>

Note that the NEW_VALUE column is null since the record was deleted and there is no new
value.
Week-7: PROCEDURES

1. Create the procedure for palindrome of given number.

SET SERVEROUTPUT ON;


CREATE OR REPLACE PROCEDURE ispalin(num IN NUMBER)
IS
reverse NUMBER;
digit NUMBER;
original_number NUMBER;
f_num NUMBER:=num;
BEGIN
reverse:=0;
original_number := num;
While f_num > 0 LOOP
digit := mod(f_num,10);
reverse := reverse * 10 + digit;
f_num := floor(f_num,10);
END LOOP;
if original_number = reverse then
dbms_output.put_line(“Palindrome”);
else
dbms_output.put_line(“Not a Palindrome”);
END if;
END;
/
+
BEGIN
ispalin(12321);
END;
/
Output: -
Palindrome

2. Create the procedure for GCD: Program should load two registers with two Numbers and
then apply the logic for GCD of two numbers. GCD of two numbers is performed by dividing
the greater number by the smaller number till the remainder is zero. If it is zero, the divisor is
the GCD if not the remainder and the divisors of the previous division are the new set of two
numbers. The process is repeated by dividing greater of the two numbers by the smaller
number till the remainder is zero and GCD is found.

CREATE OR REPLACE PROCEDURE find_gcd(num1 IN NUMBER, num2 IN


NUMBER) IS
DECLARE
f_num1 NUMBER;
f_num2 NUMBER;
remainder NUMBER;
BEGIN
f_num1 := num1;
f_num2 := num2;
dividend := greatest(f_num1,f_num2);
divisor := least(f_num1,f_num2);
LOOP
remainder := mod(dividend,dividor);
if remainder = 0 then
dbms_output.put_line(‘GCD is ‘||divisor);
EXIT;
else
dividend := divisor;
divisor := remainder;
end if;
end loop;
END;
/
+
BEGIN
find_gcd(36, 48);
END;
/
Output: -
GCD is 2

3. Write the PL/SQL programs to create the procedure for factorial of given number.

CREATE OR REPLACE PROCEDURE factorial(num IN NUMBER) IS


fact NUMBER;
f_num NUMBER:=num;
BEGIN
fact:=1;
while f_num>0 loop
fact:=fact*f_num;
f_num:=f_num-1;
END LOOP;
dbms_output.put_line('Factorial is' || fact);
END;
/
+
BEGIN
factorial(7);
END;
/
Output: -
Factorial is 5040

4. Write the PL/SQL programs to create the procedure to find sum of N natural numbers.

CREATE OR REPLACE PROCEDURE sumOfNaturalNumbers(num IN


NUMBER) IS
sum NUMBER;
f_num NUMBER := num;
BEGIN
sum:=0;
while f_num>0 LOOP
sum:=sum+f_num;
f_num:=f_num-1;
END LOOP;
dbms_output.put_line('Sum of
natural Number is’|| sum);
END;
/
+
BEGIN
SumOfNaturalNumbers(10);
END;
/
Output: -
The sum of natural Numbers is 55

5. Write the PL/SQL programs to create the procedure to find Fibonacci series.

CREATE OR REPLACE PROCEDURE fibonacci(num IN NUMBER)


IS
f_num NUMBER := num;
a NUMBER;
b NUMBER;
c NUMBER;
counter NUMBER;
BEGIN
a:=0;
b:=1;
counter:=2;
dbms_output.put_line(a||b);
while counter<f_num LOOP
c:=a+b;
dbms_output.put_line(c);
a:=b;
b:=c;
counter:=counter+1;
END LOOP;
END;
/
+
BEGIN
fibonacci(10);
END;
/
Output: -

0
1
1
2
3
5
8
13
21
34
6. Write the PL/SQL programs to create the procedure to check the given number is perfect or
not

CREATE OR REPLACE PROCEDURE perfectNumber(num IN NUMBER) IS


total NUMBER:=0;
f_num NUMBER:=num;
BEGIN
for i in 1..f_num/2 LOOP
if f_num mod i = 0 then
total:=total+1;
END if;
END LOOP;
if num = total then
dbms_output.put_line(‘Perfect
Number’);
else
dbms_output.put_line(‘Not a
Perfect Number’);
END if;
END;
/
+
BEGIN
perfectNumber(5);
END;
/
Output: -

Not a Perfect Number

You might also like