OceanBase logo

OceanBase

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

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

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

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.0.0Enterprise Edition

  • Overview
    • Overview
    • System architecture
    • Compatibility with MySQL
    • Compatibility with Oracle
      • Overview
      • SQL data types
      • Built-in functions
      • System views
    • Limits
  • Get Started
    • Quick start with OceanBase Database
    • Hands on for OceanBase SQL
      • Usage notes
      • Basic SQL operations (MySQL mode)
      • Basic SQL operations (Oracle mode)
    • Build applications (MySQL Mode)
      • Connect Python applications to OceanBase Database
      • Connect Java applications to OceanBase Database
      • Connect C applications to OceanBase Database
    • Build applications(Oracle Mode)
      • Connect Python applications to OceanBase Database
      • Connect Java applications to OceanBase Database
      • Connect C applications to OceanBase Database
    • Experience OceanBase advanced features
      • Experience scalable OLTP
        • Run the TPC-C benchmark in OceanBase Database
        • Experience the hot row update capability of OceanBase Database
      • Experience operational OLAP
      • Experience parallel import and data compression
      • Experience the multitenancy feature
  • Develop
    • Develop (MySQL Mode)
      • Connect to Oceanbase Database with client
        • Overview
        • Connect to an OceanBase Database tenant by using a MySQL client
        • Connect to an OceanBase Database tenant by using OBClient
        • Connect to OceanBase Database by using ODC
        • Java application
          • OceanBase Connector/J
          • Java APIs
          • Connect Java applications to OceanBase Database
        • C application
          • OceanBase Connector/C
          • C API functions
          • Connect C applications to OceanBase Database
        • Python applications
        • SpringBoot connection example
        • SpringBatch connection example
        • Example of Database connection pool configuration
          • Overview of database connection pool configuration
          • Example of configuring a Tomcat connection pool
          • Example of configuring a C3P0 connection pool
          • Example of configuring a Proxool connection pool
          • Example of configuring a HiKariCP connection pool
          • Example of configuring a DBCP connection pool
          • CommonPool configuration example
          • Example of configuring a JDBC connection pool
        • SpringJDBC connection example
        • SpringJPA connection example
        • Hibernate connection example
        • MyBatis connection example
      • Create and manage database objects
        • About DDL statements
        • View the currently connected database
        • Change the password of a user
        • Data type
          • General data types
          • Unsupported data types
        • Create and manage tables
          • About tables
          • Create a table
          • About auto-increment columns
          • About types of column constraints
          • About table structure modification
          • About table clearing
          • About table dropping
          • Flash back a dropped table
          • About table privileges
        • Create and manage partition tables
          • About partitioned tables
          • Create a partitioned table
          • Manage a partitioned table
          • Create a subpartitioned table
          • Manage a subpartitioned table
          • Partition routing
          • Indexes on partitioned tables
          • Suggestions on using partitioned tables
        • Create and manage indexes
          • About indexes
          • Create an index
          • Drop an index
        • Create and manage views
          • About views
          • Create a view
          • Modify a view
          • Delete a view
        • Create and manage sequences
          • About sequences
          • Create a sequence
          • Modify a sequence
          • Delete a sequence
        • Create and manage triggers
          • About triggers
          • Create a trigger
          • Delete a trigger
      • Query
        • About queries
        • Single-table queries
        • Conditional queries
        • ORDER BY queries
        • GROUP BY queries
        • Use the LIMIT clause in queries
        • Query data from multiple tables
          • About multi-table join queries
          • INNER JOIN queries
          • OUTER JOIN queries
          • Subqueries
        • Use operators and functions in a query
          • Use arithmetic operators in queries
          • Use numerical functions in queries
          • Use string connectors in queries
          • Use string functions in queries
          • Use datetime functions in queries
          • Use type conversion functions in queries
          • Use aggregate functions in queries
          • Use NULL-related functions in queries
          • Use the CASE conditional operator in queries
          • Use the SELECT FOR UPDATE statement to lock query results
        • Execution plan
          • View an execution plan
          • Understand an execution plan
        • Use SQL hints in queries
        • Variables of query timeout
      • DML statements and transactions
        • DML statement
          • About DML statements
          • About the INSERT statement
          • UPDATE statements
          • About the DELETE statement
          • About the REPLACE INTO statement
        • Transactions
          • About transaction control statements
          • Start a transaction
          • Transaction savepoints
          • Commit a transaction
          • Roll back a transaction
          • About transaction timeout
      • Common errors and solutions
        • About error codes
        • Database connection error
        • About timeout
          • Idle session timeout
          • Transaction timeout errors
        • About user
          • Locked user
          • Incorrect user password
        • About table
          • Table already exists
          • Table does not exist
          • Invalid use of NULL value
        • About constraint
          • Unique key conflict
          • Foreign key conflict
        • About SQL commands
          • Data truncation
    • Develop (Oracle Mode)
      • Connect to Oceanbase Database with client
        • Overview
        • Connect to an OceanBase tenant by using an Oracle client
        • Connect to an OceanBase Database tenant by using OBClient
        • Connect to OceanBase Database by using ODC
      • Application development
        • Java application
          • OceanBase Connector/J
          • Java APIs
          • Connect Java applications to OceanBase Database
        • Python application
          • OceanBase Connector/J in Python
          • Python APIs
          • Connect Python applications to OceanBase Database
        • 300.c-application-1
          • OceanBase Connector/C
          • OBCI APIs
          • Connect C applications to OceanBase Database
      • Create and manage database objects
        • About DDL statements
        • View the currently connected database
        • Change the password of a user
        • Data type
          • General data types
          • Unsupported data types
        • Create and manage tables
          • About tables
          • Create a table
          • Define an auto-increment column by using the SEQUENCE keyword
          • About types of column constraints
          • About table structure modification
          • About table clearing
          • About table dropping
          • Flash back a dropped table
          • About table privileges
        • Create and manage partition tables
          • About partitioned tables
          • Create a partitioned table
          • Manage a partitioned table
          • Create a subpartitioned table
          • Manage a subpartitioned table
          • Partition routing
          • Indexes on partitioned tables
          • Suggestions on using partitioned tables
        • Create and manage indexes
          • Drop an index
          • About indexes
          • Create an index
        • Create and manage views
          • About views
          • Create a view
          • Modify a view
          • Drop a view
        • Create and manage sequences
          • About sequences
          • Create a sequence
          • Modify a sequence
          • Delete a sequence
        • Create and manage synonyms
          • About synonyms
          • Create a synonym
          • Delete a synonym
        • Create and manage triggers
          • About triggers
          • Create a trigger
          • Modify a trigger
          • Delete a trigger
          • Enable or disable a trigger
      • Query
        • About queries
        • Single-table queries
        • Conditional queries
        • ORDER BY queries
        • GROUP BY queries
        • Use Row_Limiting_Clause in queries
        • Query data from multiple tables
          • About multi-table join queries
          • INNER JOIN queries
          • OUTER JOIN queries
          • Subqueries
        • Use operators and functions in a query
          • Use arithmetic operators in queries
          • Use numerical functions in queries
          • Use string concatenation operators in queries
          • Use string functions in queries
          • Use datetime functions in queries
          • Use type conversion functions in queries
          • Use aggregate functions in queries
          • Use NULL-related functions in queries
          • Use CASE functions in queries
          • Use the SELECT FOR UPDATE statement to lock query results
        • Execution plan
          • View execution plans of queries
          • Understand an execution plan
        • Use SQL hints in queries
        • About query timeout variables
      • DML statements and transactions
        • DML statement
          • About DML statements
          • About the INSERT statement
          • UPDATE statements
          • About the MERGE statement
          • About the DELETE statement
        • Transactions
          • About transaction control statements
          • Start a transaction
          • Transaction savepoints
          • Commit a transaction
          • Roll back a transaction
          • About transaction timeout
      • Common errors and solutions
        • Overview
        • Database connection error
        • About timeout
          • Idle session timeout
          • Transaction timeout errors
        • About user
          • Locked user
          • Incorrect user password
        • About object
          • Object already exists
          • Object does not exist
        • About constraint
          • Unique key conflict
          • Foreign key conflict
        • About sql
          • Data type inconsistency
  • Deploy
    • Overview
    • Deployment procedure
    • Preparations before deployment
      • Prepare servers
      • Configure servers
      • Prepare installation packages
    • HA solution for OceanBase clusters
    • Deploy through the CLI
      • Configure the deployment environment
        • Install oat-cli
        • Use oat-cli to configure the deployment environment
        • Configure the clock source
        • Upload installation packages
        • Configure host information
        • Check the environment before deployment
      • Deploy OCP
        • Deployment description
        • Generate a configuration file
        • Start deployment
        • Check after deployment
      • Deploy the OceanBase cluster
        • Deploy a single-replica OceanBase cluster
        • Deploy a three-replica OceanBase cluster
      • Deploy OBProxy
        • Deploy OBProxy through the CLI
      • Create an OceanBase tenant
        • View resources available for a business tenant
        • Create a tenant
        • Verify after deployment
      • Deploy OMS
        • Deployment description
        • Modify the configuration file
        • Start deployment
        • Check after deployment
      • Deploy ODC
        • Deployment description
        • Modify the configuration file
        • Start deployment
        • Check after deployment
  • Migrate
    • Overview
    • Migrate data by using SQL scripts
    • Migrate data by using MySQLDump
    • Migrate data by using DataX
    • Migrate data by using OUTFILE statements

Download PDF

Overview System architecture Compatibility with MySQL Overview SQL data types Built-in functions System views Limits Quick start with OceanBase Database Usage notes Basic SQL operations (MySQL mode) Basic SQL operations (Oracle mode) Connect Python applications to OceanBase Database Connect Java applications to OceanBase Database Connect C applications to OceanBase Database Connect Python applications to OceanBase Database Connect Java applications to OceanBase Database Connect C applications to OceanBase Database Experience operational OLAP Experience parallel import and data compression Experience the multitenancy feature Overview Deployment procedure Prepare servers Configure servers Prepare installation packages HA solution for OceanBase clustersOverview Migrate data by using SQL scripts Migrate data by using MySQLDumpMigrate data by using DataX Migrate data by using OUTFILE statements
OceanBase logo

The Unified Distributed Database for the AI Era.

Follow Us
Products
OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
Resources
DocsBlogLive DemosTraining & Certification
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.0.0
iconOceanBase Database
SQL - V 4.0.0Enterprise Edition
SQL
KV
  • 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

Basic SQL operations (Oracle mode)

Last Updated:2023-07-24 09:52:12  Updated
share
What is on this page
Table-level operations
Create a table
Modify a table
Drop a table
Index operations
Create an index
Query indexes
Drop an index
Insert data
Delete data
Update data
Querying data
Commit a transaction
Roll back a transaction

folded

share

This topic describes the basic SQL operations in OceanBase Database in Oracle mode.

Table-level operations

This topic describes how to create, view, modify, and drop database tables.

Create a table

Use the CREATE TABLE statement to create a table in the database.

Example: Create a table named test.

obclient> CREATE TABLE test (c1 INT PRIMARY KEY, c2 VARCHAR(3));
Query OK, 0 rows affected

Modify a table

Use the ALTER TABLE statement to modify the structure of an existing table, including modifying the table name and table attributes, adding columns, modifying columns and attributes, and dropping columns.

Example 1: Modify the type of the c2 field in the test table.

obclient> DESCRIBE test;
+-------+-------------+------+-----+---------+-------+
| FIELD | TYPE        | NULL | KEY | DEFAULT | EXTRA |
+-------+-------------+------+-----+---------+-------+
| C1    | NUMBER(38)  | NO   | PRI | NULL    | NULL  |
| C2    | VARCHAR2(3) | YES  | NULL| NULL    | NULL  |
+-------+-------------+------+-----+---------+-------+
2 rows in set

obclient> ALTER TABLE test MODIFY c2 CHAR(10);
Query OK, 0 rows affected

obclient> DESCRIBE test;
+-------+------------+------+-----+---------+-------+
| FIELD | TYPE       | NULL | KEY | DEFAULT | EXTRA |
+-------+------------+------+-----+---------+-------+
| C1    | NUMBER(38) | NO   | PRI | NULL    | NULL  |
| C2    | CHAR(10)   | YES  | NULL| NULL    | NULL  |
+-------+------------+------+-----+---------+-------+
2 rows in set

Example 2: Add a column to and drop a column from the test table.

obclient> ALTER TABLE test ADD c3 int;
Query OK, 0 rows affected

obclient> DESCRIBE test;
+-------+------------+------+-----+---------+-------+
| FIELD | TYPE       | NULL | KEY | DEFAULT | EXTRA |
+-------+------------+------+-----+---------+-------+
| C1    | NUMBER(38) | NO   | PRI | NULL    | NULL  |
| C2    | CHAR(10)   | YES  | NULL | NULL    | NULL  |
| C3    | NUMBER(38) | YES  | NULL | NULL    | NULL  |
+-------+------------+------+-----+---------+-------+
3 rows in set

obclient> ALTER TABLE test DROP COLUMN c3;
Query OK, 0 rows affected

obclient> DESCRIBE test;
+-------+------------+------+-----+---------+-------+
| FIELD | TYPE       | NULL | KEY | DEFAULT | EXTRA |
+-------+------------+------+-----+---------+-------+
| C1    | NUMBER(38) | NO   | PRI | NULL    | NULL  |
| C2    | CHAR(10)   | YES  | NULL | NULL    | NULL  |
+-------+------------+------+-----+---------+-------+
2 rows in set

Drop a table

Use the DROP TABLE statement to drop a table.

Example: Drop the test table.

obclient> DROP TABLE test;
Query OK, 0 rows affected

Index operations

An index is a database structure created for a table to sort data in one or more columns of the table in a specific order. It improves the query speed and reduces the performance overhead of database systems.

Create an index

Use the CREATE INDEX statement to create a table index.

Example: Create an index on the test table.

obclient> DESCRIBE test;
+-------+------------+------+-----+---------+-------+
| FIELD | TYPE       | NULL | KEY | DEFAULT | EXTRA |
+-------+------------+------+-----+---------+-------+
| C1    | NUMBER(38) | NO   | PRI | NULL    | NULL  |
| C2    | CHAR(10)   | YES  | NULL | NULL    | NULL  |
+-------+------------+------+-----+---------+-------+
2 rows in set

obclient> CREATE INDEX test_index ON test (c1, c2);
Query OK, 0 rows affected

Query indexes

Query all indexes on a table by using the ALL_INDEXES view. Sample code:

obclient> SELECT * FROM ALL_INDEXES WHERE table_name='TEST'\G
*************************** 1. row ***************************
                  OWNER: SYS
             INDEX_NAME: TEST_OBPK_1664353339491130
             INDEX_TYPE: NORMAL
            TABLE_OWNER: SYS
             TABLE_NAME: TEST
             TABLE_TYPE: TABLE
             UNIQUENESS: UNIQUE
            COMPRESSION: ENABLED
          PREFIX_LENGTH: NULL
        TABLESPACE_NAME: NULL
              INI_TRANS: NULL
              MAX_TRANS: NULL
         INITIAL_EXTENT: NULL
            NEXT_EXTENT: NULL
            MIN_EXTENTS: NULL
            MAX_EXTENTS: NULL
           PCT_INCREASE: NULL
          PCT_THRESHOLD: NULL
         INCLUDE_COLUMN: NULL
              FREELISTS: NULL
        FREELIST_GROUPS: NULL
               PCT_FREE: NULL
                LOGGING: NULL
                 BLEVEL: NULL
            LEAF_BLOCKS: NULL
          DISTINCT_KEYS: NULL
AVG_LEAF_BLOCKS_PER_KEY: NULL
AVG_DATA_BLOCKS_PER_KEY: NULL
      CLUSTERING_FACTOR: NULL
                 STATUS: VALID
               NUM_ROWS: NULL
            SAMPLE_SIZE: NULL
          LAST_ANALYZED: NULL
                 DEGREE: 1
              INSTANCES: NULL
            PARTITIONED: NO
              TEMPORARY: NULL
              GENERATED: NULL
              SECONDARY: NULL
            BUFFER_POOL: NULL
            FLASH_CACHE: NULL
       CELL_FLASH_CACHE: NULL
             USER_STATS: NULL
               DURATION: NULL
      PCT_DIRECT_ACCESS: NULL
             ITYP_OWNER: NULL
              ITYP_NAME: NULL
             PARAMETERS: NULL
           GLOBAL_STATS: NULL
          DOMIDX_STATUS: NULL
        DOMIDX_OPSTATUS: NULL
         FUNCIDX_STATUS: NULL
             JOIN_INDEX: NO
IOT_REDUNDANT_PKEY_ELIM: NULL
                DROPPED: NO
             VISIBILITY: VISIBLE
      DOMIDX_MANAGEMENT: NULL
        SEGMENT_CREATED: NULL
       ORPHANED_ENTRIES: NULL
               INDEXING: NULL
                   AUTO: NULL
*************************** 2. row ***************************
                  OWNER: SYS
             INDEX_NAME: TEST_INDEX
             INDEX_TYPE: NORMAL
            TABLE_OWNER: SYS
             TABLE_NAME: TEST
             TABLE_TYPE: TABLE
             UNIQUENESS: NONUNIQUE
            COMPRESSION: ENABLED
          PREFIX_LENGTH: NULL
        TABLESPACE_NAME: NULL
              INI_TRANS: NULL
              MAX_TRANS: NULL
         INITIAL_EXTENT: NULL
            NEXT_EXTENT: NULL
            MIN_EXTENTS: NULL
            MAX_EXTENTS: NULL
           PCT_INCREASE: NULL
          PCT_THRESHOLD: NULL
         INCLUDE_COLUMN: NULL
              FREELISTS: NULL
        FREELIST_GROUPS: NULL
               PCT_FREE: NULL
                LOGGING: NULL
                 BLEVEL: NULL
            LEAF_BLOCKS: NULL
          DISTINCT_KEYS: NULL
AVG_LEAF_BLOCKS_PER_KEY: NULL
AVG_DATA_BLOCKS_PER_KEY: NULL
      CLUSTERING_FACTOR: NULL
                 STATUS: VALID
               NUM_ROWS: NULL
            SAMPLE_SIZE: NULL
          LAST_ANALYZED: NULL
                 DEGREE: 1
              INSTANCES: NULL
            PARTITIONED: NO
              TEMPORARY: NULL
              GENERATED: NULL
              SECONDARY: NULL
            BUFFER_POOL: NULL
            FLASH_CACHE: NULL
       CELL_FLASH_CACHE: NULL
             USER_STATS: NULL
               DURATION: NULL
      PCT_DIRECT_ACCESS: NULL
             ITYP_OWNER: NULL
              ITYP_NAME: NULL
             PARAMETERS: NULL
           GLOBAL_STATS: NULL
          DOMIDX_STATUS: NULL
        DOMIDX_OPSTATUS: NULL
         FUNCIDX_STATUS: NULL
             JOIN_INDEX: NO
IOT_REDUNDANT_PKEY_ELIM: NULL
                DROPPED: NO
             VISIBILITY: VISIBLE
      DOMIDX_MANAGEMENT: NULL
        SEGMENT_CREATED: NULL
       ORPHANED_ENTRIES: NULL
               INDEXING: NULL
                   AUTO: NULL
2 rows in set (0.02 sec)

Use the USER_IND_COLUMNS statement to view details about the indexes of a table. Sample code:

obclient> SELECT * FROM USER_IND_COLUMNS WHERE table_name='TEST'\G
*************************** 1. row ***************************
        INDEX_NAME: TEST_OBPK_1664353339491130
        TABLE_NAME: TEST
       COLUMN_NAME: C1
   COLUMN_POSITION: 1
     COLUMN_LENGTH: 22
       CHAR_LENGTH: 0
           DESCEND: ASC
COLLATED_COLUMN_ID: NULL
*************************** 2. row ***************************
        INDEX_NAME: TEST_INDEX
        TABLE_NAME: TEST
       COLUMN_NAME: C1
   COLUMN_POSITION: 1
     COLUMN_LENGTH: 22
       CHAR_LENGTH: 0
           DESCEND: ASC
COLLATED_COLUMN_ID: NULL
*************************** 3. row ***************************
        INDEX_NAME: TEST_INDEX
        TABLE_NAME: TEST
       COLUMN_NAME: C2
   COLUMN_POSITION: 2
     COLUMN_LENGTH: 10
       CHAR_LENGTH: 10
           DESCEND: ASC
COLLATED_COLUMN_ID: NULL
3 rows in set

Drop an index

Use the DROP INDEX statement to drop a table index.

Example: Drop the index named test_index.

obclient> DROP INDEX test_index;
Query OK, 0 rows affected

Insert data

Use the INSERT statement to add one or more records to a table.

Example 1: Use the CREATE TABLE statement to create a table named t1 and insert a row into the t1 table.

obclient> CREATE TABLE t1(c1 INT PRIMARY KEY, c2 INT);
Query OK, 0 rows affected

obclient> SELECT * FROM t1;
Empty set

obclient> INSERT INTO t1 VALUES(1,1);
Query OK, 1 row affected

obclient> SELECT * FROM t1;
+----+------+
| c1 | c2   |
+----+------+
|  1 |    1 |
+----+------+
1 row in set

Example 2: Insert data into a subquery.

obclient> INSERT INTO (SELECT * FROM t1) VALUES(2,2);
Query OK, 1 row affected

obclient> SELECT * FROM t1;
+----+------+
| C1 | C2   |
+----+------+
|  1 |    1 |
|  2 |    2 |
+----+------+
2 rows in set

Example 3: Insert data by using the RETURNING clause.

obclient> INSERT INTO t1 VALUES(3,3) RETURNING c1;
+----+
| C1 |
+----+
|  3 |
+----+
1 row in set

obclient> SELECT * FROM t1;
+----+------+
| C1 | C2   |
+----+------+
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
+----+------+
3 rows in set

Delete data

Use the DELETE statement to delete data.

Example: Delete the row where c1 = 2 from the t1 table.

obclient> DELETE FROM t1 WHERE c1 = 2;
Query OK, 1 row affected

obclient> SELECT * FROM t1;
+----+------+
| C1 | C2   |
+----+------+
|  1 |    1 |
|  3 |    3 |
+----+------+
2 rows in set

Update data

Use the UPDATE statement to modify the field values in a table.

Example 1: For the row where t1.c1 = 1 in the t1 table, set its value in the c2 column to 100.

obclient> UPDATE t1 SET t1.c2 = 100 WHERE t1.c1 = 1;
Query OK, 1 row affected
Rows matched: 1  Changed: 1  Warnings: 0

obclient> SELECT * FROM t1;
+----+------+
| C1 | C2   |
+----+------+
|  1 |  100 |
|  3 |    3 |
+----+------+
2 rows in set

Example 2: For the row where v.c1 = 1, run a subquery and set its value in the c2 column to 300.

obclient> UPDATE (SELECT * FROM t1) v SET v.c2 = 300 WHERE v.c1 = 3;
Query OK, 1 row affected
Rows matched: 1  Changed: 1  Warnings: 0

obclient> SELECT * FROM t1;
+----+------+
| C1 | C2   |
+----+------+
|  1 |  100 |
|  3 |  300 |
+----+------+
2 rows in set

Querying data

Use the SELECT statement to query data from a table.

Example 1: Use the CREATE TABLE statement to create a table named t2. Query the data in the name field from the t2 table.

obclient> CREATE TABLE t2 (id INT, name VARCHAR(50), num INT);
Query OK, 0 rows affected

obclient> INSERT INTO t2 VALUES(1,'a',100),(2,'b',200),(3,'a',50);
Query OK, 3 rows affected
Records: 3  Duplicates: 0  Warnings: 0

obclient> SELECT * FROM t2;
+------+------+------+
| ID   | NAME | NUM  |
+------+------+------+
|    1 | a    |  100 |
|    2 | b    |  200 |
|    3 | a    |   50 |
+------+------+------+
3 rows in set

obclient> SELECT name FROM t2;
+------+
| NAME |
+------+
| a    |
| b    |
| a    |
+------+
3 rows in set

Example 2: Deduplicate the query results of the name field.

obclient> SELECT DISTINCT name FROM t2;
+------+
| NAME |
+------+
| a    |
| b    |
+------+
2 rows in set

Example 3: Return the values of the corresponding id, name, and num fields based on the filter condition name = 'a' from the t2 table.

obclient> SELECT id, name, num FROM t2 WHERE name = 'a';
+------+------+------+
| ID   | NAME | NUM  |
+------+------+------+
|    1 | a    |  100 |
|    3 | a    |   50 |
+------+------+------+
2 rows in set

Commit a transaction

Use the COMMIT statement to commit a transaction.

Before you commit the transaction, your changes are not persisted and take effect only for the current session. You can use the ROLLBACK statement to revoke the changes.

After you commit the transaction, your changes take effect for all database sessions. After your changes are persisted, you cannot roll them back with a ROLLBACK statement.

Example: Use the CREATE TABLE statement to create a table named t_insert. Use the COMMIT statement to commit a transaction.

obclient> CREATE TABLE t_insert(
     id number NOT NULL PRIMARY KEY,
     name varchar(10) NOT NULL,
     value number NOT NULL,
     gmt_create date NOT NULL DEFAULT sysdate
 );
Query OK, 0 rows affected

obclient> INSERT INTO t_insert(id, name, value, gmt_create) VALUES(1,'CN',10001, sysdate),(2,'US',10002, sysdate),(3,'EN',10003, sysdate);
Query OK, 3 rows affected
Records: 3  Duplicates: 0  Warnings: 0

obclient> SELECT * FROM t_insert;
+----+------+-------+------------+
| ID | NAME | VALUE | GMT_CREATE |
+----+------+-------+------------+
|  1 | CN   | 10001 | 22-AUG-22  |
|  2 | US   | 10002 | 22-AUG-22  |
|  3 | EN   | 10003 | 22-AUG-22  |
+----+------+-------+------------+
3 rows in set

obclient> INSERT INTO t_insert(id, name, value) VALUES(4,'JP',10004);
Query OK, 1 row affected

obclient> COMMIT;
Query OK, 0 rows affected

obclient> SELECT * FROM t_insert;
+----+------+-------+------------+
| ID | NAME | VALUE | GMT_CREATE |
+----+------+-------+------------+
|  1 | CN   | 10001 | 22-AUG-22  |
|  2 | US   | 10002 | 22-AUG-22  |
|  3 | EN   | 10003 | 22-AUG-22  |
|  4 | JP   | 10004 | 22-AUG-22  |
+----+------+-------+------------+
4 rows in set

Roll back a transaction

Use the ROLLBACK statement to roll back a transaction.

A transaction rollback reverses all changes made in the transaction. You can roll back an entire uncommitted transaction or roll back a transaction to any savepoints of the transaction. To roll back to a specific savepoint, you must use both the ROLLBACK and TO SAVEPOINT statements.

Notes:

  • If you roll back an entire transaction:

    • The transaction will end.
    • All modifications made from the start of the transaction will be discarded.
    • All savepoints will be cleared.
    • All locks held by the transaction will be released.
  • If you roll back a transaction to a specific savepoint:

    • The transaction will not end.
    • Modifications made before the savepoint will be retained but those made after it will be discarded.
    • All savepoints after the specific savepoint will be cleared.
    • All locks held by the transaction after the specific savepoint will be released.

Example: Roll back all the changes of a transaction.

obclient> SELECT * FROM t_insert;
+----+------+-------+------------+
| ID | NAME | VALUE | GMT_CREATE |
+----+------+-------+------------+
|  1 | CN   | 10001 | 29-SEP-22  |
|  2 | US   | 10002 | 29-SEP-22  |
|  3 | EN   | 10003 | 29-SEP-22  |
|  4 | JP   | 10004 | 29-SEP-22  |
+----+------+-------+------------+
4 rows in set

obclient> INSERT INTO t_insert(id, name, value) VALUES(5,'FR',10005),(6,'RU',10006);
Query OK, 3 rows affected
Records: 3  Duplicates: 0  Warnings: 0

obclient> SELECT * FROM t_insert;
+----+------+-------+------------+
| ID | NAME | VALUE | GMT_CREATE |
+----+------+-------+------------+
|  1 | CN   | 10001 | 22-AUG-22  |
|  2 | US   | 10002 | 22-AUG-22  |
|  3 | EN   | 10003 | 22-AUG-22  |
|  4 | JP   | 10004 | 22-AUG-22  |
|  5 | FR   | 10005 | 22-AUG-22  |
|  6 | RU   | 10006 | 22-AUG-22  |
+----+------+-------+------------+
6 rows in set

obclient> ROLLBACK;
Query OK, 0 rows affected

obclient> SELECT * FROM t_insert;
+----+------+-------+------------+
| ID | NAME | VALUE | GMT_CREATE |
+----+------+-------+------------+
|  1 | CN   | 10001 | 29-SEP-22  |
|  2 | US   | 10002 | 29-SEP-22  |
|  3 | EN   | 10003 | 29-SEP-22  |
|  4 | JP   | 10004 | 29-SEP-22  |
+----+------+-------+------------+
3 rows in set

Previous topic

Basic SQL operations (MySQL mode)
Last

Next topic

Connect Python applications to OceanBase Database
Next
What is on this page
Table-level operations
Create a table
Modify a table
Drop a table
Index operations
Create an index
Query indexes
Drop an index
Insert data
Delete data
Update data
Querying data
Commit a transaction
Roll back a transaction