OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Database

SQL - V4.2.0

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogLive DemosTraining & CertificationTicket
    Company
    About OceanBaseTrust CenterLegalPartnerContact Us
    Follow Us

    © OceanBase 2026. All rights reserved

    Cloud Service AgreementPrivacy PolicySecurity
    Contact Us
    Document Feedback
    1. Documentation Center
    2. OceanBase Database
    3. SQL
    4. V4.2.0
    iconOceanBase Database
    SQL - V 4.2.0
    Databases
    • OceanBase Database
    • OceanBase Cloud
    • OceanBase Tugraph
    • Interactive Tutorials
    • OceanBase Best Practices
    Tools
    • OceanBase Cloud Platform
    • OceanBase Migration Service
    • OceanBase Developer Center
    • OceanBase Migration Assessment
    • OceanBase Admin Tool
    • OceanBase Loader and Dumper
    • OceanBase Deployer
    • Kubernetes operator for OceanBase
    • OceanBase Diagnostic Tool
    • OceanBase Binlog Service
    Connectors and Middleware
    • OceanBase Database Proxy
    • Embedded SQL in C for OceanBase
    • OceanBase Call Interface
    • OceanBase Connector/C
    • OceanBase Connector/J
    • OceanBase Connector/ODBC
    • OceanBase Connector/NET
    SQL
    KV
    • V 4.6.0
    • V 4.4.2
    • V 4.3.5
    • V 4.3.3
    • V 4.3.1
    • V 4.3.0
    • V 4.2.5
    • V 4.2.2
    • V 4.2.1
    • V 4.2.0
    • V 4.1.0
    • V 4.0.0
    • V 3.1.4 and earlier

    Triggers

    Last Updated:2023-10-31 11:17:12  Updated
    Share
    What is on this page
    Trigger types
    Create a trigger
    Limitations on triggers

    folded

    Share

    OceanBase Database in MySQL mode is compatible with the triggers of MySQL 5.6. A trigger is a table-related database object that you can activate when you use a statement to insert a row into, update a row of, or delete a row from a table that is associated with the trigger. For example, you can activate an INSERT trigger each time you insert a row by using an INSERT or LOAD DATA statement. If you insert two rows at the same time, the trigger fires twice.

    You can specify whether a trigger fires before or after a triggering event. For example, you can specify that a trigger fires before a row is inserted into a table or after a row in the table is updated.

    Trigger types

    OceanBase Database in MySQL mode supports the following types of triggers:

    • INSERT: A trigger of this type fires when you insert a row by using an INSERT, LOAD DATA, or REPLACE statement.

    • UPDATE: A trigger of this type fires when you update a row by using an UPDATE statement.

    • DELETE: A trigger of this type fires when you delete a row by using a DELETE or REPLACE statement.

    The INSERT INTO ... ON DUPLICATE KEY UPDATE statement is a special case in which a BEFORE INSERT trigger fires for each row, followed by an AFTER INSERT trigger or both the BEFORE UPDATE and AFTER UPDATE triggers, based on whether the row has a duplicate key.

    Create a trigger

    You can use a CREATE TRIGGER statement to create a trigger.

    To create a trigger, you must have the following privileges:

    • Privileges on the table associated with the trigger, such as SELECT, INSERT, UPDATE, and DELETE privileges

    • The CREATE privilege on triggers

    • Privileges on statements to be executed after the trigger fires

    The syntax is as follows:

    CREATE
        [DEFINER = user]
        TRIGGER trigger_name
        trigger_time trigger_event
        ON table_name FOR EACH ROW
        trigger_body
    
    trigger_time: { BEFORE | AFTER }
    
    trigger_event: { INSERT | UPDATE | DELETE }
    
    trigger_order: { FOLLOWS | PRECEDES } other_trigger_name
    

    The following list describes the syntax:

    • trigger_name: the name of the trigger. The name must be unique.

    • tbl_name: the name of the table for which the trigger is created.

    • BEFORE or AFTER: specifies whether the trigger fires before or after the triggering event. For example, whether the trigger fires before or after each row is inserted into the associated table.

    • INSERT, UPDATE, or DELETE: the triggering event, which specifies the type of the operation that fires the trigger.

    • FOR EACH ROW: specifies whether a trigger is a row trigger. When a row trigger fires, statements in the trigger are executed once for each row that is affected by the triggering event.

    • The trigger_order clause specifies the firing sequence of triggers with the same trigger event and operation time by using the FOLLOWS and PRECEDES keywords. The FOLLOWS keyword indicates that a new trigger will fire after the current trigger. The PRECEDES keyword indicates that a new trigger will fire before the current trigger.

    OceanBase Database also supports NEW.columnName and OLD.columnName.

    • For an INSERT trigger, NEW.columnName specifies new data that is inserted in a BEFORE scenario or was inserted in an AFTER scenario. columnName refers to a column name in the corresponding table.

    • For an UPDATE trigger, OLD.columnName specifies existing data that is updated. NEW.columnName specifies new data after the update.

    • For a DELETE trigger, OLD.columnName specifies existing data that is deleted.

    • Values in OLD.columnName are read-only, whereas values in NEW.columnName can be specified by using SET statements.

    For example, you can use the following syntax to create a trigger named test_trg and then associate the trigger with a table named test to activate INSERT operations. In this example, the trigger is also an accumulator, which can sum the values that are inserted into the columns of the table.

    obclient> CREATE TABLE test (user_id INT, user_num DECIMAL(10,2));
    Query OK, 0 rows affected
    
    obclient> CREATE TRIGGER test_trg BEFORE INSERT ON test
           FOR EACH ROW SET @sum = @sum + NEW.user_num;
    Query OK, 0 rows affected
    

    In addition, if you want to create a trigger that contains multiple statements, you can use the BEGIN … END statement to specify the start and end of the entire code block.

    BEGIN … END syntax:

    BEGIN
    [statement_list]
    END
    

    In the preceding syntax, statement_list specifies one or a list of statements to be executed. Each statement in the list must end with a semicolon (;). A semicolon (;) also indicates the end of an SQL statement. If the system detects a semicolon (;), the system determines it as the end of a statement and starts to execute the statement. In this case, an error is reported because the interpreter fails to find the END keyword that matches the BEGIN keyword during SQL execution. To prevent the error, you can use a DELIMITER command to change the delimiter of a statement.

    Here is a sample DELIMITER command:

    DELIMITER new_delemiter
    

    Here, new_delemiter specifies the delimiter of a statement. A delimiter can be a sign of one or more bytes in length. The default delimiter is a semicolon (;). You can change the semicolon (;) to another delimiter, such as a number sign (#).

    After the DELIMITER command is added to the syntax, statements with semicolons (;) following the command can be executed without an error reported. This is because the system does not consider that a statement ends until it detects the specified delimiter, such as a number sign (#).

    Notice

    If you use the DELIMITER command to change the delimiter, make sure that you reset the delimiter to the semicolon (;) after the statements are executed.

    Here is an example:

    obclient> CREATE TABLE test (user_id INT, user_num DECIMAL(10,2));
    Query OK, 0 rows affected
    
    obclient> DELIMITER #
    
    obclient> CREATE TRIGGER test_trg BEFORE UPDATE ON test
                   FOR EACH ROW
                   BEGIN
                   IF NEW.user_num < 1 THEN
                  SET NEW.user_num  = 1;
                  ELSEIF NEW.user_num > 45 THEN
                  SET NEW.user_num= 45;
                 END IF;
                 END;#
    Query OK, 0 rows affected
    
    obclient> DELIMITER;
    

    Limitations on triggers

    Triggers in MySQL mode have the following limitations:

    • Multiple triggers that are associated with the same table cannot share the triggering event or firing time.

    • You can create a trigger only for a permanent table, but not for a temporary table.

    • A trigger cannot use the CALL statement to call a stored procedure that returns data to the client or uses a dynamic SQL statement. However, a stored procedure or function can use an OUT or IN OUT parameter to return data to a trigger.

    • A statement used to start or end a transaction is not allowed in a trigger. For example, the following statements are not allowed: START TRANSACTION, COMMIT, and ROLLBACK. However, rolling back a transaction to a savepoint is allowed because this process does not end the transaction.

    • A foreign key action cannot fire a trigger.

    • A trigger cannot return a value. Make sure that the trigger does not contain a statement that is used to return values. If you want to instantly stop a trigger, use a LEAVE statement.

    Previous topic

    Functions
    Last

    Next topic

    PL subprogram execution mechanism
    Next
    What is on this page
    Trigger types
    Create a trigger
    Limitations on triggers