唯一空索引
Unique null index
我有一个table
CREATE TABLE mandantconfigentity
(
id bigint NOT NULL,
currentlegalversionnumber integer NOT NULL DEFAULT 5,
belongsto_userid character varying(255)
)
我如何创建唯一索引,以确保 table 只能有一行 belongsto_userid 为空?
这不起作用:
CREATE UNIQUE INDEX unique_mandantconfig_for_null_belongsto
ON mandantconfigentity (1)
WHERE (belongsto_userid IS NULL);
创建索引的代码需要列名:
CREATE UNIQUE INDEX unique_mandantconfig_for_null_belongsto
ON mandantconfigentity (id)
WHERE (belongsto_userid IS NULL);
CREATE UNIQUE INDEX unique_mandantconfig_for_null_belongsto
ON mandantconfigentity ((1))
WHERE (belongsto_userid IS NULL);
那么它做了什么 - 它在表达式上创建了一个部分索引:对于 belongsto_userid IS NULL
.
的每一行都有明确的固定值 1
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.
因此语法需要额外的括号。
参考文献:
我有一个table
CREATE TABLE mandantconfigentity
(
id bigint NOT NULL,
currentlegalversionnumber integer NOT NULL DEFAULT 5,
belongsto_userid character varying(255)
)
我如何创建唯一索引,以确保 table 只能有一行 belongsto_userid 为空?
这不起作用:
CREATE UNIQUE INDEX unique_mandantconfig_for_null_belongsto
ON mandantconfigentity (1)
WHERE (belongsto_userid IS NULL);
创建索引的代码需要列名:
CREATE UNIQUE INDEX unique_mandantconfig_for_null_belongsto
ON mandantconfigentity (id)
WHERE (belongsto_userid IS NULL);
CREATE UNIQUE INDEX unique_mandantconfig_for_null_belongsto
ON mandantconfigentity ((1))
WHERE (belongsto_userid IS NULL);
那么它做了什么 - 它在表达式上创建了一个部分索引:对于 belongsto_userid IS NULL
.
1
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.
因此语法需要额外的括号。
参考文献: