8.8. CREATE TABLE

Synopsis

  1. CREATE TABLE [ IF NOT EXISTS ]
  2. table_name (
  3. { column_name data_type [ COMMENT comment ] [ WITH ( property_name = expression [, ...] ) ]
  4. | LIKE existing_table_name [ { INCLUDING | EXCLUDING } PROPERTIES ] }
  5. [, ...]
  6. )
  7. [ COMMENT table_comment ]
  8. [ WITH ( property_name = expression [, ...] ) ]

Description

Create a new, empty table with the specified columns.Use CREATE TABLE AS to create a table with data.

The optional IF NOT EXISTS clause causes the error to besuppressed if the table already exists.

The optional WITH clause can be used to set propertieson the newly created table or on single columns. To list all available tableproperties, run the following query:

  1. SELECT * FROM system.metadata.table_properties

To list all available column properties, run the following query:

  1. SELECT * FROM system.metadata.column_properties

The LIKE clause can be used to include all the column definitions froman existing table in the new table. Multiple LIKE clauses may bespecified, which allows copying the columns from multiple tables.

If INCLUDING PROPERTIES is specified, all of the table properties arecopied to the new table. If the WITH clause specifies the same propertyname as one of the copied properties, the value from the WITH clausewill be used. The default behavior is EXCLUDING PROPERTIES. TheINCLUDING PROPERTIES option maybe specified for at most one table.

Examples

Create a new table orders:

  1. CREATE TABLE orders (
  2. orderkey bigint,
  3. orderstatus varchar,
  4. totalprice double,
  5. orderdate date
  6. )
  7. WITH (format = 'ORC')

Create the table orders if it does not already exist, adding a table commentand a column comment:

  1. CREATE TABLE IF NOT EXISTS orders (
  2. orderkey bigint,
  3. orderstatus varchar,
  4. totalprice double COMMENT 'Price in cents.',
  5. orderdate date
  6. )
  7. COMMENT 'A table to keep track of orders.'

Create the table bigger_orders using the columns from ordersplus additional columns at the start and end:

  1. CREATE TABLE bigger_orders (
  2. another_orderkey bigint,
  3. LIKE orders,
  4. another_orderdate date
  5. )

See Also

ALTER TABLE, DROP TABLE, CREATE TABLE AS, SHOW CREATE TABLE