CREATE TABLE
Defines a new table.
Synopsis
CREATE [[GLOBAL | LOCAL] {TEMPORARY | TEMP}] TABLE <table_name> (
[ { <column_name> <data_type> [ DEFAULT <default_expr> ]
[<column_constraint> [ ... ]
[ ENCODING ( <storage_directive> [,...] ) ]
]
| <table_constraint>
| LIKE <other_table> [{INCLUDING | EXCLUDING}
{DEFAULTS | CONSTRAINTS}] ...} ]
[, ... ] ]
[<column_reference_storage_directive> [, …] ]
)
[ INHERITS ( <parent_table> [, ... ] ) ]
[ WITH ( <storage_parameter>=<value> [, ... ] )
[ ON COMMIT {PRESERVE ROWS | DELETE ROWS | DROP} ]
[ TABLESPACE <tablespace> ]
[ DISTRIBUTED BY (<column>, [ ... ] ) | DISTRIBUTED RANDOMLY ]
[ PARTITION BY <partition_type> (<column>)
[ SUBPARTITION BY <partition_type> (<column>) ]
[ SUBPARTITION TEMPLATE ( <template_spec> ) ]
[...]
( <partition_spec> )
| [ SUBPARTITION BY partition_type (<column>) ]
[...]
( <partition_spec>
[ ( <subpartition_spec>
[(...)]
) ]
)
where
[CONSTRAINT <constraint_name>]
NOT NULL | NULL
| CHECK ( <expression> )
where
COMPRESSTYPE={ZLIB | SNAPPY | GZIP | NONE}
| COMPRESSLEVEL={0-9}
| BLOCKSIZE={8192-2097152}
where
APPENDONLY={TRUE}
BLOCKSIZE={8192-2097152}
bucketnum={<x>}
ORIENTATION={ROW | PARQUET}
COMPRESSTYPE={ZLIB | SNAPPY | GZIP | NONE}
COMPRESSLEVEL={0-9}
FILLFACTOR={10-100}
OIDS=[TRUE|FALSE]
PAGESIZE={1024-1073741823}
ROWGROUPSIZE={1024-1073741823}
and
[CONSTRAINT <constraint_name>]
| CHECK ( <expression> )
where
LIST | RANGE
where
<partition_element> [, ...]
and
DEFAULT PARTITION <name>
| [PARTITION <name>] VALUES (<list_value> [,...] )
| [PARTITION <name>]
START ([<datatype>] '<start_value>') [INCLUSIVE | EXCLUSIVE]
[ END ([<datatype>] '<end_value>') [INCLUSIVE | EXCLUSIVE] ]
[ EVERY ([<datatype>] [<number> | INTERVAL] '<interval_value>') ]
| [PARTITION <name>]
END ([<datatype>] '<end_value>') [INCLUSIVE | EXCLUSIVE]
[ EVERY ([<datatype>] [<number> | INTERVAL] '<interval_value>') ]
[ WITH ( <partition_storage_parameter>=<value> [, ... ] ) ]
[<column_reference_storage_directive> [, …] ]
[ TABLESPACE <tablespace> ]
where
<subpartition_element> [, ...]
and
DEFAULT SUBPARTITION <name>
| [SUBPARTITION <name>] VALUES (<list_value> [,...] )
| [SUBPARTITION <name>]
START ([<datatype>] '<start_value>') [INCLUSIVE | EXCLUSIVE]
[ END ([<datatype>] '<end_value>') [INCLUSIVE | EXCLUSIVE] ]
[ EVERY ([<datatype>] [<number> | INTERVAL] '<interval_value>') ]
| [SUBPARTITION <name>]
END ([<datatype>] '<end_value>') [INCLUSIVE | EXCLUSIVE]
[ EVERY ([<datatype>] [<number> | INTERVAL] '<interval_value>') ]
[ WITH ( <partition_storage_parameter>=<value> [, ... ] ) ]
[<column_reference_storage_directive> [, …] ]
[ TABLESPACE <tablespace> ]
where
COMPRESSTYPE={ZLIB | SNAPPY | GZIP | NONE}
| COMPRESSLEVEL={0-9}
| BLOCKSIZE={8192-2097152}
where
COLUMN column_name ENCODING (<storage_directive> [, ... ] ), ...
|
DEFAULT COLUMN ENCODING (<storage_directive> [, ... ] )
where
APPENDONLY={TRUE}
BLOCKSIZE={8192-2097152}
ORIENTATION={ROW | PARQUET}
COMPRESSTYPE={ZLIB | SNAPPY | GZIP | NONE}
COMPRESSLEVEL={0-9}
FILLFACTOR={10-100}
OIDS=[TRUE|FALSE]
PAGESIZE={1024-1073741823}
ROWGROUPSIZE={1024-1073741823}
Description
CREATE TABLE
creates a new, initially empty table in the current database. The table is owned by the user issuing the command. If a schema name is given then the table is created in the specified schema. Otherwise it is created in the current schema. Temporary tables exist in a special schema, so a schema name may not be given when creating a temporary table. The name of the table must be distinct from the name of any other table, external table, sequence, or view in the same schema.
The optional constraint clauses specify conditions that new rows must satisfy for an insert operation to succeed. A constraint is an SQL object that helps define the set of valid values in the table in various ways. Constraints apply to tables, not to partitions. You cannot add a constraint to a partition or subpartition.
There are two ways to define constraints: table constraints and column constraints. A column constraint is defined as part of a column definition. A table constraint definition is not tied to a particular column, and it can encompass more than one column. Every column constraint can also be written as a table constraint; a column constraint is only a notational convenience for use when the constraint only affects one column.
When creating a table, there is an additional clause to declare the HAWQ distribution policy. If a DISTRIBUTED BY
clause is not supplied, HAWQ assigns a RANDOM
distribution policy to the table, where the rows are distributed based on a round-robin or random distribution. You can also choose to distribute data with a hash-based policy, where the bucketnum
attribute sets the number of hash buckets used by a hash-distributed table. Columns of geometric or user-defined data types are not eligible as HAWQ distribution key columns. The number of buckets affects how many virtual segments will be used in processing.
By default, a HASH distributed table is created with the number of hash buckets specified by the parameter bucketnum
storage parameter.
Note: Column-oriented tables are no longer supported. Use Parquet tables for HAWQ internal tables.
The PARTITION BY
clause allows you to divide the table into multiple sub-tables (or parts) that, taken together, make up the parent table and share its schema. Though the sub-tables exist as independent tables, HAWQ restricts their use in important ways. Internally, partitioning is implemented as a special form of inheritance. Each child table partition is created with a distinct CHECK
constraint which limits the data the table can contain, based on some defining criteria. The CHECK
constraints are also used by the query planner to determine which table partitions to scan in order to satisfy a given query predicate. These partition constraints are managed automatically by HAWQ.
Parameters
GLOBAL | LOCAL
These keywords are present for SQL standard compatibility, but have no effect in HAWQ.
TEMPORARY | TEMP
If specified, the table is created as a temporary table. Temporary tables are automatically dropped at the end of a session, or optionally at the end of the current transaction (see ON COMMIT
). Existing permanent tables with the same name are not visible to the current session while the temporary table exists, unless they are referenced with schema-qualified names. Any indexes created on a temporary table are automatically temporary as well.