我正在使用 PostgreSQL 开发我的第一个应用程序。
场景
这是我的表“人”的样子:
Column | Type | Modifiers
------------+-----------------------------+-----------------------------------------------------
id | bigint | not null default nextval('person_id_seq'::regclass)
first_name | character varying(255) | not null
last_name | character varying(255) | not null
email | character varying(255) | not null
password | character varying(255) | not null
created_at | timestamp without time zone |
updated_at | timestamp without time zone |
Indexes:
"person_pkey" PRIMARY KEY, btree (id)
"person_email_unique" UNIQUE CONSTRAINT, btree (email)
"person_id_unique" UNIQUE CONSTRAINT, btree (id)
Referenced by:
TABLE "access" CONSTRAINT "access_person_id_foreign" FOREIGN KEY (person_id) REFERENCES person(id)
这是使用 knex.schema 中的迁移创建的.
如果我在 psql 中运行以下查询...
insert into person (first_name, last_name, email, password) values ('Max', 'Mustermann', '', '123123123');
我返回 INSERT 0 1
并成功插入该行:
id | first_name | last_name | email | password | created_at | updated_at
----+------------+------------+-----------------------------------+----------------+-------------------------+-------------------------
12 | Max | Mustermann | | 123123123 | |
我的问题:
我预计操作会失败,因为没有指定电子邮件(NOT NULL)。为什么它不会失败?
非常感谢您的帮助!
最大
最佳答案
某些 DBMS(如 Oracle)将空字符串 (''
) 视为 NULL
。其他(如 MySQL、PostgreSQL 等)将空字符串和 NULL
视为不同。
PostgreSQL 将 ''
视为空字符串,而不是 NULL
,因此您的 insert
语句已成功执行。
关于sql - PostgreSQL:为什么 NOT NULL 在这里不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23353595/