删除mysql数据库实体外键的方法

1、查询所有外键

select
TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
from INFORMATION_SCHEMA.KEY_COLUMN_USAGE
where CONSTRAINT_SCHEMA ='databasename' and REFERENCED_TABLE_NAME is not NULL
order by REFERENCED_TABLE_NAME

2、拼接生成删除外键sql语句

SELECT
    CONCAT('ALTER TABLE `', table_name,'` DROP FOREIGN KEY `', CONSTRAINT_NAME,'`;' )AS 'drop'
FROM
    INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
WHERE
    CONSTRAINT_SCHEMA ='databasename' and REFERENCED_TABLE_NAME is not NULL

3、导出拼接的语言执行

删除mysql数据库实体外键的方法_第1张图片

 

你可能感兴趣的:(SQL,数据库,SQL,mysql,外键)