Database Management System-Ii
Database Management System-Ii
DATABASE
MANAGEMENT
SYSTEM-II
INDEX
Sr Date Title
Pg Sign
No
No
1 Creating and working with Insert/Update/Delete
Trigger using Before/After clause.
PRACTICAL NO : 1
Aim : Creating and working with
Insert/Update/Delete Trigger using Before/After
clause.
Answer :
Trigger :
1 .Trigger can be define on the table , view , schema or database with which the
event is associated.
2 .A trigger in pl/sql block structure which is fixed when a DML statement like
insert , delete , update is executed on a database table .
3 . A trigger is triggered automatically when an associated DML statement is
execute.
4 . Trigger are stored program , which are automatically
executed or fired when some event occurred.
Syntax:
[OF col_name]
ON table_name
WHEN (condition)
BEGIN
// sql statements
END;
Program :
A) Create table
Input:
create table stu12(id varchar(20),name varchar(20),salary varchar(20))
Output:
Input :
insert into stu12 values(1,'yasha',200000)
Output :
Input :
insert into stu12 values(2,'sona',2000)
Output :
Input :
insert into stu12 values(3,'ranu',2000)
Output :
Input :
insert into stu12 values(4,'pane',200)
Output :
B)Create trigger
Input :
create or replace trigger display313 before delete or insert or update on stu12
for each row
when (NEW.id >0)
declare
diff number;
begin
diff :=:NEW.salary - :OLD.salary;
dbms_output.put_line('old salary: ' ||:OLD.salary);
Output :
Input :
select * from stu12
Output :
Insertion of value : -
Input :
insert into stu12 (id,name,salary) values(5,'yasha2',30000);
Output :
C) Update trigger :
Input :
UPDATE stu12
SET salary = salary + 500
WHERE id = 2 ;
Output :
PRACTICAL NO : 2
Aim : Writing PL/SQL Blocks with basic
programming constructs by including following:
A} Sequential Statements B} unconstrained loop
Answer:
A} Sequential Statements
1}GoTO statement 2}NULL statement
1}GoTO statement
Use :
1 . Used to transfer the controls to the specific label .
2 . The label must be unique within its scope and labeled statement or block
which may be anywhere in the program.
Syntax:
GOTO label_name;
label_name is the name of a label identifying the target statement.
This GOTO label is defined in the program as follows:
<<label_name>>
Program:
declare
n number;
begin
for n in 1..8 loop
dbms_output.put_line(n);
if n=4 then
goto label;
end if;
end loop;
<<label>>
dbms_output.put_line('Science');
end;
Output :
2 }NULL statement
Def : The NULL statement is an executable statement that does nothing.
Use :
1 . The NULL statement can act as a placeholder whenever an
executable statement is required.
Syntax:
NULL;
Program :
declare
n number;
begin
for n in 1..8 loop
dbms_output.put_line(n);
if n=4 then
null;
end if;
end loop;
end;
Output :
B} Unconstrained Loop
1}Exit Statement
Use :
When the EXIT statement is encountered inside a loop,
the loop is immediately terminated and
the program control resumes at the next statement following the loop.
Syntax:-
EXIT ;
Program :
declare
n number :=1;
begin
loop
n:=n+1;
if( n mod 2 =0) then
dbms_output.put_line(n);
if (n>=10) then
exit;
end if;
end if;
end loop;
end;
Output :
PRACTICAL NO : 3
Output:
Input :
Create sequence seq
minvalue 1
maxvalue 10
start with 2
increment by 2
cache 10;
Output:
Input :
insert into stu12 values(seq.nextval,'Yashasvi',70);
Output:
Input
insert into stu12 values(seq.nextval,'omika',80);
Output :
Input :
insert into stu12 values(seq.nextval,'keyuri',90);
Output :
Input :
select * from stu12;
Output:
PRACTICAL NO : 4
Syntax:
IF expression THEN
Statement;
ELSE
Statement;
END IF;
Program :
declare
a number :=20;
b number :=10;
begin
dbms_output.put_line( 'value of a : '||a);
dbms_output.put_line( 'value of b : '||b);
if a>b then
dbms_output.put_line( 'a is greater than b');
else
Output :
Syntax:
IF expression THEN
Statement;
ELSEIF expression THEN
Statement;
ELSE
Statement;
END IF;
Program :
declare
a number :=20;
b number :=40;
c number :=30;
begin
dbms_output.put_line( 'value of a : '||a);
dbms_output.put_line( 'value of b : '||b);
dbms_output.put_line( 'value of c : '||c);
if ((a>b)and(a>c)) then
dbms_output.put_line( 'a is greater than b and c');
elsif ((b>a)and(b>c)) then
dbms_output.put_line( 'b is greater than a and c');
else
dbms_output.put_line( 'c is greater than a and b');
end if;
end;
Output :
B} Case statement
Use :
We compare the value of the expression with constant expression .The
statements are executed when it matches.
Syntax:
CASE expression
END CASE
Program :
declare
n number :=:month_number;
begin
case n
when 1 then dbms_output.put_line('your selection is for : jan');
when 2 then dbms_output.put_line('your selection is for : feb');
when 3 then dbms_output.put_line('your selection is for : march');
when 4 then dbms_output.put_line('your selection is for : april');
when 5 then dbms_output.put_line('your selection is for : may');
when 6 then dbms_output.put_line('your selection is for : june');
when 7 then dbms_output.put_line('your selection is for : july');
when 8 then dbms_output.put_line('your selection is for : aug');
when 9 then dbms_output.put_line('your selection is for : sep' );
when 10 then dbms_output.put_line('your selection is for : oct');
Output :
PRACTICAL NO : 5
Syntax:
WHILE expression DO
Statement
END loop;
Program :
declare
n number :=1;
begin
while n<10 loop
dbms_output.put_line(n);
n:=n+1;
end loop;
end;
Output :
B} For-loop Statements.
Use :
Used to execute particular code repeatedly . This loop helps to minimize
the code. The initialization , condition and increment or decrement is done in a
single statement.
Syntax:
FOR counter in condition
Statement;
END loop ;
Program :
Declare
a number(20);
begin
for a in 1..10 loop
dbms_output.put_line('the Value of A is :'||a);
end loop;
end;
Output :
PRACTICAL NO : 6
KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 27
ROLL NO : 205120 SUBJECT: DBMS DATE: | |
Syntax:
GOTO label_name;
label_name is the name of a label identifying the target statement.
This GOTO label is defined in the program as follows:
<<label_name>>
Program :
declare
n number;
begin
for n in 1..8 loop
dbms_output.put_line(n);
if n=4 then
goto label;
end if;end loop;
<<label>>
dbms_output.put_line('Science');
end;
Output :
Use :
1 . The NULL statement can act as a placeholder whenever an
executable statement is required.
Syntax:
NULL;
Program :
declare
n number;
begin
for n in 1..8 loop
dbms_output.put_line(n);
if n=4 then
null;
end if;
end loop;
end;
Output :
PRACTICAL NO : 7
Aim : Writing Procedures in PL/SQL Block
Output :
Input :
begin
practical;
end;
Output :
Input :
drop procedure practical;
Output:
Program :
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:= 44;
b:= 450;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (44, 450) : ' || c);
END;
Output :
PRACTICAL NO : 8
Output :
Input :
insert into student (stu_id ,stu_name ,stu_age )values(12,'geeta',123);
Output :
Input :
insert into student (stu_id ,stu_name,stu_age )values(13,'ranu',16);
Output :
Input :
insert into student (stu_id ,stu_name ,stu_age )values(14,'rupa',18);
Output :
Input :
select * from student;
Output :
Input :
create function totalstudent
return number is
total number:=0;
begin
select count(*) into total from student;
return total;
end;
Output:
Input :
declare
c number;
begin
c:= totalstudent ();
dbms_output.put_line('total number of student : '||c);
end;
Output :
PRACTICAL NO : 9
Aim : Writing a recursive Functions in PL/SQL
Block
Answer :
Recursive function:
When a subprogram calls itself, it is referred to
as a recursive call and the process is known
as recursion.
Program :
declare
num number;
factorial number;
begin
num:= 6;
factorial := fact(num);
dbms_output.put_line(' factorial '|| num || ' is ' || factorial);
end;
Output :