{"id":45267,"date":"2023-04-01T05:21:56","date_gmt":"2023-03-31T21:21:56","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=45267"},"modified":"2023-04-01T05:21:56","modified_gmt":"2023-03-31T21:21:56","slug":"mysql-null-vs-not-null","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mysql-null-vs-not-null.html","title":{"rendered":"mysql\u4e2dnull\u4e0e\u201c\u7a7a\u503c\u201d\u7684\u5751"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6570\u636e\u5e93\u5728\u4f01\u4e1a\u73af\u5883\u4e2d\u662f\u975e\u5e38\u5e38\u7528\u7684\uff0c\u4e0d\u4ec5\u4ec5\u662fDBA\uff0c\u8fd0\u7ef4\u4eba\u5458\u548c\u5f00\u53d1\u4eba\u5458\u90fd\u8981\u719f\u6089\u6570\u636e\u5e93\u7684\u4f7f\u7528\uff0c\u589e\u5220\u6539\u67e5\u7b49\u64cd\u4f5c\u3002\u800c\u5bf9\u4e8e\u4f7f\u7528\u6570\u636e\u5e93\u7684\u4eba\u5458\u6765\u8bf4\uff0c\u5bf9\u4e8e\u5b57\u6bb5\u3001\u5c5e\u6027\u7684\u719f\u6089\u662f\u76f8\u5f53\u91cd\u8981\u7684\u3002\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5206\u4eab\u4e0bmysql\u4e0b\u9762\u7684\u5b57\u6bb5\u7c7b\u578bnull \u548c''\u7684\u533a\u522b\uff0c\u65b0\u624b\u5f88\u9700\u8981\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u5b57\u6bb5\u5c5e\u6027\u7684\"\u7a7a\u503c\"\u4e0e\"NULL\"\u4e0d\u4e00\u6837<\/strong><\/div>\n

\u76f8\u4fe1\u5f88\u591a\u7528\u4e86MySQL\u5f88\u4e45\u7684\u4eba\uff0c\u5bf9\u8fd9\u4e24\u4e2a\u5b57\u6bb5\u5c5e\u6027\u7684\u6982\u5ff5\u8fd8\u4e0d\u662f\u5f88\u6e05\u695a\uff0c\u4e00\u822c\u4f1a\u6709\u4ee5\u4e0b\u7591\u95ee\uff1a<\/p>\n

1\u3001\u6211\u5b57\u6bb5\u7c7b\u578b\u662fnot null\uff0c\u4e3a\u4ec0\u4e48\u6211\u53ef\u4ee5\u63d2\u5165\u7a7a\u503c\r\n\r\n2\u3001\u4e3a\u6bdbnot null\u7684\u6548\u7387\u6bd4null\u9ad8\r\n\r\n3\u3001\u5224\u65ad\u5b57\u6bb5\u4e0d\u4e3a\u7a7a\u7684\u65f6\u5019\uff0c\u5230\u5e95\u8981 \u00a0select * from table where column <> '' \u8fd8\u662f\u8981\u7528\u00a0select * from table where column is not null \u5462\u3002\r\n<\/pre>\n

\u5e26\u7740\u4e0a\u9762\u51e0\u4e2a\u7591\u95ee\uff0c\u6211\u4eec\u6765\u6df1\u5165\u7814\u7a76\u4e00\u4e0bnull \u548c not null \u5230\u5e95\u6709\u4ec0\u4e48\u4e0d\u4e00\u6837\u3002<\/p>\n

\u7a7a\u503c\u201d \u548c \u201cNULL\u201d \u7684\u6982\u5ff5<\/strong><\/span><\/div>\n

\u9996\u5148\uff0c\u6211\u4eec\u8981\u641e\u6e05\u695a\u201c\u7a7a\u503c\u201d \u548c \u201cNULL\u201d \u7684\u6982\u5ff5\uff1a<\/p>\n

1\u3001\u7a7a\u503c\u662f\u4e0d\u5360\u7528\u7a7a\u95f4\u7684<\/p>\n

2\u3001mysql\u4e2d\u7684NULL\u5176\u5b9e\u662f\u5360\u7528\u7a7a\u95f4\u7684\uff0c\u4e0b\u9762\u662f\u6765\u81ea\u4e8eMYSQL\u5b98\u65b9\u7684\u89e3\u91ca<\/p>\n

\u201cNULL columns require additional space in the row to record whether their values are NULL. For MyISAM tables, each NULL column takes one bit extra, rounded up to the nearest byte.\u201d<\/p><\/blockquote>\n

PS\uff1a\u6253\u4e2a\u6bd4\u65b9\u6765\u8bf4\uff0c\u4f60\u6709\u4e00\u4e2a\u676f\u5b50\uff0c\u7a7a\u503c\u4ee3\u8868\u676f\u5b50\u662f\u771f\u7a7a\u7684\uff0cNULL\u4ee3\u8868\u676f\u5b50\u4e2d\u88c5\u6ee1\u4e86\u7a7a\u6c14\uff0c\u867d\u7136\u676f\u5b50\u770b\u8d77\u6765\u90fd\u662f\u7a7a\u7684\uff0c\u4f46\u662f\u533a\u522b\u662f\u5f88\u5927\u7684\u3002<\/p>\n

\u5b9e\u9a8c\u6d4b\u8bd5\"\u7a7a\u503c\"\u548c\"NULL\"\u7684\u533a\u522b<\/strong><\/div>\n
CREATE TABLE  `test` (  \r\n `col1` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,  \r\n `col2` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL  \r\n) ENGINE = MYISAM ;  \r\n<\/pre>\n

\u63d2\u5165\u6570\u636e\uff1a<\/p>\n

INSERT INTO `test` VALUES (null,1);  \r\n<\/pre>\n

mysql\u53d1\u751f\u9519\u8bef\uff1a<\/p>\n

#1048 - Column 'col1' cannot be null\r\n<\/pre>\n

\u518d\u6765\u4e00\u6761<\/p>\n

INSERT INTO `test` VALUES ('',1); \r\n<\/pre>\n

\u6210\u529f\u63d2\u5165\u6570\u636e\uff0c\u53ef\u89c1\uff0cNOT NULL \u7684\u5b57\u6bb5\u662f\u4e0d\u80fd\u63d2\u5165\u201cNULL\u201d\u7684\uff0c\u53ea\u80fd\u63d2\u5165\u201c\u7a7a\u503c\u201d\uff0c\u4e0a\u9762\u7684\u95ee\u98981\u4e5f\u5c31\u6709\u7b54\u6848\u4e86\u3002<\/p>\n

\u5bf9\u4e8e\u95ee\u98982\uff0c\u4e0a\u9762\u6211\u4eec\u5df2\u7ecf\u8bf4\u8fc7\u4e86\uff0cNULL \u5176\u5b9e\u5e76\u4e0d\u662f\u7a7a\u503c\uff0c\u800c\u662f\u8981\u5360\u7528\u7a7a\u95f4\uff0c\u6240\u4ee5mysql\u5728\u8fdb\u884c\u6bd4\u8f83\u7684\u65f6\u5019\uff0cNULL \u4f1a\u53c2\u4e0e\u5b57\u6bb5\u6bd4\u8f83\uff0c\u6240\u4ee5\u5bf9\u6548\u7387\u6709\u4e00\u90e8\u5206\u5f71\u54cd\uff0c\u800c\u4e14B\u6811\u7d22\u5f15\u65f6\u4e0d\u4f1a\u5b58\u50a8NULL\u503c\u7684\uff0c\u6240\u4ee5\u5982\u679c\u7d22\u5f15\u7684\u5b57\u6bb5\u53ef\u4ee5\u4e3aNULL\uff0c\u7d22\u5f15\u7684\u6548\u7387\u4f1a\u4e0b\u964d\u5f88\u591a\u3002
\n\u6211\u4eec\u518d\u5411test\u7684\u8868\u4e2d\u63d2\u5165\u51e0\u6761\u6570\u636e\uff1a<\/p>\n

INSERT INTO `test` VALUES ('', NULL);  \r\nINSERT INTO `test` VALUES ('1', '2');  \r\n<\/pre>\n

\u73b0\u5728\u8868\u4e2d\u6570\u636e\uff1a<\/p>\n

\"\"<\/p>\n

\u73b0\u5728\u6839\u636e\u9700\u6c42\uff0c\u6211\u8981\u7edf\u8ba1test\u8868\u4e2dcol1\u4e0d\u4e3a\u7a7a\u7684\u6240\u6709\u6570\u636e\uff0c\u6211\u662f\u8be5\u7528\u201c<> ''\u201d \u8fd8\u662f \u201cIS NOT NULL\u201d \u5462\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e0b\u7ed3\u679c\u7684\u533a\u522b\u3002<\/p>\n

SELECT * FROM `test` WHERE col1 IS NOT NULL \r\n<\/pre>\n

\"\"<\/p>\n

\u53ef\u4ee5\u770b\u5230\uff0c\u7ed3\u679c\u8fe5\u7136\u4e0d\u540c\uff0c\u6240\u4ee5\u6211\u4eec\u4e00\u5b9a\u8981\u6839\u636e\u4e1a\u52a1\u9700\u6c42\uff0c\u641e\u6e05\u695a\u5230\u5e95\u662f\u8981\u7528\u90a3\u79cd\u641c\u7d22\u6761\u4ef6\u3002<\/p>\n

\"is not null \" \u548c \"!=null\"<\/strong><\/div>\n

\u5728\u7b5b\u9009\u975e\u7a7a\u7684\u5b57\u6bb5\u7ecf\u5e38\u4f1a\u7528\u5230\"is not null\"\u548c\"!=null\"\uff0c\u4f46\u662f\u8fd9\u4e24\u79cd\u65b9\u6cd5\u662f\u4e0d\u4e00\u6837\u7684\uff01\uff01\uff01\uff01
\nWhy\uff1f\uff1f\uff1f\uff1f\uff1f\u800c\"null\" \u8868\u793a\u4ec0\u4e48\u4e5f\u4e0d\u662f\uff0c \u7528\u201c=\u3001>\u3001< ...\u201d \u6240\u6709\u7684\u5224\u65ad\uff0c\u7ed3\u679c\u90fd\u662ffalse\uff0c\u6240\u6709\u53ea\u80fd\u7528 is null\u8fdb\u884c\u5224\u65ad\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u63a8\u8350\u4f7f\u7528 IS NOT NULL\u53bb\u5224\u65ad\uff0c\u56e0\u4e3aSQL\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5bf9\u3002\"\uff01= Null\"\u7684\u5224\u65ad\u4f1a\u6c38\u8fdc\u8fd4\u56de0\u884c\uff0c\u4f46\u6ca1\u6709\u8bed\u6cd5\u9519\u8bef\u3002\u5982\u679c\u4f60\u4e00\u5b9a\u60f3\u8981\u4f7f\u7528\"\uff01= Null\"\u6765\u5224\u65ad\uff0c\u9700\u8981\u52a0\u4e0a\u8fd9\u4e2a\u8bed\u53e5\uff1a\u201cset ANSI_NULLS off\u201d\u8fd9\u65f6\u4f60\u4f1a\u53d1\u73b0\u201cIS NOT NULL\u201d \u548c \u201c!= null\u201d \u662f\u7b49\u6548\u7684\u3002<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/blog.csdn.net\/fwkjdaghappy1\/article\/details\/7703974<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/mysql-null-vs-not-null.html<\/a>\u7f16\u8f91\uff1a\u9676\u6b66\u6770\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb \u6570\u636e\u5e93\u5728\u4f01\u4e1a\u73af\u5883\u4e2d\u662f\u975e\u5e38\u5e38\u7528\u7684\uff0c\u4e0d\u4ec5\u4ec5\u662fDBA\uff0c\u8fd0\u7ef4\u4eba\u5458\u548c\u5f00\u53d1\u4eba\u5458\u90fd\u8981\u719f\u6089\u6570\u636e\u5e93\u7684\u4f7f\u7528\uff0c\u589e\u5220\u6539\u67e5\u7b49\u64cd\u4f5c\u3002 […]<\/p>\n","protected":false},"author":63,"featured_media":45318,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-45267","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\/45267","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=45267"}],"version-history":[{"count":12,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/45267\/revisions"}],"predecessor-version":[{"id":268654,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/45267\/revisions\/268654"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/45318"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=45267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=45267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=45267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}