All Products
Search
Document Center

Lindorm:CREATE TABLE LIKE

Last Updated:Mar 19, 2024

You can sue the CREATE TABLE LIKE syntax to replica the schema of an existing table and create a table based on the schema.

Applicable engines and versions

Syntax

create_table_statement ::=  CREATE TABLE new_table_identifier LIKE old_table_identifier 
                            [ WITH  '(' like_option (',' like_option)* ')']
like_option            ::=  option_identifier '=' string_literal 

Parameters

The CREATE TABLE LIKE syntax replicates only the schema but not the data of the original table.

The new table name (new_table_identifier)

The new table name must comply with the following rules:

  • The name can contain digits, letters, commas (,), hyphens (-), and underscores (_).

  • The name cannot start with a period (.) or a hyphen (-).

  • The name must be 1 to 255 characters in length.

The original table name (old_table_identifier)

You can use the DESCRIBE syntax to view the schema of the original table. For more information, see DESCRIBE/SHOW/USE.

Replication options (like_option)

You can use the WITH keyword to specify additional replication options.

The following table describes the supported replication options

Option

Type

Description

COPY_INDEX

STRING

Specifies whether to create a secondary index that is the same as that of the existing table for the new table that you want to create.

Valid values:

  • false (default): Do not replica the secondary index of the original table.

  • true: Replica the secondary index of the original table.

COPY_SPLITKEYS

STRING

Specifies whether to replicate the partition start key of the original table in the new table.

Valid values:

  • true: Replica the partition start key of the original table. In this case, the partitioning rules of the original table are also replicated in the new table.

  • false (default): Do not replicate the partition start key of the original table.

Examples

In the following examples, the original table is created by executing the following statement:Testable

CREATE TABLE sensor(
  p1 INT NOT NULL,
  p2 INT NOT NULL,
  c1 VARCHAR,
  c2 BIGINT,
  PRIMARY KEY(p1, p2)
) WITH (SPLITKEYS = '100000,300000,500000,700000,900000');

Replicate the table schema

Create a new table named table2 that has the same schema with the original table named sensor.

CREATE TABLE table2 LIKE sensor;

Replicate the schema and partition start key of the original table.

CREATE TABLE table2 LIKE sensor WITH (COPY_SPLITKEYS='true');