要在MySQL中批量删除数据库,您可以使用循环和动态SQL语句。这里是一个示例存储过程,用于删除以特定前缀开头的所有数据库:
DELIMITER $$
CREATE PROCEDURE drop_databases_with_prefix(IN db_prefix VARCHAR(255))
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE db_name VARCHAR(255);
DECLARE cur CURSOR FOR SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME LIKE CONCAT(db_prefix, '%');
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
read_loop: LOOP
FETCH cur INTO db_name;
IF done THEN
LEAVE read_loop;
END IF;
SET @sql = CONCAT('DROP DATABASE ', db_name);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE cur;
END$$
DELIMITER ;
要使用此存储过程,请按照以下步骤操作:
test_
开头的所有数据库,请运行以下命令:CALL drop_databases_with_prefix('test_');
注意:在执行此操作之前,请确保您已备份所有重要数据,因为此操作将永久删除数据库及其内容。