PL SQL Assertions Triggers
PL SQL Assertions Triggers
Assertions,
Triggers
PL/SQL (procedural language extension to Structured Query Language)
• PL/SQL is to combine database language and procedural programming language.
• 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.
• PL/SQL enables users to
• Mix SQL statements with procedural constructs,
• It is possible to use PL/SQL blocks and
• Subprograms to group SQL statements before sending them to oracle for
execution.
• Without PL/SQL,
• Oracle must process SQL statements one at a time.
• In a network environment, this can affect traffic flow and slow
down response time.
PL/SQL (procedural language extension to Structured Query Language)
• The PL/SQL blocks can be compiled once and stored in executable form
to improve response time.
• A PL/SQL program that is stored in a database in compiled form and can be
called by name is referred to as a stored procedure.
• A PL/SQL stored procedure that is implicitly started when an INSERT,
UPDATE or DELETE statement is issued against an associated table is
called a trigger.
Disadvantages of SQL:
• SQL doesn’t provide the programmers with a technique of condition
checking, looping and branching.
• SQL statements are passed to Oracle engine one at a time which increases
traffic and decreases speed.
• SQL has no facility of error checking during manipulation of data.
Features of PL/SQL:
• PL/SQL is basically a procedural language, which provides the functionality
of decision making, iteration and many more features of procedural
programming languages.
• PL/SQL can execute a number of queries in one block using single command.
• One can create a PL/SQL unit such as procedures, functions, packages,
triggers, and types, which are stored in the database for reuse by
applications.
• PL/SQL provides a feature to handle the exception which occurs in PL/SQL
block known as exception handling block.
• Applications written in PL/SQL are portable to computer hardware or
operating system where Oracle is operational.
• PL/SQL Offers extensive error checking.
Differences between SQL and PL/SQL:
SQL PL/SQL
SQL is a single query that is used to PL/SQL is a block of codes that used to
perform DML and DDL operations. write the entire program blocks/
procedure/ function, etc.
It is declarative, that defines what PL/SQL is procedural that defines how
needs to be done, rather than how the things needs to be done.
things need to be done.
Execute as a single statement. Execute as a whole block.
HEADER
Type and Name of block
DECLARE
Variables; Constants; Cursors;
BEGIN
EXCEPTION
Exception handlers
END;
PL/SQL BLOCKS
• PL/SQL blocks can be divided into two groups:
1. Named and
2. Anonymous.
• Named blocks are used when creating subroutines.
• These subroutines are procedures, functions, and
packages.
• The subroutines can be stored in the database and referenced
by their names later on.
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE
('There is no student with student id 123');
END;
DISPLAYING OUTPUT
• The outputs are displayed by using DBMS_OUTPUT which is a built-in
package that enables the user to display output, debugging
information, and send messages from PL/SQL blocks, subprograms,
packages, and triggers.
• Let us see an example to see how to display a message using PL/SQL:
dbms_output.put_line
• command is used to direct the
PL/SQL output to a screen.
USING COMMENTS
• Like in many other programming languages, in PL/SQL also, comments
can be put within the code which has no effect in the code.
• There are two syntaxes to create comments in PL/SQL:
• Single Line Comment: To create a single line comment,
• the symbol – – is used.
• Multi Line Comment: To create comments that span over several lines,
the symbol /* and */ is used.
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.
• Example : code to print sum of two numbers taken from the user.
EXAMPLE
BEGIN
DBMS_OUTPUT.PUT_LINE('Today is ‘||’&sv_day');
DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ &sv_day');
END;
This example produces the following output:
The set of rows returned by a multi-row query is called the result set. An explicit cursor
"points" to the current row in the result set. This allows your program to process the
rows one at a time.
PL/SQL Cursors
Manipulating Cursors
You use the OPEN, FETCH, and CLOSE statements to control a cursor. The OPEN
statement executes the query associated with the cursor, identifies the result set, and
positions the cursor before the first row. The FETCH statement retrieves the current row
and advances the cursor to the next row. When the last row has been processed, the
CLOSE statement disables the cursor.