PL SQL
PL SQL
PL SQL
PL/SQL Concepts
Topics to be covered
• View
• Stored Procedures
• Database Triggers
• Cursors
Advantages of PL/SQL
§ Block structure:
• PL/SQL consist of block of code, which can be nested within each other.
• Each block forms a unit of a task or a logical module.
• PL/SQL blocks can be stored in the database and reused.
§ Procedural language capability:
• PL/SQL consist of procedural constructs such as conditional statements (if, if
else, nested if, else if ladder) and loops (for, while, do while).
§ Better performance:
• PL/SQL engine processes multiple SQL statements simultaneously as a single
block, thereby reducing network traffic.
§ Error handling:
• PL/SQL handles errors or exceptions effectively during the execution of PL/SQL
program.
• Once an exception is caught, specific action can be taken depending upon the
type of the exception or it can be displayed to the user with message.
View
§ In SQL, a VIEW is a virtual relation based on the result-set of a
SELECT statement.
§ A view contains rows and columns, just like a real table. The fields
in a view are fields from one or more real tables in the database.
§ In some cases, we can modify a view and present the data as if the
data were coming from a single table.
§ Syntax:-
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition;
Example of view
§ Consider the CUSTOMERS table having the following records:
ID NAME AGE CITY SALARY
1 Ramesh 32 Ahmedabad 20000
2 Karan 35 Rajkot 15000
3 Mayur 30 Surat 22000
4 Dipak 35 Rajkot 20000
5 Nilesh 38 Surat 29000
6 Kalpesh 37 Ahmedabad 26000
AS
BEGIN
Executable statements
END;
Triggers
§ CREATE [OR ALTER ] TRIGGER trigger_name:-
• This clause creates a trigger with the given name or overwrites an existing
trigger.
§ ON table_name:-
• This clause identifies the name of the table or view to which the trigger is
related.
§ { FOR | AFTER | INSTEAD OF }:-
• This clause indicates at what time the trigger should be fired. Before
executing DML statements or after executing DML statements.
Triggers
§ { [ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE ] } :-
• This clause determines on which kind of statement the trigger should be
fired.
• Either on insert or update or delete or combination of any or all.
• More than one statement can be used together separated by comma. The
trigger gets fired at all the specified triggering event.
Example of triggers
§ Trigger to display a message when we perform insert operation on
student table.