在唯一的索引甲骨文上启用Novalidate

发布于 2025-02-02 13:17:00 字数 565 浏览 1 评论 0原文

在Oracle中,我想将此独特的约束添加到表格,并带有一些记录,这些记录与此独特的约束相关。为了在表中添加此独特的约束而没有验证表格上的先前数据,我在语句结束时启用了NovAlidate,但是它有一个错误:

 ORA-02158: invalid CREATE INDEX option

有什么方法可以在表中添加此唯一索引而不验证表格中的先前记录?

create unique index UK_SAME_THREAD ON T_THREADPARTICIPANT
    (case when C_OPPOSITE_USER_ID is not null then C_OPPOSITE_USER_ID else null end,
     case when C_OPPOSITE_USER_ID is not null then F_PARTICIPANT else null end,
     case when C_OPPOSITE_USER_ID is not null then C_CONTACT_TYPE else null end)
    ENABLE NOVALIDATE;

In oracle, I want to add this unique constraint to a table with some records that contracted this unique constraint. for adding this unique constraint without validating previous data on the table I added ENABLE NOVALIDATE at the end of the statement but it has an error:

 ORA-02158: invalid CREATE INDEX option

Is there any way to add this unique index without validating previous records in Table?

create unique index UK_SAME_THREAD ON T_THREADPARTICIPANT
    (case when C_OPPOSITE_USER_ID is not null then C_OPPOSITE_USER_ID else null end,
     case when C_OPPOSITE_USER_ID is not null then F_PARTICIPANT else null end,
     case when C_OPPOSITE_USER_ID is not null then C_CONTACT_TYPE else null end)
    ENABLE NOVALIDATE;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玻璃人 2025-02-09 13:17:01

解决方法是 a)创建索引(非唯一), b)创建不验证现有值的独特约束。

包含重复ID值的表:

SQL> select * From test;

        ID
----------
         1
         1
         2

这是您尝试执行的操作:

SQL> create unique index i1 on test (id) enable novalidate;
create unique index i1 on test (id) enable novalidate
                                    *
ERROR at line 1:
ORA-02158: invalid CREATE INDEX option

让我们只Alter Table并添加唯一约束(这也不起作用):

SQL> alter table test add constraint uk_id unique (id) enable novalidate;
alter table test add constraint uk_id unique (id) enable novalidate
                                *
ERROR at line 1:
ORA-02299: cannot validate (SCOTT.UK_ID) - duplicate keys found

So:创建索引 首先...

SQL> create index i1_test_id on test (id);

Index created.

...和更改表接下来:

SQL> alter table test add constraint uk_id unique (id) enable novalidate;

Table altered.

SQL>

它可以使用吗?

SQL> insert into test (id) values (2);
insert into test (id) values (2)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.UK_ID) violated


SQL> insert into test (id) values (3);

1 row created.

SQL>

我想是的。

Workaround is to a) create index (non-unique), b) create unique constraint that doesn't validate existing values.

Table that contains duplicate ID values:

SQL> select * From test;

        ID
----------
         1
         1
         2

This is what you tried to do:

SQL> create unique index i1 on test (id) enable novalidate;
create unique index i1 on test (id) enable novalidate
                                    *
ERROR at line 1:
ORA-02158: invalid CREATE INDEX option

Let's just alter table and add unique constraint (that won't work either):

SQL> alter table test add constraint uk_id unique (id) enable novalidate;
alter table test add constraint uk_id unique (id) enable novalidate
                                *
ERROR at line 1:
ORA-02299: cannot validate (SCOTT.UK_ID) - duplicate keys found

So: create index first ...

SQL> create index i1_test_id on test (id);

Index created.

... and alter the table next:

SQL> alter table test add constraint uk_id unique (id) enable novalidate;

Table altered.

SQL>

Does it work?

SQL> insert into test (id) values (2);
insert into test (id) values (2)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.UK_ID) violated


SQL> insert into test (id) values (3);

1 row created.

SQL>

I guess it does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文