{"id":5697,"date":"2024-11-14T09:55:15","date_gmt":"2024-11-14T01:55:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5697\/"},"modified":"2024-11-14T09:55:15","modified_gmt":"2024-11-14T01:55:15","slug":"sql%e5%a4%96%e9%94%ae%e6%80%8e%e4%b9%88%e5%88%a0%e6%8e%89","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5697\/","title":{"rendered":"sql\u5916\u952e\u600e\u4e48\u5220\u6389"},"content":{"rendered":"<blockquote><p>\n  sql\u4e2d\u5220\u9664\u5916\u952e\u7684\u6b65\u9aa4\uff1a\u786e\u5b9a\u8981\u5220\u9664\u7684\u5916\u952e\u540d\u79f0\u3002\u4f7f\u7528alter table\u8bed\u53e5\uff0c\u540e\u8ddfdrop constraint\u5b50\u53e5\u5220\u9664\u5916\u952e\u3002\u4f7f\u7528information_schema.key_column_usage\u89c6\u56fe\u67e5\u8be2\u4ee5\u9a8c\u8bc1\u5220\u9664\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053020541777378.jpg\" class=\"aligncenter\" title=\"sql\u5916\u952e\u600e\u4e48\u5220\u6389\u63d2\u56fe\" alt=\"sql\u5916\u952e\u600e\u4e48\u5220\u6389\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5220\u9664 SQL \u4e2d\u7684\u5916\u952e<\/strong><\/p>\n<p>\u5f53\u60a8\u4e0d\u518d\u9700\u8981\u5916\u952e\uff08\u7528\u4e8e\u5728\u4e0d\u540c\u8868\u4e4b\u95f4\u5efa\u7acb\u5173\u7cfb\uff09\u65f6\uff0c\u53ef\u4ee5\u5c06\u5176\u5220\u9664\u3002\u4ee5\u4e0b\u662f\u5220\u9664 SQL \u4e2d\u5916\u952e\u7684\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u786e\u5b9a\u8981\u5220\u9664\u7684\u5916\u952e<\/strong><\/p>\n<ul>\n<li>\u6b64\u4fe1\u606f\u53ef\u4ee5\u5728\u8868\u7684 INFORMATION_SCHEMA.KEY_COLUMN_USAGE \u89c6\u56fe\u4e2d\u627e\u5230\u3002<\/li>\n<li>\u67e5\u627e\u5e26\u6709 CONSTRAINT_NAME \u5217\u7684\u5916\u952e\u540d\u79f0\u3002<\/li>\n<\/ul>\n<p><strong>2. \u4f7f\u7528 ALTER TABLE \u8bed\u53e5<\/strong><\/p>\n<p>\u8981\u5220\u9664\u5916\u952e\uff0c\u8bf7\u4f7f\u7528 ALTER TABLE \u8bed\u53e5\uff0c\u540e\u8ddf DROP CONSTRAINT \u5b50\u53e5\uff1a<\/p>\n<pre>ALTER TABLE [table_name]\nDROP CONSTRAINT [foreign_key_name];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u540d\u4e3a Orders \u7684\u8868\uff0c\u5b83\u4e0e Customers \u8868\u7684\u5916\u952e\u76f8\u5173\u8054\uff0c\u540d\u4e3a FK_Order_Customer\uff1a<\/p>\n<pre>ALTER TABLE Orders\nDROP CONSTRAINT FK_Order_Customer;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u67e5\u8be2\u4ee5\u9a8c\u8bc1\u5220\u9664<\/strong><\/p>\n<p>\u8981\u9a8c\u8bc1\u5916\u952e\u662f\u5426\u5df2\u5220\u9664\uff0c\u8bf7\u4f7f\u7528 INFORMATION_SCHEMA.KEY_COLUMN_USAGE \u89c6\u56fe\u518d\u6b21\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT *\nFROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE\nWHERE TABLE_NAME = '[table_name]'\nAND CONSTRAINT_NAME = '[foreign_key_name]';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c\u67e5\u8be2\u8fd4\u56de\u7a7a\u7ed3\u679c\uff0c\u5219\u5916\u952e\u5df2\u6210\u529f\u5220\u9664\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u5916\u952e\u600e\u4e48\u5220\u6389\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>sql\u4e2d\u5220\u9664\u5916\u952e\u7684\u6b65\u9aa4\uff1a\u786e\u5b9a\u8981\u5220\u9664\u7684\u5916\u952e\u540d\u79f0\u3002\u4f7f\u7528alter table\u8bed\u53e5\uff0c\u540e\u8ddfdrop constraint\u5b50\u53e5\u5220\u9664\u5916\u952e\u3002\u4f7f\u7528information_schema.key_column_usage\u89c6\u56fe\u67e5\u8be2\u4ee5\u9a8c\u8bc1\u5220\u9664\u3002 \u5982\u4f55\u5220\u9664 SQL \u4e2d\u7684\u5916\u952e \u5f53\u60a8\u4e0d\u518d\u9700\u8981\u5916\u952e\uff08\u7528\u4e8e\u5728\u4e0d\u540c\u8868\u4e4b\u95f4\u5efa\u7acb\u5173\u7cfb\uff09\u65f6\uff0c\u53ef\u4ee5\u5c06\u5176\u5220\u9664\u3002\u4ee5\u4e0b\u662f\u5220\u9664 SQL \u4e2d\u5916\u952e\u7684\u6b65\u9aa4\uff1a 1. \u786e\u5b9a\u8981\u5220\u9664\u7684\u5916\u952e \u6b64\u4fe1\u606f\u53ef\u4ee5\u5728\u8868\u7684 INFORMATION_SCHEMA.KEY_COLUMN_USAGE \u89c6\u56fe\u4e2d\u627e\u5230\u3002 \u67e5\u627e\u5e26\u6709 CONSTRAINT_NAME \u5217\u7684\u5916\u952e\u540d\u79f0\u3002 2. \u4f7f\u7528 ALTER TABLE \u8bed\u53e5 \u8981\u5220\u9664\u5916\u952e\uff0c\u8bf7\u4f7f\u7528 ALTER TABLE \u8bed\u53e5\uff0c\u540e\u8ddf DROP CONSTRAINT \u5b50\u53e5\uff1a ALTER TABLE [table_name] DROP CONSTRAINT [foreign_key_name]; \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a \u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u540d\u4e3a Orders \u7684\u8868\uff0c\u5b83\u4e0e Customers \u8868\u7684\u5916\u952e\u76f8\u5173\u8054\uff0c\u540d\u4e3a FK_Order_Customer\uff1a ALTER TABLE Orders DROP CONSTRAINT FK_Order_Customer; \u767b\u5f55\u540e\u590d\u5236 3. \u67e5\u8be2\u4ee5\u9a8c\u8bc1\u5220\u9664 \u8981\u9a8c\u8bc1\u5916\u952e\u662f\u5426\u5df2\u5220\u9664\uff0c\u8bf7\u4f7f\u7528 INFORMATION_SCHEMA.KEY_COLUMN_USAGE \u89c6\u56fe\u518d\u6b21\u67e5\u8be2\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5697","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5697"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5697\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}