質問

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

役に立ちましたか?

解決

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

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

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

他のヒント

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.

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.

If this is the case then Moudiz's answer is 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 ever disable 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.

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

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

ALTER TABLE mytable
disable CONSTRAINT fk_mytable;

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top