Auto-increment primary key column
When you set a primary key column that is not a partition key as an auto-increment column, you do not need to provide a value for it when you write data. Tablestore automatically generates a value for the column that is unique and strictly increases within each partition key.
Features
An auto-increment primary key column has the following features:
-
The values of an auto-increment column are unique and strictly increasing within a partition key, but are not guaranteed to be consecutive.
-
The data type of an auto-increment column is a 64-bit signed integer.
-
Auto-increment columns are defined at the data table level. An instance can contain data tables both with and without auto-increment columns.
This feature does not affect the rules for conditional updates. For more information, see Conditional update.
Use cases
This feature is ideal for generating unique identifiers, such as product IDs for e-commerce sites, user IDs for large-scale websites, post IDs for forums, and message IDs for chat applications.
For an application example, see How to optimize the system architecture of a high-concurrency IM system.
Usage notes
-
Each data table can have at most one auto-increment primary key column. A partition key cannot be defined as an auto-increment column.
-
You can define an auto-increment column only when you create a data table. You cannot add one to an existing data table.
-
Only a primary key column of an integer type can be defined as an auto-increment column. The system-generated value for an auto-increment column is a 64-bit signed integer.
-
You cannot define an attribute column as an auto-increment column.
-
The auto-increment primary key column feature and the local transaction feature cannot be used together.
-
When writing to a table with an auto-increment primary key column, you must retrieve and store the column's generated value for subsequent updates or reads.
API operations
This table describes the API operations related to the auto-increment primary key column feature.
|
API |
Description |
|
To use this feature, you must define a primary key column that is not a partition key as an auto-increment column when you create the data table. |
|
|
After a data table is created, you cannot use the UpdateTable API operation to redefine a primary key column as an auto-increment column. |
|
|
When writing data, you do not need to specify a value for the auto-increment column because Tablestore generates one automatically. To obtain the complete primary key, set the ReturnType parameter to RT_PK. You can then use the primary key to query data using the GetRow API operation. Important
If you need to update a row but have not stored its auto-incremented primary key value, first use the GetRange API operation to retrieve the full primary key before performing the update. |
|
|
The GetRow API operation requires a complete primary key. You can obtain the complete primary key by setting the ReturnType parameter to RT_PK in your PutRow, UpdateRow, or BatchWriteRow request. Important
If you do not store the value of the auto-increment primary key column when writing data, you can still read a range of data based on the first primary key column. For more information, see Read a range of data. |
|
How to use
Console
CLI
SDKs
Billing
This feature does not affect existing billing rules. Returning the primary key data consumes no additional read capacity units.