]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ddl.sgml
Add functions pg_start_backup, pg_stop_backup to create backup label
[postgresql] / doc / src / sgml / ddl.sgml
1 <!-- $PostgreSQL: pgsql/doc/src/sgml/ddl.sgml,v 1.27 2004/08/03 20:32:30 tgl Exp $ -->
2
3 <chapter id="ddl">
4  <title>Data Definition</title>
5
6  <para>
7   This chapter covers how one creates the database structures that
8   will hold one's data.  In a relational database, the raw data is
9   stored in tables, so the majority of this chapter is devoted to
10   explaining how tables are created and modified and what features are
11   available to control what data is stored in the tables.
12   Subsequently, we discuss how tables can be organized into
13   schemas, and how privileges can be assigned to tables.  Finally,
14   we will briefly look at other features that affect the data storage,
15   such as views, functions, and triggers.
16  </para>
17
18  <sect1 id="ddl-basics">
19   <title>Table Basics</title>
20
21   <indexterm zone="ddl-basics">
22    <primary>table</primary>
23   </indexterm>
24
25   <indexterm>
26    <primary>row</primary>
27   </indexterm>
28
29   <indexterm>
30    <primary>column</primary>
31   </indexterm>
32
33   <para>
34    A table in a relational database is much like a table on paper: It
35    consists of rows and columns.  The number and order of the columns
36    is fixed, and each column has a name.  The number of rows is
37    variable -- it reflects how much data is stored at a given moment.
38    SQL does not make any guarantees about the order of the rows in a
39    table.  When a table is read, the rows will appear in random order,
40    unless sorting is explicitly requested.  This is covered in <xref
41    linkend="queries">.  Furthermore, SQL does not assign unique
42    identifiers to rows, so it is possible to have several completely
43    identical rows in a table.  This is a consequence of the
44    mathematical model that underlies SQL but is usually not desirable.
45    Later in this chapter we will see how to deal with this issue.
46   </para>
47
48   <para>
49    Each column has a data type.  The data type constrains the set of
50    possible values that can be assigned to a column and assigns
51    semantics to the data stored in the column so that it can be used
52    for computations.  For instance, a column declared to be of a
53    numerical type will not accept arbitrary text strings, and the data
54    stored in such a column can be used for mathematical computations.
55    By contrast, a column declared to be of a character string type
56    will accept almost any kind of data but it does not lend itself to
57    mathematical calculations, although other operations such as string
58    concatenation are available.
59   </para>
60
61   <para>
62    <productname>PostgreSQL</productname> includes a sizable set of
63    built-in data types that fit many applications.  Users can also
64    define their own data types.  Most built-in data types have obvious
65    names and semantics, so we defer a detailed explanation to <xref
66    linkend="datatype">.  Some of the frequently used data types are
67    <type>integer</type> for whole numbers, <type>numeric</type> for
68    possibly fractional numbers, <type>text</type> for character
69    strings, <type>date</type> for dates, <type>time</type> for
70    time-of-day values, and <type>timestamp</type> for values
71    containing both date and time.
72   </para>
73
74   <indexterm>
75    <primary>table</primary>
76    <secondary>creating</secondary>
77   </indexterm>
78
79   <para>
80    To create a table, you use the aptly named <command>CREATE
81    TABLE</command> command.  In this command you specify at least a
82    name for the new table, the names of the columns and the data type
83    of each column.  For example:
84 <programlisting>
85 CREATE TABLE my_first_table (
86     first_column text,
87     second_column integer
88 );
89 </programlisting>
90    This creates a table named <literal>my_first_table</literal> with
91    two columns.  The first column is named
92    <literal>first_column</literal> and has a data type of
93    <type>text</type>; the second column has the name
94    <literal>second_column</literal> and the type <type>integer</type>.
95    The table and column names follow the identifier syntax explained
96    in <xref linkend="sql-syntax-identifiers">.  The type names are
97    usually also identifiers, but there are some exceptions.  Note that the
98    column list is comma-separated and surrounded by parentheses.
99   </para>
100
101   <para>
102    Of course, the previous example was heavily contrived.  Normally,
103    you would give names to your tables and columns that convey what
104    kind of data they store.  So let's look at a more realistic
105    example:
106 <programlisting>
107 CREATE TABLE products (
108     product_no integer,
109     name text,
110     price numeric
111 );
112 </programlisting>
113    (The <type>numeric</type> type can store fractional components, as
114    would be typical of monetary amounts.)
115   </para>
116
117   <tip>
118    <para>
119     When you create many interrelated tables it is wise to choose a
120     consistent naming pattern for the tables and columns.  For
121     instance, there is a choice of using singular or plural nouns for
122     table names, both of which are favored by some theorist or other.
123    </para>
124   </tip>
125
126   <para>
127    There is a limit on how many columns a table can contain.
128    Depending on the column types, it is between 250 and 1600.
129    However, defining a table with anywhere near this many columns is
130    highly unusual and often a questionable design.
131   </para>
132
133   <indexterm>
134    <primary>table</primary>
135    <secondary>removing</secondary>
136   </indexterm>
137
138   <para>
139    If you no longer need a table, you can remove it using the
140    <command>DROP TABLE</command> command.  For example:
141 <programlisting>
142 DROP TABLE my_first_table;
143 DROP TABLE products;
144 </programlisting>
145    Attempting to drop a table that does not exist is an error.
146    Nevertheless, it is common in SQL script files to unconditionally
147    try to drop each table before creating it, ignoring the error
148    messages.
149   </para>
150
151   <para>
152    If you need to modify a table that already exists look into <xref
153    linkend="ddl-alter"> later in this chapter.
154   </para>
155
156   <para>
157    With the tools discussed so far you can create fully functional
158    tables.  The remainder of this chapter is concerned with adding
159    features to the table definition to ensure data integrity,
160    security, or convenience.  If you are eager to fill your tables with
161    data now you can skip ahead to <xref linkend="dml"> and read the
162    rest of this chapter later.
163   </para>
164  </sect1>
165
166  <sect1 id="ddl-system-columns">
167   <title>System Columns</title>
168
169   <para>
170    Every table has several <firstterm>system columns</> that are
171    implicitly defined by the system.  Therefore, these names cannot be
172    used as names of user-defined columns.  (Note that these
173    restrictions are separate from whether the name is a key word or
174    not; quoting a name will not allow you to escape these
175    restrictions.)  You do not really need to be concerned about these
176    columns, just know they exist.
177   </para>
178
179   <indexterm>
180    <primary>column</primary>
181    <secondary>system column</secondary>
182   </indexterm>
183
184   <variablelist>
185    <varlistentry>
186     <term><structfield>oid</></term>
187     <listitem>
188      <para>
189       <indexterm>
190        <primary>OID</primary>
191        <secondary>column</secondary>
192       </indexterm>
193       The object identifier (object ID) of a row.  This is a serial
194       number that is automatically added by
195       <productname>PostgreSQL</productname> to all table rows (unless
196       the table was created using <literal>WITHOUT OIDS</literal>, in which
197       case this column is not present).  This column is of type
198       <type>oid</type> (same name as the column); see <xref
199       linkend="datatype-oid"> for more information about the type.
200      </para>
201     </listitem>
202    </varlistentry>
203
204    <varlistentry>
205     <term><structfield>tableoid</></term>
206     <listitem>
207      <indexterm>
208       <primary>tableoid</primary>
209      </indexterm>
210
211      <para>
212       The OID of the table containing this row.  This column is
213       particularly handy for queries that select from inheritance
214       hierarchies, since without it, it's difficult to tell which
215       individual table a row came from.  The
216       <structfield>tableoid</structfield> can be joined against the
217       <structfield>oid</structfield> column of
218       <structname>pg_class</structname> to obtain the table name.
219      </para>
220     </listitem>
221    </varlistentry>
222
223    <varlistentry>
224     <term><structfield>xmin</></term>
225     <listitem>
226      <indexterm>
227       <primary>xmin</primary>
228      </indexterm>
229
230      <para>
231       The identity (transaction ID) of the inserting transaction for
232       this row version.  (A row version is an individual state of a
233       row; each update of a row creates a new row version for the same
234       logical row.)
235      </para>
236     </listitem>
237    </varlistentry>
238
239    <varlistentry>
240     <term><structfield>cmin</></term>
241     <listitem>
242      <indexterm>
243       <primary>cmin</primary>
244      </indexterm>
245
246      <para>
247       The command identifier (starting at zero) within the inserting
248       transaction.
249      </para>
250     </listitem>
251    </varlistentry>
252
253    <varlistentry>
254     <term><structfield>xmax</></term>
255     <listitem>
256      <indexterm>
257       <primary>xmax</primary>
258      </indexterm>
259
260      <para>
261       The identity (transaction ID) of the deleting transaction, or
262       zero for an undeleted row version.  It is possible for this column to
263       be nonzero in a visible row version: That usually indicates that the
264       deleting transaction hasn't committed yet, or that an attempted
265       deletion was rolled back.
266      </para>
267     </listitem>
268    </varlistentry>
269
270    <varlistentry>
271     <term><structfield>cmax</></term>
272     <listitem>
273      <indexterm>
274       <primary>cmax</primary>
275      </indexterm>
276
277      <para>
278       The command identifier within the deleting transaction, or zero.
279      </para>
280     </listitem>
281    </varlistentry>
282
283    <varlistentry>
284     <term><structfield>ctid</></term>
285     <listitem>
286      <indexterm>
287       <primary>ctid</primary>
288      </indexterm>
289
290      <para>
291       The physical location of the row version within its table.  Note that
292       although the <structfield>ctid</structfield> can be used to
293       locate the row version very quickly, a row's
294       <structfield>ctid</structfield> will change each time it is
295       updated or moved by <command>VACUUM FULL</>.  Therefore
296       <structfield>ctid</structfield> is useless as a long-term row
297       identifier.  The OID, or even better a user-defined serial
298       number, should be used to identify logical rows.
299      </para>
300     </listitem>
301    </varlistentry>
302   </variablelist>
303
304    <para>
305     OIDs are 32-bit quantities and are assigned from a single
306     cluster-wide counter.  In a large or long-lived database, it is
307     possible for the counter to wrap around.  Hence, it is bad
308     practice to assume that OIDs are unique, unless you take steps to
309     ensure that this is the case.  If you need to identify the rows in
310     a table, using a sequence generator is strongly recommended.
311     However, OIDs can be used as well, provided that a few additional
312     precautions are taken:
313
314     <itemizedlist>
315      <listitem>
316       <para>
317        A unique constraint should be created on the OID column of each
318        table for which the OID will be used to identify rows.
319       </para>
320      </listitem>
321      <listitem>
322       <para>
323        OIDs should never be assumed to be unique across tables; use
324        the combination of <structfield>tableoid</> and row OID if you
325        need a database-wide identifier.
326       </para>
327      </listitem>
328      <listitem>
329       <para>
330        The tables in question should be created using <literal>WITH
331        OIDS</literal> to ensure forward compatibility with future
332        releases of <productname>PostgreSQL</productname> in which OIDs
333        are not included in all tables by default.
334       </para>
335      </listitem>
336     </itemizedlist>
337    </para>
338
339    <para>
340     Transaction identifiers are also 32-bit quantities.  In a
341     long-lived database it is possible for transaction IDs to wrap
342     around.  This is not a fatal problem given appropriate maintenance
343     procedures; see <xref linkend="maintenance"> for details.  It is
344     unwise, however, to depend on the uniqueness of transaction IDs
345     over the long term (more than one billion transactions).
346    </para>
347
348    <para>
349     Command
350     identifiers are also 32-bit quantities.  This creates a hard limit
351     of 2<superscript>32</> (4 billion) <acronym>SQL</acronym> commands
352     within a single transaction.  In practice this limit is not a
353     problem --- note that the limit is on number of
354     <acronym>SQL</acronym> commands, not number of rows processed.
355    </para>
356  </sect1>
357
358  <sect1 id="ddl-default">
359   <title>Default Values</title>
360
361   <indexterm zone="ddl-default">
362    <primary>default value</primary>
363   </indexterm>
364
365   <para>
366    A column can be assigned a default value.  When a new row is
367    created and no values are specified for some of the columns, the
368    columns will be filled with their respective default values.  A
369    data manipulation command can also request explicitly that a column
370    be set to its default value, without knowing what this value is.
371    (Details about data manipulation commands are in <xref linkend="dml">.)
372   </para>
373
374   <para>
375    <indexterm><primary>null value</primary><secondary>default value</secondary></indexterm>
376    If no default value is declared explicitly, the null value is the
377    default value.  This usually makes sense because a null value can
378    be thought to represent unknown data.
379   </para>
380
381   <para>
382    In a table definition, default values are listed after the column
383    data type.  For example:
384 <programlisting>
385 CREATE TABLE products (
386     product_no integer,
387     name text,
388     price numeric <emphasis>DEFAULT 9.99</emphasis>
389 );
390 </programlisting>
391   </para>
392
393   <para>
394    The default value may be a scalar expression, which will be
395    evaluated whenever the default value is inserted
396    (<emphasis>not</emphasis> when the table is created).
397   </para>
398  </sect1>
399
400  <sect1 id="ddl-constraints">
401   <title>Constraints</title>
402
403   <indexterm zone="ddl-constraints">
404    <primary>constraint</primary>
405   </indexterm>
406
407   <para>
408    Data types are a way to limit the kind of data that can be stored
409    in a table.  For many applications, however, the constraint they
410    provide is too coarse.  For example, a column containing a product
411    price should probably only accept positive values.  But there is no
412    data type that accepts only positive numbers.  Another issue is
413    that you might want to constrain column data with respect to other
414    columns or rows.  For example, in a table containing product
415    information, there should only be one row for each product number.
416   </para>
417
418   <para>
419    To that end, SQL allows you to define constraints on columns and
420    tables.  Constraints give you as much control over the data in your
421    tables as you wish.  If a user attempts to store data in a column
422    that would violate a constraint, an error is raised.  This applies
423    even if the value came from the default value definition.
424   </para>
425
426   <sect2>
427    <title>Check Constraints</title>
428
429    <indexterm>
430     <primary>check constraint</primary>
431    </indexterm>
432
433    <indexterm>
434     <primary>constraint</primary>
435     <secondary>check</secondary>
436    </indexterm>
437
438    <para>
439     A check constraint is the most generic constraint type.  It allows
440     you to specify that the value in a certain column must satisfy an
441     arbitrary expression.  For instance, to require positive product
442     prices, you could use:
443 <programlisting>
444 CREATE TABLE products (
445     product_no integer,
446     name text,
447     price numeric <emphasis>CHECK (price > 0)</emphasis>
448 );
449 </programlisting>
450    </para>
451
452    <para>
453     As you see, the constraint definition comes after the data type,
454     just like default value definitions.  Default values and
455     constraints can be listed in any order.  A check constraint
456     consists of the key word <literal>CHECK</literal> followed by an
457     expression in parentheses.  The check constraint expression should
458     involve the column thus constrained, otherwise the constraint
459     would not make too much sense.
460    </para>
461
462    <indexterm>
463     <primary>constraint</primary>
464     <secondary>name</secondary>
465    </indexterm>
466
467    <para>
468     You can also give the constraint a separate name.  This clarifies
469     error messages and allows you to refer to the constraint when you
470     need to change it.  The syntax is:
471 <programlisting>
472 CREATE TABLE products (
473     product_no integer,
474     name text,
475     price numeric <emphasis>CONSTRAINT positive_price</emphasis> CHECK (price > 0)
476 );
477 </programlisting>
478     So, to specify a named constraint, use the key word
479     <literal>CONSTRAINT</literal> followed by an identifier followed
480     by the constraint definition.
481    </para>
482
483    <para>
484     A check constraint can also refer to several columns.  Say you
485     store a regular price and a discounted price, and you want to
486     ensure that the discounted price is lower than the regular price.
487 <programlisting>
488 CREATE TABLE products (
489     product_no integer,
490     name text,
491     price numeric CHECK (price > 0),
492     discounted_price numeric CHECK (discounted_price > 0),
493     CHECK (price > discounted_price)
494 );
495 </programlisting>
496    </para>
497
498    <para>
499     The first two constraints should look familiar.  The third one
500     uses a new syntax.  It is not attached to a particular column,
501     instead it appears as a separate item in the comma-separated
502     column list.  Column definitions and these constraint
503     definitions can be listed in mixed order.
504    </para>
505
506    <para>
507     We say that the first two constraints are column constraints, whereas the
508     third one is a table constraint because it is written separately
509     from the column definitions.  Column constraints can also be
510     written as table constraints, while the reverse is not necessarily
511     possible.  The above example could also be written as
512 <programlisting>
513 CREATE TABLE products (
514     product_no integer,
515     name text,
516     price numeric,
517     CHECK (price > 0),
518     discounted_price numeric,
519     CHECK (discounted_price > 0),
520     CHECK (price > discounted_price)
521 );
522 </programlisting>
523     or even
524 <programlisting>
525 CREATE TABLE products (
526     product_no integer,
527     name text,
528     price numeric CHECK (price > 0),
529     discounted_price numeric,
530     CHECK (discounted_price > 0 AND price > discounted_price)
531 );
532 </programlisting>
533     It's a matter of taste.
534    </para>
535
536    <indexterm>
537     <primary>null value</primary>
538     <secondary sortas="check constraints">with check constraints</secondary>
539    </indexterm>
540
541    <para>
542     It should be noted that a check constraint is satisfied if the
543     check expression evaluates to true or the null value.  Since most
544     expressions will evaluate to the null value if one operand is null,
545     they will not prevent null values in the constrained columns.  To
546     ensure that a column does not contain null values, the not-null
547     constraint described in the next section should be used.
548    </para>
549   </sect2>
550
551   <sect2>
552    <title>Not-Null Constraints</title>
553
554    <indexterm>
555     <primary>not-null constraint</primary>
556    </indexterm>
557
558    <indexterm>
559     <primary>constraint</primary>
560     <secondary>NOT NULL</secondary>
561    </indexterm>
562
563    <para>
564     A not-null constraint simply specifies that a column must not
565     assume the null value.  A syntax example:
566 <programlisting>
567 CREATE TABLE products (
568     product_no integer <emphasis>NOT NULL</emphasis>,
569     name text <emphasis>NOT NULL</emphasis>,
570     price numeric
571 );
572 </programlisting>
573    </para>
574
575    <para>
576     A not-null constraint is always written as a column constraint.  A
577     not-null constraint is functionally equivalent to creating a check
578     constraint <literal>CHECK (<replaceable>column_name</replaceable>
579     IS NOT NULL)</literal>, but in
580     <productname>PostgreSQL</productname> creating an explicit
581     not-null constraint is more efficient.  The drawback is that you
582     cannot give explicit names to not-null constraints created that
583     way.
584    </para>
585
586    <para>
587     Of course, a column can have more than one constraint.  Just write
588     the constraints after one another:
589 <programlisting>
590 CREATE TABLE products (
591     product_no integer NOT NULL,
592     name text NOT NULL,
593     price numeric NOT NULL CHECK (price > 0)
594 );
595 </programlisting>
596     The order doesn't matter.  It does not necessarily determine in which
597     order the constraints are checked.
598    </para>
599
600    <para>
601     The <literal>NOT NULL</literal> constraint has an inverse: the
602     <literal>NULL</literal> constraint.  This does not mean that the
603     column must be null, which would surely be useless.  Instead, this
604     simply defines the default behavior that the column may be null.
605     The <literal>NULL</literal> constraint is not defined in the SQL
606     standard and should not be used in portable applications.  (It was
607     only added to <productname>PostgreSQL</productname> to be
608     compatible with some other database systems.)  Some users, however,
609     like it because it makes it easy to toggle the constraint in a
610     script file.  For example, you could start with
611 <programlisting>
612 CREATE TABLE products (
613     product_no integer NULL,
614     name text NULL,
615     price numeric NULL
616 );
617 </programlisting>
618     and then insert the <literal>NOT</literal> key word where desired.
619    </para>
620
621    <tip>
622     <para>
623      In most database designs the majority of columns should be marked
624      not null.
625     </para>
626    </tip>
627   </sect2>
628
629   <sect2>
630    <title>Unique Constraints</title>
631
632    <indexterm>
633     <primary>unique constraint</primary>
634    </indexterm>
635
636    <indexterm>
637     <primary>constraint</primary>
638     <secondary>unique</secondary>
639    </indexterm>
640
641    <para>
642     Unique constraints ensure that the data contained in a column or a
643     group of columns is unique with respect to all the rows in the
644     table.  The syntax is
645 <programlisting>
646 CREATE TABLE products (
647     product_no integer <emphasis>UNIQUE</emphasis>,
648     name text,
649     price numeric
650 );
651 </programlisting>
652     when written as a column constraint, and
653 <programlisting>
654 CREATE TABLE products (
655     product_no integer,
656     name text,
657     price numeric,
658     <emphasis>UNIQUE (product_no)</emphasis>
659 );
660 </programlisting>
661     when written as a table constraint.
662    </para>
663
664    <para>
665     If a unique constraint refers to a group of columns, the columns
666     are listed separated by commas:
667 <programlisting>
668 CREATE TABLE example (
669     a integer,
670     b integer,
671     c integer,
672     <emphasis>UNIQUE (a, c)</emphasis>
673 );
674 </programlisting>
675    </para>
676
677    <para>
678     It is also possible to assign names to unique constraints:
679 <programlisting>
680 CREATE TABLE products (
681     product_no integer <emphasis>CONSTRAINT must_be_different</emphasis> UNIQUE,
682     name text,
683     price numeric
684 );
685 </programlisting>
686    </para>
687
688    <indexterm>
689     <primary>null value</primary>
690     <secondary sortas="unique constraints">with unique constraints</secondary>
691    </indexterm>
692
693    <para>
694     In general, a unique constraint is violated when there are (at
695     least) two rows in the table where the values of each of the
696     corresponding columns that are part of the constraint are equal.
697     However, null values are not considered equal in this
698     consideration.  That means even in the presence of a
699     unique constraint it is possible to store an unlimited number of
700     rows that contain a null value in at least one of the constrained
701     columns.  This behavior conforms to the SQL standard, but we have
702     heard that other SQL databases may not follow this rule.  So be
703     careful when developing applications that are intended to be
704     portable.
705    </para>
706   </sect2>
707
708   <sect2>
709    <title>Primary Keys</title>
710
711    <indexterm>
712     <primary>primary key</primary>
713    </indexterm>
714
715    <indexterm>
716     <primary>constraint</primary>
717     <secondary>primary key</secondary>
718    </indexterm>
719
720    <para>
721     Technically, a primary key constraint is simply a combination of a
722     unique constraint and a not-null constraint.  So, the following
723     two table definitions accept the same data:
724 <programlisting>
725 CREATE TABLE products (
726     product_no integer UNIQUE NOT NULL,
727     name text,
728     price numeric
729 );
730 </programlisting>
731
732 <programlisting>
733 CREATE TABLE products (
734     product_no integer <emphasis>PRIMARY KEY</emphasis>,
735     name text,
736     price numeric
737 );
738 </programlisting>
739    </para>
740
741    <para>
742     Primary keys can also constrain more than one column; the syntax
743     is similar to unique constraints:
744 <programlisting>
745 CREATE TABLE example (
746     a integer,
747     b integer,
748     c integer,
749     <emphasis>PRIMARY KEY (a, c)</emphasis>
750 );
751 </programlisting>
752    </para>
753
754    <para>
755     A primary key indicates that a column or group of columns can be
756     used as a unique identifier for rows in the table.  (This is a
757     direct consequence of the definition of a primary key.  Note that
758     a unique constraint does not, by itself, provide a unique identifier
759     because it does not exclude null values.)  This is useful both for
760     documentation purposes and for client applications.  For example,
761     a GUI application that allows modifying row values probably needs
762     to know the primary key of a table to be able to identify rows
763     uniquely.
764    </para>
765
766    <para>
767     A table can have at most one primary key (while it can have many
768     unique and not-null constraints).  Relational database theory
769     dictates that every table must have a primary key.  This rule is
770     not enforced by <productname>PostgreSQL</productname>, but it is
771     usually best to follow it.
772    </para>
773   </sect2>
774
775   <sect2 id="ddl-constraints-fk">
776    <title>Foreign Keys</title>
777
778    <indexterm>
779     <primary>foreign key</primary>
780    </indexterm>
781
782    <indexterm>
783     <primary>constraint</primary>
784     <secondary>foreign key</secondary>
785    </indexterm>
786
787    <indexterm>
788     <primary>referential integrity</primary>
789    </indexterm>
790
791    <para>
792     A foreign key constraint specifies that the values in a column (or
793     a group of columns) must match the values appearing in some row
794     of another table.
795     We say this maintains the <firstterm>referential
796     integrity</firstterm> between two related tables.
797    </para>
798
799    <para>
800     Say you have the product table that we have used several times already:
801 <programlisting>
802 CREATE TABLE products (
803     product_no integer PRIMARY KEY,
804     name text,
805     price numeric
806 );
807 </programlisting>
808     Let's also assume you have a table storing orders of those
809     products.  We want to ensure that the orders table only contains
810     orders of products that actually exist.  So we define a foreign
811     key constraint in the orders table that references the products
812     table:
813 <programlisting>
814 CREATE TABLE orders (
815     order_id integer PRIMARY KEY,
816     product_no integer <emphasis>REFERENCES products (product_no)</emphasis>,
817     quantity integer
818 );
819 </programlisting>
820     Now it is impossible to create orders with
821     <structfield>product_no</structfield> entries that do not appear in the
822     products table.
823    </para>
824
825    <para>
826     We say that in this situation the orders table is the
827     <firstterm>referencing</firstterm> table and the products table is
828     the <firstterm>referenced</firstterm> table.  Similarly, there are
829     referencing and referenced columns.
830    </para>
831
832    <para>
833     You can also shorten the above command to
834 <programlisting>
835 CREATE TABLE orders (
836     order_id integer PRIMARY KEY,
837     product_no integer REFERENCES products,
838     quantity integer
839 );
840 </programlisting>
841     because in absence of a column list the primary key of the
842     referenced table is used as the referenced column.
843    </para>
844
845    <para>
846     A foreign key can also constrain and reference a group of columns.
847     As usual, it then needs to be written in table constraint form.
848     Here is a contrived syntax example:
849 <programlisting>
850 CREATE TABLE t1 (
851   a integer PRIMARY KEY,
852   b integer,
853   c integer,
854   <emphasis>FOREIGN KEY (b, c) REFERENCES other_table (c1, c2)</emphasis>
855 );
856 </programlisting>
857     Of course, the number and type of the constrained columns needs to
858     match the number and type of the referenced columns.
859    </para>
860
861    <para>
862     A table can contain more than one foreign key constraint.  This is
863     used to implement many-to-many relationships between tables.  Say
864     you have tables about products and orders, but now you want to
865     allow one order to contain possibly many products (which the
866     structure above did not allow).  You could use this table structure:
867 <programlisting>
868 CREATE TABLE products (
869     product_no integer PRIMARY KEY,
870     name text,
871     price numeric
872 );
873
874 CREATE TABLE orders (
875     order_id integer PRIMARY KEY,
876     shipping_address text,
877     ...
878 );
879
880 CREATE TABLE order_items (
881     product_no integer REFERENCES products,
882     order_id integer REFERENCES orders,
883     quantity integer,
884     PRIMARY KEY (product_no, order_id)
885 );
886 </programlisting>
887     Note also that the primary key overlaps with the foreign keys in
888     the last table.
889    </para>
890
891    <indexterm>
892     <primary>CASCADE</primary>
893     <secondary>foreign key action</secondary>
894    </indexterm>
895
896    <indexterm>
897     <primary>RESTRICT</primary>
898     <secondary>foreign key action</secondary>
899    </indexterm>
900
901    <para>
902     We know that the foreign keys disallow creation of orders that
903     do not relate to any products.  But what if a product is removed
904     after an order is created that references it?  SQL allows you to
905     specify that as well.  Intuitively, we have a few options:
906     <itemizedlist spacing="compact">
907      <listitem><para>Disallow deleting a referenced product</para></listitem>
908      <listitem><para>Delete the orders as well</para></listitem>
909      <listitem><para>Something else?</para></listitem>
910     </itemizedlist>
911    </para>
912
913    <para>
914     To illustrate this, let's implement the following policy on the
915     many-to-many relationship example above: when someone wants to
916     remove a product that is still referenced by an order (via
917     <literal>order_items</literal>), we disallow it.  If someone
918     removes an order, the order items are removed as well.
919 <programlisting>
920 CREATE TABLE products (
921     product_no integer PRIMARY KEY,
922     name text,
923     price numeric
924 );
925
926 CREATE TABLE orders (
927     order_id integer PRIMARY KEY,
928     shipping_address text,
929     ...
930 );
931
932 CREATE TABLE order_items (
933     product_no integer REFERENCES products <emphasis>ON DELETE RESTRICT</emphasis>,
934     order_id integer REFERENCES orders <emphasis>ON DELETE CASCADE</emphasis>,
935     quantity integer,
936     PRIMARY KEY (product_no, order_id)
937 );
938 </programlisting>
939    </para>
940
941    <para>
942     Restricting and cascading deletes are the two most common options.
943     <literal>RESTRICT</literal> can also be written as <literal>NO
944     ACTION</literal> and it's also the default if you do not specify
945     anything.  There are two other options for what should happen with
946     the foreign key columns when a primary key is deleted:
947     <literal>SET NULL</literal> and <literal>SET DEFAULT</literal>.
948     Note that these do not excuse you from observing any constraints.
949     For example, if an action specifies <literal>SET DEFAULT</literal>
950     but the default value would not satisfy the foreign key, the
951     deletion of the primary key will fail.
952    </para>
953
954    <para>
955     Analogous to <literal>ON DELETE</literal> there is also
956     <literal>ON UPDATE</literal> which is invoked when a primary key
957     is changed (updated).  The possible actions are the same.
958    </para>
959
960    <para>
961     More information about updating and deleting data is in <xref
962     linkend="dml">.
963    </para>
964
965    <para>
966     Finally, we should mention that a foreign key must reference
967     columns that are either a primary key or form a unique constraint.
968     If the foreign key references a unique constraint, there are some
969     additional possibilities regarding how null values are matched.
970     These are explained in the reference documentation for
971     <xref linkend="sql-createtable" endterm="sql-createtable-title">.
972    </para>
973   </sect2>
974  </sect1>
975
976  <sect1 id="ddl-inherit">
977   <title>Inheritance</title>
978
979   <remark>This section needs to be rethought.  Some of the
980   information should go into the following chapters.</remark>
981
982   <para>
983    Let's create two tables. The capitals  table  contains
984    state  capitals  which  are also cities. Naturally, the
985    capitals table should inherit from cities.
986
987 <programlisting>
988 CREATE TABLE cities (
989     name            text,
990     population      float,
991     altitude        int     -- (in ft)
992 );
993
994 CREATE TABLE capitals (
995     state           char(2)
996 ) INHERITS (cities);
997 </programlisting>
998
999    In this case, a row  of  capitals  <firstterm>inherits</firstterm>  all
1000    attributes  (name,  population,  and altitude) from its
1001    parent, cities.  The type  of  the  attribute  name  is
1002    <type>text</type>,  a  native  <productname>PostgreSQL</productname>  type
1003    for variable length character strings.  The type of the attribute
1004    population is
1005    <type>float</type>,  a  native <productname>PostgreSQL</productname> type for double precision
1006    floating-point numbers.  State capitals have  an  extra
1007    attribute, state, that shows their state.  In <productname>PostgreSQL</productname>,
1008    a  table  can inherit from zero or more other tables,
1009    and a query can reference either  all  rows  of  a
1010    table  or  all  rows of  a  table plus all of its
1011    descendants. 
1012
1013    <note>
1014     <para>
1015      The inheritance hierarchy is actually a directed acyclic graph.
1016     </para>
1017    </note>
1018   </para>
1019
1020   <para>
1021     For example, the  following  query finds the  names  of  all  cities,
1022     including  state capitals, that are located at an altitude 
1023     over 500ft:
1024
1025 <programlisting>
1026 SELECT name, altitude
1027     FROM cities
1028     WHERE altitude &gt; 500;
1029 </programlisting>
1030
1031    which returns:
1032
1033 <programlisting>
1034    name    | altitude
1035 -----------+----------
1036  Las Vegas |     2174
1037  Mariposa  |     1953
1038  Madison   |      845
1039 </programlisting>
1040   </para>
1041
1042   <para>
1043     On the other hand, the  following  query  finds
1044     all  the cities that are not state capitals and
1045     are situated at an altitude over 500ft:
1046
1047 <programlisting>
1048 SELECT name, altitude
1049     FROM ONLY cities
1050     WHERE altitude &gt; 500;
1051
1052    name    | altitude
1053 -----------+----------
1054  Las Vegas |     2174
1055  Mariposa  |     1953
1056 </programlisting>         
1057   </para>
1058
1059   <para>
1060    Here the <quote>ONLY</quote> before cities indicates that the query should
1061    be  run over only cities and not tables below cities in the
1062    inheritance hierarchy.  Many of the  commands  that  we
1063    have  already discussed -- <command>SELECT</command>,
1064    <command>UPDATE</command> and <command>DELETE</command> --
1065    support this <quote>ONLY</quote> notation.
1066   </para>
1067
1068   <para>
1069   In some cases you may wish to know which table a particular row
1070   originated from. There is a system column called
1071   <structfield>TABLEOID</structfield> in each table which can tell you the
1072   originating table:
1073
1074 <programlisting>
1075 SELECT c.tableoid, c.name, c.altitude
1076 FROM cities c
1077 WHERE c.altitude &gt; 500;
1078 </programlisting>
1079
1080    which returns:
1081
1082 <programlisting>
1083  tableoid |   name    | altitude
1084 ----------+-----------+----------
1085    139793 | Las Vegas |     2174
1086    139793 | Mariposa  |     1953
1087    139798 | Madison   |      845
1088 </programlisting>
1089
1090    (If you try to reproduce this example, you will probably get
1091    different numeric OIDs.)  By doing a join with
1092    <structname>pg_class</> you can see the actual table names:
1093
1094 <programlisting>
1095 SELECT p.relname, c.name, c.altitude
1096 FROM cities c, pg_class p
1097 WHERE c.altitude &gt; 500 and c.tableoid = p.oid;
1098 </programlisting>
1099
1100    which returns:
1101
1102 <programlisting>
1103  relname  |   name    | altitude
1104 ----------+-----------+----------
1105  cities   | Las Vegas |     2174
1106  cities   | Mariposa  |     1953
1107  capitals | Madison   |      845
1108 </programlisting>
1109    
1110   </para>
1111
1112   <note>
1113    <title>Deprecated</title> 
1114    <para>
1115      In previous versions of <productname>PostgreSQL</productname>, the
1116      default behavior was not to include child tables in queries. This was
1117      found to be error prone and is also in violation of the SQL99
1118      standard. Under the old syntax, to get the sub-tables you append
1119      <literal>*</literal> to the table name.
1120      For example
1121 <programlisting>
1122 SELECT * from cities*;
1123 </programlisting>
1124      You can still explicitly specify scanning child tables by appending
1125      <literal>*</literal>, as well as explicitly specify not scanning child tables by
1126      writing <quote>ONLY</quote>.  But beginning in version 7.1, the default
1127      behavior for an undecorated table name is to scan its child tables
1128      too, whereas before the default was not to do so.  To get the old
1129      default behavior, set the configuration option
1130      <literal>SQL_Inheritance</literal> to off, e.g.,
1131 <programlisting>
1132 SET SQL_Inheritance TO OFF;
1133 </programlisting>
1134      or add a line in your <filename>postgresql.conf</filename> file.
1135    </para>
1136   </note>
1137
1138   <para>
1139    A limitation of the inheritance feature is that indexes (including
1140    unique constraints) and foreign key constraints only apply to single
1141    tables, not to their inheritance children.  Thus, in the above example,
1142    specifying that another table's column <literal>REFERENCES cities(name)</>
1143    would allow the other table to contain city names but not capital names.
1144    This deficiency will probably be fixed in some future release.
1145   </para>
1146  </sect1>
1147
1148  <sect1 id="ddl-alter">
1149   <title>Modifying Tables</title>
1150
1151   <indexterm zone="ddl-alter">
1152    <primary>table</primary>
1153    <secondary>modifying</secondary>
1154   </indexterm>
1155
1156   <para>
1157    When you create a table and you realize that you made a mistake, or
1158    the requirements of the application changed, then you can drop the
1159    table and create it again.  But this is not a convenient option if
1160    the table is already filled with data, or if the table is
1161    referenced by other database objects (for instance a foreign key
1162    constraint).  Therefore <productname>PostgreSQL</productname>
1163    provides a family of commands to make modifications on existing
1164    tables.
1165   </para>
1166
1167   <para>
1168    You can
1169    <itemizedlist spacing="compact">
1170     <listitem>
1171      <para>Add columns,</para>
1172     </listitem>
1173     <listitem>
1174      <para>Remove columns,</para>
1175     </listitem>
1176     <listitem>
1177      <para>Add constraints,</para>
1178     </listitem>
1179     <listitem>
1180      <para>Remove constraints,</para>
1181     </listitem>
1182     <listitem>
1183      <para>Change default values,</para>
1184     </listitem>
1185     <listitem>
1186      <para>Rename columns,</para>
1187     </listitem>
1188     <listitem>
1189      <para>Rename tables.</para>
1190     </listitem>
1191    </itemizedlist>
1192
1193    All these actions are performed using the <literal>ALTER
1194    TABLE</literal> command.
1195   </para>
1196
1197   <sect2>
1198    <title>Adding a Column</title>
1199
1200    <indexterm>
1201     <primary>column</primary>
1202     <secondary>adding</secondary>
1203    </indexterm>
1204
1205    <para>
1206     To add a column, use this command:
1207 <programlisting>
1208 ALTER TABLE products ADD COLUMN description text;
1209 </programlisting>
1210     The new column will initially be filled with null values in the
1211     existing rows of the table.
1212    </para>
1213
1214    <para>
1215     You can also define a constraint on the column at the same time,
1216     using the usual syntax:
1217 <programlisting>
1218 ALTER TABLE products ADD COLUMN description text CHECK (description &lt;&gt; '');
1219 </programlisting>
1220     A new column cannot have a not-null constraint since the column
1221     initially has to contain null values.  But you can add a not-null
1222     constraint later.  Also, you cannot define a default value on a
1223     new column.  According to the SQL standard, this would have to
1224     fill the new columns in the existing rows with the default value,
1225     which is not implemented yet.  But you can adjust the column
1226     default later on.
1227    </para>
1228   </sect2>
1229
1230   <sect2>
1231    <title>Removing a Column</title>
1232
1233    <indexterm>
1234     <primary>column</primary>
1235     <secondary>removing</secondary>
1236    </indexterm>
1237
1238    <para>
1239     To remove a column, use this command:
1240 <programlisting>
1241 ALTER TABLE products DROP COLUMN description;
1242 </programlisting>
1243    </para>
1244   </sect2>
1245
1246   <sect2>
1247    <title>Adding a Constraint</title>
1248
1249    <indexterm>
1250     <primary>constraint</primary>
1251     <secondary>adding</secondary>
1252    </indexterm>
1253
1254    <para>
1255     To add a constraint, the table constraint syntax is used.  For example:
1256 <programlisting>
1257 ALTER TABLE products ADD CHECK (name &lt;&gt; '');
1258 ALTER TABLE products ADD CONSTRAINT some_name UNIQUE (product_no);
1259 ALTER TABLE products ADD FOREIGN KEY (product_group_id) REFERENCES product_groups;
1260 </programlisting>
1261     To add a not-null constraint, which cannot be written as a table
1262     constraint, use this syntax:
1263 <programlisting>
1264 ALTER TABLE products ALTER COLUMN product_no SET NOT NULL;
1265 </programlisting>
1266    </para>
1267
1268    <para>
1269     The constraint will be checked immediately, so the table data must
1270     satisfy the constraint before it can be added.
1271    </para>
1272   </sect2>
1273
1274   <sect2>
1275    <title>Removing a Constraint</title>
1276
1277    <indexterm>
1278     <primary>constraint</primary>
1279     <secondary>removing</secondary>
1280    </indexterm>
1281
1282    <para>
1283     To remove a constraint you need to know its name.  If you gave it
1284     a name then that's easy.  Otherwise the system assigned a
1285     generated name, which you need to find out.  The
1286     <application>psql</application> command <literal>\d
1287     <replaceable>tablename</replaceable></literal> can be helpful
1288     here; other interfaces might also provide a way to inspect table
1289     details.  Then the command is:
1290 <programlisting>
1291 ALTER TABLE products DROP CONSTRAINT some_name;
1292 </programlisting>
1293     (If you are dealing with a generated constraint name like <literal>$2</>,
1294     don't forget that you'll need to double-quote it to make it a valid
1295     identifier.)
1296    </para>
1297
1298    <para>
1299     This works the same for all constraint types except not-null
1300     constraints. To drop a not null constraint use
1301 <programlisting>
1302 ALTER TABLE products ALTER COLUMN product_no DROP NOT NULL;
1303 </programlisting>
1304     (Recall that not-null constraints do not have names.)
1305    </para>
1306   </sect2>
1307
1308   <sect2>
1309    <title>Changing the Default</title>
1310
1311    <indexterm>
1312     <primary>default value</primary>
1313     <secondary>changing</secondary>
1314    </indexterm>
1315
1316    <para>
1317     To set a new default for a column, use a command like this:
1318 <programlisting>
1319 ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77;
1320 </programlisting>
1321     To remove any default value, use
1322 <programlisting>
1323 ALTER TABLE products ALTER COLUMN price DROP DEFAULT;
1324 </programlisting>
1325     This is equivalent to setting the default to null, at least in
1326     <productname>PostgreSQL</>.  As a consequence, it is not an error
1327     to drop a default where one hadn't been defined, because the
1328     default is implicitly the null value.
1329    </para>
1330   </sect2>
1331
1332   <sect2>
1333    <title>Renaming a Column</title>
1334
1335    <indexterm>
1336     <primary>column</primary>
1337     <secondary>renaming</secondary>
1338    </indexterm>
1339
1340    <para>
1341     To rename a column:
1342 <programlisting>
1343 ALTER TABLE products RENAME COLUMN product_no TO product_number;
1344 </programlisting>
1345    </para>
1346   </sect2>
1347
1348   <sect2>
1349    <title>Renaming a Table</title>
1350
1351    <indexterm>
1352     <primary>table</primary>
1353     <secondary>renaming</secondary>
1354    </indexterm>
1355
1356    <para>
1357     To rename a table:
1358 <programlisting>
1359 ALTER TABLE products RENAME TO items;
1360 </programlisting>
1361    </para>
1362   </sect2>
1363  </sect1>
1364  
1365  <sect1 id="ddl-priv">
1366   <title>Privileges</title>
1367
1368   <indexterm zone="ddl-priv">
1369    <primary>privilege</primary>
1370   </indexterm>
1371
1372   <indexterm>
1373    <primary>permission</primary>
1374    <see>privilege</see>
1375   </indexterm>
1376
1377   <para>
1378    When you create a database object, you become its owner.  By
1379    default, only the owner of an object can do anything with the
1380    object. In order to allow other users to use it,
1381    <firstterm>privileges</firstterm> must be granted.  (There are also
1382    users that have the superuser privilege.  Those users can always
1383    access any object.)
1384   </para>
1385
1386   <note>
1387    <para>
1388     To change the owner of a table, index, sequence, or view, use the
1389     <xref linkend="sql-altertable" endterm="sql-altertable-title">
1390     command.
1391    </para>
1392   </note>
1393
1394   <para>
1395    There are several different privileges: <literal>SELECT</>,
1396    <literal>INSERT</>, <literal>UPDATE</>, <literal>DELETE</>,
1397    <literal>RULE</>, <literal>REFERENCES</>, <literal>TRIGGER</>,
1398    <literal>CREATE</>, <literal>TEMPORARY</>, <literal>EXECUTE</>,
1399    <literal>USAGE</>, and <literal>ALL PRIVILEGES</>. For complete
1400    information on the different types of privileges supported by
1401    <productname>PostgreSQL</productname>, refer to the
1402    <xref linkend="sql-grant" endterm="sql-grant-title">
1403    reference page.  The following sections
1404    and chapters will also show you how those privileges are used.
1405   </para>
1406
1407   <para>
1408    The right to modify or destroy an object is always the privilege of
1409    the owner only.
1410   </para>
1411
1412   <para>
1413    To assign privileges, the <command>GRANT</command> command is
1414    used. So, if <literal>joe</literal> is an existing user, and
1415    <literal>accounts</literal> is an existing table, the privilege to
1416    update the table can be granted with
1417 <programlisting>
1418 GRANT UPDATE ON accounts TO joe;
1419 </programlisting>
1420    The user executing this command must be the owner of the table. To
1421    grant a privilege to a group, use
1422 <programlisting>
1423 GRANT SELECT ON accounts TO GROUP staff;
1424 </programlisting>
1425    The special <quote>user</quote> name <literal>PUBLIC</literal> can
1426    be used to grant a privilege to every user on the system. Writing
1427    <literal>ALL</literal> in place of a specific privilege specifies that all
1428    privileges will be granted.
1429   </para>
1430
1431   <para>
1432    To revoke a privilege, use the fittingly named
1433    <command>REVOKE</command> command:
1434 <programlisting>
1435 REVOKE ALL ON accounts FROM PUBLIC;
1436 </programlisting>
1437    The special privileges of the table owner (i.e., the right to do
1438    <command>DROP</>, <command>GRANT</>, <command>REVOKE</>, etc.)
1439    are always implicit in being the owner,
1440    and cannot be granted or revoked.  But the table owner can choose
1441    to revoke his own ordinary privileges, for example to make a
1442    table read-only for himself as well as others.
1443   </para>
1444  </sect1>
1445
1446  <sect1 id="ddl-schemas">
1447   <title>Schemas</title>
1448
1449   <indexterm zone="ddl-schemas">
1450    <primary>schema</primary>
1451   </indexterm>
1452
1453   <para>
1454    A <productname>PostgreSQL</productname> database cluster
1455    contains one or more named databases.  Users and groups of users are
1456    shared across the entire cluster, but no other data is shared across
1457    databases.  Any given client connection to the server can access
1458    only the data in a single database, the one specified in the connection
1459    request.
1460   </para>
1461
1462   <note>
1463    <para>
1464     Users of a cluster do not necessarily have the privilege to access every
1465     database in the cluster.  Sharing of user names means that there
1466     cannot be different users named, say, <literal>joe</> in two databases
1467     in the same cluster; but the system can be configured to allow
1468     <literal>joe</> access to only some of the databases.
1469    </para>
1470   </note>
1471
1472   <para>
1473    A database contains one or more named <firstterm>schemas</>, which
1474    in turn contain tables.  Schemas also contain other kinds of named
1475    objects, including data types, functions, and operators.  The same
1476    object name can be used in different schemas without conflict; for
1477    example, both <literal>schema1</> and <literal>myschema</> may
1478    contain tables named <literal>mytable</>.  Unlike databases,
1479    schemas are not rigidly separated: a user may access objects in any
1480    of the schemas in the database he is connected to, if he has
1481    privileges to do so.
1482   </para>
1483
1484   <para>
1485    There are several reasons why one might want to use schemas:
1486
1487    <itemizedlist>
1488     <listitem>
1489      <para>
1490       To allow many users to use one database without interfering with
1491       each other.
1492      </para>
1493     </listitem>
1494
1495     <listitem>
1496      <para>
1497       To organize database objects into logical groups to make them
1498       more manageable.
1499      </para>
1500     </listitem>
1501
1502     <listitem>
1503      <para>
1504       Third-party applications can be put into separate schemas so
1505       they cannot collide with the names of other objects.
1506      </para>
1507     </listitem>
1508    </itemizedlist>
1509
1510    Schemas are analogous to directories at the operating system level,
1511    except that schemas cannot be nested.
1512   </para>
1513
1514   <sect2 id="ddl-schemas-create">
1515    <title>Creating a Schema</title>
1516
1517    <indexterm zone="ddl-schemas-create">
1518     <primary>schema</primary>
1519     <secondary>creating</secondary>
1520    </indexterm>
1521
1522    <para>
1523     To create a separate schema, use the command <literal>CREATE
1524     SCHEMA</literal>.  Give the schema a name of your choice.  For
1525     example:
1526 <programlisting>
1527 CREATE SCHEMA myschema;
1528 </programlisting>
1529    </para>
1530
1531    <indexterm>
1532     <primary>qualified name</primary>
1533    </indexterm>
1534
1535    <indexterm>
1536     <primary>name</primary>
1537     <secondary>qualified</secondary>
1538    </indexterm>
1539
1540    <para>
1541     To create or access objects in a schema, write a
1542     <firstterm>qualified name</> consisting of the schema name and
1543     table name separated by a dot:
1544 <synopsis>
1545 <replaceable>schema</><literal>.</><replaceable>table</>
1546 </synopsis>
1547     Actually, the even more general syntax
1548 <synopsis>
1549 <replaceable>database</><literal>.</><replaceable>schema</><literal>.</><replaceable>table</>
1550 </synopsis>
1551     can be used too, but at present this is just for pro-forma compliance
1552     with the SQL standard; if you write a database name it must be the
1553     same as the database you are connected to.
1554    </para>
1555
1556    <para>
1557     So to create a table in the new schema, use
1558 <programlisting>
1559 CREATE TABLE myschema.mytable (
1560  ...
1561 );
1562 </programlisting>
1563     This works anywhere a table name is expected, including the table
1564     modification commands and the data access commands discussed in
1565     the following chapters.
1566    </para>
1567
1568    <indexterm>
1569     <primary>schema</primary>
1570     <secondary>removing</secondary>
1571    </indexterm>
1572
1573    <para>
1574     To drop a schema if it's empty (all objects in it have been
1575     dropped), use
1576 <programlisting>
1577 DROP SCHEMA myschema;
1578 </programlisting>
1579     To drop a schema including all contained objects, use
1580 <programlisting>
1581 DROP SCHEMA myschema CASCADE;
1582 </programlisting>
1583     See <xref linkend="ddl-depend"> for a description of the general
1584     mechanism behind this.
1585    </para>
1586
1587    <para>
1588     Often you will want to create a schema owned by someone else
1589     (since this is one of the ways to restrict the activities of your
1590     users to well-defined namespaces).  The syntax for that is:
1591 <programlisting>
1592 CREATE SCHEMA <replaceable>schemaname</replaceable> AUTHORIZATION <replaceable>username</replaceable>;
1593 </programlisting>
1594     You can even omit the schema name, in which case the schema name
1595     will be the same as the user name.  See <xref
1596     linkend="ddl-schemas-patterns"> for how this can be useful.
1597    </para>
1598
1599    <para>
1600     Schema names beginning with <literal>pg_</> are reserved for
1601     system purposes and may not be created by users.
1602    </para>
1603   </sect2>
1604
1605   <sect2 id="ddl-schemas-public">
1606    <title>The Public Schema</title>
1607
1608    <indexterm zone="ddl-schemas-public">
1609     <primary>schema</primary>
1610     <secondary>public</secondary>
1611    </indexterm>
1612
1613    <para>
1614     In the previous sections we created tables without specifying any
1615     schema names.  By default, such tables (and other objects) are
1616     automatically put into a schema named <quote>public</quote>.  Every new
1617     database contains such a schema.  Thus, the following are equivalent:
1618 <programlisting>
1619 CREATE TABLE products ( ... );
1620 </programlisting>
1621     and
1622 <programlisting>
1623 CREATE TABLE public.products ( ... );
1624 </programlisting>
1625    </para>
1626   </sect2>
1627
1628   <sect2 id="ddl-schemas-path">
1629    <title>The Schema Search Path</title>
1630
1631    <indexterm>
1632     <primary>search path</primary>
1633    </indexterm>
1634
1635    <indexterm>
1636     <primary>unqualified name</primary>
1637    </indexterm>
1638
1639    <indexterm>
1640     <primary>name</primary>
1641     <secondary>unqualified</secondary>
1642    </indexterm>
1643
1644    <para>
1645     Qualified names are tedious to write, and it's often best not to
1646     wire a particular schema name into applications anyway.  Therefore
1647     tables are often referred to by <firstterm>unqualified names</>,
1648     which consist of just the table name.  The system determines which table
1649     is meant by following a <firstterm>search path</>, which is a list
1650     of schemas to look in.  The first matching table in the search path
1651     is taken to be the one wanted.  If there is no match in the search
1652     path, an error is reported, even if matching table names exist
1653     in other schemas in the database.
1654    </para>
1655
1656    <indexterm>
1657     <primary>schema</primary>
1658     <secondary>current</secondary>
1659    </indexterm>
1660
1661    <para>
1662     The first schema named in the search path is called the current schema.
1663     Aside from being the first schema searched, it is also the schema in
1664     which new tables will be created if the <command>CREATE TABLE</>
1665     command does not specify a schema name.
1666    </para>
1667
1668    <indexterm>
1669     <primary>search_path</primary>
1670    </indexterm>
1671
1672    <para>
1673     To show the current search path, use the following command:
1674 <programlisting>
1675 SHOW search_path;
1676 </programlisting>
1677     In the default setup this returns:
1678 <screen>
1679  search_path
1680 --------------
1681  $user,public
1682 </screen>
1683     The first element specifies that a schema with the same name as
1684     the current user is to be searched.  If no such schema exists,
1685     the entry is ignored.  The second element refers to the
1686     public schema that we have seen already.
1687    </para>
1688
1689    <para>
1690     The first schema in the search path that exists is the default
1691     location for creating new objects.  That is the reason that by
1692     default objects are created in the public schema.  When objects
1693     are referenced in any other context without schema qualification
1694     (table modification, data modification, or query commands) the
1695     search path is traversed until a matching object is found.
1696     Therefore, in the default configuration, any unqualified access
1697     again can only refer to the public schema.
1698    </para>
1699
1700    <para>
1701     To put our new schema in the path, we use
1702 <programlisting>
1703 SET search_path TO myschema,public;
1704 </programlisting>
1705     (We omit the <literal>$user</literal> here because we have no
1706     immediate need for it.)  And then we can access the table without
1707     schema qualification:
1708 <programlisting>
1709 DROP TABLE mytable;
1710 </programlisting>
1711     Also, since <literal>myschema</literal> is the first element in
1712     the path, new objects would by default be created in it.
1713    </para>
1714
1715    <para>
1716     We could also have written
1717 <programlisting>
1718 SET search_path TO myschema;
1719 </programlisting>
1720     Then we no longer have access to the public schema without
1721     explicit qualification.  There is nothing special about the public
1722     schema except that it exists by default.  It can be dropped, too.
1723    </para>
1724
1725    <para>
1726     See also <xref linkend="functions-info"> for other ways to access
1727     the schema search path.
1728    </para>
1729
1730    <para>
1731     The search path works in the same way for data type names, function names,
1732     and operator names as it does for table names.  Data type and function
1733     names can be qualified in exactly the same way as table names.  If you
1734     need to write a qualified operator name in an expression, there is a
1735     special provision: you must write
1736 <synopsis>
1737 <literal>OPERATOR(</><replaceable>schema</><literal>.</><replaceable>operator</><literal>)</>
1738 </synopsis>
1739     This is needed to avoid syntactic ambiguity.  An example is
1740 <programlisting>
1741 SELECT 3 OPERATOR(pg_catalog.+) 4;
1742 </programlisting>
1743     In practice one usually relies on the search path for operators,
1744     so as not to have to write anything so ugly as that.
1745    </para>
1746   </sect2>
1747
1748   <sect2 id="ddl-schemas-priv">
1749    <title>Schemas and Privileges</title>
1750
1751    <indexterm zone="ddl-schemas-priv">
1752     <primary>privilege</primary>
1753     <secondary sortas="schemas">for schemas</secondary>
1754    </indexterm>
1755
1756    <para>
1757     By default, users cannot access any objects in schemas they do not
1758     own.  To allow that, the owner of the schema needs to grant the
1759     <literal>USAGE</literal> privilege on the schema.  To allow users
1760     to make use of the objects in the schema, additional privileges
1761     may need to be granted, as appropriate for the object.
1762    </para>
1763
1764    <para>
1765     A user can also be allowed to create objects in someone else's
1766     schema.  To allow that, the <literal>CREATE</literal> privilege on
1767     the schema needs to be granted.  Note that by default, everyone
1768     has <literal>CREATE</literal> and <literal>USAGE</literal> privileges on
1769     the schema 
1770     <literal>public</literal>.  This allows all users that are able to
1771     connect to a given database to create objects in its
1772     <literal>public</literal> schema.  If you do
1773     not want to allow that, you can revoke that privilege:
1774 <programlisting>
1775 REVOKE CREATE ON SCHEMA public FROM PUBLIC;
1776 </programlisting>
1777     (The first <quote>public</quote> is the schema, the second
1778     <quote>public</quote> means <quote>every user</quote>.  In the
1779     first sense it is an identifier, in the second sense it is a
1780     reserved word, hence the different capitalization; recall the
1781     guidelines from <xref linkend="sql-syntax-identifiers">.)
1782    </para>
1783   </sect2>
1784
1785   <sect2 id="ddl-schemas-catalog">
1786    <title>The System Catalog Schema</title>
1787
1788    <indexterm zone="ddl-schemas-catalog">
1789     <primary>system catalog</primary>
1790     <secondary>schema</secondary>
1791    </indexterm>
1792
1793    <para>
1794     In addition to <literal>public</> and user-created schemas, each
1795     database contains a <literal>pg_catalog</> schema, which contains
1796     the system tables and all the built-in data types, functions, and
1797     operators.  <literal>pg_catalog</> is always effectively part of
1798     the search path.  If it is not named explicitly in the path then
1799     it is implicitly searched <emphasis>before</> searching the path's
1800     schemas.  This ensures that built-in names will always be
1801     findable.  However, you may explicitly place
1802     <literal>pg_catalog</> at the end of your search path if you
1803     prefer to have user-defined names override built-in names.
1804    </para>
1805
1806    <para>
1807     In <productname>PostgreSQL</productname> versions before 7.3,
1808     table names beginning with <literal>pg_</> were reserved.  This is
1809     no longer true: you may create such a table name if you wish, in
1810     any non-system schema.  However, it's best to continue to avoid
1811     such names, to ensure that you won't suffer a conflict if some
1812     future version defines a system table named the same as your
1813     table.  (With the default search path, an unqualified reference to
1814     your table name would be resolved as the system table instead.)
1815     System tables will continue to follow the convention of having
1816     names beginning with <literal>pg_</>, so that they will not
1817     conflict with unqualified user-table names so long as users avoid
1818     the <literal>pg_</> prefix.
1819    </para>
1820   </sect2>
1821
1822   <sect2 id="ddl-schemas-patterns">
1823    <title>Usage Patterns</title>
1824
1825    <para>
1826     Schemas can be used to organize your data in many ways.  There are
1827     a few usage patterns that are recommended and are easily supported by
1828     the default configuration:
1829     <itemizedlist>
1830      <listitem>
1831       <para>
1832        If you do not create any schemas then all users access the
1833        public schema implicitly.  This simulates the situation where
1834        schemas are not available at all.  This setup is mainly
1835        recommended when there is only a single user or a few cooperating
1836        users in a database.  This setup also allows smooth transition
1837        from the non-schema-aware world.
1838       </para>
1839      </listitem>
1840
1841      <listitem>
1842       <para>
1843        You can create a schema for each user with the same name as
1844        that user.  Recall that the default search path starts with
1845        <literal>$user</literal>, which resolves to the user name.
1846        Therefore, if each user has a separate schema, they access their
1847        own schemas by default.
1848       </para>
1849
1850       <para>
1851        If you use this setup then you might also want to revoke access
1852        to the public schema (or drop it altogether), so users are
1853        truly constrained to their own schemas.
1854       </para>
1855      </listitem>
1856
1857      <listitem>
1858       <para>
1859        To install shared applications (tables to be used by everyone,
1860        additional functions provided by third parties, etc.), put them
1861        into separate schemas.  Remember to grant appropriate
1862        privileges to allow the other users to access them.  Users can
1863        then refer to these additional objects by qualifying the names
1864        with a schema name, or they can put the additional schemas into
1865        their path, as they choose.
1866       </para>
1867      </listitem>
1868     </itemizedlist>
1869    </para>
1870   </sect2>
1871
1872   <sect2 id="ddl-schemas-portability">
1873    <title>Portability</title>
1874
1875    <para>
1876     In the SQL standard, the notion of objects in the same schema
1877     being owned by different users does not exist.  Moreover, some
1878     implementations do not allow you to create schemas that have a
1879     different name than their owner.  In fact, the concepts of schema
1880     and user are nearly equivalent in a database system that
1881     implements only the basic schema support specified in the
1882     standard.  Therefore, many users consider qualified names to
1883     really consist of
1884     <literal><replaceable>username</>.<replaceable>tablename</></literal>.
1885     This is how <productname>PostgreSQL</productname> will effectively
1886     behave if you create a per-user schema for every user.
1887    </para>
1888
1889    <para>
1890     Also, there is no concept of a <literal>public</> schema in the
1891     SQL standard.  For maximum conformance to the standard, you should
1892     not use (perhaps even remove) the <literal>public</> schema.
1893    </para>
1894
1895    <para>
1896     Of course, some SQL database systems might not implement schemas
1897     at all, or provide namespace support by allowing (possibly
1898     limited) cross-database access.  If you need to work with those
1899     systems, then maximum portability would be achieved by not using
1900     schemas at all.
1901    </para>
1902   </sect2>
1903  </sect1>
1904
1905  <sect1 id="ddl-others">
1906   <title>Other Database Objects</title>
1907
1908   <para>
1909    Tables are the central objects in a relational database structure,
1910    because they hold your data.  But they are not the only objects
1911    that exist in a database.  Many other kinds of objects can be
1912    created to make the use and management of the data more efficient
1913    or convenient.  They are not discussed in this chapter, but we give
1914    you a list here so that you are aware of what is possible.
1915   </para>
1916
1917   <itemizedlist>
1918    <listitem>
1919     <para>
1920      Views
1921     </para>
1922    </listitem>
1923
1924    <listitem>
1925     <para>
1926      Functions, operators, data types, domains
1927     </para>
1928    </listitem>
1929
1930    <listitem>
1931     <para>
1932      Triggers and rewrite rules
1933     </para>
1934    </listitem>
1935   </itemizedlist>
1936
1937   <para>
1938    Detailed information on
1939    these topics appears in <xref linkend="server-programming">.
1940   </para>
1941  </sect1>
1942
1943  <sect1 id="ddl-depend">
1944   <title>Dependency Tracking</title>
1945
1946   <indexterm zone="ddl-depend">
1947    <primary>CASCADE</primary>
1948    <secondary sortas="DROP">with DROP</secondary>
1949   </indexterm>
1950
1951   <indexterm zone="ddl-depend">
1952    <primary>RESTRICT</primary>
1953    <secondary sortas="DROP">with DROP</secondary>
1954   </indexterm>
1955
1956   <para>
1957    When you create complex database structures involving many tables
1958    with foreign key constraints, views, triggers, functions, etc. you
1959    will implicitly create a net of dependencies between the objects.
1960    For instance, a table with a foreign key constraint depends on the
1961    table it references.
1962   </para>
1963
1964   <para>
1965    To ensure the integrity of the entire database structure,
1966    <productname>PostgreSQL</productname> makes sure that you cannot
1967    drop objects that other objects still depend on.  For example,
1968    attempting to drop the products table we had considered in <xref
1969    linkend="ddl-constraints-fk">, with the orders table depending on
1970    it, would result in an error message such as this:
1971 <screen>
1972 DROP TABLE products;
1973
1974 NOTICE:  constraint $1 on table orders depends on table products
1975 ERROR:  cannot drop table products because other objects depend on it
1976 HINT:  Use DROP ... CASCADE to drop the dependent objects too.
1977 </screen>
1978    The error message contains a useful hint: if you do not want to
1979    bother deleting all the dependent objects individually, you can run
1980 <screen>
1981 DROP TABLE products CASCADE;
1982 </screen>
1983    and all the dependent objects will be removed.  In this case, it
1984    doesn't remove the orders table, it only removes the foreign key
1985    constraint.  (If you want to check what <literal>DROP ... CASCADE</> will do,
1986    run <command>DROP</> without <literal>CASCADE</> and read the <literal>NOTICE</> messages.)
1987   </para>
1988
1989   <para>
1990    All drop commands in <productname>PostgreSQL</productname> support
1991    specifying <literal>CASCADE</literal>.  Of course, the nature of
1992    the possible dependencies varies with the type of the object.  You
1993    can also write <literal>RESTRICT</literal> instead of
1994    <literal>CASCADE</literal> to get the default behavior, which is to
1995    prevent drops of objects that other objects depend on.
1996   </para>
1997
1998   <note>
1999    <para>
2000     According to the SQL standard, specifying either
2001     <literal>RESTRICT</literal> or <literal>CASCADE</literal> is
2002     required.  No database system actually implements it that way, but
2003     whether the default behavior is <literal>RESTRICT</literal> or
2004     <literal>CASCADE</literal> varies across systems.
2005    </para>
2006   </note>
2007
2008   <note>
2009    <para>
2010     Foreign key constraint dependencies and serial column dependencies
2011     from <productname>PostgreSQL</productname> versions prior to 7.3
2012     are <emphasis>not</emphasis> maintained or created during the
2013     upgrade process.  All other dependency types will be properly
2014     created during an upgrade.
2015    </para>
2016   </note>
2017  </sect1>
2018
2019 </chapter>