All Products
Search
Document Center

Tablestore:Create a table

Last Updated:Aug 11, 2026

Use the Tablestore SDK for Go to create a Wide Column model table and configure its schema, properties, Stream settings, local transactions, secondary indexes, and data encryption.

Prerequisites

Install the Tablestore SDK for Go and initialize the client.

Description

Call CreateTable to create a table. You must specify the table schema, table options, and reserved throughput. You can also configure secondary indexes, Stream, local transactions, and data encryption.

func (tableStoreClient *TableStoreClient) CreateTable(request *CreateTableRequest) (*CreateTableResponse, error)
Note

After you create a table, wait until the table is loaded before you perform data operations. The loading process usually takes a few seconds.

The following sample creates the example_table table with the user_id and record_id primary key columns.

tableMeta := &tablestore.TableMeta{TableName: "example_table"}
tableMeta.AddPrimaryKeyColumn("user_id", tablestore.PrimaryKeyType_STRING)
tableMeta.AddPrimaryKeyColumn("record_id", tablestore.PrimaryKeyType_INTEGER)

request := &tablestore.CreateTableRequest{
    TableMeta: tableMeta,
    TableOption: &tablestore.TableOption{
        TimeToAlive:               -1,
        MaxVersion:                1,
        DeviationCellVersionInSec: 86400,
    },
    ReservedThroughput: &tablestore.ReservedThroughput{},
}

_, err := client.CreateTable(request)
if err != nil {
    log.Fatal(err)
}

Parameters

CreateTableRequest contains the following parameters.

Name

Type

Description

TableMeta (required)

*TableMeta

The table schema.

TableOption (required)

*TableOption

The table options.

ReservedThroughput (required)

*ReservedThroughput

The reserved read/write throughput, in CUs. The default read and write CUs are both 0. Non-zero values apply only to high-performance instances in CU mode.

IndexMetas (optional)

[]*IndexMeta

The secondary indexes to create with the table.

StreamSpec (optional)

*StreamSpecification

The Stream settings.

EnableLocalTxn (optional)

*bool

Specifies whether to enable local transactions. Default value: false.

SSESpecification (optional)

*SSESpecification

The data encryption settings. Data encryption can be enabled and configured only when the table is created. It cannot be disabled or modified after creation.

Table schema

TableMeta contains the following parameters.

Name

Type

Description

TableName (required)

string

The table name.

SchemaEntry (required)

[]*PrimaryKeySchema

The primary key schema. A table must contain one to four primary key columns. The first primary key column is the partition key.

DefinedColumns (optional)

[]*DefinedColumnSchema

The predefined columns. Predefined columns can be used to create secondary indexes.

Primary key column

Each element in TableMeta.SchemaEntry is of the PrimaryKeySchema type and contains the following parameters.

Name

Type

Description

Name (required)

*string

The primary key column name.

Type (required)

*PrimaryKeyType

The primary key column type. Valid values: PrimaryKeyType_STRING, PrimaryKeyType_INTEGER, and PrimaryKeyType_BINARY.

Option (optional)

*PrimaryKeyOption

The primary key column option. A non-partition INTEGER primary key column can be set to AUTO_INCREMENT. For more information, see Use an auto-increment primary key column.

Predefined column

Each element in TableMeta.DefinedColumns is of the DefinedColumnSchema type and contains the following parameters.

Name

Type

Description

Name (required)

string

The predefined column name.

ColumnType (required)

DefinedColumnType

The predefined column type. Valid values: STRING, INTEGER, BINARY, DOUBLE, and BOOLEAN.

Table options

TableOption contains the following parameters.

Name

Type

Description

TimeToAlive (required)

int

The time to live (TTL), in seconds. A value of -1 indicates that data never expires. For other values, the minimum is 86400 (one day). To use a search index or secondary index, set this parameter to -1 or set AllowUpdate to false.

MaxVersion (required)

int

The maximum number of versions retained for each attribute column. To use a search index or secondary index, set this parameter to 1.

DeviationCellVersionInSec (optional)

int64

The maximum version offset, in seconds. Default value: 86400 (one day). The difference between the timestamp of written data and the current system time must be within this range. Valid versions fall within [max(write time - maximum version offset, write time - TTL), write time + maximum version offset).

AllowUpdate (optional)

*bool

Specifies whether data can be updated by calling UpdateRow. Default value: true.

UpdateFullRow (optional)

*bool

Specifies whether to enable full-row updates. This parameter can be configured only when the table is created and cannot be modified by calling UpdateTable.

Reserved throughput

ReservedThroughput contains the following parameters.

Name

Type

Description

Readcap (optional)

int

The reserved read throughput, in CUs. Default value: 0.

Writecap (optional)

int

The reserved write throughput, in CUs. Default value: 0.

Secondary index

Each element in IndexMetas is of the IndexMeta type and contains the following parameters.

Name

Type

Description

IndexName (required)

string

The secondary index name.

Primarykey (required)

[]string

The index primary key columns, which can consist of table primary key columns and predefined columns. For a local secondary index, the first index primary key column must be the first table primary key column.

DefinedColumns (optional)

[]string

The index predefined columns.

IndexType (optional)

IndexType

The index type. IT_GLOBAL_INDEX specifies a global secondary index, and IT_LOCAL_INDEX specifies a local secondary index. Default value: IT_GLOBAL_INDEX.

Stream settings

StreamSpec is of the StreamSpecification type and contains the following parameters.

Name

Type

Description

EnableStream (optional)

bool

Specifies whether to enable Stream. Default value: false.

ExpirationTime (optional)

int32

The retention period of incremental logs, in hours. Maximum value: 168 (seven days). This parameter is required when EnableStream is set to true.

Local transaction settings

Set EnableLocalTxn to true to enable local transactions. Take note of the following items:

  • This parameter is supported in Tablestore SDK for Go 1.7.8 and later. We recommend that you use the latest version.

  • Local transactions and auto-increment primary key columns cannot be used together. If an auto-increment primary key column is configured, local transactions do not take effect even when enabled.

  • To enable local transactions on an existing table, submit a ticket.

Data encryption settings

SSESpecification contains the following parameters.

Name

Type

Description

Enable (optional)

bool

Specifies whether to enable data encryption. Default value: false.

KeyType (optional)

*SSEKeyType

The encryption type. SSE_KMS_SERVICE specifies KMS service key encryption, and SSE_BYOK specifies BYOK encryption.

KeyId (optional)

*string

The customer master key ID. This parameter is required for BYOK encryption.

RoleArn (optional)

*string

The RAM role ARN. This parameter is required for BYOK encryption.

Examples

Create a table with Stream enabled

The following sample enables Stream when the table is created and sets the incremental log retention period to 168 hours.

tableMeta := &tablestore.TableMeta{TableName: "stream_table"}
tableMeta.AddPrimaryKeyColumn("id", tablestore.PrimaryKeyType_STRING)

request := &tablestore.CreateTableRequest{
    TableMeta:          tableMeta,
    TableOption:        &tablestore.TableOption{TimeToAlive: -1, MaxVersion: 1},
    ReservedThroughput: &tablestore.ReservedThroughput{},
    StreamSpec: &tablestore.StreamSpecification{
        EnableStream:   true,
        ExpirationTime: 168,
    },
}

_, err := client.CreateTable(request)
if err != nil {
    log.Fatal(err)
}

Create a table with local transactions enabled

The following sample enables local transactions when the table is created.

tableMeta := &tablestore.TableMeta{TableName: "transaction_table"}
tableMeta.AddPrimaryKeyColumn("user_id", tablestore.PrimaryKeyType_STRING)
tableMeta.AddPrimaryKeyColumn("record_id", tablestore.PrimaryKeyType_INTEGER)

request := &tablestore.CreateTableRequest{
    TableMeta:          tableMeta,
    TableOption:        &tablestore.TableOption{TimeToAlive: -1, MaxVersion: 1},
    ReservedThroughput: &tablestore.ReservedThroughput{},
    EnableLocalTxn:     proto.Bool(true),
}

_, err := client.CreateTable(request)
if err != nil {
    log.Fatal(err)
}