Defines a new table.
Synopsis
CREATE [ [GLOBAL | LOCAL] {TEMPORARY | TEMP } | UNLOGGED] TABLE [IF NOT EXISTS]
table_name (
[ { column_name data_type [ COLLATE collation ] [column_constraint [ ... ] ]
[ ENCODING ( storage_directive [, ...] ) ]
| table_constraint
| LIKE source_table [ like_option ... ] }
| [ column_reference_storage_directive [, ...]
[, ... ]
] )
[ INHERITS ( parent_table [, ... ] ) ]
[ WITH ( storage_parameter [=value] [, ... ] )
| WITH OIDS | WITHOUT OIDS ]
[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
[ TABLESPACE tablespace_name ]
[ DISTRIBUTED BY (column [opclass], [ ... ] )
| DISTRIBUTED RANDOMLY | DISTRIBUTED REPLICATED ]
[ 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
[(...)]
) ]
) ]
CREATE [ [GLOBAL | LOCAL] {TEMPORARY | TEMP} | UNLOGGED ] TABLE [IF NOT EXISTS]
table_name
OF type_name [ (
{ column_name WITH OPTIONS [ column_constraint [ ... ] ]
| table_constraint }
[, ... ]
) ]
[ WITH ( storage_parameter [=value] [, ... ] ) | WITH OIDS | WITHOUT OIDS ]
[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
[ TABLESPACE tablespace_name ]
where column_constraint is:
[ CONSTRAINT constraint_name]
{ NOT NULL
| NULL
| CHECK ( expression ) [ NO INHERIT ]
| DEFAULT default_expr
| UNIQUE index_parameters
| PRIMARY KEY index_parameters
| REFERENCES reftable [ ( refcolumn ) ]
[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
[ ON DELETE key_action ] [ ON UPDATE key_action ] }
[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
and table_constraint is:
[ CONSTRAINT constraint_name ]
{ CHECK ( expression ) [ NO INHERIT ]
| UNIQUE ( column_name [, ... ] ) index_parameters
| PRIMARY KEY ( column_name [, ... ] ) index_parameters
| FOREIGN KEY ( column_name [, ... ] )
REFERENCES reftable [ ( refcolumn [, ... ] ) ]
[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
[ ON DELETE key_action ] [ ON UPDATE key_action ] }
[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
and like_option
is:{INCLUDING|EXCLUDING} {DEFAULTS|CONSTRAINTS|INDEXES|STORAGE|COMMENTS|ALL}
and index_parameters in UNIQUE and PRIMARY
KEY constraints are:
[ WITH ( storage_parameter [=value] [, ... ] ) ]
[ USING INDEX TABLESPACE tablespace_name ]
and storage_directive for a column is:
compresstype={ZLIB|ZSTD|QUICKLZ|RLE_TYPE|NONE}
[compresslevel={0-9}]
[blocksize={8192-2097152} ]
and storage_parameter for the table is:
appendoptimized={TRUE|FALSE}
blocksize={8192-2097152}
orientation={COLUMN|ROW}
checksum={TRUE|FALSE}
compresstype={ZLIB|ZSTD|QUICKLZ|RLE_TYPE|NONE}
compresslevel={0-9}
fillfactor={10-100}
oids[=TRUE|FALSE]
and key_action is:
ON DELETE
| ON UPDATE
| NO ACTION
| RESTRICT
| CASCADE
| SET NULL
| SET DEFAULT
and partition_type is:
LIST | RANGE
and partition_specification is:
partition_element [, ...]
and partition_element is:
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_spec or template_spec is:
subpartition_element [, ...]
and subpartition_element is:
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 storage_parameter for a partition is:
appendoptimized={TRUE|FALSE}
blocksize={8192-2097152}
orientation={COLUMN|ROW}
checksum={TRUE|FALSE}
compresstype={ZLIB|ZSTD|QUICKLZ|RLE_TYPE|NONE}
compresslevel={1-19}
fillfactor={10-100}
oids[=TRUE|FALSE]
Description
CREATE TABLE creates an initially empty table in the current database. The
user who issues the command owns the table.
To be able to create a table, you must have USAGE privilege on all column
types or the type in the OF clause, respectively.
If you specify a schema name, Greenplum creates the table in the specified schema.
Otherwise Greenplum creates the table in the current schema. Temporary tables exist in a
special schema, so you cannot specify a schema name when creating a temporary table. Table
names must be distinct from the name of any other table, external table, sequence, index,
view, or foreign table in the same schema.
CREATE TABLE also automatically creates a data type that represents the
composite type corresponding to one row of the table. Therefore, tables cannot have the same
name as any existing data type in the same schema.
The optional constraint clauses specify conditions that new or updated rows must satisfy
for an insert or update 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.
Referential integrity constraints (foreign keys) are accepted but not enforced. The
information is kept in the system catalogs but is otherwise ignored.
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 Greenplum Database
distribution policy. If a DISTRIBUTED BY, DISTRIBUTED
RANDOMLY, or DISTRIBUTED REPLICATED clause is not supplied, then
Greenplum Database assigns a hash distribution policy to the table using either the
PRIMARY KEY (if the table has one) or the first column of the table as
the distribution key. Columns of geometric or user-defined data types are not eligible as
Greenplum distribution key columns. If a table does not have a column of an eligible data
type, the rows are distributed based on a round-robin or random distribution. To ensure an
even distribution of data in your Greenplum Database system, you want to choose a
distribution key that is unique for each record, or if that is not possible, then choose
DISTRIBUTED RANDOMLY.
If the DISTRIBUTED REPLICATED clause is supplied, Greenplum Database
distributes all rows of the table to all segments in the Greenplum Database system. This
option can be used in cases where user-defined functions must execute on the segments, and
the functions require access to all rows of the table. Replicated functions can also be used
to improve query performance by preventing broadcast motions for the table. The
DISTRIBUTED REPLICATED clause cannot be used with the PARTITION
BY clause or the INHERITS clause. A replicated table also cannot
be inherited by another table. The hidden system columns (ctid,
cmin, cmax, xmin,
xmax, and gp_segment_id) cannot be referenced in user
queries on replicated tables because they have no single, unambiguous value.
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, the Greenplum Database 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 optimizer to determine which
table partitions to scan in order to satisfy a given query predicate. These partition
constraints are managed automatically by the Greenplum Database.