{"id":259,"date":"2026-01-23T04:56:03","date_gmt":"2026-01-23T04:56:03","guid":{"rendered":"https:\/\/www.kwwd.co.uk\/blog\/?p=259"},"modified":"2026-01-23T04:56:18","modified_gmt":"2026-01-23T04:56:18","slug":"on-delete-cascade-remove-child-records-when-parent-records-deleted","status":"publish","type":"post","link":"https:\/\/www.kwwd.co.uk\/blog\/on-delete-cascade-remove-child-records-when-parent-records-deleted\/","title":{"rendered":"ON DELETE Cascade (Remove child records when parent records deleted)"},"content":{"rendered":"<p>If you haven&#8217;t created the child table you can run the following SQL command:<\/p>\n<pre><code class=\"language-sql\">\r\nCREATE TABLE ChildTable (\r\n    ChildID INT PRIMARY KEY AUTO_INCREMENT,\r\n    ParentID INT,\r\n    ChildFields VARCHAR(255),\r\n    -- Add fields as required\r\n    -- Define the foreign key and the cascade rule\r\n    FOREIGN KEY (ParentID) \r\n        REFERENCES ParentTable(ParentID) \r\n        ON DELETE CASCADE\r\n);\r\n<\/code><\/pre>\n<p>If you have already created the Child Table you can run an alter statement:<\/p>\n<pre><code class=\"language-sql\">\r\nALTER TABLE ChildTable\r\nADD CONSTRAINT fk_ParentTable_link\r\nFOREIGN KEY (ParentID) \r\nREFERENCES ParentTable(ParentID) \r\nON DELETE CASCADE;\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you haven&#8217;t created the child table you can run the following SQL command: CREATE TABLE ChildTable ( ChildID INT PRIMARY KEY AUTO_INCREMENT, ParentID INT, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[31,24],"class_list":["post-259","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-database","tag-mysql"],"_links":{"self":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/259","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=259"}],"version-history":[{"count":0,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/259\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}