【发布时间】:2020-05-16 13:22:54
【问题描述】:
我有一张这样的桌子:
CREATE TABLE schema.mytable
(
id serial NOT NULL,
number integer NOT NULL,
state boolean NOT NULL,
);
我需要创建一组唯一的“数字”,但状态列必须为真;如果状态栏为假,数字可以重复,这是我需要有效的示例:
id number state
1 123 true
2 124 true
3 125 true
4 123 false
5 129 false
如您所见,数字 123 重复了,但在一种情况下状态为假,另一种情况下为真;这是不正确的:
id number state
1 123 true
2 124 true
3 125 true
4 123 true (*incorrect)
5 129 false
另外,123 有可能重复两次或多次,状态为假;我怎样才能做到这一点?
【问题讨论】:
标签: sql postgresql database-design sql-insert unique-constraint