删除数据库中的所有外键(MySql)?
程序员文章站
2022-06-04 11:14:54
...
我不想手动删除所有的目录,有办法删除数据库中的所有外键约束吗?
可以使用此SQL生成ALTERTABLE:
SELECT concat(‘alter table ‘,table_schema,’.’,table_name,’ DROP FOREIGN KEY ‘,constraint_name,’;’)
FROM information_schema.table_constraints
WHERE constraint_type=’FOREIGN KEY’
AND table_schema=’!!YOUR_SCHEMA_HERE!!’;
它将输出这样的SQL:
alter table viewpoint_test.answer_code DROP FOREIGN KEY fk_answer_code_codebook_item1;
alter table viewpoint_test.answer_code DROP FOREIGN KEY fk_answer_code_questionary_answer1;
alter table viewpoint_test.codebook DROP FOREIGN KEY codebook_ibfk_1;