{"id":502,"date":"2011-08-08T20:12:56","date_gmt":"2011-08-08T20:12:56","guid":{"rendered":"http:\/\/nestor.nestasoft.fr\/?p=502"},"modified":"2011-08-13T13:01:19","modified_gmt":"2011-08-13T13:01:19","slug":"astuce-sql-on-delete-cascade","status":"publish","type":"post","link":"https:\/\/nestasoft.fr\/?p=502","title":{"rendered":"Astuce SQL : ON DELETE CASCADE"},"content":{"rendered":"<p><img decoding=\"async\" class=\"alignleft\" src=\"http:\/\/www.nestor.nestasoft.fr\/wp-content\/uploads\/2011\/08\/sql2008.jpg\" alt=\"SQL\" width=\"25%\" height=\"15%\" \/><br \/>\nD\u00e9couvrons ensemble comment supprimer les donn\u00e9es dans une table en <b>cascade<\/b><\/p>\n<p>Soit le mod\u00e8le de donn\u00e9es suivant:<\/p>\n<p><img decoding=\"async\" class=\"alignleft\" src=\"http:\/\/www.nestor.nestasoft.fr\/wp-content\/uploads\/2011\/08\/visibiliteClient.bmp\" alt=\"SQL\"  width=\"100%\" \/><br \/>\n<!--more--><\/p>\n<p>Si la clause <b>On Delete Cascade<\/b> n&rsquo;a pas \u00e9t\u00e9 sp\u00e9cifi\u00e9 lors de la cr\u00e9ation de la r\u00e9f\u00e9rence dans la table <b>Visibilite<\/b>, lors de la suppression d&rsquo;un client, vous aurez erreur semblable \u00e0 celle-ci :<\/p>\n<pre class=\"brush:csharp\">\r\n        The DELETE statement conflicted with the REFERENCE constraint \"FK_VisibilitePage\". \r\n        The conflict occurred in database \"Tuto\", table \"dbo.Visibilite\", column 'ClientID'\r\n<\/pre>\n<p>Pour r\u00e9soudre ce probl\u00e8me, nous devons:<\/p>\n<p><b>1<\/b> Supprimer la cl\u00e9 r\u00e9f\u00e9rence de Client dans la table Visibilite<\/p>\n<pre class=\"brush:csharp\">\r\n   GO\r\n   ALTER TABLE Visibilite\r\n\tDROP CONSTRAINT FK_VISIBILITE_CLIENT  \r\n<\/pre>\n<p><b>2<\/b> Ajouter la r\u00e9f\u00e9rence (cl\u00e9 primaine ):<\/p>\n<pre class=\"brush:csharp\">\t\r\nALTER TABLE Visibilite\r\n\tADD CONSTRAINT FK_VISIBILITE_CLIENT FOREIGN KEY( ClientID ) \r\nREFERENCES Client ( ClientID) \r\nON DELETE CASCADE;\r\n<\/pre>\n<p><b>R\u00e9sultat<\/b> : Le delete en cascade s&rsquo;est r\u00e9alis\u00e9 avec succ\u00e8s dans les tables concern\u00e9es. <\/p>\n<p><b>Bon \u00e0 savoir<\/b>:<br \/>\nON DELETE CASCADE permet de r\u00e9aliser des suppression en cascade (ON UPDATE CASCADE permet de r\u00e9aliser des mis \u00e0 jour en cascade). <\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_502\" class=\"pvc_stats all  \" data-element-id=\"502\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/nestasoft.fr\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>D\u00e9couvrons ensemble comment supprimer les donn\u00e9es dans une table en cascade Soit le mod\u00e8le de donn\u00e9es suivant:<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_502\" class=\"pvc_stats all  \" data-element-id=\"502\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/nestasoft.fr\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-502","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/posts\/502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=502"}],"version-history":[{"count":30,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/posts\/502\/revisions"}],"predecessor-version":[{"id":539,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/posts\/502\/revisions\/539"}],"wp:attachment":[{"href":"https:\/\/nestasoft.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}