【发布时间】:2020-12-08 20:16:47
【问题描述】:
我试图理解为什么某些 Postgres 约束可以在不同的表中命名相同,而有些则不能
这里是一个简单的例子:
drop table if exists public.table_1;
drop table if exists public.table_2;
CREATE TABLE public.table_1 (
id serial NOT NULL,
date_start date NOT NULL,
date_end date NULL
);
CREATE TABLE public.table_2 (
id serial NOT NULL,
date_start date NOT NULL,
date_end date NULL
);
alter table public.table_1 add constraint my_constraint_1 check (date_start > now());
alter table public.table_2 add constraint my_constraint_1 check (date_start > now());
alter table public.table_1 add constraint my_constraint_2 EXCLUDE USING gist (daterange(date_start, coalesce(date_end, 'infinity'), '[]') WITH &&);
alter table public.table_2 add constraint my_constraint_2 EXCLUDE USING gist (daterange(date_start, coalesce(date_end, 'infinity'), '[]') WITH &&);
如您所见,我可以在不同的表中使用相同的名称my_constraint_1
为什么名称my_constraint_1可以在不同的表中使用相同的名称,而my_constraint_2必须是唯一的,否则我会收到错误 Errore SQL [42P07]: ERROR: relation "my_constraint_2" already exists?
【问题讨论】:
标签: postgresql constraints exclude-constraint