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.1.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.1.0
    iconOceanBase Database
    SQL - V 4.1.0
    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

    Differences between replace and insert_or_update

    Last Updated:2023-07-28 02:55:43  Updated
    share
    What is on this page
    insert_or_update
    replace

    folded

    share

    replace and insert_or_update are two operations that are confusing due to their similar performance in many cases. However, they are semantically different. This topic compares the differences between these two operations in detail.

    If a conflict occurs when you perform the replace operation, all conflicting rows are deleted before new rows are inserted.

    If a conflict occurs when you perform the insert_or_update operation, the conflicting rows are updated.

    The following examples show the differences between the two operations.

    insert_or_update

    The insert_or_update operation of TableAPI is equivalent to a special SQL syntax. The following example shows how insert_or_update works:

    OceanBase (root@test)> desc test_replace;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | c1    | int(11) | NO   | PRI | NULL    |       |
    | c2    | int(11) | YES  |     | NULL    |       |
    | c3    | int(11) | YES  | UNI | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    3 rows in set (0.03 sec)
    
    OceanBase (root@test)> select * from test_replace;
    +----+------+------+
    | c1 | c2   | c3   |
    +----+------+------+
    |  1 |    2 |    3 |
    |  4 |    5 |    6 |
    +----+------+------+
    2 rows in set (0.03 sec)
    
    OceanBase (root@test)> insert into test_replace (c1, c2, c3) values (4, 7, 8), (5, 8, 9) on duplicate key update c1=values(c1), c2=values(c2), c3=values(c3);
    Query OK, 3 rows affected (0.03 sec)
    Records: 2  Duplicates: 1  Warnings: 0
    
    OceanBase (root@test)> select * from test_replace;
    +----+------+------+
    | c1 | c2   | c3   |
    +----+------+------+
    |  1 |    2 |    3 |
    |  4 |    7 |    8 |
    |  5 |    8 |    9 |
    +----+------+------+
    3 rows in set (0.03 sec)
    

    replace

    OceanBase (root@test)> select * from test_replace;
    +----+------+------+
    | c1 | c2   | c3   |
    +----+------+------+
    |  1 |    2 |    3 |
    |  4 |    7 |    8 |
    |  5 |    8 |    9 |
    +----+------+------+
    3 rows in set (0.03 sec)
    
    OceanBase (root@test)> replace into test_replace (c1, c3) values (4, 9);
    Query OK, 3 rows affected (0.04 sec)
    
    OceanBase (root@test)> select * from test_replace;
    +----+------+------+
    | c1 | c2   | c3   |
    +----+------+------+
    |  1 |    2 |    3 |
    |  4 | NULL |    9 |
    +----+------+------+
    2 rows in set (0.02 sec)
    

    Based on the preceding examples, unlike insert_or_update, the replace operation deletes rows and changes the value of c2 in the row where c1 is 4 to the default value NULL because of the insertion of a new value. In most cases, you need to use the insert_or_update operation, which is similar to the put method in NoSQL mode.

    The two operations achieve the same results in the following two circumstances:

    • The table only has the primary key but no unique indexes.
    • The operation specifies the values of all columns in a row and the system does not need to insert the default value for columns where values are missing.

    Although the results are the same in the preceding two circumstances, TableAPI optimizes the implementation of insert_or_update to provide better performance than that of replace.

    Previous topic

    Implementation of TableAPI
    Last

    Next topic

    TableAPI data models
    Next
    What is on this page
    insert_or_update
    replace