This topic describes the Sequence Engine provided by RDS for MySQL.
Background
RDS for MySQL provides Sequence Engine to obtain unique and auto-incrementing sequence values. Sequence Engine is a logical engine that still uses the InnoDB storage engine for data persistence and is compatible with existing data backup and recovery tools. Through encapsulation, the upper layer realizes the functions of sequence value generation, caching, and so on.
Supported version
- MySQL 5.7 version, and the kernel minor version is MySQL 5.7.32_20241215 and above.
- MySQL 8.0 version, and the kernel minor version is MySQL 8.0.32_20250215 and above.
- MySQL 8.4 version, and the kernel minor version is MySQL 8.4.7_20260415 and above.
Prerequisites
Creating a Sequence
Use the CREATE SEQUENCE statement to create a Sequence.
The parameters in the statement are described as follows:
tip
The preceding parameters cannot be negative.
For compatibility with the MySQL Dump backup method, you can also create a Sequence by using Engine=Sequence. Specify the Sequence parameters by inserting an initial data row. The parameters are based on the first record. Users can download, back up, and restore a Sequence created with Engine=Sequence using tools such as MySQL Dump. Example as follows:
Deleting a Sequence
You can delete a Sequence using the DROP SEQUENCE and DROP TABLE statements.
- Using the DROP SEQUENCE statement
- Using the DROP TABLE statement
Obtaining nextval
You can obtain nextval by using the nextval() function or accessing the nextval column in the Sequence table.
- Using the nextval() function
- Accessing the nextval column in the Sequence table
warning
SELECT nextval from <Database name>.<Sequence name>; queries records in the Sequence table instead of obtaining the next value of the Sequence.
Obtaining currval
You can obtain currval (the value of the last nextval obtained in the current session) by using the `currval()` function or by accessing the `currval` column in the Sequence table.
- Using the `currval()` function
- Accessing the `currval` column in the Sequence table
warning
- SELECT currval from <Database name>.<Sequence name>; queries records in the Sequence table instead of obtaining the current value of the Sequence.
- For a newly created Sequence, you must call its NEXTVAL once before querying it. Otherwise, a `Sequence 'xxx' is not yet defined in current session` error occurs.
- If the connection pool mode for the proxy endpoint is a transaction-level connection pool, the obtained currval value may be incorrect. To obtain an accurate currval value, use a proxy endpoint in direct connection mode or disable the database proxy for the instance.
View Sequence
You can view the specific properties of a Sequence by using the SHOW statement.