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

Database Management System-Ii

The document contains details about creating and working with triggers in Oracle PL/SQL. It includes examples of creating a table, inserting records, and then creating and testing before and after triggers on the table for insert, update and delete operations. It also discusses sequential statements like GOTO and NULL statements, unconstrained loops using EXIT, and creating and using sequences to generate unique identifiers for inserting records into a table.

Uploaded by

Mohini Chorat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Database Management System-Ii

The document contains details about creating and working with triggers in Oracle PL/SQL. It includes examples of creating a table, inserting records, and then creating and testing before and after triggers on the table for insert, update and delete operations. It also discusses sequential statements like GOTO and NULL statements, unconstrained loops using EXIT, and creating and using sequences to generate unique identifiers for inserting records into a table.

Uploaded by

Mohini Chorat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

ROLL NO : 205120 SUBJECT: DBMS DATE: | |

DATABASE
MANAGEMENT
SYSTEM-II

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 1


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

Name : Mohini Rajaram Chorat


Roll No : 205102
Class : SYCS
College Name : Karmaveer
Bhaurao Patil College, Vashi

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 2


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

INDEX
Sr Date Title
Pg Sign
No
No
1 Creating and working with Insert/Update/Delete
Trigger using Before/After clause.

2 Writing PL/SQL Blocks with basic programming


constructs by including following :
A) Sequential Statements
B)Unconstrained Loop

3 Sequences : A) Creating simple sequence with


clauses like START WITH, INCREMENT BY,
MAXVALUE, MINVALUE, CYCLE |
NOCYCLE, CACHE | NOCACHE, ORDER |
NOORDER .
B) Creating and using sequences for table.

4 Writing PL/SQL Blocks with basic programming


constructs by including following
A) 1] If… then … Else
2] IF…ELSIF…ELSF…END IF.
B) Case statement.

5 Writing PL/SQL Blocks with basic programming


constructs for following iterative structure :
A) While-loop Statements
B) For-loop Statements

6 Writing PL/SQL Blocks with basic programming


constructs by including a GOTO to jump of a loop
and NULL as a statement inside IF ?

7 Writing procedure 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

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 3


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

D) A forward declaration of procedure

8 Writing Functions in PL/SQL Block.


A) Call function in dbms_output.put_line
B) Count Employee from a function and return
value back
C) Call function and store the return value to a
variable

9 Writing a recursive Functions in PL/SQL


Block

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:

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 4


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

CREATE [OR REPLACE ] TRIGGER trigger_name

{BEFORE | AFTER | INSTEAD OF }


{INSERT [OR] | UPDATE [OR] | DELETE}

[OF col_name]
ON table_name

[REFERENCING OLD AS o NEW AS n]


[FOR EACH ROW]

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 :

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 5


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

Input :
insert into stu12 values(2,'sona',2000)

Output :

Input :
insert into stu12 values(3,'ranu',2000)

Output :

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 6


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

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);

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 7


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

dbms_output.put_line('new salary: ' ||:NEW.salary);


dbms_output.put_line('Salary difference : ' ||diff);
end;

Output :

Input :
select * from stu12

Output :

Insertion of value : -
Input :
insert into stu12 (id,name,salary) values(5,'yasha2',30000);

Output :

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 8


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

C) Update trigger :

Input :
UPDATE stu12 
SET salary = salary + 500 
WHERE id = 2 ;
Output :

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 9


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

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 .

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 10


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

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 :

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 11


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

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

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 12


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

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.

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 13


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

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 :

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 14


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

PRACTICAL NO : 3

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 15


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

Aim: Sequences: A) Creating simple Sequences with


clauses like START WITH, INCREMENT BY,
MAXVALUE, MINVALUE, CYCLE | NOCYCLE,
CACHE | NOCACHE, ORDER | NOORECER.
B) Creating and using Sequences for tables.
Answer: INCREMENT BY 
Specify the interval between sequence numbers.

The absolute of this value must be less than the difference


of MAXVALUE and MINVALUE.

START WITH - Specify the first sequence number to be generated.


MAXVALUE - Specify the maximum value the sequence can generate.
MINVALUE - Specify the minimum value of the sequence.
CACHE - Specify how many values of the sequence the database
reallocates and keeps in memory for faster access.

Creating and using Sequences for tables.


Input:

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 16


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

Create table stu12(stu_idvarchar(5),stu_name varchar(15),stu_marks


varchar(5));

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);

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 17


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

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;

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 18


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

Output:

PRACTICAL NO : 4

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 19


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

Aim : Writing PL/SQL Blocks with basic


programming constructs by including following:
A} 1] If...then...Else.
2] IF...ELSIF...ELSE... END IF.
B} Case statement.
Answer :
A} 1] If...then...Else.
Use :
IF-THEN-ELSE statement used when user wants to perform some
operation when expression is true and performing some other operation if the
expression is false.

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

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 20


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

dbms_output.put_line('b is greater than a');


end if;end;

Output :

A} 2] IF...ELSIF...ELSE... END IF.


Use :
If else statement only two operation are there i.e. if condition satisfies then
some code else other code.

Syntax:

IF expression THEN
Statement;
ELSEIF expression THEN
Statement;
ELSE
Statement;
END IF;

Program :
declare

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 21


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

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

Case expression can be any valid expression .

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 22


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

Use :
We compare the value of the expression with constant expression .The
statements are executed when it matches.

Syntax:
CASE expression

WHEN constant_expression THEN statement


[WHEN constant_expression THEN statement]
……………………….
[ELSE statements]

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');

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 23


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

when 11 then dbms_output.put_line('your selection is for : nov');


when 12 then dbms_output.put_line('your selection is for : dec');
else dbms_output.put_line('plz select valid number');
end case;
end;

Output :

PRACTICAL NO : 5

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 24


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

Aim : Writing PL/SQL Blocks with basic


programming constructs for following Iterative
Structure:
A} While-loop Statements.
B}For-loop Statements.
Answer :
A} While-loop Statements.
Use :
Checks the condition at the beginning of each iteration . If the condition
is true , MYSQL will executes between WHILE and END WHILE until the
condition is statisfying.

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 :

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 25


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

B} For-loop Statements.

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 26


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

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: | |

Aim : 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 ?
Answer :
A}GoTO to jump out of a loop
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>>

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 28


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

dbms_output.put_line('Science');
end;
Output :

B}NULL as a statement inside IF


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;

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 29


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

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

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 30


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

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
Answer :
Input :
create procedure practical as
begin
dbms_output.put_line('Welcome Practicals');
end;

Output :

Input :
begin
practical;
end;

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 31


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

Output :

Input :
drop procedure practical;

Output:

Program :
DECLARE

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 32


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

  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

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 33


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

Aim : Writing Functions in PL/SQL Block.


A) Call function in dbms_output.put_line
B) Count Employee from a function and return
value back
C) Call function and store the return value to a
variable
Answer :
Input :
create table student(stu_id number(10),stu_name varchar(10),stu_age
number(10));

Output :

Input :
insert into student (stu_id ,stu_name ,stu_age )values(12,'geeta',123);

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 34


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

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;

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 35


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

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;

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 36


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

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

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 37


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

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;

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

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 38


ROLL NO : 205120 SUBJECT: DBMS DATE: | |

num:= 6;
factorial := fact(num);
dbms_output.put_line(' factorial '|| num || ' is ' || factorial);
end;

Output :

KARMAVEER BHAURAO PATIL COLLEGE, VASHI PAGE NO : 39

You might also like