SET DEFAULT: If a row in the parent table is deleted or updated, the value of the matching row in the child table is set to the default value.SET NULL: If a row in the parent table is deleted or updated, the value of the matching row in the child table is set to NULL.CASCADE: If a row in the parent table is deleted or updated, the value of the matching row in the child table is automatically deleted or updated.RESTRICT: A PostgreSQL error is raised when attempting to delete or update a row in the parent table if a row in the parent table has a matching row in the child table.You can use one of the following 5 strategies: The ON DELETE and ON UPDATE specifies the constraint strategy to take when deleting or updating rows in the parent table. ![]() The REFERENCES parent_table_name (column) indicates that the foreign key refers to the column column in the parent_table_name table. Tge FOREIGN KEY (column) indicates that the column column is a foreign key. The foreign_key_name is the name of the foreign key constraint. FOREIGN KEY ( column ) REFERENCES parent_table_name ( column ) ON UPDATE. To add foreign keys when creating a table, use the following syntax:ĬREATE TABLE table_name ( column_defination_1. PostgreSQL Foreign Key Syntax Add foreign key when creating table Usually, the table that has the foreign key is called the child table, and the table referenced by the foreign key is called the parent table. You also can’t set a non-existent country_id for a city, otherwise the city data will be wrong.įoreign key constraints ensure that the data is complete and correct. If a country already has cities, you cannot easily delete countries from the country table, otherwise the corresponding city data will be incomplete. A country can have multiple cities, and a city can only be located in one country. +-+-+-įrom this we can see that the country table and the city table is a one-to-many relationship. City_id | city | country_id | last_update
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |