gpt4 book ai didi

sql - 删除唯一约束不起作用

转载 作者:行者123 更新时间:2023-12-04 16:16:58 24 4
gpt4 key购买 nike

我试图在我的数据库中删除一个唯一的约束:

ALTER TABLE MyDbAdmin.myTable
DROP UNIQUE (myCol);

控制台说 drop 有效,但是当我尝试插入重复的记录时 myCol , ORA-00001: unique constraint错误被返回。

我尝试查看表的约束页面,唯一约束确实消失了。此外,如果我运行相同的 SQL 再次删除约束,它会返回 ORA-02442: Cannot drop nonexistent unique key .

上述查询是使用帐户 myDbUser 运行的,这是造成上述奇怪行为的原因吗?

最佳答案

也许您的唯一索引是在创建约束之前创建的:

create table t(col1 number);
create unique index t_idx on t(col1);
alter table t add constraint t_unique unique(col1);
insert into t values(1);
--> 1 row created
insert into t values(1);
--> ORA-00001: unique constraint (TEST.T_UNIQUE) violated
alter table t drop unique (col1);
insert into t values(1);
--> ORA-00001: unique constraint (TEST.T_IDX) violated

虽然 user_constraints 中没有出现唯一索引 T_IDX,但它会显示在错误消息中。

如果索引是作为“更改表...添加约束”的一部分在内部创建的,那么您可以在删除约束后插入重复项,因为支持约束的索引与约束一起删除。因此,如果没有“创建唯一索引”,代码将按预期工作:
create table t(col1 number);
-- create unique index t_idx on t(col1);
alter table t add constraint t_unique unique(col1);
insert into t values(1);
--> 1 row created
insert into t values(1);
--> ORA-00001: unique constraint (TEST.T_UNIQUE) violated
alter table t drop unique (col1);
insert into t values(1);
--> 1 row created

关于sql - 删除唯一约束不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51781000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com