{"id":171411,"date":"2024-11-07T02:31:18","date_gmt":"2024-11-06T18:31:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/171411\/"},"modified":"2024-11-07T02:31:19","modified_gmt":"2024-11-06T18:31:19","slug":"%e6%b7%ba%e8%ab%87%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e7%9a%84null%e8%a1%a8%e7%a4%ba%e6%96%b9%e6%b3%95-%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e8%a1%a8%e7%a4%ba%e7%a9%ba","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/171411\/","title":{"rendered":"\u6dfa\u8ac7\u6578\u64da\u5eab\u4e2d\u7684NULL\u8868\u793a\u65b9\u6cd5 (\u6578\u64da\u5eab\u4e2d\u8868\u793a\u7a7a)"},"content":{"rendered":"<h1 id=\"%e6%b7%ba%e8%ab%87%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e7%9a%84null%e8%a1%a8%e7%a4%ba%e6%96%b9%e6%b3%95-%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e8%a1%a8%e7%a4%ba%e7%a9%ba-fkFSMXtXWN\">\u6dfa\u8ac7\u6578\u64da\u5eab\u4e2d\u7684NULL\u8868\u793a\u65b9\u6cd5 (\u6578\u64da\u5eab\u4e2d\u8868\u793a\u7a7a)<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0cNULL\u662f\u4e00\u500b\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u4ee3\u8868\u8457\u201c\u7121\u503c\u201d\u6216\u201c\u672a\u77e5\u503c\u201d\u3002\u9019\u500b\u6982\u5ff5\u5728\u6578\u64da\u5eab\u8a2d\u8a08\u548c\u67e5\u8a62\u4e2d\u626e\u6f14\u8457\u95dc\u9375\u89d2\u8272\uff0c\u56e0\u70ba\u5b83\u5f71\u97ff\u5230\u6578\u64da\u7684\u5b8c\u6574\u6027\u548c\u67e5\u8a62\u7684\u7d50\u679c\u3002\u672c\u6587\u5c07\u63a2\u8a0eNULL\u7684\u5b9a\u7fa9\u3001\u4f7f\u7528\u60c5\u5883\u4ee5\u53ca\u5728\u4e0d\u540c\u6578\u64da\u5eab\u7cfb\u7d71\u4e2d\u7684\u8868\u793a\u65b9\u6cd5\u3002<\/p>\n<h2 id=\"null%e7%9a%84%e5%ae%9a%e7%be%a9-fkFSMXtXWN\">NULL\u7684\u5b9a\u7fa9<\/h2>\n<p>NULL\u4e26\u4e0d\u7b49\u540c\u65bc\u96f6\u6216\u7a7a\u5b57\u7b26\u4e32\u3002\u5b83\u662f\u4e00\u500b\u7279\u6b8a\u7684\u6a19\u8a18\uff0c\u7528\u4f86\u8868\u793a\u6578\u64da\u7f3a\u5931\u6216\u672a\u77e5\u3002\u7576\u4e00\u500b\u5b57\u6bb5\u7684\u503c\u70baNULL\u6642\uff0c\u9019\u610f\u5473\u8457\u8a72\u5b57\u6bb5\u6c92\u6709\u88ab\u8ce6\u503c\uff0c\u6216\u8005\u8a72\u503c\u662f\u4e0d\u53ef\u7528\u7684\u3002\u5728\u6578\u64da\u5eab\u4e2d\uff0cNULL\u53ef\u4ee5\u7528\u65bc\u5404\u7a2e\u6578\u64da\u985e\u578b\uff0c\u5305\u62ec\u6574\u6578\u3001\u6d6e\u9ede\u6578\u3001\u5b57\u7b26\u4e32\u7b49\u3002<\/p>\n<h2 id=\"null%e7%9a%84%e4%bd%bf%e7%94%a8%e6%83%85%e5%a2%83-fkFSMXtXWN\">NULL\u7684\u4f7f\u7528\u60c5\u5883<\/h2>\n<p>\u5728\u6578\u64da\u5eab\u8a2d\u8a08\u4e2d\uff0cNULL\u7684\u4f7f\u7528\u60c5\u5883\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u53ef\u9078\u5b57\u6bb5\uff1a<\/strong>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u67d0\u4e9b\u5b57\u6bb5\u53ef\u80fd\u4e0d\u662f\u5fc5\u9700\u7684\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u8005\u7684\u4e2d\u9593\u540d\u53ef\u4ee5\u662fNULL\uff0c\u56e0\u70ba\u4e26\u975e\u6240\u6709\u4f7f\u7528\u8005\u90fd\u6709\u4e2d\u9593\u540d\u3002<\/li>\n<li><strong>\u7f3a\u5931\u6578\u64da\uff1a<\/strong>\u5728\u6578\u64da\u6536\u96c6\u904e\u7a0b\u4e2d\uff0c\u67d0\u4e9b\u6578\u64da\u53ef\u80fd\u56e0\u70ba\u5404\u7a2e\u539f\u56e0\u800c\u7f3a\u5931\uff0c\u4f8b\u5982\u7528\u6236\u672a\u586b\u5beb\u67d0\u4e9b\u8868\u55ae\u5b57\u6bb5\u3002<\/li>\n<li><strong>\u672a\u77e5\u503c\uff1a<\/strong>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u6578\u64da\u7684\u771f\u5be6\u503c\u53ef\u80fd\u5c1a\u672a\u78ba\u5b9a\uff0c\u4f8b\u5982\u65b0\u7522\u54c1\u7684\u50f9\u683c\u5c1a\u672a\u8a2d\u5b9a\u3002<\/li>\n<\/ul>\n<h2 id=\"null%e5%9c%a8%e4%b8%8d%e5%90%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b3%bb%e7%b5%b1%e4%b8%ad%e7%9a%84%e8%a1%a8%e7%a4%ba%e6%96%b9%e6%b3%95-fkFSMXtXWN\">NULL\u5728\u4e0d\u540c\u6578\u64da\u5eab\u7cfb\u7d71\u4e2d\u7684\u8868\u793a\u65b9\u6cd5<\/h2>\n<p>\u4e0d\u540c\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u5c0dNULL\u7684\u8655\u7406\u53ef\u80fd\u7565\u6709\u4e0d\u540c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u53ca\u5176\u5c0dNULL\u7684\u8868\u793a\u65b9\u6cd5\uff1a<\/p>\n<h3 id=\"1-mysql-fkFSMXtXWN\">1. MySQL<\/h3>\n<p>\u5728MySQL\u4e2d\uff0cNULL\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002\u7576\u5275\u5efa\u8868\u6642\uff0c\u53ef\u4ee5\u6307\u5b9a\u67d0\u500b\u5b57\u6bb5\u662f\u5426\u5141\u8a31NULL\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>CREATE TABLE users (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    name VARCHAR(100) NOT NULL,\n    middle_name VARCHAR(100) NULL\n);<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0cmiddle_name\u5b57\u6bb5\u5141\u8a31NULL\u503c\uff0c\u800cname\u5b57\u6bb5\u5247\u4e0d\u5141\u8a31\u3002<\/p>\n<h3 id=\"2-postgresql-fkFSMXtXWN\">2. PostgreSQL<\/h3>\n<p>PostgreSQL\u7684NULL\u8655\u7406\u8207MySQL\u985e\u4f3c\u3002\u53ef\u4ee5\u4f7f\u7528IS NULL\u548cIS NOT NULL\u4f86\u6aa2\u67e5NULL\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT * FROM users WHERE middle_name IS NULL;<\/code><\/pre>\n<h3 id=\"3-sql-server-fkFSMXtXWN\">3. SQL Server<\/h3>\n<p>\u5728SQL Server\u4e2d\uff0cNULL\u7684\u4f7f\u7528\u4e5f\u5f88\u666e\u904d\u3002\u53ef\u4ee5\u4f7f\u7528COALESCE\u51fd\u6578\u4f86\u8655\u7406NULL\u503c\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT COALESCE(middle_name, 'N\/A') AS middle_name FROM users;<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56demiddle_name\u7684\u503c\uff0c\u5982\u679c\u70baNULL\u5247\u8fd4\u56de&#8217;N\/A&#8217;\u3002<\/p>\n<h2 id=\"null%e7%9a%84%e6%9f%a5%e8%a9%a2%e8%88%87%e6%af%94%e8%bc%83-fkFSMXtXWN\">NULL\u7684\u67e5\u8a62\u8207\u6bd4\u8f03<\/h2>\n<p>\u5728\u9032\u884c\u67e5\u8a62\u6642\uff0cNULL\u7684\u6bd4\u8f03\u9700\u8981\u7279\u5225\u6ce8\u610f\u3002\u4f7f\u7528\u7b49\u865f\uff08=\uff09\u4f86\u6bd4\u8f03NULL\u662f\u4e0d\u6b63\u78ba\u7684\uff0c\u56e0\u70baNULL\u8868\u793a\u672a\u77e5\u3002\u56e0\u6b64\uff0c\u61c9\u4f7f\u7528IS NULL\u6216IS NOT NULL\u4f86\u9032\u884c\u67e5\u8a62\u3002<\/p>\n<pre><code>SELECT * FROM users WHERE middle_name IS NULL;<\/code><\/pre>\n<p>\u9019\u6a23\u7684\u67e5\u8a62\u5c07\u6b63\u78ba\u8fd4\u56de\u6240\u6709middle_name\u70baNULL\u7684\u8a18\u9304\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-fkFSMXtXWN\">\u7d50\u8ad6<\/h2>\n<p>NULL\u5728\u6578\u64da\u5eab\u4e2d\u662f\u4e00\u500b\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u6b63\u78ba\u7406\u89e3\u548c\u4f7f\u7528NULL\u5c0d\u65bc\u6578\u64da\u5eab\u7684\u8a2d\u8a08\u548c\u67e5\u8a62\u81f3\u95dc\u91cd\u8981\u3002\u7121\u8ad6\u662f\u5728MySQL\u3001PostgreSQL\u9084\u662fSQL Server\u4e2d\uff0cNULL\u7684\u8655\u7406\u65b9\u5f0f\u90fd\u6709\u5176\u7279\u5b9a\u7684\u898f\u5247\u548c\u65b9\u6cd5\u3002\u4e86\u89e3\u9019\u4e9b\u898f\u5247\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\uff0c\u4e26\u78ba\u4fdd\u6578\u64da\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u6578\u64da\u5eab\u7ba1\u7406\u6216\u5176\u4ed6\u6280\u8853\u554f\u984c\u6709\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4e86\u89e3\u66f4\u591a\u8cc7\u8a0a\uff0c\u7279\u5225\u662f\u6211\u5011\u7684<a href=\"https:\/\/server.hk\">VPS<\/a> \u65b9\u6848\uff0c\u63d0\u4f9b\u7a69\u5b9a\u7684\u6578\u64da\u5eab\u652f\u6301\u548c\u9ad8\u6548\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u8a0e\u6578\u64da\u5eab\u4e2dNULL\u7684\u8868\u793a\u65b9\u6cd5\uff0c\u4e86\u89e3\u5176\u5728\u6578\u64da\u7ba1\u7406\u4e2d\u7684\u91cd\u8981\u6027\u53ca\u5f71\u97ff\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u7684\u6e96\u78ba\u6027\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-171411","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=171411"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171411\/revisions"}],"predecessor-version":[{"id":171412,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171411\/revisions\/171412"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=171411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=171411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=171411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}