{"id":2660,"date":"2025-03-12T23:35:30","date_gmt":"2025-03-12T15:35:30","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2660"},"modified":"2025-03-12T23:35:30","modified_gmt":"2025-03-12T15:35:30","slug":"postgresql-null-%e5%80%bc%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/12\/postgresql-null-%e5%80%bc%e8%af%a6%e8%a7%a3\/","title":{"rendered":"PostgreSQL NULL \u503c\u8be6\u89e3"},"content":{"rendered":"\n<p>\u5728 PostgreSQL \u4e2d\uff0c<code>NULL<\/code> \u4ee3\u8868 <strong>\u672a\u77e5\u503c\u6216\u7f3a\u5931\u503c<\/strong>\uff0c\u800c\u4e0d\u662f\u7a7a\u5b57\u7b26\u4e32 (<code>''<\/code>) \u6216 <code>0<\/code>\u3002\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\uff0c<code>NULL<\/code> \u9700\u8981\u7279\u6b8a\u5904\u7406\uff0c\u56e0\u4e3a\u5b83\u7684\u884c\u4e3a\u4e0e\u666e\u901a\u503c\u4e0d\u540c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. NULL \u7684\u57fa\u672c\u6982\u5ff5<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>NULL<\/code> <strong>\u4e0d\u662f<\/strong> \u4e00\u4e2a\u5177\u4f53\u7684\u503c\uff0c\u800c\u662f\u201c\u6ca1\u6709\u503c\u201d\u3002<\/li>\n\n\n\n<li>\u4e0d\u80fd\u7528 <code>=<\/code> \u6216 <code>!=<\/code> \u6bd4\u8f83 <code>NULL<\/code>\uff0c\u9700\u8981\u4f7f\u7528 <code>IS NULL<\/code> \u6216 <code>IS NOT NULL<\/code> \u8fdb\u884c\u5224\u65ad\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT NULL = NULL;  -- \u7ed3\u679c\uff1aNULL\uff08\u4e0d\u662f TRUE \u4e5f\u4e0d\u662f FALSE\uff09\nSELECT NULL &lt;&gt; NULL; -- \u7ed3\u679c\uff1aNULL\nSELECT NULL IS NULL; -- \u7ed3\u679c\uff1aTRUE\nSELECT NULL IS NOT NULL; -- \u7ed3\u679c\uff1aFALSE<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u5728\u8868\u4e2d\u4f7f\u7528 NULL<\/strong><\/h2>\n\n\n\n<p>\u521b\u5efa\u5305\u542b <code>NULL<\/code> \u503c\u7684\u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE students (\n    id SERIAL PRIMARY KEY,\n    name VARCHAR(100),\n    age INTEGER\n);<\/code><\/pre>\n\n\n\n<p>\u63d2\u5165\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO students (name, age) VALUES ('Alice', 20);\nINSERT INTO students (name, age) VALUES ('Bob', NULL);\nINSERT INTO students (name, age) VALUES ('Charlie', 25);<\/code><\/pre>\n\n\n\n<p>\u67e5\u8be2\u5305\u542b <code>NULL<\/code> \u7684\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM students;<\/code><\/pre>\n\n\n\n<p><strong>\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>id<\/th><th>name<\/th><th>age<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Alice<\/td><td>20<\/td><\/tr><tr><td>2<\/td><td>Bob<\/td><td>NULL<\/td><\/tr><tr><td>3<\/td><td>Charlie<\/td><td>25<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. NULL \u7684\u6bd4\u8f83\u4e0e\u5224\u65ad<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>(1) IS NULL \/ IS NOT NULL<\/strong><\/h3>\n\n\n\n<p>\u7528\u6765\u68c0\u67e5 <code>NULL<\/code> \u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM students WHERE age IS NULL;<\/code><\/pre>\n\n\n\n<p><strong>\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>id<\/th><th>name<\/th><th>age<\/th><\/tr><\/thead><tbody><tr><td>2<\/td><td>Bob<\/td><td>NULL<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5982\u679c\u60f3\u67e5\u8be2 <strong>\u975e NULL<\/strong> \u7684\u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM students WHERE age IS NOT NULL;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>(2) COALESCE()<\/strong><\/h3>\n\n\n\n<p><code>COALESCE()<\/code> \u51fd\u6570\u7528\u4e8e <strong>\u66ff\u6362 NULL<\/strong> \u503c\uff0c\u8fd4\u56de\u7b2c\u4e00\u4e2a\u975e <code>NULL<\/code> \u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, COALESCE(age, 18) AS age FROM students;<\/code><\/pre>\n\n\n\n<p><strong>\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>name<\/th><th>age<\/th><\/tr><\/thead><tbody><tr><td>Alice<\/td><td>20<\/td><\/tr><tr><td>Bob<\/td><td>18<\/td><\/tr><tr><td>Charlie<\/td><td>25<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>(3) NULLIF()<\/strong><\/h3>\n\n\n\n<p><code>NULLIF(value1, value2)<\/code>\uff1a\u5982\u679c <code>value1 = value2<\/code>\uff0c\u8fd4\u56de <code>NULL<\/code>\uff0c\u5426\u5219\u8fd4\u56de <code>value1<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT NULLIF(10, 10); -- \u7ed3\u679c\uff1aNULL\nSELECT NULLIF(10, 20); -- \u7ed3\u679c\uff1a10<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. NULL \u5728\u8ba1\u7b97\u4e2d\u7684\u5f71\u54cd<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>NULL<\/code> \u53c2\u4e0e\u8ba1\u7b97\u65f6\uff0c\u7ed3\u679c\u4ecd\u4e3a <code>NULL<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT 10 + NULL; -- \u7ed3\u679c\uff1aNULL\nSELECT NULL * 5;  -- \u7ed3\u679c\uff1aNULL<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>COALESCE()<\/code> \u907f\u514d NULL \u5f71\u54cd\u8ba1\u7b97<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, COALESCE(age, 0) * 2 AS double_age FROM students;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. NULL \u5728\u805a\u5408\u51fd\u6570\u4e2d\u7684\u5f71\u54cd<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>COUNT(*)<\/code> \u8ba1\u7b97\u6240\u6709\u884c\uff0c\u5305\u62ec <code>NULL<\/code> \u503c\u3002<\/li>\n\n\n\n<li><code>COUNT(column)<\/code> <strong>\u4e0d\u8ba1\u7b97<\/strong> <code>NULL<\/code> \u503c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT COUNT(*) FROM students;     -- \u7ed3\u679c\uff1a3\nSELECT COUNT(age) FROM students;   -- \u7ed3\u679c\uff1a2<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5176\u4ed6\u805a\u5408\u51fd\u6570\u5982 <code>SUM()<\/code>\u3001<code>AVG()<\/code>\u3001<code>MIN()<\/code>\u3001<code>MAX()<\/code> \u4e5f\u4f1a\u5ffd\u7565 <code>NULL<\/code> \u503c\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT AVG(age) FROM students;  -- \u7ed3\u679c\uff1a(20+25)\/2 = 22.5\nSELECT SUM(age) FROM students;  -- \u7ed3\u679c\uff1a45<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. NULL \u5728 ORDER BY \u6392\u5e8f\u4e2d\u7684\u5f71\u54cd<\/strong><\/h2>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>NULL<\/code> \u503c\u5728 <strong><code>ORDER BY ASC<\/code> \u65f6\u6392\u5728\u6700\u540e<\/strong>\uff0c\u5728 <strong><code>ORDER BY DESC<\/code> \u65f6\u6392\u5728\u6700\u524d<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM students ORDER BY age ASC;<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u60f3\u8ba9 <code>NULL<\/code> \u503c\u6392\u5728\u6700\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>NULLS FIRST<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM students ORDER BY age ASC NULLS FIRST;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. NULL \u5728 JOIN \u67e5\u8be2\u4e2d\u7684\u5f71\u54cd<\/strong><\/h2>\n\n\n\n<p>\u5728 <code>JOIN<\/code> \u64cd\u4f5c\u4e2d\uff0c<code>NULL<\/code> \u53ef\u80fd\u4f1a\u5f71\u54cd\u5339\u914d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE courses (\n    student_name VARCHAR(100),\n    course VARCHAR(50)\n);\n\nINSERT INTO courses VALUES ('Alice', 'Math'), ('Charlie', 'Physics');\n\nSELECT students.name, students.age, courses.course\nFROM students\nLEFT JOIN courses ON students.name = courses.student_name;<\/code><\/pre>\n\n\n\n<p><strong>\u7ed3\u679c<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>name<\/th><th>age<\/th><th>course<\/th><\/tr><\/thead><tbody><tr><td>Alice<\/td><td>20<\/td><td>Math<\/td><\/tr><tr><td>Bob<\/td><td>NULL<\/td><td>NULL<\/td><\/tr><tr><td>Charlie<\/td><td>25<\/td><td>Physics<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. NULL \u5728 CHECK \u7ea6\u675f\u4e2d\u7684\u5904\u7406<\/strong><\/h2>\n\n\n\n<p>\u521b\u5efa\u8868\u65f6\u53ef\u4ee5\u4f7f\u7528 <code>CHECK<\/code> \u9650\u5236 <code>NULL<\/code> \u5904\u7406\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE employees (\n    id SERIAL PRIMARY KEY,\n    name VARCHAR(100) NOT NULL,\n    salary INTEGER CHECK (salary &gt; 0)\n);<\/code><\/pre>\n\n\n\n<p>\u5982\u679c <code>salary<\/code> \u63d2\u5165 <code>NULL<\/code>\uff0c\u4e0d\u4f1a\u89e6\u53d1 <code>CHECK<\/code> \u7ea6\u675f\uff0c\u56e0\u4e3a <code>NULL<\/code> \u4e0d\u662f <code>&gt; 0<\/code> <strong>\u4e5f\u4e0d\u662f<\/strong> <code>&lt;= 0<\/code>\u3002<\/p>\n\n\n\n<p>\u8981\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u9700\u8981 <code>salary<\/code> \u8bbe\u7f6e <code>NOT NULL<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE employees (\n    id SERIAL PRIMARY KEY,\n    name VARCHAR(100) NOT NULL,\n    salary INTEGER NOT NULL CHECK (salary &gt; 0)\n);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9. \u5904\u7406 NULL \u7684\u6700\u4f73\u5b9e\u8df5<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>COALESCE()<\/code> \u66ff\u6362 <code>NULL<\/code><\/strong>\uff0c\u907f\u514d <code>NULL<\/code> \u5f71\u54cd\u8ba1\u7b97\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528 <code>IS NULL<\/code> \/ <code>IS NOT NULL<\/code> \u8fdb\u884c\u5224\u65ad<\/strong>\uff0c\u4e0d\u8981\u7528 <code>=<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u5728 <code>ORDER BY<\/code> \u65f6\u6ce8\u610f <code>NULL<\/code> \u7684\u6392\u5e8f\u89c4\u5219<\/strong>\u3002<\/li>\n\n\n\n<li><strong>\u5728 <code>JOIN<\/code> \u67e5\u8be2\u4e2d\u8981\u8003\u8651 <code>NULL<\/code> \u53ef\u80fd\u5f71\u54cd\u5339\u914d\u7ed3\u679c<\/strong>\u3002<\/li>\n\n\n\n<li><strong>\u5728 <code>CHECK<\/code> \u7ea6\u675f\u4e2d\u8981\u6ce8\u610f <code>NULL<\/code> \u7684\u7279\u6b8a\u60c5\u51b5<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PostgreSQL \u4e2d\uff0cNULL \u4ee3\u8868 \u672a\u77e5\u503c\u6216\u7f3a\u5931\u503c\uff0c\u800c\u4e0d\u662f\u7a7a\u5b57\u7b26\u4e32 (&#8221;) \u6216 0\u3002\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2661,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-2660","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2660"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2660\/revisions"}],"predecessor-version":[{"id":2662,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2660\/revisions\/2662"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2661"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}