• No results found

Advanced Computing Training School (ACTS) Advanced Computing for Human Advancement. Triggers Part - II. - Jayendra Khatod. Copyright CDAC-ACTS 1

N/A
N/A
Protected

Academic year: 2021

Share "Advanced Computing Training School (ACTS) Advanced Computing for Human Advancement. Triggers Part - II. - Jayendra Khatod. Copyright CDAC-ACTS 1"

Copied!
21
0
0

Loading.... (view fulltext now)

Full text

(1)

Triggers Part - II

- Jayendra Khatod

(2)

Objectives

Learn more Trigger concepts

Create additional database triggers

Explain the rules governing triggers

Implement triggers

(3)

Creating Triggers on System Events

CREATE [OR REPLACE] TRIGGER trigger_name

trigger_body

CREATE [OR REPLACE] TRIGGER trigger_name timing

[database_event1

[OR database_event2 OR ...]]

ON {DATABASE|SCHEMA}

trigger_body

(4)

Log On and Log Off Trigger Example

SQL> CREATE OR REPLACE TRIGGER LOGON_TRIG

, 'Logging on');

SQL> CREATE OR REPLACE TRIGGER LOGON_TRIG 2 AFTER logon ON SCHEMA

3 BEGIN

4 INSERT INTO log_trig_table

5 (user_id, log_date, action)

6 VALUES (user, sysdate, 'Logging on');

7 END;

SQL> CREATE OR REPLACE TRIGGER LOGOFF_TRIG

SQL> CREATE OR REPLACE TRIGGER LOGOFF_TRIG 2 BEFORE logoff ON SCHEMA

3 BEGIN

4 INSERT INTO log_trig_table

5 (user_id, log_date, action)

(5)

CALL Statement

CREATE [OR REPLACE] TRIGGER trigger_name

[FOR EACH ROW]

CALL

CREATE [OR REPLACE] TRIGGER trigger_name timing

event1 [OR event2 OR event3]

ON table_name

[REFERENCING OLD AS old | NEW AS new]

[FOR EACH ROW]

[WHEN condition]

CALL procedure_name

SQL> CREATE TRIGGER TEST3

SQL> CREATE TRIGGER TEST3 2 BEFORE INSERT ON EMP 3 CALL LOG_EXECUTION 4 /

(6)

Reading Data from a Mutating Table

Trigger event Trigger event

UPDATE employees SET salary = 3400

WHERE last_name = SMITH';

EMPLOYEEStabletable EMPNO

7369 7698 7788

ENAME SMITH BLAKE SCOTT

SAL 1500 2850 3000

Failure

Trigged table/

Trigged table/

mutating table mutating table

BEFORE BEFORE UPDATE UPDATE row

row

CHECK_SALARY CHECK_SALARY trigger

trigger JOB

CLERK MANAGER ANALYST

(7)

Mutating Table: Example

CREATE OR REPLACE TRIGGER

BEFORE INSERT OR UPDATE OF salary, ON employees

FOR EACH ROW WHEN (

DECLARE

BEGIN

END;

CREATE OR REPLACE TRIGGER check_salary BEFORE INSERT OR UPDATE OF salary, job_id ON employees

FOR EACH ROW

WHEN (NEW.job_id <> 'AD_PRES') DECLARE

v_minsalary employees.salary%TYPE;

v_maxsalary employees.salary%TYPE;

BEGIN

SELECT MIN(salary), MAX(salary) INTO v_minsalary, v_maxsalary FROM employees

WHERE job_id = :NEW.job_id;

IF :NEW.salary < v_minsalary OR :NEW.salary > v_maxsalary THEN

RAISE_APPLICATION_ERROR(-20505,'Out of range');

END IF;

END;

(8)

Mutating Table: Example

UPDATE employees SET salary = 3400 WHERE

*

ERROR at line 2:

ORA

trigger/function may not see it ORA

ORA

UPDATE employees SET salary = 3400

WHERE last_name = 'Stiles';

*

ERROR at line 2:

ORA-04091: table EMPLOYEES is mutating, trigger/function may not see it

ORA-06512: at "CHECK_SALARY", line 5

ORA-04088: error during execution of trigger 'CHECK_SALARY'

(9)

Implementation of Triggers

Security

Data integrity

Referential integrity Table replication

Event logging

You can use trigger for:

(10)

Controlling Security Within the Server

GRANT SELECT, INSERT, UPDATE, DELETE ON employees

TO clerk;

GRANT clerk TO

GRANT SELECT, INSERT, UPDATE, DELETE ON employees

TO clerk; -- database role GRANT clerk TO scott;

(11)

Controlling Security with a Database Trigger

CREATE OR REPLACE TRIGGER

BEFORE INSERT OR UPDATE OR DELETE ON employees DECLARE

BEGIN

END;

CREATE OR REPLACE TRIGGER secure_emp

BEFORE INSERT OR UPDATE OR DELETE ON employees DECLARE

v_dummy VARCHAR2(1);

BEGIN

IF (TO_CHAR (SYSDATE, 'DY') IN ('SAT','SUN')) THEN

RAISE_APPLICATION_ERROR (-20506,'You may only change data during normal business hours.');

END IF;

SELECT COUNT(*) INTO v_dummy FROM holiday WHERE holiday_date = TRUNC (SYSDATE);

IF v_dummy > 0 THEN

RAISE_APPLICATION_ERROR(-20507, 'You may not change data on a holiday.');

END IF;

END;

(12)

Auditing Using the Server Facility

AUDIT INSERT, UPDATE, DELETE ON departments

BY ACCESS

WHENEVER SUCCESSFUL;

AUDIT INSERT, UPDATE, DELETE ON departments

BY ACCESS

WHENEVER SUCCESSFUL;

Oracle will store the audit information in a data Oracle will store the audit information in a data dictionary table.

dictionary table.

(13)

Auditing Using a Trigger

CREATE OR REPLACE TRIGGER audit_emp_values AFTER DELETE OR INSERT OR UPDATE ON employees FOR EACH ROW

BEGIN

,

END;

CREATE OR REPLACE TRIGGER audit_emp_values

AFTER DELETE OR INSERT OR UPDATE ON employees FOR EACH ROW

BEGIN

IF (audit_emp_package.g_reason IS NULL) THEN

RAISE_APPLICATION_ERROR (-20059, 'Specify a reason for the data operation through the procedure SET_REASON of the AUDIT_EMP_PACKAGE before proceeding.');

ELSE

INSERT INTO audit_emp_table (user_name, timestamp, id, old_last_name, new_last_name, old_title, new_title,

old_salary, new_salary, comments)

VALUES (USER, SYSDATE, :OLD.employee_id, :OLD.last_name, :NEW.last_name, :OLD.job_id, :NEW.job_id, :OLD.salary,

:NEW.salary, audit_emp_package.g_reason);

END IF;

END;

(14)

Enforce Data Integrity Within the Server

ALTER TABLE employees ADD CONSTRAINT

ALTER TABLE employees ADD

CONSTRAINT ck_salary CHECK (salary >= 500);

(15)

Protect Data Integrity with a Trigger

CREATE OR REPLACE TRIGGER check_salary BEFORE UPDATE OF salary ON employees

FOR EACH ROW

WHEN (NEW.salary < OLD.salary) BEGIN

RAISE_APPLICATION_ERROR (-20508, 'Do not decrease salary.');

END;

/

(16)

Enforce Referential Integrity Within the Server

ALTER TABLE employees ADD CONSTRAINT

FOREIGN KEY (

REFERENCES departments(

ON DELETE CASCADE;

ALTER TABLE employees

ADD CONSTRAINT emp_deptno_fk FOREIGN KEY (department_id)

REFERENCES departments(department_id) ON DELETE CASCADE;

(17)

Protect Referential Integrity with a Trigger

CREATE OR REPLACE TRIGGER AFTER UPDATE OF

FOR EACH ROW BEGIN

;

END;

CREATE OR REPLACE TRIGGER cascade_updates AFTER UPDATE OF department_id ON departments FOR EACH ROW

BEGIN

UPDATE employees

SET employees.department_id=:NEW.department_id

WHERE employees.department_id=:OLD.department_id;

UPDATE job_history

SET department_id=:NEW.department_id

WHERE department_id=:OLD.department_id;

END;

(18)

Trigger Information

You can view the following trigger information:

USER_OBJECTS data dictionary view: object information

USER_TRIGGERS data dictionary view: the text of the trigger

USER_ERRORS data dictionary view: PL/SQL syntax errors (compilation errors) of the trigger

(19)

Benefits of Database Triggers

•Improved data security:

Provide value-based security checks Provide value-based auditing

•Improved data integrity:

Enforce dynamic data integrity constraints

Enforce complex referential integrity constraints

Ensure related operations are performed together implicitly

(20)

Summary

• Use advanced database triggers

• List mutating and constraining rules for triggers

• Describe the real-world application of triggers

• Auditing

• Protecting Referential Integrity

• Enforcing Data Integrity

• Computing Derived Values

• Manage triggers

• View trigger information

(21)

Thank You !

References

Related documents

Nevertheless, it is considerably much more flexible to generate triangular meshes over curved surfaces, and by the systematic merging of triangles, unstructured quadrilateral

The total energy consumption shown in Figure 11a was divided into drive and auxiliary energy consumption to better understand the source of energy variation between

Advanced Business Analytics (Revised Program) Certificate 3.. Advanced Cloud Computing (Revised Program)

China Marketing International Conference, Xi’An, China, July 2015 INFORMS Marketing Science Conference, Baltimore, June 2015 INFORMS Marketing Science Conference, Atlanta, June 2014

• Oracle Advanced Supply Chain Planning explodes the forecast from model to option using the Oracle Demantra Demand Management planning percentages Oracle Advanced Supply

Objective: The current Surgical Care Improvement Project (SCIP) measure for controlled postoperative 6-AM glycemic control after cardiac surgery identifies those with blood

We tested whether song complexity correlated with performance on a suite of novel foraging problems in flocks of male zebra finches, starting by replicating the lid-flipping task used

A mobile communication means according to claim 6, Wherein said means are arranged so that the cookie proxy is informed to continue storing cookies associated With current