All Products
Search
Document Center

MaxCompute:Create and Drop Tables

Last Updated:Jul 02, 2025

This topic describes how to use SQL statements to create and drop tables.

Type

Feature

Role

Access Point

Create Table

Creates a non-partitioned table, a partitioned table, a foreign table, or a clustered table.

Users who have the CREATE TABLE permission on a project.

You can execute the statements that are described in this topic on the following platforms:

Drop Table

Drops a partitioned table or a non-partitioned table.

Users who have the Drop permission on tables.

Create table

Create a non-partitioned table, a partitioned table, a foreign table, or a clustered table.

Limits

  • A partitioned table can have a maximum of six levels of partitions. For example, a table can use date as the partition column, and the partition levels can be year/month/week/day/hour/minute.

  • By default, a table can have a maximum of 60,000 partitions. You can adjust the maximum number of partitions in a table based on your business requirements.

For more information about table limits, see Limits on SQL Usage.

Command format

  • Create an internal table (including non-partitioned tables and partitioned tables).

    CREATE [OR REPLACE] TABLE [IF NOT EXISTS] <table_name> (
    <col_name> <data_type>, ... )
    [comment <table_comment>]
    [PARTITIONED BY (<col_name> <data_type> [comment <col_comment>], ...)]
    [AUTO PARTITIONED 
       BY (<auto_partition_expression> [AS <auto_partition_column_name>])
      [TBLPROPERTIES('ingestion_time_partition'='true')]
    ];
  • Create a clustered table.

    CREATE TABLE [IF NOT EXISTS] <table_name> (
    <col_name> <data_type>, ... )
    [CLUSTERED BY | RANGE CLUSTERED BY (<col_name> [, <col_name>, ...]) 
    [SORTED BY (<col_name> [ASC | DESC] [, <col_name> [ASC | DESC] ...])] 
    INTO <number_of_buckets> buckets];
  • Create a foreign table.

    For instance, to create an OSS foreign table using the built-in text data parser, refer to ORC Foreign Tables.

    CREATE EXTERNAL TABLE [IF NOT EXISTS] <mc_oss_extable_name> ( 
    <col_name> <data_type>, ... ) 
    STORED AS '<file_format>' 
    [WITH SERDEPROPERTIES (options)]  
    LOCATION '<oss_location>';
  • Create a table and specify its type.

    • Designate the table as a transactional table. You can update or delete the data of the table after the table is created. However, specific limits are imposed on the transactional table. We recommend that you create a transactional table based on your business requirements.

      CREATE [EXTERNAL] TABLE [IF NOT EXISTS] <table_name> (
      <col_name <data_type> [NOT NULL] [DEFAULT <default_value>] [comment <col_comment>], ...   
      [comment <table_comment>]
      [TBLPROPERTIES ("transactional"="true")];
    • Designate the table as a Delta table. You can use the primary key to perform operations on the table. For example, you can perform upserts, incremental queries, and time travel queries on the table.

      CREATE [EXTERNAL] TABLE [IF NOT EXISTS] <table_name> (
      <col_name <data_type> [NOT NULL] [DEFAULT <default_value>] [comment <col_comment>], ...   
      [PRIMARY KEY (<pk_col_name>[, <pk_col_name2>, ...] )]) 
      [comment <table_comment>]
      [TBLPROPERTIES ("transactional"="true" 
      [, "write.bucket.num" = "N", "acid.data.retain.hours"="hours"...])] [LIFECYCLE <days>];
  • Create a table based on an existing table.

    • Create a table and replicate data from an existing table to the new table. Partition properties are not replicated. You can create a table based on an external table or an existing table of an external project that is used to implement the data lakehouse solution.

      CREATE TABLE [IF NOT EXISTS] <table_name> [LIFECYCLE <days>] AS <select_statement>;
    • Create a table that has the same schema as an existing table. Data in the existing table is not replicated. You can create a table based on an external table or an existing table of an external project that is used to implement the data lakehouse solution.

      CREATE TABLE [IF NOT EXISTS] <table_name> [LIFECYCLE <days>] LIKE <existing_table_name>;

Parameter description

General parameters

Parameter

Required

Description

Remarks

OR REPLACE

No

If the table specified by <table_name> already exists, you can execute the DROP TABLE statement for the table, and then create a table with the same name for replacement.

This is equivalent to executing the following two commands:

DROP TABLE IF EXISTS <tableName>;  -- If the destination table exists, drop it first
CREATE TABLE <tableName> ...;      -- Create a table
Note

Limits: You cannot use CREATE OR REPLACE TABLE together with the following syntaxes:

  • CREATE TABLE ... IF NOT EXISTS.

  • CREATE TABLE ... AS SELECT.

  • CREATE TABLE ... LIKE.

EXTERNAL

No

Specifies that the table to be created is an external table.

None

IF NOT EXISTS

No

Specifies whether a table with the same name exists in the database.

If you do not specify the IF NOT EXISTS option and a table with the same name exists, an error is returned. If you specify the IF NOT EXISTS option, a success message is returned regardless of whether a table with the same name exists. The metadata of the existing table remains unchanged.

table_name

Yes

The name of the table.

The table name is not case-sensitive and cannot contain special characters. The name can contain letters, digits, and underscores (_). The name must start with a letter and cannot exceed 128 bytes in length. If the value of this parameter does not meet the requirements, an error is returned.

PRIMARY KEY(pk)

No

The primary key of the table.

You can specify one or more columns as the primary key. This indicates that the combination of these columns must be unique in the table. You must comply with the standard SQL syntax for primary keys. The columns that are defined as the primary key must be set to not null and cannot be modified.

Important

This parameter is only applicable to Delta tables.

col_name

Yes

The name of the table column.

The column name is not case-sensitive and cannot contain special characters. The name can contain letters, digits, and underscores (_). The name must start with a letter and cannot exceed 128 bytes in length. If the value of this parameter does not meet the requirements, an error is returned.

col_comment

No

The comment of a column.

The comment must be a valid string that does not exceed 1,024 bytes in length. If the value of this parameter does not meet the requirements, an error is returned.

data_type

Yes

The data type of the column.

Data types include BIGINT, DOUBLE, BOOLEAN, DATETIME, DECIMAL, and STRING. For more information, see Data Type Version Description.

NOT NULL

No

If you specify this parameter for a column, the values of the column cannot be null.

For more information about how to modify the NOT NULL attribute, see Partition Operations.

default_value

No

The default value for the column.

When the insert operation does not specify this column, the default value is written to the column.

Note

The current default value does not support functions such as GETDATE() and NOW().

table_comment

No

The comment of the table.

The comment must be a valid string that does not exceed 1,024 bytes in length. If the value of this parameter does not meet the requirements, an error is returned.

LIFECYCLE

No

The lifecycle of the table.

The value must be a positive integer. Unit: days.

  • Non-partitioned tables: If no data is modified within days after the table is last modified, MaxCompute automatically reclaims the table. This operation is similar to the DROP TABLE operation.

  • Partitioned tables: MaxCompute determines whether to reclaim a partition based on the value of LastModifiedTime. Unlike non-partitioned tables, a partitioned table is not dropped even if all of its partitions have been reclaimed. You can configure lifecycles for tables, but not for partitions.

Parameters for partitioned tables

MaxCompute supports two types of partitioned tables: regular partitioned tables and auto-partitioned tables (AUTO PARTITION). You can choose the appropriate type based on how you want partition columns to be generated in different scenarios. For more information, see Overview of partitioned tables.

Parameters for regular partitioned tables

Parameter

Required

Description

Remarks

PARTITIONED BY

Yes

Specifies the partition for a regular partitioned table.

You can specify only one of PARTITIONED BY or AUTO PARTITIONED BY to define partitions.

col_name

Yes

The name of the partition column.

The column name is not case-sensitive and cannot contain special characters. The name can contain letters, digits, and underscores (_). The name must start with a letter and cannot exceed 128 bytes in length. If the value of this parameter does not meet the requirements, an error is returned.

data_type

Yes

The data type of the partition column.

In the MaxCompute V1.0 data type edition, partition key columns must be of the STRING type. In the MaxCompute V2.0 data type edition, partition key columns can be of the TINYINT, SMALLINT, INT, BIGINT, VARCHAR, or STRING type. For more information, see Data Type Version Description. If you use a partition field to partition a table, a full table scan is not required when you add partitions, update partition data, or read partition data. This improves the efficiency of data processing.

col_comment

No

The comment of the partition column.

The comment must be a valid string that does not exceed 1,024 bytes in length. If the value of this parameter does not meet the requirements, an error is returned.

Note

The value of a partition key column cannot contain double-byte characters, such as Chinese characters. The value of a partition key column must start with a letter and can contain letters, digits, and supported special characters. It must be 1 to 255 bytes in length. Supported special characters include spaces, colons (:), underscores (_), dollar signs ($), number signs (#), periods (.), exclamation points (!), and at signs (@). The behavior of other characters is undefined, such as escape characters \t, \n, and /.

Parameters for AUTO PARTITION tables

Auto-partitioned tables (AUTO PARTITION) support automatically generating partition columns in a specific way. For more information about AUTO PARTITION tables, see Types of partitioned tables.

Parameter

Required

Description

Remarks

AUTO PARTITIONED BY

Yes

Specifies the partition for an AUTO PARTITION table.

You can specify only one of PARTITIONED BY or AUTO PARTITIONED BY to define partitions.

auto_partition_expression

Yes

Defines the expression for calculating partition columns.

Currently, only the TRUNC_TIME function is supported for generating partition columns, and only one partition column is supported.

The TRUNC_TIME function supports truncating date/time type column data in the table according to the specified time unit and generating partition columns.

auto_partition_column_name

No

The name of the generated partition column.

If the partition column name is not specified, the system uses _pt_col_0_ as the default name and checks if this column name exists in the table. If it exists, the suffix is automatically incremented (such as _pt_col_1_, _pt_col_2_, etc.) until an unused column name is found.

After partitioning with the partition expression, a STRING type partition column is generated based on the calculation result. The column name can be explicitly specified, but the column type and column values cannot be directly manipulated.

TBLPROPERTIES('ingestion_time_partition'='true')

No

Specifies whether to generate partition columns based on data write time.

For information about how to use partitioning based on data write time, see Auto-partitioned tables based on data write time.

Clustered table parameters

Clustered tables are classified into hash-clustered tables and range-clustered tables.

Hash-clustered tables

Parameter

Required

Description

Remarks

CLUSTERED BY

Yes

The hash key.

MaxCompute performs a hash operation on the specified columns and distributes data to each bucket based on the hash values. To avoid data skew and hot spots and ensure optimal parallel execution, the CLUSTERED BY columns must have many distinct values and a small number of repeat keys. In addition, to optimize join operations, you must also consider selecting common join keys or aggregation keys, which are similar to primary keys in traditional databases.

SORTED BY

Yes

Specifies how to sort fields in a bucket.

We recommend that you specify the same columns for SORTED BY and CLUSTERED BY to ensure optimal performance. In addition, after you specify the SORTED BY clause, MaxCompute automatically generates an index and uses the index to accelerate query execution.

number_of_buckets

Yes

The number of hash buckets.

This parameter is required and the value of this parameter varies based on the amount of data. By default, MaxCompute supports a maximum of 1,111 reducers. This means that MaxCompute supports a maximum of 1,111 hash buckets. You can use set odps.stage.reducer.num =<concurrency>; to increase this limit. However, the maximum number of hash buckets cannot exceed 4,000. Otherwise, performance is affected.

Note

To maintain optimal performance, we recommend that you take note of the following rules when you specify the number of hash buckets:

  • Keep the size of each hash bucket around 500 MB. For example, if the partition size is 500 GB, we recommend that you specify 1,000 buckets. This way, the size of each hash bucket is 500 MB on average. If a table contains a large amount of data, you can increase the size of each hash bucket from 500 MB to a size in the range of 2 GB to 3 GB. You can also use set odps.stage.reducer.num=<concurrency>; to increase the limit of 1,111 buckets.

  • For join optimization scenarios, removing the shuffle and sort steps can significantly improve performance. Therefore, the number of hash buckets of a table must be a multiple of the number of hash buckets of the other table. For example, one table has 256 hash buckets and the other table has 512 hash buckets. We recommend that you specify the number of hash buckets as 2n, such as 512, 1,024, 2,048, or 4,096. This way, the system can automatically split and merge hash buckets and remove the shuffle and sort steps to improve execution efficiency.

Range-clustered tables

Parameter

Required

Description

Remarks

RANGE CLUSTERED BY

Yes

The range-clustered columns.

MaxCompute performs the bucket operation on the specified columns and distributes data to each bucket based on the bucket ID.

SORTED BY

Yes

The sequence of fields in a bucket.

You can use this parameter in the same way as you use it for a hash-clustered table.

number_of_buckets

Yes

The number of hash buckets.

Range-clustered tables do not have the best practices of 2n for hash-clustered tables. If data is evenly distributed, you can specify any number of buckets. If you do not specify the number of buckets in a range-clustered table, MaxCompute automatically determines the optimal number based on the amount of data.

If JOIN and AGGREGATE operations are performed on range-clustered tables and the join key or group key is the range-clustered key or the prefix of the range-clustered key, you can manage flags to disable shuffling. This improves execution efficiency. You can use set odps.optimizer.enable.range.partial.repartitioning=true/false; to configure this feature. By default, this feature is disabled.

Note
  • Clustered tables help optimize the following aspects:

    • Bucket pruning.

    • Optimize Aggregation

    • Optimize storage.

  • Clustered tables have the following limits:

    • The INSERT INTO statement is not supported. You can use only the INSERT OVERWRITE statement to add data.

    • The data that is imported by using Tunnel commands is not arranged in order. Therefore, you cannot import data into a range-clustered table by using Tunnel commands.

    • The data backup and restoration feature is not supported.

Parameters for foreign tables

The following example uses parameters for creating an OSS foreign table. For more information about how to create foreign tables, see Foreign Tables.

Parameter

Required

Description

STORED AS '<file_format>'

Yes

Specifies the file_format based on the data format of the foreign table.

WITH SERDEPROPERTIES(options)

No

The parameters related to the authorization, compression, and character parsing of the foreign table.

oss_location

Yes

The OSS storage location of the data in the foreign table. For more information, see ORC Foreign Tables.

Transaction Table and Delta Table parameters

Parameters for Delta tables

Delta tables support capabilities such as near real-time reads and writes, incremental reads and writes, incremental storage, and real-time updates. Only Delta tables that have a primary key are supported.

Parameter

Required

Description

Remarks

PRIMARY KEY(PK)

Yes

This parameter is required when you create a Delta table. You can specify multiple columns as the primary key.

You must comply with the standard SQL syntax for primary keys. The columns that are defined as the primary key must be set to not null and cannot be modified. After you specify a primary key for a Delta table, duplicate data is removed from the table based on the primary key. The uniqueness constraint for the primary key column is valid in a single partition or in a non-partitioned table.

transactional

Yes

This parameter is required when you create a Delta table. You must set this parameter to true.

The true value indicates that the table complies with the transaction characteristics of MaxCompute atomicity, consistency, isolation, durability (ACID) tables and the Multi Version Concurrency Control (MVCC) model is used to support snapshot isolation.

write.bucket.num

No

The default value is 16. Valid values: (0, 4096].

This parameter indicates the number of buckets in a partition of a partitioned table or in a non-partition table. This parameter also specifies the number of concurrent nodes that are used to write data. You can change the value of this parameter for a partitioned table. If partitions are added to a partitioned table, the configuration of this parameter automatically takes effect on the new partitions. You cannot change the value of this parameter for a non-partitioned table. Take note of the following points:

  • If data is written by using a MaxCompute tunnel, the value of this parameter specifies the number of concurrent nodes that are used to write data. The setting of the parameter affects the import traffic and is also subject to the maximum number of concurrent nodes in the tunnel.

  • If data is written by using an SQL statement, the value of this parameter specifies the concurrency of the reducers that are used to write data. The setting is subject to the maximum number of concurrent reducer nodes.

  • We recommend that you write approximately 500 MB of data to each bucket. For example, if the partition size is about 500 GB, we recommend that you specify 1,000 buckets. This way, the size of each bucket is 500 MB on average. If a table contains a large amount of data, you can increase the size of each bucket from 500 MB to a size in the range of 2 GB to 3 GB.

acid.data.retain.hours

No

The default value is 24. Valid values: [24, 168].

The time range during which the historical data status can be queried by using the time travel feature. Unit: hours. If you need to query the historical data status for a period more than 168 hours (7 days), contact MaxCompute technical support.

  • If you set this parameter to 0, the historical data status is not retained, and time travel queries are not supported.

  • If the historical data status is retained for a period of time that is not in the range that is specified by this parameter, the data can be deleted. You can use the compact method to reclaim the space that is occupied by the data.

  • If you perform an SQL time travel query on data that is generated earlier than the time range specified by this parameter, an error is returned. For example, if the value of this parameter is 72, and the time travel query is performed to query the historical data status 72 hours ago, an error is returned.

acid.incremental.query.out.of.time.range.enabled

No

The default value is false.

If you set this parameter to true, the value of the endTimestamp property specified by an incremental query can be a point in time that is later than the maximum commit time of data in a table. If the value of the endTimestamp property is greater than the current time, new data may be inserted into a Delta table, and you may obtain different results for multiple queries. You can change the value of this parameter for a table.

acid.write.precombine.field

No

You can use this parameter to specify the name of only one column.

If you specify a column name, the system performs data deduplication based on the primary key (PK) column in the file that contains the SQL statement committed together with this parameter. This ensures data uniqueness and consistency.

Note

If the size of data that is committed at a time exceeds 128 MB, multiple files are generated. This parameter cannot be used for multiple files.

acid.partial.fields.update.enable

No

If you set this parameter to true, you can update data in specific columns in a Delta table by using SQL or Tunnel.

This parameter is set when the table is created. It cannot be modified after the table has been successfully created.

  • Requirements for other general parameters of Delta tables:

    • LIFECYCLE: The lifecycle of the table must be greater than or equal to the lifecycle for time travel queries, which means lifecycle >= acid.data.retain.hours / 24. When you create a table, MaxCompute checks the lifecycle of the table that you specified. If the specified lifecycle does not meet the requirements, an error is returned.

    • Unsupported features: Delta tables do not support the CLUSTER BY and CREATE TABLE AS statements and cannot be used as external tables.

  • Other limits:

    • Only MaxCompute SQL can directly perform operations on Delta tables.

    • You cannot change existing common tables to Delta tables.

    • You cannot change the schema of the primary key column of a Delta table.

Parameters for transaction tables

Parameter

Required

Description

TBLPROPERTIES(transactional"="true")

Yes

Set the table to a transactional table. You can subsequently perform update or delete operations on the transactional table to achieve row-level updates or data deletion. For more information, see Update or delete data (UPDATE | DELETE).

Take note of the following limits when you use a transactional table:

  • MaxCompute allows you to set a table to a transactional table only when you create the table. You cannot modify an existing table to make it transactional by using the ALTER TABLE statement. The following statement returns an error:

    ALTER TABLE not_txn_tbl SET TBLPROPERTIES("transactional"="true");
    -- An error is returned.
    FAILED: Catalog Service Failed, ErrorCode: 151, Error Message: Set transactional is not supported
  • When you create a clustered table or an external table, you cannot set it to a transactional table.

  • You cannot convert between transactional tables and MaxCompute internal tables, external tables, or clustered tables.

  • Transactional table files are not automatically merged. You need to manually execute merge operations. For more information, see Merge Transactional Table Files.

  • The merge partition operation is not supported.

  • Specific limits are imposed on access to transactional tables from jobs of other systems. For example, if your job is a Graph job, you cannot use the job to read data from or write data to a transactional table. If your job is a Spark job or a Platform for AI (PAI) job, you can use the job to only read data from a transactional table and you cannot use the job to write data to a transactional table.

  • Before you perform update, delete, or insert overwrite operations on important data in a transactional table, you must manually back up the data to another table by using select and insert operations.

Parameters for creating tables based on existing data tables

  • You can use the CREATE TABLE [IF NOT EXISTS] <table_name> [LIFECYCLE <days>] AS <select_statement>; statement to create a table and replicate data from an existing table to the new table.

    • However, partition properties and the lifecycle property of the source table are not replicated to the created table. The partition key columns of the source table are considered common columns in the created table.

    • You can also use the lifecycle parameter to reclaim the table. You can also execute this statement to create an internal table and replicate data of an external table to the internal table.

  • You can use the CREATE TABLE [IF NOT EXISTS] <table_name> [LIFECYCLE <days>] LIKE <existing_table_name>; statement to create a table that has the same schema as an existing table.

    • However, tables created by using this statement do not replicate data or replicate the lifecycle property of the source table.

    • You can also use the lifecycle parameter to reclaim the table. You can also execute this statement to create an internal table that has the same schema as an existing external table.

Examples

Create a non-partitioned table

  • Create a non-partitioned table

    CREATE TABLE test1 (key STRING);
  • Create a non-partitioned table and configure default values for table fields.

    CREATE TABLE test_default( 
    tinyint_name tinyint NOT NULL default 1Y,
    smallint_name SMALLINT NOT NULL DEFAULT 1S,
    int_name INT NOT NULL DEFAULT 1,
    bigint_name BIGINT NOT NULL DEFAULT 1,
    binary_name BINARY ,
    float_name FLOAT ,
    double_name DOUBLE NOT NULL DEFAULT 0.1,
    decimal_name DECIMAL(2, 1) NOT NULL DEFAULT 0.0BD,
    varchar_name VARCHAR(10) ,
    char_name CHAR(2) ,
    string_name STRING NOT NULL DEFAULT 'N',
    boolean_name BOOLEAN NOT NULL DEFAULT TRUE
    );

Create a partitioned table

  • Create a regular partitioned table.

    CREATE TABLE IF NOT EXISTS sale_detail(
     shop_name     STRING,
     customer_id   STRING,
     total_price   DOUBLE)
    PARTITIONED BY (sale_date STRING, region STRING);
  • Create an AUTO PARTITION table that generates partitions based on time-type data columns using time calculation functions.

    -- Truncate the sale_date column by month to generate a partition column named sale_month, and partition the table based on this column
    CREATE TABLE IF NOT EXISTS auto_sale_detail(
     shop_name     STRING,
     customer_id   STRING,
     total_price   DOUBLE,
     sale_date    DATE )
    AUTO PARTITIONED BY (TRUNC_TIME(sale_date, 'month') AS sale_month);
  • Create an AUTO PARTITION table and specify partition generation based on data write time. The system automatically captures the time when data is written to MaxCompute and generates partitions using time calculation functions.

    -- After the table is created, when data is written, the system automatically captures the data write time _partitiontime, truncates it by day, generates a partition column named sale_date, and partitions the table based on this column
    CREATE TABLE IF NOT EXISTS auto_sale_detail2(
     shop_name     STRING,
     customer_id   STRING,
     total_price   DOUBLE,
     _partitiontime TIMESTAMP_NTZ)
    AUTO PARTITIONED BY (TRUNC_TIME(_partitiontime, 'day') AS sale_date)
    TBLPROPERTIES('ingestion_time_partition'='true');

Create a new table

  • Create a table to replace an existing table.

    1. Create the existing table mytable and write data to it.

      CREATE OR REPLACE TABLE my_table(a BIGINT);
      
      INSERT INTO my_table(a) VALUES (1),(2),(3);
    2. Use OR REPLACE to create a table with the same name and modify its fields.

      CREATE OR REPLACE TABLE my_table(b STRING);
    3. Query the my_table table. The result shown in the following figure is returned.

      +------------+
      | b          | 
      +------------+
      +------------+

    The following SQL statements are invalid:

    CREATE OR REPLACE TABLE IF NOT EXISTS my_table(b STRING);
    CREATE OR REPLACE TABLE my_table AS SELECT;
    CREATE OR REPLACE TABLE my_table LIKE newtable;
  • Create a table, replicate data from an existing table to the new table, and then configure the lifecycle for the new table.

    -- Create a new table sale_detail_ctas1, replicate data from sale_detail to sale_detail_ctas1, and configure the lifecycle for the new table.
    SET odps.sql.allow.fullscan=true;
    CREATE TABLE sale_detail_ctas1 LIFECYCLE 10 AS SELECT * FROM sale_detail;

    Run the DESC EXTENDED sale_detail_ctas1; command to view table details like schema and lifecycle.

    Note

    The sale_detail table is partitioned, yet the CREATE TABLE ... AS select_statement ... statement generates the sale_detail_ctas1 table without inheriting the partition attributes of the original table. In the new table, the partition key columns from the source table are treated as regular columns. As a result, the sale_detail_ctas1 table is a standard table with five columns and no partitioning.

  • Create a table and use constants as column values in the SELECT clause.

    Note

    When using constants as column values in the SELECT clause, it's recommended to specify column names. Otherwise, columns like the fourth and fifth in the created table sale_detail_ctas3 will default to names such as _c4 and _c5.

    • Column names are specified.

      SET odps.sql.allow.fullscan=true;
      CREATE TABLE sale_detail_ctas2
      AS
      SELECT shop_name, customer_id, total_price, '2013' AS sale_date, 'China' AS region
      FROM sale_detail;
    • Column names are not specified.

      SET odps.sql.allow.fullscan=true;
      
      CREATE TABLE sale_detail_ctas3
      AS
      SELECT shop_name, customer_id, total_price, '2013', 'China' 
      FROM sale_detail;
  • Create a table that uses the same schema as an existing table and configure the lifecycle for the new table.

    CREATE TABLE sale_detail_like LIKE sale_detail LIFECYCLE 10;

    Run the DESC EXTENDED sale_detail_like; command to view table details like schema and lifecycle.

    Note

    The schema of the sale_detail_like table matches that of the sale_detail table, including properties like column names, comments, and table comments, except for the lifecycle. Data from the sale_detail table is not replicated to the sale_detail_like table.

  • Create a table that uses the same schema as a foreign table.

    -- Create a new table mc_oss_extable_orc_like that uses the same schema as the mc_oss_extable_orc foreign table.
    CREATE TABLE mc_oss_extable_orc_like LIKE mc_oss_extable_orc;

    Run the DESC mc_oss_extable_orc_like; command to view the schema details.

    +------------------------------------------------------------------------------------+
    | Owner: ALIYUN$****@***.aliyunid.com | Project: max_compute_7u************yoq              |
    | TableComment:                                                                      |
    +------------------------------------------------------------------------------------+
    | CreateTime:               2022-08-11 11:10:47                                      |
    | LastDDLTime:              2022-08-11 11:10:47                                      |
    | LastModifiedTime:         2022-08-11 11:10:47                                      |
    +------------------------------------------------------------------------------------+
    | InternalTable: YES      | Size: 0                                                  |
    +------------------------------------------------------------------------------------+
    | Native Columns:                                                                    |
    +------------------------------------------------------------------------------------+
    | Field           | Type       | Label | Comment                                     |
    +------------------------------------------------------------------------------------+
    | id              | string     |       |                                             |
    | name            | string     |       |                                             |
    +------------------------------------------------------------------------------------+

Create a table using new data types

SET odps.sql.type.system.odps2=true;
CREATE TABLE test_newtype (
    c1 TINYINT,
    c2 SMALLINT,
    c3 INT,
    c4 BIGINT,
    c5 FLOAT,
    c6 DOUBLE,
    c7 DECIMAL,
    c8 BINARY,
    c9 TIMESTAMP,
    c10 ARRAY<MAP<BIGINT,BIGINT>>,
    c11 MAP<STRING,ARRAY<BIGINT>>,
    c12 STRUCT<s1:STRING,s2:BIGINT>,
    c13 VARCHAR(20))
LIFECYCLE 1;

Create hash/range clustered tables

  • Create a hash-clustered non-partitioned table.

    CREATE TABLE t1 (a STRING, b STRING, c BIGINT) CLUSTERED BY (c) SORTED BY (c) INTO 1024 buckets;
  • Create a hash-clustered partitioned table.

    CREATE TABLE t2 (a STRING, b STRING, c BIGINT) 
    PARTITIONED BY (dt STRING) CLUSTERED BY (c) SORTED BY (c) INTO 1024 buckets;
  • Create a range-clustered non-partitioned table.

    CREATE TABLE t3 (a STRING, b STRING, c BIGINT) RANGE CLUSTERED BY (c) SORTED BY (c) INTO 1024 buckets;
  • Create a range-clustered partitioned table.

    CREATE TABLE t4 (a STRING, b STRING, c BIGINT) 
    PARTITIONED BY (dt STRING) RANGE CLUSTERED BY (c) SORTED BY (c); 

Create transaction tables

  • Create a transactional non-partitioned table.

    CREATE TABLE t5(id BIGINT) TBLPROPERTIES ("transactional"="true");
  • Create a transactional partitioned table.

    CREATE TABLE IF NOT EXISTS t6(id BIGINT) 
    PARTITIONED BY (ds STRING) TBLPROPERTIES ("transactional"="true");

Create internal tables

  • Create an internal table and replicate data of a partitioned external table to the internal table. Partition properties are not replicated to the internal table.

    1. Create an OSS external table and a MaxCompute internal table.

      -- Create an OSS external table and insert data into the table.
      CREATE EXTERNAL TABLE max_oss_test(a INT, b INT, c INT) 
      STORED AS TEXTFILE
      LOCATION "oss://oss-cn-hangzhou-internal.aliyuncs.com/<bucket_name>";
      
      INSERT INTO max_oss_test VALUES 
      (101, 1, 20241108),
      (102, 2, 20241109),
      (103, 3, 20241110);
      
      SELECT * FROM max_oss_test;
      
      -- Result
      a    b    c
      101    1    20241108
      102    2    20241109
      103    3    20241110
      
      
      -- Execute the CREATE TABLE AS statement to create an internal table
      CREATE TABLE from_exetbl_oss AS SELECT * FROM max_oss_test;
      
      -- Query data of the internal table
      SELECT * FROM from_exetbl_oss;
      
      -- All data in the internal table is returned
      a    b    c
      101    1    20241108
      102    2    20241109
      103    3    20241110
    2. Execute DESC from_exetbl_as_par; to query the internal table structure. The following result is returned:

      +------------------------------------------------------------------------------------+
      | Owner:                    ALIYUN$***********                                       |
      | Project:                  ***_*****_***                                            |
      | TableComment:                                                                      |
      +------------------------------------------------------------------------------------+
      | CreateTime:               2023-01-10 15:16:33                                      |
      | LastDDLTime:              2023-01-10 15:16:33                                      |
      | LastModifiedTime:         2023-01-10 15:16:33                                      |
      +------------------------------------------------------------------------------------+
      | InternalTable: YES      | Size: 919                                                |
      +------------------------------------------------------------------------------------+
      | Native Columns:                                                                    |
      +------------------------------------------------------------------------------------+
      | Field           | Type       | Label | Comment                                     |
      +------------------------------------------------------------------------------------+
      | a               | string     |       |                                             |
      | b               | string     |       |                                             |
      | c               | string     |       |                                             |
      +------------------------------------------------------------------------------------+
  • Create an internal table and replicate data of a partitioned external table to the internal table. Partition properties are replicated to the internal table.

    1. Create the internal table from_exetbl_like.

      -- Query the external table of the data lakehouse solution from the MaxCompute side
      SELECT * FROM max_oss_test;
      -- Result
      a    b    c
      101    1    20241108
      102    2    20241109
      103    3    20241110
      
      -- Execute the CREATE TABLE LIKE statement to create an internal table
      CREATE TABLE from_exetbl_like LIKE max_oss_test;
      
      -- Query the internal table
      SELECT * FROM from_exetbl_like;
      -- Only the schema of the internal table is returned
      a    b    c
    2. Execute DESC from_exetbl_like; to query the internal table structure. The following result is returned:

      +------------------------------------------------------------------------------------+
      | Owner:                    ALIYUN$************                                      |
      | Project:                  ***_*****_***                                            |
      | TableComment:                                                                      |
      +------------------------------------------------------------------------------------+
      | CreateTime:               2023-01-10 15:09:47                                      |
      | LastDDLTime:              2023-01-10 15:09:47                                      |
      | LastModifiedTime:         2023-01-10 15:09:47                                      |
      +------------------------------------------------------------------------------------+
      | InternalTable: YES      | Size: 0                                                  |
      +------------------------------------------------------------------------------------+
      | Native Columns:                                                                    |
      +------------------------------------------------------------------------------------+
      | Field           | Type       | Label | Comment                                     |
      +------------------------------------------------------------------------------------+
      | a               | string     |       |                                             |
      | b               | string     |       |                                             |
      +------------------------------------------------------------------------------------+
      | Partition Columns:                                                                 |
      +------------------------------------------------------------------------------------+
      | c               | string     |                                                     |
      +------------------------------------------------------------------------------------+

Create Delta tables

  • Create a Delta table.

    CREATE TABLE mf_tt (pk BIGINT NOT NULL PRIMARY KEY, val BIGINT) 
      TBLPROPERTIES ("transactional"="true");
  • Create a Delta table and configure the main table properties.

    CREATE TABLE mf_tt2 ( 
      pk BIGINT NOT NULL, 
      pk2 BIGINT NOT NULL, 
      val BIGINT, 
      val2 BIGINT, 
      PRIMARY KEY (pk, pk2)
    ) 
    TBLPROPERTIES (
      "transactional"="true", 
      "write.bucket.num" = "64", 
      "acid.data.retain.hours"="120"
    ) LIFECYCLE 7;

Drop table

Drops a partitioned table or a non-partitioned table.

Precautions

  • Before you drop a table, confirm that the table can be dropped. Proceed with caution. If you accidentally drop a table, you can restore the table if the backup and restoration feature is enabled for the project and the table is dropped within the backup data retention period specified for the project. For more information about backup and restoration, see Local Backup.

  • After you drop a table, the volume of stored data in a MaxCompute project decreases.

Command format

DROP TABLE [IF EXISTS] <table_name>; 

Parameter description

Parameter

Required

Description

IF EXISTS

No

If you do not specify the IF EXISTS option and the table does not exist, an error is returned. If you specify the IF EXISTS option, a success message is returned regardless of whether the table exists.

table_name

Yes

The name of the table that you want to drop.

Examples

-- Drop the sale_detail table. A success message is returned regardless of whether the sale_detail table exists.
DROP TABLE IF EXISTS sale_detail; 

References