{"id":633,"date":"2023-02-26T11:31:34","date_gmt":"2023-02-26T03:31:34","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=633"},"modified":"2023-04-29T20:14:22","modified_gmt":"2023-04-29T12:14:22","slug":"simple-use-of-mysql-foreign-key","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/simple-use-of-mysql-foreign-key\/","title":{"rendered":"MySQL\u5916\u952e(FOREIGN KEY)\u7684\u7b80\u5355\u4f7f\u7528"},"content":{"rendered":"<h2>\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>1\u3001MySQL\u4e2d\u201c\u952e\u201d\u548c\u201c\u7d22\u5f15\u201d\u7684\u5b9a\u4e49\u76f8\u540c\uff0c\u6240\u4ee5\u5916\u952e\u548c\u4e3b\u952e\u4e00\u6837\u4e5f\u662f\u7d22\u5f15\u7684\u4e00\u79cd\u3002\u4e0d\u540c\u7684\u662fMySQL\u4f1a\u81ea\u52a8\u4e3a\u6240\u6709\u8868\u7684\u4e3b\u952e\u8fdb\u884c\u7d22\u5f15\uff0c\u4f46\u662f\u5916\u952e\u5b57\u6bb5\u5fc5\u987b\u7531\u7528\u6237\u8fdb\u884c\u660e\u786e\u7684\u7d22\u5f15\u3002\u7528\u4e8e\u5916\u952e\u5173\u7cfb\u7684\u5b57\u6bb5\u5fc5\u987b\u5728\u6240\u6709\u7684\u53c2\u7167\u8868\u4e2d\u8fdb\u884c\u660e\u786e\u5730\u7d22\u5f15\uff0cInnoDB\u4e0d\u80fd\u81ea\u52a8\u5730\u521b\u5efa\u7d22\u5f15\u3002<br \/>\n2\u3001\u5916\u952e\u53ef\u4ee5\u662f\u4e00\u5bf9\u4e00\u7684\uff0c\u4e00\u4e2a\u8868\u7684\u8bb0\u5f55\u53ea\u80fd\u4e0e\u53e6\u4e00\u4e2a\u8868\u7684\u4e00\u6761\u8bb0\u5f55\u8fde\u63a5\uff0c\u6216\u8005\u662f\u4e00\u5bf9\u591a\u7684\uff0c\u4e00\u4e2a\u8868\u7684\u8bb0\u5f55\u4e0e\u53e6\u4e00\u4e2a\u8868\u7684\u591a\u6761\u8bb0\u5f55\u8fde\u63a5\u3002<!-- more --><br \/>\n3\u3001\u5982\u679c\u9700\u8981\u66f4\u597d\u7684\u6027\u80fd\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u5b8c\u6574\u6027\u68c0\u67e5\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528MyISAM\u8868\u7c7b\u578b\uff0c\u5982\u679c\u60f3\u8981\u5728MySQL\u4e2d\u6839\u636e\u53c2\u7167\u5b8c\u6574\u6027\u6765\u5efa\u7acb\u8868\u5e76\u4e14\u5e0c\u671b\u5728\u6b64\u57fa\u7840\u4e0a\u4fdd\u6301\u826f\u597d\u7684\u6027\u80fd\uff0c\u6700\u597d\u9009\u62e9\u8868\u7ed3\u6784\u4e3ainnoDB\u7c7b\u578b\u3002<\/p>\n<p>\u5916\u952e\u7684\u4f7f\u7528\u6761\u4ef6<\/p>\n<ul>\n<li>\u2460 \u4e24\u4e2a\u8868\u5fc5\u987b\u662fInnoDB\u8868\uff0cMyISAM\u8868\u6682\u65f6\u4e0d\u652f\u6301\u5916\u952e<\/li>\n<li>\u2461 \u5916\u952e\u5217\u5fc5\u987b\u5efa\u7acb\u4e86\u7d22\u5f15\uff0cMySQL 4.1.2\u4ee5\u540e\u7684\u7248\u672c\u5728\u5efa\u7acb\u5916\u952e\u65f6\u4f1a\u81ea\u52a8\u521b\u5efa\u7d22\u5f15\uff0c\u4f46\u5982\u679c\u5728\u8f83\u65e9\u7684\u7248\u672c\u5219\u9700\u8981\u663e\u5f0f\u5efa\u7acb\uff1b<\/li>\n<li>\u2462 \u5916\u952e\u5173\u7cfb\u7684\u4e24\u4e2a\u8868\u7684\u5217\u5fc5\u987b\u662f\u6570\u636e\u7c7b\u578b\u76f8\u4f3c\uff0c\u4e5f\u5c31\u662f\u53ef\u4ee5\u76f8\u4e92\u8f6c\u6362\u7c7b\u578b\u7684\u5217\uff0c\u6bd4\u5982int\u548ctinyint\u53ef\u4ee5\uff0c\u800cint\u548cchar\u5219\u4e0d\u53ef\u4ee5\uff1b<\/li>\n<\/ul>\n<p>\u5916\u952e\u7684\u597d\u5904\uff1a\u53ef\u4ee5\u4f7f\u5f97\u4e24\u5f20\u8868\u5173\u8054\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u5b9e\u73b0\u4e00\u4e9b\u7ea7\u8054\u64cd\u4f5c\u3002<\/p>\n<h2>\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<h3>1\u3001\u521b\u5efa\u5916\u952e\u7684\u8bed\u6cd5\uff1a<\/h3>\n<p>\u5916\u952e\u7684\u5b9a\u4e49\u8bed\u6cd5\uff1a<\/p>\n<pre><code class=\"language-sql\">[CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, \u2026)\n    REFERENCES tbl_name (index_col_name, \u2026)\n    [ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT}]\n    [ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT}]<\/code><\/pre>\n<p>\u8be5\u8bed\u6cd5\u53ef\u4ee5\u5728 CREATE TABLE \u548c ALTER TABLE \u65f6\u4f7f\u7528\uff0c\u5982\u679c\u4e0d\u6307\u5b9aCONSTRAINT symbol\uff0cMYSQL\u4f1a\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u540d\u5b57\u3002\u5982\u679c\u6307\u5b9a\uff0c\u5b83\u5728\u6570\u636e\u5e93\u91cc\u5fc5\u987b\u662f\u552f\u4e00\u7684\u3002<\/p>\n<p>ON DELETE\u3001ON UPDATE\u8868\u793a\u4e8b\u4ef6\u89e6\u53d1\u9650\u5236\uff0c\u53ef\u8bbe\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>\u2460 RESTRICT\uff08\u9650\u5236\u5916\u8868\u4e2d\u7684\u5916\u952e\u6539\u52a8\uff0c\u9ed8\u8ba4\u503c\uff0c\u6700\u5b89\u5168\u7684\u8bbe\u7f6e\uff09<\/li>\n<li>\u2461 CASCADE\uff08\u8ddf\u968f\u5916\u952e\u6539\u52a8\uff09<\/li>\n<li>\u2462 SET NULL\uff08\u8bbe\u7a7a\u503c\uff09<\/li>\n<li>\u2463 SET DEFAULT\uff08\u8bbe\u9ed8\u8ba4\u503c\uff09<\/li>\n<li>\u2464 NO ACTION\uff08\u65e0\u52a8\u4f5c\uff0c\u9ed8\u8ba4\u7684\uff09<\/li>\n<\/ul>\n<pre><code class=\"language-sql\">\/\/FK_ID\u662f\u5916\u952e\u7684\u540d\u79f0\n\/*\nCREATE TABLE `tb_active` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `title` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,\n `content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,\n `user_id` int(11) NOT NULL,\n PRIMARY KEY (`id`),\n KEY `user_id` (`user_id`),\n CONSTRAINT `FK_ID` FOREIGN KEY (`user_id`) REFERENCES `tb_user` (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=latin1\n*\/<\/code><\/pre>\n<h3>2\u3001\u793a\u4f8b<\/h3>\n<p>1\uff09\u521b\u5efa\u8868<\/p>\n<p>\u88681\uff1a<\/p>\n<pre><code class=\"language-sql\">CREATE TABLE `category_table`(\n `category_id` varchar(6) not null primary key,\n `category_name` varchar(32) not null\n ) ENGINE=InnoDB DEFAULT CHARSET=gb2312;<\/code><\/pre>\n<p>\u88682\uff1a<\/p>\n<pre><code class=\"language-sql\">CREATE TABLE `goods_table`(\n goods_id varchar(20) not null primary key,\n goods_name char(13) not null,\n category_id char(13) not null,\n constraint `category_id_check` foreign key(`category_id`) references `category_table`(`category_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=gb2312;<\/code><\/pre>\n<p>2\uff09\u63d2\u5165\u6570\u636e<\/p>\n<pre><code class=\"language-sql\">insert into `category_table` values(&quot;11&quot;, &quot;\u6570\u7801&quot;); \/\/success\ninsert into `category_table` values(&quot;12&quot;, &quot;\u8863\u670d&quot;); \/\/success\ninsert into `goods_table` values(&quot;1001&quot;, &quot;\u624b\u673a&quot;, &quot;11&quot;); \/\/success\ninsert into `goods_table` values(&quot;1002&quot;, &quot;\u886c\u886b&quot;, &quot;12&quot;); \/\/success\ninsert into `goods_table` values(&quot;1003&quot;, &quot;\u897f\u74dc&quot;, &quot;13&quot;); \/\/failed,\u63d0\u793a\uff1a\nERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`ouloon_test`.`goods_table`, CONSTRAINT `category_id_check` FOREIGN KEY (`category_id`) REFERENCES `category_table` (`category_id`))<\/code><\/pre>\n<p>3\uff09\u589e\u52a0\u7ea7\u8054\u64cd\u4f5c<\/p>\n<pre><code class=\"language-sql\">mysql&gt; alter table goods_table\n    -&gt; add constraint category_id_check\n    -&gt; foreign key(category_id)\n    -&gt; references category_table(category_id)\n    -&gt; on delete cascade\n    -&gt; on update cascade;<\/code><\/pre>\n<h3>3\u3001\u5916\u952e\u76f8\u5173\u8bed\u6cd5<\/h3>\n<p>1\uff09\u4e3a\u5df2\u7ecf\u6dfb\u52a0\u597d\u7684\u6570\u636e\u8868\u6dfb\u52a0\u5916\u952e:<\/p>\n<p>\u8bed\u6cd5\uff1aalter table \u8868\u540d add constraint FK_ID foreign key(\u4f60\u7684\u5916\u952e\u5b57\u6bb5\u540d) REFERENCES \u5916\u8868\u8868\u540d(\u5bf9\u5e94\u7684\u8868\u7684\u4e3b\u952e\u5b57\u6bb5\u540d);<br \/>\n\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-sql\">alter table `tb_active` add constraint `FK_ID` foreign key(`user_id`) REFERENCES `tb_user`(`id`)<\/code><\/pre>\n<p>2\uff09\u5220\u9664\u5916\u952e<\/p>\n<p>\u8bed\u6cd5\uff1aalter table table-name drop foreign key key-id;<br \/>\n\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-sql\">alter table `tb_active` drop foreign key `FK_ID`<\/code><\/pre>\n<p>3\uff09\u67e5\u770b\u8868\u6709\u54ea\u4e9b\u5916\u952e<\/p>\n<p>\u8bed\u6cd5\uff1ashow create table table-name<\/p>\n<p>4\uff09\u5728\u5916\u952e\u4e0a\u5efa\u7acb\u7d22\u5f15:<\/p>\n<pre><code class=\"language-sql\">index `category_id`(`category_id`),\nforeign key(`category_id`) references `category_table`(`category_id`)<\/code><\/pre>\n<p>\u5916\u952e\u53ef\u4ee5\u4fdd\u8bc1\u65b0\u63d2\u5165\u7684\u8bb0\u5f55\u7684\u5b8c\u6574\u6027\uff0c\u4f46\u662f\u5728\u901a\u8fc7ON UPDATE \u548c ON DELETE\u89c4\u5219\uff0c\u8bbe\u7f6eMYSQL\u5b9e\u73b0\u81ea\u52a8\u64cd\u4f5c\u65f6\uff0c\u5982\u679c\u952e\u7684\u5173\u7cfb\u6ca1\u6709\u8bbe\u7f6e\u597d\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e25\u91cd\u7684\u6570\u636e\u7834\u574f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c\u6982\u5ff5 1\u3001MySQL\u4e2d\u201c\u952e\u201d\u548c\u201c\u7d22\u5f15\u201d\u7684\u5b9a\u4e49\u76f8\u540c\uff0c\u6240\u4ee5\u5916\u952e\u548c\u4e3b\u952e\u4e00\u6837\u4e5f\u662f\u7d22\u5f15\u7684\u4e00\u79cd\u3002\u4e0d\u540c\u7684\u662fMySQL\u4f1a\u81ea [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-633","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=633"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/633\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}