Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
32 views

PLSQL

The document discusses various MySQL programming concepts including PL/SQL constructs, views, triggers, and cursors. PL/SQL allows combining SQL with procedural programming and is a block-structured language. Views are virtual tables that do not store data but derive their contents from other tables. Triggers automatically run SQL or PL/SQL when events occur. Cursors manage processing of SQL statements and rows can be accessed one at a time. Implicit cursors are automatically created while explicit cursors provide more control.

Uploaded by

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

PLSQL

The document discusses various MySQL programming concepts including PL/SQL constructs, views, triggers, and cursors. PL/SQL allows combining SQL with procedural programming and is a block-structured language. Views are virtual tables that do not store data but derive their contents from other tables. Triggers automatically run SQL or PL/SQL when events occur. Cursors manage processing of SQL statements and rows can be accessed one at a time. Implicit cursors are automatically created while explicit cursors provide more control.

Uploaded by

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

MySQL Programming

Contents
PL/SQL Constructs
Views
Triggers
Cursors
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.

DECLARE declaration statements;


BEGIN executable statements;
EXCEPTIONS exception handling statements;
END;
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 that 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 interact with a Database server while PL/SQL does not interact with the
database server.
 SQL cannot contain PL/SQL code in it while PL/SQL is an extension of SQL,
so it can contain SQL inside it.
View
 View is a data object which does not contain any data. Contents of the view are the
resultant of a base table. They are operated just like base table but they don’t
contain any data of their own. The programming concepts are very similar to
MySQL.
 The difference between a view and a table is that views are definitions built on top
of other tables.
 View is a virtual table.
 If data is changed in the underlying table, the same change is reflected in the view
and vice-versa.
 Advantages of Views are:-
 To achieve logical data independence.
 To improve data security
 To reduce data distraction
 To preserves the appearance of original table structure.
Continue…
 We can create a view for single table as well as for multiple table.
 Types of Views :-
 Read Only View: Allows only SELECT operations.
 Updatable View: Allows SELECT as well as INSERT , UPDATE and DELETE
operations.
 Syntax:
CREATE <OR REPLACE> VIEW <ViewName> AS SELECT <ColumnName1 >,
<ColumnName2> FROM <TableName> WHERE <ColumnName> = < Expression List>
<WITH READ ONLY> ;
Example of Views

 We are creating a view emp_view on employee table by the following query:


Queries applicable for Views
 All the queries which are applicable for table, is also applicable for view
like select, update, delete, drop etc.
 If any modification done in view, it will be reflected in the base table also.
 For updating the View we can use only those column name which are
actually present in the View.
 For deleting the View we can use only those column name which are
actually present in the View.
 For dropping a View, we can use drop command. This command will not
drop the underlying table.
View for multiple table
 View from multiple tables can be created by simply include multiple tables
in the SELECT statement.
 In the given example, a view is created named MarksView from two tables
Student_Detail and Student_Marks.
CREATE VIEW MarksView AS
SELECT Student_Detail.NAME, Student_Detail.ADDRESS,
Student_Marks.MARKS
FROM Student_Detail, Student_Mark
WHERE Student_Detail.NAME = Student_Marks.NAME;
Triggers

 Triggers are stored program which automatically fired when some


events occur.
 Triggers are written to be executed in response to any of the following
events:
DML
DDL
Database Operations like Server Error, Log on , Log off, Startup or
shut down.
Syntax of Triggers
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;
Continue…
 CREATE [OR REPLACE] TRIGGER trigger_name − Creates or replaces an existing
trigger with the trigger_name.
 {BEFORE | AFTER | INSTEAD OF} − This specifies when the trigger will be executed.
The INSTEAD OF clause is used for creating trigger on a view.
 {INSERT [OR] | UPDATE [OR] | DELETE} − This specifies the DML operation.
 [OF col_name] − This specifies the column name that will be updated.
 [ON table_name] − This specifies the name of the table associated with the trigger.
 [REFERENCING OLD AS o NEW AS n] − This allows you to refer new and old values for
various DML statements, such as INSERT, UPDATE, and DELETE.
 [FOR EACH ROW] − This specifies a row-level trigger, i.e., the trigger will 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.
Example of Trigger
 We are considering the same Employee table to understand the Trigger.
Example of Trigger
Write a trigger to ensure that no employee of age less
than 20 can be inserted in the database.
Create a trigger which will work before deletion in employee
table and create a duplicate copy of the record in another
table.
 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.
Types of Cursor
 There are two types of cursors in PL/SQL:
 Implicit cursor
 Explicit cursor
Implicit Cursor
 If the Oracle engine opened a cursor for its internal processing it is known
as an Implicit Cursor. It is created “automatically” for the user by Oracle
when a query is executed and is simpler to code.
 Whenever a DML statement (INSERT, UPDATE and DELETE) is issued,
an implicit cursor is associated with this statement.
 For INSERT operations, the cursor holds the data that needs to be
inserted.
 For UPDATE and DELETE operations, the cursor identifies the rows that
would be affected.
Continue…
 If the Oracle engine opened a cursor for its internal processing it is known
as an Implicit Cursor. It is created “automatically” for the user by Oracle
when a query is executed and is simpler to code.
 Whenever a DML statement (INSERT, UPDATE and DELETE) is issued,
an implicit cursor is associated with this statement.
 For INSERT operations, the cursor holds the data that needs to be
inserted.
 For UPDATE and DELETE operations, the cursor identifies the rows that
would be affected.
Mostly used attributes of Implicit Cursor
S.No Attribute & Description
%FOUND
Returns TRUE if an INSERT, UPDATE, or DELETE statement affected one or more
1.
rows or a SELECT INTO statement returned one or more rows. Otherwise, it returns
FALSE.
%NOTFOUND
The logical opposite of %FOUND. It returns TRUE if an INSERT, UPDATE, or
2
DELETE statement affected no rows, or a SELECT INTO statement returno rows.
Otherwise, it returns FALSE.
%ISOPEN
3 Always returns FALSE for implicit cursors, because Oracle closes the SQL cursor
automatically after executing its associated SQL statement.
%ROWCOUNT
4 Returns the number of rows affected by an INSERT, UPDATE, or DELETE statement,
or returned by a SELECT INTO statement.
Explicit Cursor
 Explicit cursors are programmer-defined cursors for gaining more control over
the context area.
 An explicit cursor should be defined in the declaration section of the PL/SQL
Block. It is created on a SELECT Statement which returns more than one row.
 There are four steps in using an Explicit Cursor.
 DECLARE the cursor in the declaration section
 OPEN the cursor in the Execution Section.
 FETCH the data from cursor into PL/SQL variables or records in the
Execution Section.
 CLOSE the cursor in the Execution Section before you end the PL/SQL
Block.
Declaring the Cursor
 Declaring the cursor defines the cursor with a name and the associated SELECT
statement. For example −

 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
Continue…
For any query contact me on WhatsApp.

Be Safe, Stay at home.

You might also like