Update attribute columns of a mapping table

Updated at:

Use Tablestore SDK for Java to add or remove attribute columns from a mapping table.

Prerequisites

Usage notes

  • Only mapping tables that are created with a CREATE TABLE statement support attribute column updates with ALTER TABLE. Mapping tables that are automatically bound by operations such as DESCRIBE do not support attribute column updates.

  • Each ALTER TABLE statement can add or remove only one attribute column. To update multiple attribute columns, execute an ALTER TABLE statement for each column.

  • An ALTER TABLE statement updates only the mapping table schema. It does not update the data storage schema of the Tablestore table.

  • An ALTER TABLE statement cannot add or remove primary key columns from a mapping table.

  • After you execute an ALTER TABLE statement, the SQL engine refreshes the mapping table asynchronously. The refresh can take up to 30 seconds. During the refresh, queries that return all attribute columns may not return the newly added column.

  • To change the definition of an existing attribute column, use DROP COLUMN to remove the column and then use ADD COLUMN to add the column with the new definition.

Description

Call the sqlQuery method to execute an ALTER TABLE statement and add or remove attribute columns from a mapping table.

SQLQueryResponse sqlQuery(SQLQueryRequest request)

The following example adds the new_column attribute column of the BIGINT type to the example_table mapping table.

SQLQueryRequest request = new SQLQueryRequest(
        "ALTER TABLE example_table ADD COLUMN new_column BIGINT");
client.sqlQuery(request);

Parameters

SQLQueryRequest contains the following parameter.

Name

Type

Description

query (required)

String

The ALTER TABLE statement to execute.

To add an attribute column, use ALTER TABLE <mapping_table_name> ADD COLUMN <column_name> <data_type>.

To remove an attribute column, use ALTER TABLE <mapping_table_name> DROP COLUMN <column_name>.

Scenario example

Remove an attribute column

The following example removes the new_column attribute column from the example_table mapping table.

SQLQueryRequest request = new SQLQueryRequest(
        "ALTER TABLE example_table DROP COLUMN new_column");
client.sqlQuery(request);