All Products
Search
Document Center

Tablestore:Operations on tables

Last Updated:Nov 15, 2023

Tables are used to store and query data. After you create tables, you can manage the tables based on your business requirements. For example, you can query the names of all tables in the current instance, query the configurations of a table, and update the configurations of a table.

Usage notes

  • You cannot use the auto-increment primary key column feature and the local transaction feature at the same time. If the auto-increment primary key column feature is enabled when you create a table, the configurations of the local transaction feature do not take effect.

  • After you create a table, you cannot immediately perform read and write operations on the table. In most cases, you can perform read and write operations on a table about one minute after the table is created.

  • An instance can contain up to 64 tables.

Features

Tablestore provides the CreateTable, ListTable, UpdateTable, DescribeTable, and DeleteTable operations.

Before you store data in Tablestore, you must call the CreateTable operation to create a table.

When you create a table, you must configure the schema and features of the table based on your business requirements. The schema of a table includes the table name, primary key information, and predefined columns. In addition, you can configure the following features for a table:

  • Specify the max versions and time to live (TTL) of data. For more information, see Data versions and TTL.

  • Specify the reserved read and write throughput if the table is created in a pay-as-you-go high-performance instance. For more information, see the "Reserved read and write throughput" section of the Read and write throughput topic.

  • Create one or more index tables. For more information, see Overview.

    When you create a table, you can define some columns that are not primary key columns and their data types. When you use the secondary index feature, you can use the predefined columns as the index columns or attribute columns of an index table. You can also add or remove predefined columns after the table is created.

  • Enable the data encryption feature. For more information, see Data encryption.

  • By default, you can call the UpdateRow operation to write data to a table. However, if you want to use the TTL feature of a search index, you must prohibit the UpdataRow operation. You can also select No for the Allow Updates parameter after a table is created.

  • Enable the local transaction feature. For more information, see Local transactions.

    If you do not enable the local transaction feature when you create a table, you must submit a ticket to apply for the use of the feature.

    Important

    Only Tablestore SDK for Java V5.11.0 and later support the local transaction feature.

  • Configure an auto-increment primary key column. For more information, see Configure an auto-increment primary key column.

After a table is created, you can perform the following operations based on your business requirements.

Operation

Description

Query the names of tables

You can call the ListTable operation to query the names of all tables in the current instance.

Update the configurations of a table

You can call the UpdateTable operation to modify table configurations and the reserved read and write throughput.

Query the information about a table

You can call the DescribeTable operation to query the information about a table, such as the schema and the reserved read and write throughput of the table.

Delete a table

You can call the DeleteTable operation to delete a table from the current instance.

Important
  • If you delete a table, the table becomes unavailable and cannot be recovered. Perform this operation with caution.

  • Before you delete a table, make sure that the secondary indexes, search indexes, and data shipping tasks of the table are deleted.

Manage predefined columns

Predefined columns are used as the index columns or attribute columns of an index table.

When you use the secondary index feature but a table does not contain predefined columns or the existing predefined columns are not suitable for creating a secondary index, you must add predefined columns to the table.

You can remove the predefined columns that you no longer need.

Usage methods

Use the Tablestore console

To create a table in the Tablestore console, perform the following steps:

  1. Log on to the Tablestore console.

  2. In the top navigation bar, select a resource group and a region.

  3. On the Overview page, find the instance that you want to manage. Click the name of the instance or click Manage Instance in the Actions column.

  4. On the Instance Details tab, click Create Table.

  5. In the Create Table dialog box, configure the Table Name and Primary Key parameters. Configure other parameters based on your business requirements.

    Note

    For more information, see the "Step 3: Create a data table" section of the Use the Wide Column model in the Tablestore console topic.

    • If you want to use the auto-increment primary key column feature, select Auto Increment for a primary key column that is not a partition key column.

    • If you want to use the TTL feature of a search index, set the Allow Updates parameter to No.

    • If you want to encrypt the data stored to a disk, turn on Encryption and set the Encryption Type parameter to CMK of KMS.

    • If you want to create a local secondary index or a global secondary index, turn on Global Secondary Index, add predefined columns, and then create a secondary index.

    • If you want to configure other parameters, turn on Advanced Settings and configure the Time to Live, Max Versions, Max Version Offset, Reserved Read Throughput, and Reserved Write Throughput parameters.

  6. Click OK.

After you create tables, you can perform the following operations to manage the tables.

Operation

Description

View the names of tables

On the Tables tab, you can view the names of all tables in the current instance.

Update the configurations of a table

  1. On the Tables tab, click the name of the table that you want to manage.

  2. In the Description section of the Basic Information tab, click Modify Attributes.

  3. In the Modify Attributes dialog box, modify the parameters such as Time to Live, Max Versions, Max Version Offset, and Allow Updates based on your business requirements.

  4. Click OK.

View the information about a table

  1. On the Tables tab, click the name of the table that you want to manage.

  2. On the Basic Information tab, view the information about the table.

    In the Description section, you can view the values of the parameters such as Table Name, Time to Live, Max Versions, Max Version Offset, and Allow Updates. In the Advanced Features section, you can view the information about search indexes, primary key columns, and predefined columns.

Manage predefined columns

  1. On the Tables tab, click the name of the table that you want to manage.

  2. In the Advanced Features section of the Basic Information tab, click Add Pre-Defined Column.

  3. In the Add Pre-Defined Column dialog box, click Add and specify the name and data type of a column.

  4. Click OK.

To remove a predefined column, find the predefined column in the Advanced Features section of the Basic Information tab. Click the image.png icon in the Remove Pre-Defined Column column. In the Remove Pre-Defined Column message, click OK.

Delete a table

  1. On the Tables tab, find the table that you want to delete, click the image.png icon in the Actions column, and then select Delete.

  2. In the Delete Table dialog box, confirm the information about the table and click OK.

Use the Tablestore CLI

Run the create command to create a table on the Tablestore CLI. For more information, see the "Create a table" section of the Operations on data tables topic.

The following sample code provides an example on how to create a table named mytable. This table has a primary key column of the STRING type named uid and a primary key column of the INTEGER type named pid. Data in the table never expires.

create -t mytable --pk '[{"c":"uid", "t":"string"}, {"c":"pid", "t":"integer"}]'

To perform operations on a table, you must run the use command to select the table that you want to manage. For more information, see the "Use a data table" section of the Operations on data tables topic.

The following sample code provides an example on how to select the mytable table:

use --wc -t mytable

After you create tables, you can perform the following operations to manage the tables.

Operation

Description

Query the names of tables

Run the list command to query the names of all tables in the current instance. For more information, see the "List the names of tables" section of the Operations on data tables topic.

The following sample code provides an example on how to query the names of all tables in the current instance:

list -w

Update the configurations of a table

Run the alter command to update the configurations of a table. For more information, see the "Update a data table" section of the Operations on data tables topic.

The following sample code provides an example on how to update the configurations of the mytable table. In this example, the TTL of data is set to 86,400 seconds (one day), the max versions are set to 1, and the number of reserved read and write capacity units (CU) is set to 0.

alter -t mytable --ttl 86400 --version 1 --read_cu 0 --write_cu 0

Query the information about a table

Run the desc command to query the information about a table. For more information, see the "Query the information about a data table" section of the Operations on data tables topic.

The following sample code provides an example on how to query the information about the current table:

desc

Delete a table

Run the drop command to delete a table that you no longer need. For more information, see the "Delete a data table" section of the Operations on data tables topic.

The following sample code provides an example on how to delete the mytable table:

drop -t mytable -y

Use Tablestore SDKs

You can use Tablestore SDK for Java, Tablestore SDK for Go, Tablestore SDK for Python, Tablestore SDK for Node.js, Tablestore SDK for .NET, or Tablestore SDK for PHP to create and manage tables. In this example, Tablestore SDK for Java is used.

Important

The features supported by SDKs may vary. The actual features prevail.

Create a table based on your business requirements.

Create a table without indexes

The following sample code provides an example on how to create a data table without an index. The primary key column of the data table is the pk column of the STRING type. A maximum of three versions of data can be retained for each attribute column, and the data never expires.

private static void createTable(SyncClient client) {
    // Specify the name of the data table. 
    TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
    // Add a primary key column to the data table. 
    tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk", PrimaryKeyType.STRING)); 
    // Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table. 
    int timeToLive = -1; 
    // Specify the maximum number of versions that can be retained for each column. A value of 1 specifies that only the latest version is retained for each column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table. 
    int maxVersions = 3; 
    TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
        
    CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
    request.setReservedThroughput(new ReservedThroughput(new CapacityUnit(0, 0))); // Specify the reserved read throughput and reserved write throughout. The reserved read throughput and reserved write throughout can be set only to 0 for data tables in capacity instances. You can set the reserved read throughput and reserved write throughout to a non-zero value for data tables in high-performance instances. 
    client.createTable(request);
}

Configure a global secondary index when you create a table

The following sample code provides an example on how to create a data table and configure a global secondary index for the data table at the same time. The data table contains two primary key columns: the pk1 column of the STRING type and the pk2 column of the INTEGER type. The data table also contains two predefined columns: the defcol1 column of the STRING type and the defcol2 column of the INTEGER type. Only the latest version of data is retained for each attribute column, and the data never expires. The primary key columns of the global secondary index are the defcol1, pk1, and pk2 columns. The attribute column of the global secondary index is the defcol2 column.

private static void createTable(SyncClient client) {
    // Specify the name of the data table. 
    TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
    // Add primary key columns to the data table. 
    tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING)); 
    tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER)); 
    // Add predefined columns to the data table. 
    tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol1", DefinedColumnType.STRING)); 
    tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol2", DefinedColumnType.INTEGER)); 
    // Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table. 
    int timeToLive = -1; 
    // Specify the maximum number of versions that can be retained for each column. A value of 1 specifies that only the latest version is retained for each column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table. 
    int maxVersions = 1; 
    TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);

    ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
    // Specify the name of the index table. 
    IndexMeta indexMeta = new IndexMeta("<INDEX_NAME>");
    // Add a primary key column to the index table. 
    indexMeta.addPrimaryKeyColumn("defcol1"); 
    // Add an attribute column to the index table. 
    indexMeta.addDefinedColumn("defcol2"); 
    indexMetas.add(indexMeta);

    CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas); // Create the index table when you create the data table. 

    client.createTable(request);
}

Configure a local secondary index when you create a table

The following sample code provides an example on how to create a data table and configure a local secondary index for the data table at the same time. The data table contains two primary key columns: the pk1 column of the STRING type and the pk2 column of the INTEGER type. The data table also contains two predefined columns: the defcol1 column of the STRING type and the defcol2 column of the INTEGER type. Only the latest version of data is retained for each attribute column, and the data never expires. The primary key columns of the local secondary index are the defcol1, pk1, and pk2 columns. The attribute column of the local secondary index is the defcol2 column.

private static void createTable(SyncClient client) {
    // Specify the name of the data table. 
    TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
    // Add primary key columns to the data table. 
    tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING)); 
    tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER)); 
    // Add predefined columns to the data table. 
    tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol1", DefinedColumnType.STRING)); 
    tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol2", DefinedColumnType.INTEGER)); 
    // Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table. 
    int timeToLive = -1; 
    // Specify the maximum number of versions that can be retained for each column. A value of 1 specifies that only the latest version is retained for each column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table. 
    int maxVersions = 1; 
    TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);

    ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
    // Specify the name of the index table. 
    IndexMeta indexMeta = new IndexMeta("<INDEX_NAME>");
    // Set the index type to IT_LOCAL_INDEX. 
    indexMeta.setIndexType(IT_LOCAL_INDEX); 
    // Set the index update mode to IUM_SYNC_INDEX. If the index type is set to IT_LOCAL_INDEX, the index update mode must be set to IUM_SYNC_INDEX. 
    indexMeta.setIndexUpdateMode(IUM_SYNC_INDEX);  
    // Add a primary key column to the index table. The first primary key column of the index table must be the same as the first primary key column of the data table. 
    indexMeta.addPrimaryKeyColumn("pk1"); 
    // Add a primary key column to the index table. 
    indexMeta.addPrimaryKeyColumn("defcol1"); 
    // Add an attribute column to the index table. 
    indexMeta.addDefinedColumn("defcol2"); 
    indexMetas.add(indexMeta);

    CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas); // Create the index table when you create the data table. 

    client.createTable(request);
}

Enable the local transaction feature when you create a table

The following sample code provides an example on how to enable the local transaction feature when you create a data table. The data table contains two primary key columns: the pk1 column of the STRING type and the pk2 column of the INTEGER type. Only the latest version of data is retained for each attribute column, and the data never expires.

private static void createTable(SyncClient client) {
    // Specify the name of the data table. 
    TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
    // Add primary key columns to the data table. 
    tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
    tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER));
    // Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table. 
    int timeToLive = -1; 
    // Specify the maximum number of versions that can be retained for each column. A value of 1 specifies that only the latest version is retained for each column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table. 
    int maxVersions = 1; 
    TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);

    CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
    // Enable the local transaction feature. If you enable the auto-increment primary key column feature for the data table, the configurations of the local transaction feature do not take effect. 
    request.setLocalTxnEnabled(true); 

    client.createTable(request);
}

Enable the KMS key-based encryption feature when you create a table

The following sample code provides an example on how to enable KMS key-based encryption for a data table when you create the data table. The primary key of the data table is pk and of the STRING type. For the values of the attribute columns, only the latest version of data is retained, and the data never expires.

private static void createTable(SyncClient client) {
    // Specify the name of the data table. 
    TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
    // If you want to configure data encryption, configure encryption-related parameters in the CreateTable request. The following encryption modes are supported: KMS key-based encryption and BYOK-based encryption. In this example, KMS key-based encryption is enabled. 
    SSESpecification sseKms = new SSESpecification(true, SSEKeyType.SSE_KMS_SERVICE);
    // Add a primary key column to the data table. 
    tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk", PrimaryKeyType.STRING)); 
    // Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table. 
    int timeToLive = -1; 
    // Specify the maximum number of versions that can be retained for each column. A value of 1 specifies that only the latest version is retained for each column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table. 
    int maxVersions = 1; 
    TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
    CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
    // Specify the reserved read throughput and reserved write throughout. The values must be set to 0 for data tables in capacity instances and can be set to non-zero values for data tables in high-performance instances. 
    request.setReservedThroughput(new ReservedThroughput(new CapacityUnit(0, 0))); 
    request.setSseSpecification(sseKms);
    client.createTable(request);
}

Configure an auto-increment primary key column when you create a table

The following sample code provides an example on how to create a table that contains an auto-increment primary key column. The table contains the pk1 primary key column whose data type is STRING and the pk2 primary key column whose data type is INTEGER. The pk1 column is a partition key column, and the pk2 column is an auto-increment primary key column.

private static void createTable(SyncClient client) {
    // Specify the name of the table. 
    TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
    // Create the first primary key column. The first primary key column is a partition key column. 
    tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
    // Create the second primary key column whose data type is INTEGER and set the attribute of the column to AUTO_INCREMENT. 
    tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER, PrimaryKeyOption.AUTO_INCREMENT));
    // Specify the time to live (TTL) of data. A value of -1 specifies that the data never expires. Unit: seconds. 
    int timeToLive = -1;  
    // Specify the maximum number of versions that can be retained for each column. A value of 1 specifies that only the latest version is retained for each column. 
    int maxVersions = 1; 
    TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
    CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
    client.createTable(request);
}

After you create tables, you can perform the following operations to manage the tables.

Query the names of tables

The following sample code provides an example on how to query the names of all tables in an instance:

private static void listTable(SyncClient client) {
    ListTableResponse response = client.listTable();
    System.out.println("List of tables:");
    for (String tableName : response.getTableNames()) {
        System.out.println(tableName);
    }
}

Update the configurations of a table

The following sample code provides an example on how to update the configurations of a table. In this example, the max versions of the table are set to 5.

private static void updateTable(SyncClient client) {
    // Specify the time to live (TTL) of data. A value of -1 specifies that the data never expires. Unit: seconds. 
    int timeToLive = -1;
    // Update the max versions of the table to five. 
    int maxVersions = 5; 
    TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
    // Specify the name of the table. 
    UpdateTableRequest request = new UpdateTableRequest("<TABLE_NAME>");
    request.setTableOptionsForUpdate(tableOptions);
    client.updateTable(request);
}

Query the information about a table

The following sample code provides an example on how to query the schema information, optional configurations, reserved read throughput, and reserved write throughput of a table:

private static void describeTable(SyncClient client) {
    // Specify the name of the table. 
    DescribeTableRequest request = new DescribeTableRequest("<TABLE_NAME>");
    DescribeTableResponse response = client.describeTable(request);
    TableMeta tableMeta = response.getTableMeta();
    System.out.println("Table name:" + tableMeta.getTableName());
    System.out.println("Primary key information:");
    for (PrimaryKeySchema primaryKeySchema : tableMeta.getPrimaryKeyList()) {
        System.out.println(primaryKeySchema);
    }
    TableOptions tableOptions = response.getTableOptions();
    System.out.println("Table TTL:" + tableOptions.getTimeToLive());
    System.out.println("Maximum number of data versions:" + tableOptions.getMaxVersions());
    // You can view the encryption configurations of a table only if the table is an encrypted table. If the table is not an encrypted table, this parameter is not returned. 
    System.out.println("Encryption configurations:" + response.getSseDetails());
    ReservedThroughputDetails reservedThroughputDetails = response.getReservedThroughputDetails();
    System.out.println("Reserved read throughput:"
            + reservedThroughputDetails.getCapacityUnit().getReadCapacityUnit());
    System.out.println("Reserved write throughput:"
            + reservedThroughputDetails.getCapacityUnit().getWriteCapacityUnit());
}

Manage predefined columns

The following sample code provides an example on how to add predefined columns to a table. In this example, the following predefined columns are added: definedColumnName01 (type: STRING), definedColumnName02 (type: INTEGER), and definedColumnName03 (type: STRING).

public static void addDefinedColumnRequest(SyncClient client) {
    AddDefinedColumnRequest addDefinedColumnRequest = new AddDefinedColumnRequest();
    // Specify the name of the table. 
    addDefinedColumnRequest.setTableName("<TABLE_NAME>");
    // Add predefined columns to the table. 
    addDefinedColumnRequest.addDefinedColumn("definedColumnName01",DefinedColumnType.STRING);
    addDefinedColumnRequest.addDefinedColumn("definedColumnName02",DefinedColumnType.INTEGER);
    addDefinedColumnRequest.addDefinedColumn("definedColumnName03",DefinedColumnType.STRING);
    client.addDefinedColumn(addDefinedColumnRequest);
}

You can remove the predefined columns that you no longer need.

The following sample code provides an example on how to remove the definedColumnName01 and definedColumnName02 predefined columns from a table:

public static void deleteDefinedColumnRequest(SyncClient client){
    DeleteDefinedColumnRequest deleteDefinedColumnRequest = new DeleteDefinedColumnRequest();
    // Specify the name of the table. 
    deleteDefinedColumnRequest.setTableName("<TABLE_NAME>");
    // Remove predefined columns from the table. 
    deleteDefinedColumnRequest.addDefinedColumn("definedColumnName01");
    deleteDefinedColumnRequest.addDefinedColumn("definedColumnName02");
    client.deleteDefinedColumn(deleteDefinedColumnRequest);
}

Delete a table

The following sample code provides an example on how to delete the exampletable table:

private static void deleteTable(SyncClient client) {
    // Specify the name of the table. 
    DeleteTableRequest request = new DeleteTableRequest("<TABLE_NAME>");
    client.deleteTable(request);
}