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