oracle "SET FOREIGN_KEY_CHECKS = 0;" 甲骨文等效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17233504/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 05:06:57  来源:igfitidea点击:

"SET FOREIGN_KEY_CHECKS = 0;" Oracle Equivalent

oraclekeydatabase-integrity

提问by abderrahim_05

Is there some equivalent to the Mysql specific instruction that disable the check of the foreign keys constraints ?
SET FOREIGN_KEY_CHECKS = 0;

是否有一些等效于禁用外键约束检查的 Mysql 特定指令?
SET FOREIGN_KEY_CHECKS = 0;

采纳答案by Vincent Malgrat

There is no command in Oracle that will disable all constraints at once.

Oracle 中没有命令会立即禁用所有约束。

However it seems you want to disable constraints in the context of dropping tables. In that case you can use the CASCADE CONSTRAINTSclause to drop the referencing constraints from other tables along with the table being dropped.

但是,您似乎想在删除表的上下文中禁用约束。在这种情况下,您可以使用该CASCADE CONSTRAINTS子句从其他表中删除引用约束以及正在删除的表。

Here's an example:

下面是一个例子:

SQL> CREATE TABLE t1 (ID NUMBER PRIMARY KEY);

Table created

SQL> CREATE TABLE t2 (ID NUMBER REFERENCES t1);

Table created

SQL> INSERT INTO t1 VALUES (1);

1 row inserted

SQL> INSERT INTO t2 VALUES (1);

1 row inserted

SQL> -- this fails because of the foreign key
SQL> DROP TABLE t1;

ORA-02449: unique/primary keys in table referenced by foreign keys

SQL> DROP TABLE t1 CASCADE CONSTRAINTS;

Table dropped

回答by Ben

SET FOREIGN_KEY_CHECKS = 0;is session based. In an Oracle context I can only imagine you needing to do this when you have circular references.

SET FOREIGN_KEY_CHECKS = 0;基于会话的。在 Oracle 上下文中,我只能想象当您有循环引用时需要这样做。

You've commented that this is what you want to do:

你评论说这是你想要做的:

SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE table1;
DROP TABLE table2;
SET FOREIGN_KEY_CHECKS = 1; 

I'm assuming that this means that TABLE1 has a foreign key referencing TABLE2 and TABLE2 has a foreign key referencing TABLE1.

我假设这意味着 TABLE1 有一个引用 TABLE2 的外键,而 TABLE2 有一个引用 TABLE1 的外键。

If this is the case then Moudiz's answeris correct. You want to disable the foreign keys before dropping the table:

如果是这种情况,那么穆迪兹的回答是正确的。您想在删除表之前禁用外键:

alter table table1 disable constraint <constraint_name>;
alter table table2 disable constraint <constraint_name>;
drop table table1;
drop table table2;

There's no point disabling all foreign keys for the length of the session, you're only interested in two of them, both of which will be dropped with the table.

在会话期间禁用所有外键是没有意义的,您只对其中两个感兴趣,这两个都将随表一起删除。

You don't want to everdisable all foreign keys.

你不希望永远禁止所有外键。

The only other context I can think of this in is if you want to insert something into your circular reference, in which case you would declare the constraint as DEFERRABLE. This means that the constraint check is done at the end of the transaction rather than on DML being performed, see the documentation.

我能想到的唯一其他上下文是,如果您想在循环引用中插入一些内容,在这种情况下,您可以将约束声明为 DEFERRABLE。这意味着约束检查是在事务结束时完成的,而不是在执行 DML 时完成,请参阅文档

If your references aren't circular then simply drop the tables in the other order.

如果您的引用不是循环的,那么只需按其他顺序删除表格即可。

回答by Moudiz

IF Your asking for a query to disable a foreign key then try this :

如果您要求查询禁用外键,请尝试以下操作:

ALTER TABLE mytable
disable CONSTRAINT fk_mytable;

ALTER TABLE mytable
禁用 CONSTRAINT fk_mytable;