{"id":170,"date":"2024-11-20T16:32:49","date_gmt":"2024-11-20T09:32:49","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=170"},"modified":"2025-01-02T08:51:13","modified_gmt":"2025-01-02T01:51:13","slug":"postgresql-foreign-key","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-foreign-key\/","title":{"rendered":"PostgreSQL Foreign Key"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about foreign keys and how to use PostgreSQL foreign key constraints to create foreign key columns for a table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-foreign-keys'>Introduction to foreign keys <a href=\"#introduction-to-foreign-keys\" class=\"anchor\" id=\"introduction-to-foreign-keys\" title=\"Anchor for Introduction to foreign keys\">#<\/a><\/h2>\n\n\n\n<p>In PostgreSQL, a foreign key is a column or set of columns in one table that references the <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-primary-key\/\">primary key<\/a> of <strong>another table<\/strong>. It serves as a link between the two tables.<\/p>\n\n\n\n<p>For example, the following table <code>products<\/code> has a foreign key column called <code>brand_id<\/code> that references the <code>brand_id<\/code> column of the table <code>brands<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"862\" height=\"246\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/postgresql-foreign-key.png\" alt=\"postgresql foreign key\" class=\"wp-image-239\" srcset=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/postgresql-foreign-key.png 862w, https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/postgresql-foreign-key-300x86.png 300w, https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/postgresql-foreign-key-768x219.png 768w\" sizes=\"auto, (max-width: 862px) 100vw, 862px\" \/><\/figure>\n\n\n\n<p>The table with the foreign key column is called the <em>child table<\/em>, while the table with the primary key column that the child table references is known as the referenced or <em>parent table<\/em>.<\/p>\n\n\n\n<p>In this example, the table <code>brands<\/code> is the <em>referenced <\/em>or <em>parent table<\/em>, while the products table is the <em>child table<\/em>.<\/p>\n\n\n\n<p>A foreign key ensures data in the child table matches the related data in the parent table. For example, if you have a brand id in the <code>products<\/code> table, that brand id should match a brand id in the <code>brands<\/code> table.<\/p>\n\n\n\n<p>A foreign key helps maintain <em>referential integrity<\/em>. For instance, if a product references a brand, that brand must exist in the <code>brands<\/code> table.<\/p>\n\n\n\n<p>When you delete a row in the parent table, you should handle the child table properly to avoid orphaned records.<\/p>\n\n\n\n<p class=\"note\">Note that orphaned records are records in a table referencing a non-existent primary key value in another table.<\/p>\n\n\n\n<p>For example, if a row in the <code>brands<\/code> is deleted, all the related rows in the <code>products<\/code> table should be deleted automatically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='foreign-keys-in-one-to-many-relationship'>Foreign keys in one-to-many relationship <a href=\"#foreign-keys-in-one-to-many-relationship\" class=\"anchor\" id=\"foreign-keys-in-one-to-many-relationship\" title=\"Anchor for Foreign keys in one-to-many relationship\">#<\/a><\/h2>\n\n\n\n<p>When you have two tables with a one-to-many relationship, the table on the &#8220;many&#8221; side will have the foreign key column.<\/p>\n\n\n\n<p>For example, the relationship between the <code>brands<\/code> and <code>products<\/code> tables is one-to-many. The <code>products<\/code> table is the &#8220;many&#8221; side; it should get the foreign key column:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"471\" height=\"170\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/products_brands.png\" alt=\"\" class=\"wp-image-174\" srcset=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/products_brands.png 471w, https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/products_brands-300x108.png 300w\" sizes=\"auto, (max-width: 471px) 100vw, 471px\" \/><\/figure>\n\n\n\n<p>The <code>products<\/code> table has an additional column called <code>brand_id<\/code> that references the <code>brand_id<\/code> of the <code>brands<\/code> table.<\/p>\n\n\n\n<p>Similarly, the relationship between the <code>categories<\/code> and <code>products<\/code> tables is a one-to-many relationship.<\/p>\n\n\n\n<p>The <code>products<\/code> table has a foreign key column called <code>category_id<\/code> that references the <code>category_id<\/code> column of the <code>categories<\/code> table:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"661\" height=\"187\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/products_categories.png\" alt=\"\" class=\"wp-image-175\" srcset=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/products_categories.png 661w, https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/products_categories-300x85.png 300w\" sizes=\"auto, (max-width: 661px) 100vw, 661px\" \/><\/figure>\n\n\n\n<p>A table may have multiple foreign key columns, like the <code>products<\/code> table.<\/p>\n\n\n\n<p>The values in the foreign key columns are equal to those of the primary key of the reference rows. For example, the value in the <code>brand_id<\/code> column of row one is equal to the value in the <code>brand_id<\/code> column of the <code>brands<\/code> table.<\/p>\n\n\n\n<p>By convention, the foreign key column has the format <code>table_id<\/code>. For example, the foreign key columns are <code>category_id<\/code> and <code>brand_id<\/code>.<\/p>\n\n\n\n<p>Multiple rows can have the same foreign key column values. For example, both rows with the product name <code>iPhone 14 Pro<\/code> and <code>iPhone 15 Pro<\/code> have the same <code>brand_id<\/code> 1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='foreign-keys-in-a-many-to-many-relationship'>Foreign keys in a many-to-many relationship <a href=\"#foreign-keys-in-a-many-to-many-relationship\" class=\"anchor\" id=\"foreign-keys-in-a-many-to-many-relationship\" title=\"Anchor for Foreign keys in a many-to-many relationship\">#<\/a><\/h2>\n\n\n\n<p>In PostgreSQL, you use two one-to-many relationships to model a many-to-many relationship.<\/p>\n\n\n\n<p>For example, the relationship between <code>products<\/code> and <code>tags<\/code> tables is many-to-many.<\/p>\n\n\n\n<p>To model this relationship, we create a new table called <code>product_tags<\/code>, often called a <em>link table<\/em>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"622\" height=\"170\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/products_tags_tables.png\" alt=\"\" class=\"wp-image-176\" srcset=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/products_tags_tables.png 622w, https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/products_tags_tables-300x82.png 300w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\" \/><\/figure>\n\n\n\n<p>The <code>product_tags<\/code> will have two foreign keys; one references the <code>product_id<\/code> column in the products table, and another references the <code>tag_id<\/code> column in the <code>tags<\/code> table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='foreign-key-constraints'>Foreign key constraints <a href=\"#foreign-key-constraints\" class=\"anchor\" id=\"foreign-key-constraints\" title=\"Anchor for Foreign key constraints\">#<\/a><\/h2>\n\n\n\n<p>In PostgreSQL, you use the foreign key constraint to set up a foreign key.<\/p>\n\n\n\n<p>Here&#8217;s the basic syntax for defining a foreign key constraint:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CONSTRAINT<\/span> <span class=\"hljs-built_in\">constraint_name<\/span>\n<span class=\"hljs-keyword\">FOREIGN KEY<\/span> (fk_column)\n<span class=\"hljs-keyword\">REFERENCES<\/span> <span class=\"hljs-keyword\">table<\/span>(pk_column)\n<span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">DELETE<\/span> delete_action\n<span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">UPDATE<\/span> update_action;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the name of the foreign key constraint in the <code>CONSTRAINT<\/code> clause. If you don&#8217;t, PostgreSQL will automatically generate a constraint name.<\/li>\n\n\n\n<li>Second, place the foreign key column (<code>fk_column<\/code>) inside the parentheses <code>()<\/code> after the <code>FOREIGN KEY<\/code> keywords.<\/li>\n\n\n\n<li>Third, provide the table and primary key column that the foreign key column references after the <code>REFERENCES<\/code> keyword.<\/li>\n\n\n\n<li>Finally, set the delete and update actions to specify the behaviors when a row in the parent table primary key in the table is deleted and updated.<\/li>\n<\/ul>\n\n\n\n<p>The <code>FOREIGN KEY<\/code> and <code>REFERENCES<\/code> clauses are mandatory, while the <code>CONSTRAINT<\/code>, <code>ON DELETE<\/code>, and <code>ON UPDATE<\/code> clauses are optional.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='foreign-key-constraint-example'>Foreign key constraint example <a href=\"#foreign-key-constraint-example\" class=\"anchor\" id=\"foreign-key-constraint-example\" title=\"Anchor for Foreign key constraint example\">#<\/a><\/h2>\n\n\n\n<p>First, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-table\/\">create a new table<\/a> called <code>brands<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> brands (\n    brand_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n    <span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-table\/\">create a new table<\/a> called <code>products<\/code> with the foreign key column <code>brand_id<\/code> that references the <code>brand_id<\/code> column of the <code>brands<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products (\n   product_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n   <span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n   price <span class=\"hljs-type\">DECIMAL<\/span>(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n   brand_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n   <span class=\"hljs-keyword\">FOREIGN KEY<\/span> (brand_id) <span class=\"hljs-keyword\">REFERENCES<\/span> brands (brand_id)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-insert\/\">insert three rows<\/a> into the <code>brands<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> brands(<span class=\"hljs-type\">name<\/span>)\n<span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-string\">'Apple'<\/span>), (<span class=\"hljs-string\">'Samsung'<\/span>), (<span class=\"hljs-string\">'Google'<\/span>)\n<span class=\"hljs-keyword\">RETURNING<\/span> *;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\"> brand_id |  name\n----------+---------\n        1 | Apple\n        2 | Samsung\n        3 | Google<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='inserting-data-into-a-foreign-key-table'>Inserting data into a foreign key table <a href=\"#inserting-data-into-a-foreign-key-table\" class=\"anchor\" id=\"inserting-data-into-a-foreign-key-table\" title=\"Anchor for Inserting data into a foreign key table\">#<\/a><\/h2>\n\n\n\n<p>If you <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-insert\/\">insert a new row<\/a> into the <code>products<\/code> table, the value of the <code>brand_id<\/code> must exist in the <code>brands<\/code> table. For example, the following insert a new row into the <code>products<\/code> table with the <code>brand_id<\/code> 1:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> products(<span class=\"hljs-type\">name<\/span>, price, brand_id)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'iPhone 14 Pro'<\/span>, <span class=\"hljs-number\">999.99<\/span>, <span class=\"hljs-number\">1<\/span>)\n<span class=\"hljs-keyword\">RETURNING<\/span> *;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=SU5TRVJUIElOVE8gcHJvZHVjdHMgKG5hbWUsIHByaWNlLCBicmFuZF9pZCkgVkFMVUVTICgnaVBob25lIDE0IFBybycsIDk5OS45OSwgMSkgUkVUVVJOSU5HICo7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\"> product_id |     name      | price  | brand_id\n------------+---------------+--------+----------\n          1 | iPhone 14 Pro | 999.99 |        1<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You will encounter an error if you attempt to insert a row with a brand id that does not exist in the <code>brands<\/code> table.<\/p>\n\n\n\n<p>The following statement attempts to insert a new row into the <code>products<\/code> table with an invalid brand id:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> products(<span class=\"hljs-type\">name<\/span>, price, brand_id)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'iPhone 15 Pro'<\/span>, <span class=\"hljs-number\">1299.99<\/span>, <span class=\"hljs-number\">11<\/span>)\n<span class=\"hljs-keyword\">RETURNING<\/span> *;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=SU5TRVJUIElOVE8gcHJvZHVjdHMgKG5hbWUsIHByaWNlLCBicmFuZF9pZCkgVkFMVUVTICgnaVBob25lIDE1IFBybycsIDEyOTkuOTksIDExKSBSRVRVUk5JTkcgKjs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>PostgreSQL issues the following error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">insert or update on table \"products\" violates foreign key constraint \"products_brand_id_fkey\"<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='dropping-tables-with-foreign-key-constraints'>Dropping tables with foreign key constraints <a href=\"#dropping-tables-with-foreign-key-constraints\" class=\"anchor\" id=\"dropping-tables-with-foreign-key-constraints\" title=\"Anchor for Dropping tables with foreign key constraints\">#<\/a><\/h2>\n\n\n\n<p>When you <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-drop-table\/\">drop a table<\/a> referenced by other tables via foreign key constraints, PostgreSQL will issue an error.<\/p>\n\n\n\n<p>For example, the <code>products<\/code> table references the <code>brands<\/code> table via a foreign key constraint. If you drop the brands table, you will get an error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> brands;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=RFJPUCBUQUJMRSBicmFuZHM7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">cannot drop table brands because other objects depend on it<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To drop the <code>brands<\/code> table, you follow these steps: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, drop foreign key constraint that references the <code>brand_id<\/code> column in the table brands.<\/li>\n\n\n\n<li>Second, drop the table <code>brands<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>You can do both steps using the <code>DROP TABLE ... CASCADE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> brands <span class=\"hljs-keyword\">CASCADE<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=RFJPUCBUQUJMRSBicmFuZHMgQ0FTQ0FERTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>This statement dropped the constraint <code>products_brand_id_fkey<\/code> on the table <code>products<\/code>.<\/p>\n\n\n\n<p>Let&#8217;s also drop the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=RFJPUCBUQUJMRSBwcm9kdWN0czs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='deletion-actions'>Deletion actions <a href=\"#deletion-actions\" class=\"anchor\" id=\"deletion-actions\" title=\"Anchor for Deletion actions\">#<\/a><\/h2>\n\n\n\n<p>The deletion action allows you to specify an action to the rows in the child tables when rows in the parent table are deleted:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">DELETE<\/span> delete_action;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>delete_action<\/code> can be one of the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>NO ACTION<\/code> &#8211; issues a constraint violation error. The <code>NO ACTION<\/code> is the default.<\/li>\n\n\n\n<li><code>SET NULL<\/code> &#8211; sets the values of foreign key column to <code>NULL<\/code>.<\/li>\n\n\n\n<li><code>CASCADE<\/code> &#8211; deletes all related rows in the child table.<\/li>\n\n\n\n<li><code>SET DEFAULT<\/code> &#8211; sets the default values for the foreign key columns in the child table.<\/li>\n\n\n\n<li><code>RESTRICT<\/code> works like the <code>NO ACTION.<\/code> <\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='on-delete-set-null'>ON DELETE SET NULL <a href=\"#on-delete-set-null\" class=\"anchor\" id=\"on-delete-set-null\" title=\"Anchor for ON DELETE SET NULL\">#<\/a><\/h3>\n\n\n\n<p>First, recreate the <code>brands<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> brands (\n    brand_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n    <span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=Q1JFQVRFIFRBQkxFIGJyYW5kcyAoIGJyYW5kX2lkIElOVCBHRU5FUkFURUQgQUxXQVlTIEFTIElERU5USVRZIFBSSU1BUlkgS0VZLCBuYW1lIFZBUkNIQVIoMjU1KSBOT1QgTlVMTCApOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Second, create the <code>products<\/code> table that has the <code>brand_id<\/code> as the foreign key column referenced the <code>brand_id<\/code> column of the <code>brands<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products (\n  product_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n  <span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  price <span class=\"hljs-type\">DECIMAL<\/span>(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  brand_id <span class=\"hljs-type\">INT<\/span>,\n  <span class=\"hljs-keyword\">FOREIGN KEY<\/span> (brand_id) <span class=\"hljs-keyword\">REFERENCES<\/span> brands (brand_id) \n  <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">SET<\/span> <span class=\"hljs-keyword\">NULL<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=Q1JFQVRFIFRBQkxFIHByb2R1Y3RzICggcHJvZHVjdF9pZCBJTlQgR0VORVJBVEVEIEFMV0FZUyBBUyBJREVOVElUWSBQUklNQVJZIEtFWSwgbmFtZSBWQVJDSEFSKDEwMCkgTk9UIE5VTEwsIHByaWNlIERFQ0lNQUwoMTAsIDIpIE5PVCBOVUxMLCBicmFuZF9pZCBJTlQsIEZPUkVJR04gS0VZIChicmFuZF9pZCkgUkVGRVJFTkNFUyBicmFuZHMgKGJyYW5kX2lkKSBPTiBERUxFVEUgU0VUIE5VTEwgKTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Third, insert rows into the <code>brands<\/code> and <code>products<\/code> tables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> brands(<span class=\"hljs-type\">name<\/span>)\n<span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-string\">'Apple'<\/span>), (<span class=\"hljs-string\">'Samsung'<\/span>)\n<span class=\"hljs-keyword\">RETURNING<\/span> *;\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> products(<span class=\"hljs-type\">name<\/span>, price, brand_id)\n<span class=\"hljs-keyword\">VALUES<\/span>\n(<span class=\"hljs-string\">'iPhone 14 Pro'<\/span>, <span class=\"hljs-number\">999.99<\/span>, <span class=\"hljs-number\">1<\/span>),\n(<span class=\"hljs-string\">'iPhone 15 Pro'<\/span>, <span class=\"hljs-number\">1299.99<\/span>, <span class=\"hljs-number\">1<\/span>),\n(<span class=\"hljs-string\">'Galaxy S23 Ultra'<\/span>, <span class=\"hljs-number\">1299.99<\/span>, <span class=\"hljs-number\">2<\/span>)\n<span class=\"hljs-keyword\">RETURNING<\/span> *;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=SU5TRVJUIElOVE8gYnJhbmRzKG5hbWUpIFZBTFVFUyAoJ0FwcGxlJyksICgnU2Ftc3VuZycpIFJFVFVSTklORyAqOyBJTlNFUlQgSU5UTyBwcm9kdWN0cyhuYW1lLCBwcmljZSwgYnJhbmRfaWQpIFZBTFVFUyAoJ2lQaG9uZSAxNCBQcm8nLCA5OTkuOTksIDEpLCAoJ2lQaG9uZSAxNSBQcm8nLCAxMjk5Ljk5LCAxKSwgKCdHYWxheHkgUzIzIFVsdHJhJywgMTI5OS45OSwgMikgUkVUVVJOSU5HICo7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Fourth, delete a row from the <code>brands<\/code> table with the id 1:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">FROM<\/span> brands <span class=\"hljs-keyword\">WHERE<\/span> brand_id = <span class=\"hljs-number\">1<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=REVMRVRFIEZST00gYnJhbmRzIFdIRVJFIGJyYW5kX2lkID0gMTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Finally, retrieve data from the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=U0VMRUNUICogRlJPTSBwcm9kdWN0czs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> product_id |       <span class=\"hljs-type\">name<\/span>       |  price  | brand_id\n<span class=\"hljs-comment\">------------+------------------+---------+----------<\/span>\n          <span class=\"hljs-number\">3<\/span> | Galaxy S23 Ultra | <span class=\"hljs-number\">1299.99<\/span> |        <span class=\"hljs-number\">2<\/span>\n          <span class=\"hljs-number\">1<\/span> | iPhone <span class=\"hljs-number\">14<\/span> Pro    |  <span class=\"hljs-number\">999.99<\/span> |     <span class=\"hljs-keyword\">NULL<\/span>\n          <span class=\"hljs-number\">2<\/span> | iPhone <span class=\"hljs-number\">15<\/span> Pro    | <span class=\"hljs-number\">1299.99<\/span> |     <span class=\"hljs-keyword\">NULL<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The output shows that the <code>ON DELETE SET NULL<\/code> action sets the values of the <code>brand_id<\/code> column of the related products to <code>NULL<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='on-delete-cascade'>ON DELETE CASCADE <a href=\"#on-delete-cascade\" class=\"anchor\" id=\"on-delete-cascade\" title=\"Anchor for ON DELETE CASCADE\">#<\/a><\/h3>\n\n\n\n<p>First, drop the <code>brands<\/code> and <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> brands, products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=RFJPUCBUQUJMRSBicmFuZHMsIHByb2R1Y3RzOw%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Second, create the <code>brands<\/code> table and <code>products<\/code> table with the <code>ON DELETE CASCADE<\/code> action:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> brands (\n    brand_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n    <span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>\n);\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products (\n  product_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n  <span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  price <span class=\"hljs-type\">DECIMAL<\/span>(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  brand_id <span class=\"hljs-type\">INT<\/span>,\n  <span class=\"hljs-keyword\">FOREIGN KEY<\/span> (brand_id) <span class=\"hljs-keyword\">REFERENCES<\/span> brands (brand_id) \n  <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">CASCADE<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=Q1JFQVRFIFRBQkxFIGJyYW5kcyAoIGJyYW5kX2lkIElOVCBHRU5FUkFURUQgQUxXQVlTIEFTIElERU5USVRZIFBSSU1BUlkgS0VZLCBuYW1lIFZBUkNIQVIoMjU1KSBOT1QgTlVMTCApOyBDUkVBVEUgVEFCTEUgcHJvZHVjdHMgKCBwcm9kdWN0X2lkIElOVCBHRU5FUkFURUQgQUxXQVlTIEFTIElERU5USVRZIFBSSU1BUlkgS0VZLCBuYW1lIFZBUkNIQVIoMTAwKSBOT1QgTlVMTCwgcHJpY2UgREVDSU1BTCgxMCwgMikgTk9UIE5VTEwsIGJyYW5kX2lkIElOVCwgRk9SRUlHTiBLRVkgKGJyYW5kX2lkKSBSRUZFUkVOQ0VTIGJyYW5kcyAoYnJhbmRfaWQpIE9OIERFTEVURSBDQVNDQURFICk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Third, insert rows into the <code>products<\/code> tables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> brands(<span class=\"hljs-type\">name<\/span>)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'Apple'<\/span>), (<span class=\"hljs-string\">'Samsung'<\/span>)\n<span class=\"hljs-keyword\">RETURNING<\/span> *;\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> products(<span class=\"hljs-type\">name<\/span>, price, brand_id)\n<span class=\"hljs-keyword\">VALUES<\/span>\n(<span class=\"hljs-string\">'iPhone 14 Pro'<\/span>, <span class=\"hljs-number\">999.99<\/span>, <span class=\"hljs-number\">1<\/span>),\n(<span class=\"hljs-string\">'iPhone 15 Pro'<\/span>, <span class=\"hljs-number\">1299.99<\/span>, <span class=\"hljs-number\">1<\/span>),\n(<span class=\"hljs-string\">'Galaxy S23 Ultra'<\/span>, <span class=\"hljs-number\">1299.99<\/span>, <span class=\"hljs-number\">2<\/span>)\n<span class=\"hljs-keyword\">RETURNING<\/span> *;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=SU5TRVJUIElOVE8gYnJhbmRzKG5hbWUpIFZBTFVFUygnQXBwbGUnKSwgKCdTYW1zdW5nJykgUkVUVVJOSU5HICo7IElOU0VSVCBJTlRPIHByb2R1Y3RzKG5hbWUsIHByaWNlLCBicmFuZF9pZCkgVkFMVUVTICgnaVBob25lIDE0IFBybycsIDk5OS45OSwgMSksICgnaVBob25lIDE1IFBybycsIDEyOTkuOTksIDEpLCAoJ0dhbGF4eSBTMjMgVWx0cmEnLCAxMjk5Ljk5LCAyKSBSRVRVUk5JTkcgKjs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Fourth, delete a row from the <code>brands<\/code> table with the id 1:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">FROM<\/span> brands \n<span class=\"hljs-keyword\">WHERE<\/span> brand_id = <span class=\"hljs-number\">1<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=REVMRVRFIEZST00gYnJhbmRzIFdIRVJFIGJyYW5kX2lkID0gMTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Finally, retrieve data from the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=U0VMRUNUICogRlJPTSBwcm9kdWN0czs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\"> product_id |       name       |  price  | brand_id\n------------+------------------+---------+----------\n          3 | Galaxy S23 Ultra | 1299.99 |        2<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The output shows that the <code>ON DELETE CASCADE<\/code> action delete the rows in the <code>products<\/code> table with the <code>brand_id<\/code> column 1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='on-delete-set-default'>ON DELETE SET DEFAULT <a href=\"#on-delete-set-default\" class=\"anchor\" id=\"on-delete-set-default\" title=\"Anchor for ON DELETE SET DEFAULT\">#<\/a><\/h2>\n\n\n\n<p>First, drop the <code>brands<\/code> and <code>products<\/code> tables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> brands, products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=RFJPUCBUQUJMRSBicmFuZHMsIHByb2R1Y3RzOw%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Second, create the <code>brands<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-28\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> brands (\n    brand_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n    <span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=Q1JFQVRFIFRBQkxFIGJyYW5kcyAoIGJyYW5kX2lkIElOVCBHRU5FUkFURUQgQUxXQVlTIEFTIElERU5USVRZIFBSSU1BUlkgS0VZLCBuYW1lIFZBUkNIQVIoMjU1KSBOT1QgTlVMTCApOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Third, create the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-29\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products (\n  product_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span>  <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n  <span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  price <span class=\"hljs-type\">DECIMAL<\/span>(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  brand_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-number\">1<\/span>,\n  <span class=\"hljs-keyword\">FOREIGN KEY<\/span> (brand_id) <span class=\"hljs-keyword\">REFERENCES<\/span> brands (brand_id) \n  <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">SET<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-29\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=Q1JFQVRFIFRBQkxFIHByb2R1Y3RzICggcHJvZHVjdF9pZCBJTlQgR0VORVJBVEVEIEFMV0FZUyBBUyBJREVOVElUWSBQUklNQVJZIEtFWSwgbmFtZSBWQVJDSEFSKDEwMCkgTk9UIE5VTEwsIHByaWNlIERFQ0lNQUwoMTAsIDIpIE5PVCBOVUxMLCBicmFuZF9pZCBJTlQgREVGQVVMVCAxLCBGT1JFSUdOIEtFWSAoYnJhbmRfaWQpIFJFRkVSRU5DRVMgYnJhbmRzIChicmFuZF9pZCkgT04gREVMRVRFIFNFVCBERUZBVUxUICk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this statement, we set the default value of the <code>brand_id<\/code> column to 1.<\/p>\n\n\n\n<p>Fourth, insert rows into the <code>brands<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-30\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> brands(<span class=\"hljs-type\">name<\/span>)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'Unknown'<\/span>),\n      (<span class=\"hljs-string\">'Apple'<\/span>), \n      (<span class=\"hljs-string\">'Samsung'<\/span>)\n<span class=\"hljs-keyword\">RETURNING<\/span> *;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-30\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=SU5TRVJUIElOVE8gYnJhbmRzKG5hbWUpIFZBTFVFUygnVW5rbm93bicpLCAoJ0FwcGxlJyksICgnU2Ftc3VuZycpIFJFVFVSTklORyAqOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The row with id <code>1<\/code> will be &#8220;<code>Unknown<\/code>&#8220;.<\/p>\n\n\n\n<p>Fifth, insert rows into <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-31\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> products(<span class=\"hljs-type\">name<\/span>, price, brand_id)\n<span class=\"hljs-keyword\">VALUES<\/span>\n(<span class=\"hljs-string\">'iPhone 14 Pro'<\/span>, <span class=\"hljs-number\">999.99<\/span>, <span class=\"hljs-number\">2<\/span>),\n(<span class=\"hljs-string\">'iPhone 15 Pro'<\/span>, <span class=\"hljs-number\">1299.99<\/span>, <span class=\"hljs-number\">2<\/span>),\n(<span class=\"hljs-string\">'Galaxy S23 Ultra'<\/span>, <span class=\"hljs-number\">1299.99<\/span>, <span class=\"hljs-number\">3<\/span>)\n<span class=\"hljs-keyword\">RETURNING<\/span> *;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-31\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=SU5TRVJUIElOVE8gcHJvZHVjdHMobmFtZSwgcHJpY2UsIGJyYW5kX2lkKSBWQUxVRVMgKCdpUGhvbmUgMTQgUHJvJywgOTk5Ljk5LCAyKSwgKCdpUGhvbmUgMTUgUHJvJywgMTI5OS45OSwgMiksICgnR2FsYXh5IFMyMyBVbHRyYScsIDEyOTkuOTksIDMpIFJFVFVSTklORyAqOw\">Try it<\/a><\/p>\n\n\n\n<p>Sixth, delete a row from the <code>brands<\/code> table with the id 2:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-32\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">FROM<\/span> brands \n<span class=\"hljs-keyword\">WHERE<\/span> brand_id = <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-32\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=REVMRVRFIEZST00gYnJhbmRzIFdIRVJFIGJyYW5kX2lkID0gMjs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Due to the <code>ON DELETE SET DEFAULT<\/code> action, the statement sets the value of the <code>brand_id<\/code> column of the rows with <code>brand_id<\/code> 2 to 1.<\/p>\n\n\n\n<p>Seventh, retrieve data from the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-33\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-33\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=fk&amp;q=U0VMRUNUICogRlJPTSBwcm9kdWN0czs\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-34\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\"> product_id |       name       |  price  | brand_id\n------------+------------------+---------+----------\n          3 | Galaxy S23 Ultra | 1299.99 |        3\n          1 | iPhone 14 Pro    |  999.99 |        1\n          2 | iPhone 15 Pro    | 1299.99 |        1<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-34\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The output shows that the <code>ON DELETE SET DEFAULT<\/code> action sets the values of the <code>brand_id<\/code> column of the related products to a default value of the <code>brand_id<\/code> column, which is 1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A foreign key is a column or set of columns that references the primary key of another table.<\/li>\n\n\n\n<li>A table can have one or more foreign key columns.<\/li>\n\n\n\n<li>Use the PostgreSQL foreign key constraint to create a foreign key for a table.<\/li>\n\n\n\n<li>Use the <code>ON DELETE SET NULL<\/code> to set the value of the foreign key columns to NULL when the corresponding rows from the parent table are deleted.<\/li>\n\n\n\n<li>Use the <code>ON DELETE CASCADE<\/code> to delete the rows in the child table when the corresponding rows from the parent table are deleted.<\/li>\n\n\n\n<li>Use the <code>ON DELETE SET DEFAULT<\/code> to set the values of the foreign key columns to their default values when the corresponding rows from the parent table are deleted.<\/li>\n\n\n\n<li>Use the <code>ON DELETE NO ACTION<\/code> to prevent the rows from the parent table to be deleted when they have referenced rows in the child tables.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=foreign-key\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"170\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-foreign-key\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Foreign Key\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"170\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-foreign-key\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Foreign Key\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;ll learn about foreign keys and how to use PostgreSQL foreign key constraint to create foreign key columns for a table.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":17,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-170","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL Foreign Key<\/title>\n<meta name=\"description\" content=\"You&#039;ll learn about foreign keys and how to use PostgreSQL foreign key constraint to create foreign key columns for a table.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-foreign-key\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL Foreign Key\" \/>\n<meta property=\"og:description\" content=\"You&#039;ll learn about foreign keys and how to use PostgreSQL foreign key constraint to create foreign key columns for a table.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-foreign-key\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-02T01:51:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/postgresql-foreign-key.png\" \/>\n\t<meta property=\"og:image:width\" content=\"862\" \/>\n\t<meta property=\"og:image:height\" content=\"246\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-foreign-key\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-foreign-key\\\/\",\"name\":\"PostgreSQL Foreign Key\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-foreign-key\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-foreign-key\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/postgresql-foreign-key.png\",\"datePublished\":\"2024-11-20T09:32:49+00:00\",\"dateModified\":\"2025-01-02T01:51:13+00:00\",\"description\":\"You'll learn about foreign keys and how to use PostgreSQL foreign key constraint to create foreign key columns for a table.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-foreign-key\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-foreign-key\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-foreign-key\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/postgresql-foreign-key.png\",\"contentUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/postgresql-foreign-key.png\",\"width\":862,\"height\":246,\"caption\":\"postgresql foreign key\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-foreign-key\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Tutorial\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL Foreign Key\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PostgreSQL Foreign Key","description":"You'll learn about foreign keys and how to use PostgreSQL foreign key constraint to create foreign key columns for a table.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-foreign-key\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL Foreign Key","og_description":"You'll learn about foreign keys and how to use PostgreSQL foreign key constraint to create foreign key columns for a table.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-foreign-key\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-02T01:51:13+00:00","og_image":[{"width":862,"height":246,"url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/postgresql-foreign-key.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-foreign-key\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-foreign-key\/","name":"PostgreSQL Foreign Key","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-foreign-key\/#primaryimage"},"image":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-foreign-key\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/postgresql-foreign-key.png","datePublished":"2024-11-20T09:32:49+00:00","dateModified":"2025-01-02T01:51:13+00:00","description":"You'll learn about foreign keys and how to use PostgreSQL foreign key constraint to create foreign key columns for a table.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-foreign-key\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-foreign-key\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-foreign-key\/#primaryimage","url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/postgresql-foreign-key.png","contentUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/11\/postgresql-foreign-key.png","width":862,"height":246,"caption":"postgresql foreign key"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-foreign-key\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Tutorial","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL Foreign Key"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=170"}],"version-history":[{"count":18,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/170\/revisions"}],"predecessor-version":[{"id":1335,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/170\/revisions\/1335"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}