只允许一行的 parent_id 为 NULL:
CREATE TABLE simple21_page (
id integer NOT NULL,
name character varying(120) NOT NULL,
text text NOT NULL,
parent_id integer
);
Run Code Online (Sandbox Code Playgroud)
这是一棵树,应该正好有一个根节点。
我试过这个,但它不起作用:
create unique index on simple21_page (parent_id) where parent_id is null;
Run Code Online (Sandbox Code Playgroud)
这是否可以通过约束或唯一索引实现,或者是否需要触发器?
你快到了。要获得单例,您需要对常量值进行唯一约束:
CREATE UNIQUE INDEX ON simple21_page ((1)) WHERE parent_id IS NULL;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
73 次 |
| 最近记录: |