Newer
Older
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_table.sgml,v 1.22 2000/04/13 07:19:27 momjian Exp $
Postgres documentation
-->
<refentry id="SQL-CREATETABLE">
<refmeta>
<refentrytitle id="sql-createtable-title">
</refentrytitle>
<refmiscinfo>SQL - Language Statements</refmiscinfo>
</refmeta>
<refnamediv>
<refname>
Creates a new table
</refnamediv>
</refsynopsisdivinfo>
<synopsis>
CREATE [ TEMPORARY | TEMP ] TABLE <replaceable class="PARAMETER">table</replaceable> (
<replaceable class="PARAMETER">column</replaceable> <replaceable class="PARAMETER">type</replaceable>
[ NULL | NOT NULL ] [ UNIQUE ] [ DEFAULT <replaceable class="PARAMETER">value</replaceable> ]
[<replaceable>column_constraint_clause</replaceable> | PRIMARY KEY } [ ... ] ]
[, PRIMARY KEY ( <replaceable class="PARAMETER">column</replaceable> [, ...] ) ]
[, CHECK ( <replaceable class="PARAMETER">condition</replaceable> ) ]
[, <replaceable>table_constraint_clause</replaceable> ]
) [ INHERITS ( <replaceable>inherited_table</replaceable> [, ...] ) ]
</synopsis>
<refsect2 id="R2-SQL-CREATETABLE-1">
<title>
<variablelist>
<varlistentry>
<term>TEMPORARY</term>
<listitem>
<para>
The table is created only for this session, and is
automatically dropped on session exit.
Existing permanent tables with the same name are not visible
while the temporary table exists.
</para>
</listitem>
</varlistentry>
<term><replaceable class="PARAMETER">table</replaceable></term>
<listitem>
<para>
The name of a new class or table to be created.
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">column</replaceable></term>
<listitem>
<para>
The name of a column.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">type</replaceable></term>
<listitem>
<para>
The type of the column. This may include array specifiers.
Refer to the <citetitle>PostgreSQL User's Guide</citetitle> for
further information about data types and arrays.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>DEFAULT <replaceable class="PARAMETER">value</replaceable></term>
<listitem>
<para>
A default value for a column.
See the DEFAULT clause for more information.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable>column_constraint_clause</replaceable></term>
<listitem>
<para>
The optional column constraint clauses specify a list of integrity
constraints or tests which new or updated entries must satisfy for
an insert or update operation to succeed. Each constraint
must evaluate to a boolean expression. Although <acronym>SQL92</acronym>
requires the <replaceable class="PARAMETER">column_constraint_clause</replaceable>
to refer to that column only, <productname>Postgres</productname>
allows multiple columns
to be referenced within a single column constraint.
See the column constraint clause for more information.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable>table_constraint_clause</replaceable></term>
<listitem>
<para>
The optional table CONSTRAINT clause specifies a list of integrity
constraints which new or updated entries must satisfy for
an insert or update operation to succeed. Each constraint
must evaluate to a boolean expression. Multiple columns
may be referenced within a single constraint.
Only one PRIMARY KEY clause may be specified for a table;
PRIMARY KEY <replaceable>column</replaceable>
(a table constraint) and PRIMARY KEY (a column constraint) are
mutually exclusive..
See the table constraint clause for more information.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>INHERITS <replaceable class="PARAMETER">inherited_table</replaceable></term>
The optional INHERITS clause specifies a collection of table
names from which this table automatically inherits all fields.
If any inherited field name appears more than once,
<productname>Postgres</productname>
reports an error.
<productname>Postgres</productname> automatically allows the created
table to inherit functions on tables above it in the inheritance
hierarchy.
<note>
<title>Aside</title>
<para>
Inheritance of functions is done according
to the conventions of the Common Lisp Object System (CLOS).
</para>
</note>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
<refsect2 id="R2-SQL-CREATETABLE-2">
<title>
<variablelist>
<varlistentry>
<term><computeroutput>
CREATE
</computeroutput></term>
<listitem>
<para>
Message returned if table is successfully created.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><computeroutput>
ERROR
</computeroutput></term>
<listitem>
<para>
Message returned if table creation failed.
This is usually accompanied by some descriptive text, such as:
<computeroutput>
ERROR: Relation '<replaceable class="parameter">table</replaceable>' already exists
</computeroutput>
which occurs at runtime, if the table specified already exists
in the database.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><computeroutput>
ERROR: DEFAULT: type mismatched
</computeroutput></term>
<listitem>
<para>
If data type of default value doesn't match the
column definition's data type.
</para>
</listitem>
</varlistentry>
</para>
<refsect1 id="R1-SQL-CREATETABLE-1">
<title>
<command>CREATE TABLE</command> will enter a new class or table
into the current data base. The table will be "owned" by the user issuing the
</para>
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<para>
Each <replaceable class="PARAMETER">type</replaceable>
may be a simple type, a complex type (set) or an array type.
Each attribute may be specified to be non-null and
each may have a default value, specified by the
<xref linkend="R1-SQL-DEFAULTCLAUSE-1-TITLE" endterm="R1-SQL-DEFAULTCLAUSE-1-TITLE">.
</para>
<para>
<note>
<para>
As of Postgres version 6.0, consistant array dimensions within an
attribute are not enforced. This will likely change in a future
release.
</para>
</note>
</para>
<para>
The optional INHERITS
clause specifies a collection of class names from which this class
automatically inherits all fields. If any inherited field name
appears more than once, Postgres reports an error. Postgres automatically
allows the created class to inherit functions on classes above it in
the inheritance hierarchy. Inheritance of functions is done according
to the conventions of the Common Lisp Object System (CLOS).
</para>
<para>
Each new table or class <replaceable class="PARAMETER">table</replaceable>
is automatically created as a type. Therefore, one or more instances
from the class are automatically a type and can be used in
<xref linkend="sql-altertable-title" endterm="sql-altertable-title">
or other <command>CREATE TABLE</command> statements.
</para>
The new table is created as a heap with no initial data.
A table can have no more than 1600 columns (realistically,
this is limited by the fact that tuple sizes must
be less than 8192 bytes), but this limit may be configured
lower at some sites. A table cannot have the same name as
a system catalog table.
</refsect1>
<title id="R1-SQL-DEFAULTCLAUSE-1-TITLE">
DEFAULT Clause
</title>
<para>
<synopsis>
DEFAULT <replaceable class="PARAMETER">value</replaceable>
</para>
<refsect2 id="R2-SQL-DEFAULTCLAUSE-1">
<title>
Inputs
</title>
<para>
<variablelist>
<varlistentry>
<term><replaceable class="parameter">value</replaceable></term>
<listitem>
<para>
The possible values for the default value expression are:
<itemizedlist>
<listitem>
<simpara>
a literal value
</simpara>
</listitem>
<simpara>
a user function
</simpara>
</listitem>
<listitem>
<simpara>
a niladic function
</simpara>
</itemizedlist>
</para>
</listitem>
</variablelist>
</para>
</refsect2>
<refsect2 id="R2-SQL-DEFAULTCLAUSE-2">
<title>
Outputs
<para>
</para>
</refsect2>
<refsect2 id="R2-SQL-DEFAULTCLAUSE-3">
<title>
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
Description
</title>
<para>
The DEFAULT clause assigns a default data value to a column
(via a column definition in the CREATE TABLE statement).
The data type of a default value must match the column definition's
data type.
</para>
<para>
An INSERT operation that includes a column without a specified
default value will assign the NULL value to the column
if no explicit data value is provided for it.
Default <replaceable class="parameter">literal</replaceable> means
that the default is the specified constant value.
Default <replaceable class="parameter">niladic-function</replaceable>
or <replaceable class="parameter">user-function</replaceable> means
that the default
is the value of the specified function at the time of the INSERT.
</para>
<para>
There are two types of niladic functions:
<variablelist>
<varlistentry>
<term>niladic USER</term>
<listitem>
<variablelist>
<varlistentry>
<term>CURRENT_USER / USER</term>
<listitem>
<simpara>See CURRENT_USER function</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>SESSION_USER</term>
<listitem>
<simpara>not yet supported</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>SYSTEM_USER</term>
<listitem>
<simpara>not yet supported</simpara>
</listitem>
</varlistentry>
</variablelist>
</listitem>
</varlistentry>
<varlistentry>
<term>niladic datetime</term>
<listitem>
<variablelist>
<varlistentry>
<term>CURRENT_DATE</term>
<listitem>
<simpara>See CURRENT_DATE function</simpara>
</listitem>
</varlistentry>
<term>CURRENT_TIME</term>
<simpara>See CURRENT_TIME function</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>CURRENT_TIMESTAMP</term>
<simpara>See CURRENT_TIMESTAMP function</simpara>
</listitem>
</varlistentry>
</variablelist>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect2>
<refsect2 id="R2-SQL-DEFAULTCLAUSE-4">
<title>
To assign a constant value as the default for the
columns <literal>did</literal> and <literal>number</literal>,
and a string literal to the column <literal>did</literal>:
<programlisting>
CREATE TABLE video_sales (
did VARCHAR(40) DEFAULT 'luso films',
number INTEGER DEFAULT 0,
total CASH DEFAULT '$0.0'
);
</para>
To assign an existing sequence
as the default for the column <literal>did</literal>,
and a literal to the column <literal>name</literal>:
<programlisting>
CREATE TABLE distributors (
did DECIMAL(3) DEFAULT NEXTVAL('serial'),
name VARCHAR(40) DEFAULT 'luso films'
);
</para>
</refsect2>
</refsect1>
<refsect1 id="R1-SQL-COLUMNCONSTRAINT-1">
<title id="R1-SQL-COLUMNCONSTRAINT-1-TITLE">
</title>
<para>
<synopsis>
[ CONSTRAINT <replaceable class="parameter">name</replaceable> ] { [
NULL | NOT NULL ] | UNIQUE | PRIMARY KEY | CHECK <replaceable
class="parameter">constraint</replaceable> | REFERENCES
<replaceable class="parameter">reftable</replaceable>
(<replaceable class="parameter">refcolumn</replaceable>)
[ MATCH <replaceable class="parameter">matchtype</replaceable> ]
[ ON DELETE <replaceable class="parameter">action</replaceable> ]
[ ON UPDATE <replaceable class="parameter">action</replaceable> ]
[ [ NOT ] DEFERRABLE ]
[ INITIALLY <replaceable class="parameter">checktime</replaceable> ] }
</para>
<refsect2 id="R2-SQL-COLUMNCONSTRAINT-1">
<title>
<variablelist>
<varlistentry>
<term><replaceable class="parameter">name</replaceable></term>
<listitem>
<para>
An arbitrary name given to the integrity constraint.
If <replaceable class="parameter">name</replaceable> is not specified,
it is generated from the table and column names,
which should ensure uniqueness for
<replaceable class="parameter">name</replaceable>.
</para>
</listitem>
</varlistentry>
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<varlistentry>
<term>NULL</term>
<listitem>
<para>
The column is allowed to contain NULL values. This is the default.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>NOT NULL</term>
<listitem>
<para>
The column is not allowed to contain NULL values.
This is equivalent to the column constraint
CHECK (<replaceable class="PARAMETER">column</replaceable> NOT NULL).
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>UNIQUE</term>
<listitem>
<para>
The column must have unique values. In <productname>Postgres</productname>
this is enforced by an implicit creation of a unique index on the table.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>PRIMARY KEY</term>
<listitem>
<para>
This column is a primary key, which implies that uniqueness is
enforced by the system and that other tables may rely on this column
as a unique identifier for rows.
See PRIMARY KEY for more information.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<replaceable class="parameter">constraint</replaceable>
</term>
<listitem>
<para>
The definition of the constraint.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect2>
<refsect2 id="R2-SQL-COLUMNCONSTRAINT-2">
<title>
The optional constraint
clauses specify constraints or tests which new or updated entries
must satisfy for an insert or update operation to succeed. Each constraint
must evaluate to a boolean expression. Multiple attributes may be referenced within
a single constraint. The use of PRIMARY KEY
as a table constraint
is mutually incompatible with PRIMARY KEY as a column constraint.
</para>
<para>
A constraint is a named rule: an SQL object which helps define
valid sets of values by putting limits on the results of INSERT,
UPDATE or DELETE operations performed on a Base Table.
</para>
<para>
There are two ways to define integrity constraints:
table constraints, covered later, and column constraints, covered here.
</para>
<para>
A column constraint is an integrity constraint defined as part
of a column definition, and logically becomes a table
constraint as soon as it is created. The column
constraints available are:
<simplelist columns="1">
<member>PRIMARY KEY</member>
<member>REFERENCES</member>
<member>UNIQUE</member>
<member>CHECK</member>
<member>NOT NULL</member>
<productname>Postgres</productname> now
(new for v7.0) supports
REFERENCES integrity constraints.
</refsect2>
<refsect2 id="R2-SQL-NOTNULL-1">
<title>
[ CONSTRAINT <replaceable class="parameter">name</replaceable> ] NOT NULL
The NOT NULL constraint specifies a rule that a column may
contain only non-null values.
This is a column constraint only, and not allowed
as a table constraint.
</para>
<refsect3 id="R3-SQL-NOTNULL-1">
<title>
Outputs
<variablelist>
<varlistentry>
<term><replaceable>status</replaceable></term>
<listitem>
<para>
<variablelist>
<varlistentry>
<term><computeroutput>
ERROR: ExecAppend: Fail to add null value in not null attribute "<replaceable class="parameter">column</replaceable>".
</computeroutput></term>
<listitem>
<para>
This error occurs at runtime if one tries to insert a null value
into a column which has a NOT NULL constraint.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect3>
<refsect3 id="R3-SQL-NOTNULL-2">
<title>
Description
</title>
<para>
</para>
</refsect3>
<refsect3 id="R3-SQL-NOTNULL-3">
<title>
Usage
</title>
Define two NOT NULL column constraints on the table
<classname>distributors</classname>,
one of which being a named constraint:
<programlisting>
CREATE TABLE distributors (
did DECIMAL(3) CONSTRAINT no_null NOT NULL,
name VARCHAR(40) NOT NULL
);
</para>
</refsect3>
</refsect2>
<refsect2 id="R2-SQL-UNIQUECLAUSE-1">
<title>
<synopsis>
[ CONSTRAINT <replaceable class="parameter">name</replaceable> ] UNIQUE
<refsect3>
<title>Inputs</title>
<para>
<variablelist>
<varlistentry>
<term>CONSTRAINT <replaceable class="parameter">name</replaceable></term>
<listitem>
<para>
An arbitrary label given to a constraint.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
<variablelist>
<varlistentry>
<term><replaceable>status</replaceable></term>
<listitem>
<para>
<variablelist>
<varlistentry>
<term><computeroutput>
ERROR: Cannot insert a duplicate key into a unique index.
</computeroutput></term>
<listitem>
<para>
This error occurs at runtime if one tries to insert a
duplicate value into a column.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
The UNIQUE constraint specifies a rule that a group of one or
more distinct columns of a table may contain only unique values.
</para>
<para>
The column definitions of the specified columns do not have to
include a NOT NULL constraint to be included in a UNIQUE
constraint. Having more than one null value in a column without a
NOT NULL constraint, does not violate a UNIQUE constraint.
(This deviates from the <acronym>SQL92</acronym> definition, but
is a more sensible convention. See the section on compatibility
for more details.).
Each UNIQUE column constraint must name a column that is
different from the set of columns named by any other UNIQUE or
PRIMARY KEY constraint defined for the table.
<productname>Postgres</productname> automatically creates a unique
index for each UNIQUE constraint, to assure
data integrity. See CREATE INDEX for more information.
</refsect3>
<refsect3 id="R3-SQL-UNIQUECLAUSE-3">
<title>
Usage
</title>
<para>
Defines a UNIQUE column constraint for the table distributors.
UNIQUE column constraints can only be defined on one column
of the table:
<programlisting>
CREATE TABLE distributors (
did DECIMAL(3),
name VARCHAR(40) UNIQUE
);
which is equivalent to the following specified as a table constraint:
<programlisting>
CREATE TABLE distributors (
did DECIMAL(3),
name VARCHAR(40),
UNIQUE(name)
);
</para>
</refsect3>
</refsect2>
<title>
The CHECK Constraint
</title>
<synopsis>
[ CONSTRAINT <replaceable class="parameter">name</replaceable> ] CHECK
( <replaceable>condition</replaceable> [, ...] )
<refsect3 id="R3-SQL-CHECK-1">
<title>Inputs</title>
<para>
<variablelist>
<varlistentry>
<term><replaceable class="parameter">name</replaceable></term>
<listitem>
<para>
An arbitrary name given to a constraint.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable>condition</replaceable></term>
Any valid conditional expression evaluating to a boolean result.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
<refsect3 id="R3-SQL-CHECK-2">
<title>
Outputs
<term><replaceable>status</replaceable></term>
<term><computeroutput>
ERROR: ExecAppend: rejected due to CHECK constraint "<replaceable class="parameter">table_column</replaceable>".
</computeroutput></term>
This error occurs at runtime if one tries to insert an illegal
value into a column subject to a CHECK constraint.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</para>
<refsect3>
<title>Description</title>
<para>
The CHECK constraint specifies a restriction on allowed values
within a column.
The CHECK constraint is also allowed as a table constraint.
The SQL92 CHECK column constraints can only be defined on, and
refer to, one column of the table. <productname>Postgres</productname>
does not have
<refsect2 id="R2-SQL-PRIMARYKEY-1">
<title>
</title>
<synopsis>
[ CONSTRAINT <replaceable class="PARAMETER">name</replaceable> ] PRIMARY KEY
</synopsis>
<para>
<variablelist>
<varlistentry>
<term>CONSTRAINT <replaceable class="PARAMETER">name</replaceable></term>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect3>
<refsect3>
<title>Outputs</title>
<variablelist>
<varlistentry>
<term><computeroutput>
ERROR: Cannot insert a duplicate key into a unique index.
</computeroutput></term>
<listitem>
<para>
This occurs at run-time if one tries to insert a duplicate value into
a column subject to a PRIMARY KEY constraint.
</listitem>
</varlistentry>
</variablelist>
</refsect3>
<refsect3>
<title>Description</title>
The PRIMARY KEY column constraint specifies that a column of a table
may contain only unique
(non-duplicate), non-NULL values. The definition of
the specified column does not have to include an explicit NOT NULL
constraint to be included in a PRIMARY KEY constraint.
Only one PRIMARY KEY can be specified for a table.
<refsect3 id="R3-SQL-PRIMARYKEY-3">
<title>
<productname>Postgres</productname> automatically creates
a unique index to assure
data integrity. (See CREATE INDEX statement)
The PRIMARY KEY constraint should name a set of columns that is
different from other sets of columns named by any UNIQUE constraint
defined for the same table, since it will result in duplication
of equivalent indexes and unproductive additional runtime overhead.
However, <productname>Postgres</productname> does not specifically
disallow this.
</refsect2>
<refsect2 id="R2-SQL-REFERENCES-1">
<refsect2info>
<date>2000-02-04</date>
</refsect2info>
<title>
REFERENCES Constraint
</title>
<synopsis>
[ CONSTRAINT <replaceable class="parameter">name</replaceable> ] REFERENCES
<replaceable class="parameter">reftable</replaceable> [ ( <replaceable class="parameter">refcolumn</replaceable> ) ]
[ MATCH <replaceable class="parameter">matchtype</replaceable> ]
[ ON DELETE <replaceable class="parameter">action</replaceable> ]
[ ON UPDATE <replaceable class="parameter">action</replaceable> ]
[ [ NOT ] DEFERRABLE ]
[ INITIALLY <replaceable class="parameter">checktime</replaceable> ]
</synopsis>
<para>
The REFERENCES constraint specifies a rule that a column
value is checked against the values of another column.
REFERENCES can also be specified as part of
a FOREIGN KEY table constraint.
</para>
<refsect3 id="R3-SQL-REFERENCES-1">
<title>Inputs</title>
<para>
<variablelist>
<varlistentry>
<term>CONSTRAINT <replaceable class="PARAMETER">name</replaceable></term>
<listitem>
<para>
An arbitrary name for the constraint.
</para>
</listitem>
</varlistentry>
<term><replaceable class="parameter">reftable</replaceable></term>
<listitem>
<para>
The table that contains the data to check against.
</para>
</listitem>
</varlistentry>
<term><replaceable class="parameter">refcolumn</replaceable></term>
<listitem>
<para>
The column in <replaceable class="parameter">reftable</replaceable>
to check the data against. If this is not specified, the PRIMARY KEY of the
<replaceable class="parameter">reftable</replaceable> is used.
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>MATCH <replaceable class="parameter">matchtype</replaceable></term>
<listitem>
<para>
The type of comparison to do between the table data. There are three
types of matching, MATCH FULL, MATCH PARTIAL, and the unspecified match type
used if no match type is specified.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>ON DELETE <replaceable class="parameter">action</replaceable></term>
<listitem>
<para>
The action to do when a referenced row in the referenced table is being
deleted. There are the following actions.
<variablelist>
<varlistentry>
<term>CASCADE</term>
<listitem>
<para>
Delete any rows referencing the deleted row.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>RESTRICT</term>
<listitem>