How does mysql delete an index when determining whether an index exists? bitsCN.com
How to delete an index when an index exists in mysql
Mysql's drop index statement does not support the if exists condition. you can delete an index in SQL,
Create an index again. if the index is not found in the database for the newly created Database, an error is returned,
As a result, subsequent SQL statements are not executed.
Therefore, you must use stored procedures to determine whether an index exists. if so, delete the index.
The SQL code is as follows:
SQL code
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;
Call del_idx ('Table _ name', 'index _ name ');
Alter table table_name add index index_name (column1, column2 );
BitsCN.com