{"id":192592,"date":"2024-11-12T19:24:00","date_gmt":"2024-11-12T11:24:00","guid":{"rendered":"https:\/\/server.hk\/cnblog\/192592\/"},"modified":"2024-11-12T19:24:01","modified_gmt":"2024-11-12T11:24:01","slug":"%e6%96%b0%e6%89%8b%e5%ad%b8%e7%bf%92mysql%e5%a4%96%e9%8d%b5%e6%89%80%e8%a6%81%e7%9f%a5%e9%81%93%e7%9a%84%e7%9f%a5%e8%ad%98","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/192592\/","title":{"rendered":"\u65b0\u624b\u5b78\u7fd2MySQL\u5916\u9375\u6240\u8981\u77e5\u9053\u7684\u77e5\u8b58"},"content":{"rendered":"<h1 id=\"%e6%96%b0%e6%89%8b%e5%ad%b8%e7%bf%92mysql%e5%a4%96%e9%8d%b5%e6%89%80%e8%a6%81%e7%9f%a5%e9%81%93%e7%9a%84%e7%9f%a5%e8%ad%98-HhCINUtlVH\">\u65b0\u624b\u5b78\u7fd2MySQL\u5916\u9375\u6240\u8981\u77e5\u9053\u7684\u77e5\u8b58<\/h1>\n<p>\u5728\u8cc7\u6599\u5eab\u8a2d\u8a08\u4e2d\uff0c\u5916\u9375\u662f\u4e00\u500b\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u7279\u5225\u662f\u5728\u4f7f\u7528MySQL\u9019\u985e\u95dc\u806f\u578b\u8cc7\u6599\u5eab\u6642\u3002\u5916\u9375\u7684\u4e3b\u8981\u529f\u80fd\u662f\u7dad\u8b77\u8cc7\u6599\u7684\u5b8c\u6574\u6027\uff0c\u4e26\u5efa\u7acb\u4e0d\u540c\u8cc7\u6599\u8868\u4e4b\u9593\u7684\u95dc\u806f\u3002\u5c0d\u65bc\u65b0\u624b\u4f86\u8aaa\uff0c\u7406\u89e3\u5916\u9375\u7684\u57fa\u672c\u6982\u5ff5\u53ca\u5176\u4f7f\u7528\u65b9\u6cd5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5916\u9375\u7684\u5b9a\u7fa9\u3001\u4f5c\u7528\u3001\u4f7f\u7528\u65b9\u6cd5\u4ee5\u53ca\u4e00\u4e9b\u5be6\u969b\u7bc4\u4f8b\uff0c\u5e6b\u52a9\u65b0\u624b\u66f4\u597d\u5730\u638c\u63e1\u9019\u4e00\u77e5\u8b58\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e5%a4%96%e9%8d%b5%ef%bc%9f-HhCINUtlVH\">\u4ec0\u9ebc\u662f\u5916\u9375\uff1f<\/h2>\n<p>\u5916\u9375\uff08Foreign Key\uff09\u662f\u6307\u5728\u4e00\u500b\u8cc7\u6599\u8868\u4e2d\uff0c\u67d0\u4e00\u6b04\u4f4d\u7684\u503c\u5fc5\u9808\u4f86\u81ea\u53e6\u4e00\u8cc7\u6599\u8868\u7684\u4e3b\u9375\uff08Primary Key\uff09\u6216\u552f\u4e00\u9375\uff08Unique Key\uff09\u3002\u9019\u7a2e\u95dc\u806f\u6027\u4f7f\u5f97\u8cc7\u6599\u5eab\u80fd\u5920\u4fdd\u6301\u8cc7\u6599\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002\u5916\u9375\u7684\u5b58\u5728\u53ef\u4ee5\u9632\u6b62\u5728\u8cc7\u6599\u8868\u4e2d\u63d2\u5165\u4e0d\u5408\u6cd5\u7684\u6578\u64da\uff0c\u5f9e\u800c\u4fdd\u8b77\u8cc7\u6599\u7684\u6b63\u78ba\u6027\u3002<\/p>\n<h2 id=\"%e5%a4%96%e9%8d%b5%e7%9a%84%e4%bd%9c%e7%94%a8-HhCINUtlVH\">\u5916\u9375\u7684\u4f5c\u7528<\/h2>\n<ul>\n<li><strong>\u7dad\u8b77\u8cc7\u6599\u5b8c\u6574\u6027\uff1a<\/strong>\u5916\u9375\u53ef\u4ee5\u78ba\u4fdd\u8cc7\u6599\u8868\u4e4b\u9593\u7684\u95dc\u806f\u6027\uff0c\u9632\u6b62\u5b64\u7acb\u7684\u6578\u64da\u51fa\u73fe\u3002<\/li>\n<li><strong>\u5be6\u73fe\u8cc7\u6599\u7684\u806f\u7d50\uff1a<\/strong>\u901a\u904e\u5916\u9375\uff0c\u53ef\u4ee5\u8f15\u9b06\u5730\u5f9e\u4e00\u500b\u8cc7\u6599\u8868\u4e2d\u67e5\u8a62\u5230\u8207\u4e4b\u76f8\u95dc\u7684\u8cc7\u6599\u3002<\/li>\n<li><strong>\u652f\u6301\u7d1a\u806f\u64cd\u4f5c\uff1a<\/strong>\u5916\u9375\u53ef\u4ee5\u8a2d\u7f6e\u7d1a\u806f\u522a\u9664\u6216\u66f4\u65b0\uff0c\u9019\u6a23\u5728\u522a\u9664\u6216\u66f4\u65b0\u4e3b\u8868\u4e2d\u7684\u6578\u64da\u6642\uff0c\u76f8\u95dc\u7684\u5f9e\u8868\u6578\u64da\u4e5f\u6703\u81ea\u52d5\u66f4\u65b0\u6216\u522a\u9664\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%9c%a8mysql%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%a4%96%e9%8d%b5-HhCINUtlVH\">\u5982\u4f55\u5728MySQL\u4e2d\u4f7f\u7528\u5916\u9375<\/h2>\n<p>\u5728MySQL\u4e2d\uff0c\u5275\u5efa\u5916\u9375\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>CREATE TABLE \u5b50\u8868 (\n    id INT PRIMARY KEY,\n    \u4e3b\u8868_id INT,\n    FOREIGN KEY (\u4e3b\u8868_id) REFERENCES \u4e3b\u8868(id)\n);<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c<code>\u5b50\u8868<\/code>\u4e2d\u7684<code>\u4e3b\u8868_id<\/code>\u6b04\u4f4d\u662f\u5916\u9375\uff0c\u6307\u5411<code>\u4e3b\u8868<\/code>\u4e2d\u7684<code>id<\/code>\u6b04\u4f4d\u3002\u9019\u6a23\uff0c<code>\u5b50\u8868<\/code>\u4e2d\u7684\u6bcf\u4e00\u500b<code>\u4e3b\u8868_id<\/code>\u503c\u90fd\u5fc5\u9808\u5728<code>\u4e3b\u8868<\/code>\u4e2d\u5b58\u5728\u3002<\/p>\n<h3 id=\"%e5%a4%96%e9%8d%b5%e7%9a%84%e7%b4%84%e6%9d%9f%e6%a2%9d%e4%bb%b6-HhCINUtlVH\">\u5916\u9375\u7684\u7d04\u675f\u689d\u4ef6<\/h3>\n<p>\u5728\u5b9a\u7fa9\u5916\u9375\u6642\uff0c\u53ef\u4ee5\u8a2d\u7f6e\u4e00\u4e9b\u7d04\u675f\u689d\u4ef6\uff0c\u4ee5\u63a7\u5236\u8cc7\u6599\u7684\u884c\u70ba\u3002\u5e38\u898b\u7684\u7d04\u675f\u689d\u4ef6\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>ON DELETE CASCADE\uff1a<\/strong>\u7576\u4e3b\u8868\u4e2d\u7684\u4e00\u689d\u8a18\u9304\u88ab\u522a\u9664\u6642\uff0c\u6240\u6709\u5f15\u7528\u8a72\u8a18\u9304\u7684\u5b50\u8868\u8a18\u9304\u4e5f\u6703\u88ab\u81ea\u52d5\u522a\u9664\u3002<\/li>\n<li><strong>ON UPDATE CASCADE\uff1a<\/strong>\u7576\u4e3b\u8868\u4e2d\u7684\u4e00\u689d\u8a18\u9304\u88ab\u66f4\u65b0\u6642\uff0c\u6240\u6709\u5f15\u7528\u8a72\u8a18\u9304\u7684\u5b50\u8868\u8a18\u9304\u4e5f\u6703\u81ea\u52d5\u66f4\u65b0\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code>CREATE TABLE \u5b50\u8868 (\n    id INT PRIMARY KEY,\n    \u4e3b\u8868_id INT,\n    FOREIGN KEY (\u4e3b\u8868_id) REFERENCES \u4e3b\u8868(id) ON DELETE CASCADE ON UPDATE CASCADE\n);<\/code><\/pre>\n<h2 id=\"%e5%af%a6%e9%9a%9b%e7%af%84%e4%be%8b-HhCINUtlVH\">\u5be6\u969b\u7bc4\u4f8b<\/h2>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u5169\u500b\u8cc7\u6599\u8868\uff1a<code>users<\/code>\u548c<code>orders<\/code>\u3002<code>users<\/code>\u8868\u5b58\u5132\u7528\u6236\u4fe1\u606f\uff0c\u800c<code>orders<\/code>\u8868\u5b58\u5132\u8a02\u55ae\u4fe1\u606f\u3002\u6bcf\u500b\u8a02\u55ae\u90fd\u5fc5\u9808\u8207\u4e00\u500b\u7528\u6236\u76f8\u95dc\u806f\uff0c\u56e0\u6b64\u6211\u5011\u53ef\u4ee5\u5728<code>orders<\/code>\u8868\u4e2d\u8a2d\u7f6e\u5916\u9375\uff0c\u6307\u5411<code>users<\/code>\u8868\u7684\u4e3b\u9375\u3002<\/p>\n<pre><code>CREATE TABLE users (\n    id INT PRIMARY KEY,\n    name VARCHAR(100)\n);\n\nCREATE TABLE orders (\n    id INT PRIMARY KEY,\n    user_id INT,\n    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE\n);<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c<code>orders<\/code>\u8868\u4e2d\u7684<code>user_id<\/code>\u6b04\u4f4d\u662f\u5916\u9375\uff0c\u6307\u5411<code>users<\/code>\u8868\u4e2d\u7684<code>id<\/code>\u6b04\u4f4d\u3002\u9019\u6a23\uff0c\u7576\u4e00\u500b\u7528\u6236\u88ab\u522a\u9664\u6642\uff0c\u6240\u6709\u8207\u8a72\u7528\u6236\u76f8\u95dc\u7684\u8a02\u55ae\u4e5f\u6703\u88ab\u81ea\u52d5\u522a\u9664\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-HhCINUtlVH\">\u7e3d\u7d50<\/h2>\n<p>\u5916\u9375\u5728MySQL\u4e2d\u626e\u6f14\u8457\u81f3\u95dc\u91cd\u8981\u7684\u89d2\u8272\uff0c\u5e6b\u52a9\u7dad\u8b77\u8cc7\u6599\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002\u5c0d\u65bc\u65b0\u624b\u4f86\u8aaa\uff0c\u7406\u89e3\u5916\u9375\u7684\u6982\u5ff5\u53ca\u5176\u4f7f\u7528\u65b9\u6cd5\u662f\u5b78\u7fd2\u8cc7\u6599\u5eab\u8a2d\u8a08\u7684\u57fa\u790e\u3002\u900f\u904e\u672c\u6587\u7684\u4ecb\u7d39\uff0c\u5e0c\u671b\u80fd\u5920\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u638c\u63e1MySQL\u5916\u9375\u7684\u77e5\u8b58\uff0c\u4e26\u5728\u5be6\u969b\u61c9\u7528\u4e2d\u9748\u6d3b\u904b\u7528\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u8cc7\u6599\u5eab\u7ba1\u7406\u6216\u5176\u4ed6\u6280\u8853\u554f\u984c\u6709\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4e86\u89e3\u66f4\u591a\u8cc7\u8a0a\uff0c\u7279\u5225\u662f\u6211\u5011\u63d0\u4f9b\u7684<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u548c<a href=\"https:\/\/server.hk\">\u96f2\u4f3a\u670d\u5668<\/a>\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u65b0\u624b\u5b78\u7fd2MySQL\u5916\u9375\u7684\u57fa\u672c\u77e5\u8b58\uff0c\u5305\u62ec\u5b9a\u7fa9\u3001\u4f7f\u7528\u65b9\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u52a9\u4f60\u5efa\u7acb\u7a69\u5b9a\u7684\u6578\u64da\u5eab\u95dc\u806f\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-192592","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/192592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=192592"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/192592\/revisions"}],"predecessor-version":[{"id":192593,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/192592\/revisions\/192593"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=192592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=192592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=192592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}