oracle 如何约束数据库表,以便在一列中只有一行可以具有特定值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/182262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to constrain a database table so only one row can have a particular value in a column?
提问by Trigger
Using Oracle, if a column value can be 'YES' or 'NO' is it possible to constrain a table so that only one row can have a 'YES' value?
使用 Oracle,如果列值可以是“是”或“否”,是否可以限制表,以便只有一行可以具有“是”值?
I would rather redesign the table structure but this is not possible.
我宁愿重新设计表结构,但这是不可能的。
[UDPATE] Sadly, null values are not allowed in this table.
[UDPATE] 遗憾的是,此表中不允许使用空值。
回答by Tony Andrews
Use a function-based index:
使用基于函数的索引:
create unique index only_one_yes on mytable
(case when col='YES' then 'YES' end);
Oracle only indexes keys that are not completely null, and the CASE expression here ensures that all the 'NO' values are changed to nulls and so not indexed.
Oracle 只索引不完全为空的键,这里的 CASE 表达式确保所有“NO”值都更改为空值,因此不会被索引。
回答by yukondude
This is a kludgy hack, but if the column allows NULLs, then you could use NULL in place of "NO" and use "YES" just as before. Apply a unique key constraint to that column, and you'll never get two "YES" values, but still have many NOs.
这是一个笨拙的技巧,但如果该列允许 NULL,那么您可以使用 NULL 代替“NO”并像以前一样使用“YES”。对该列应用唯一键约束,您将永远不会得到两个“YES”值,但仍然有很多 NO。
Update: @Nick Pierpoint: suggested adding a check constraint so that the column values are restricted to just "YES" and NULL. The syntax is all worked out in his answer.
更新:@Nick Pierpoint:建议添加检查约束,以便将列值限制为“YES”和 NULL。语法都在他的回答中解决了。
回答by Guy
You will want to check a Tom Kyte article with exactly this question being asked and his answer:
您将需要查看 Tom Kyte 的一篇文章,其中提出了这个问题以及他的回答:
http://tkyte.blogspot.com/2008/05/another-of-day.html
http://tkyte.blogspot.com/2008/05/another-of-day.html
Summary: don't use triggers, don't use autonomous transactions, use two tables.
总结:不要使用触发器,不要使用自治事务,使用两个表。
If you use an Oracle database, then you MUST get to know AskTomand get his books.
如果您使用 Oracle 数据库,那么您必须了解AskTom并获得他的书籍。
回答by poezn
It doesn't work on the table definition.
它不适用于表定义。
However, if you update the table using a trigger calling a stored procedure, you could make sure that only one row contains "YES".
但是,如果您使用触发器调用存储过程来更新表,则可以确保只有一行包含“YES”。
- Set all rows to "NO"
- Set the row you want to YES
- 将所有行设置为“NO”
- 将您想要的行设置为 YES
回答by Nick Pierpoint
Following on from my comment to a previous answer by yukondude, I'd add a unique index and a check constraint:
继我对 yukondude 先前回答的评论之后,我将添加一个唯一索引和一个检查约束:
create table mytest (
yesorno varchar2(3 char)
);
create unique index uk_mytest_yesorno on mytest(yesorno);
alter table mytest add constraint ck_mytest_yesorno check (yesorno is null or yesorno = 'YES');
回答by Matthias Meid
Does Oracle support something like filtered indices(last week I heard that e.g. MSSQL2008 does)? Maybe you can define a unique keywhich applies only to rows with the value "Yes" in your column.
Oracle 是否支持过滤索引之类的东西(上周我听说 MSSQL2008 支持)?也许您可以定义一个唯一键,该键仅适用于列中值为“是”的行。
回答by Rimas Kudelis
I guess I'd use a second table to point to the appropriate row in your current table. That other table could be used to store values of other variables too too.
我想我会使用第二个表来指向当前表中的相应行。另一个表也可以用来存储其他变量的值。