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
    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.3.5
    iconOceanBase Database
    SQL - V 4.3.5
    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

    Sequence pseudo columns

    Last Updated:2026-04-09 02:53:55  Updated
    share
    What is on this page
    How to retrieve sequence values
    Limitations and considerations
    Using sequences
    Examples

    folded

    share

    A sequence is a set of incrementing numbers generated by the database according to a specific rule. Typically, these are evenly spaced numerical values. Due to their incrementing nature, sequences are commonly used as primary keys and unique keys.

    How to retrieve sequence values

    There are two methods to retrieve sequence values:

    • CURRVAL: Returns the current value of the sequence.

    • NEXTVAL: Returns the next incrementing value of the sequence.

    When using a sequence, you must specify the sequence name before CURRVAL and NEXTVAL, and use a period (.) to reference it.

    For example, if the sequence name is SEQ_FOO, you can use SEQ_FOO.CURRVAL to get the current value of the SEQ_FOO sequence. Similarly, you can use SEQ_FOO.NEXTVAL to get the next incrementing value of the SEQ_FOO sequence.

    Limitations and considerations

    • Sequences cannot be used with statements such as HAVING, ORDER BY, and GROUP BY.
    • Sequences cannot appear in subqueries, except in the case of INSERT INTO SELECT.
    • Sequences cannot appear in WHERE expressions.

    Using sequences

    The values of CURRVAL and NEXTVAL can be used in the following contexts:

    • In the select list of a top-level SELECT statement.

    • In the VALUE clause of an INSERT statement.

    • In the SET clause of an UPDATE statement.

    Here is an example of querying a sequence:

    SELECT SEQUENCE_NAME.NEXTVAL FROM DUAL;      /*The sequence number increases each time it is executed.*/
    
    SELECT SEQUENCE_NAME.CURRVAL FROM DUAL;     /*The sequence number remains unchanged after multiple executions.*/
    

    When creating a sequence, you must specify its initial value and increment. The first reference to NEXTVAL will return the initial value of the sequence. Subsequent references to NEXTVAL will return a new value by adding the defined increment to the last returned value. Any reference to CURRVAL will always return the current value of the sequence, which is the value returned by the last reference to NEXTVAL.

    Before referencing the CURRVAL pseudo column of a sequence in a session, you should first initialize the sequence value for the session by referencing the NEXTVAL pseudo column of the sequence.

    When creating a sequence, you can define its initial value and the increment between values. The first reference to NEXTVAL will return the initial value of the sequence. Subsequent references to NEXTVAL will increment the sequence value by the defined amount and return the new value. Any reference to CURRVAL will always return the current value of the sequence, which is the value returned by the last reference to NEXTVAL. For more information about creating and dropping sequences, see CREATE SEQUENCE and DROP SEQUENCE.

    Examples

    obclient> CREATE SEQUENCE s1 START WITH 95 INCREMENT BY 1 NOORDER CACHE 10000;
    Query OK, 0 rows affected
    
    obclient> CREATE TABLE tbl1 (i INT,j INT);
    Query OK, 0 rows affected
    
    obclient> INSERT INTO tbl1 VALUES(1,70),(2,71),(3,3),(4,4);
    4 rows affected
    
    obclient> SELECT * FROM tbl1;
    +---+------+
    | I | J    |
    +---+------+
    | 1 |   70 |
    | 2 |   71 |
    | 3 |    3 |
    | 4 |    4 |
    +---+------+
    4 rows in set
    
    obclient> SELECT s1.nextval, i, j FROM tbl1;
    +------------+---+------+
    | S1.NEXTVAL | I | J    |
    +------------+---+------+
    |         95 | 1 |   70 |
    |         96 | 2 |   71 |
    |         97 | 3 |    3 |
    |         98 | 4 |    4 |
    +------------+---+------+
    4 rows in set
    
    obclient> SELECT s1.nextval, i, j FROM tbl1;
    +------------+---+------+
    | S1.NEXTVAL | I | J    |
    +------------+---+------+
    |        99  | 1 |   70 |
    |        100 | 2 |   71 |
    |        101 | 3 |    3 |
    |        102 | 4 |    4 |
    +------------+---+------+
    4 rows in set
    
    obclient> UPDATE tbl1 SET i = s1.nextval;
    Query OK, 4 rows affected
    Rows matched: 4  Changed: 4  Warnings: 0
    
    obclient> SELECT * FROM tbl1;
    +-----+------+
    | I   | J    |
    +-----+------+
    | 103 |   70 |
    | 104 |   71 |
    | 105 |    3 |
    | 106 |    4 |
    +-----+------+
    4 rows in set
    
    obclient> SELECT s1.currval FROM DUAL;
    +---------+
    | currval |
    +---------+
    | 106     |
    +---------+
    1 row in set
    

    Previous topic

    PARTITIONID pseudo column
    Last

    Next topic

    Overview
    Next
    What is on this page
    How to retrieve sequence values
    Limitations and considerations
    Using sequences
    Examples