]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/create_table.sgml
344efa52e6e42fa729ba376e26b3989467d1298e
[postgresql] / doc / src / sgml / ref / create_table.sgml
1 <!--
2 $PostgreSQL: pgsql/doc/src/sgml/ref/create_table.sgml,v 1.88 2004/12/13 18:05:10 petere Exp $
3 PostgreSQL documentation
4 -->
5
6 <refentry id="SQL-CREATETABLE">
7  <refmeta>
8   <refentrytitle id="sql-createtable-title">CREATE TABLE</refentrytitle>
9   <refmiscinfo>SQL - Language Statements</refmiscinfo>
10  </refmeta>
11
12  <refnamediv>
13   <refname>CREATE TABLE</refname>
14   <refpurpose>define a new table</refpurpose>
15  </refnamediv>
16
17  <indexterm zone="sql-createtable">
18   <primary>CREATE TABLE</primary>
19  </indexterm>
20
21  <refsynopsisdiv>
22 <synopsis>
23 CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE <replaceable class="PARAMETER">table_name</replaceable> (
24   { <replaceable class="PARAMETER">column_name</replaceable> <replaceable class="PARAMETER">data_type</replaceable> [ DEFAULT <replaceable>default_expr</> ] [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]
25     | <replaceable>table_constraint</replaceable>
26     | LIKE <replaceable>parent_table</replaceable> [ { INCLUDING | EXCLUDING } DEFAULTS ] }  [, ... ]
27 )
28 [ INHERITS ( <replaceable>parent_table</replaceable> [, ... ] ) ]
29 [ WITH OIDS | WITHOUT OIDS ]
30 [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
31 [ TABLESPACE <replaceable class="PARAMETER">tablespace</replaceable> ]
32
33 where <replaceable class="PARAMETER">column_constraint</replaceable> is:
34
35 [ CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> ]
36 { NOT NULL | 
37   NULL | 
38   UNIQUE [ USING INDEX TABLESPACE <replaceable class="PARAMETER">tablespace</replaceable> ] |
39   PRIMARY KEY [ USING INDEX TABLESPACE <replaceable class="PARAMETER">tablespace</replaceable> ] |
40   CHECK (<replaceable class="PARAMETER">expression</replaceable>) |
41   REFERENCES <replaceable class="PARAMETER">reftable</replaceable> [ ( <replaceable class="PARAMETER">refcolumn</replaceable> ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
42     [ ON DELETE <replaceable class="parameter">action</replaceable> ] [ ON UPDATE <replaceable class="parameter">action</replaceable> ] }
43 [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
44
45 and <replaceable class="PARAMETER">table_constraint</replaceable> is:
46
47 [ CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> ]
48 { UNIQUE ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] ) [ USING INDEX TABLESPACE <replaceable class="PARAMETER">tablespace</replaceable> ] |
49   PRIMARY KEY ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] ) [ USING INDEX TABLESPACE <replaceable class="PARAMETER">tablespace</replaceable> ] |
50   CHECK ( <replaceable class="PARAMETER">expression</replaceable> ) |
51   FOREIGN KEY ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] ) REFERENCES <replaceable class="PARAMETER">reftable</replaceable> [ ( <replaceable class="PARAMETER">refcolumn</replaceable> [, ... ] ) ]
52     [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE <replaceable class="parameter">action</replaceable> ] [ ON UPDATE <replaceable class="parameter">action</replaceable> ] }
53 [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
54 </synopsis>
55
56  </refsynopsisdiv>
57
58  <refsect1 id="SQL-CREATETABLE-description">
59   <title>Description</title>
60
61   <para>
62    <command>CREATE TABLE</command> will create a new, initially empty table
63    in the current database. The table will be owned by the user issuing the
64    command.
65   </para>
66
67   <para>
68    If a schema name is given (for example, <literal>CREATE TABLE
69    myschema.mytable ...</>) then the table is created in the
70    specified schema.  Otherwise it is created in the current schema.
71    Temporary tables exist in a special schema, so a schema name may not be
72    given when creating a temporary table.
73    The table name must be distinct from the name of any other table,
74    sequence, index, or view in the same schema.
75   </para>
76
77   <para>
78    <command>CREATE TABLE</command> also automatically creates a data
79    type that represents the composite type corresponding
80    to one row of the table.  Therefore, tables cannot have the same
81    name as any existing data type in the same schema.
82   </para>
83
84   <para>
85    A table cannot have more than 1600 columns.  (In practice, the
86    effective limit is lower because of tuple-length constraints).
87   </para>
88
89   <para>
90    The optional constraint clauses specify constraints (or tests) that
91    new or updated rows must satisfy for an insert or update operation
92    to succeed.  A constraint is an SQL object that helps define the
93    set of valid values in the table in various ways.
94   </para>
95
96   <para>
97    There are two ways to define constraints: table constraints and
98    column constraints.  A column constraint is defined as part of a
99    column definition.  A table constraint definition is not tied to a
100    particular column, and it can encompass more than one column.
101    Every column constraint can also be written as a table constraint;
102    a column constraint is only a notational convenience if the
103    constraint only affects one column.
104   </para>
105  </refsect1>
106
107  <refsect1>
108   <title>Parameters</title>
109
110   <variablelist>
111
112    <varlistentry>
113     <term><literal>TEMPORARY</> or <literal>TEMP</></term>
114     <listitem>
115      <para>
116       If specified, the table is created as a temporary table.
117       Temporary tables are automatically dropped at the end of a
118       session, or optionally at the end of the current transaction
119       (see <literal>ON COMMIT</literal> below).  Existing permanent
120       tables with the same name are not visible to the current session
121       while the temporary table exists, unless they are referenced
122       with schema-qualified names. Any indexes created on a temporary
123       table are automatically temporary as well.
124      </para>
125
126      <para>
127       Optionally, <literal>GLOBAL</literal> or <literal>LOCAL</literal>
128       can be written before <literal>TEMPORARY</> or <literal>TEMP</>.
129       This makes no difference in <productname>PostgreSQL</>, but see
130       <xref linkend="sql-createtable-compatibility"
131       endterm="sql-createtable-compatibility-title">.
132      </para>
133     </listitem>
134    </varlistentry>
135
136    <varlistentry>
137     <term><replaceable class="PARAMETER">table_name</replaceable></term>
138     <listitem>
139      <para>
140       The name (optionally schema-qualified) of the table to be created.
141      </para>
142     </listitem>
143    </varlistentry>
144
145    <varlistentry>
146     <term><replaceable class="PARAMETER">column_name</replaceable></term>
147     <listitem>
148      <para>
149       The name of a column to be created in the new table.
150      </para>
151     </listitem>
152    </varlistentry>
153
154    <varlistentry>
155     <term><replaceable class="PARAMETER">data_type</replaceable></term>
156     <listitem>
157      <para>
158       The data type of the column. This may include array
159       specifiers. For more information on the data types included with
160       <productname>PostgreSQL</productname>, refer to <xref
161       linkend="datatype">.
162      </para>
163     </listitem>
164    </varlistentry>
165
166    <varlistentry>
167     <term><literal>DEFAULT
168     <replaceable>default_expr</replaceable></literal></term>
169     <listitem>
170      <para>
171       The <literal>DEFAULT</> clause assigns a default data value for
172       the column whose column definition it appears within.  The value
173       is any variable-free expression (subqueries and cross-references
174       to other columns in the current table are not allowed).  The
175       data type of the default expression must match the data type of the
176       column.
177      </para>
178
179      <para>
180       The default expression will be used in any insert operation that
181       does not specify a value for the column.  If there is no default
182       for a column, then the default is null.
183      </para>
184     </listitem>
185    </varlistentry>
186
187    <varlistentry>
188     <term><literal>LIKE <replaceable>parent_table</replaceable> [ { INCLUDING | EXCLUDING } DEFAULTS ]</literal></term>
189     <listitem>
190      <para>
191       The <literal>LIKE</literal> clause specifies a table from which
192       the new table automatically copies all column names, their data types,
193       and their not-null constraints.
194      </para>
195      <para>
196       Unlike <literal>INHERITS</literal>, the new table and original table
197       are completely decoupled after creation is complete.  Changes to the
198       original table will not be applied to the new table, and it is not
199       possible to include data of the new table in scans of the original
200       table.
201      </para>
202      <para>
203       Default expressions for the copied column definitions will only be
204       included if <literal>INCLUDING DEFAULTS</literal> is specified.  The
205       default is to exclude default expressions.
206      </para>
207     </listitem>
208    </varlistentry>
209
210    <varlistentry>
211     <term><literal>INHERITS ( <replaceable>parent_table</replaceable> [, ... ] )</literal></term>
212     <listitem>
213      <para>
214       The optional <literal>INHERITS</> clause specifies a list of
215       tables from which the new table automatically inherits all
216       columns.
217      </para>
218
219      <para>
220       Use of <literal>INHERITS</> creates a persistent relationship
221       between the new child table and its parent table(s).  Schema
222       modifications to the parent(s) normally propagate to children
223       as well, and by default the data of the child table is included in
224       scans of the parent(s).
225      </para>
226
227      <para>
228       If the same column name exists in more than one parent
229       table, an error is reported unless the data types of the columns
230       match in each of the parent tables.  If there is no conflict,
231       then the duplicate columns are merged to form a single column in
232       the new table.  If the column name list of the new table
233       contains a column name that is also inherited, the data type must
234       likewise match the inherited column(s), and the column
235       definitions are merged into one.  However, inherited and new
236       column declarations of the same name need not specify identical
237       constraints: all constraints provided from any declaration are
238       merged together and all are applied to the new table.  If the
239       new table explicitly specifies a default value for the column,
240       this default overrides any defaults from inherited declarations
241       of the column.  Otherwise, any parents that specify default
242       values for the column must all specify the same default, or an
243       error will be reported.
244      </para>
245 <!--
246      <para>
247       <productname>PostgreSQL</> automatically allows the
248      created table to inherit
249       functions on tables above it in the inheritance hierarchy; that
250       is, if we create table <literal>foo</literal> inheriting from
251       <literal>bar</literal>, then functions that accept the tuple
252       type <literal>bar</literal> can also be applied to instances of
253       <literal>foo</literal>.  (Currently, this works reliably for
254       functions on the first or only parent table, but not so well for
255       functions on additional parents.)
256      </para>
257 -->
258     </listitem>
259    </varlistentry>
260
261    <varlistentry>
262     <term><literal>WITH OIDS</></term>
263     <term><literal>WITHOUT OIDS</></term>
264     <listitem>
265      <para>
266       This optional clause specifies whether rows of the new table
267       should have OIDs (object identifiers) assigned to them.  If
268       neither <literal>WITH OIDS</literal> nor <literal>WITHOUT
269       OIDS</literal> is specified, the default value depends upon the
270       <xref linkend="guc-default-with-oids"> configuration parameter. (If
271       the new table inherits from any tables that have OIDs, then
272       <literal>WITH OIDS</> is forced even if the command says
273       <literal>WITHOUT OIDS</>.)
274      </para>
275
276      <para>
277       If <literal>WITHOUT OIDS</literal> is specified or implied, this
278       means that the generation of OIDs for this table will be
279       supressed. This is generally considered worthwhile, since it
280       will reduce OID consumption and thereby postpone the wraparound
281       of the 32-bit OID counter. Once the counter wraps around, OIDs
282       can no longer be assumed to be unique, which makes them
283       considerably less useful. In addition, excluding OIDs from a
284       table reduces the space required on disk to storage the table by
285       4 bytes per row, leading to increased performance.
286      </para>
287
288      <para>
289       To remove OIDs from a table after it has been created, use <xref
290       linkend="sql-altertable" endterm="sql-altertable-title">.
291      </para>
292     </listitem>
293    </varlistentry>
294
295    <varlistentry>
296     <term><literal>CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable></literal></term>
297     <listitem>
298      <para>
299       An optional name for a column or table constraint.  If not specified,
300       the system generates a name.
301      </para>
302     </listitem>
303    </varlistentry>
304
305    <varlistentry>
306     <term><literal>NOT NULL</></term>
307     <listitem>
308      <para>
309       The column is not allowed to contain null values.
310      </para>
311     </listitem>
312    </varlistentry>
313
314    <varlistentry>
315     <term><literal>NULL</></term>
316     <listitem>
317      <para>
318       The column is allowed to contain null values. This is the default.
319      </para>
320
321      <para>
322       This clause is only available for compatibility with
323       non-standard SQL databases.  Its use is discouraged in new
324       applications.
325      </para>
326     </listitem>
327    </varlistentry>
328
329    <varlistentry>
330     <term><literal>UNIQUE</> (column constraint)</term>
331     <term><literal>UNIQUE ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] )</> (table constraint)</term>
332
333     <listitem>
334      <para>
335       The <literal>UNIQUE</literal> constraint specifies that a
336       group of one or more distinct columns of a table may contain
337       only unique values. The behavior of the unique table constraint
338       is the same as that for column constraints, with the additional
339       capability to span multiple columns.
340      </para>
341
342      <para>
343       For the purpose of a unique constraint, null values are not
344       considered equal.
345      </para>
346
347      <para>
348       Each unique table constraint must name a set of columns that is
349       different from the set of columns named by any other unique or
350       primary key constraint defined for the table.  (Otherwise it
351       would just be the same constraint listed twice.)
352      </para>
353     </listitem>
354    </varlistentry>
355
356    <varlistentry>
357     <term><literal>PRIMARY KEY</> (column constraint)</term>
358     <term><literal>PRIMARY KEY ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] )</> (table constraint)</term>
359     <listitem>
360      <para>
361       The primary key constraint specifies that a column or columns of a table
362       may contain only unique (non-duplicate), nonnull values.
363       Technically, <literal>PRIMARY KEY</literal> is merely a
364       combination of <literal>UNIQUE</> and <literal>NOT NULL</>, but
365       identifying a set of columns as primary key also provides
366       metadata about the design of the schema, as a primary key
367       implies that other tables
368       may rely on this set of columns as a unique identifier for rows.
369      </para>
370
371      <para>
372       Only one primary key can be specified for a table, whether as a
373       column constraint or a table constraint.
374      </para>
375
376      <para>
377       The primary key constraint should name a set of columns that is
378       different from other sets of columns named by any unique
379       constraint defined for the same table.
380      </para>
381     </listitem>
382    </varlistentry>
383
384    <varlistentry>
385     <term><literal>CHECK (<replaceable class="PARAMETER">expression</replaceable>)</literal></term>
386     <listitem>
387      <para>
388       The <literal>CHECK</> clause specifies an expression producing a
389       Boolean result which new or updated rows must satisfy for an
390       insert or update operation to succeed.  Expressions evaluating
391       to TRUE or UNKNOWN succeed.  Should any row of an insert or
392       update operation produce a FALSE result an error exception is
393       raised and the insert or update does not alter the database.  A
394       check constraint specified as a column constraint should
395       reference that column's value only, while an expression
396       appearing in a table constraint may reference multiple columns.
397      </para>
398
399      <para>
400       Currently, <literal>CHECK</literal> expressions cannot contain
401       subqueries nor refer to variables other than columns of the
402       current row.
403      </para>
404     </listitem>
405    </varlistentry>
406
407
408    <varlistentry>
409     <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>
410
411    <term><literal>FOREIGN KEY ( <replaceable class="parameter">column</replaceable> [, ... ] )
412     REFERENCES <replaceable class="parameter">reftable</replaceable> [ ( <replaceable class="parameter">refcolumn</replaceable> [, ... ] ) ]
413     [ MATCH <replaceable class="parameter">matchtype</replaceable> ]
414     [ ON DELETE <replaceable class="parameter">action</replaceable> ]
415     [ ON UPDATE <replaceable class="parameter">action</replaceable> ]</literal>
416     (table constraint)</term>
417
418     <listitem>
419      <para>
420       These clauses specify a foreign key constraint, which requires
421       that a group of one or more columns of the new table must only
422       contain values that match values in the referenced
423       column(s) of some row of the referenced table.  If <replaceable
424       class="parameter">refcolumn</replaceable> is omitted, the
425       primary key of the <replaceable
426       class="parameter">reftable</replaceable> is used.  The
427       referenced columns must be the columns of a unique or primary
428       key constraint in the referenced table.
429      </para>
430
431      <para>
432       A value inserted into the referencing column(s) is matched against the
433       values of the referenced table and referenced columns using the
434       given match type.  There are three match types: <literal>MATCH
435       FULL</>, <literal>MATCH PARTIAL</>, and <literal>MATCH
436       SIMPLE</literal>, which is also the default.  <literal>MATCH
437       FULL</> will not allow one column of a multicolumn foreign key
438       to be null unless all foreign key columns are null.
439       <literal>MATCH SIMPLE</literal> allows some foreign key columns
440       to be null while other parts of the foreign key are not
441       null. <literal>MATCH PARTIAL</> is not yet implemented.
442      </para>
443
444      <para>
445       In addition, when the data in the referenced columns is changed,
446       certain actions are performed on the data in this table's
447       columns.  The <literal>ON DELETE</literal> clause specifies the
448       action to perform when a referenced row in the referenced table is
449       being deleted.  Likewise, the <literal>ON UPDATE</literal>
450       clause specifies the action to perform when a referenced column
451       in the referenced table is being updated to a new value. If the
452       row is updated, but the referenced column is not actually
453       changed, no action is done. Referential actions apart from the
454       check of <literal>NO ACTION</literal> can not be deferred even if
455       the constraint is deferrable. There are the following possible
456       actions for each clause:
457
458       <variablelist>
459        <varlistentry>
460         <term><literal>NO ACTION</literal></term>
461         <listitem>
462          <para>
463           Produce an error indicating that the deletion or update
464           would create a foreign key constraint violation.
465           If the constraint is deferred, this
466           error will be produced at constraint check time if there still
467           exist any referencing rows.  This is the default action.
468          </para>
469         </listitem>
470        </varlistentry>
471
472        <varlistentry>
473         <term><literal>RESTRICT</literal></term>
474         <listitem>
475          <para>
476           Produce an error indicating that the deletion or update
477           would create a foreign key constraint violation.
478           This is the same as <literal>NO ACTION</literal> except that
479           the check is not deferrable.
480          </para>
481         </listitem>
482        </varlistentry>
483
484        <varlistentry>
485         <term><literal>CASCADE</literal></term>
486         <listitem>
487          <para>
488           Delete any rows referencing the deleted row, or update the
489           value of the referencing column to the new value of the
490           referenced column, respectively.
491          </para>
492         </listitem>
493        </varlistentry>
494
495        <varlistentry>
496         <term><literal>SET NULL</literal></term>
497         <listitem>
498          <para>
499           Set the referencing column(s) to null.
500          </para>
501         </listitem>
502        </varlistentry>
503
504        <varlistentry>
505         <term><literal>SET DEFAULT</literal></term>
506         <listitem>
507          <para>
508           Set the referencing column(s) to their default values.
509          </para>
510         </listitem>
511        </varlistentry>
512       </variablelist>
513      </para>
514
515      <para>
516       If the referenced column(s) are changed frequently, it may be wise to
517       add an index to the foreign key column so that referential actions
518       associated with the foreign key column can be performed more
519       efficiently.
520      </para>
521     </listitem>
522    </varlistentry>
523
524    <varlistentry>
525     <term><literal>DEFERRABLE</literal></term>
526     <term><literal>NOT DEFERRABLE</literal></term>
527     <listitem>
528      <para>
529       This controls whether the constraint can be deferred.  A
530       constraint that is not deferrable will be checked immediately
531       after every command.  Checking of constraints that are
532       deferrable may be postponed until the end of the transaction
533       (using the <xref linkend="sql-set-constraints" endterm="sql-set-constraints-title"> command).
534       <literal>NOT DEFERRABLE</literal> is the default.  Only foreign
535       key constraints currently accept this clause.  All other
536       constraint types are not deferrable.
537      </para>
538     </listitem>
539    </varlistentry>
540
541    <varlistentry>
542     <term><literal>INITIALLY IMMEDIATE</literal></term>
543     <term><literal>INITIALLY DEFERRED</literal></term>
544     <listitem>
545      <para>
546       If a constraint is deferrable, this clause specifies the default
547       time to check the constraint.  If the constraint is
548       <literal>INITIALLY IMMEDIATE</literal>, it is checked after each
549       statement. This is the default.  If the constraint is
550       <literal>INITIALLY DEFERRED</literal>, it is checked only at the
551       end of the transaction.  The constraint check time can be
552       altered with the <xref linkend="sql-set-constraints" endterm="sql-set-constraints-title"> command.
553      </para>
554     </listitem>
555    </varlistentry>
556
557    <varlistentry>
558     <term><literal>ON COMMIT</literal></term>
559     <listitem>
560      <para>
561       The behavior of temporary tables at the end of a transaction
562       block can be controlled using <literal>ON COMMIT</literal>.
563       The three options are:
564
565       <variablelist>
566        <varlistentry>
567         <term><literal>PRESERVE ROWS</literal></term>
568         <listitem>
569          <para>
570           No special action is taken at the ends of transactions.
571           This is the default behavior.
572          </para>
573         </listitem>
574        </varlistentry>
575
576        <varlistentry>
577         <term><literal>DELETE ROWS</literal></term>
578         <listitem>
579          <para>
580           All rows in the temporary table will be deleted at the
581           end of each transaction block.  Essentially, an automatic
582           <xref linkend="sql-truncate"> is done at each commit.
583          </para>
584         </listitem>
585        </varlistentry>
586
587        <varlistentry>
588         <term><literal>DROP</literal></term>
589         <listitem>
590          <para>
591           The temporary table will be dropped at the end of the current
592           transaction block.
593          </para>
594         </listitem>
595        </varlistentry>
596       </variablelist>
597      </para>
598     </listitem>
599    </varlistentry>
600
601    <varlistentry>
602     <term><literal>TABLESPACE <replaceable class="PARAMETER">tablespace</replaceable></literal></term>
603     <listitem>
604      <para>
605       The <replaceable class="PARAMETER">tablespace</replaceable> is the name
606       of the tablespace in which the new table is to be created.
607       If not specified,
608       <xref linkend="guc-default-tablespace"> is used, or the database's
609       default tablespace if <varname>default_tablespace</> is an empty
610       string.
611      </para>
612     </listitem>
613    </varlistentry>
614
615    <varlistentry>
616     <term><literal>USING INDEX TABLESPACE <replaceable class="PARAMETER">tablespace</replaceable></literal></term>
617     <listitem>
618      <para>
619       This clause allows selection of the tablespace in which the index
620       associated with a <literal>UNIQUE</literal> or <literal>PRIMARY
621       KEY</literal> constraint will be created.
622       If not specified,
623       <xref linkend="guc-default-tablespace"> is used, or the database's
624       default tablespace if <varname>default_tablespace</> is an empty
625       string.
626      </para>
627     </listitem>
628    </varlistentry>
629
630   </variablelist>
631  </refsect1>
632
633  <refsect1 id="SQL-CREATETABLE-notes">
634   <title>Notes</title>
635
636   <itemizedlist>
637    <listitem>
638     <para>
639      Using OIDs in new applications is not recommended: where
640      possible, using a <literal>SERIAL</literal> or other sequence
641      generator as the table's primary key is preferred. However, if
642      your application does make use of OIDs to identify specific rows
643      rows of a table, it is recommended to create a unique constraint
644      on the <structfield>oid</> column of that table, to ensure that
645      OIDs in the table will indeed uniquely identify rows even after
646      counter wraparound.  Avoid assuming that OIDs are unique across
647      tables; if you need a database-wide unique identifier, use the
648      combination of <structfield>tableoid</> and row OID for the
649      purpose.
650     </para>
651
652     <tip>
653      <para>
654       The use of <literal>WITHOUT OIDS</literal> is not recommended
655       for tables with no primary key, since without either an OID or a
656       unique data key, it is difficult to identify specific rows.
657      </para>
658     </tip>
659    </listitem>
660
661    <listitem>
662     <para>
663      <productname>PostgreSQL</productname> automatically creates an
664      index for each unique constraint and primary key constraint to
665      enforce the uniqueness.  Thus, it is not necessary to create an
666      explicit index for primary key columns.  (See <xref
667      linkend="sql-createindex" endterm="sql-createindex-title"> for more information.)
668     </para>
669    </listitem>
670
671    <listitem>
672     <para>
673      Unique constraints and primary keys are not inherited in the
674      current implementation.  This makes the combination of
675      inheritance and unique constraints rather dysfunctional.
676     </para>
677    </listitem>
678   </itemizedlist>
679  </refsect1>
680
681
682  <refsect1 id="SQL-CREATETABLE-examples">
683   <title>Examples</title>
684
685   <para>
686    Create table <structname>films</> and table
687    <structname>distributors</>:
688
689 <programlisting>
690 CREATE TABLE films (
691     code        char(5) CONSTRAINT firstkey PRIMARY KEY,
692     title       varchar(40) NOT NULL,
693     did         integer NOT NULL,
694     date_prod   date,
695     kind        varchar(10),
696     len         interval hour to minute
697 );
698 </programlisting>
699
700 <programlisting>
701 CREATE TABLE distributors (
702      did    integer PRIMARY KEY DEFAULT nextval('serial'),
703      name   varchar(40) NOT NULL CHECK (name &lt;&gt; '')
704 );
705 </programlisting>
706   </para>
707
708   <para>
709    Create a table with a 2-dimensional array:
710
711 <programlisting>
712 CREATE TABLE array (
713     vector  int[][]
714 );
715 </programlisting>
716   </para>
717
718   <para>
719    Define a unique table constraint for the table
720    <literal>films</literal>.  Unique table constraints can be defined
721    on one or more columns of the table.
722
723 <programlisting>
724 CREATE TABLE films (
725     code        char(5),
726     title       varchar(40),
727     did         integer,
728     date_prod   date,
729     kind        varchar(10),
730     len         interval hour to minute,
731     CONSTRAINT production UNIQUE(date_prod)
732 );
733 </programlisting>
734   </para>
735
736   <para>
737    Define a check column constraint:
738
739 <programlisting>
740 CREATE TABLE distributors (
741     did     integer CHECK (did > 100),
742     name    varchar(40)
743 );
744 </programlisting>
745   </para>
746
747   <para>
748    Define a check table constraint:
749
750 <programlisting>
751 CREATE TABLE distributors (
752     did     integer,
753     name    varchar(40)
754     CONSTRAINT con1 CHECK (did > 100 AND name &lt;&gt; '')
755 );
756 </programlisting>
757   </para>
758
759   <para>
760    Define a primary key table constraint for the table
761    <structname>films</>.  Primary key table constraints can be defined
762    on one or more columns of the table.
763
764 <programlisting>
765 CREATE TABLE films (
766     code        char(5),
767     title       varchar(40),
768     did         integer,
769     date_prod   date,
770     kind        varchar(10),
771     len         interval hour to minute,
772     CONSTRAINT code_title PRIMARY KEY(code,title)
773 );
774 </programlisting>
775   </para>
776
777   <para>
778    Define a primary key constraint for table
779    <structname>distributors</>.  The following two examples are
780    equivalent, the first using the table constraint syntax, the second
781    the column constraint notation.
782
783 <programlisting>
784 CREATE TABLE distributors (
785     did     integer,
786     name    varchar(40),
787     PRIMARY KEY(did)
788 );
789 </programlisting>
790
791 <programlisting>
792 CREATE TABLE distributors (
793     did     integer PRIMARY KEY,
794     name    varchar(40)
795 );
796 </programlisting>
797   </para>
798
799   <para>
800    This assigns a literal constant default value for the column
801    <literal>name</literal>, arranges for the default value of column
802    <literal>did</literal> to be generated by selecting the next value
803    of a sequence object, and makes the default value of
804    <literal>modtime</literal> be the time at which the row is
805    inserted.
806
807 <programlisting>
808 CREATE TABLE distributors (
809     name      varchar(40) DEFAULT 'Luso Films',
810     did       integer DEFAULT nextval('distributors_serial'),
811     modtime   timestamp DEFAULT current_timestamp
812 );
813 </programlisting>
814   </para>
815
816   <para>
817    Define two <literal>NOT NULL</> column constraints on the table
818    <classname>distributors</classname>, one of which is explicitly
819    given a name:
820
821 <programlisting>
822 CREATE TABLE distributors (
823     did     integer CONSTRAINT no_null NOT NULL,
824     name    varchar(40) NOT NULL
825 );
826 </programlisting>
827     </para>
828
829     <para>
830      Define a unique constraint for the <literal>name</literal> column:
831
832 <programlisting>
833 CREATE TABLE distributors (
834     did     integer,
835     name    varchar(40) UNIQUE
836 );
837 </programlisting>
838
839      The above is equivalent to the following specified as a table constraint:
840
841 <programlisting>
842 CREATE TABLE distributors (
843     did     integer,
844     name    varchar(40),
845     UNIQUE(name)
846 );
847 </programlisting>
848   </para>
849
850   <para>
851    Create table <structname>cinemas</> in tablespace <structname>diskvol1</>:
852
853 <programlisting>
854 CREATE TABLE cinemas (
855         id serial,
856         name text,
857         location text
858 ) TABLESPACE diskvol1;
859 </programlisting>
860   </para>
861
862  </refsect1>
863
864  <refsect1 id="SQL-CREATETABLE-compatibility">
865   <title id="SQL-CREATETABLE-compatibility-title">Compatibility</title>
866
867   <para>
868    The <command>CREATE TABLE</command> command conforms to SQL-92 and
869    to a subset of SQL:1999, with exceptions listed below.
870   </para>
871
872   <refsect2>
873    <title>Temporary Tables</title>
874
875    <para>
876     Although the syntax of <literal>CREATE TEMPORARY TABLE</literal>
877     resembles that of the SQL standard, the effect is not the same.  In the
878     standard,
879     temporary tables are defined just once and automatically exist (starting
880     with empty contents) in every session that needs them.
881     <productname>PostgreSQL</productname> instead
882     requires each session to issue its own <literal>CREATE TEMPORARY
883     TABLE</literal> command for each temporary table to be used.  This allows
884     different sessions to use the same temporary table name for different
885     purposes, whereas the standard's approach constrains all instances of a
886     given temporary table name to have the same table structure.
887    </para>
888
889    <para>
890     The standard's definition of the behavior of temporary tables is
891     widely ignored.  <productname>PostgreSQL</productname>'s behavior
892     on this point is similar to that of several other SQL databases.
893    </para>
894
895    <para>
896     The standard's distinction between global and local temporary tables
897     is not in <productname>PostgreSQL</productname>, since that distinction
898     depends on the concept of modules, which
899     <productname>PostgreSQL</productname> does not have.
900     For compatibility's sake, <productname>PostgreSQL</productname> will
901     accept the <literal>GLOBAL</literal> and <literal>LOCAL</literal> keywords
902     in a temporary table declaration, but they have no effect.
903    </para>
904
905    <para>
906     The <literal>ON COMMIT</literal> clause for temporary tables
907     also resembles the SQL standard, but has some differences.
908     If the <literal>ON COMMIT</> clause is omitted, SQL specifies that the
909     default behavior is <literal>ON COMMIT DELETE ROWS</>.  However, the
910     default behavior in <productname>PostgreSQL</productname> is
911     <literal>ON COMMIT PRESERVE ROWS</literal>.  The <literal>ON COMMIT
912     DROP</literal> option does not exist in SQL.
913    </para>
914   </refsect2>
915
916   <refsect2>
917    <title>Column Check Constraints</title>
918
919    <para>
920     The SQL standard says that <literal>CHECK</> column constraints
921     may only refer to the column they apply to; only <literal>CHECK</>
922     table constraints may refer to multiple columns.
923     <productname>PostgreSQL</productname> does not enforce this
924     restriction; it treats column and table check constraints alike.
925    </para>
926   </refsect2>
927
928   <refsect2>
929    <title><literal>NULL</literal> <quote>Constraint</quote></title>
930
931    <para>
932     The <literal>NULL</> <quote>constraint</quote> (actually a
933     non-constraint) is a <productname>PostgreSQL</productname>
934     extension to the SQL standard that is included for compatibility with some
935     other database systems (and for symmetry with the <literal>NOT
936     NULL</literal> constraint).  Since it is the default for any
937     column, its presence is simply noise.
938    </para>
939   </refsect2>
940
941   <refsect2>
942    <title>Inheritance</title>
943
944    <para>
945     Multiple inheritance via the <literal>INHERITS</literal> clause is
946     a <productname>PostgreSQL</productname> language extension.
947     SQL:1999 (but not SQL-92) defines single inheritance using a
948     different syntax and different semantics.  SQL:1999-style
949     inheritance is not yet supported by
950     <productname>PostgreSQL</productname>.
951    </para>
952   </refsect2>
953
954   <refsect2>
955    <title>Object IDs</title>
956
957    <para>
958     The <productname>PostgreSQL</productname> concept of OIDs is not
959     standard.
960    </para>
961   </refsect2>
962
963   <refsect2>
964    <title>Zero-column tables</title>
965
966    <para>
967     <productname>PostgreSQL</productname> allows a table of no columns
968     to be created (for example, <literal>CREATE TABLE foo();</>).  This
969     is an extension from the SQL standard, which does not allow zero-column
970     tables.  Zero-column tables are not in themselves very useful, but
971     disallowing them creates odd special cases for <command>ALTER TABLE
972     DROP COLUMN</>, so it seems cleaner to ignore this spec restriction.
973    </para>
974   </refsect2>
975
976   <refsect2>
977    <title>Tablespaces</title>
978
979    <para>
980     The <productname>PostgreSQL</productname> concept of tablespaces is not
981     part of the standard.  Hence, the clauses <literal>TABLESPACE</literal>
982     and <literal>USING INDEX TABLESPACE</literal> are extensions.
983    </para>
984   </refsect2>
985  </refsect1>
986
987
988  <refsect1>
989   <title>See Also</title>
990
991   <simplelist type="inline">
992    <member><xref linkend="sql-altertable" endterm="sql-altertable-title"></member>
993    <member><xref linkend="sql-droptable" endterm="sql-droptable-title"></member>
994    <member><xref linkend="sql-createtablespace" endterm="sql-createtablespace-title"></member>
995   </simplelist>
996  </refsect1>
997 </refentry>
998
999 <!-- Keep this comment at the end of the file
1000 Local variables:
1001 mode: sgml
1002 sgml-omittag:nil
1003 sgml-shorttag:t
1004 sgml-minimize-attributes:nil
1005 sgml-always-quote-attributes:t
1006 sgml-indent-step:1
1007 sgml-indent-data:t
1008 sgml-parent-document:nil
1009 sgml-default-dtd-file:"../reference.ced"
1010 sgml-exposed-tags:nil
1011 sgml-local-catalogs:"/usr/lib/sgml/catalog"
1012 sgml-local-ecat-files:nil
1013 End:
1014 -->