mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	Doc: add an example of a self-referential foreign key to ddl.sgml.
While we've always allowed such cases, the documentation didn't say you could do it. Discussion: https://postgr.es/m/161969805833.690.13680986983883602407@wrigleys.postgresql.org
This commit is contained in:
		@@ -770,6 +770,11 @@ CREATE TABLE orders (
 | 
				
			|||||||
    referenced table is used as the referenced column(s).
 | 
					    referenced table is used as the referenced column(s).
 | 
				
			||||||
   </para>
 | 
					   </para>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   <para>
 | 
				
			||||||
 | 
					    You can assign your own name for a foreign key constraint,
 | 
				
			||||||
 | 
					    in the usual way.
 | 
				
			||||||
 | 
					   </para>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   <para>
 | 
					   <para>
 | 
				
			||||||
    A foreign key can also constrain and reference a group of columns.
 | 
					    A foreign key can also constrain and reference a group of columns.
 | 
				
			||||||
    As usual, it then needs to be written in table constraint form.
 | 
					    As usual, it then needs to be written in table constraint form.
 | 
				
			||||||
@@ -786,9 +791,28 @@ CREATE TABLE t1 (
 | 
				
			|||||||
    match the number and type of the referenced columns.
 | 
					    match the number and type of the referenced columns.
 | 
				
			||||||
   </para>
 | 
					   </para>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   <indexterm>
 | 
				
			||||||
 | 
					    <primary>foreign key</primary>
 | 
				
			||||||
 | 
					    <secondary>self-referential</secondary>
 | 
				
			||||||
 | 
					   </indexterm>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   <para>
 | 
					   <para>
 | 
				
			||||||
    You can assign your own name for a foreign key constraint,
 | 
					    Sometimes it is useful for the <quote>other table</quote> of a
 | 
				
			||||||
    in the usual way.
 | 
					    foreign key constraint to be the same table; this is called
 | 
				
			||||||
 | 
					    a <firstterm>self-referential</firstterm> foreign key.  For
 | 
				
			||||||
 | 
					    example, if you want rows of a table to represent nodes of a tree
 | 
				
			||||||
 | 
					    structure, you could write
 | 
				
			||||||
 | 
					<programlisting>
 | 
				
			||||||
 | 
					CREATE TABLE tree (
 | 
				
			||||||
 | 
					    node_id integer PRIMARY KEY,
 | 
				
			||||||
 | 
					    parent_id integer REFERENCES tree,
 | 
				
			||||||
 | 
					    name text,
 | 
				
			||||||
 | 
					    ...
 | 
				
			||||||
 | 
					);
 | 
				
			||||||
 | 
					</programlisting>
 | 
				
			||||||
 | 
					    A top-level node would have NULL <structfield>parent_id</structfield>,
 | 
				
			||||||
 | 
					    but non-NULL <structfield>parent_id</structfield> entries would be
 | 
				
			||||||
 | 
					    constrained to reference valid rows of the table.
 | 
				
			||||||
   </para>
 | 
					   </para>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   <para>
 | 
					   <para>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user