{"id":36466,"date":"2024-11-26T11:26:16","date_gmt":"2024-11-26T03:26:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36466\/"},"modified":"2024-11-26T11:26:16","modified_gmt":"2024-11-26T03:26:16","slug":"%e8%a7%a3%e8%af%bbmysql%e7%9a%84%e6%95%b0%e6%8d%ae%e5%ba%93%e7%ba%a6%e6%9d%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36466\/","title":{"rendered":"\u89e3\u8bfbMySQL\u7684\u6570\u636e\u5e93\u7ea6\u675f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/172684122871547.jpg\" class=\"aligncenter\" title=\"\u89e3\u8bfbMySQL\u7684\u6570\u636e\u5e93\u7ea6\u675f\u63d2\u56fe\" alt=\"\u89e3\u8bfbMySQL\u7684\u6570\u636e\u5e93\u7ea6\u675f\u63d2\u56fe\" \/><\/p>\n<p>\u6570\u636e\u5e93\u7ea6\u675f\uff1a\u7ea6\u675f\u662f\u5728\u8868\u4e0a\u5f3a\u5236\u6267\u884c\u7684\u6570\u636e\u6821\u9a8c\u89c4\u5219\uff0c\u4e3b\u8981\u7528\u4e8e\u4fdd\u8bc1\u6570\u636e\u5e93\u91cc\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u5f53\u8868\u4e2d\u7684\u6570\u636e\u5b58\u5728\u76f8\u4e92\u4f9d\u8d56\u6027\u65f6\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u76f8\u5173\u7684\u6570\u636e\u4e0d\u88ab\u5220\u9664\u3002<\/p>\n<p><strong>1.NOT NULL\uff1a \u975e\u7a7a\u7ea6\u675f\uff0c\u6307\u5b9a\u67d0\u5217\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u53ea\u80fd\u4f5c\u4e3a\u5217\u7ea7\u7ea6\u675f\u4f7f\u7528\uff0c\u53ea\u80fd\u4f7f\u7528\u5217\u7ea7\u7ea6\u675f\u8bed\u6cd5\u5b9a\u4e49\u3002<\/strong>  <\/p>\n<p>SQL\u4e2d\u7684null\u503c\uff0cnull\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff0c\u5177\u6709\u5982\u4e0b\u7279\u5f81\uff1a\u6240\u6709\u6570\u636e\u7c7b\u578b\u7684\u503c\u90fd\u53ef\u4ee5\u662fnull\uff0c\u5305\u62ecint\u3001float\u3001boolean\u7b49\u3002\u7a7a\u5b57\u7b26\u4e32\u4e0d\u7b49\u4e8enull\uff0c0\u4e5f\u4e0d\u7b49\u4e8enull\u3002\u5efa\u8868\u65f6\u4e3a\u6307\u5b9a\u5217\u8bbe\u7f6e\u975e\u7a7a\u7ea6\u675f\uff0c\u53ea\u9700\u5728\u5217\u5b9a\u4e49\u540e\u589e\u52a0not null\u5373\u53ef\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>create table t_test\n(\n&nbsp; &nbsp; id int not null,\n&nbsp; &nbsp; name varchar(255) default 'xyz' not null,\n&nbsp; &nbsp; gender varchar(2) null\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f7f\u7528alter table\u4fee\u6539\u8868\u65f6\u589e\u52a0\u6216\u5220\u9664\u975e\u7a7a\u7ea6\u675f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>#\u589e\u52a0\u975e\u7a7a\u7ea6\u675f\nalter table t_test modify gender varchar(2) not null;\n#\u53d6\u6d88\u975e\u7a7a\u7ea6\u675f\nalter table t_test modify gender varchar(2) null;\n#\u53d6\u6d88\u975e\u7a7a\u7ea6\u675f\uff0c\u5e76\u6307\u5b9a\u9ed8\u8ba4\u503c\nalter table t_test modify name varchar(255) default 'abc' null;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236<br \/>\n<strong>2.UNIQUE: \u552f\u4e00\u7ea6\u675f\uff0c\u6307\u5b9a\u67d0\u5217\u6216\u8005\u51e0\u5217\u7ec4\u5408\u4e0d\u80fd\u91cd\u590d\u3002<\/strong>  <\/p>\n<p>\u867d\u7136\u552f\u4e00\u7ea6\u675f\u7684\u5217\u4e0d\u53ef\u4ee5\u51fa\u73b0\u91cd\u590d\u503c\uff0c\u4f46\u53ef\u4ee5\u51fa\u73b0\u591a\u4e2anull\u503c\uff0c\u56e0\u4e3a\u5728\u6570\u636e\u5e93\u4e2dnull\u4e0d\u7b49\u4e8enull\u3002\u540c\u4e00\u4e2a\u8868\u5185\u53ef\u521b\u5efa\u591a\u4e2a\u552f\u4e00\u7ea6\u675f\uff0c\u552f\u4e00\u7ea6\u675f\u4e5f\u53ef\u6709\u591a\u5217\u7ec4\u5408\u800c\u6210\u3002 \u5f53\u4e3a\u67d0\u5217\u521b\u5efa\u552f\u4e00\u7ea6\u675f\u65f6\uff0cMySQL\u4f1a\u4e3a\u8be5\u5217\u521b\u5efa\u552f\u4e00\u7d22\u5f15\uff0c \u5982\u679c\u4e0d\u7ed9\u552f\u4e00\u7ea6\u675f\u8d77\u540d\uff0c\u8be5\u552f\u4e00\u7ea6\u675f\u9ed8\u8ba4\u4e0e\u5217\u540d\u76f8\u540c\u3002\u552f\u4e00\u7ea6\u675f\u53ef\u4ee5\u4f7f\u7528\u5217\u7ea7\u8bed\u6cd5\u5efa\u7acb\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8868\u7ea7\u8bed\u6cd5\u5efa\u7acb\u3002 \u5982\u679c\u662f\u4e3a\u591a\u5217\u5efa\u7acb\u7ec4\u5408\u7ea6\u675f\uff0c\u6216\u8005\u9700\u8981\u4e3a\u7ea6\u675f\u6307\u5b9a\u7ea6\u675f\u540d\uff0c\u5219\u53ea\u80fd\u4f7f\u7528\u8868\u7ea7\u8bed\u6cd5\u3002<\/p>\n<p>\u4f7f\u7528\u5217\u7ea7\u8bed\u6cd5\u5efa\u7acb\u552f\u4e00\u7ea6\u675f\uff0c\u53ea\u9700\u8981\u5728\u5217\u5b9a\u4e49\u540e\u589e\u52a0unique\u5173\u952e\u5b57\u5373\u53ef:<\/p>\n<pre>#\u521b\u5efa\u8868\u65f6\u5efa\u7acb\u552f\u4e00\u7ea6\u675f\uff0c\u4f7f\u7528\u5217\u7ea7\u8bed\u6cd5\u5efa\u7acb\ncreate table unique_test\n(\n&nbsp; &nbsp; #\u5efa\u7acb\u975e\u7a7a\u7ea6\u675f\uff0c\u610f\u5473\u7740id\u4e0d\u80fd\u4e3anull\n&nbsp; &nbsp; id int not null,\n&nbsp; &nbsp; #\u5efa\u7acb\u552f\u4e00\u7ea6\u675f\uff0c\u610f\u5473\u7740\u591a\u884c\u6570\u636e\u7684\u8be5\u5217\u503c\u4e0d\u80fd\u76f8\u7b49\n&nbsp; &nbsp; name varchar(255) unique\n);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8868\u7ea7\u8bed\u6cd5\u521b\u5efa\u552f\u4e00\u7ea6\u675f\u7684\u683c\u5f0f\uff1a[constraint \u7ea6\u675f\u540d] unique (\u5217\u540d[,\u5217\u540d,&#8230;])\uff0c\u4e0a\u9762\u7684\u8868\u7ea7\u7ea6\u675f\u8bed\u6cd5\u683c\u5f0f\u65e2\u53ef\u4ee5\u653e\u5728create table\u8bed\u53e5\u4e2d\u4e0e\u5217\u5b9a\u4e49\u5e76\u5217\uff0c\u4e5f\u53ef\u4ee5\u653e\u5728alter table\u8bed\u53e5\u4e2d\u4f7f\u7528add\u5173\u952e\u5b57\u6dfb\u52a0\uff1a<\/p>\n<pre>#\u521b\u5efa\u8868\u65f6\uff0c\u4f7f\u7528\u8868\u7ea7\u8bed\u6cd5\u521b\u5efa\u552f\u4e00\u7ea6\u675f\ncreate table uniques_test2\n(\n&nbsp; &nbsp; #\u4e3aid\u5efa\u7acb\u975e\u7a7a\u7ea6\u675f\uff0c\u610f\u5473\u7740id\u4e0d\u80fd\u4e3anull\n&nbsp; &nbsp; id int not null,\n&nbsp; &nbsp; name varchar(255),\n&nbsp; &nbsp; pass varchar(255),\n&nbsp; &nbsp; #\u4f7f\u7528\u8868\u7ea7\u8bed\u6cd5\u4e3aname\u5efa\u7acb\u552f\u4e00\u7ea6\u675f\n&nbsp; &nbsp; unique (name),\n&nbsp; &nbsp; #\u4f7f\u7528\u8868\u7ea7\u8bed\u6cd5\u4e3apass\u5efa\u7acb\u552f\u4e00\u7ea6\u675f\uff0c\u5e76\u6307\u5b9a\u7ea6\u675f\u540d\u4e3atest2_uk\n&nbsp; &nbsp; constraint test2_uk unique (pass)\n);\n\n#\u521b\u5efa\u8868\u65f6\uff0c\u4f7f\u7528\u8868\u7ea7\u8bed\u6cd5\u5efa\u7acb\u7ec4\u5408\u5217\u7684\u552f\u4e00\u7ea6\u675f\ncreate table unique_test3\n(\n&nbsp; &nbsp; id int not null,\n&nbsp; &nbsp; name varchar(255),\n&nbsp; &nbsp; pass varchar(255),\n&nbsp; &nbsp; #\u6307\u5b9aname\u548cpass\u4e24\u5217\u7ec4\u5408\u4e0d\u80fd\u91cd\u590d\n&nbsp; &nbsp; constraint test3_uk unique (name, pass)\n);\n\n#\u5728\u4fee\u6539\u8868\u65f6\uff0c\u4f7f\u7528add\u5173\u952e\u5b57\u6765\u589e\u52a0\u552f\u4e00\u7ea6\u675f\nalter table unique_test3 add unique (id, name);\n#\u5728\u4fee\u6539\u8868\u65f6\uff0c\u4f7f\u7528modify\u5173\u952e\u5b57\uff0c\u6765\u4e3a\u5355\u5217\u8bbe\u7f6e\u552f\u4e00\u7ea6\u675f\nalter table unique_test3 modify name varchar(100) unique;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>MySQL\u4e2d\u5220\u9664\u552f\u4e00\u7ea6\u675f\uff1a<\/p>\n<pre>alter table tableName drop index \u7ea6\u675f\u540d\uff1b\n&nbsp; &nbsp; \u4f8b\u53e5\uff1a\n#\u5220\u9664unique_test3\u8868\u4e2d\u7684test3_uk\u552f\u4e00\u7ea6\u675f\nalter table unique_test3 drop index test3_uk;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236<br \/>\n<strong>3.PRIMARY KEY: \u4e3b\u952e\u7ea6\u675f\uff0c\u6307\u5b9a\u8be5\u5217\u7684\u503c\u53ef\u4ee5\u552f\u4e00\u5730\u6807\u8bc6\u8be5\u6761\u8bb0\u5f55\u3002<\/strong><br \/>\n<strong>4.FOREIGN KEY: \u5916\u952e\u7ea6\u675f\uff0c\u6307\u5b9a\u8be5\u884c\u8bb0\u5f55\u4ece\u5c5e\u4e8e\u4e3b\u8868\u4e2d\u7684\u4e00\u6761\u8bb0\u5f55\uff0c\u4e3b\u8981\u7528\u4e8e\u4fdd\u8bc1\u53c2\u7167\u5b8c\u6574\u6027\u3002<\/strong><br \/>\n<strong>5.CHECK(MySQL\u4e0d\u652f\u6301): \u68c0\u67e5\u7ea6\u675f\uff0c\u6307\u5b9a\u4e00\u4e2a\u5e03\u5c14\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u6307\u5b9a\u5bf9\u5e94\u5217\u7684\u503c\u5fc5\u987b\u6ee1\u8db3\u8be5\u8868\u8fbe\u5f0f\u3002<\/strong>  <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u89e3\u8bfbMySQL\u7684\u6570\u636e\u5e93\u7ea6\u675f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e\u5e93\u7ea6\u675f\uff1a\u7ea6\u675f\u662f\u5728\u8868\u4e0a\u5f3a\u5236\u6267\u884c\u7684\u6570\u636e\u6821\u9a8c\u89c4\u5219\uff0c\u4e3b\u8981\u7528\u4e8e\u4fdd\u8bc1\u6570\u636e\u5e93\u91cc\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u5f53\u8868\u4e2d\u7684\u6570\u636e\u5b58\u5728\u76f8\u4e92\u4f9d\u8d56\u6027\u65f6\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u76f8\u5173\u7684\u6570\u636e\u4e0d\u88ab\u5220\u9664\u3002 1.NOT NULL\uff1a \u975e\u7a7a\u7ea6\u675f\uff0c\u6307\u5b9a\u67d0\u5217\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u53ea\u80fd\u4f5c\u4e3a\u5217\u7ea7\u7ea6\u675f\u4f7f\u7528\uff0c\u53ea\u80fd\u4f7f\u7528\u5217\u7ea7\u7ea6\u675f\u8bed\u6cd5\u5b9a\u4e49\u3002 SQL\u4e2d\u7684null\u503c\uff0cnull\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff0c\u5177\u6709\u5982\u4e0b\u7279\u5f81\uff1a\u6240\u6709\u6570\u636e\u7c7b\u578b\u7684\u503c\u90fd\u53ef\u4ee5\u662fnull\uff0c\u5305\u62ecint\u3001float\u3001boolean\u7b49\u3002\u7a7a\u5b57\u7b26\u4e32\u4e0d\u7b49\u4e8enull\uff0c0\u4e5f\u4e0d\u7b49\u4e8enull\u3002\u5efa\u8868\u65f6\u4e3a\u6307\u5b9a\u5217\u8bbe\u7f6e\u975e\u7a7a\u7ea6\u675f\uff0c\u53ea\u9700\u5728\u5217\u5b9a\u4e49\u540e\u589e\u52a0not null\u5373\u53ef\uff0c\u4f8b\u5982\uff1a create table t_test ( &nbsp; &nbsp; id int not null, &nbsp; &nbsp; name varchar(255) default &#8216;xyz&#8217; not null, &nbsp; &nbsp; gender varchar(2) null ); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528alter table\u4fee\u6539\u8868\u65f6\u589e\u52a0\u6216\u5220\u9664\u975e\u7a7a\u7ea6\u675f\uff0c\u4f8b\u5982\uff1a #\u589e\u52a0\u975e\u7a7a\u7ea6\u675f alter table t_test modify gender varchar(2) not null; #\u53d6\u6d88\u975e\u7a7a\u7ea6\u675f alter table t_test modify gender varchar(2) null; #\u53d6\u6d88\u975e\u7a7a\u7ea6\u675f\uff0c\u5e76\u6307\u5b9a\u9ed8\u8ba4\u503c alter table t_test modify [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-36466","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36466","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=36466"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36466\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}