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