This topic describes how to create a table by calling the CreateTable operation. You must specify the schema information and configuration information of the table when you call the CreateTable operation. You can set reserved read and write throughput for tables in high performance instances. You can create one or more index tables when you create the base table.

Note
  • After a table is created, it takes several seconds to load the table. During this period, the read and write operations performed on the table may fail. Perform operations on a table after the table is loaded.
  • You must specify the primary key when you create a table. A primary key can contain one to four primary key columns. Each primary key column has a name and a data type.

Prerequisites

Operations

  /**
   * Create a table based on the specified table schema.
   */
  createTable(params, callback)
            

Parameters

Parameter Description
tableMeta The schema information of the table, which includes the following items:
  • tableName: the name of the base table.
  • primaryKey: the primary key of the table. For more information, see Primary keys and attributes.
    Note Attribute columns do not need to be defined. Different rows in Tablestore can have different attribute columns. The names of attribute columns can be specified when data is written.
    • The primary key of a table can contain one to four primary key columns. Primary key columns are sorted in the order in which they are added. For example, PRIMARY KEY (A, B, C) and PRIMARY KEY (A, C, B) have different primary key schemas. Tablestore sorts rows based on the values of all primary key columns.
    • The first primary key column serves as the partition key. Data with the same partition key value is stored in the same partition. Therefore, we recommend that you assign no more than 10 GB of data with the same partition key value. Otherwise, a single partition may be too large to split. We recommend that you set a well-designed primary key so that read and write operations can be evenly distributed to different partitions to facilitate load balancing.
  • definedColumn: the predefined columns of the table and the data types of the predefined column values. Primary key columns cannot be set to predefined columns. You can use predefined columns as the indexed columns or attribute columns for index tables.
tableOptions The configurations of the table. For more information, see Max versions and TTL.

The configurations include the following items:

  • timeToLive: the period for which data in the table can be retained. This period is the validity period of data. When the retention period exceeds the timeToLive value, Tablestore automatically deletes expired data.

    The minimum timeToLive value is 86,400 (one day). A value of -1 indicates that data never expires.

    When you create a table, you can set timeToLive to -1 if you expect that data never expires. After the table is created, you can call the UpdateTable operation to modify the timeToLive value.

    Unit: seconds.

    Note You must set timeToLive to -1 if you want to create index tables for the base table.
  • maxVersions: the maximum number of versions of data that can be retained for a single attribute column. When the number of versions of data in an attribute column exceeds the maxVersions value, Tablestore automatically deletes data of earlier versions.

    When you create a table, you can customize the maximum number of versions that can be retained for the data in an attribute column. After the table is created, you can call the UpdateTable operation to modify the maxVersions value for the table.

    Note You must set maxVersions to 1 if you want to create index tables for the base table.
reservedThroughput The reserved read and write throughput of the table.

For capacity instances, the reserved read and write throughput can be set only to 0. Reserved throughput does not apply to these instances.

The default value 0 indicates that all throughput is billed on a pay-as-you-go basis.

Unit: CU.

  • When reserved read throughput or reserved write throughput is set to a value greater than 0, Tablestore allocates and reserves related resources for the table. After the table is created, Tablestore charges reserved throughput resources. Additional throughput is billed on a pay-as-you-go basis. For more information, see Billing overview.
  • When reserved read throughput or reserved write throughput is set to 0, Tablestore does not allocate or reserve related resources for the table.
indexMetas The schema information of the index tables. Each indexMeta includes the following items:
  • name: the name of the index table.
  • primaryKey: the primary key of the index table, which is a combination of primary key columns and predefined columns of the base table.
  • definedColumns: the indexed attribute column, which is a combination of predefined columns of the base table.

Examples

  • Create a base table without creating an index table

    The following code provides an example on how to create a table that has two primary key columns and a reserved read and write throughput of (0, 0):

    var client = require('./client');
    
    var params = {
      tableMeta: {
        tableName: 'sampleTable',
        primaryKey: [
          {
            name: 'gid',
            type: 'INTEGER'
          },
          {
            name: 'uid',
            type: 'INTEGER'
          }
        ]
      },
      reservedThroughput: {
        capacityUnit: {
          read: 0,
          write: 0
        }
      },
      tableOptions: {
        timeToLive: -1,// Specify the validity period of data in seconds. A value of -1 indicates that the data never expires. If you set the validity period to a year, the value of timeToLive is 365 × 24 × 3600.
        maxVersions: 1// Specify the maximum number of versions that can be retained for each column. A value of 1 indicates that only the latest version is retained for each column.
      }
    };
    
    client.createTable(params, function (err, data) {
      if (err) {
        console.log('error:', err);
        return;
      }
      console.log('success:', data);
    });
                

    For the detailed sample code, visit CreateTable@GitHub.

  • Create an index table when you create the base table
    var client = require('./client');
    var TableStore = require('../index.js');
    
    var params = {
      tableMeta: {
        tableName: 'sdkGlobalTest',
        primaryKey: [
          {
            name: 'pk1',
            type: TableStore.PrimaryKeyType.INTEGER
          },
          {
            name: 'pk2',
            type: TableStore.PrimaryKeyType.INTEGER
          }
        ],
        definedColumn: [
          {
            "name": "col1",
            "type": TableStore.DefinedColumnType.DCT_INTEGER
          },
          {
            "name": "col2",
            "type": TableStore.DefinedColumnType.DCT_INTEGER
          }
        ],
      },
      reservedThroughput: {
        capacityUnit: {
          read: 0,
          write: 0
        }
      },
      tableOptions: {
        timeToLive: -1,// Specify the validity period of data in seconds. A value of -1 indicates that the data never expires. You must set timeToLive to -1 for the base table that has index tables.
        maxVersions: 1// Specify the maximum number of versions that can be retained for each column. A value of 1 indicates that only the latest version is retained for each column. You must set maxVersions to 1 for the base table that has index tables.
      },
      streamSpecification: {
        enableStream: false,// Stream cannot be enabled for global secondary indexes.
      },
      indexMetas: [
        {
          name: "sdkIndex1",
          primaryKey: ["pk2"],
          definedColumn: ["col1", "col2"]
        },
        {
          name: "sdkIndex2",
          primaryKey: ["col1"],
          definedColumn: ["col2"]
        }
      ]
    };
    
    client.createTable(params, function (err, data) {
      if (err) {
        console.log('error:', err);
        return;
      }
      console.log('success:', data);
    });