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)原文链接:https://www.f2er.com/postgresql/195285.html