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.4.2

    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.4.2
    iconOceanBase Database
    SQL - V 4.4.2
    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:2026-04-02 06:23:56  Updated
    Share
    What is on this page
    Trigger types
    Create a trigger
    Limitations on triggers

    folded

    Share

    The MySQL-compatible mode of OceanBase Database 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

    The MySQL-compatible mode of OceanBase Database 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 [IF NOT EXISTS] trigger_name
        trigger_time trigger_event
        ON table_name FOR EACH ROW
        [trigger_order]
        trigger_body
    
    trigger_time: { BEFORE | AFTER }
    
    trigger_event: { INSERT | UPDATE | DELETE }
    
    trigger_order: { FOLLOWS | PRECEDES } other_trigger_name
    

    Syntax description:

    • IF NOT EXISTS: If you try to create a trigger whose name already exists without using the IF NOT EXISTS option, an error message is returned. If you use the IF NOT EXISTS option, a warning message instead of an error message is returned.

    • trigger_name: the name of the trigger, which must be unique.

    • tbl_name: the name of the table on 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 is the type of the operation that fires the trigger.

    • FOR EACH ROW: defines the trigger body. The statement is executed each time the trigger fires, once for each row affected by the triggering event.

    • trigger_order: the firing sequence of triggers with the same triggering event and operation time, which is specified by using the FOLLOWS and PRECEDES keywords. FOLLOWS indicates that a new trigger will fire after the current trigger. PRECEDES 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 the new data that is to be inserted in a BEFORE scenario or has been inserted in an AFTER scenario. Here, columnName refers to a column name in the corresponding table.

    • For an UPDATE trigger, OLD.columnName specifies the original data that is to be updated or has been updated, while NEW.columnName specifies the new data that is to be or has been updated to.

    • For a DELETE trigger, OLD.columnName specifies the original data that is to be deleted or has been deleted.

    • OLD.columnName is read-only, whereas NEW.columnName can be specified by using a SET statement.

    Example: Create a trigger named test_trg, associate it with a table named test to trigger an INSERT operation, and use the trigger as an accumulator to return the sum of column values inserted into 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.

    The BEGIN ... END syntax is as follows:

    BEGIN
    [statement_list]
    END
    

    In the preceding syntax, statement_list specifies a list of one or more 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-compatible mode have the following limitations:

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

      Note

      • Starting from OceanBase Database V4.1.0 BP4, the temporary table feature is disabled in MySQL-compatible mode.
      • For V4.4.x versions, the temporary table feature is supported in MySQL-compatible mode starting from V4.4.2.

    • 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 execution mechanism
    Next
    What is on this page
    Trigger types
    Create a trigger
    Limitations on triggers