
PostgreSQL foreign key constraint examples PostgreSQL supports the following actions: Since the primary key is rarely updated, the ON UPDATE action is not often used in practice. The delete and update actions determine the behaviors when the primary key in the parent table is deleted and updated.

If you omit it, PostgreSQL will assign an auto-generated name. First, specify the name for the foreign key constraint after the CONSTRAINT keyword.REFERENCES parent_table( parent_key_columns) The following illustrates a foreign key constraint syntax: FOREIGN KEY( fk_columns)

The foreign key constraint helps maintain the referential integrity of data between the child and parent tables.Ī foreign key constraint indicates that values in a column or a group of columns in the child table equal the values in a column or a group of columns of the parent table. In PostgreSQL, you define a foreign key using the foreign key constraint. And the table referenced by the foreign key is called the referenced table or parent table.Ī table can have multiple foreign keys depending on its relationships with other tables. The table that contains the foreign key is called the referencing table or child table.

Introduction to PostgreSQL Foreign Key ConstraintĪ foreign key is a column or a group of columns in a table that reference the primary key of another table.
#Postgres add constraint how to#
Summary: in this tutorial, you will learn about PostgreSQL foreign key and how to add foreign keys to tables using foreign key constraints.
