{"id":5358,"date":"2020-07-29T23:07:08","date_gmt":"2020-07-29T15:07:08","guid":{"rendered":"http:\/\/39.97.231.10\/?p=5358"},"modified":"2020-07-29T23:07:08","modified_gmt":"2020-07-29T15:07:08","slug":"mysql%e5%a4%96%e9%94%ae%e7%ba%a6%e6%9d%9f%e3%80%81%e7%ba%a7%e8%81%94%e6%9b%b4%e6%96%b0%e3%80%81%e7%ba%a7%e8%81%94%e5%88%a0%e9%99%a4","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=5358","title":{"rendered":"MySQL\u5916\u952e\u7ea6\u675f\u3001\u7ea7\u8054\u66f4\u65b0\u3001\u7ea7\u8054\u5220\u9664"},"content":{"rendered":"<div>\n<div>\n<div>\nMySQL\u652f\u6301\u5916\u952e(foreign key)\uff0c\u5916\u952e\u5141\u8bb8\u8de8\u8868\u4ea4\u53c9\u5f15\u7528\u76f8\u5173\u6570\u636e\uff0c\u53e6\u5916\u5916\u952e\u7ea6\u675f\u80fd\u591f\u4fdd\u8bc1\u76f8\u5173\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002\u4e00\u4e2a\u5916\u952e\u5173\u7cfb\u901a\u5e38\u5305\u62ec\u4e00\u4e2a\u7236\u8868\u548c\u4e00\u4e2a\u5b50\u8868\u3002\u7236\u8868\u5305\u542b\u539f\u59cb\u7684\u5b57\u6bb5\u6570\u636e\uff0c\u5b50\u8868\u5f15\u7528\u7236\u8868\u4e2d\u8be5\u5b57\u6bb5\u7684\u6570\u636e\uff0c\u5916\u952e\u7ea6\u675f\u5b9a\u4e49\u5728\u5b50\u8868\u4e0a\u3002<\/p>\n<h4>1. \u5916\u952e\u5b9a\u4e49\u7684\u8bed\u6cd5<\/h4>\n<p>\u901a\u5e38\u5728create table\u548calter table\u8bed\u53e5\u4e2d\u5b9a\u4e49\u5916\u952e\u7ea6\u675f\uff0c\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>[CONSTRAINT [symbol]] FOREIGN KEY\n    [index_name] (col_name, ...)\n    REFERENCES tbl_name (col_name,...)\n    [ON DELETE reference_option]\n    [ON UPDATE reference_option]\nreference_option:\n    RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT\n<\/pre>\n<p>CONSTRAINT\u00a0symbol \u4e3a\u5916\u952e\u7ea6\u675f\u540d\u79f0\uff0c\u5982\u679c\u6ca1\u6709\u5b9a\u4e49\uff0cInnoDB\u8868\u4f1a\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u540d\u79f0\uff0c\u8fd9\u4e2a\u540d\u79f0\u5728\u6570\u636e\u5e93\u91cc\u9762\u5fc5\u987b\u552f\u4e00\u3002<br \/>\nON DELETE\u3001ON UPDATE\u8868\u793a\u4e8b\u4ef6\u89e6\u53d1\u65f6\u7684\u52a8\u4f5c\uff0c\u53ef\u8bbe\u7f6e\u503c\u4e3a\uff1a<\/p>\n<ul>\n<li>CASCADE\uff0c\u66f4\u65b0\u6216\u8005\u5220\u9664\u7236\u8868\u8bb0\u5f55\u65f6\uff0c\u5b50\u8868\u4e2d\u7684\u76f8\u5e94\u8bb0\u5f55\u540c\u6b65\u66f4\u65b0\u6216\u8005\u5220\u9664\u3002\u7531\u7ea7\u8054\u66f4\u65b0\u6216\u8005\u5220\u9664\u5bfc\u81f4\u7684\u5b50\u8868\u8bb0\u5f55\u4fee\u6539\uff0c\u4e0d\u4f1a\u89e6\u53d1\u5b50\u8868\u7684\u89e6\u53d1\u5668\u6267\u884c\u3002<\/li>\n<li>SET NULL\uff0c\u66f4\u65b0\u6216\u8005\u5220\u9664\u7236\u8868\u8bb0\u5f55\u65f6\uff0c\u5b50\u8868\u4e2d\u7684\u76f8\u5e94\u8bb0\u5f55\u5b57\u6bb5\u8bbe\u7f6e\u4e3aNULL\uff0c\u524d\u63d0\u662f\u5b50\u8868\u4e2d\u76f8\u5e94\u5b57\u6bb5\u4e0d\u80fd\u5b9a\u4e49\u4e3aNOT NULL\u3002<\/li>\n<li>RESTRICT\uff0c\u6709\u5916\u952e\u5173\u7cfb\u7ea6\u675f\u65f6\uff0c\u62d2\u7edd\u7236\u8868\u8bb0\u5f55\u7684\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\u3002<\/li>\n<li>NO ACTION\uff0c\u65e0\u52a8\u4f5c\uff0c\u5b9e\u9645\u529f\u80fd\u4e0eRESTRICT\u76f8\u540c\u3002<\/li>\n<li>SET DEFAULT\uff0c\u4ec5\u4ec5\u89e3\u6790\u5668\u80fd\u8bc6\u522b\uff0c\u5b9e\u9645\u529f\u80fd\u672a\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n<h4>2. \u7ea7\u8054\u66f4\u65b0\u548c\u7ea7\u8054\u5220\u9664<\/h4>\n<p>\u5f53\u7236\u8868\u5bf9\u5916\u952e\u5173\u8054\u7684\u5b57\u6bb5\u8fdb\u884cupdate\u3001delete\u64cd\u4f5c\u65f6\uff0c\u5b50\u8868\u5bf9\u5e94\u7684\u5b57\u6bb5\u503c\u4e5f\u4f1a\u540c\u6b65\u53d8\u5316\uff0c\u79f0\u4e4b\u4e3a\u7ea7\u8054\u66f4\u65b0\u6216\u8005\u7ea7\u8054\u5220\u9664\u3002\u5f53\u5916\u952e\u5b9a\u4e49\u65f6\uff0c\u8bbe\u7f6e referential action \u4e3aCASCADE\u5373\u53ef\u5b9e\u73b0\u7ea7\u8054\u66f4\u65b0\u4e0e\u5220\u9664\u3002<br \/>\n\u793a\u4f8b\uff1a<br \/>\nforeign key(parent_id) references parent(id) on update cascade<br \/>\nforeign key(parent_id) references parent(id) on delete cascade<br \/>\nforeign key(parent_id) references parent(id) on update cascade on delete cascade<\/p>\n<h4>3. \u5916\u952e\u7ea6\u675f\u6848\u4f8b<\/h4>\n<h5>3.1 \u5916\u952e\u7ea6\u675f\u5b57\u6bb5\u503c<\/h5>\n<pre># \u521b\u5efa\u7236\u8868\ncreate table parent(id int, primary key(id));\n# \u521b\u5efa\u5b50\u8868\ncreate table child(id int, parent_id int,\nforeign key(parent_id) references parent(id),\nprimary key(id));\n<\/pre>\n<p>\u5b50\u8868child\u4f1a\u81ea\u52a8\u5728parent_id\u5b57\u6bb5\u4e0a\u52a0\u4e0a\u7d22\u5f15\u3002\u5f80\u5b50\u8868\u63d2\u5165\u6570\u636e\u6216\u8005\u66f4\u65b0\u6570\u636e\u65f6\uff0cparent_id\u5b57\u6bb5\u503c\u5fc5\u987b\u4e3a\u7236\u8868parent\u4e2d\u5df2\u7ecf\u5b58\u5728\u7684\u503c\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002\u5982\u4e0b\uff1a<\/p>\n<pre>Cannot add or update a child row: a foreign key\nconstraint fails (`db`.`child`, CONSTRAINT `child_ibfk_1`\nFOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`))\n<\/pre>\n<p>\u5220\u9664\u7236\u8868\u4e2d\u6709\u5916\u952e\u7ea6\u675f\u7684\u8bb0\u5f55\u65f6\uff0c\u4e5f\u4f1a\u62a5\u9519\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>\u00a0Cannot delete or update a parent row: a foreign key\nconstraint fails (`db`.`child`, CONSTRAINT `child_ibfk_1`\nFOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`))\n<\/pre>\n<h5>3.2 \u5916\u952e\u7ea7\u8054\u66f4\u65b0<\/h5>\n<pre># \u521b\u5efa\u7236\u8868\ncreate table parent(id int, primary key(id));\n# \u521b\u5efa\u5b50\u8868\ncreate table child(id int, parent_id int,\nforeign key(parent_id) references parent(id)\non update cascade,\nprimary key(id));\n<\/pre>\n<p>\u66f4\u65b0\u7236\u8868parent\u7684id\u5b57\u6bb5\u503c\uff0c\u90a3\u4e48\u5b50\u8868\u5b57\u6bb5parent_id\u4e2d\u76f8\u5e94\u7684\u503c\u4e5f\u4f1a\u540c\u6b65\u66f4\u65b0\u3002<\/p>\n<h5>3.3 \u5916\u952e\u7ea7\u8054\u5220\u9664<\/h5>\n<pre># \u521b\u5efa\u7236\u8868\ncreate table parent(id int, primary key(id));\n# \u521b\u5efa\u5b50\u8868\ncreate table child(id int, parent_id int,\nforeign key(parent_id) references parent(id)\non delete cascade,\nprimary key(id));\n<\/pre>\n<p>\u5220\u9664\u7236\u8868parent\u7684\u67d0\u4e2aid\u5b57\u6bb5\u503c\u7684\u8bb0\u5f55\uff0c\u90a3\u4e48\u5b50\u8868\u5b57\u6bb5parent_id\u4e2d\u76f8\u5e94\u503c\u7684\u8bb0\u5f55\u4e5f\u4f1a\u540c\u6b65\u5220\u9664\u3002<\/p>\n<h5>3.4 \u5220\u9664\u5916\u952e<\/h5>\n<p>alter table child drop foreign key child_ibfk_1;<\/p>\n<h5>3.5 \u521b\u5efa\u5916\u952e<\/h5>\n<p>alter table child add foreign key(parent_id) references parent(id);<\/p>\n<h4>4. \u5916\u952e\u7ea6\u675f\u7684\u6761\u4ef6\u548c\u9650\u5236<\/h4>\n<ol>\n<li>\u7236\u8868\u548c\u5b50\u8868\u5fc5\u987b\u4e3a\u540c\u4e00\u79cd\u5b58\u50a8\u5f15\u64ce\uff0c\u4e14\u4e0d\u80fd\u5b9a\u4e49\u4e3a\u4e34\u65f6\u8868\u3002<\/li>\n<li>\u521b\u5efa\u5916\u952e\u7ea6\u675f\u9700\u8981\u6709REFERENCES\u6743\u9650\u3002<\/li>\n<li>\u5916\u952e\u7ea6\u675f\u4e2d\u4f7f\u7528\u7684\u5b57\u6bb5\u5fc5\u987b\u6709\u76f8\u4f3c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6574\u578b\u7684\u5927\u5c0f\u5fc5\u987b\u76f8\u540c\uff0c\u6709\u65e0\u7b26\u53f7\u4e5f\u5fc5\u987b\u76f8\u540c\u3002\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u957f\u5ea6\u53ef\u4ee5\u4e0d\u76f8\u540c\uff0c\u4f46\u662f\u5b57\u7b26\u96c6\u548c\u6821\u9a8c\u89c4\u5219\u5fc5\u987b\u76f8\u540c\u3002<\/li>\n<li>\u5916\u952e\u7ea6\u675f\u7684\u4e24\u4e2a\u5b57\u6bb5\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u8868\u4e2d\uff0c\u4f46\u662f\u4e0d\u80fd\u662f\u540c\u4e00\u4e2a\u5b57\u6bb5\u3002<\/li>\n<li>\u5916\u952e\u7ea6\u675f\u7684\u5b57\u6bb5\u4e0a\u5fc5\u987b\u8981\u6709\u7d22\u5f15\uff0c\u4ee5\u4fbf\u52a0\u5feb\u5916\u952e\u68c0\u67e5\uff0c\u5982\u679c\u5f15\u7528\u8868\u4e0a\u7684\u5916\u952e\u5b57\u6bb5\u6ca1\u6709\u7d22\u5f15\u7684\u8bdd\uff0cMySQL\u4f1a\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u7d22\u5f15\u3002<\/li>\n<li>\u4e0d\u652f\u6301\u5916\u952e\u5b57\u6bb5\u4e0a\u7684\u524d\u7f00\u7d22\u5f15\uff0cblob\u548ctext\u5b57\u6bb5\u4e0d\u80fd\u7528\u4e8e\u5916\u952e\u3002<\/li>\n<li>InnoDB\u8868\u5916\u952e\u7ea6\u675f\u4e0d\u652f\u6301\u5206\u533a\u3002<\/li>\n<li>\u4e00\u4e2a\u6709\u5916\u952e\u7ea6\u675f\u7684\u8868\u4e0d\u80fd\u4fee\u6539\u5176\u5b58\u50a8\u5f15\u64ce\u3002<\/li>\n<li>\u5916\u952e\u7ea6\u675f\u4e0d\u80fd\u5f15\u7528\u865a\u62df\u5b57\u6bb5\u3002<\/li>\n<\/ol>\n<h4>5. \u7981\u7528\u5916\u952e\u68c0\u67e5<\/h4>\n<p>MySQL\u901a\u8fc7\u53d8\u91cf foreign_key_checks\u00a0\u6765\u5f00\u542f\u6216\u8005\u7981\u7528\u5916\u952e\u68c0\u67e5\uff0c\u8fd9\u4e2a\u53d8\u91cf\u4f5c\u7528\u8303\u56f4\u53ef\u4ee5\u662f\u5168\u5c40\uff0c\u4e5f\u53ef\u4ee5\u662f\u4f1a\u8bdd\u7ea7\u522b\u3002\u6709\u4e9b\u573a\u666f\uff0c\u7981\u7528\u5916\u952e\u68c0\u67e5\u662f\u975e\u5e38\u6709\u7528\u7684\uff0c\u6bd4\u5982\uff1a<\/p>\n<ol>\n<li>\u5220\u9664\u4e00\u4e2a\u6709\u5916\u952e\u7ea6\u675f\u5f15\u7528\u7684\u8868\u3002<\/li>\n<li>\u4ee5\u4e0d\u540c\u7684\u987a\u5e8f\u91cd\u65b0\u52a0\u8f7d\u8868\uff0c\u6bd4\u5982\u4f7f\u7528mysqldump\u5bfc\u51fa\u7684\u8868\uff0c\u987a\u5e8f\u4e0e\u5916\u952e\u7ea6\u675f\u7684\u4f9d\u8d56\u987a\u5e8f\u4e0d\u4e00\u81f4\u65f6\uff0c\u5148\u7981\u7528\u5916\u952e\u68c0\u67e5\uff0c\u7b49\u5f85\u8868\u5bfc\u5165\u5b8c\u6210\u540e\uff0c\u518d\u6253\u5f00\u5916\u952e\u68c0\u67e5\u3002<\/li>\n<li>\u6267\u884cload data\u64cd\u4f5c\u65f6\uff0c\u5173\u95ed\u5916\u952e\u68c0\u67e5\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li>\u5728\u4e00\u4e2a\u6709\u5916\u952e\u7ea6\u675f\u7684\u8868\u4e0a\u6267\u884calter table\u6539\u8868\u3002<\/li>\n<li>\u5220\u9664\u4e00\u4e2a\u5916\u952e\u7ea6\u675f\u4f9d\u8d56\u7684\u7d22\u5f15\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u5916\u952e\u7ea6\u675f\uff0c\u5f15\u7528\u7684\u8868\u5b57\u6bb5\u6682\u65f6\u8fd8\u4e0d\u5b58\u5728\u3002<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u652f\u6301\u5916\u952e(foreign key)\uff0c\u5916\u952e\u5141\u8bb8\u8de8\u8868\u4ea4\u53c9\u5f15\u7528\u76f8\u5173\u6570\u636e\uff0c\u53e6\u5916\u5916\u952e\u7ea6\u675f\u80fd\u591f\u4fdd\u8bc1\u76f8\u5173\u6570\u636e\u7684\u4e00\u81f4 &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=5358\"> <span class=\"screen-reader-text\">MySQL\u5916\u952e\u7ea6\u675f\u3001\u7ea7\u8054\u66f4\u65b0\u3001\u7ea7\u8054\u5220\u9664<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[90,88],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"MySQL\u652f\u6301\u5916\u952e(foreign key)\uff0c\u5916\u952e\u5141\u8bb8\u8de8\u8868\u4ea4\u53c9\u5f15\u7528\u76f8\u5173\u6570\u636e\uff0c\u53e6\u5916\u5916\u952e\u7ea6\u675f\u80fd\u591f\u4fdd\u8bc1\u76f8\u5173\u6570\u636e\u7684\u4e00\u81f4&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/5358"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5358"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/5358\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5358"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}