francs=> create table test_unique (id int4,name varchar(32)); CREATE TABLE
francs=> create unique index idx_test_unique_id on test_unique using btree (id); CREATE INDEX
francs=> /d test_unique; Table "francs.test_unique" Column | Type | Modifiers --------+-----------------------+----------- id | integer | name | character varying(32) | Indexes: "idx_test_unique_id" UNIQUE, btree (id)
1.2 重复插入数据 1
1 2 3 4 5
francs=> insert into test_unique (id ,name )values (1,'a'); INSERT 0 1 francs=> insert into test_unique (id ,name )values (1,'a'); ERROR: duplicate key value violates unique constraint "idx_test_unique_id" DETAIL: Key (id)=(1) already exists.
1.3 重复插入 null 值
1 2 3 4 5 6 7 8 9 10 11
francs=> insertinto test_unique (id ,name )values (null,'b'); INSERT01 francs=> insertinto test_unique (id ,name )values (null,'b'); INSERT01 francs=> select * from test_unique; id | name ----+------ 1 | a | b | b (3 rows)