Created
September 4, 2020 15:14
-
-
Save TravisBernard/e6945c639fdfae37f2c8e1e248b5139f to your computer and use it in GitHub Desktop.
Drop FK IF EXISTS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Derived from https://stackoverflow.com/a/34545062/2992570 | |
DROP PROCEDURE IF EXISTS dropForeignKeyIfExists; | |
delimiter /// | |
create procedure dropForeignKeyIfExists(IN tableName VARCHAR(64), IN constraintName VARCHAR(64)) | |
begin | |
IF EXISTS( | |
SELECT * FROM information_schema.table_constraints | |
WHERE | |
table_schema = DATABASE() AND | |
table_name = tableName AND | |
constraint_name = constraintName AND | |
constraint_type = 'FOREIGN KEY') | |
THEN | |
SET @query = CONCAT('ALTER TABLE ', tableName, ' DROP FOREIGN KEY ', constraintName, ';'); | |
PREPARE stmt FROM @query; | |
EXECUTE stmt; | |
DEALLOCATE PREPARE stmt; | |
END IF; | |
end/// | |
delimiter ; | |
CALL dropForeignKeyIfExists('RMSSiteDetails', 'RMSSiteDetails_ibfk_1'); | |
CALL dropForeignKeyIfExists('RMSDestination', 'RMSDestination_ibfk_1'); | |
CALL dropForeignKeyIfExists('RMSRequestedOptions', 'RMSRequestedOptions_ibfk_1'); | |
CALL dropForeignKeyIfExists('RMSCart', 'RMSCart_ibfk_1'); | |
CALL dropForeignKeyIfExists('RMSCustomer', 'RMSCustomer_ibfk_1'); | |
CALL dropForeignKeyIfExists('Item', 'Item_ibfk_2'); | |
DROP PROCEDURE IF EXISTS dropForeignKeyIfExists; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment