根据我对文档的理解,以下定义是等价的:
create table foo (
id serial primary key,
code integer,
label text,
constraint foo_uq unique (code, label));
create table foo (
id serial primary key,
code integer,
label text);
create unique index foo_idx on foo using btree (code, label);
然而,Postgres 9.4手册中的注释说:
向表中添加唯一约束的首选方法是ALTER table…添加约束。使用索引来强制惟一的约束
可以被认为是一个不应该被认为是实现细节的实现吗
直接访问。
(编辑:此说明已从Postgres 9.5的手册中删除。)
仅仅是风格的问题吗?选择其中一个变量的实际后果是什么(例如在性能方面)?
有一件非常小的事情,只能使用约束而不能使用索引,那就是使用ON冲突ON约束子句(另见此问题)。
这行不通:
CREATE TABLE T (a INT PRIMARY KEY, b INT, c INT);
CREATE UNIQUE INDEX u ON t(b);
INSERT INTO T (a, b, c)
VALUES (1, 2, 3)
ON CONFLICT ON CONSTRAINT u
DO UPDATE SET c = 4
RETURNING *;
它产生:
[42704]: ERROR: constraint "u" for table "t" does not exist
将索引转换为约束:
DROP INDEX u;
ALTER TABLE t ADD CONSTRAINT u UNIQUE (b);
INSERT语句现在可以工作了。
SELECT a.phone_number,count(*) FROM public.users a
Group BY phone_number Having count(*)>1;
SELECT a.phone_number,count(*) FROM public.retailers a
Group BY phone_number Having count(*)>1;
select a.phone_number from users a inner join users b
on a.id <> b.id and a.phone_number = b.phone_number order by a.id;
select a.phone_number from retailers a inner join retailers b
on a.id <> b.id and a.phone_number = b.phone_number order by a.id
DELETE FROM
users a
USING users b
WHERE
a.id > b.id
AND a.phone_number = b.phone_number;
DELETE FROM
retailers a
USING retailers b
WHERE
a.id > b.id
AND a.phone_number = b.phone_number;
CREATE UNIQUE INDEX CONCURRENTLY users_phone_number
ON users (phone_number);
验证:
insert into users(name,phone_number,created_at,updated_at) select name,phone_number,created_at,updated_at from users