如何只允许一张桌子排? [英] How to allow only one row for a table?
问题描述
我有一张表,我只想一个条目。因此,如果有人试图插入另一行,则只有在有人删除了先前存在的行之后,才应禁止该行。
I have one table in which I would like only one entry. So if someone is trying to insert another row it shouldn't be allowed, only after someone deleted the previously existing row.
如何为这样的表设置规则?
How do I set a rule for a table like this?
推荐答案
一个 UNIQUE
约束允许具有 NULL
值的多行,因为两个 NULL $永远不会认为c $ c>的值是相同的。
A UNIQUE
constraint allows multiple rows with NULL
values, because two NULL
values are never considered to be the same.
类似的考虑也适用于 CHECK
约束。它们允许表达式为 TRUE
或 NULL
(不是 FALSE
)。再次, NULL
值通过检查。
Similar considerations apply to CHECK
constraints. They allow the expression to be TRUE
or NULL
(just not FALSE
). Again, NULL
values get past the check.
要排除这一点,列必须被定义为不为空
。或者将其设为 PRIMARY KEY
,因为PK列已自动定义为 NOT NULL
。详细信息:
To rule that out, the column must be defined NOT NULL
. Or make it the PRIMARY KEY
since PK columns are defined NOT NULL
automatically. Details:
- Why can I create a table with PRIMARY KEY on a nullable column?
此外,只需使用布尔值
:
CREATE TABLE onerow (
onerow_id bool PRIMARY KEY DEFAULT TRUE
, data text
, CONSTRAINT onerow_uni CHECK (onerow_id)
);
CHECK
约束对于 boolean
列。仅允许 TRUE
。
The CHECK
constraint can be that simple for a boolean
column. Only TRUE
is allowed.
这篇关于如何只允许一张桌子排?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!