Tôi vừa xây dựng một quy trình có thể tái sử dụng có thể giúp tạo ra DROP COLUMN
Idempotent:
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 ;
Sử dụng:
CALL drop_column_if_exists('my_table', 'my_column');
Thí dụ:
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');