How to delete an index when an index exists in mysql _ MySQL

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.