SYBSc(CS) PRACTICAL-8 ADC
Practical-08
Title: - Creating and working with Insert/Update/Delete Trigger using Before/After clause.
Programs:
1. Create table customer
create table customer1(id number, name varchar2(20), age number(2), address
varchar2(50), salary number(10));
SQL> insert into customer1 values(1,'Ramesh',25,'Mumbai',20000);
SQL> insert into customer1 values(2,'Suresh',24,'Delhi',40000);
SQL> insert into customer1 values(3,'Mahesh',28,'Chennai',50000);
SQL> insert into customer1 values(4,'Nilesh',30,'Kolkata',60000);
2. Creating Trigger using Before clause
CREATE OR REPLACE TRIGGER display_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON customer1
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;
/
SYBSc(CS) PRACTICAL-8 ADC
insert into customer1 values(5,'Shailesh',27,'Banglore',70000);
update customer1 set salary=salary+5000 where id=2;
3. Creating Trigger using After clause
CREATE OR REPLACE TRIGGER display_salary_changes
AFTER DELETE OR INSERT OR UPDATE ON customer1
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
sal_new number;
BEGIN
sal_new := :NEW.salary + :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary : ' || sal_new);
END;
/
SYBSc(CS) PRACTICAL-8 ADC
insert into customer1 values(6,'Manish',29,'Nagpur',80000);
update customer1 set salary=salary+5000 where id=3;
Conclusion:
NOTE: Attach printout of outputs at the end of each experiment.
SYBSc(CS) PRACTICAL-7 ADC
Practical-07
Title: - Writing Functions in PL/SQL Block.
a. Define and call a function
b. Define and use function in select clause,
c. Call function in dbms_output.put_line
d. Recursive function
Programs:
1. Define and call a function
create or replace function adder(n1 in number, n2 in number)
return number
is
n3 number(8);
begin
n3 :=n1+n2;
return n3;
end;
/
DECLARE
n3 number(2);
BEGIN
n3 := adder(11,22);
dbms_output.put_line('Addition is: ' || n3);
END;
/
2. Define and use function in select clause
create table customer(id number, name varchar2(20), dept varchar2(20), salary
number(10));
SYBSc(CS) PRACTICAL-7 ADC
insert into customer values(1,'Pavan','IT',500000);
insert into customer values(2,'Ghanshyam','CS',600000);
insert into customer values(3,'Om','CS',550000);
insert into customer values(4,'Bhagirath','IT',650000);
CREATE OR REPLACE FUNCTION totalCustomers
RETURN number IS
total number(2) := 0;
BEGIN
SELECT count(*) into total
FROM customer;
RETURN total;
END;
/
DECLARE
c number(2);
BEGIN
c := totalCustomers();
dbms_output.put_line('Total no. of Customers: ' || c);
END;
/
SYBSc(CS) PRACTICAL-7 ADC
3. Call function in dbms_output.put_line
DECLARE
a number;
b number;
c number;
FUNCTION findMax(x IN number, y IN number)
RETURN number
IS
z number;
BEGIN
IF x > y THEN
z:= x;
ELSE
z:= y;
END IF;
RETURN z;
END;
BEGIN
a:= 23;
b:= 45;
dbms_output.put_line(' Maximum of (23,45): ' || findMax(a, b));
END;
/
SYBSc(CS) PRACTICAL-7 ADC
4. Recursive function
DECLARE
num number;
factorial number;
FUNCTION fact(x number)
RETURN number
IS
f number;
BEGIN
IF x=0 THEN
f := 1;
ELSE
f := x * fact(x-1);
END IF;
RETURN f;
END;
BEGIN
num:= 6;
factorial := fact(num);
dbms_output.put_line(' Factorial '|| num || ' is ' || factorial);
END;
/
SYBSc(CS) PRACTICAL-7 ADC
Conclusion:
NOTE: Attach printout of outputs at the end of each experiment.
SYBSc(CS) PRACTICAL-5 ADC
Practical-05
Title: - Writing PL/SQL Blocks with basic programming constructs by including a GoTO to
jump out of a loop and NULL as a statement inside IF.
Programs:
1. Write a PL/SQL block to print 10 to 19 numbers except 15 using goto statement.
declare
a int;
begin
a:=10;
<<jump>>
while a<20 loop
dbms_output.put_line('value of a: ' || a);
a:=a+1;
if(a=15) then
a:=a+1;
goto jump;
end if;
end loop;
end;
/
2. Write a PL/SQL block to demonstrate use of NULL statement.
declare
a int;
begin
a:=&a;
if(a=15) then
dbms_output.put_line('Guess is correct');
else
NULL;
end if;
end;
/
Conclusion:
NOTE: Attach printout of outputs at the end of each experiment.
SYBSc(CS) PRACTICAL-6 ADC
Practical-06
Title: - Writing Procedures in PL/SQL Block
a. Create an empty procedure, replace a procedure and call procedure
b. Create a stored procedure and call it
c. Define procedure to insert data
d. A forward declaration of procedure
Programs:
1. Create an empty procedure, replace a procedure and call procedure
create procedure pr6a
as
begin
null;
end;
/
create procedure pr6a1
AS
BEGIN
dbms_output.put_line('Hello World!');
END;
/
SYBSc(CS) PRACTICAL-6 ADC
create or replace procedure pr6a1
as
begin
dbms_output.put_line('Replace Procedure');
end;
/
2. Create a stored procedure and call it
DECLARE
a number;
b number;
c number;
PROCEDURE findMin(x IN number, y IN number, z OUT number) IS
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
END;
/
3. Define procedure to insert data
create table pr6c(id number, name varchar2(20));
SYBSc(CS) PRACTICAL-6 ADC
create procedure insertuser(i in number,n in varchar2)
as
begin
insert into pr6c values(i,n);
end;
/
declare
id number;
nm varchar2(20);
begin
id:=&id;
nm:=&nm;
insertuser(id,nm);
end;
/
select * from pr6c;
4. A forward declaration of procedure
DECLARE
a number;
PROCEDURE squareNum(x IN OUT number) IS
SYBSc(CS) PRACTICAL-6 ADC
BEGIN
x := x * x;
END;
BEGIN
a:= 23;
squareNum(a);
dbms_output.put_line(' Square of (23): ' || a);
END;
/
Conclusion:
NOTE: Attach printout of outputs at the end of each experiment.
SYBSc(CS) PRACTICAL-3 ADC
Practical-03
Title: - Writing PL/SQL blocks with basic programming constructs by including following:
i. If…then…else, if….elsif…else…..endif
ii. Case statement
Programs:
1. Write a PL/SQL block to find whether the entered number is Even or Odd.
declare
a int;
begin
a:=&a;
if(mod(a,2)=0) then
dbms_output.put_line(a||' is Even.');
else
dbms_output.put_line(a||' is Odd.');
end if;
end;
/
2. Write a PL/SQL block to find greatest of three numbers.
declare
a int;
b int;
c int;
begin
a:=&a;
b:=&b;
c:=&c;
if(a>b and a>c) then
dbms_output.put_line(a||' is greatest.');
elsif(b>a and b>c) then
dbms_output.put_line(b||' is greatest.');
else
dbms_output.put_line(c||' is greatest.');
end if;
end;
/
SYBSc(CS) PRACTICAL-3 ADC
3. Write a PL/SQL block to display grade of students depending on their percentage.
Follow the following criteria:
Percentage Grade
per >= 75 Distinction
Per<75 and per>=60 First Class
Per<60 and per>=45 Second Class
Per<45 and per>=35 Pass
Per<35 Fail
declare
per float;
grade char(1);
begin
per:=&per;
if(per>=75.00) then
grade:='D';
elsif(per<75.00 and per>=60.00) then
grade:='F';
elsif(per<60.00 and per>=45.00) then
grade:='S';
elsif(per<45.00 and per>=35.00) then
grade:='P';
elsif(per<35.00) then
grade:='K';
end if;
case
when grade='D' then dbms_output.put_line('Distinction');
when grade='F' then dbms_output.put_line('First Class');
when grade='S' then dbms_output.put_line('Second Class');
when grade='P' then dbms_output.put_line('Pass');
when grade='K' then dbms_output.put_line('Fail');
else dbms_output.put_line('No such grade');
end case;
end;
/
Conclusion:
NOTE: Attach printout of outputs at the end of each experiment.
SYBSc(CS) PRACTICAL-4 ADC
Practical-04
Title: - Writing PL/SQL Blocks with basic programming constructs for following Iterative
Structure:
a. While-loop Statements
b. For-loop Statements.
Programs:
1. Write a PL/SQL block to find factorial of a given number.
declare
num int;
fact int;
i int;
begin
num:=#
fact:=1;
for i in 1..num loop
fact:=fact*i;
end loop;
dbms_output.put_line(fact);
end;
/
2. Write a PL/SQL block to find fibonacci series upto 10 terms.
declare
a int;
b int;
c int;
i int;
begin
a:=0;
b:=1;
i:=3;
dbms_output.put_line(a);
dbms_output.put_line(b);
while i<=10 loop
c:=a+b;
dbms_output.put_line(c);
a:=b;
b:=c;
i:=i+1;
end loop;
end;
/
Conclusion:
NOTE: Attach printout of outputs at the end of each experiment.
SYBSc(CS) PRACTICAL-1 ADC
Practical-01
Title: - Writing PL/SQL blocks with basic programming constructs by including following:
i. Sequential Statement
ii. Unconstrained Loop
Programs:
1. Write PL/SQL block to display message “Welcome to PL/SQL Programming”.
begin
dbms_output.put_line(„Welcome to PL/SQL Programming‟);
end;
/
2. Write PL/SQL block to add two numbers.
declare
a int;
b int;
c int;
begin
a:=&a;
b:=&b;
c:=a+b;
dbms_output.put_line('Sum of two numbers = ' || c);
end;
/
3. Write PL/SQL block to display natural numbers up to 10 using basic loop.
declare
num int:= 1;
begin
loop
if num > 10 then
exit;
end if;
dbms_output.put_line(num);
num := num + 1;
end loop;
end;
/
Conclusion:
NOTE: Attach printout of outputs at the end of each experiment.
SYBSc(CS) PRACTICAL-2 ADC
Practical-02
Title: - Sequences:
i. Creating simple sequence with clauses like START WITH, INCREMENT BY,
MAXVALUE, MINVALUE, CYCLE | NOCYCLE, CACHE | NOCACHE.
ORDER | NOORDER.
ii. Creating and using sequences for tables.
Programs:
1. Create sequence A1 with increment by and start with as 1 and max value 5.
create sequence A1
increment by 1
start with 1
maxvalue 5;
select A1.currval,A1.nextval from dual connect by level<=7;
SYBSc(CS) PRACTICAL-2 ADC
2. Create sequence A2 with increment by and start with as 10, max value 50 min
value 10, cache 2 and with cycle clause.
create sequence A2
start with 10
increment by 10
minvalue 10
maxvalue 50
cache 2
cycle;
select A2.currval,A2.nextval from dual connect by level<=7;
SYBSc(CS) PRACTICAL-2 ADC
3. Create sequence A3 with increment by 1, start with 100, max value 105, cache 3
and with cycle clause.
create sequence A3
start with 100
increment by 1
maxvalue 105
cache 3
cycle;
select A3.currval,A3.nextval from dual connect by level<=7;
SYBSc(CS) PRACTICAL-2 ADC
select A3.currval,A3.nextval from dual connect by level<=9;
4. Create table with column rollno and sname.
create table stud
(rollno number(3),
sname varchar2(20));
5. Insert values in the table using sequence A2 and A3.
insert into stud values(A2.nextval,'Kalpesh');
insert into stud values(A2.nextval,'Nitin');
insert into stud values(A2.nextval,'Amol');
insert into stud values(A2.nextval,'Abhishek');
insert into stud values(A2.nextval,'Rahul');
SYBSc(CS) PRACTICAL-2 ADC
select * from stud;
SYBSc(CS) PRACTICAL-2 ADC
insert into stud values(A3.nextval,'Akash');
insert into stud values(A3.nextval,'Praful');
insert into stud values(A3.nextval,'Shreyansh');
insert into stud values(A3.nextval,'Mehul');
select * from stud;
SYBSc(CS) PRACTICAL-2 ADC
Conclusion: