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

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

    Migrate data between tables

    Last Updated:2026-04-28 09:23:26  Updated
    share
    What is on this page
    Migrate data between tables
    Statement 1
    Statement 2
    Example 1
    Example 2
    References

    folded

    share

    This topic describes how to use SQL statements to migrate data between tables.

    Migrate data between tables

    Statement 1

    INSERT INTO target_table_name[(target_col_name[, target_col_name] ...)]
    SELECT [(source_col_name[, source_col_name] ...)]
    FROM source_table_name
    [WHERE expr];
    

    The parameters are described as follows:

    Parameter
    Description
    target_table_name The destination table to which data is migrated.
    target_col_name The name of a column in the destination table. To update data for all columns in the destination table, you can omit the column names.
    source_col_name The name of a column in the source table. Select the columns to be migrated. To select all data, use an asterisk (*).

    Notice

    The number of columns selected must be the same as the number of columns in the destination table.

    source_table_name The source table from which data is migrated.
    WHERE expr The filtering condition for data migration. If you do not specify this parameter, all row records specified in SELECT are migrated.

    Statement 2

    Applicability

    Currently, the MERGE statement is supported only in the Oracle mode of OceanBase Database.

    MERGE INTO target_table_name 
    USING source_table_name
    ON (expr)
    WHEN NOT MATCHED THEN INSERT VALUES((source_col_name[, source_col_name] ...))
    [WHERE expr];
    

    The parameters are described as follows:

    Parameter
    Description
    target_table_name The destination table to which data is migrated.
    source_table_name The source table from which data is migrated.
    ON (expr) The condition for joining the source and target tables.
    source_col_name The name of a column in the source table. Select the columns to be migrated. To select all data, use an asterisk (*).

    Notice

    The columns selected must be of the same number and the same data types as those in the destination table.

    WHERE expr The filtering condition for data migration.

    Example 1

    Insert data that meets the condition age > 10 in the tbl1 table into the tbl2 table.

    obclient [test]> SELECT * FROM tbl1;
    +------+------+------+
    | id   | name | age  |
    +------+------+------+
    |    1 | ab   |    8 |
    |    2 | bc   |   18 |
    |    3 | cd   |   14 |
    |    4 | de   |   19 |
    |    5 | ef   |    6 |
    |    6 | fg   |   15 |
    +------+------+------+
    6 rows in set
    
    obclient [test]> DESC tbl2;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | col1  | int(11) | YES  |     | NULL    |       |
    | col2  | int(11) | YES  |     | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    2 rows in set
    
    obclient [test]> SELECT * FROM tbl2;
    Empty set
    
    obclient [test]> INSERT INTO tbl2 SELECT id,age FROM tbl1 WHERE age > 10;
    Query OK, 4 rows affected
     Records: 4  Duplicates: 0  Warnings: 0
    
    obclient [test]> SELECT * FROM tbl2;
    +------+------+
    | col1 | col2 |
    +------+------+
    |    2 |   18 |
    |    3 |   14 |
    |    4 |   19 |
    |    6 |   15 |
    +------+------+
    4 rows in set
    

    Example 2

    If a row in the tbl1 table is not equal to any row in the tbl2 table, the row in the tbl1 table is inserted into the tbl2 table when this row meets the tbl1.age < 10 condition. Rows that meet the tbl1.id=tbl2.col1 and tbl1.age=tbl2.col2 condition are not inserted into the tbl2 table.

    obclient [SYS]> SELECT * FROM tbl1;
    +------+------+------+
    | ID   | NAME | AGE  |
    +------+------+------+
    |    1 | ab   |    8 |
    |    2 | bc   |   18 |
    |    3 | cd   |   14 |
    |    4 | de   |   19 |
    |    5 | ef   |    6 |
    |    6 | fg   |   15 |
    +------+------+------+
    6 rows in set
    
    obclient [SYS]> SELECT * FROM tbl2;
    Empty set
    
    obclient [SYS]> MERGE INTO tbl2
        USING tbl1
        ON (tbl1.id=tbl2.col1 and tbl1.age=tbl2.col2)
        WHEN NOT MATCHED THEN INSERT VALUES(tbl1.id,tbl1.age)
        WHERE tbl1.age < 10;
    Query OK, 2 rows affected
    
    obclient [SYS]> SELECT * FROM tbl2;
    +------+------+
    | COL1 | COL2 |
    +------+------+
    |    1 |    8 |
    |    5 |    6 |
    +------+------+
    2 rows in set
    

    References

    • INSERT (MySQL mode)
    • INSERT (Oracle mode)
    • MERGE (Oracle mode)

    Previous topic

    Use OBLOADER & OBDUMPER to migrate data from an Oracle tenant to a MySQL tenant in OceanBase Database
    Last

    Next topic

    Migrate resource units
    Next
    What is on this page
    Migrate data between tables
    Statement 1
    Statement 2
    Example 1
    Example 2
    References