sql - PostgreSQL 在 alter table 上添加 EXCLUDE 约束

标签 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 语法是什么?

最佳答案

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

首先添加列:

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 &&
  )

关于sql - PostgreSQL 在 alter table 上添加 EXCLUDE 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47352108/

相关文章:

sql - 大表中行之间的时间差

MySQL 查询获取属于某个组织的所有用户

sql - 如何从日期期间获取特定的工作日?

postgresql - 无法将 PPGool 与 Amazon RDS Postgres 一起使用

sql - 如何在 Rails 4 中构建跨多个表的高效选择命令?

php - 如何在 Laravel 中使用 order by array 选择或排序

mysql - SQL错误: 1191 Can't find FULLTEXT index matching the column list

postgresql - 在 Talend 中使用 Postgres "copy"命令将表内容加载到 CSV 文件中

postgresql - 我需要一个函数来从表中选择 88 个随机行(不重复)

database - 数据类型字符变化没有访问方法 "gist"的默认运算符类