3 PostgreSQL documentation
6 <refentry id=
"SQL-CREATETABLE">
8 <refentrytitle id=
"sql-createtable-title">CREATE TABLE
</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
14 <refname>CREATE TABLE
</refname>
15 <refpurpose>define a new table
</refpurpose>
18 <indexterm zone=
"sql-createtable">
19 <primary>CREATE TABLE
</primary>
24 CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE
<replaceable class=
"PARAMETER">table_name
</replaceable> ( [
25 {
<replaceable class=
"PARAMETER">column_name
</replaceable> <replaceable class=
"PARAMETER">data_type
</replaceable> [ DEFAULT
<replaceable>default_expr<
/> ] [
<replaceable class=
"PARAMETER">column_constraint
</replaceable> [ ... ] ]
26 |
<replaceable>table_constraint
</replaceable>
27 | LIKE
<replaceable>parent_table
</replaceable> [ { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS | INDEXES } ] ... }
30 [ INHERITS (
<replaceable>parent_table
</replaceable> [, ... ] ) ]
31 [ WITH (
<replaceable class=
"PARAMETER">storage_parameter
</replaceable> [=
<replaceable class=
"PARAMETER">value
</replaceable>] [, ... ] ) | WITH OIDS | WITHOUT OIDS ]
32 [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
33 [ TABLESPACE
<replaceable class=
"PARAMETER">tablespace
</replaceable> ]
35 where
<replaceable class=
"PARAMETER">column_constraint
</replaceable> is:
37 [ CONSTRAINT
<replaceable class=
"PARAMETER">constraint_name
</replaceable> ]
40 UNIQUE
<replaceable class=
"PARAMETER">index_parameters
</replaceable> |
41 PRIMARY KEY
<replaceable class=
"PARAMETER">index_parameters
</replaceable> |
42 CHECK (
<replaceable class=
"PARAMETER">expression
</replaceable> ) |
43 REFERENCES
<replaceable class=
"PARAMETER">reftable
</replaceable> [ (
<replaceable class=
"PARAMETER">refcolumn
</replaceable> ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
44 [ ON DELETE
<replaceable class=
"parameter">action
</replaceable> ] [ ON UPDATE
<replaceable class=
"parameter">action
</replaceable> ] }
45 [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
47 and
<replaceable class=
"PARAMETER">table_constraint
</replaceable> is:
49 [ CONSTRAINT
<replaceable class=
"PARAMETER">constraint_name
</replaceable> ]
50 { UNIQUE (
<replaceable class=
"PARAMETER">column_name
</replaceable> [, ... ] )
<replaceable class=
"PARAMETER">index_parameters
</replaceable> |
51 PRIMARY KEY (
<replaceable class=
"PARAMETER">column_name
</replaceable> [, ... ] )
<replaceable class=
"PARAMETER">index_parameters
</replaceable> |
52 CHECK (
<replaceable class=
"PARAMETER">expression
</replaceable> ) |
53 FOREIGN KEY (
<replaceable class=
"PARAMETER">column_name
</replaceable> [, ... ] ) REFERENCES
<replaceable class=
"PARAMETER">reftable
</replaceable> [ (
<replaceable class=
"PARAMETER">refcolumn
</replaceable> [, ... ] ) ]
54 [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE
<replaceable class=
"parameter">action
</replaceable> ] [ ON UPDATE
<replaceable class=
"parameter">action
</replaceable> ] }
55 [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
57 <replaceable class=
"PARAMETER">index_parameters
</replaceable> in
<literal>UNIQUE<
/> and
<literal>PRIMARY KEY<
/> constraints are:
59 [ WITH (
<replaceable class=
"PARAMETER">storage_parameter
</replaceable> [=
<replaceable class=
"PARAMETER">value
</replaceable>] [, ... ] ) ]
60 [ USING INDEX TABLESPACE
<replaceable class=
"PARAMETER">tablespace
</replaceable> ]
65 <refsect1 id=
"SQL-CREATETABLE-description">
66 <title>Description
</title>
69 <command>CREATE TABLE
</command> will create a new, initially empty table
70 in the current database. The table will be owned by the user issuing the
75 If a schema name is given (for example,
<literal>CREATE TABLE
76 myschema.mytable ...<
/>) then the table is created in the specified
77 schema. Otherwise it is created in the current schema. Temporary
78 tables exist in a special schema, so a schema name cannot be given
79 when creating a temporary table. The name of the table must be
80 distinct from the name of any other table, sequence, index, or view
85 <command>CREATE TABLE
</command> also automatically creates a data
86 type that represents the composite type corresponding
87 to one row of the table. Therefore, tables cannot have the same
88 name as any existing data type in the same schema.
92 The optional constraint clauses specify constraints (tests) that
93 new or updated rows must satisfy for an insert or update operation
94 to succeed. A constraint is an SQL object that helps define the
95 set of valid values in the table in various ways.
99 There are two ways to define constraints: table constraints and
100 column constraints. A column constraint is defined as part of a
101 column definition. A table constraint definition is not tied to a
102 particular column, and it can encompass more than one column.
103 Every column constraint can also be written as a table constraint;
104 a column constraint is only a notational convenience for use when the
105 constraint only affects one column.
110 <title>Parameters
</title>
115 <term><literal>TEMPORARY<
/> or
<literal>TEMP<
/></term>
118 If specified, the table is created as a temporary table.
119 Temporary tables are automatically dropped at the end of a
120 session, or optionally at the end of the current transaction
121 (see
<literal>ON COMMIT
</literal> below). Existing permanent
122 tables with the same name are not visible to the current session
123 while the temporary table exists, unless they are referenced
124 with schema-qualified names. Any indexes created on a temporary
125 table are automatically temporary as well.
129 Optionally,
<literal>GLOBAL
</literal> or
<literal>LOCAL
</literal>
130 can be written before
<literal>TEMPORARY<
/> or
<literal>TEMP<
/>.
131 This makes no difference in
<productname>PostgreSQL<
/>, but see
132 <xref linkend=
"sql-createtable-compatibility"
133 endterm=
"sql-createtable-compatibility-title">.
139 <term><replaceable class=
"PARAMETER">table_name
</replaceable></term>
142 The name (optionally schema-qualified) of the table to be created.
148 <term><replaceable class=
"PARAMETER">column_name
</replaceable></term>
151 The name of a column to be created in the new table.
157 <term><replaceable class=
"PARAMETER">data_type
</replaceable></term>
160 The data type of the column. This can include array
161 specifiers. For more information on the data types supported by
162 <productname>PostgreSQL
</productname>, refer to
<xref
169 <term><literal>DEFAULT
170 <replaceable>default_expr
</replaceable></literal></term>
173 The
<literal>DEFAULT<
/> clause assigns a default data value for
174 the column whose column definition it appears within. The value
175 is any variable-free expression (subqueries and cross-references
176 to other columns in the current table are not allowed). The
177 data type of the default expression must match the data type of the
182 The default expression will be used in any insert operation that
183 does not specify a value for the column. If there is no default
184 for a column, then the default is null.
190 <term><literal>INHERITS (
<replaceable>parent_table
</replaceable> [, ... ] )
</literal></term>
193 The optional
<literal>INHERITS<
/> clause specifies a list of
194 tables from which the new table automatically inherits all
199 Use of
<literal>INHERITS<
/> creates a persistent relationship
200 between the new child table and its parent table(s). Schema
201 modifications to the parent(s) normally propagate to children
202 as well, and by default the data of the child table is included in
203 scans of the parent(s).
207 If the same column name exists in more than one parent
208 table, an error is reported unless the data types of the columns
209 match in each of the parent tables. If there is no conflict,
210 then the duplicate columns are merged to form a single column in
211 the new table. If the column name list of the new table
212 contains a column name that is also inherited, the data type must
213 likewise match the inherited column(s), and the column
214 definitions are merged into one. If the
215 new table explicitly specifies a default value for the column,
216 this default overrides any defaults from inherited declarations
217 of the column. Otherwise, any parents that specify default
218 values for the column must all specify the same default, or an
219 error will be reported.
223 <literal>CHECK<
/> constraints are merged in essentially the same way as
224 columns: if multiple parent tables and/or the new table definition
225 contain identically-named
<literal>CHECK<
/> constraints, these
226 constraints must all have the same check expression, or an error will be
227 reported. Constraints having the same name and expression will
228 be merged into one copy. Notice that an unnamed
<literal>CHECK<
/>
229 constraint in the new table will never be merged, since a unique name
230 will always be chosen for it.
235 <productname>PostgreSQL</> automatically allows the
236 created table to inherit
237 functions on tables above it in the inheritance hierarchy; that
238 is, if we create table <literal>foo</literal> inheriting from
239 <literal>bar</literal>, then functions that accept the tuple
240 type <literal>bar</literal> can also be applied to instances of
241 <literal>foo</literal>. (Currently, this works reliably for
242 functions on the first or only parent table, but not so well for
243 functions on additional parents.)
250 <term><literal>LIKE
<replaceable>parent_table
</replaceable> [ { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS | INDEXES } ]
</literal></term>
253 The
<literal>LIKE
</literal> clause specifies a table from which
254 the new table automatically copies all column names, their data types,
255 and their not-null constraints.
258 Unlike
<literal>INHERITS
</literal>, the new table and original table
259 are completely decoupled after creation is complete. Changes to the
260 original table will not be applied to the new table, and it is not
261 possible to include data of the new table in scans of the original
265 Default expressions for the copied column definitions will only be
266 copied if
<literal>INCLUDING DEFAULTS
</literal> is specified. The
267 default behavior is to exclude default expressions, resulting in the
268 copied columns in the new table having null defaults.
271 Not-null constraints are always copied to the new table.
272 <literal>CHECK
</literal> constraints will only be copied if
273 <literal>INCLUDING CONSTRAINTS
</literal> is specified; other types of
274 constraints will never be copied. Also, no distinction is made between
275 column constraints and table constraints
— when constraints are
276 requested, all check constraints are copied.
279 Any indexes on the original table will not be created on the new
280 table, unless the
<literal>INCLUDING INDEXES
</literal> clause is
284 Note also that unlike
<literal>INHERITS
</literal>, copied columns and
285 constraints are not merged with similarly named columns and constraints.
286 If the same name is specified explicitly or in another
287 <literal>LIKE
</literal> clause, an error is signalled.
293 <term><literal>CONSTRAINT
<replaceable class=
"PARAMETER">constraint_name
</replaceable></literal></term>
296 An optional name for a column or table constraint. If the
297 constraint is violated, the constraint name is present in error messages,
298 so constraint names like
<literal>col must be positive<
/> can be used
299 to communicate helpful constraint information to client applications.
300 (Double-quotes are needed to specify constraint names that contain spaces.)
301 If a constraint name is not specified, the system generates a name.
307 <term><literal>NOT NULL<
/></term>
310 The column is not allowed to contain null values.
316 <term><literal>NULL<
/></term>
319 The column is allowed to contain null values. This is the default.
323 This clause is only provided for compatibility with
324 non-standard SQL databases. Its use is discouraged in new
331 <term><literal>UNIQUE<
/> (column constraint)
</term>
332 <term><literal>UNIQUE (
<replaceable class=
"PARAMETER">column_name
</replaceable> [, ... ] )<
/> (table constraint)
</term>
336 The
<literal>UNIQUE
</literal> constraint specifies that a
337 group of one or more columns of a table can contain
338 only unique values. The behavior of the unique table constraint
339 is the same as that for column constraints, with the additional
340 capability to span multiple columns.
344 For the purpose of a unique constraint, null values are not
349 Each unique table constraint must name a set of columns that is
350 different from the set of columns named by any other unique or
351 primary key constraint defined for the table. (Otherwise it
352 would just be the same constraint listed twice.)
358 <term><literal>PRIMARY KEY<
/> (column constraint)
</term>
359 <term><literal>PRIMARY KEY (
<replaceable class=
"PARAMETER">column_name
</replaceable> [, ... ] )<
/> (table constraint)
</term>
362 The primary key constraint specifies that a column or columns of a table
363 can contain only unique (non-duplicate), nonnull values.
364 Technically,
<literal>PRIMARY KEY
</literal> is merely a
365 combination of
<literal>UNIQUE<
/> and
<literal>NOT NULL<
/>, but
366 identifying a set of columns as primary key also provides
367 metadata about the design of the schema, as a primary key
368 implies that other tables
369 can rely on this set of columns as a unique identifier for rows.
373 Only one primary key can be specified for a table, whether as a
374 column constraint or a table constraint.
378 The primary key constraint should name a set of columns that is
379 different from other sets of columns named by any unique
380 constraint defined for the same table.
386 <term><literal>CHECK (
<replaceable class=
"PARAMETER">expression
</replaceable> )
</literal></term>
389 The
<literal>CHECK<
/> clause specifies an expression producing a
390 Boolean result which new or updated rows must satisfy for an
391 insert or update operation to succeed. Expressions evaluating
392 to TRUE or UNKNOWN succeed. Should any row of an insert or
393 update operation produce a FALSE result an error exception is
394 raised and the insert or update does not alter the database. A
395 check constraint specified as a column constraint should
396 reference that column's value only, while an expression
397 appearing in a table constraint can reference multiple columns.
401 Currently,
<literal>CHECK
</literal> expressions cannot contain
402 subqueries nor refer to variables other than columns of the
410 <term><literal>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> ]
</literal> (column constraint)
</term>
412 <term><literal>FOREIGN KEY (
<replaceable class=
"parameter">column
</replaceable> [, ... ] )
413 REFERENCES
<replaceable class=
"parameter">reftable
</replaceable> [ (
<replaceable class=
"parameter">refcolumn
</replaceable> [, ... ] ) ]
414 [ MATCH
<replaceable class=
"parameter">matchtype
</replaceable> ]
415 [ ON DELETE
<replaceable class=
"parameter">action
</replaceable> ]
416 [ ON UPDATE
<replaceable class=
"parameter">action
</replaceable> ]
</literal>
417 (table constraint)
</term>
421 These clauses specify a foreign key constraint, which requires
422 that a group of one or more columns of the new table must only
423 contain values that match values in the referenced
424 column(s) of some row of the referenced table. If
<replaceable
425 class=
"parameter">refcolumn
</replaceable> is omitted, the
426 primary key of the
<replaceable
427 class=
"parameter">reftable
</replaceable> is used. The
428 referenced columns must be the columns of a unique or primary
429 key constraint in the referenced table. Note that foreign key
430 constraints cannot be defined between temporary tables and
435 A value inserted into the referencing column(s) is matched against the
436 values of the referenced table and referenced columns using the
437 given match type. There are three match types:
<literal>MATCH
438 FULL<
/>,
<literal>MATCH PARTIAL<
/>, and
<literal>MATCH
439 SIMPLE
</literal>, which is also the default.
<literal>MATCH
440 FULL<
/> will not allow one column of a multicolumn foreign key
441 to be null unless all foreign key columns are null.
442 <literal>MATCH SIMPLE
</literal> allows some foreign key columns
443 to be null while other parts of the foreign key are not
444 null.
<literal>MATCH PARTIAL<
/> is not yet implemented.
448 In addition, when the data in the referenced columns is changed,
449 certain actions are performed on the data in this table's
450 columns. The
<literal>ON DELETE
</literal> clause specifies the
451 action to perform when a referenced row in the referenced table is
452 being deleted. Likewise, the
<literal>ON UPDATE
</literal>
453 clause specifies the action to perform when a referenced column
454 in the referenced table is being updated to a new value. If the
455 row is updated, but the referenced column is not actually
456 changed, no action is done. Referential actions other than the
457 <literal>NO ACTION
</literal> check cannot be deferred, even if
458 the constraint is declared deferrable. There are the following possible
459 actions for each clause:
463 <term><literal>NO ACTION
</literal></term>
466 Produce an error indicating that the deletion or update
467 would create a foreign key constraint violation.
468 If the constraint is deferred, this
469 error will be produced at constraint check time if there still
470 exist any referencing rows. This is the default action.
476 <term><literal>RESTRICT
</literal></term>
479 Produce an error indicating that the deletion or update
480 would create a foreign key constraint violation.
481 This is the same as
<literal>NO ACTION
</literal> except that
482 the check is not deferrable.
488 <term><literal>CASCADE
</literal></term>
491 Delete any rows referencing the deleted row, or update the
492 value of the referencing column to the new value of the
493 referenced column, respectively.
499 <term><literal>SET NULL
</literal></term>
502 Set the referencing column(s) to null.
508 <term><literal>SET DEFAULT
</literal></term>
511 Set the referencing column(s) to their default values.
519 If the referenced column(s) are changed frequently, it might be wise to
520 add an index to the foreign key column so that referential actions
521 associated with the foreign key column can be performed more
528 <term><literal>DEFERRABLE
</literal></term>
529 <term><literal>NOT DEFERRABLE
</literal></term>
532 This controls whether the constraint can be deferred. A
533 constraint that is not deferrable will be checked immediately
534 after every command. Checking of constraints that are
535 deferrable can be postponed until the end of the transaction
536 (using the
<xref linkend=
"sql-set-constraints" endterm=
"sql-set-constraints-title"> command).
537 <literal>NOT DEFERRABLE
</literal> is the default. Only foreign
538 key constraints currently accept this clause. All other
539 constraint types are not deferrable.
545 <term><literal>INITIALLY IMMEDIATE
</literal></term>
546 <term><literal>INITIALLY DEFERRED
</literal></term>
549 If a constraint is deferrable, this clause specifies the default
550 time to check the constraint. If the constraint is
551 <literal>INITIALLY IMMEDIATE
</literal>, it is checked after each
552 statement. This is the default. If the constraint is
553 <literal>INITIALLY DEFERRED
</literal>, it is checked only at the
554 end of the transaction. The constraint check time can be
555 altered with the
<xref linkend=
"sql-set-constraints" endterm=
"sql-set-constraints-title"> command.
561 <term><literal>WITH (
<replaceable class=
"PARAMETER">storage_parameter
</replaceable> [=
<replaceable class=
"PARAMETER">value
</replaceable>] [, ... ] )
</literal></term>
564 This clause specifies optional storage parameters for a table or index;
565 see
<xref linkend=
"sql-createtable-storage-parameters"
566 endterm=
"sql-createtable-storage-parameters-title"> for more
567 information. The
<literal>WITH<
/> clause for a
568 table can also include
<literal>OIDS=TRUE<
/> (or just
<literal>OIDS<
/>)
569 to specify that rows of the new table
570 should have OIDs (object identifiers) assigned to them, or
571 <literal>OIDS=FALSE<
/> to specify that the rows should not have OIDs.
572 If
<literal>OIDS<
/> is not specified, the default setting depends upon
573 the
<xref linkend=
"guc-default-with-oids"> configuration parameter.
574 (If the new table inherits from any tables that have OIDs, then
575 <literal>OIDS=TRUE<
/> is forced even if the command says
576 <literal>OIDS=FALSE<
/>.)
580 If
<literal>OIDS=FALSE
</literal> is specified or implied, the new
581 table does not store OIDs and no OID will be assigned for a row inserted
582 into it. This is generally considered worthwhile, since it
583 will reduce OID consumption and thereby postpone the wraparound
584 of the
32-bit OID counter. Once the counter wraps around, OIDs
585 can no longer be assumed to be unique, which makes them
586 considerably less useful. In addition, excluding OIDs from a
587 table reduces the space required to store the table on disk by
588 4 bytes per row (on most machines), slightly improving performance.
592 To remove OIDs from a table after it has been created, use
<xref
593 linkend=
"sql-altertable" endterm=
"sql-altertable-title">.
599 <term><literal>WITH OIDS<
/></term>
600 <term><literal>WITHOUT OIDS<
/></term>
603 These are obsolescent syntaxes equivalent to
<literal>WITH (OIDS)<
/>
604 and
<literal>WITH (OIDS=FALSE)<
/>, respectively. If you wish to give
605 both an
<literal>OIDS<
/> setting and storage parameters, you must use
606 the
<literal>WITH ( ... )<
/> syntax; see above.
612 <term><literal>ON COMMIT
</literal></term>
615 The behavior of temporary tables at the end of a transaction
616 block can be controlled using
<literal>ON COMMIT
</literal>.
617 The three options are:
621 <term><literal>PRESERVE ROWS
</literal></term>
624 No special action is taken at the ends of transactions.
625 This is the default behavior.
631 <term><literal>DELETE ROWS
</literal></term>
634 All rows in the temporary table will be deleted at the end
635 of each transaction block. Essentially, an automatic
<xref
636 linkend=
"sql-truncate" endterm=
"sql-truncate-title"> is done
643 <term><literal>DROP
</literal></term>
646 The temporary table will be dropped at the end of the current
657 <term><literal>TABLESPACE
<replaceable class=
"PARAMETER">tablespace
</replaceable></literal></term>
660 The
<replaceable class=
"PARAMETER">tablespace
</replaceable> is the name
661 of the tablespace in which the new table is to be created.
663 <xref linkend=
"guc-default-tablespace"> is consulted, or
664 <xref linkend=
"guc-temp-tablespaces"> if the table is temporary.
670 <term><literal>USING INDEX TABLESPACE
<replaceable class=
"PARAMETER">tablespace
</replaceable></literal></term>
673 This clause allows selection of the tablespace in which the index
674 associated with a
<literal>UNIQUE
</literal> or
<literal>PRIMARY
675 KEY
</literal> constraint will be created.
677 <xref linkend=
"guc-default-tablespace"> is consulted, or
678 <xref linkend=
"guc-temp-tablespaces"> if the table is temporary.
685 <refsect2 id=
"SQL-CREATETABLE-storage-parameters">
686 <title id=
"SQL-CREATETABLE-storage-parameters-title">Storage Parameters
</title>
688 <indexterm zone=
"sql-createtable-storage-parameters">
689 <primary>storage parameters
</primary>
693 The
<literal>WITH<
/> clause can specify
<firstterm>storage parameters<
/>
694 for tables, and for indexes associated with a
<literal>UNIQUE
</literal> or
695 <literal>PRIMARY KEY
</literal> constraint. Storage parameters for
696 indexes are documented in
<xref linkend=
"SQL-CREATEINDEX"
697 endterm=
"sql-createindex-title">. The storage parameters currently
698 available for tables are listed below. For each parameter, there is an
699 additional, identically named parameter, prefixed with
700 <literal>toast.
</literal> which can be used to control the behavior of the
701 supplementary storage table, if any; see
<xref linkend=
"storage-toast">.
702 Note that the supplementary storage table inherits the
703 <literal>autovacuum
</literal> values from its parent table, if there are
704 no
<literal>toast.autovacuum_*
</literal> settings set.
710 <term><literal>fillfactor<
/>,
<literal>toast.fillfactor
</literal> (
<type>integer<
/>)
</term>
713 The fillfactor for a table is a percentage between
10 and
100.
714 100 (complete packing) is the default. When a smaller fillfactor
715 is specified,
<command>INSERT<
/> operations pack table pages only
716 to the indicated percentage; the remaining space on each page is
717 reserved for updating rows on that page. This gives
<command>UPDATE<
/>
718 a chance to place the updated copy of a row on the same page as the
719 original, which is more efficient than placing it on a different page.
720 For a table whose entries are never updated, complete packing is the
721 best choice, but in heavily updated tables smaller fillfactors are
728 <term><literal>autovacuum_enabled<
/>,
<literal>toast.autovacuum_enabled
</literal> (
<type>boolean<
/>)
</term>
731 Enables or disables the autovacuum daemon on a particular table.
732 If true, the autovacuum daemon will initiate a
<command>VACUUM<
/> operation
733 on a particular table when the number of updated or deleted tuples exceeds
734 <literal>autovacuum_vacuum_threshold<
/> plus
735 <literal>autovacuum_vacuum_scale_factor<
/> times the number of live tuples
736 currently estimated to be in the relation.
737 Similarly, it will initiate an
<command>ANALYZE<
/> operation when the
738 number of inserted, updated or deleted tuples exceeds
739 <literal>autovacuum_analyze_threshold<
/> plus
740 <literal>autovacuum_analyze_scale_factor<
/> times the number of live tuples
741 currently estimated to be in the relation.
742 If false, this table will not be autovacuumed, except to prevent
743 transaction Id wraparound. See
<xref linkend=
"vacuum-for-wraparound"> for
744 more about wraparound prevention.
745 Observe that this variable inherits its value from the
<xref
746 linkend=
"guc-autovacuum"> setting.
752 <term><literal>autovacuum_vacuum_threshold<
/>,
<literal>toast.autovacuum_vacuum_threshold
</literal> (
<type>integer<
/>)
</term>
755 Minimum number of updated or deleted tuples before initiate a
756 <command>VACUUM<
/> operation on a particular table.
762 <term><literal>autovacuum_vacuum_scale_factor<
/>,
<literal>toast.autovacuum_vacuum_scale_factor
</literal> (
<type>float4<
/>)
</term>
765 Multiplier for
<structfield>reltuples<
/> to add to
766 <literal>autovacuum_vacuum_threshold<
/>.
772 <term><literal>autovacuum_analyze_threshold<
/>,
<literal>toast.autovacuum_analyze_threshold
</literal> (
<type>integer<
/>)
</term>
775 Minimum number of inserted, updated, or deleted tuples before initiate an
776 <command>ANALYZE<
/> operation on a particular table.
782 <term><literal>autovacuum_analyze_scale_factor<
/>,
<literal>toast.autovacuum_analyze_scale_factor
</literal> (
<type>float4<
/>)
</term>
785 Multiplier for
<structfield>reltuples<
/> to add to
786 <literal>autovacuum_analyze_threshold<
/>.
792 <term><literal>autovacuum_vacuum_cost_delay<
/>,
<literal>toast.autovacuum_vacuum_cost_delay
</literal> (
<type>integer<
/>)
</term>
795 Custom
<xref linkend=
"guc-autovacuum-vacuum-cost-delay"> parameter.
801 <term><literal>autovacuum_vacuum_cost_limit<
/>,
<literal>toast.autovacuum_vacuum_cost_limit
</literal> (
<type>integer<
/>)
</term>
804 Custom
<xref linkend=
"guc-autovacuum-vacuum-cost-limit"> parameter.
810 <term><literal>autovacuum_freeze_min_age<
/>,
<literal>toast.autovacuum_freeze_min_age
</literal> (
<type>integer<
/>)
</term>
813 Custom
<xref linkend=
"guc-vacuum-freeze-min-age"> parameter. Note that
814 autovacuum will ignore attempts to set a per-table
815 <literal>autovacuum_freeze_min_age<
/> larger than the half system-wide
816 <xref linkend=
"guc-autovacuum-freeze-max-age"> setting.
822 <term><literal>autovacuum_freeze_max_age<
/>,
<literal>toast.autovacuum_freeze_max_age
</literal> (
<type>integer<
/>)
</term>
825 Custom
<xref linkend=
"guc-autovacuum-freeze-max-age"> parameter. Note that
826 autovacuum will ignore attempts to set a per-table
827 <literal>autovacuum_freeze_max_age<
/> larger than the system-wide setting
828 (it can only be set smaller). Note that while you can set
829 <literal>autovacuum_freeze_max_age<
/> very small, or even zero, this is
830 usually unwise since it will force frequent vacuuming.
836 <term><literal>autovacuum_freeze_table_age
</literal> (
<type>integer
</type>)
</term>
839 Custom
<xref linkend=
"guc-vacuum-freeze-table-age"> parameter.
849 <refsect1 id=
"SQL-CREATETABLE-notes">
853 Using OIDs in new applications is not recommended: where
854 possible, using a
<literal>SERIAL
</literal> or other sequence
855 generator as the table's primary key is preferred. However, if
856 your application does make use of OIDs to identify specific
857 rows of a table, it is recommended to create a unique constraint
858 on the
<structfield>oid<
/> column of that table, to ensure that
859 OIDs in the table will indeed uniquely identify rows even after
860 counter wraparound. Avoid assuming that OIDs are unique across
861 tables; if you need a database-wide unique identifier, use the
862 combination of
<structfield>tableoid<
/> and row OID for the
868 The use of
<literal>OIDS=FALSE
</literal> is not recommended
869 for tables with no primary key, since without either an OID or a
870 unique data key, it is difficult to identify specific rows.
875 <productname>PostgreSQL
</productname> automatically creates an
876 index for each unique constraint and primary key constraint to
877 enforce uniqueness. Thus, it is not necessary to create an
878 index explicitly for primary key columns. (See
<xref
879 linkend=
"sql-createindex" endterm=
"sql-createindex-title"> for more information.)
883 Unique constraints and primary keys are not inherited in the
884 current implementation. This makes the combination of
885 inheritance and unique constraints rather dysfunctional.
889 A table cannot have more than
1600 columns. (In practice, the
890 effective limit is usually lower because of tuple-length constraints.)
896 <refsect1 id=
"SQL-CREATETABLE-examples">
897 <title>Examples
</title>
900 Create table
<structname>films<
/> and table
901 <structname>distributors<
/>:
905 code char(
5) CONSTRAINT firstkey PRIMARY KEY,
906 title varchar(
40) NOT NULL,
907 did integer NOT NULL,
910 len interval hour to minute
915 CREATE TABLE distributors (
916 did integer PRIMARY KEY DEFAULT nextval('serial'),
917 name varchar(
40) NOT NULL CHECK (name
<> '')
923 Create a table with a
2-dimensional array:
926 CREATE TABLE array_int (
933 Define a unique table constraint for the table
934 <literal>films
</literal>. Unique table constraints can be defined
935 on one or more columns of the table:
944 len interval hour to minute,
945 CONSTRAINT production UNIQUE(date_prod)
951 Define a check column constraint:
954 CREATE TABLE distributors (
955 did integer CHECK (did
> 100),
962 Define a check table constraint:
965 CREATE TABLE distributors (
968 CONSTRAINT con1 CHECK (did
> 100 AND name
<> '')
974 Define a primary key table constraint for the table
975 <structname>films<
/>:
984 len interval hour to minute,
985 CONSTRAINT code_title PRIMARY KEY(code,title)
991 Define a primary key constraint for table
992 <structname>distributors<
/>. The following two examples are
993 equivalent, the first using the table constraint syntax, the second
994 the column constraint syntax:
997 CREATE TABLE distributors (
1005 CREATE TABLE distributors (
1006 did integer PRIMARY KEY,
1013 Assign a literal constant default value for the column
1014 <literal>name
</literal>, arrange for the default value of column
1015 <literal>did
</literal> to be generated by selecting the next value
1016 of a sequence object, and make the default value of
1017 <literal>modtime
</literal> be the time at which the row is
1021 CREATE TABLE distributors (
1022 name varchar(
40) DEFAULT 'Luso Films',
1023 did integer DEFAULT nextval('distributors_serial'),
1024 modtime timestamp DEFAULT current_timestamp
1030 Define two
<literal>NOT NULL<
/> column constraints on the table
1031 <classname>distributors
</classname>, one of which is explicitly
1035 CREATE TABLE distributors (
1036 did integer CONSTRAINT no_null NOT NULL,
1037 name varchar(
40) NOT NULL
1043 Define a unique constraint for the
<literal>name
</literal> column:
1046 CREATE TABLE distributors (
1048 name varchar(
40) UNIQUE
1052 The same, specified as a table constraint:
1055 CREATE TABLE distributors (
1064 Create the same table, specifying
70% fill factor for both the table
1065 and its unique index:
1068 CREATE TABLE distributors (
1071 UNIQUE(name) WITH (fillfactor=
70)
1073 WITH (fillfactor=
70);
1078 Create table
<structname>cinemas<
/> in tablespace
<structname>diskvol1<
/>:
1081 CREATE TABLE cinemas (
1085 ) TABLESPACE diskvol1;
1091 <refsect1 id=
"SQL-CREATETABLE-compatibility">
1092 <title id=
"SQL-CREATETABLE-compatibility-title">Compatibility
</title>
1095 The
<command>CREATE TABLE
</command> command conforms to the
1096 <acronym>SQL
</acronym> standard, with exceptions listed below.
1100 <title>Temporary Tables
</title>
1103 Although the syntax of
<literal>CREATE TEMPORARY TABLE
</literal>
1104 resembles that of the SQL standard, the effect is not the same. In the
1106 temporary tables are defined just once and automatically exist (starting
1107 with empty contents) in every session that needs them.
1108 <productname>PostgreSQL
</productname> instead
1109 requires each session to issue its own
<literal>CREATE TEMPORARY
1110 TABLE
</literal> command for each temporary table to be used. This allows
1111 different sessions to use the same temporary table name for different
1112 purposes, whereas the standard's approach constrains all instances of a
1113 given temporary table name to have the same table structure.
1117 The standard's definition of the behavior of temporary tables is
1118 widely ignored.
<productname>PostgreSQL
</productname>'s behavior
1119 on this point is similar to that of several other SQL databases.
1123 The standard's distinction between global and local temporary tables
1124 is not in
<productname>PostgreSQL
</productname>, since that distinction
1125 depends on the concept of modules, which
1126 <productname>PostgreSQL
</productname> does not have.
1127 For compatibility's sake,
<productname>PostgreSQL
</productname> will
1128 accept the
<literal>GLOBAL
</literal> and
<literal>LOCAL
</literal> keywords
1129 in a temporary table declaration, but they have no effect.
1133 The
<literal>ON COMMIT
</literal> clause for temporary tables
1134 also resembles the SQL standard, but has some differences.
1135 If the
<literal>ON COMMIT<
/> clause is omitted, SQL specifies that the
1136 default behavior is
<literal>ON COMMIT DELETE ROWS<
/>. However, the
1137 default behavior in
<productname>PostgreSQL
</productname> is
1138 <literal>ON COMMIT PRESERVE ROWS
</literal>. The
<literal>ON COMMIT
1139 DROP
</literal> option does not exist in SQL.
1144 <title>Column Check Constraints
</title>
1147 The SQL standard says that
<literal>CHECK<
/> column constraints
1148 can only refer to the column they apply to; only
<literal>CHECK<
/>
1149 table constraints can refer to multiple columns.
1150 <productname>PostgreSQL
</productname> does not enforce this
1151 restriction; it treats column and table check constraints alike.
1156 <title><literal>NULL
</literal> <quote>Constraint
</quote></title>
1159 The
<literal>NULL<
/> <quote>constraint
</quote> (actually a
1160 non-constraint) is a
<productname>PostgreSQL
</productname>
1161 extension to the SQL standard that is included for compatibility with some
1162 other database systems (and for symmetry with the
<literal>NOT
1163 NULL
</literal> constraint). Since it is the default for any
1164 column, its presence is simply noise.
1169 <title>Inheritance
</title>
1172 Multiple inheritance via the
<literal>INHERITS
</literal> clause is
1173 a
<productname>PostgreSQL
</productname> language extension.
1174 SQL:
1999 and later define single inheritance using a
1175 different syntax and different semantics. SQL:
1999-style
1176 inheritance is not yet supported by
1177 <productname>PostgreSQL
</productname>.
1182 <title>Zero-column tables
</title>
1185 <productname>PostgreSQL
</productname> allows a table of no columns
1186 to be created (for example,
<literal>CREATE TABLE foo();<
/>). This
1187 is an extension from the SQL standard, which does not allow zero-column
1188 tables. Zero-column tables are not in themselves very useful, but
1189 disallowing them creates odd special cases for
<command>ALTER TABLE
1190 DROP COLUMN<
/>, so it seems cleaner to ignore this spec restriction.
1195 <title><literal>WITH<
/> clause
</title>
1198 The
<literal>WITH<
/> clause is a
<productname>PostgreSQL
</productname>
1199 extension; neither storage parameters nor OIDs are in the standard.
1204 <title>Tablespaces
</title>
1207 The
<productname>PostgreSQL
</productname> concept of tablespaces is not
1208 part of the standard. Hence, the clauses
<literal>TABLESPACE
</literal>
1209 and
<literal>USING INDEX TABLESPACE
</literal> are extensions.
1216 <title>See Also
</title>
1218 <simplelist type=
"inline">
1219 <member><xref linkend=
"sql-altertable" endterm=
"sql-altertable-title"></member>
1220 <member><xref linkend=
"sql-droptable" endterm=
"sql-droptable-title"></member>
1221 <member><xref linkend=
"sql-createtablespace" endterm=
"sql-createtablespace-title"></member>