Skip navigation links

MySQL Forums :: Transactions :: Drop foreign key only if it exists


Advanced Search

Re: Drop foreign key only if it exists
Posted by: James Rivord ()
Date: February 12, 2009 10:47AM

With the help form a post by Nick Parr, I created the following that works.

DELIMITER $$
DROP PROCEDURE IF EXISTS DropFK $$
CREATE PROCEDURE DropFK (
IN parm_table_name VARCHAR(100),
IN parm_key_name VARCHAR(100)
)
BEGIN
-- Verify the foreign key exists
IF EXISTS (SELECT NULL FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND CONSTRAINT_NAME = parm_key_name) THEN
-- Turn the parameters into local variables
set @ParmTable = parm_table_name ;
set @ParmKey = parm_key_name ;
-- Create the full statement to execute
set @StatementToExecute = concat('ALTER TABLE ',@ParmTable,' DROP FOREIGN KEY ',@ParmKey);
-- Prepare and execute the statement that was built
prepare DynamicStatement from @StatementToExecute ;
execute DynamicStatement ;
-- Cleanup the prepared statement
deallocate prepare DynamicStatement ;
END IF;
END $$
DELIMITER ; $$

Options: ReplyQuote


Subject Views Written By Posted
Drop foreign key only if it exists 27066 Eugene Chechel 07/17/2008 02:24AM
Re: Drop foreign key only if it exists 12658 William Chiquito 07/17/2008 08:21PM
Re: Drop foreign key only if it exists 9867 Eugene Chechel 08/12/2008 06:00AM
Re: Drop foreign key only if it exists 10116 James Rivord 02/11/2009 01:32PM
Re: Drop foreign key only if it exists 11102 James Rivord 02/12/2009 10:47AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.