如题,请大家帮个忙
mysql下,如果判断某个表的某个索引是否存在?如果存在,就把它删除,完整的sql要怎么写?

解决方案 »

  1.   

    show index from table;
    alter table drop index idx_name;
      

  2.   

    可用存储过程实现delimiter //
    create procedure t_index()
    begin
    select index_name into @in_name from information_schema.statistics where table_name='表名' and index_name='索引名';
    if @in_name !='' then
    drop procedure if exists '索引名';
    end if
    end大概思路是这样。也可以在存储过程中添加参数,这样想删除什么索引就给参数就可以。
    快下班走人了。来不及详细整理。可能会有不足的地方。
    晚上再详细看看。也请高手指教
      

  3.   

    DROP PROCEDURE IF EXISTS del_idx;
    create procedure del_idx(IN p_tablename varchar(200), IN p_idxname VARCHAR(200))
    begin
    DECLARE str VARCHAR(250);
      set @str=concat(' drop index ',p_idxname,' on ',p_tablename); 
      
      select count(*) into @cnt from information_schema.statistics where table_name=p_tablename and index_name=p_idxname ;
      if @cnt >0 then 
        PREPARE stmt FROM @str;
        EXECUTE stmt ;
      end if;end