【问题标题】:Check constraint before insert a value into the database在将值插入数据库之前检查约束
【发布时间】:2020-03-24 15:23:07
【问题描述】:

我正在尝试创建一个表来存储 order_details。该表有一个列 status,每个 order_id 只允许包含一次值 true,但可能包含值 false每个order_id 多次。

create table order_details (
    id serial, 
    order_id integer not null, 
    status boolean not null
);

我想在表结构本身内执行此操作。任何帮助,将不胜感激。提前致谢。

【问题讨论】:

    标签: sql postgresql unique-constraint check-constraints


    【解决方案1】:

    您可以创建过滤的唯一索引:

    create unique index on order_details(order_id)
    where status;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多