All Products
Search
Document Center

MaxCompute:Create and drop tables

Last Updated:Dec 05, 2024

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

Operation

Description

Authorized user

Operation platform

Create a table

Creates a non-partitioned table, a partitioned table, an external table, or a clustered table.

Users who have the CreateTable permission on a project

You can perform the operations by using the following platforms:

Drop a table

Drops a partitioned table or a non-partitioned table.

Users who have the Drop permission on tables

Create a table

Creates a non-partitioned table, a partitioned table, an external table, or a clustered table.

Limits

  • A partitioned table can have a maximum of six levels of partitions. For example, if a table uses date columns as partition key columns, the six levels of the partitions are 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 the limits on tables, see MaxCompute SQL limits.

Syntax

  • Create a table.

    CREATE [OR REPLACE] [EXTERNAL] TABLE [IF NOT EXISTS] <table_name> (
    <col_name> <data_type>, ... )
    [comment <table_comment>]
    [PATITIONED BY (<col_name> <data_type> [comment <col_comment>], ...)];
  • 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 an external table. In this example, an Object Storage Service (OSS) external table is created.

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

    • Create 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> [
      PRIMARY KEY (<pk_col_name>, <pk_col_name2), 
        (<col_name <data_type> [NOT NULL] [DEFAULT <default_value>] [comment <col_comment>], ...)
      ]
      [comment <table_comment>]
      [TBLPROPERTIES ("transactional"="true")];
    • Create 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> [
      PRIMARY KEY (<pk_col_name>, <pk_col_name2), 
        (<col_name <data_type> [NOT NULL] [DEFAULT <default_value>] [comment <col_comment>], ...)]
      [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 based on an existing table and replicate data from the existing table to the new table. Partition properties are not replicated. You can create a table based on an existing 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 existing 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>;

Parameters

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.

You can use this parameter instead of the following statements:

DROP TABLE IF EXISTS <tableName>;  -- If a table with the same name already exist, drop the table.
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 create a table by using the name of an existing table but do not specify the IF NOT EXISTS parameter, an error is returned. If you create a table by using the name of an existing table and specify the IF NOT EXISTS parameter, a success message is returned even if the schema of the existing table is different from the schema of the table that you want to create. The metadata of the existing table remains unchanged.

table_name

Yes

The name of the table that you want to create.

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.

col_name

No

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 the 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

No

The data type of the column.

The following data types are supported: BIGINT, DOUBLE, BOOLEAN, DATETIME, DECIMAL, and STRING. For more information about data types, see Data type editions.

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 parameter, see Change the non-nullable property of a non-partition key column in a table.

default_value

No

The default value for the column.

If a column is not specified in an INSERT operation, the default value is used for the column.

Note

The default value for the specified column cannot be a function, such as GETDATE() or 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 data in a non-partitioned table remains unchanged for the number of days specified by days after the last data update, MaxCompute executes a statement, such as DROP TABLE, to reclaim the table.

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

Parameters for partitioned tables

Parameter

Required

Description

Remarks

PARTITIONED BY

No

The partition fields of a partitioned table.

None.

col_name

No

The name of a partition key 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

No

The data type of a partition key 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 about data types, see Data type editions. 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 a partition key 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. The length cannot exceed 255 bytes. The following special characters are supported: spaces, colons (:), underscores (_), dollar signs ($), number signs (#), periods (.), exclamation points (!), and at signs (@). The behavior of other characters is not defined, such as escape characters \t, \n, and /.

Parameters for clustered tables

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

Hash-clustered tables

Parameter

Required

Description

Remarks

CLUSTERED BY

No

The hash key.

MaxCompute performs a hash operation on specified columns and distributes data to each bucket based on the hash values. To prevent data skew and hot spots and to better execute concurrent statements, we recommend that you specify columns that have a large value range and a small number of duplicate key values in CLUSTERED BY. In addition, to optimize the JOIN operation, we recommend that you select commonly used join or aggregation keys. Join and aggregation keys are similar to the primary keys in conventional databases.

SORTED BY

No

Specifies how to sort fields in a bucket.

To improve performance, we recommend that you keep the configuration of the SORTED BY clause consistent with that of the CLUSTERED BY clause. After you specify fields in the SORTED BY clause, MaxCompute automatically generates indexes, which can be used to accelerate data queries.

number_of_buckets

No

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 run the set odps.stage.reducer.num =<Number of concurrent reducers>; command to increase the maximum number of hash buckets. The maximum number of hash buckets cannot exceed 4,000. If the number of hash buckets exceeds 4,000, computing performance may be adversely 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 hash 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 bucket from 500 MB to a size in the range of 2 GB to 3 GB. You can also run the set odps.stage.reducer.num=<Number of concurrent reducers>; command to set the maximum number of hash buckets to a value that is greater than 1111.

  • To optimize the performance of the JOIN operation, we recommend that you do not configure the shuffle and sort properties for hash-clustered tables. 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 set the number of hash buckets to 2n, such as 512, 1024, 2048, or 4096. This way, MaxCompute can automatically split and merge hash buckets. To ensure that the execution is more efficient, we recommend that you do not configure the shuffle and sort properties.

Range-clustered tables

Parameter

Required

Description

Remarks

RANGE CLUSTERED BY

No

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

No

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

No

The number of hash buckets.

Compared with hash-clustered tables, range-clustered tables have no limits on the number of buckets when data is evenly distributed. 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 set odps.optimizer.enable.range.partial.repartitioning to true or false to control shuffling. By default, this parameter is set to false. The default value indicates that shuffling is disabled.

Note
  • Clustered tables help optimize the following aspects:

    • Bucket pruning

    • Aggregation

    • Storage

  • Clustered tables have the following limits:

    • The INSERT INTO statement is not supported. You can execute 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 to a range-clustered table by using Tunnel commands.

    • The data backup and restoration feature is not supported.

Parameters for external tables

Parameter

Required

Description

STORED BY '<StorageHandler>'

No

The StorageHandler specified based on the data format of the external table.

WITH SERDEPROPERTIES(options)

No

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

oss_location

No

The OSS bucket where the data of the external table is stored. For more information, see Create an OSS external table.

Parameters for transactional tables and Delta tables

Parameters for transactional tables

Parameter

Required

Description

TBLPROPERTIES(transactional"="true")

No

This parameter is used to set the table to a transactional table. You can perform the UPDATE or DELETE operation on the transactional table to update or delete data by rows. For more information, see UPDATE and DELETE.

A transactional table has the following limits:

  • MaxCompute allows you to set a table to a transactional table only when you create the table. If you execute the ALTER TABLE statement to change an existing table to a transactional table, an error is returned.

    ALTER TABLE not_txn_tbl SET tblproperties("transactional"="true");
    -- The following 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 cannot be automatically merged. You must manually merge transactional table files. For more information, see the ALTER TABLE COMPACT section in UPDATE and DELETE.

  • 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 execute the UPDATE, DELETE, or INSERT OVERWRITE statement on important data in transactional tables, you must execute the SELECT and INSERT statements to back up the data to other tables.

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

Default value: 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 new partitions are added to a partitioned table, the configuration of this parameter takes effect on the new partitions by default. 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

Default value: 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

Default value: 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 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 data deduplication of multiple files.

  • Requirements for other general parameters of Delta tables

    • LIFECYCLE: the lifecycle of a table. The lifecycle of a table must be greater than or equal to the value of the following formula: 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.

    • Other 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 creating a table based on an existing table

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

    • 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 configure 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 execute 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 the source table.

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

    • You can configure 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 TABLE test1 (key STRING);
  • Create a 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 a table to replace an existing table.

    1. Create a table named mytable and write data to the table.

      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 the table fields.

      CREATE OR REPLACE TABLE my_table(b string);
    3. Query data of the my_table table. The following result 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 table named sale_detail_ctas1, replicate data from the sale_detail table to the sale_detail_ctas1 table, and then configure the lifecycle for the sale_detail_ctas1 table. 
    SET odps.sql.allow.fullscan=true;
    CREATE TABLE sale_detail_ctas1 LIFECYCLE 10 AS SELECT * FROM sale_detail;

    You can execute the DESC EXTENDED sale_detail_ctas1; statement to view table details, such as the schema and lifecycle of a table.

    Note

    The sale_detail table is a partitioned table, but the sale_detail_ctas1 table that is created by using CREATE TABLE ... AS select_statement ... does not replicate partition properties. The partition key columns of the source table are considered common columns in the table that is created. The sale_detail_ctas1 table is a non-partitioned table that contains five columns.

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

    Note

    If you use constants as column values in the SELECT clause, we recommend that you specify column names. In this example, the names of the fourth and fifth columns in the sale_detail_ctas3 table contain suffixes that are similar to _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;

    You can execute the DESC EXTENDED sale_detail_like; statement to view table details, such as the schema and lifecycle of a table.

    Note

    The schema of the sale_detail_like table is the same as that of the sale_detail table. The two tables have the same properties, such as column names, column comments, and table comments, aside from the lifecycle. However, data in the sale_detail table is not replicated to the sale_detail_like table.

  • Create a table that uses the same schema as an external table.

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

    You can execute the DESC mc_oss_extable_orc_like; statement to view table details, such as the schema of a table.

    +------------------------------------------------------------------------------------+
    | 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 that uses 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 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); 
  • Creates a transactional non-partitioned table.

    CREATE TABLE t5(id bigint) tblproperties("transactional"="true");
  • Creates a transactional partitioned table.

    CREATE TABLE IF NOT EXISTS t6(id bigint) 
    PARTITIONED BY (ds string) tblproperties ("transactional"="true");
  • 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 an internal table and replicate data of an 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;
      
      -- The following result is returned:
      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 the DESC from_exetbl_as_par; statement to query the schema of the internal table. 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 an external table and an internal table.

      -- Query the external table of an external project that is used to implement the data lakehouse solution in MaxCompute.
      SELECT * FROM max_oss_test;
      -- The following result is returned:
      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 data of the internal table.
      SELECT * FROM from_exetbl_like;
      -- Only the schema of the internal table is returned.
      a    b    c
    2. Execute the DESC from_exetbl_like; statement to query the schema of the internal table. 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 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 a table

Drops a non-partitioned table or partitioned table.

Usage notes

  • 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 the backup and restoration feature, see Backup and restoration.

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

Syntax

DROP TABLE [IF EXISTS] <table_name>; 

Parameters

Parameter

Required

Description

IF EXISTS

No

If you do not specify the IF EXISTS parameter and the table that you want to drop does not exist, an error is returned. If you specify the IF EXISTS parameter, 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

For more information about how to replicate data from one table to another, see CLONE TABLE.