mysql 创建一个存储过程,内容为创建一个不存在的索引
时间: 2023-11-10 09:05:40 浏览: 65
详解mysql索引总结----mysql索引类型以及创建
DELIMITER $$
CREATE PROCEDURE create_index_if_not_exists (IN index_name VARCHAR(100), IN table_name VARCHAR(100), IN column_name VARCHAR(100))
BEGIN
IF NOT EXISTS (SELECT * FROM information_schema.statistics WHERE table_name = table_name AND index_name = index_name) THEN
SET @sql = CONCAT('CREATE INDEX ', index_name, ' ON ', table_name, ' (', column_name, ')');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END IF;
END$$
DELIMITER ;
调用存储过程:
CALL create_index_if_not_exists('idx_name', 'table_name', 'column_name');
阅读全文