如何只允许一张桌子排? [英] How to allow only one row for a table?

查看:70
本文介绍了如何只允许一张桌子排?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表,我只想一个条目。因此,如果有人试图插入另一行,则只有在有人删除了先前存在的行之后,才应禁止该行。

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 的值是相同的。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆