我有一张桌子
CREATE TABLE mandantconfigentity
(
id bigint NOT NULL,
currentlegalversionnumber integer NOT NULL DEFAULT 5,
belongsto_userid character varying(255)
)
如何创建一个唯一索引,以确保该表只能有一行其中belongssto_userid为null?
这不起作用:
CREATE UNIQUE INDEX unique_mandantconfig_for_null_belongsto
ON mandantconfigentity (1)
WHERE (belongsto_userid IS NULL);
答案 0 :(得分:3)
CREATE UNIQUE INDEX unique_mandantconfig_for_null_belongsto
ON mandantconfigentity ((1))
WHERE (belongsto_userid IS NULL);
So what it does - it creates a partial index over expression: that has explicitly the fixed value 1
for every row where belongsto_userid IS NULL
.
The syntax of the CREATE INDEX command normally requires writing parentheses around index expressions, as shown in the second example. The parentheses can be omitted when the expression is just a function call, as in the first example.
So extra parentheses are required by the syntax.
References:
答案 1 :(得分:0)
The code to create index needs column name:
CREATE UNIQUE INDEX unique_mandantconfig_for_null_belongsto
ON mandantconfigentity (id)
WHERE (belongsto_userid IS NULL);