Drop foreign key only if it exists
问题 I'm on a MySQL database. I'm doing this, but it doesn't work. ALTER TABLE `object` DROP FOREIGN KEY IF EXISTS `object_ibfk_1`; I've tried to put this IF EXISTS wherever I could. How can check if foreign key is exists before drop it? 回答1: If you want to drop foreign key if it exists and do not want to use procedures you can do it this way (for MySQL) : set @var=if((SELECT true FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND TABLE_NAME = 'table_name' AND