{"id":6585,"date":"2024-11-14T08:11:39","date_gmt":"2024-11-14T00:11:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6585\/"},"modified":"2024-11-14T08:11:39","modified_gmt":"2024-11-14T00:11:39","slug":"mysql-constraint%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6585\/","title":{"rendered":"mysql constraint\u7528\u6cd5"},"content":{"rendered":"<blockquote><p>\n  mysql \u7ea6\u675f\u7528\u4e8e\u5b9a\u4e49\u6570\u636e\u5e93\u5173\u7cfb\uff0c\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u3002\u5177\u4f53\u7c7b\u578b\u5305\u62ec\uff1a\u4e3b\u952e\u7ea6\u675f\uff1a\u6807\u8bc6\u552f\u4e00\u884c\uff1b\u5916\u952e\u7ea6\u675f\uff1a\u5efa\u7acb\u8868\u95f4\u5173\u7cfb\uff1b\u552f\u4e00\u7d22\u5f15\u7ea6\u675f\uff1a\u9632\u6b62\u91cd\u590d\u503c\uff1b\u975e\u7a7a\u7ea6\u675f\uff1a\u5f3a\u5236\u503c\u4e0d\u4e3a null\uff1b\u9ed8\u8ba4\u503c\u7ea6\u675f\uff1a\u6307\u5b9a\u9ed8\u8ba4\u503c\uff1b\u68c0\u67e5\u7ea6\u675f\uff1a\u9650\u5236\u503c\u8303\u56f4\u3002\u901a\u8fc7 alter table \u8bed\u53e5\u6dfb\u52a0\u7ea6\u675f\uff0c\u53ef\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u3001\u7b80\u5316\u8bbe\u8ba1\u5e76\u63d0\u5347\u6027\u80fd\uff0c\u4f46\u9700\u6ce8\u610f\u5176\u5bf9\u6027\u80fd\u548c\u4fee\u6539\u590d\u6742\u6027\u7684\u5f71\u54cd\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/06\/2024080618342692053.jpg\" class=\"aligncenter\" title=\"mysql constraint\u7528\u6cd5\u63d2\u56fe\" alt=\"mysql constraint\u7528\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u7ea6\u675f\u7684\u4f7f\u7528<\/strong><\/p>\n<p>MySQL \u7ea6\u675f\u662f\u4e00\u79cd\u7528\u4e8e\u5b9a\u4e49\u6570\u636e\u5e93\u5217\u6216\u8868\u4e4b\u95f4\u5173\u7cfb\u7684\u673a\u5236\u3002\u7ea6\u675f\u901a\u8fc7\u5f3a\u5236\u67d0\u4e9b\u89c4\u5219\u6765\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<p><strong>\u7ea6\u675f\u7c7b\u578b<\/strong><\/p>\n<ul>\n<li> <strong>\u4e3b\u952e\u7ea6\u675f (PRIMARY KEY)<\/strong>\uff1a\u6307\u5b9a\u8868\u4e2d\u552f\u4e00\u6807\u8bc6\u6bcf\u884c\u7684\u5217\u3002<\/li>\n<li> <strong>\u5916\u952e\u7ea6\u675f (FOREIGN KEY)<\/strong>\uff1a\u6307\u5b9a\u8868\u4e2d\u4e00\u4e2a\u5217\u4e0e\u53e6\u4e00\u8868\u7684\u4e3b\u952e\u5217\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/li>\n<li> <strong>\u552f\u4e00\u7d22\u5f15\u7ea6\u675f (UNIQUE INDEX)<\/strong>\uff1a\u9632\u6b62\u8868\u4e2d\u4e00\u5217\u5305\u542b\u91cd\u590d\u503c\u3002<\/li>\n<li> <strong>\u975e\u7a7a\u7ea6\u675f (NOT NULL)<\/strong>\uff1a\u5f3a\u5236\u4e00\u5217\u4e0d\u80fd\u4e3a NULL\u3002<\/li>\n<li> <strong>\u9ed8\u8ba4\u503c\u7ea6\u675f (DEFAULT)<\/strong>\uff1a\u4e3a\u4e00\u5217\u6307\u5b9a\u9ed8\u8ba4\u503c\u3002<\/li>\n<li> <strong>\u68c0\u67e5\u7ea6\u675f (CHECK)<\/strong>\uff1a\u901a\u8fc7\u6307\u5b9a\u6761\u4ef6\u5bf9\u5217\u503c\u8fdb\u884c\u9650\u5236\u3002<\/li>\n<\/ul>\n<p><strong>\u6dfb\u52a0\u7ea6\u675f<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 ALTER TABLE \u8bed\u53e5\u53ef\u4ee5\u5411\u8868\u4e2d\u6dfb\u52a0\u7ea6\u675f\uff1a<\/p>\n<pre>ALTER TABLE table_name ADD CONSTRAINT constraint_name CONSTRAINT_TYPE;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4f7f\u7528\u4e3b\u952e\u7ea6\u675f\u552f\u4e00\u6807\u8bc6\u8868\u4e2d\u6bcf\u4e2a\u5ba2\u6237\uff1a<\/p>\n<pre>ALTER TABLE customers ADD PRIMARY KEY (customer_id);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f7f\u7528\u5916\u952e\u7ea6\u675f\u5c06\u8ba2\u5355\u8868\u4e0e\u5ba2\u6237\u8868\u8fde\u63a5\uff1a<\/p>\n<pre>ALTER TABLE orders ADD FOREIGN KEY (customer_id) REFERENCES customers(customer_id);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f7f\u7528\u975e\u7a7a\u7ea6\u675f\u5f3a\u5236\u5730\u5740\u5217\u4e0d\u4e3a NULL\uff1a<\/p>\n<pre>ALTER TABLE customers ALTER COLUMN address SET NOT NULL;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n<p>\u4f7f\u7528 MySQL \u7ea6\u675f\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li>\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027<\/li>\n<li>\u9632\u6b62\u6570\u636e\u4e0d\u4e00\u81f4<\/li>\n<li>\u7b80\u5316\u6570\u636e\u5e93\u8bbe\u8ba1<\/li>\n<li>\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<p>\u4f7f\u7528\u7ea6\u675f\u65f6\u5e94\u6ce8\u610f\u4ee5\u4e0b\u4e8b\u9879\uff1a<\/p>\n<ul>\n<li>\u7ea6\u675f\u4f1a\u5f71\u54cd\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u6027\u80fd\u3002<\/li>\n<li>\u66f4\u6539\u7ea6\u675f\u53ef\u80fd\u5f88\u590d\u6742\u3002<\/li>\n<li>\u5e94\u4ed4\u7ec6\u8003\u8651\u7ea6\u675f\u7684\u542b\u4e49\uff0c\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u9650\u5236\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f constraint\u7528\u6cd5\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>mysql \u7ea6\u675f\u7528\u4e8e\u5b9a\u4e49\u6570\u636e\u5e93\u5173\u7cfb\uff0c\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u3002\u5177\u4f53\u7c7b\u578b\u5305\u62ec\uff1a\u4e3b\u952e\u7ea6\u675f\uff1a\u6807\u8bc6\u552f\u4e00\u884c\uff1b\u5916\u952e\u7ea6\u675f\uff1a\u5efa\u7acb\u8868\u95f4\u5173\u7cfb\uff1b\u552f\u4e00\u7d22\u5f15\u7ea6\u675f\uff1a\u9632\u6b62\u91cd\u590d\u503c\uff1b\u975e\u7a7a\u7ea6\u675f\uff1a\u5f3a\u5236\u503c\u4e0d\u4e3a null\uff1b\u9ed8\u8ba4\u503c\u7ea6\u675f\uff1a\u6307\u5b9a\u9ed8\u8ba4\u503c\uff1b\u68c0\u67e5\u7ea6\u675f\uff1a\u9650\u5236\u503c\u8303\u56f4\u3002\u901a\u8fc7 alter table \u8bed\u53e5\u6dfb\u52a0\u7ea6\u675f\uff0c\u53ef\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u3001\u7b80\u5316\u8bbe\u8ba1\u5e76\u63d0\u5347\u6027\u80fd\uff0c\u4f46\u9700\u6ce8\u610f\u5176\u5bf9\u6027\u80fd\u548c\u4fee\u6539\u590d\u6742\u6027\u7684\u5f71\u54cd\u3002 MySQL \u7ea6\u675f\u7684\u4f7f\u7528 MySQL \u7ea6\u675f\u662f\u4e00\u79cd\u7528\u4e8e\u5b9a\u4e49\u6570\u636e\u5e93\u5217\u6216\u8868\u4e4b\u95f4\u5173\u7cfb\u7684\u673a\u5236\u3002\u7ea6\u675f\u901a\u8fc7\u5f3a\u5236\u67d0\u4e9b\u89c4\u5219\u6765\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002 \u7ea6\u675f\u7c7b\u578b \u4e3b\u952e\u7ea6\u675f (PRIMARY KEY)\uff1a\u6307\u5b9a\u8868\u4e2d\u552f\u4e00\u6807\u8bc6\u6bcf\u884c\u7684\u5217\u3002 \u5916\u952e\u7ea6\u675f (FOREIGN KEY)\uff1a\u6307\u5b9a\u8868\u4e2d\u4e00\u4e2a\u5217\u4e0e\u53e6\u4e00\u8868\u7684\u4e3b\u952e\u5217\u4e4b\u95f4\u7684\u5173\u7cfb\u3002 \u552f\u4e00\u7d22\u5f15\u7ea6\u675f (UNIQUE INDEX)\uff1a\u9632\u6b62\u8868\u4e2d\u4e00\u5217\u5305\u542b\u91cd\u590d\u503c\u3002 \u975e\u7a7a\u7ea6\u675f (NOT NULL)\uff1a\u5f3a\u5236\u4e00\u5217\u4e0d\u80fd\u4e3a NULL\u3002 \u9ed8\u8ba4\u503c\u7ea6\u675f (DEFAULT)\uff1a\u4e3a\u4e00\u5217\u6307\u5b9a\u9ed8\u8ba4\u503c\u3002 \u68c0\u67e5\u7ea6\u675f (CHECK)\uff1a\u901a\u8fc7\u6307\u5b9a\u6761\u4ef6\u5bf9\u5217\u503c\u8fdb\u884c\u9650\u5236\u3002 \u6dfb\u52a0\u7ea6\u675f \u901a\u8fc7\u4f7f\u7528 ALTER TABLE \u8bed\u53e5\u53ef\u4ee5\u5411\u8868\u4e2d\u6dfb\u52a0\u7ea6\u675f\uff1a ALTER TABLE table_name ADD CONSTRAINT constraint_name CONSTRAINT_TYPE; \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b \u4f7f\u7528\u4e3b\u952e\u7ea6\u675f\u552f\u4e00\u6807\u8bc6\u8868\u4e2d\u6bcf\u4e2a\u5ba2\u6237\uff1a ALTER TABLE customers ADD PRIMARY KEY (customer_id); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528\u5916\u952e\u7ea6\u675f\u5c06\u8ba2\u5355\u8868\u4e0e\u5ba2\u6237\u8868\u8fde\u63a5\uff1a ALTER TABLE orders [&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-6585","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6585","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=6585"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6585\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}