PL SQL
PL SQL
MANAGEMENT
SYSTEM
Contents
Views
Triggers
Cursor
Transaction Processing
PL/SQL CONSTRUCTS
PL/SQL is a combination of SQL along with the procedural features of programming
languages. It was developed by Oracle Corporation in the early 90's to enhance the
capabilities of SQL.
It is a block structured language that enables developers to combine the power of SQL with
procedural statements. All the statements of blocks are passed to oracle engine all at once
which increases processing speed and decreases the traffic.
⚫ Complex View: These views can contain more than one base table and they can
contain a group by clause, join conditions, an order by clause.
Syntax:
CREATE <OR REPLACE> VIEW <ViewName>
AS SELECT <ColumnName1 >, <ColumnName2> FROM <TableName>
WHERE <Condition> ;
EXAMPLE OF VIEWS
• Before creating the Trigger we need to create one emp_backup table so that it
holds the value of those employees who are no more the employee of the
institution.
CONTINUE…
We can drop the trigger by using drop command.
CURSOR
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
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 can hold more than one row, but can process only one
row at a time.
The set of rows the cursor holds is called the active set.
PROPERTIES CURSOR
Read only
Non-scrollable
Asensitive
TYPES OF CURSOR
Declare c_emp cursor for select Name, id, Address from employee [where
id>4];
OPENING THE CURSOR
Opening the cursor allocates the memory for the cursor and makes it ready for
fetching the rows returned by the SQL statement into it. For example, we will
open the above defined cursor as follows :-
OPEN c_emp;
FETCHING THE CURSOR
Fetching the cursor involves accessing one row at a time. For example, we will
fetch rows from the above-opened cursor as follows −
FETCH c_emp INTO c_id, c_name, c_addr;
CLOSING THE CURSOR
Closing the cursor means releasing the allocated memory. For example, we will
close the above-opened cursor as follows −
CLOSE c_emp;
COMPLETE EXAMPLE OF EXPLICIT CURSOR
mysql> select * from employee;
+-------+----------+------------+--------+------------+----------+
| EMPNO | EMP_NAME | DEPT | SALARY | DOJ | BRANCH |
+-------+----------+------------+--------+------------+----------+
| 101 | AMIT | PRODUCTION | 45000 | 2000-03-12 | BANGLORE |
| 103 | SUNITA | MANAGER | 120000 | 2001-01-11 | Indore |
| 105 | SUNITA | IT | 67000 | 2001-08-01 | delhi |
| 106 | MAHESH | IT | 145000 | 2003-09-20 | MUMBAI |
+-------+----------+------------+--------+------------+----------+
4 rows in set (0.00 sec)
mysql> create procedure cursordemo(id int)
-> begin
-> declare e_id int;
-> declare e_name varchar(20);
-> declare cur1 cursor for select empno, emp_name
-> from employee where id>101;
-> open cur1;
-> fetch cur1 into e_id,e_name ;
-> select e_id,e_name;
-> close cur1;
-> end $$
Query OK, 0 rows affected (0.01 sec)
DIFFERENCES BETWEEN SQL AND PL/SQL
• SQL is a single query that is used to perform DML and DDL operations while
PL/SQL is a block of codes, used to write the entire program blocks/
procedure/ function, etc.
• SQL is declarative, that defines what need to be done, rather than how things
need to be done while PL/SQL is procedural that defines how the things needs
to be done.
• SQL executes as a single statement while PL/SQL executes as a whole block.
• SQL mainly used to manipulate data while PL/SQL mainly used to create an
application.
• SQL cannot contain PL/SQL code in it while PL/SQL is an extension of SQL,
so it can contain SQL inside it.
TRANSACTION
A transaction is a program including a collection of database operations, executed as a
logical unit of data processing.
The operations performed in a transaction include one or more of database operations like
insert, delete, update or retrieve data.
It is an atomic process that is either performed into completion entirely or is not performed
at all.
A transaction involving only data retrieval without any data update is called read-only
transaction.
PROPERTIES OF TRANSACTION
• For example, if funds are transferred from one account to another, this only counts as a
bona fide transaction if both the withdrawal and deposit take place.
• If one account is debited and the other is not credited, it does not qualify as a transaction.
This property ensures that any transaction will bring the database from one valid state to
another.
The actions taken as a group do not violate any of the integrity constraints associated with
the state.
For example, when a fund transfer is made between two accounts the debiting of one and
the crediting of another must appear to take place simultaneously.
The funds cannot be credited to an account before they are debited from another.
If the TPS suffers failure, to recover the transaction a log will be created to keep the
record of all completed instructions.
Failed − A transaction is said to be in a failed state if any of the checks made by the
database recovery system fails. A failed transaction can no longer proceed further.
CONTINUE...
Aborted − If any of the checks fails and the transaction has reached a failed state, then the
recovery manager rolls back all its write operations on the database to bring the database
back to its original state where it was prior to the execution of the transaction. Transactions
in this state are called aborted. The database recovery module can select one of the two
operations after a transaction aborts −
⚫ Re-start the transaction
⚫ Kill the transaction
RELEASE SAVEPOINT:- This command is used to remove a SAVEPOINT that you have
created.
COMMIT
All the DDL commands are auto-committed whereas the DML commands need to commit
explicitly.
When we use any DML command like INSERT, UPDATE or DELETE, the changes made by
these commands are not permanent, until the current session is closed, the changes made by
these commands can be rolled back.
When you perform any insert, update or delete query it reflected to your table because in every
terminal the auto commit mode is by default set to 1 (i.e. ON).
If you set it to 0 (i.e. OFF) then the DML commands run by you will not affect your table. You
can check this by closing the terminal and reopen it.
Syntax: set autocommit=0;
OR
Start transaction;
Syntax for commit :- Commit;
ROLLBACK
The ROLLBACK command is used to undo transactions that have not already been saved to
the database.
Once you commit the data then you can’t rollback it.
Using this command we can name the different states of our data in any table and then rollback
to that state using the ROLLBACK command whenever required.
If you commit the data then you can’t rollback it to any created savepoint.
Once you release the higher level savepoint , then all the lower level savepoint will
automatically be released.