MYSQL判断索引是否存在,不存在则创建索引(亲测可用)
- springboot集成flyway管理数据库版本
- 使用存储过程平滑更新系统数据库
-
- 这是一个不存在就添加索引的例子,若需求是存在则删除 微调下面的 if not exists 即可
-
- DROP PROCEDURE IF EXISTS add_index;
- DELIMITER $
- CREATE PROCEDURE add_index()
- BEGIN
- DECLARE target_database VARCHAR(100);
- DECLARE target_table_name VARCHAR(100);
- DECLARE target_column_name VARCHAR(100);
- DECLARE target_index_name VARCHAR(100);
- set target_table_name = '表名';
- set target_index_name = '索引名';
- SELECT DATABASE() INTO target_database;
- IF NOT EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema = target_database AND table_name = target_table_name AND index_name = target_index_name) THEN
- set @statement = "alter table `实际表名` add UNIQUE KEY 索引名 (字段,多字段逗号分隔)";
- PREPARE STMT FROM @statement;
- EXECUTE STMT;
- END IF;
- END;
- $
- DELIMITER ;
- CALL add_index();
ps: 修改的两个地方:
一是定义好索引名和表名,
二是写好@statement,就是平时写的操作索引的语句
原文链接:https://blog.csdn.net/TurboAnho/article/details/119808724 标签:index,target,name,索引,add,MYSQL,table,亲测 From: https://www.cnblogs.com/sunny3158/p/17782156.html