Update attribute columns of a mapping table

Updated at:

Tablestore SDK for Python adds or removes attribute columns of a mapping table.

Prerequisites

  • Install Tablestore SDK for Python and initialize a client.

  • SQL query requires version 5.4.2 or later. We recommend that you use the latest version.

Usage notes

  • Only mapping tables created by a CREATE TABLE statement support ALTER TABLE. Mapping tables automatically bound by operations such as DESCRIBE cannot be updated.

  • Each ALTER TABLE statement can add or remove only one attribute column.

  • The operation updates only the mapping table schema. It does not update the Tablestore storage schema and cannot add or remove primary key columns.

  • The SQL engine refreshes the mapping table asynchronously within 30 seconds. During the refresh, a query for all attribute columns may not return a newly added column.

  • To change an existing attribute column definition, remove the column and then add it with the new definition.

Description

Call the exe_sql_query method to execute an ALTER TABLE statement and add or remove an attribute column.

def exe_sql_query(self, query)

The following example adds the new_column attribute column to example_table.

query = "ALTER TABLE example_table ADD COLUMN new_column BIGINT"
client.exe_sql_query(query)

Parameters

The exe_sql_query method 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 examples

Remove an attribute column

The following example removes new_column from example_table.

query = "ALTER TABLE example_table DROP COLUMN new_column"
client.exe_sql_query(query)