How to delete an index when an index exists in mysql

Source: Internet
Author: User

Mysql determines whether an index exists. The mysql drop index statement does not support the if exists condition. In SQL, the index is deleted first and then created, if this index is not found in the database, an error will be reported, resulting in subsequent SQL statements not being executed. Www.2cto.com therefore, you need to use the stored procedure to determine whether the 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 );

Related Article

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.