{"id":2089,"date":"2013-05-06T18:33:03","date_gmt":"2013-05-07T02:33:03","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=2089"},"modified":"2020-04-11T02:02:23","modified_gmt":"2020-04-11T09:02:23","slug":"mysql-on-delete-cascade","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-on-delete-cascade\/","title":{"rendered":"MySQL ON DELETE CASCADE"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use MySQL &nbsp;<code>ON DELETE CASCADE<\/code>&nbsp;referential action for a foreign key to delete data from multiple related tables.<\/p>\n\n\n\n<p>In the previous tutorial, you learned how to delete data from multiple related tables using a single <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-delete\/\">DELETE<\/a><\/code> statement. However, MySQL provides a more effective way called <code>ON DELETE CASCADE<\/code> referential action for a <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-foreign-key\/\">foreign key<\/a> that allows you to delete data from child tables automatically when you delete the data from the parent table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL ON DELETE CASCADE example<\/h2>\n\n\n\n<p>Let&#8217;s take a look at an example of using MySQL <code>ON DELETE CASCADE<\/code> .<\/p>\n\n\n\n<p>Suppose that we have two tables:<code>buildings<\/code> and <code>rooms<\/code> . In this database model, each building has one or many rooms. However, each room belongs to one only one building. A room would not exist without a building.<\/p>\n\n\n\n<p>The relationship between the <code>buildings<\/code>&nbsp;and <code>rooms<\/code> tables is one-to-many (1:N) as illustrated in the following database diagram:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"444\" height=\"104\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-ON-DELETE-CASCADE-sample-tables.png\" alt=\"MySQL ON DELETE CASCADE - sample tables\" class=\"wp-image-8350\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-ON-DELETE-CASCADE-sample-tables.png 444w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-ON-DELETE-CASCADE-sample-tables-200x47.png 200w\" sizes=\"auto, (max-width: 444px) 100vw, 444px\" \/><\/figure>\n\n\n\n<p>When you delete a row from the <code>buildings<\/code> table, you also want to delete all rows in the <code>rooms<\/code> &nbsp;table that references to the row in the <code>buildings<\/code> table. For example, when you delete a row with building no. 2 in the <code>buildings<\/code> &nbsp;table as the following query:<\/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\"><span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">FROM<\/span> buildings \n<span class=\"hljs-keyword\">WHERE<\/span> building_no = <span class=\"hljs-number\">2<\/span>;<\/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>You also want the rows in the <code>rooms<\/code> table&nbsp;that refers to building number 2 will be also removed.<\/p>\n\n\n\n<p>The following are steps that demonstrate how the <code>ON DELETE CASCADE<\/code> &nbsp;referential action works.<\/p>\n\n\n\n<p><strong>Step 1<\/strong>. Create the <code>buildings<\/code> table:<\/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\">TABLE<\/span> buildings (\n    building_no <span class=\"hljs-built_in\">INT<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span> AUTO_INCREMENT,\n    building_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    address <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">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\">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><strong>Step 2<\/strong>. Create the <code>rooms<\/code> table:<\/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> rooms (\n    room_no <span class=\"hljs-built_in\">INT<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span> AUTO_INCREMENT,\n    room_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    building_no <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    <span class=\"hljs-keyword\">FOREIGN<\/span> <span class=\"hljs-keyword\">KEY<\/span> (building_no)\n        <span class=\"hljs-keyword\">REFERENCES<\/span> buildings (building_no)\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-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>Notice that the <code>ON DELETE CASCADE<\/code> &nbsp;clause at the end of the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-foreign-key\/\">foreign key constraint<\/a> definition.<\/p>\n\n\n\n<p><strong>Step 3<\/strong>. <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert-multiple-rows\/\">Insert rows<\/a> into the <code>buildings<\/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> buildings(building_name,address)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'ACME Headquaters'<\/span>,<span class=\"hljs-string\">'3950 North 1st Street CA 95134'<\/span>),\n      (<span class=\"hljs-string\">'ACME Sales'<\/span>,<span class=\"hljs-string\">'5000 North 1st Street CA 95134'<\/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><strong>Step 4<\/strong>. Query data from the <code>buildings<\/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> buildings;<\/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=\"374\" height=\"61\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-ON-DELETE-CASCADE-buildings-table.png\" alt=\"MySQL ON DELETE CASCADE buildings table\" class=\"wp-image-6180\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-ON-DELETE-CASCADE-buildings-table.png 374w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-ON-DELETE-CASCADE-buildings-table-300x49.png 300w\" sizes=\"auto, (max-width: 374px) 100vw, 374px\" \/><\/figure>\n\n\n\n<p>We have two rows in&nbsp;the <code>buildings<\/code> table.<\/p>\n\n\n\n<p><strong>Step 5<\/strong>. <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert-multiple-rows\/\">Insert rows<\/a> into the <code>rooms<\/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> rooms(room_name,building_no)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'Amazon'<\/span>,<span class=\"hljs-number\">1<\/span>),\n      (<span class=\"hljs-string\">'War Room'<\/span>,<span class=\"hljs-number\">1<\/span>),\n      (<span class=\"hljs-string\">'Office of CEO'<\/span>,<span class=\"hljs-number\">1<\/span>),\n      (<span class=\"hljs-string\">'Marketing'<\/span>,<span class=\"hljs-number\">2<\/span>),\n      (<span class=\"hljs-string\">'Showroom'<\/span>,<span class=\"hljs-number\">2<\/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><strong>Step 6<\/strong>. Query data from the <code>rooms<\/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\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> rooms;<\/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<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"234\" height=\"122\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-ON-DELETE-CASCADE-rooms-table.png\" alt=\"MySQL ON DELETE CASCADE - rooms table\" class=\"wp-image-6181\"\/><\/figure>\n\n\n\n<p>We have three rooms that belong to building no 1 and two rooms that belong to the building no 2.<\/p>\n\n\n\n<p><strong>Step 7<\/strong>. <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-delete\/\">Delete<\/a> the building with building no. 2:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" 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> buildings \n<span class=\"hljs-keyword\">WHERE<\/span> building_no = <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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><strong>Step 8<\/strong>. Query data from <code>rooms<\/code> table:<\/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\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> rooms;<\/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<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"235\" height=\"81\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-ON-DELETE-CASCADE-rooms-table-after-delete.png\" alt=\"MySQL ON DELETE CASCADE - rooms table after delete\" class=\"wp-image-6182\"\/><\/figure>\n\n\n\n<p>As you can see, all the rows that reference to&nbsp;<code>building_no<\/code> 2 were automatically deleted.<\/p>\n\n\n\n<p class=\"tip\">Notice that <code>ON DELETE CASCADE<\/code> &nbsp;works only with tables with the&nbsp;<a title=\"MySQL Storage Engines\" href=\"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-storage-engines\/\">storage engines<\/a> that support foreign keys e.g., InnoDB.<\/p>\n\n\n\n<p class=\"tip\">Some table types do not support foreign keys such as MyISAM so you should choose appropriate storage engines for the tables that you plan to use the MySQL <code>ON DELETE CASCADE<\/code> &nbsp;referential action.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tips to find tables affected by MySQL ON DELETE CASCADE action<\/h2>\n\n\n\n<p>Sometimes, it is useful to know which table is affected by the <code>ON DELETE CASCADE<\/code> &nbsp;referential action when you delete data from a table. You can query this data from the <code>referential_constraints<\/code> in the <code>information_schema<\/code> &nbsp;database as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">USE<\/span> information_schema;\n\n<span class=\"hljs-keyword\">SELECT<\/span> \n    table_name\n<span class=\"hljs-keyword\">FROM<\/span>\n    referential_constraints\n<span class=\"hljs-keyword\">WHERE<\/span>\n    constraint_schema = <span class=\"hljs-string\">'database_name'<\/span>\n        <span class=\"hljs-keyword\">AND<\/span> referenced_table_name = <span class=\"hljs-string\">'parent_table'<\/span>\n        <span class=\"hljs-keyword\">AND<\/span> delete_rule = <span class=\"hljs-string\">'CASCADE'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><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 find tables that associated with the <code>buildings<\/code> table with the <code>CASCADE<\/code> &nbsp;deletion rule &nbsp;in the <code>classicmodels<\/code> database, you use the following query:<\/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\">USE<\/span> information_schema;\n\n<span class=\"hljs-keyword\">SELECT<\/span> \n    table_name\n<span class=\"hljs-keyword\">FROM<\/span>\n    referential_constraints\n<span class=\"hljs-keyword\">WHERE<\/span>\n    constraint_schema = <span class=\"hljs-string\">'classicmodels'<\/span>\n        <span class=\"hljs-keyword\">AND<\/span> referenced_table_name = <span class=\"hljs-string\">'buildings'<\/span>\n        <span class=\"hljs-keyword\">AND<\/span> delete_rule = <span class=\"hljs-string\">'CASCADE'<\/span><\/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<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"94\" height=\"39\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-ON-DELETE-CASCADE-tips.png\" alt=\"MySQL ON DELETE CASCADE tips\" class=\"wp-image-6183\"\/><\/figure>\n\n\n\n<p>In this tutorial, you have learned how to use the MySQL <code>ON DELETE CASCADE<\/code> &nbsp;referential action for a foreign key to delete data automatically&nbsp;from the child tables when you delete data from the parent table.<\/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=\"2089\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-on-delete-cascade\/\"\n\t\t\t\tdata-post-title=\"MySQL ON DELETE CASCADE\"\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=\"2089\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-on-delete-cascade\/\"\n\t\t\t\tdata-post-title=\"MySQL ON DELETE CASCADE\"\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>Learn how to use MySQL ON DELETE CASCADE referential action for a foreign key to delete data from a child table automatically when you delete data from a parent table.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":64,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-2089","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 ON DELETE CASCADE: Deleting Data from Related Tables Automatically<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use MySQL ON DELETE CASCADE to automatically delete data from a child table when you delete data from the parent 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.mysqltutorial.org\/mysql-on-delete-cascade\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL ON DELETE CASCADE: Deleting Data from Related Tables Automatically\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use MySQL ON DELETE CASCADE to automatically delete data from a child table when you delete data from the parent table.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-on-delete-cascade\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-11T09:02:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-ON-DELETE-CASCADE-sample-tables.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-on-delete-cascade\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-on-delete-cascade\\\/\",\"name\":\"MySQL ON DELETE CASCADE: Deleting Data from Related Tables Automatically\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-on-delete-cascade\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-on-delete-cascade\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/MySQL-ON-DELETE-CASCADE-sample-tables.png\",\"datePublished\":\"2013-05-07T02:33:03+00:00\",\"dateModified\":\"2020-04-11T09:02:23+00:00\",\"description\":\"This tutorial shows you how to use MySQL ON DELETE CASCADE to automatically delete data from a child table when you delete data from the parent table.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-on-delete-cascade\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-on-delete-cascade\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-on-delete-cascade\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/MySQL-ON-DELETE-CASCADE-sample-tables.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/MySQL-ON-DELETE-CASCADE-sample-tables.png\",\"width\":444,\"height\":104,\"caption\":\"MySQL ON DELETE CASCADE - sample tables\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-on-delete-cascade\\\/#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 Basics\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"MySQL ON DELETE CASCADE\"}]},{\"@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 ON DELETE CASCADE: Deleting Data from Related Tables Automatically","description":"This tutorial shows you how to use MySQL ON DELETE CASCADE to automatically delete data from a child table when you delete data from the parent 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.mysqltutorial.org\/mysql-on-delete-cascade\/","og_locale":"en_US","og_type":"article","og_title":"MySQL ON DELETE CASCADE: Deleting Data from Related Tables Automatically","og_description":"This tutorial shows you how to use MySQL ON DELETE CASCADE to automatically delete data from a child table when you delete data from the parent table.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-on-delete-cascade\/","og_site_name":"MySQL Tutorial","article_modified_time":"2020-04-11T09:02:23+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-ON-DELETE-CASCADE-sample-tables.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-on-delete-cascade\/","url":"https:\/\/www.mysqltutorial.org\/mysql-on-delete-cascade\/","name":"MySQL ON DELETE CASCADE: Deleting Data from Related Tables Automatically","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-on-delete-cascade\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-on-delete-cascade\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-ON-DELETE-CASCADE-sample-tables.png","datePublished":"2013-05-07T02:33:03+00:00","dateModified":"2020-04-11T09:02:23+00:00","description":"This tutorial shows you how to use MySQL ON DELETE CASCADE to automatically delete data from a child table when you delete data from the parent table.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-on-delete-cascade\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-on-delete-cascade\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-on-delete-cascade\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-ON-DELETE-CASCADE-sample-tables.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-ON-DELETE-CASCADE-sample-tables.png","width":444,"height":104,"caption":"MySQL ON DELETE CASCADE - sample tables"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-on-delete-cascade\/#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 Basics","item":"https:\/\/www.mysqltutorial.org\/mysql-basics\/"},{"@type":"ListItem","position":4,"name":"MySQL ON DELETE CASCADE"}]},{"@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\/2089","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=2089"}],"version-history":[{"count":0,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2089\/revisions"}],"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=2089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}