0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[MySQL]一時的に外部キー制約を回避したい

Posted at

どんな時に使うか

  • テーブルを一括インポートしたい
  • テーブルを一括削除したい

外部キー参照があると、テーブルを作成する順番や削除する順番を考えなければエラーになってしまう。
一括でインポート、削除したい時は一旦外部キー制約を回避した上で、作業後元に戻せば楽ちん

やり方

外部キー制約を無視する
SET FOREIGN_KEY_CHECKS = 0;

外部キー制約を元に戻す
SET FOREIGN_KEY_CHECKS = 1;

注意

必ず作業後に外部キー制約を元に戻す。
そうでないとデータの不整合が発生するかも

所管

これで楽ちんや...
まあ一括インポート、一括削除するときなんて滅多にないけど。

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?