ADBMSUnit5pptx 2023 10 19 12 50 24
ADBMSUnit5pptx 2023 10 19 12 50 24
COMPUTER
ENGINEERING
Unit 5
Advance
PL / SQL Database
Management
System
(09CE1502)
PL/SQL is a superset of SQL.
IF <condition> THEN
<action>
Conditiona ELSEIF < condition > THEN
Conditiona
dbms_output.put_line('Value of a is 20' );
ELSEIF ( a = 30 ) THEN
l control - dbms_output.put_line('Value of a is 30' );
Example ELSE
dbms_output.put_line('None of the values is matching');
END IF;
dbms_output.put_line('Exact value of a is: '|| a );
END;
/
None of the values is matching
Exact value of a is: 100
Control
exit;
END IF;
PL/SQL procedure successfully
completed.
END LOOP;
-- after exit, control resumes here
dbms_output.put_line('After Exit x is: '
|| x);
END;
/
Syntax: While loop
WHILE <condition>
Iterative Loop
Control <action>
End loop;
DECLARE
a number(2) := 10;
BEGIN
WHILE a < 15 LOOP
dbms_output.put_line('value of
a: ' || a);
Iterative a := a + 1;
Control END LOOP;
END;
/
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
Control <action>
End loop;
DECLARE
a number(2);
BEGIN
FOR a in 10 .. 15 LOOP
dbms_output.put_line('value of a: ' || a);
Iterative END LOOP;
Control END;
/
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
ns 5. User exits
the EXIT command.
from SQL*PLUS by issuing
BEGIN
SAVEPOINT dup_found;
Rollback UPDATE emp SET eno=1 WHERE
empname = 'Forbs ross'
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
ROLLBACK TO dup_found;
END;
SAVEPOINT savepoint_names marks the current point
in the processing of a transaction.
Savepoints let you rollback part of a transaction
instead of the whole transaction.
Savepoint
SYNTAX:
SAVEPOINT SAVEPOINT_NAME;
Example:
SQL>DECLARE
emp_id emp.empno%TYPE;
BEGIN
SAVEPOINT dup_found;
Savepoint UPDATE emp SET eno=1 WHERE
empname = 'Forbs ross'
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
ROLLBACK TO dup_found;
END;
When an SQL statement is processed, Oracle creates
a memory area known as context area.
A cursor is a pointer to this context area.
It contains all information needed for processing the
Cursor statement.
In PL/SQL, the context area is controlled by Cursor.
A cursor contains information on a select statement
and the rows of data accessed by it.
A cursor is used to referred to a program to fetch and
process the rows returned by the SQL statement, one
at a time.
1 %FOUND
Returns TRUE if an INSERT, UPDATE, or DELETE
statement affected one or more rows or a SELECT
INTO statement returned one or more rows.
Otherwise, it returns FALSE.
Implicit 2 %NOTFOUND
The logical opposite of %FOUND. It returns TRUE if an
cursor INSERT, UPDATE, or DELETE statement affected no
rows, or a SELECT INTO statement returned no rows.
Otherwise, it returns FALSE.
3 %ISOPEN
Always returns FALSE for implicit cursors, because
Oracle closes the SQL cursor automatically after
executing its associated SQL statement.
4 %ROWCOUNT
Returns the number of rows affected by an INSERT,
UPDATE, or DELETE statement, or returned by a
SELECT INTO statement.
The Explicit cursors are defined by the programmers
to gain more control over the context area.
These cursors should be defined in the declaration
Explicit section of the PL/SQL block.
It is created on a SELECT statement which returns
Cursor more than one row.
SYNTAX:
CURSOR c_customers IS
SELECT id, name, address FROM customers;
Opening the Cursor
CLOSE c_customers;
Locks in PL/SQL is a way to maintain data integrity
of the database.
As oracle is a multi-user platform where tables used
in a database acts as a global resource being shared
by multiple users at the same time.
PL/SQL There is a possibility that the data may become
Security inconsistent due to concurrent processing of data by
multiple user at the same time.
Therefore, locks play an important role to maintain
concurrency control ensuring data integrity of stored
data in the database.
TYPES OF LOCKS DESCRIPTION
• Applied on a table when
data is being viewed i.e,
the stored data is just
being read.
• Multiple shared locks can
Shared Lock
be applied on the table at
the same time.
Type of • Used in case
PL/SQL BEGIN
executable_section
Procedure [EXCEPTION
exception_section]
END [procedure_name];
In this example, we are going to insert record in user
table. So you need to create user table first.
Table creation:
Create Now
name
table.
varchar2(100));
write the procedure code to insert record in user
BEGIN
insertuser(101,'Rahul');
PL/SQL dbms_output.put_line('record inserted su
program ccessfully');
END;
to call /
procedure
PL/SQL
Syntax for drop procedure
Drop
Procedure DROP PROCEDURE procedure_name;
The PL/SQL Function is very similar to PL/SQL
Procedure.
The main difference between procedure and a
PL/SQL function is, a function must always return a value, and
Function on the other hand a procedure may or may not return
a value.
Except this, all the other things of PL/SQL procedure
are true for PL/SQL function too.
Syntax to create a function:
PL/SQL updated.
[ON table_name]: This specifies the name of the table associated
Trigger with the trigger.
[REFERENCING OLD AS o NEW AS n]: This allows you to refer new and
old values for various DML statements, like INSERT, UPDATE, and
DELETE.
[FOR EACH ROW]: This specifies a row level trigger, i.e., the trigger
would be executed for each row being affected. Otherwise the trigger
will execute just once when the SQL statement is executed, which is
called a table level trigger.
WHEN (condition): This provides a condition for rows for which the
trigger would fire. This clause is valid only for row level triggers.
The four main types of triggers are:
Row Level Trigger: This gets executed before or
after any value of a row changes
Column Level Trigger: This gets executed before or
after the specified column changes
Types of For Each Row Type: This trigger gets executed once
Trigger for each row of the result set affected by an
insert/update/delete
For Each Statement Type: This trigger gets
executed only once for the entire result set, but also
fires each time the statement is executed.
THANK YOU