vefbetter.blogg.se

Postgresql add foreign key to existing table
Postgresql add foreign key to existing table












postgresql add foreign key to existing table

The following illustrates the syntax of defining a foreign key constraint:Ĭode language: SQL (Structured Query Language) ( sql )

postgresql add foreign key to existing table

Typically, you define foreign key constraints for a table at the time of the table creation.

postgresql add foreign key to existing table

To define a foreign key for a table, you use the foreign key constraint. In other words, a country will not exist without a region.Ī table can have multiple foreign keys that refer to the primary keys of different parent tables. The foreign key constraint prevents you from inserting a row into the countries table without a corresponding row in the regions table. The region_id in the countries table is the foreign key that references to the region_id column in the regions table.įor each row in the countries table, you can find a corresponding row in the regions table. See the following regions and countries table from the nation sample database: Typically, a foreign key column in a child table references to the primary key column of the parent table. What is a foreign keyĪ foreign key is a column or set of columns in a table that references a column or a set of columns in another table, which enforces the referential integrity between the two tables.Ī table that has the foreign key is called a child table while the table to which the foreign key references is known as a parent table.

Postgresql add foreign key to existing table how to#

Summary: in this tutorial, you will learn how to use the MariaDB foreign key constraints to enforce the referential integrity between tables.














Postgresql add foreign key to existing table