0% found this document useful (0 votes)
19 views21 pages

4 عملي student

The document provides an overview of procedures, functions, and triggers in PL/SQL, detailing how to create and execute procedures, the types of parameters, and the syntax for triggers. It explains the differences between row and statement triggers, their purposes, and how they are invoked automatically upon specific database events. Examples illustrate the creation of procedures and triggers, highlighting their functionality in managing database operations.

Uploaded by

marwan.jaaafar
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)
19 views21 pages

4 عملي student

The document provides an overview of procedures, functions, and triggers in PL/SQL, detailing how to create and execute procedures, the types of parameters, and the syntax for triggers. It explains the differences between row and statement triggers, their purposes, and how they are invoked automatically upon specific database events. Examples illustrate the creation of procedures and triggers, highlighting their functionality in managing database operations.

Uploaded by

marwan.jaaafar
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/ 21

PROCEDURES, FUNCTIONS, and

TRIGGERS
PROCEDURES
• A PROCEDURE IS A MODULE PERFORMING
ONE OR MORE ACTIONS; IT DOES NOT
NEED TO RETURN ANY VALUES.
• THE SYNTAX FOR CREATING A PROCEDURE
IS AS FOLLOWS:
CREATE OR REPLACE PROCEDURE name
[(parameter[, parameter, ...])]
AS
[local declarations]
BEGIN
executable statements
[EXCEPTION
exception handlers]
END [name];
PROCEDURES
• A PROCEDURE MAY HAVE 0 TO MANY
PARAMETERS.
• EVERY PROCEDURE HAS TWO PARTS:
1. The header portion, which comes before AS
(sometimes you will see IS—they are
interchangeable), keyword (this contains the
procedure name and the parameter list),
2. The body, which is everything after the IS keyword.
• The word REPLACE is optional.
• WHEN THE WORD REPLACE IS NOT USED
IN THE HEADER OF THE PROCEDURE, IN
ORDER TO CHANGE THE CODE IN THE
PROCEDURE, IT MUST BE DROPPED FIRST
AND THEN RE-CREATED.
Example
CREATE OR REPLACE PROCEDURE DISCOUNT
AS
CURSOR C_GROUP_DISCOUNT
IS
SELECT DISTINCT S.COURSE_NO, C.DESCRIPTION
FROM SECTION S, ENROLLMENT E, COURSE C
WHERE S.SECTION_ID = E.SECTION_ID
AND C.COURSE_NO = S.COURSE_NO
GROUP BY S.COURSE_NO, C.DESCRIPTION,
E.SECTION_ID, S.SECTION_ID
HAVING COUNT(*) >=8;
BEGIN
[CONTINUED IN THE NEXT SLIDE]
Example
FOR R_GROUP_DISCOUNT IN C_GROUP_DISCOUNT
LOOP
UPDATE COURSE
SET COST = COST * .95
WHERE COURSE_NO =
R_GROUP_DISCOUNT.COURSE_NO;
DBMS_OUTPUT.PUT_LINE
('A 5% DISCOUNT HAS BEEN GIVEN TO'||
R_GROUP_DISCOUNT.COURSE_NO||' '||
R_GROUP_DISCOUNT.DESCRIPTION
);
END LOOP;
END;
Example
• IN ORDER TO EXECUTE A PROCEDURE,
USE THE FOLLOWING SYNTAX:

EXECUTE PROCEDURE_NAME

SQL> EXECUTE DISCOUNT


PARAMETERS
• PARAMETERS ARE THE MEANS TO PASS
VALUES TO AND FROM THE CALLING
ENVIRONMENT TO THE SERVER.
• THESE ARE THE VALUES THAT WILL BE
PROCESSED OR RETURNED VIA THE
EXECUTION OF THE PROCEDURE.
• THERE ARE THREE TYPES OF PARAMETERS:
• IN, OUT, AND IN OUT.
• MODES SPECIFY WHETHER THE PARAMETER
PASSED IS READ IN OR A RECEPTACLE FOR
WHAT COMES OUT.
Types of Parameters
FORMAL AND ACTUAL
PARAMETERS
• FORMAL PARAMETERS ARE THE
NAMES SPECIFIED WITHIN
PARENTHESES AS PART OF THE
HEADER OF A MODULE.
• ACTUAL PARAMETERS ARE THE
VALUES—EXPRESSIONS SPECIFIED
WITHIN PARENTHESES AS A
PARAMETER LIST—WHEN A CALL IS
MADE TO THE MODULE.
• THE FORMAL PARAMETER AND THE
RELATED ACTUAL PARAMETER MUST
BE OF THE SAME OR COMPATIBLE
MATCHING ACTUAL AND FORMAL PARAMETERS
• TWO METHODS CAN BE USED TO MATCH ACTUAL
AND FORMAL PARAMETERS: POSITIONAL
NOTATION AND NAMED NOTATION.
• POSITIONAL NOTATION IS SIMPLY ASSOCIATION BY
POSITION: THE ORDER OF THE PARAMETERS USED
WHEN EXECUTING THE PROCEDURE MATCHES THE
ORDER IN THE PROCEDURE’S HEADER EXACTLY.
• NAMED NOTATION IS EXPLICIT ASSOCIATION
USING THE SYMBOL =>
Syntax: formal_parameter_name =>
argument_value
• IN NAMED NOTATION, THE ORDER DOES NOT
MATTER.
• IF YOU MIX NOTATION, LIST POSITIONAL NOTATION
BEFORE NAMED NOTATION.
MATCHING ACTUAL AND FORMAL PARAMETERS
TRIGGERS
• A DATABASE TRIGGER IS A STORED
PL/SQL PROGRAM UNIT ASSOCIATED
WITH A SPECIFIC DATABASE TABLE.
• ORACLE EXECUTES (FIRES) A DATABASE
TRIGGER AUTOMATICALLY WHEN A GIVEN
SQL OPERATION (LIKE INSERT, UPDATE OR
DELETE) AFFECTS THE TABLE.
• UNLIKE A PROCEDURE, OR A FUNCTION,
WHICH MUST BE INVOKED EXPLICITLY,
DATABASE TRIGGERS ARE INVOKED
IMPLICITLY.
TRIGGERS

DATABASE TRIGGERS CAN BE USED TO


PERFORM ANY OF THE FOLLOWING:
• AUDIT DATA MODIFICATION
• LOG EVENTS TRANSPARENTLY
• ENFORCE COMPLEX BUSINESS RULES
• DERIVE COLUMN VALUES
AUTOMATICALLY
• IMPLEMENT COMPLEX SECURITY
AUTHORIZATIONS
• MAINTAIN REPLICATE TABLES
TRIGGERS
• YOU CAN ASSOCIATE UP TO 12
DATABASE TRIGGERS WITH A GIVEN
TABLE. A DATABASE TRIGGER HAS
THREE PARTS: A TRIGGERING EVENT,
AN OPTIONAL TRIGGER
CONSTRAINT, AND A TRIGGER
ACTION.
• WHEN AN EVENT OCCURS, A DATABASE
TRIGGER IS FIRED, AND A PREDEFINED
PL/SQL BLOCK WILL PERFORM THE
NECESSARY ACTION.
TRIGGERS
SYNTAX:
CREATE [OR REPLACE] TRIGGER TRIGGER_NAME
{BEFORE|AFTER} TRIGGERING_EVENT ON TABLE_NA
[FOR EACH ROW]
[WHEN CONDITION]
DECLARE
DECLARATION STATEMENTS
BEGIN
EXECUTABLE STATEMENTS
EXCEPTION
EXCEPTION-HANDLING STATEMENTS
END;
TRIGGERS
THE TRIGGER_NAME REFERENCES THE NAME OF
THE TRIGGER.
BEFORE OR AFTER SPECIFY WHEN THE TRIGGER IS
FIRED (BEFORE OR AFTER THE TRIGGERING
EVENT).
THE TRIGGERING_EVENT REFERENCES A DML
STATEMENT ISSUED AGAINST THE TABLE (E.G.,
INSERT, DELETE, UPDATE).
THE TABLE_NAME IS THE NAME OF THE TABLE
ASSOCIATED WITH THE TRIGGER.
THE CLAUSE, FOR EACH ROW, SPECIFIES A
TRIGGER IS A ROW TRIGGER AND FIRES ONCE
FOR EACH MODIFIED ROW.
A WHEN CLAUSE SPECIFIES THE CONDITION FOR A
TRIGGER TO BE FIRED.
BEAR IN MIND THAT IF YOU DROP A TABLE, ALL
THE ASSOCIATED TRIGGERS FOR THE TABLE
ARE DROPPED AS WELL.
TYPES OF TRIGGERS

TRIGGERS MAY BE CALLED BEFORE OR


AFTER THE FOLLOWING EVENTS:
INSERT, UPDATE AND DELETE.
THE BEFORE/AFTER OPTIONS CAN BE USED
TO SPECIFY WHEN THE TRIGGER BODY
SHOULD BE FIRED WITH RESPECT TO
THE TRIGGERING STATEMENT.
IF THE USER INDICATES A BEFORE OPTION,
THEN ORACLE FIRES THE TRIGGER
BEFORE EXECUTING THE TRIGGERING
STATEMENT.
ON THE OTHER HAND, IF AN AFTER IS
USED, ORACLE FIRES THE TRIGGER
AFTER EXECUTING THE TRIGGERING
STATEMENT.
TYPES OF TRIGGERS
• A TRIGGER MAY BE A ROW OR
STATEMENT TYPE.
• IF THE STATEMENT FOR EACH ROW IS
PRESENT IN THE CREATE TRIGGER
CLAUSE OF A TRIGGER, THE TRIGGER
IS A ROW TRIGGER. A ROW TRIGGER IS
FIRED FOR EACH ROW AFFECTED BY A
TRIGGERING STATEMENT.
• A STATEMENT TRIGGER, HOWEVER, IS
FIRED ONLY ONCE FOR THE
TRIGGERING STATEMENT, REGARDLESS
OF THE NUMBER OF ROWS AFFECTED
BY THE TRIGGERING STATEMENT.
TYPES OF TRIGGERS
EXAMPLE: STATEMENT TRIGGER

CREATE OR REPLACE TRIGGER MYTRIG1 BEFORE DELETE OR INSERT OR


UPDATE ON EMPLOYEE
BEGIN
IF (TO_CHAR(SYSDATE, 'DAY') IN ('SAT', 'SUN')) OR
(TO_CHAR(SYSDATE,'HH:MI') NOT BETWEEN '08:30' AND '18:30')
THEN RAISE_APPLICATION_ERROR(-20500, 'TABLE IS
SECURED');
END IF;
END;
/
THE ABOVE EXAMPLE SHOWS A TRIGGER
THAT LIMITS THE DML ACTIONS TO THE EMPLOYEE TABLE
TO WEEKDAYS FROM 8.30AM TO
6.30PM. IF A USER TRIES TO INSERT/UPDATE/DELETE
A ROW IN THE EMPLOYEE TABLE, A WARNING
MESSAGE WILL BE PROMPTED.
Example: ROW Trigger
CREATE OR REPLACE TRIGGER MYTRIG2
AFTER DELETE OR INSERT OR UPDATE ON EMPLOYEE
FOR EACH ROW
BEGIN
IF DELETING THEN
INSERT INTO XEMPLOYEE (EMP_SSN, EMP_LAST_NAME,EMP_FIRST_NAME,
DELDATE)
VALUES (:OLD.EMP_SSN, :OLD.EMP_LAST_NAME,:OLD.EMP_FIRST_NAME,
SYSDATE);
ELSIF INSERTING THEN
INSERT INTO NEMPLOYEE (EMP_SSN, EMP_LAST_NAME,EMP_FIRST_NAME,
ADDDATE)
VALUES (:NEW.EMP_SSN, :NEW.EMP_LAST_NAME,:NEW.EMP_FIRST_NAME,
SYSDATE);
ELSIF UPDATING('EMP_SALARY') THEN
INSERT INTO CEMPLOYEE (EMP_SSN, OLDSALARY, NEWSALARY, UP_DATE)
VALUES (:OLD.EMP_SSN,:OLD.EMP_SALARY, :NEW.EMP_SALARY, SYSDATE);
ELSE
INSERT INTO UEMPLOYEE (EMP_SSN, EMP_ADDRESS, UP_DATE)
VALUES (:OLD.EMP_SSN, :NEW.EMP_ADDRESS, SYSDATE);
END IF;
END;
/
TYPES OF TRIGGERS
SQL> DELETE FROM EMPLOYEE
WHERE EMP_LAST_NAME = 'JOSHI';
1 ROW DELETED.
SQL> SELECT * FROM XEMPLOYEE;

EMP_SSN EMP_LAST_NAME EMP_FIRST_NAME


DELDATE
------------- ----------------------- --------------------------
-----------------
999333333 JOSHI DINESH 02-MAY-23

You might also like