Article directory
Review of past issues
- [Oracle] One of the Oracle series – Oracle data type
- [Oracle] Oracle Series 2 – Oracle Data Dictionary
- [Oracle] Oracle series three – Oracle character set
- [Oracle] Oracle Series 4 – User Management
- [Oracle] Oracle Series 5 – Oracle Table Space
- [Oracle] Oracle Series Six – Oracle Table Partitioning
- [Oracle] Oracle Series 7 – Table Creation and Management
- [Oracle] Oracle Series 8 – SQL Query
- [Oracle] Oracle Series 9 – Oracle Common Functions
- [Oracle] Oracle Series 10 – Oracle Regular Expressions
- [Oracle] Oracle Series 11 – PL/SQL
- [Oracle] Oracle series no. 12 – views, records, synonyms, sequences
- [Oracle] Oracle Series Thirteen – Cursor
Preface
1. Basic concepts
A trigger is a database object that can be viewed as a special stored procedure triggered by an event. When a specific event occurs, certain operations on the database table will be automatically performed. For example, when an operation (insert, delete, update) is performed on a table, its execution will be activated, causing other data in the database to change.
Triggers are often used to strengthen data integrity constraints and business rules.
Data validation: Ensures that insert, update, or delete operations comply with business rules and integrity constraints.
Data conversion: Convert data in insert, update, or delete operations to other formats or units.
Data record: Record the details of the insert, update or delete operation, such as timestamp, user ID, etc.
The syntax of Oracle trigger is as follows:
CREATE OR REPLACE TRIGGER trigger_name
{BEFORE | AFTER} {
INSERT | UPDATE | DELETE}
[OF column_name]
[ON table_name]
[REFERENCING OLD AS old NEW AS new]
[FOR EACH ROW]
WHEN (condition)
DECLARE
-- 声明局部变量和游标
BEGIN
-- 执行触发器操作
END;
The meaning of each keyword is as follows:
CREATE OR REPLACE TRIGGER: Create or replace a trigger.
BEFORE | AFTER: Specify whether the trigger fires before (BEFORE) or after (AFTER) the insert, update, or delete operation.
INSERT | UPDATE | DELETE: Specifies which operation the trigger fires on.
[OF column_name]: Specifies that only a certain column should be operated.
[ON table_name]: Specify the table name to which the trigger belongs.
[REFERENCING OLD AS old NEW AS new]: Specifies the use of OLD and NEW pseudo records to reference the old and new values.
[FOR EACH ROW]: Specifies that the trigger action be performed for each row.
[WHEN (condition)]: Specify the conditions for trigger execution.
DECLARE: Declare local variables and cursors.
BEGIN: Start executing the trigger operation.
END;: End the trigger code block.
/: Indicates the end of trigger definition.
There are three main types of Oracle triggers:
Row-level triggers: Fire every time a single record is inserted, updated, or deleted.
Statement-level triggers: Triggered after the SQL statement is executed.
System-level triggers: Triggered when the database as a whole is running.
2. Row-level triggers
A row-level trigger is a row-based trigger that fires every time a single record is inserted, updated, or deleted. This trigger is typically used to check whether a recorded value meets certain conditions and to prevent illegal operations.
e.g
-- delete时触发
Create Or Replace Trigger del_deptid
After Delete On dept
For Each Row
Begin
Delete From emp Where deptno=:Old.deptno;
End;
– Triggered when inserting
Create Or Replace Trigger insert_dept
After Insert On dept
For Each Row
Begin
Insert Into emp(empno,ename,deptno) Values('8999','bob',:New.deptno);
End;
– Triggered when updating
Create Or Replace Trigger update_dept
After Update On dept
For Each Row
Begin
Update emp Set deptno=:New.deptno Where deptno=:Old.deptno;
End;
Note: The update trigger uses the :Old and :New handles to reference the old and new values of the record being operated on. :Old.deptno refers to the id value of the updated record, while :New.deptno refers to the id value of the record after it has been updated. In this way, the details of the modified record can be obtained and logged in the trigger.
– Use row-level triggers and SEQUENCE to generate auto-incrementing IDs
create or replace trigger trg_emp
before insert on
T_EMP
for each row
begin
select seq_EMP_ID.nextval into :new.id from dual;
End;
3. Statement-level triggers
A statement-level trigger is a SQL statement-based trigger that fires after the SQL statement is executed. Typically used for logging, or synchronizing data between multiple tables.
Create Or Replace Trigger dnl_emp
After Insert Or Delete Or Update On emp
Begin
If Inserting Then
Insert Into mylog Values(User,Sysdate,'I');
Elsif Deleting Then
Insert Into mylog Values(User,Sysdate,'D');
Else
Insert Into mylog Values(User,Sysdate,'U');
End If;
End;
4. System-level triggers
System-level triggers are triggers that can be triggered at the entire database level. They are often used to monitor database objects and handle user logins, logouts, DDL statements and other important events, allowing administrators or DBAs to better control and manage the database. .
eg Record all SELECT query operations into the log table (audit_log).
CREATE OR REPLACE TRIGGER audit_select
AFTER SELECT ON SCOTT.EMP
FOR EACH STATEMENT
BEGIN
INSERT INTO audit_log
(username, query_date, table_name, sql_text)
VALUES
(USER, SYSDATE, 'EMP', ora_sql_txt);
END;
5. Alternative triggers
Since in ORACLE, views created by more than two tables cannot be directly operated, so alternative triggers are used to solve the problem of being unable to update when the tables that make up the view are two or more.
e.g
Create Or Replace Trigger tr_v_e_d
Instead Of Insert On v_emp_dept
For Each Row
Begin
Insert Into dept(deptno,dname) Values(:New.deptno,:New.dname);
Insert Into emp(empno,ename,job,deptno) Values(:New.empno,:New.ename,:New.job,:New.deptno);
End;