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

04 - PL_SQL

The document provides an overview of PL/SQL, Oracle's procedural extension to SQL, highlighting its advantages such as performance optimization and higher productivity. It details the structure of PL/SQL blocks, including declaration, execution, and exception handling, as well as the syntax for procedures, functions, and triggers. Each component is explained with its purpose and syntax for better understanding and implementation in database management.

Uploaded by

actvtymhs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

04 - PL_SQL

The document provides an overview of PL/SQL, Oracle's procedural extension to SQL, highlighting its advantages such as performance optimization and higher productivity. It details the structure of PL/SQL blocks, including declaration, execution, and exception handling, as well as the syntax for procedures, functions, and triggers. Each component is explained with its purpose and syntax for better understanding and implementation in database management.

Uploaded by

actvtymhs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Pemrograman

Desktop
Week 4 - PL/SQL
Outline
● Introduction
● Procedure
● Function
● Trigger
What is PL/SQL?
Stands for Procedural Language/Structured Query
Language, is Oracle's procedural extension to SQL
(Structured Query Language). It is used for managing and
manipulating data within Oracle databases
Advantages of PL/SQL

● Performance Optimization
● Integration with Oracle Tools
● Automated Tasks
● Higher Productivity
PL/SQL Block Structure
● Declaration
Where variables, constants, and cursors are declared. The declarative section starts with the
keyword DECLARE and ends at the time of the section executable is started.
● Execution
Where the SQL statements and procedural logic are executed. The execution section start
with keyword BEGIN and ends with keyword END.
● Exception Handling
Where errors and exceptions are managed. The exception handling section start with
keyword EXCEPTION. The exception handling section specifies the action to be performed
when errors and abnormal conditions occur in the execution section
Procedure

In database management systems, a stored procedure (often simply referred to as a


procedure) is a precompiled collection of one or more SQL statements that can be
executed as a single unit. Stored procedures are used to encapsulate complex operations,
encapsulate business logic, and simplify database interactions.
Procedure - syntax

CREATE [OR REPLACE] PROCEDURE procedure_name


[ (parameter [,parameter]) ]
IS
[declaration_section]
BEGIN
executable_section
[EXCEPTION
exception_section]
END [procedure_name];
Function
A function is a named PL/SQL block that performs a specific task and returns a value.
Functions can be used in SQL statements (such as SELECT, INSERT, UPDATE, and DELETE)
as well as in PL/SQL blocks.

Key Features:

● Must return a value.


● Can be used in SQL expressions.
● Can take parameters (input values).
● Typically used for calculations, data retrieval, or other operations that need to return a
single result.
Function - syntax

CREATE [OR REPLACE] FUNCTION function_name [parameters]


[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];
Trigger

A trigger is a stored procedure in the database that is automatically executed or "triggered"


when certain events occur, such as INSERT, UPDATE, or DELETE operations on a table.
Triggers are commonly used for enforcing business rules, auditing changes, or maintaining
complex data integrity.
Trigger - syntax
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)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;

You might also like