PostgreSQL在alter table上添加EXCLUDE约束

时间:2017-11-17 13:48:01

标签: sql postgresql

我试图将一个EXCLUDE约束添加到我现有的一个表中,这是我正在运行的SQL:

ALTER TABLE appointment_service
  ADD COLUMN start_time bigint,
  ADD COLUMN end_time bigint,
  EXCLUDE USING gist (
            professional_id WITH =,
            int8range(start_time, end_time) WITH &&
  ),
  ADD COLUMN professional_id text REFERENCES professionals ON DELETE CASCADE ON UPDATE CASCADE

这就是我得到的错误。

ERROR:  syntax error at or near "EXCLUDE"
LINE 4:   EXCLUDE USING gist (

实现此目的的正确SQL语法是什么?

1 个答案:

答案 0 :(得分:3)

您需要单独添加排除约束。

首先添加列:

ALTER TABLE appointment_service
  ADD COLUMN start_time bigint,
  ADD COLUMN end_time bigint,
  ADD COLUMN professional_id text 
     REFERENCES professionals ON DELETE CASCADE ON UPDATE CASCADE;

然后添加约束:

ALTER TABLE appointment_service
  add constraint unique_professional_id
  EXCLUDE USING gist (
            professional_id WITH =,
            int8range(start_time, end_time) WITH &&
  )