Az önce DROP COLUMN
idempotent yapmaya yardımcı olabilecek yeniden kullanılabilir bir prosedür geliştirdim :
DROP FUNCTION IF EXISTS column_exists;
DELIMITER $$
CREATE FUNCTION column_exists(
tname VARCHAR(64),
cname VARCHAR(64)
)
RETURNS BOOLEAN
READS SQL DATA
BEGIN
RETURN 0 < (SELECT COUNT(*)
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA` = SCHEMA()
AND `TABLE_NAME` = tname
AND `COLUMN_NAME` = cname);
END $$
DELIMITER ;
DROP PROCEDURE IF EXISTS drop_column_if_exists;
DELIMITER $$
CREATE PROCEDURE drop_column_if_exists(
tname VARCHAR(64),
cname VARCHAR(64)
)
BEGIN
IF column_exists(tname, cname)
THEN
SET @drop_column_if_exists = CONCAT('ALTER TABLE `', tname, '` DROP COLUMN `', cname, '`');
PREPARE drop_query FROM @drop_column_if_exists;
EXECUTE drop_query;
END IF;
END $$
DELIMITER ;
Kullanım:
CALL drop_column_if_exists('my_table', 'my_column');
Misal:
SELECT column_exists('my_table', 'my_column');
CALL drop_column_if_exists('my_table', 'my_column');
SELECT column_exists('my_table', 'my_column');
CALL drop_column_if_exists('my_table', 'my_column');
SELECT column_exists('my_table', 'my_column');