@@ -770,6 +770,11 @@ CREATE TABLE orders (
770
770
referenced table is used as the referenced column(s).
771
771
</para>
772
772
773
+ <para>
774
+ You can assign your own name for a foreign key constraint,
775
+ in the usual way.
776
+ </para>
777
+
773
778
<para>
774
779
A foreign key can also constrain and reference a group of columns.
775
780
As usual, it then needs to be written in table constraint form.
@@ -786,9 +791,28 @@ CREATE TABLE t1 (
786
791
match the number and type of the referenced columns.
787
792
</para>
788
793
794
+ <indexterm>
795
+ <primary>foreign key</primary>
796
+ <secondary>self-referential</secondary>
797
+ </indexterm>
798
+
789
799
<para>
790
- You can assign your own name for a foreign key constraint,
791
- in the usual way.
800
+ Sometimes it is useful for the <quote>other table</quote> of a
801
+ foreign key constraint to be the same table; this is called
802
+ a <firstterm>self-referential</firstterm> foreign key. For
803
+ example, if you want rows of a table to represent nodes of a tree
804
+ structure, you could write
805
+ <programlisting>
806
+ CREATE TABLE tree (
807
+ node_id integer PRIMARY KEY,
808
+ parent_id integer REFERENCES tree,
809
+ name text,
810
+ ...
811
+ );
812
+ </programlisting>
813
+ A top-level node would have NULL <structfield>parent_id</structfield>,
814
+ but non-NULL <structfield>parent_id</structfield> entries would be
815
+ constrained to reference valid rows of the table.
792
816
</para>
793
817
794
818
<para>
0 commit comments