CREATE-TABLE-AS-SELECT

Name

CREATE TABLE AS SELECT

Description

This statement creates the table structure by returning the results from the Select statement and imports the data at the same time

grammar:

  1. CREATE TABLE table_name [( column_name_list )]
  2. opt_engine:engineName
  3. opt_keys:keys
  4. opt_comment:tableComment
  5. opt_partition:partition
  6. opt_distribution:distribution
  7. opt_rollup:index
  8. opt_properties:tblProperties
  9. opt_ext_properties:extProperties
  10. KW_AS query_stmt:query_def

illustrate:

  • The user needs to haveSELECTpermission for the source table andCREATEpermission for the target database
  • After a table is created, data is imported. If the import fails, the table is deleted
  • You can specify the key type. The default key type is Duplicate Key

SinceVersion 1.2

  • All columns of type string (varchar/var/string) are created as type “string”.
  • If the created source is an external table and the first column is of type String, the first column is automatically set to VARCHAR(65533). Because of Doris internal table, String column is not allowed as first column.

Example

  1. Using the field names in the SELECT statement

    1. create table `test`.`select_varchar`
    2. PROPERTIES("replication_num" = "1")
    3. as select * from `test`.`varchar_table`
  2. Custom field names (need to match the number of fields returned)

    1. create table `test`.`select_name`(user, testname, userstatus)
    2. PROPERTIES("replication_num" = "1")
    3. as select vt.userId, vt.username, jt.status
    4. from `test`.`varchar_table` vt join
    5. `test`.`join_table` jt on vt.userId=jt.userId
  3. Specify table model, partitions, and buckets

    1. CREATE TABLE t_user(dt, id, name)
    2. ENGINE=OLAP
    3. UNIQUE KEY(dt, id)
    4. COMMENT "OLAP"
    5. PARTITION BY RANGE(dt)
    6. (
    7. FROM ("2020-01-01") TO ("2021-12-31") INTERVAL 1 YEAR
    8. )
    9. DISTRIBUTED BY HASH(id) BUCKETS 1
    10. PROPERTIES("replication_num"="1")
    11. AS SELECT cast('2020-05-20' as date) as dt, 1 as id, 'Tom' as name;

Keywords

  1. CREATE, TABLE, AS, SELECT

Best Practice