How to write oracle trigger
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.
How to write an oracle trigger
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

The steps to open an Oracle database are as follows: Open the Oracle database client and connect to the database server: connect username/password@servername Use the SQLPLUS command to open the database: SQLPLUS
