PostgreSQL unique constraint allow mutiple nulls
发布时间:2020-12-13 17:20:36 所属栏目:百科 来源:网络整理
导读:1. 建表 postgres=# create table tb8(id integer,name character varying);CREATE TABLE 2. 添加唯一约束 postgres=# alter table tb8 add CONSTRAINT check_name_unique unique(name);ALTER TABLE postgres=# d tb8 Table "public.tb8" Column | Type | M
1. 建表 postgres=# create table tb8(id integer,name character varying); CREATE TABLE2. 添加唯一约束 postgres=# alter table tb8 add CONSTRAINT check_name_unique unique(name); ALTER TABLE postgres=# d tb8 Table "public.tb8" Column | Type | Modifiers --------+-------------------+----------- id | integer | name | character varying | Indexes: "check_name_unique" UNIQUE CONSTRAINT,btree (name)3. 插入数据 postgres=# insert into tb8(id) select generate_series(1,5); INSERT 0 5 postgres=# insert into tb8 select 6,'john'; INSERT 0 1. postgres=# insert into tb8 select 7,'john'; ERROR: duplicate key value violates unique constraint "check_name_unique" DETAIL: Key (name)=(john) already exists. 4. 查看数据: postgres=# select * from tb8; id | name ----+------ 1 | 2 | 3 | 4 | 5 | 6 | john (6 rows) 唯一字段name上有多个null,null是不确定类型,即null != null,null != not null.
5. 使用distinct关键字的时候判定多个null是相等的 postgres=# select distinct name from tb8; name ------ john (2 rows) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |