In Oracle, a trigger will automatically execute a defined statement when the specified condition is met. The writing method is "create [or replace] tigger trigger name trigger time trigger event on table name [for each row] begin pl/sql statement end".
The operating environment of this tutorial: Windows 10 system, Oracle 11g version, Dell G3 computer.
1. Introduction to triggers
The definition of a trigger means that when a certain condition is established, it triggers The statements defined in the container will be automatically executed.
Therefore, triggers do not need to be called manually and cannot be called.
Then, the triggering conditions of the trigger are actually set when you define it.
It needs to be explained here that triggers can be divided into statement-level triggers and row-level triggers.
For detailed introduction, please refer to the information on the Internet. Simply put, statement-level triggers can be triggered before or after the execution of certain statements. Row-level triggers are triggered once when the row data in the defined trigger table changes.
Specific examples:
1. A statement-level trigger defined in a table. When the table is deleted, the program will automatically execute the operation process defined in the trigger. This means that the operation of deleting the table is the condition for trigger execution.
2. If a row-level trigger is defined in a table, when a row of data in the table changes, such as deleting a row of records, the trigger will be automatically executed.
2. Trigger syntax
Trigger syntax:
create [or replace] tigger 触发器名 触发时间 触发事件 on 表名 [for each row] begin pl/sql语句 end
Among them:
trigger Trigger name: The name of the trigger object. Since the trigger is automatically executed by the database, the name is just a name and has no real purpose.
Trigger time: Indicates when the trigger is executed. The value is optional:
before: Indicates that the trigger is executed before the database action;
after: Indicates that the trigger is executed after the database action.
Trigger event: Indicate which database actions will trigger this trigger:
insert: Database insertion will trigger this trigger;
update: Database modification will trigger this trigger;
delete: Database deletion will trigger this trigger.
Table name: The table where the database trigger is located.
for each row: The trigger is executed once for each row of the table. Without this option, it is executed only once for the entire table.
Triggers can achieve the following functions:
Function:
1. Allow/restrict modifications to the table
2. Automatically generate derived columns, such as auto-increment fields
3. Enforce data consistency
4. Provide auditing and logging
5. Prevent invalid transaction processing
6. Enable complex business logic
Example
1) The following trigger is triggered before updating the table tb_emp, in order to not allow the table to be modified on weekends:
create or replace trigger auth_secure before insert or update or DELETE on tb_emp begin IF(to_char(sysdate,'DY')='星期日') THEN RAISE_APPLICATION_ERROR(-20600,'不能在周末修改表tb_emp'); END IF; END; /
2), use triggers to realize the serial number auto-increment
Create a test table:
create table tab_user( id number(11) primary key, username varchar(50), password varchar(50) );
Create a sequence:
Copy the code The code is as follows:
create sequence my_seq increment by 1 start with 1 nomaxvalue nocycle cache 20;
Create a trigger:
CREATE OR REPLACE TRIGGER MY_TGR BEFORE INSERT ON TAB_USER FOR EACH ROW--对表的每一行触发器执行一次 DECLARE NEXT_ID NUMBER; BEGIN SELECT MY_SEQ.NEXTVAL INTO NEXT_ID FROM DUAL; :NEW.ID := NEXT_ID; --:NEW表示新插入的那条记录 END;
Insert data into the table:
insert into tab_user(username,password) values('admin','admin'); insert into tab_user(username,password) values('fgz','fgz'); insert into tab_user(username,password) values('test','test'); COMMIT;
Query table results: SELECT * FROM TAB_USER;
Recommended tutorial: "Oracle Video tutorial》
The above is the detailed content of How to write oracle trigger. For more information, please follow other related articles on the PHP Chinese website!