@@ -915,6 +915,11 @@ CREATE TABLE orders (
915
915
referenced table is used as the referenced column(s).
916
916
</para>
917
917
918
+ <para>
919
+ You can assign your own name for a foreign key constraint,
920
+ in the usual way.
921
+ </para>
922
+
918
923
<para>
919
924
A foreign key can also constrain and reference a group of columns.
920
925
As usual, it then needs to be written in table constraint form.
@@ -931,9 +936,28 @@ CREATE TABLE t1 (
931
936
match the number and type of the referenced columns.
932
937
</para>
933
938
939
+ <indexterm>
940
+ <primary>foreign key</primary>
941
+ <secondary>self-referential</secondary>
942
+ </indexterm>
943
+
934
944
<para>
935
- You can assign your own name for a foreign key constraint,
936
- in the usual way.
945
+ Sometimes it is useful for the <quote>other table</quote> of a
946
+ foreign key constraint to be the same table; this is called
947
+ a <firstterm>self-referential</firstterm> foreign key. For
948
+ example, if you want rows of a table to represent nodes of a tree
949
+ structure, you could write
950
+ <programlisting>
951
+ CREATE TABLE tree (
952
+ node_id integer PRIMARY KEY,
953
+ parent_id integer REFERENCES tree,
954
+ name text,
955
+ ...
956
+ );
957
+ </programlisting>
958
+ A top-level node would have NULL <structfield>parent_id</structfield>,
959
+ but non-NULL <structfield>parent_id</structfield> entries would be
960
+ constrained to reference valid rows of the table.
937
961
</para>
938
962
939
963
<para>
0 commit comments