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

    Use of integrity constraints

    Last Updated:2026-04-02 06:23:56  Updated
    Share
    What is on this page
    Timing of integrity constraint checking
    Constraint states

    folded

    Share

    This topic describes the timing of integrity constraint checking and the constraint states.

    Timing of integrity constraint checking

    Knowing when OceanBase Database checks constraints helps identify the types of operations that can be performed when various constraints exist. The following examples illustrate when OceanBase Database checks constraints.

    In the following figure, the EMP table is created. A self-referential constraint is defined on the EMP table so that the values of the MGR column depend on the values of the EMPNO column. For simplicity, the following content addresses only the EMPNO (employee_id) and MGR (manager_id) columns of the EMP table.

    Integrity constraints

    This example concerns the insertion of the first row into the EMP table. The value in the MGR column cannot reference any existing value in the EMPNO column because no data exists in the table. In this case, you can insert a row as described in the following scenarios:

    • If no NOT NULL constraint is defined on the MGR column, you can insert a null value into the MGR column in the first row. This row can be inserted into the table because FOREIGN KEY constraints allow null values.

    • You can insert the same values into the EMPNO and MGR columns in the first row. In this case, OceanBase Database checks constraints after the statement is executed. To insert the first row with the same value in the parent key and the foreign key, OceanBase Database must first execute the INSERT statement to insert the first row and then check whether any row in the table has an EMPNO column value that corresponds to the MGR column value of the first row.

    • Execute a multiple row INSERT statement, such as an INSERT statement with nested SELECT statements to insert multiple rows that reference one another. For example, the first row may have 200 for the EMPNO column and 300 for the MGR column, whereas the second row may have 300 for the EMPNO column and 200 for the MGR column.

    In this case, constraint checking is deferred until the complete execution of the INSERT statement. OceanBase Database first inserts all rows and then checks for constraint violations row by row.

    The following example illustrates when OceanBase Database checks a self-referential constraint for an UPDATE statement. Assume that the company is acquired and all employee numbers must be increased by 5000 to be consistent with the employee numbers of the new company. Manager numbers must also be increased by 5000 because they are also employee numbers. The following figure shows the EMP table before the update. This table contains the EMPNO and MGR columns. The EMPNO column has three values: 210, 211, and 212. The MGR column has two values: 210 and 211.

    Integrity constraint 3

    Execute the following SQL statement on the EMP table:

    UPDATE EMP
    SET empno = empno + 5000,
    mgr = mgr + 5000;
    

    Although the constraint defined on the EMP table requires that each MGR column value must correspond to an EMPNO column value, the preceding statement can still be executed because OceanBase Database checks the constraint after the statement is executed. The following figure shows that OceanBase Database checks the constraint after all operations of the SQL statement are performed.

    Integrity constraint 5

    First, 5000 is added to each employee number and then each manager number. In Step 1, 210 in the EMPNO column is updated to 5210. In Step 2, 211 in the EMPNO column is updated to 5211, and 210 in the MGR column is updated to 5210. In Step 3, 212 in the EMPNO column is updated to 5212, and 211 in the MGR column is updated to 5211. Finally, OceanBase Database checks the constraint.

    The examples in this section describe the constraint checking mechanism for INSERT and UPDATE statements. In fact, the constraint checking mechanism is the same for all types of DML statements, including UPDATE, INSERT, and DELETE statements.

    Constraint states

    In the Oracle-compatible mode of OceanBase Database, FOREIGN KEY constraints, CHECK constraints, and NOT NULL constraints all support two types of states: ENABLE or DISABLE and VALIDATE or NOVALIDATE. ENABLE or DISABLE specifies whether to check whether new data inserted or updated by executing DML statements on the table conforms to the constraint. VALIDATE or NOVALIDATE indicates whether all existing data in the table conforms to the constraint.

    Default constraint states

    When you create a constraint, its default state for ENABLE or DISABLE is ENABLE.

    If the state for ENABLE or DISABLE is ENABLE, the default state for VALIDATE or NOVALIDATE is VALIDATE.

    If the state for ENABLE or DISABLE is DISABLE, the default state for VALIDATE or NOVALIDATE is NOVALIDATE.

    Constraint state combinations

    • ENABLE VALIDATE: OceanBase Database checks the validity of new data and existing data in the table. If the existing data violates the constraint, you cannot modify the constraint states to ENABLE VALIDATE or create a constraint whose states are ENABLE VALIDATE.

    • ENABLE NOVALIDATE: OceanBase Database does not check the validity of existing data. It checks only the validity of new data in the table.

    • DISABLE VALIDATE: DML operations cannot be performed on the table.

    • DISABLE NOVALIDATE: The constraint is invalid. OceanBase Database does not check the validity of the existing data or new data in the table.

    Example of constraint options

    obclient> CREATE TABLE t1(c1 INT, c2 INT);
    Query OK, 0 rows affected
    
    obclient> INSERT INTO t1 VALUES(0, 1);
    Query OK, 1 row affected
    
    obclient> ALTER TABLE t1 ADD CONSTRAINT cst CHECK(c1 = c2) ENABLE VALIDATE;
    OBE-02293: cannot validate (TEST.CST) - check constraint violated
    
    obclient> ALTER TABLE t1 ADD CONSTRAINT cst CHECK(c1 = c2) DISABLE VALIDATE;
    OBE-02293: cannot validate (TEST.CST) - check constraint violated
    
    obclient> ALTER TABLE t1 ADD CONSTRAINT cst CHECK(c1 = c2) ENABLE NOVALIDATE;
    Query OK, 0 rows affected
    
    obclient> INSERT INTO t1 VALUES(0, 1);
    OBE-02290: check constraint violated
    
    obclient> INSERT INTO t1 VALUES(1, 1);
    Query OK, 1 row affected
    
    obclient> ALTER TABLE t1 MODIFY CONSTRAINT cst DISABLE NOVALIDATE;
    Query OK, 0 rows affected
    
    obclient> INSERT INTO t1 VALUES(0, 1);
    Query OK, 1 row affected
    
    obclient> DELETE FROM t1 WHERE c1 != c2;
    Query OK, 2 rows affected
    
    obclient> ALTER TABLE t1 MODIFY CONSTRAINT cst DISABLE VALIDATE;
    Query OK, 0 rows affected
    
    obclient> INSERT INTO t1 VALUES(1, 1);
    OBE-25128: No insert/update/delete on table with constraint (TEST.CST) disabled and validated
    
    obclient> ALTER TABLE t1 MODIFY CONSTRAINT cst ENABLE VALIDATE;
    Query OK, 0 rows affected
    
    obclient> INSERT INTO t1 VALUES(0, 1);
    OBE-02290: check constraint violated
    
    obclient> INSERT INTO t1 VALUES(1, 1);
    Query OK, 1 row affected
    

    Previous topic

    CHECK constraints
    Last

    Next topic

    Database objects overview
    Next
    What is on this page
    Timing of integrity constraint checking
    Constraint states