edit-icon download-icon

Create a table

Last Updated: Mar 01, 2018

A table is the basic data structure for both relational databases and Table Store. When creating a relational database table, you must define the entire data structure, but if you create a Table Store table, you only need to define the primary keys.

SQL

For SQL, the CREATE TABLE statement is used to create a table.

  1. CREATE TABLE UserHistory (
  2. user_id VARCHAR(20) NOT NULL,
  3. time_stamp INT NOT NULL,
  4. item_id VARCHAR(50),
  5. behavior_type VARCHAR(10),
  6. behavior_amount DOUBLE,
  7. behavior_count INT,
  8. content VARCHAR(100),
  9. PRIMARY KEY(user_id, time_stamp)
  10. );

The table’s primary keys include user_id and time_stamp. When creating a SQL table, you must strictly define all primary key and attribute columns. Use the ALTER TABLE statement to modify these definitions if required.

Table Store

For Table Store, the following parameters are set.

  1. public static final String TABLE_NAME = "UserHistory";
  2. TableMeta tableMeta = new TableMeta(TABLE_NAME);
  3. tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("user_id", PrimaryKeyType.STRING));
  4. tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("time_stamp", PrimaryKeyType.INTEGER));
  5. // The data expiration time in seconds; -1 indicates no data expiration. To set the expiration time to 1 year, use 365 * 24 * 3600
  6. int timeToLive = -1;
  7. // The max number of versions to save. For example, if it is set to 3, each column only saves the latest three versions
  8. int maxVersions = 3;
  9. TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
  10. CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
  11. // Set the reserved read/write throughput. If no value is set, the reserved throughput is set to 0 by default
  12. request.setReservedThroughput(new ReservedThroughput(new CapacityUnit(1, 1)));
  13. client.createTable(request);

The table’s primary keys include user_id and time_stamp. When creating a Table Store table, the following parameters are required.

Parameter Description
TABLE_NAME The name of the table
PrimaryKeySchema The names and types of primary keys
timeToLive The expiration time of data in the data table
maxVersion The maximum number of versions retained by each attribute column
ReservedThroughtput The data table’s reserved read/write throughput

Note: Reserved throughput cannot be set for capacity instances.

Thank you! We've received your feedback.