edit-icon download-icon

Primary key column auto-increment

Last Updated: Apr 08, 2018

The auto-increment function of primary key column is a new feature launched by Table Store, and is available for Java SDK v4.2.0 and later versions.

The auto-increment function of primary key column means that if you specify a primary key column as the auto-increment column, Table Store generates a new value automatically in this column when you write data, and the generated value is the maximum value of this column under the same partition key. This feature mainly applies to system designing scenarios that need an auto-increment function applied to the primary key column, such as the item ID on e-commerce websites, user ID of large websites, post ID in forums and message ID in chat tools.


  • Table Store currently supports multiple primary keys, however, the first primary key is the partition key, to which auto-increment primary keys are not allowed.

  • Except for the partition key, any of the other primary keys can be set to the auto-increment column.

  • Because the automatic incrementation is generated on the basis of the partition key, the primary key column auto-increment function is at the partition key level.

  • Only one primary key column is allowed to be set to the auto-increment column in each table for the moment.

  • The automatically generated auto-increment column is of the 64-bit signed long integer type.


The primary key column auto-increment feature mainly involves two types of interfaces: creating a table and writing data.

Create a table

When creating a table, you only need to set the attribute of the auto-increment primary key to PrimaryKeyOption.AUTO_INCREMENT.

Related interface: CreateTable

  1. private static void createTable(SyncClient client) {
  2. TableMeta tableMeta = new TableMeta("table_name");
  3. // The first column is the partition key
  4. tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("PK_1", PrimaryKeyType.STRING));
  5. // The second column is the auto-increment column with INTEGER type and the attribute is AUTO_INCREMENT
  6. tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("PK_2", PrimaryKeyType.INTEGER, PrimaryKeyOption.AUTO_INCREMENT));
  7. int timeToLive = -1; // Never expire
  8. int maxVersions = 1; // Only one version is saved
  9. TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
  10. CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
  11. client.createTable(request);
  12. }


  • The first primary key is the partition key and cannot be set as the auto-increment column.
  • Only INTEGER columns can be set as the auto-increment column.
  • Only one primary key auto-increment column is allowed in each table.

Write data

When writing data, you only need to set the auto-increment column values as the placeholder PrimaryKeyValue.AUTO_INCREMENT.

If you want to have the primary key value automatically generated after the data is written to Table Store, you can set the ReturnType to RT_PK so that the primary key value is returned when the data is written successfully.

Related interfaces: PutRow/UpdateRow/BatchWriteRow

  1. private static void putRow(SyncClient client, String receive_id) {
  2. // Construct the primary key
  3. PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
  4. // The value in the first column is the first four digits of md5(receive_id)
  5. primaryKeyBuilder.addPrimaryKeyColumn("PK_1", PrimaryKeyValue.fromString("Hangzhou");
  6. // The third column is the primary key auto-increment column. This value is generated by Table Store. Set the auto-increment column values as the placeholder AUTO_INCREMENT. You do not need to enter a true value here.
  7. primaryKeyBuilder.addPrimaryKeyColumn("PK_2", PrimaryKeyValue.AUTO_INCREMENT);
  8. PrimaryKey primaryKey = primaryKeyBuilder.build();
  9. RowPutChange rowPutChange = new RowPutChange("table_name", primaryKey);
  10. // Here the return type is set to RT_PK, indicating to include the PK column value in the returned result. If the ReturnType is not set, no result is returned by default.
  11. rowPutChange.setReturnType(ReturnType.RT_PK);
  12. //Add the attribute column and message content
  13. rowPutChange.addColumn(new Column("content", ColumnValue.fromString(content)));
  14. //Write data to the Table Store
  15. PutRowResponse response = client.putRow(new PutRowRequest(rowPutChange));
  16. // Print the returned PK column
  17. Row returnRow = response.getRow();
  18. if (returnRow != null) {
  19. System.out.println("PrimaryKey:" + returnRow.getPrimaryKey().toString());
  20. }
  21. // Print the consumed CU
  22. CapacityUnit cu = response.getConsumedCapacity().getCapacityUnit();
  23. System.out.println("Read CapacityUnit:" + cu.getReadCapacityUnit());
  24. System.out.println("Write CapacityUnit:" + cu.getWriteCapacityUnit());
  25. }
Thank you! We've received your feedback.