{"id":105326,"date":"2024-09-17T19:14:11","date_gmt":"2024-09-17T11:14:11","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=105326"},"modified":"2024-09-17T19:14:11","modified_gmt":"2024-09-17T11:14:11","slug":"mysql-restrict","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mysql-restrict.html","title":{"rendered":"\u89e3\u8bfbMySQL\u7684\u6570\u636e\u5e93\u7ea6\u675f"},"content":{"rendered":"

\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

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><\/div>\n

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

create table t_test\r\n(\r\n\u00a0 \u00a0 id int not null,\r\n\u00a0 \u00a0 name varchar(255) default 'xyz' not null,\r\n\u00a0 \u00a0 gender varchar(2) null\r\n);<\/pre>\n

\u4f7f\u7528alter table\u4fee\u6539\u8868\u65f6\u589e\u52a0\u6216\u5220\u9664\u975e\u7a7a\u7ea6\u675f\uff0c\u4f8b\u5982\uff1a<\/p>\n

#\u589e\u52a0\u975e\u7a7a\u7ea6\u675f\r\nalter table t_test modify gender varchar(2) not null;\r\n#\u53d6\u6d88\u975e\u7a7a\u7ea6\u675f\r\nalter table t_test modify gender varchar(2) null;\r\n#\u53d6\u6d88\u975e\u7a7a\u7ea6\u675f\uff0c\u5e76\u6307\u5b9a\u9ed8\u8ba4\u503c\r\nalter table t_test modify name varchar(255) default 'abc' null;<\/pre>\n
2.UNIQUE: \u552f\u4e00\u7ea6\u675f\uff0c\u6307\u5b9a\u67d0\u5217\u6216\u8005\u51e0\u5217\u7ec4\u5408\u4e0d\u80fd\u91cd\u590d\u3002<\/strong><\/div>\n

\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

\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

#\u521b\u5efa\u8868\u65f6\u5efa\u7acb\u552f\u4e00\u7ea6\u675f\uff0c\u4f7f\u7528\u5217\u7ea7\u8bed\u6cd5\u5efa\u7acb\r\ncreate table unique_test\r\n(\r\n\u00a0 \u00a0 #\u5efa\u7acb\u975e\u7a7a\u7ea6\u675f\uff0c\u610f\u5473\u7740id\u4e0d\u80fd\u4e3anull\r\n\u00a0 \u00a0 id int not null,\r\n\u00a0 \u00a0 #\u5efa\u7acb\u552f\u4e00\u7ea6\u675f\uff0c\u610f\u5473\u7740\u591a\u884c\u6570\u636e\u7684\u8be5\u5217\u503c\u4e0d\u80fd\u76f8\u7b49\r\n\u00a0 \u00a0 name varchar(255) unique\r\n);<\/pre>\n

\u8868\u7ea7\u8bed\u6cd5\u521b\u5efa\u552f\u4e00\u7ea6\u675f\u7684\u683c\u5f0f\uff1a[constraint \u7ea6\u675f\u540d] unique (\u5217\u540d[,\u5217\u540d,...])\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

#\u521b\u5efa\u8868\u65f6\uff0c\u4f7f\u7528\u8868\u7ea7\u8bed\u6cd5\u521b\u5efa\u552f\u4e00\u7ea6\u675f\r\ncreate table uniques_test2\r\n(\r\n\u00a0 \u00a0 #\u4e3aid\u5efa\u7acb\u975e\u7a7a\u7ea6\u675f\uff0c\u610f\u5473\u7740id\u4e0d\u80fd\u4e3anull\r\n\u00a0 \u00a0 id int not null,\r\n\u00a0 \u00a0 name varchar(255),\r\n\u00a0 \u00a0 pass varchar(255),\r\n\u00a0 \u00a0 #\u4f7f\u7528\u8868\u7ea7\u8bed\u6cd5\u4e3aname\u5efa\u7acb\u552f\u4e00\u7ea6\u675f\r\n\u00a0 \u00a0 unique (name),\r\n\u00a0 \u00a0 #\u4f7f\u7528\u8868\u7ea7\u8bed\u6cd5\u4e3apass\u5efa\u7acb\u552f\u4e00\u7ea6\u675f\uff0c\u5e76\u6307\u5b9a\u7ea6\u675f\u540d\u4e3atest2_uk\r\n\u00a0 \u00a0 constraint test2_uk unique (pass)\r\n);\r\n\r\n#\u521b\u5efa\u8868\u65f6\uff0c\u4f7f\u7528\u8868\u7ea7\u8bed\u6cd5\u5efa\u7acb\u7ec4\u5408\u5217\u7684\u552f\u4e00\u7ea6\u675f\r\ncreate table unique_test3\r\n(\r\n\u00a0 \u00a0 id int not null,\r\n\u00a0 \u00a0 name varchar(255),\r\n\u00a0 \u00a0 pass varchar(255),\r\n\u00a0 \u00a0 #\u6307\u5b9aname\u548cpass\u4e24\u5217\u7ec4\u5408\u4e0d\u80fd\u91cd\u590d\r\n\u00a0 \u00a0 constraint test3_uk unique (name, pass)\r\n);\r\n\r\n#\u5728\u4fee\u6539\u8868\u65f6\uff0c\u4f7f\u7528add\u5173\u952e\u5b57\u6765\u589e\u52a0\u552f\u4e00\u7ea6\u675f\r\nalter table unique_test3 add unique (id, name);\r\n#\u5728\u4fee\u6539\u8868\u65f6\uff0c\u4f7f\u7528modify\u5173\u952e\u5b57\uff0c\u6765\u4e3a\u5355\u5217\u8bbe\u7f6e\u552f\u4e00\u7ea6\u675f\r\nalter table unique_test3 modify name varchar(100) unique;<\/pre>\n

MySQL\u4e2d\u5220\u9664\u552f\u4e00\u7ea6\u675f\uff1a<\/p>\n

alter table tableName drop index \u7ea6\u675f\u540d\uff1b\r\n\u00a0 \u00a0 \u4f8b\u53e5\uff1a\r\n#\u5220\u9664unique_test3\u8868\u4e2d\u7684test3_uk\u552f\u4e00\u7ea6\u675f\r\nalter table unique_test3 drop index test3_uk;<\/pre>\n
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><\/div>\n
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><\/div>\n
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><\/div>\n
\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.linuxsir.org\/linuxsjk\/14616.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttps:\/\/lrxjmw.cn\/mysql-restrict.html<\/a>\u7f16\u8f91\uff1a\u8d3e\u6625\u534e \u00a0 \u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\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 […]<\/p>\n","protected":false},"author":63,"featured_media":105241,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-105326","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/105326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=105326"}],"version-history":[{"count":9,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/105326\/revisions"}],"predecessor-version":[{"id":105586,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/105326\/revisions\/105586"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/105241"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=105326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=105326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=105326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}