0% found this document useful (0 votes)
150 views13 pages

Procedures, Cursors and Triggers

This document discusses procedures, cursors, and triggers in a database. It defines procedures as functions that can be created and called at any time, including trigger procedures meant to be invoked by triggers. It explains how to create, alter, and drop procedures. It also defines cursors as pointers to the result set of a SELECT statement that avoid re-executing queries. The four SQL commands for working with cursors - declare, open, fetch, and close - are provided. Finally, it explains that triggers define actions that are activated by SQL operations on tables, and provides an example of creating a trigger to update student marks.

Uploaded by

test
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views13 pages

Procedures, Cursors and Triggers

This document discusses procedures, cursors, and triggers in a database. It defines procedures as functions that can be created and called at any time, including trigger procedures meant to be invoked by triggers. It explains how to create, alter, and drop procedures. It also defines cursors as pointers to the result set of a SELECT statement that avoid re-executing queries. The four SQL commands for working with cursors - declare, open, fetch, and close - are provided. Finally, it explains that triggers define actions that are activated by SQL operations on tables, and provides an example of creating a trigger to update student marks.

Uploaded by

test
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Procedures, Cursors and

Triggers
Content

Procedures
Cursors
Triggers
*Procedure

Procedures are similar to functions.


They can be created and called at any time.
There are even procedures called trigger
procedures.
Trigger procedures is a way of referring to a
function that is intended to be invoked by a
trigger.
Trigger procedures are always defined before
defining a trigger.
The steps to create a procedure

CREATE PROCEDURE procedurename


(arguments)
Declarations;[]
BEGIN
statements;
END;
Example to display students who got
distinction
To create the myProc procedure type the following
Eg:CREATE PROCEDURE myProc()
BEGIN
SELECT COUNT(marks) FROM students
WHERE marks>=70;
End;
To see the result, we need to execute this function
as
Eg: CALL myProc();
Alter / Drop Procedure

You can alter the procedure as


ALTER {PROCEDURE} procedurename
[characteristic ...]

You can drop the procedure as


DROP {PROCEDURE} [IF EXISTS]
procedurename

Eg: DROP PROCEDURE myProc;


*Cursors
A cursor is a read only pointer to a fully executed SELECT
statements result set.
It returns a reference to the cursor
If a cursor is declared to a query, then there is no need to re
execute the query
It avoids memory over run as the resultant set of a query is not
stored.
Can declare a cursor on a relation or query statement
Can open a cursor, and repeatedly fetch a tuple then move the
cursor, until all tuples have been retrieved.
The 4 SQL commands for cursors

1. Declare the cursor DECLARE


Syntax:
DECLARE cursorname [BINARY] [INSENSITIVE] [scroll] CURSOR FOR query
[FOR {READ ONLY | UPDATE [OF COLUMN [,]]}]

2. Open the cursor FETCH


OPEN cursorname;
3. Fetch tuples one by one MOVE
Syntax:
FETCH [FORWARD | BACKWARD | RELATIVE] [# | ALL | NEXT | PRIOR] {IN |
FROM } cursor
4. Close the cursor CLOSE
Syntax:
CLOSE cursorname;
Example How to use Cursor
Cursor can be declared along with a procedure.
Eg: CREATE PROCEDURE myProc(in_customer_id INT)
BEGIN
DECLARE v_id INT;
DECLARE v_name VARCHAR(30);
DECLARE c1 CURSOR FOR SELECT stdId,stdFirstname FROM
students WHERE stdId=in_customer_id;
OPEN c1;
FETCH c1 into v_id, v_name;
Close c1;
END;
*Triggers

A trigger defines a set of action and can be


activated when a specified SQL operation occurs on
the table
Active Database: a database with triggers
Triggers help in
Faster development of application
Validating input data
Support alerts
Data integrity
Automating manual processes
To create a trigger

A trigger has three parts:


1. Creates the trigger
CREATE TRIGGER triggername
2. Condition (tells when the trigger should run)
{BEFORE | AFTER} {EVENT(S)} ON tablename FOR EACH{row |
statement}
3. Action (procedure executed when trigger runs)
EXECUTE PROCEDURE functionname (arguments)
Example to create a Trigger
Create a trigger so that when inserting marks this example says that if
marks > 60 then set the marks user given when updating, else set
marks to 0.
Eg: CREATE TRIGGER updcheck BEFORE UPDATE ON students
FOR EACH ROW
BEGIN
IF NEW.marks > 60 THEN
SET New.marks = students.marks;
ELSE
SET New.marks = 0;
END IF;
END;
To check that trigger got executed just try to update marks column as
Eg: update students set marks=55 where stdFirstname='srila';
References

http://www.browardphp.com/mysql_manual_e
n/manual_Triggers.html
http://dev.mysql.com/doc/refman/5.0/en/curso
rs.html
http://forums.forta.com/messages.cfm?thread
id=30AADDDB-3048-80A9-
EFD34FB987F8B9C2
http://dev.mysql.com/doc/refman/5.0/en/store
d-procedures.html

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy