如何在MySQL中创build或删除索引时使用“if exists”?

我想知道是否有一种方法来检查索引是否存在,然后在MySQL上创build它或销毁它。 看起来几年前有一个function要求,但我找不到解决scheme的任何文档。 这需要在使用MDB2的PHP应用程序中完成。

这是我的4class轮:

set @exist := (select count(*) from information_schema.statistics where table_name = 'table' and index_name = 'index' and table_schema = database()); set @sqlstmt := if( @exist > 0, 'select ''INFO: Index already exists.''', 'create index i_index on tablename ( columnname )'); PREPARE stmt FROM @sqlstmt; EXECUTE stmt; 

IF EXISTS修饰符不是为DROP INDEXCREATE INDEX而构build的。 但是在创build/删除索引之前,您可以手动检查是否存在。

使用这个句子来检查索引是否已经存在。

 SHOW INDEX FROM table_name WHERE KEY_NAME = 'index_name' 
  • 如果查询返回零(0),那么索引不存在,那么你可以创build它。
  • 如果查询返回一个正数,那么索引就存在了,那么你可以删除它。

这是一个DROP INDEX IF EXISTS过程:

 DELIMITER $$ DROP PROCEDURE IF EXISTS drop_index_if_exists $$ CREATE PROCEDURE drop_index_if_exists(in theTable varchar(128), in theIndexName varchar(128) ) BEGIN IF((SELECT COUNT(*) AS index_exists FROM information_schema.statistics WHERE TABLE_SCHEMA = DATABASE() and table_name = theTable AND index_name = theIndexName) > 0) THEN SET @s = CONCAT('DROP INDEX ' , theIndexName , ' ON ' , theTable); PREPARE stmt FROM @s; EXECUTE stmt; END IF; END $$ DELIMITER ; 

这段代码是基于以下程序创build的: 在创build之前确定MySQL表索引是否存在

我调整了在这里find的答案,以及在哪里拿出下面的sprocs下降和创build索引。 请注意,如果需要,AddTableIndex sproc可以删除索引。 他们也接受一个对我的使用至关重要的模式名称。

 DELIMITER // DROP PROCEDURE IF EXISTS migrate.DropTableIndex // CREATE PROCEDURE migrate.DropTableIndex ( in schemaName varchar(128) -- If null use name of current schema; , in tableName varchar(128) -- If null an exception will be thrown. , in indexName varchar(128) -- If null an exception will be thrown. ) BEGIN SET schemaName = coalesce(schemaName, schema()); IF((SELECT COUNT(*) AS index_exists FROM information_schema.statistics WHERE TABLE_SCHEMA = schemaName and table_name = tableName AND index_name = indexName) > 0) THEN SET @s = CONCAT('DROP INDEX `' , indexName , '` ON `' , schemaName, '`.`', tableName, '`'); PREPARE stmt FROM @s; EXECUTE stmt; END IF; END // DROP PROCEDURE IF EXISTS migrate.AddTableIndex// CREATE PROCEDURE migrate.AddTableIndex ( IN schemaName varchar(128) -- If null use name of current schema; , IN tableName varchar(128) -- If null an exception will be thrown. , IN indexName varchar(128) -- If null an exception will be thrown. , IN indexDefinition varchar(1024) -- Eg '(expireTS_ ASC)' , IN ifPresent ENUM('leaveUnchanged', 'dropAndReplace') -- null=leaveUnchanged. , OUT outcome tinyint(1) -- 0=unchanged, 1=replaced, 4=added. ) BEGIN DECLARE doDrop tinyint(1) DEFAULT NULL; DECLARE doAdd tinyint(1) DEFAULT NULL; DECLARE tmpSql varchar(4096) DEFAULT ''; SET schemaName = coalesce(schemaName, schema()); SET ifPresent = coalesce(ifPresent, 'leaveUnchanged'); IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.STATISTICS WHERE table_schema = schemaName AND table_name = tableName AND index_name = indexName) THEN IF (ifPresent = 'leaveUnchanged') THEN SET doDrop = 0; SET doAdd = 0; SET outcome = 0; ELSEIF (ifPresent = 'dropAndReplace') THEN SET doDrop = 1; SET doAdd = 1; SET outcome = 1; END IF; ELSE SET doDrop = 0; SET doAdd = 1; SET outcome = 4; END IF; IF (doDrop = 1) THEN SET tmpSql = concat( 'alter table `', schemaName, '`.`', tableName, '` drop index `', indexName, '` '); SET @sql = tmpSql; PREPARE tmp_stmt FROM @sql; EXECUTE tmp_stmt; DEALLOCATE PREPARE tmp_stmt; END IF; IF (doAdd = 1) THEN SET tmpSql = concat( 'alter table `', schemaName, '`.`', tableName, '` add index `', indexName, '` (', indexDefinition, ')'); SET @sql = tmpSql; PREPARE tmp_stmt FROM @sql; EXECUTE tmp_stmt; DEALLOCATE PREPARE tmp_stmt; END IF; END; // DELIMITER ; 

它不是内置的

然而,

http://old.nabble.com/how-to-%22drop-index-if-exists%22-td14024229.html

解释了如何编写一个存储过程来实现DROP INDEX IF EXISTS,而且看起来很容易修改这个代码来实现CREATE INDEX IF EXISTS

我有类似的东西在MySQL中使用SELECT IF()语句。

 select if ( exists( select distinct index_name from information_schema.statistics where table_schema = 'schema_db_name' and table_name = 'tab_name' and index_name like 'index_1' ) ,'select ''index index_1 exists'' _______;' ,'create index index_1 on tab_name(column_name_names)') into @a; PREPARE stmt1 FROM @a; EXECUTE stmt1; DEALLOCATE PREPARE stmt1; 

使用if()语句的优点是不需要写任何程序。

MySQL Workbench版本6.3(MySql fork MariaDb)

 drop index if exists FK_customer__client_school__school_id on dbname.tablename; 

我在这里提出的一些解决scheme遇到问题。 这就是我想到的:

 DELIMITER $$ DROP PROCEDURE IF EXISTS myschema.create_index_if_not_exists $$ CREATE PROCEDURE myschema.create_index_if_not_exists(in p_tableName VARCHAR(128), in p_indexName VARCHAR(128), in p_columnName VARCHAR(128) ) BEGIN PREPARE stmt FROM 'SELECT @indexCount := COUNT(1) from information_schema.statistics WHERE `table_name` = ? AND `index_name` = ?'; SET @table_name = p_tableName; SET @index_name = p_indexName; EXECUTE stmt USING @table_name, @index_name; DEALLOCATE PREPARE stmt; -- select @indexCount; IF( @indexCount = 0 ) THEN SELECT 'Creating index'; SET @createIndexStmt = CONCAT('CREATE INDEX ', p_indexName, ' ON ', p_tableName, ' ( ', p_columnName ,')'); PREPARE stmt FROM @createIndexStmt; EXECUTE stmt; DEALLOCATE PREPARE stmt; END IF; END $$ DELIMITER ; 

使用它如下:

 call myschema.create_index_if_not_exists('MyTable','end_time_index','end_time'); 

这是在MAC OS X 10.8.2上用MySQL 5.5.24和用MySQL 5.5.21在Windows 7上testing的

我认为这将有助于你放弃现有的索引。

  DELIMITER // CREATE PROCEDURE dropIndexing () BEGIN IF EXISTS( SELECT * FROM information_schema.statistics WHERE TABLE_SCHEMA = DATABASE() AND `table_name`='mytable' AND `index_name` = 'myindex' ) THEN ALTER TABLE `mytable` DROP INDEX `myindex`; END IF; END // DELIMITER ; CALL dropIndexing(); DROP PROCEDURE dropIndexing;