PL SQL
PL SQL
PL/SQL
PL/SQL is a procedural language extension to Structured Query
Language (SQL).
The basic unit in PL/SQL is called a block and is made up of three parts:
a declarative part, an executable part and an exception-building part.
SQL PL/SQL
SQL is a single query that is used to PL/SQL is a block of codes that used to write
perform DML and DDL operations. the entire program blocks/ procedure/
function, etc.
EXCEPTIONS
Typically, each block performs a logical Exception handling statements;
action in the program. A block has the
following structure: END;
PL/SQL Identifiers
Variables:
Like several other programming languages, variables in PL/SQL must be
declared prior to its use. They should have a valid name and data type as well.
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
var1 INTEGER;
var2 REAL;
var3 varchar2(20) ;
BEGIN
null;
END;
/
Output:
PL/SQL procedure successfully completed
INITIALISING VARIABLES
BEGIN
null;
END;
/
Output:
PL/SQL procedure successfully completed
DISPLAYING OUTPUT
BEGIN
dbms_output.put_line(var);
END;
/
Output:
Hi Everyone
PL/SQL procedure successfully completed
TAKING INPUT FROM USER
Just like in other programming languages, in PL/SQL also, we can take
input from the user and store it in a variable
Output:
PL/SQL procedure successfully completed
Example 1:
--PL/SQL code to print sum of two numbers taken from the user.
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
-- taking input forvariable a
a integer := &a ;
-- taking input forvariable b
b integer := &b ;
c integer ;
BEGIN
c := a + b ;
dbms_output.put_line('Sum of '||a||' and '||b||' is = '||c);
END;
/
Output:
2
3
Sum of 2 and 3 is =5
PL/SQL procedure successfully completed
Example 2:
--PL/SQL code to print natural numbers from 1 to 5
begin
n:=&n;
for i in 1..n
loop
fact:=fact*i;
end loop;
dbms_output.put_line('factorial='||fact);
end;
/
Example 4: Pl/SQL Program for Palindrome Number
declare
n number;
m number;
rev number:=0;
r number;
begin
n:=12321;
m:=n;
while n>0
loop
r:=mod(n,10);
rev:=(rev*10)+r;
n:=trunc(n/10);
end loop;
if m=rev
then
dbms_output.put_line('number is palindrome');
else
dbms_output.put_line('number is not palindrome');
end if;
end;
/
Procedure (stored procedure)
A stored procedure is a prepared SQL code that you can save, so the code can
be reused over and over again.
You can also pass parameters to a stored procedure. They are:
IN:
This is the Default Parameter for the procedure. It always receives the values
from calling program.
OUT:
This parameter always sends the values to the calling program.
IN OUT:
This parameter performs both the operations. It Receives value from as well as
sends the values to the calling program.
Syntax : Creating a Procedure
Output:
insertuser(101,’raju’);
Dbms_output.put_line(“inserted successfully”);
/
TRIGGERS
A trigger is a procedure that is automatically invoked by the DBMS in
response to specified changes to the database, and is typically specified
by the DBA.
If the condition part evaluates to true, the action associated with the
trigger is executed.
TRIGGERS
The execution of the action part of a trigger could again activate the
same trigger such triggers are called recursive triggers.
Contd..
The following are the key differences between triggers and stored
procedures:
Execution
operation varchar(25), /
table_name varchar(20));
Output: Trigger created.
Triggers can do much more than enforce referential integrity. Because they are
passive, constraints are limited to preventing updates in a narrow set of conditions.
This property makes a constraint easier to understand, and also gives the DBMS
more opportunities to optimize execution.
TRIGGERS
Constraints versus Triggers
Assertions are useful for enforcing data integrity and ensuring that the data in the
database meets certain conditions. They can be used to enforce business rules or
to ensure the consistency of the data. However, they can also be time-consuming
to create and maintain, so they are not always used in practice.
Thank you