LoginSignup
2
1

More than 1 year has passed since last update.

mysqlの外部キーを一括で消す方法

Posted at

どんな内容?

全文検索用のレプリケーションDB作成時やデータ移行などの場面でmysqlの外部キーを一括で消したくなる時がありますが、その効率的な方法を記載しています。

手順

STEP1 外部キー削除に必要な部分の抽出sql

SELECT TABLE_NAME,CONSTRAINT_NAME
FROM information_schema.TABLE_CONSTRAINTS 
WHERE CONSTRAINT_SCHEMA = 'test_db' AND CONSTRAINT_TYPE = 'FOREIGN KEY';

STEP2 出てきた値をスプシに貼って、下記のようなINDEXのDROP文を作成する

ALTER TABLE something DROP FOREIGN KEY `somedb_another_id_3a4999a1_fk_somedb_another_id`;

※上記を利用してSTEP1の結果を挟むようにクエリを作る(concatenate関数を使うのががおすすめ)

STEP3 出来上がったクエリを流す

以上の手順で終わりです。

2
1
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
2
1