CREATE TABLE v14
Name
CREATE TABLE
— Define a new table.
Synopsis
Where column_constraint
is:
Where table_constraint
is:
Description
CREATE TABLE
creates an empty table in the current database. The user who creates the table owns the table.
If you provide a schema name, then the table is created in the specified schema. Otherwise it's created in the current schema. Temporary tables exist in a special schema, so you can't provide a schema name when creating a temporary table. The table name must differ from the name of any table, sequence, index, or view in the same schema.
CREATE TABLE
also creates a data type that represents the composite type corresponding to one row of the table. Therefore, tables can't have the same name as any existing data type in the same schema.
The PARALLEL
clause sets the degree of parallelism for a table. If you don't specify the PARALLEL
clause, the server determines a value based on the relation size.
The NOPARALLEL
clause resets the parallelism for default execution, and reloptions
shows the parallel_workers
parameter as 0
.
A table can't have more than 1600 columns. In practice, the effective limit is lower because of tuple-length constraints.
The optional constraint clauses specify constraints or tests that new or updated rows must satisfy for an insert or update operation to succeed. A constraint is a SQL object that helps define the set of valid values in the table in various ways.
You can define table constraints and column constraints. A column constraint is part of a column definition. A table constraint definition isn't tied to a particular column, and it can encompass more than one column. You can also write every column constraint as a table constraint. A column constraint is a notational convenience only if the constraint affects only one column.
Note
EDB Postgres Advanced Server allows you to create rowids on a foreign table by specifying either the WITH (ROWIDS)
or WITH (ROWIDS=true)
option in the CREATE FOREIGN TABLE
syntax. Specifying the WITH (ROWIDS)
or WITH (ROWIDS=true)
option adds a rowid column to a foreign table. For information about CREATE FOREIGN TABLE
, see the PostgreSQL core documentation.
Parameters
GLOBAL TEMPORARY
Creates the table as a temporary table. Temporary tables are dropped at the end of a session or, optionally, at the end of the current transaction. (See the ON COMMIT
parameter.) Existing permanent tables with the same name aren't visible to the current session while the temporary table exists unless you reference them with schema-qualified names. In addition, temporary tables aren't visible outside the session in which you created them. This aspect of global temporary tables isn't compatible with Oracle databases. Any indexes created on a temporary table are also temporary.
UNLOGGED
Creates the table as an unlogged table. The data written to unlogged tables isn't written to the write-ahead log (WAL), making them faster than an ordinary table. Indexes created on an unlogged table are unlogged. The contents of an unlogged table aren't replicated to a standby server. The unlogged table is not crash-safe. It's truncated after a crash or shutdown.
table_name
The name (optionally schema-qualified) of the table to create.
column_name
The name of a column to create in the new table.
data_type
The data type of the column. This can include array specifiers. For more information on the data types included with EDB Postgres Advanced Server, see SQL reference.
DEFAULT default_expr
The DEFAULT
clause assigns a default data value for the column whose column definition it appears in. The value is any variable-free expression. Subqueries and cross references to other columns in the current table aren't allowed. The data type of the default expression must match the data type of the column.
The default expression is used in any insert operation that doesn't specify a value for the column. If you don't specify a default for a column, then the default is null
.
CONSTRAINT constraint_name
An optional name for a column or table constraint. If you don't specify one, the system generates a name.
NOT NULL
The column can't contain null values.
NULL
The column can contain null values. This is the default.
This clause is available only for compatibility with nonstandard SQL databases. We don't recommend using it in new applications.
UNIQUE
— Column constraint.
UNIQUE (column_name [, ...] )
— Table constraint.
The UNIQUE
constraint specifies that a group of one or more distinct columns of a table can contain only unique values. The behavior of the unique table constraint is the same as that for column constraints. However, the unique table constraint can span multiple columns.
For the purpose of a unique constraint, null values aren't considered equal.
Each unique table constraint must name a set of columns that's different from the set of columns named by any other unique or primary key constraint defined for the table. Otherwise it's the same constraint listed twice.
PRIMARY KEY
— Column constraint.
PRIMARY KEY ( column_name [, ...] )