{"id":2173,"date":"2013-05-08T20:11:44","date_gmt":"2013-05-09T04:11:44","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=2173"},"modified":"2023-12-27T22:06:31","modified_gmt":"2023-12-28T05:06:31","slug":"mysql-foreign-key","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-foreign-key\/","title":{"rendered":"MySQL Foreign Key"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about <strong>MySQL foreign key<\/strong> and how to create, drop, and disable a foreign key constraint.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL foreign key<\/h2>\n\n\n\n<p>A foreign key is a column or group of columns in a table that links to a column or group of columns in another table. The foreign key places constraints on data in the related tables, which allows MySQL to maintain referential integrity.<\/p>\n\n\n\n<p>Let&#8217;s take a look at the following <code>customers<\/code> and <code>orders<\/code> tables from the <a title=\"MySQL Sample Database\" href=\"https:\/\/www.mysqltutorial.org\/getting-started-with-mysql\/mysql-sample-database\/\">sample database<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"434\" height=\"304\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png\" alt=\"\" class=\"wp-image-8036\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png 434w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders-200x140.png 200w\" sizes=\"auto, (max-width: 434px) 100vw, 434px\" \/><\/figure>\n\n\n\n<p>In this diagram, each customer can have zero or many orders and each order belongs to one customer.<\/p>\n\n\n\n<p>The relationship between <code>customers<\/code> table and <code>orders<\/code> table is <strong>one-to-many<\/strong>. This relationship is established via the foreign key in the <code>orders<\/code> table specified by the <code>customerNumber<\/code> column.<\/p>\n\n\n\n<p>The <code>customerNumber<\/code> column in the <code>orders<\/code> table links to the <code>customerNumber<\/code> primary key column in the <code>customers<\/code> table.<\/p>\n\n\n\n<p>The <code>customers<\/code> table is called the&nbsp;<em>parent table<\/em> or <em>referenced table<\/em>, and the <code>orders<\/code> table is known as the <em>child table<\/em> or <em>referencing table<\/em>.<\/p>\n\n\n\n<p>Typically, the foreign key columns of the child table often refer to the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-primary-key\/\">primary key<\/a> columns of the parent table.<\/p>\n\n\n\n<p>A table can have more than one foreign key where each foreign key references a primary key of the different parent tables.<\/p>\n\n\n\n<p>Once a foreign key constraint is in place, the foreign key columns from the child table must have the corresponding row in the parent key columns of the parent table, or values in these foreign key columns must be <code>NULL<\/code> (see the <code>SET NULL<\/code> action example below).<\/p>\n\n\n\n<p>For example, each row in the <code>orders<\/code> table has a <code>customerNumber<\/code> that exists in the <code>customerNumber<\/code> column of the <code>customers<\/code> table. Multiple rows in the <code>orders<\/code> table can have the same <code>customerNumber<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Self-referencing foreign key<\/h3>\n\n\n\n<p>Sometimes, the child and parent tables may refer to the same table. In this case, the foreign key references back to the primary key within the same table.<\/p>\n\n\n\n<p>See the following <code>employees<\/code> table from the <a href=\"https:\/\/www.mysqltutorial.org\/getting-started-with-mysql\/mysql-sample-database\/\">sample database<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"163\" height=\"204\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/employees.png\" alt=\"\" class=\"wp-image-8120\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/employees.png 163w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/employees-160x200.png 160w\" sizes=\"auto, (max-width: 163px) 100vw, 163px\" \/><\/figure>\n\n\n\n<p>The <code>reportTo<\/code> column is a foreign key that refers to the <code>employeeNumber<\/code> column which is the primary key of the <code>employees<\/code> table.<\/p>\n\n\n\n<p>This relationship allows the <code>employees<\/code> table to store the reporting structure between employees and managers. Each employee reports to zero or one employee and an employee can have zero or many subordinates.<\/p>\n\n\n\n<p>The foreign key on the column <code>reportTo<\/code> is known as a <em>recursive<\/em> or <em>self-referencing<\/em> foreign key.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL FOREIGN KEY syntax<\/h2>\n\n\n\n<p>Here is the basic syntax of defining a foreign key constraint in the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">CREATE TABLE<\/a><\/code> or <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-alter-table\/\">ALTER TABLE<\/a><\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">&#91;CONSTRAINT constraint_name]\nFOREIGN KEY &#91;foreign_key_name] (column_name, ...)\nREFERENCES parent_table(colunm_name,...)\n&#91;ON <span class=\"hljs-keyword\">DELETE<\/span> reference_option]\n&#91;<span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">UPDATE<\/span> reference_option]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<p>First, specify the name of the foreign key constraint that you want to create after the <code>CONSTRAINT<\/code> keyword. If you omit the constraint name, MySQL automatically generates a name for the foreign key constraint.<\/p>\n\n\n\n<p>Second, specify a list of comma-separated foreign key columns after the <code>FOREIGN KEY<\/code> keywords. The foreign key name is also optional and is generated automatically if you skip it.<\/p>\n\n\n\n<p>Third, specify the parent table followed by a list of comma-separated columns to which the foreign key columns reference.<\/p>\n\n\n\n<p>Finally, specify how the foreign key maintains the referential integrity between the child and parent tables by using the <code>ON DELETE<\/code> and <code>ON UPDATE<\/code> clauses. The <code>reference_option<\/code> determines the action that MySQL will take when values in the parent key columns are deleted (<code>ON DELETE<\/code>) or updated (<code>ON UPDATE<\/code>).<\/p>\n\n\n\n<p>MySQL has five reference options: <code>CASCADE<\/code>, <code>SET NULL<\/code>, <code>NO ACTION<\/code>, <code>RESTRICT<\/code>, and <code>SET DEFAULT<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code class=\"literal\">CASCADE<\/code>: if a row from the parent table is deleted or updated, the values of the matching rows in the child table are automatically deleted or updated.<\/li>\n\n\n\n<li><code>SET NULL<\/code>:&nbsp; if a row from the parent table is deleted or updated, the values of the foreign key column (or columns) in the child table are set to <code>NULL<\/code>.<\/li>\n\n\n\n<li><code class=\"literal\">RESTRICT<\/code>:&nbsp; if a row from the parent table has a matching row in the child table, MySQL rejects deleting or updating rows in the parent table.<\/li>\n\n\n\n<li><code>NO ACTION<\/code>: is the same as <code>RESTRICT<\/code>.<\/li>\n\n\n\n<li><code>SET DEFAULT<\/code>: is recognized by the MySQL parser. However, this action is rejected by both InnoDB and NDB tables.<\/li>\n<\/ul>\n\n\n\n<p>MySQL fully supports three actions: <code>RESTRICT<\/code>, <code>CASCADE<\/code> and <code>SET NULL<\/code>.<\/p>\n\n\n\n<p>If you don&#8217;t specify the <code>ON DELETE<\/code> and <code>ON UPDATE<\/code> clause, the default action is <code class=\"literal\">RESTRICT<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL FOREIGN KEY examples<\/h2>\n\n\n\n<p>Let&#8217;s <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-database\/\">create a new database<\/a> called <code>fkdemo<\/code> for the demonstration.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">DATABASE<\/span> fkdemo;\n\n<span class=\"hljs-keyword\">USE<\/span> fkdemo;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">1) RESTRICT &amp; NO ACTION actions<\/h3>\n\n\n\n<p>Inside the <code>fkdemo<\/code> database, create two tables <code>categories<\/code> and <code>products<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> categories(\n  categoryId <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>, \n  categoryName <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>\n) <span class=\"hljs-keyword\">ENGINE<\/span> = <span class=\"hljs-keyword\">INNODB<\/span>;\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products(\n  productId <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>, \n  productName <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>, \n  categoryId <span class=\"hljs-built_in\">INT<\/span>, \n  <span class=\"hljs-keyword\">CONSTRAINT<\/span> fk_category <span class=\"hljs-keyword\">FOREIGN<\/span> <span class=\"hljs-keyword\">KEY<\/span> (categoryId) \n                         <span class=\"hljs-keyword\">REFERENCES<\/span> categories(categoryId)\n) <span class=\"hljs-keyword\">ENGINE<\/span> = <span class=\"hljs-keyword\">INNODB<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>categoryId<\/code> in the <code>products<\/code> table is the foreign key column that refers to the <code>categoryId<\/code> column in the&nbsp; <code>categories<\/code> table.<\/p>\n\n\n\n<p>Because we don&#8217;t specify any <code>ON UPDATE<\/code> and <code>ON DELETE<\/code> clauses, the default action is <code>RESTRICT<\/code> for both update and delete operations.<\/p>\n\n\n\n<p>The following steps illustrate the <code>RESTRICT<\/code> action.<\/p>\n\n\n\n<p>1) <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert-multiple-rows\/\">Insert two rows<\/a> into the <code>categories<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> categories(categoryName)\n<span class=\"hljs-keyword\">VALUES<\/span>\n    (<span class=\"hljs-string\">'Smartphone'<\/span>),\n    (<span class=\"hljs-string\">'Smartwatch'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>2) <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">Select<\/a> data from the <code>categories<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> categories;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"178\" height=\"57\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-categories-table.png\" alt=\"\" class=\"wp-image-8201\"\/><\/figure>\n\n\n\n<p>3) <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\">Insert a new row<\/a> into the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> products(productName, categoryId)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'iPhone'<\/span>,<span class=\"hljs-number\">1<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It works because the <code>categoryId<\/code> 1 exists in the <code>categories<\/code> table.<\/p>\n\n\n\n<p>4) Attempt to insert a new row into the <code>products<\/code> table with a <code>categoryId<\/code>&nbsp; value does not exist in the <code>categories<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> products(productName, categoryId)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'iPad'<\/span>,<span class=\"hljs-number\">3<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>MySQL issued the following error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-built_in\">Error<\/span> Code: <span class=\"hljs-number\">1452.<\/span> Cannot add or update a child row: a foreign key constraint fails (<span class=\"hljs-string\">`fkdemo`<\/span>.<span class=\"hljs-string\">`products`<\/span>, CONSTRAINT <span class=\"hljs-string\">`fk_category`<\/span> FOREIGN KEY (<span class=\"hljs-string\">`categoryId`<\/span>) REFERENCES <span class=\"hljs-string\">`categories`<\/span> (<span class=\"hljs-string\">`categoryId`<\/span>) ON DELETE RESTRICT ON UPDATE RESTRICT)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>5) Update the value in the <code>categoryId<\/code> column in the <code>categories<\/code> table to <code>100<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">UPDATE<\/span> categories\n<span class=\"hljs-keyword\">SET<\/span> categoryId = <span class=\"hljs-number\">100<\/span>\n<span class=\"hljs-keyword\">WHERE<\/span> categoryId = <span class=\"hljs-number\">1<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>MySQL issued this error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-built_in\">Error<\/span> Code: <span class=\"hljs-number\">1451.<\/span> Cannot <span class=\"hljs-keyword\">delete<\/span> or update a parent row: a foreign key constraint fails (<span class=\"hljs-string\">`fkdemo`<\/span>.<span class=\"hljs-string\">`products`<\/span>, CONSTRAINT <span class=\"hljs-string\">`fk_category`<\/span> FOREIGN KEY (<span class=\"hljs-string\">`categoryId`<\/span>) REFERENCES <span class=\"hljs-string\">`categories`<\/span> (<span class=\"hljs-string\">`categoryId`<\/span>) ON DELETE RESTRICT ON UPDATE RESTRICT)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Because of the <code>RESTRICT<\/code> option, you cannot delete or update <code>categoryId 1<\/code> since it is referenced by the <code>productId<\/code> <code>1<\/code> in the <code>products<\/code> table.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2) CASCADE action<\/h3>\n\n\n\n<p>These steps illustrate how <code>ON UPDATE CASCADE<\/code> and <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-on-delete-cascade\/\">ON DELETE CASCADE<\/a><\/code> actions work.<\/p>\n\n\n\n<p>1) <a href=\"https:\/\/www.mysqltutorial.org\/mysql-drop-table\">Drop<\/a> the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>2) Create the <code>products<\/code> table with the <code>ON UPDATE CASCADE<\/code> and <code>ON DELETE CASCADE<\/code> options for the foreign key:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products(\n    productId <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    productName <span class=\"hljs-built_in\">varchar<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-literal\">null<\/span>,\n    categoryId <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    <span class=\"hljs-keyword\">CONSTRAINT<\/span> fk_category\n    <span class=\"hljs-keyword\">FOREIGN<\/span> <span class=\"hljs-keyword\">KEY<\/span> (categoryId) \n    <span class=\"hljs-keyword\">REFERENCES<\/span> categories(categoryId)\n        <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">UPDATE<\/span> <span class=\"hljs-keyword\">CASCADE<\/span>\n        <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">CASCADE<\/span>\n) <span class=\"hljs-keyword\">ENGINE<\/span>=<span class=\"hljs-keyword\">INNODB<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>3) Insert four rows into the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> products(productName, categoryId)\n<span class=\"hljs-keyword\">VALUES<\/span>\n    (<span class=\"hljs-string\">'iPhone'<\/span>, <span class=\"hljs-number\">1<\/span>), \n    (<span class=\"hljs-string\">'Galaxy Note'<\/span>,<span class=\"hljs-number\">1<\/span>),\n    (<span class=\"hljs-string\">'Apple Watch'<\/span>,<span class=\"hljs-number\">2<\/span>),\n    (<span class=\"hljs-string\">'Samsung Galary Watch'<\/span>,<span class=\"hljs-number\">2<\/span>);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>4) Select data from the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"282\" height=\"96\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-products-table.png\" alt=\"\" class=\"wp-image-8202\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-products-table.png 282w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-products-table-200x68.png 200w\" sizes=\"auto, (max-width: 282px) 100vw, 282px\" \/><\/figure>\n\n\n\n<p>5) Update <code>categoryId<\/code> 1 to 100 in the <code>categories<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">UPDATE<\/span> categories\n<span class=\"hljs-keyword\">SET<\/span> categoryId = <span class=\"hljs-number\">100<\/span>\n<span class=\"hljs-keyword\">WHERE<\/span> categoryId = <span class=\"hljs-number\">1<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>6) Verify the update:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> categories;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"178\" height=\"59\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-categories-table-cascade.png\" alt=\"\" class=\"wp-image-8203\"\/><\/figure>\n\n\n\n<p>7) Get data from the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"285\" height=\"98\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-products-table-update-cascade-.png\" alt=\"\" class=\"wp-image-8204\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-products-table-update-cascade-.png 285w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-products-table-update-cascade--200x69.png 200w\" sizes=\"auto, (max-width: 285px) 100vw, 285px\" \/><\/figure>\n\n\n\n<p>As you can see, two rows with value <code>1<\/code> in the <code>categoryId<\/code> column of the <code>products<\/code> table was automatically updated to <code>100<\/code> because of the <code>ON UPDATE CASCADE<\/code> action.<\/p>\n\n\n\n<p>8) Delete <code>categoryId<\/code> 2 from the <code>categories<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">FROM<\/span> categories\n<span class=\"hljs-keyword\">WHERE<\/span> categoryId = <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>9) Verify the deletion:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> categories;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"177\" height=\"40\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-categories-table-delete-cascade.png\" alt=\"\" class=\"wp-image-8205\"\/><\/figure>\n\n\n\n<p>10) Check the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"230\" height=\"58\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-products-table-delete-cascade.png\" alt=\"\" class=\"wp-image-8206\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-products-table-delete-cascade.png 230w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-products-table-delete-cascade-200x50.png 200w\" sizes=\"auto, (max-width: 230px) 100vw, 230px\" \/><\/figure>\n\n\n\n<p>All products with <code>categoryId<\/code> 2 from the <code>products<\/code> table was automatically deleted because of the <code>ON DELETE CASCADE<\/code> action.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3) SET NULL action<\/h3>\n\n\n\n<p>These steps illustrate how the <code>ON UPDATE SET NULL<\/code> and <code>ON DELETE SET NULL<\/code> actions work.<\/p>\n\n\n\n<p>1) Drop both <code>categories<\/code> and <code>products<\/code> tables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">EXISTS<\/span> categories;\n<span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">EXISTS<\/span> products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>2) Create the <code>categories<\/code> and <code>products<\/code> tables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> categories(\n    categoryId <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    categoryName <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>\n)<span class=\"hljs-keyword\">ENGINE<\/span>=<span class=\"hljs-keyword\">INNODB<\/span>;\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products(\n    productId <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    productName <span class=\"hljs-built_in\">varchar<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-literal\">null<\/span>,\n    categoryId <span class=\"hljs-built_in\">INT<\/span>,\n    <span class=\"hljs-keyword\">CONSTRAINT<\/span> fk_category\n    <span class=\"hljs-keyword\">FOREIGN<\/span> <span class=\"hljs-keyword\">KEY<\/span> (categoryId) \n        <span class=\"hljs-keyword\">REFERENCES<\/span> categories(categoryId)\n        <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">UPDATE<\/span> <span class=\"hljs-keyword\">SET<\/span> <span class=\"hljs-literal\">NULL<\/span>\n        <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">SET<\/span> <span class=\"hljs-literal\">NULL<\/span> \n)<span class=\"hljs-keyword\">ENGINE<\/span>=<span class=\"hljs-keyword\">INNODB<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The foreign key in the <code>products<\/code> table changed to <code>ON UPDATE SET NULL<\/code> and <code>ON DELETE SET NULL<\/code> options.<\/p>\n\n\n\n<p>3) Insert rows into the <code>categories<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> categories(categoryName)\n<span class=\"hljs-keyword\">VALUES<\/span>\n    (<span class=\"hljs-string\">'Smartphone'<\/span>),\n    (<span class=\"hljs-string\">'Smartwatch'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>4) Insert rows into the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> products(productName, categoryId)\n<span class=\"hljs-keyword\">VALUES<\/span>\n    (<span class=\"hljs-string\">'iPhone'<\/span>, <span class=\"hljs-number\">1<\/span>), \n    (<span class=\"hljs-string\">'Galaxy Note'<\/span>,<span class=\"hljs-number\">1<\/span>),\n    (<span class=\"hljs-string\">'Apple Watch'<\/span>,<span class=\"hljs-number\">2<\/span>),\n    (<span class=\"hljs-string\">'Samsung Galary Watch'<\/span>,<span class=\"hljs-number\">2<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>5) Update <code>categoryId<\/code> from 1 to 100 in the <code>categories<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">UPDATE<\/span> categories\n<span class=\"hljs-keyword\">SET<\/span> categoryId = <span class=\"hljs-number\">100<\/span>\n<span class=\"hljs-keyword\">WHERE<\/span> categoryId = <span class=\"hljs-number\">1<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>6) Verify the update:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> categories;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"176\" height=\"59\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-categories-table-set-null.png\" alt=\"\" class=\"wp-image-8207\"\/><\/figure>\n\n\n\n<p>7) Select data from the <code>products<\/code> table:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"286\" height=\"98\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-products-table-set-null.png\" alt=\"\" class=\"wp-image-8208\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-products-table-set-null.png 286w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-products-table-set-null-200x69.png 200w\" sizes=\"auto, (max-width: 286px) 100vw, 286px\" \/><\/figure>\n\n\n\n<p>The rows with the <code>categoryId<\/code> 1 in the <code>products<\/code> table was automatically set to <code>NULL<\/code> due to the <code>ON UPDATE SET NULL<\/code> action.<\/p>\n\n\n\n<p>8) Delete the <code>categoryId<\/code> 2 from the <code>categories<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">FROM<\/span> categories \n<span class=\"hljs-keyword\">WHERE<\/span> categoryId = <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>9) Check the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-28\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"284\" height=\"100\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-products-table-on-delete-set-null.png\" alt=\"\" class=\"wp-image-8209\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-products-table-on-delete-set-null.png 284w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/MySQL-Foreign-Key-products-table-on-delete-set-null-200x70.png 200w\" sizes=\"auto, (max-width: 284px) 100vw, 284px\" \/><\/figure>\n\n\n\n<p>The values in the <code>categoryId<\/code> column of the rows with <code>categoryId<\/code> 2 in the <code>products<\/code> table was automatically set to <code>NULL<\/code> due to the <code>ON DELETE SET NULL<\/code> action.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Drop MySQL foreign key constraints<\/h2>\n\n\n\n<p>To drop a foreign key constraint, you use the <code>ALTER TABLE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-29\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name \n<span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">FOREIGN<\/span> <span class=\"hljs-keyword\">KEY<\/span> constraint_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-29\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/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 table from which you want to drop the foreign key after the <code>ALTER TABLE<\/code> keywords.<\/li>\n\n\n\n<li>Second, specify&nbsp; the constraint name after the <code>DROP FOREIGN KEY<\/code> keywords.<\/li>\n<\/ul>\n\n\n\n<p>Notice that <code>constraint_name<\/code> is the name of the foreign key constraint specified when you created or added the foreign key constraint to the table.<\/p>\n\n\n\n<p>To obtain the generated constraint name of a table, you use the <code>SHOW CREATE TABLE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-30\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SHOW<\/span> <span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-30\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, to see the foreign keys of the <code>products<\/code> table, you use the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-31\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SHOW<\/span> <span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-31\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following is the output of the statement:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"510\" height=\"152\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-Foreign-Key-drop-foreign-key-constraint.png\" alt=\"\" class=\"wp-image-8353\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-Foreign-Key-drop-foreign-key-constraint.png 510w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-Foreign-Key-drop-foreign-key-constraint-200x60.png 200w\" sizes=\"auto, (max-width: 510px) 100vw, 510px\" \/><\/figure>\n\n\n\n<p>As you can see clearly from the output, the table <code>products<\/code> table has one foreign key constraint: <code>fk_category<\/code><\/p>\n\n\n\n<p>This statement drops the foreign key constraint of the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-32\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products \n<span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">FOREIGN<\/span> <span class=\"hljs-keyword\">KEY<\/span> fk_category;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-32\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To ensure that the foreign key constraint has been dropped, you can view the structure of the products table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-33\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SHOW<\/span> <span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/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\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"470\" height=\"125\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-Foreign-Key-after-drop-foreign-key-constraint.png\" alt=\"MySQL Foreign Key - after drop foreign key constraint\" class=\"wp-image-8354\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-Foreign-Key-after-drop-foreign-key-constraint.png 470w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-Foreign-Key-after-drop-foreign-key-constraint-200x53.png 200w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Disabling foreign key checks<\/h2>\n\n\n\n<p>Sometimes, it is very useful to disable foreign key checks e.g., when you <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/import-csv-file-mysql-table\/\">import data from a CSV file into a table<\/a>. <\/p>\n\n\n\n<p>If you don&#8217;t disable foreign key checks, you have to load data into a proper order i.e., you have to load data into parent tables first and then child tables, which can be tedious. <\/p>\n\n\n\n<p>However, if you disable the foreign key checks, you can load data into tables in any order.<\/p>\n\n\n\n<p>To disable foreign key checks, you use the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-34\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SET<\/span> foreign_key_checks = <span class=\"hljs-number\">0<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-34\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And you can enable it by using the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-35\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SET<\/span> foreign_key_checks = <span class=\"hljs-number\">1<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-35\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this tutorial, you have learned about the MySQL foreign key and how to create a foreign key constraint with various reference options.<\/p>\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=\"2173\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-foreign-key\/\"\n\t\t\t\tdata-post-title=\"MySQL 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=\"2173\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-foreign-key\/\"\n\t\t\t\tdata-post-title=\"MySQL 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>This tutorial introduces you to MySQL foreign key constraint and shows you step by step how to create a foreign key for a table with various reference options.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":46,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-2173","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>MySQL Foreign Key<\/title>\n<meta name=\"description\" content=\"This tutorial introduces you to MySQL foreign key constraints and shows you how to manage foreign keys effectively.\" \/>\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.mysqltutorial.org\/mysql-foreign-key\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL Foreign Key\" \/>\n<meta property=\"og:description\" content=\"This tutorial introduces you to MySQL foreign key constraints and shows you how to manage foreign keys effectively.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-foreign-key\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-28T05:06:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-foreign-key\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-foreign-key\\\/\",\"name\":\"MySQL Foreign Key\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-foreign-key\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-foreign-key\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/customers-orders.png\",\"datePublished\":\"2013-05-09T04:11:44+00:00\",\"dateModified\":\"2023-12-28T05:06:31+00:00\",\"description\":\"This tutorial introduces you to MySQL foreign key constraints and shows you how to manage foreign keys effectively.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-foreign-key\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-foreign-key\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-foreign-key\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/customers-orders.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/customers-orders.png\",\"width\":434,\"height\":304},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-foreign-key\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Basics\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL Foreign Key\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?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":"MySQL Foreign Key","description":"This tutorial introduces you to MySQL foreign key constraints and shows you how to manage foreign keys effectively.","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.mysqltutorial.org\/mysql-foreign-key\/","og_locale":"en_US","og_type":"article","og_title":"MySQL Foreign Key","og_description":"This tutorial introduces you to MySQL foreign key constraints and shows you how to manage foreign keys effectively.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-foreign-key\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-12-28T05:06:31+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-foreign-key\/","url":"https:\/\/www.mysqltutorial.org\/mysql-foreign-key\/","name":"MySQL Foreign Key","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-foreign-key\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-foreign-key\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png","datePublished":"2013-05-09T04:11:44+00:00","dateModified":"2023-12-28T05:06:31+00:00","description":"This tutorial introduces you to MySQL foreign key constraints and shows you how to manage foreign keys effectively.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-foreign-key\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-foreign-key\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-foreign-key\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-orders.png","width":434,"height":304},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-foreign-key\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Basics","item":"https:\/\/www.mysqltutorial.org\/mysql-basics\/"},{"@type":"ListItem","position":3,"name":"MySQL Foreign Key"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=2173"}],"version-history":[{"count":3,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2173\/revisions"}],"predecessor-version":[{"id":13617,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2173\/revisions\/13617"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/174"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=2173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}