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

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

Keywords

  1. CREATE, TABLE, AS, SELECT

Best Practice