我可以在 Oracle 中拥有可延迟的唯一功能索引吗?
我想在 Oracle 10g 中创建一个可延迟的唯一功能索引。
我知道如何创建唯一的功能索引:
create unique index LIST_ITEM_ENTRY_NO_UNIQ
on LIST_ITEM (case status when 'cancel' then null else LIST_KEY end,
case status when 'cancel' then null else ENTRY_NO end);
我知道如何创建可延迟的唯一索引:
alter table LIST_ITEM add constraint LIST_ITEM_ENTRY_NO_UNIQ
unique (LIST_KEY,ENTRY_NO) deferrable initially deferred;
知道这两件事后,我尝试了以下操作:
alter table LIST_ITEM add constraint LIST_ITEM_ENTRY_NO_UNIQ
unique (case STATUS when 'cancel' then null else LIST_KEY end,
case STATUS when 'cancel' then null else ENTRY_NO end)
deferrable initially deferred;
但我收到“ORA-00904:无效标识符”错误。 要么是我的语法错误,要么是 Oracle 不支持可延迟函数索引? 有人可以为我提供解决方案或明确的答案吗?
I'd like to create a deferrable unique functional index in Oracle 10g.
I know how to create a unique functional index:
create unique index LIST_ITEM_ENTRY_NO_UNIQ
on LIST_ITEM (case status when 'cancel' then null else LIST_KEY end,
case status when 'cancel' then null else ENTRY_NO end);
I know how to create a deferrable unique index:
alter table LIST_ITEM add constraint LIST_ITEM_ENTRY_NO_UNIQ
unique (LIST_KEY,ENTRY_NO) deferrable initially deferred;
Knowing these two things, I tried this:
alter table LIST_ITEM add constraint LIST_ITEM_ENTRY_NO_UNIQ
unique (case STATUS when 'cancel' then null else LIST_KEY end,
case STATUS when 'cancel' then null else ENTRY_NO end)
deferrable initially deferred;
But I get an "ORA-00904 : invalid identifier" error. Either I've got the syntax wrong, or perhaps Oracle doesn't support deferrable functional indices? Could someone provide me with a solution or else a definitive answer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不错的尝试,但根据 Oracle 10g 文档,CREATE INDEX 和 ALTER TABLE ADD CONSTRAINT 的语法在这方面不可互换,这就是您收到语法错误的原因:
因此 CREATE INDEX 允许 column_expression,这基本上是一个“函数-为基础的索引”。
另一方面:
因此UNIQUE约束定义只能是列名,而不能是列表达式。
您可以在 11g 中使用虚拟列来完成此操作,在 10g 及更早版本中,大多数人倾向于创建派生列(以及以编程方式保持它们最新的负担)。
Nice try, but according to the Oracle 10g Documentation, the syntax for CREATE INDEX and ALTER TABLE ADD CONSTRAINT are not interchangeable in this regard, which is why you got that syntax error:
Therefore CREATE INDEX allows column_expression, which is basically a "function-based index".
On the other hand:
Therefore a UNIQUE constraint definition may only be column names, and cannot be column expressions.
You can do this in 11g using virtual columns, in 10g and earlier most people tend to create derived columns (along with the burden of keeping them up-to-date programmatically).
我认为您需要 11g 虚拟列 功能。 您可以将该函数创建为虚拟列,然后在其上添加约束。
I think you need the 11g virtual columns functionality. You'd create the function as a virtual column, then add the cosntraint on that.
询问 Tom 地址这个问题。 答案是否定的,您不能根据功能索引创建唯一约束。
Ask Tom addresses this issue. The answer is no, you can't create unique constraints based upon functional indexes.