Dbms Unit 5 Final
Dbms Unit 5 Final
UNIT 5
PL/SQL
1Q. DEFINE PL/SQL. EXPLAIN ABOUT STRUCTURE OF PL/SQL PROGRAM.
WRITE STEPS TO EXECUTE A PLSQL PROGRAM.
ANS: PL/SQL INTRODUCTION: PL/SQL stands for procedural language/structure
query language.
PL/SQL is not a 4GL or a high level language (3GL). But it acts as an interface
or platform to work with set of SQL commands to execute a task.
PL/SQL contains SQL commands along with block of statements like control
statements, functions, procedures and so on.
Advantages of PL/SQL:
1. We can group no.of SQL commands and execute them as one task.
2. We can also do decision control logics and looping programs on SQL commands
using PL/SQL.
Structure of PL/SQL Block:
declare
variable declaration;
begin
SQL Statements;
exception
SQL code to handle errors;
end;
1
III BSc, DBMS
2
III BSc, DBMS
3
III BSc, DBMS
4
III BSc, DBMS
5
Aditya Degree College, Kakinada III BSc, DBMS
7
III BSc, DBMS
8
III BSc, DBMS
Package body or definition: The package body contain various methods declared in
the package specification. Package body name and package specification name must be
same.
Example for Package Body:
Steps to execute a package: The procedures or functions can be accessed with the
help of package name. i.e., Packagename.procedurename(args);
Ex: SQL> exec p1.addition(30,40);
70
In this example, p1 represents packagename, addition represents procedure name.
9
III BSc, DBMS
declare
x number(3);
y number(3);
begin
x:=10;
y:=x/0;
dbms_output.put_line(y);
exception
when zero_divide then
dbms_output.put_line(‘ division by zero error’);
end;
10
III BSc, DBMS
declare
age_error exception;
age number(3):=&age;
begin
if age>18 then
dbms_output.put_line(‘eligible for voting’);
else
raise age_error;
end if;
exception
when age_error then
dbms_output.put_line(‘not eligible for voting’);
end;
11
III BSc, DBMS
Detete a Trigger: Drop command is used to delete a trigger from the database.
Syntax: SQL> Drop trigger <triggername>;
Example: SQl> Drop trigger t1;
12
III BSc, DBMS
2. if - else:
Syntax: Example:
if condition then if n>0 then
statements; dbms_output.put_line(‘positive number’);
else else
statements; dbms_output.put_line(‘negative number’);
end if; end if;
Syntax: Example:
if condition then if day==1 then
statements; dbms_output.put_line(‘Monday’);
elsif condition then elsif day==2 then
statements; dbms_output.put_line(‘Tuesday’);
elsif condition then elsif day==3 then
statements; dbms_output.put_line(‘Wednesday’);
....................... end if;
end if;
13
III BSc, DBMS
4. Case statement :
Example:
Syntax: case day
case variable when 1 then
when value1 then dbms_output.put_line('Monday');
statements; when 2 then
when value2 then dbms_output.put_line(‘Tuesday’);
statements; when 3 then
………………………. dbms_output.put_line('Wednesday');
else else dbms_output.put_line(‘Invalid Choice’);
statements; end case;
end case;
Looping Statements: In looping statements, a block of code is executed no.of
times ,until condition is true. When condition is false then it exit the loop. Different
types of looping statements are:
1. Simple loop 2. While loop 3. For loop
simple –loop: It is called “exit control loop”. In this loop condition is given at end
of loop.
Syntax: Example:
loop i:=1;
statements; loop
exit when <condition> dbms_output.put_line(i);
end loop; i:=i+1;
exit when i>=5;
end loop;
while –loop: It is called “entry control loop”. In this loop condition is given at
begining of loop.
Syntax: Example:
while <condition> then i:=1;
statements; while i<=5 then
end loop; dbms_output.put_line(i);
i:=i+1;
end loop;
for –loop: It is called “entry control loop”. In this loop condition is given at
begining of loop.
14
III BSc, DBMS
Syntax: Example1:
for variable in[reverse] val1..val2 for i in 1..5
loop dbms_output.put_line(i);
statements; end loop;
end loop;
15
III BSc, DBMS
4. Logical operators: These are used to check two or more logical conditions. It returns
16