Meet OceanBase AI Database, the unified database for operational data, real-time analytics, and AI. Explore ->

Meet OceanBase AI Database, the unified database for operational data, real-time analytics, and AI. Explore ->

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

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogWhite PaperLive 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.3.5
    iconOceanBase Database
    SQL - V 4.3.5
    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

    LOCK TABLE

    Last Updated:2026-04-09 02:53:55  Updated
    Share
    What is on this page
    Purpose
    Syntax
    Syntax
    Examples

    folded

    Share

    Purpose

    This statement locks one or more tables, partitions, or subpartitions in the specified schema.

    Locked tables remain locked until the transaction is committed, rolled back, or rolled back to a savepoint before the lock was applied.

    Note

    The table to be locked must be in the user's own schema, or the user must have the LOCK ANY TABLE system privilege.

    Syntax

    LOCK TABLE [ schema. ]table_name
    
      [ partition_extension_clause]
        [, [ schema. ] table_name
          [ partition_extension_clause ]
         ]...
      IN lock_mode MODE
      [ NOWAIT
       | WAIT integer
      ] ;
    
    partition_extension_clause:
    
        PARTITION (partition_name, ...)
      | SUBPARTITION (subpartition_name, ...)
    
    

    Syntax

    Field
    Description
    table_name Specifies the name of the table to be locked.
    partition_name Specifies the name of the partition to be locked.
    subpartition_name Specifies the name of the subpartition to be locked.
    lock_mode Specifies the lock mode. The lock modes supported by OceanBase Database are as follows:
    • ROW SHARE: allows concurrent access to the locked table, but prevents users from exclusively locking the entire table.
    • ROW EXCLUSIVE : same as ROW SHARE, but also prevents tables from being locked in SHARE mode. ROW EXCLUSIVE locks are automatically obtained during updates, inserts, or deletes.
    • SHARE : allows concurrent queries but prevents updates to the locked table.
    • SHARE ROW EXCLUSIVE : allows viewing of the entire table and viewing of rows by other users, but prevents other users from locking the table or updating rows in SHARE mode.
    • EXCLUSIVE : allows only queries on the locked table and prevents other operations.
    NOWAIT If a lock conflict occurs when locking the table, partition, or subpartition, specifying NOWAIT allows the database to immediately return control to the user. In this case, the database returns a message indicating that the table, partition, or subpartition is locked by another user.
    WAIT If a lock conflict occurs, the statement should wait for the conflicting lock to be released, up to the timeout specified by the user's interger value (in seconds). The value of integer is not limited.

    Examples

    The following example creates a subpartitioned table and applies a lock to its partitions.

    1. Create a subpartitioned table named test. The subpartition names generated by SUBPARTITION TEMPLATE are formed by appending 's' and the subpartition template name to the partition name. For example, the p0 partition has subpartitions named p0ssp0, p0ssp1, and p0ssp2.

      CREATE TABLE TEST(col1 INT, col2 INT)
      PARTITION BY RANGE (col1)
      SUBPARTITION BY RANGE (col2)
        SUBPARTITION TEMPLATE
          (
          SUBPARTITION sp0 VALUES LESS THAN (3),
          SUBPARTITION sp1 VALUES LESS THAN (6),
          SUBPARTITION sp2 VALUES LESS THAN (9)
          )
      (
        PARTITION p0 VALUES LESS THAN (100),
        PARTITION p1 VALUES LESS THAN (200),
        PARTITION p2 VALUES LESS THAN (300)
      );
      
    2. Apply an exclusive lock to the entire test table.

      obclient> LOCK TABLE test IN EXCLUSIVE MODE;
      Query OK, 0 rows affected
      
    3. Apply an exclusive lock to the p1 partition of the test table.

      obclient> LOCK TABLE test PARTITION (p1) IN EXCLUSIVE MODE;
      Query OK, 0 rows affected
      
    4. Apply an exclusive lock to the p1ssp1 subpartition of the test table.

      obclient> LOCK TABLE test SUBPARTITION (p1ssp1) IN EXCLUSIVE MODE;
      Query OK, 0 rows affected
      
    5. Apply an exclusive lock to multiple partitions of the test table.

      obclient> LOCK TABLE test PARTITION (p1,p2) IN EXCLUSIVE MODE;
      Query OK, 0 rows affected
      
    6. Apply an exclusive lock to multiple subpartitions of the test table.

      obclient> LOCK TABLE test SUBPARTITION (p0ssp1,p1ssp2) IN EXCLUSIVE MODE;
      Query OK, 0 rows affected
      
    7. Apply an exclusive lock to multiple partitions and subpartitions of the test table.

      obclient> LOCK TABLE test PARTITION (p1, p2), test SUBPARTITION (p0ssp0, p0ssp1) IN EXCLUSIVE MODE;
      Query OK, 0 rows affected
      
    8. Attempt to apply an exclusive lock to a non-existent partition p3 of the test table. In this example, p3 does not exist, and p0 will also fail to be locked, resulting in a rollback of the entire statement.

      obclient> LOCK TABLE test PARTITION (p0, p3) IN EXCLUSIVE MODE;
      OBE-02149: Specified partition does not exist
      
    9. Lock the test table in two client sessions. One session holds an exclusive lock, and the other requests a shared lock. When the LOCK TABLE statement does not specify the WAIT N or NOWAIT keyword, the timeout is determined by the minimum of the statement timeout and transaction timeout. In this example, the lock conflict error will be reported after 1 second.

      # conn1
      obclient> START TRANSACTION;
      Query OK, 0 rows affected
      obclient> LOCK TABLE test IN EXCLUSIVE MODE;
      Query OK, 0 rows affected
      
      # conn2
      obclient> START TRANSACTION;
      Query OK, 0 rows affected
      obclient> SET ob_trx_timeout = 10000000000; # 10000 seconds
      Query OK, 0 rows affected
      obclient> SET ob_query_timeout = 1000000;   # 1 second
      Query OK, 0 rows affected
      
      # The requesting client will receive a lock conflict error after 1 second
      obclient> LOCK TABLE test IN SHARE MODE;
      OBE-30006: resource busy; acquire with WAIT timeout expired
      
    10. Lock the test table in two client sessions. One session holds an exclusive lock, and the other requests a shared lock, using the NOWAIT keyword. When the LOCK TABLE statement specifies the NOWAIT keyword, a lock conflict will result in an immediate error, with the error message indicating "NOWAIT". This differs from when the WAIT N or NOWAIT keywords are not specified.

      # conn1
      obclient> START TRANSACTION;
      Query OK, 0 rows affected
      obclient> LOCK TABLE test IN EXCLUSIVE MODE;
      Query OK, 0 rows affected
      
      # conn2
      obclient> START TRANSACTION;
      Query OK, 0 rows affected
      obclient> LOCK TABLE test IN SHARE MODE NOWAIT;
      OBE-00054: resource busy and acquire with NOWAIT specified or timeout expired
      
    11. Lock the test table in two client sessions. One session holds an exclusive lock, and the other requests a shared lock, using the WAIT N keyword. When the LOCK TABLE statement specifies the WAIT N keyword, a lock conflict will result in a wait of N seconds. If the lock is not released after N seconds, an error will be reported, with the error message indicating "timeout expired". This differs from when the WAIT N or NOWAIT keywords are not specified.

      # conn1
      obclient> START TRANSACTION;
      Query OK, 0 rows affected
      obclient> LOCK TABLE test IN EXCLUSIVE MODE;
      Query OK, 0 rows affected
      
      # conn2
      obclient> START TRANSACTION;
      Query OK, 0 rows affected
      
      # The requesting client will receive a lock conflict error after 1 second
      obclient> LOCK TABLE test IN SHARE MODE WAIT 1;
      OBE-00054: resource busy and acquire with NOWAIT specified or timeout expired
      

    Previous topic

    LOAD DATA
    Last

    Next topic

    REVOKE
    Next
    What is on this page
    Purpose
    Syntax
    Syntax
    Examples