所以就产生了需要先判断索引是否存在,再做添加索引或者删除索引的 *** 作(若索引不存在,添加或删除索引会报错)。实例如下:
drop PROCEDURE if EXISTS add_index
DELIMITER //
create PROCEDURE add_index()
BEGIN
IF NOT EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema='Prod.Oms.OmsToSgGateway' AND table_name = 'Oms.OmsToSgGateway.IntermeDiate' AND index_name = 'index_GW_Query') then
ALTER TABLE `Prod.Oms.OmsToSgGateway`.`Oms.OmsToSgGateway.IntermeDiate` ADD INDEX `index_GW_Query`(`ResourceName`, `Category`, `ResourceType`) USING BTREE COMMENT '增加国网数据检索效率'
END IF
IF NOT EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema='Prod.Oms.OmsToSgGateway' AND table_name = 'Oms.OmsToSgGateway.IntermeDiate' AND index_name = 'index_ResourceId') then
ALTER TABLE `Prod.Oms.OmsToSgGateway`.`Oms.OmsToSgGateway.IntermeDiate` ADD INDEX `index_ResourceId`(`ResourceId`) USING BTREE COMMENT '源始id'
END IF
END
//
DELIMITER
call add_index()
显示一个表所有索引的SQL语句是:
show index from 数据库名.表名
查看某表某一列上的索引使用下面的SQL语句:
show index from 数据库名.表名 where column_name like '列名'
下面的SQL语句在我的数据库上执行成功:
show index from web.clubuser where column_name like 'user'。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)