我如何使用“如果存在"?用于在 MySQL 中创建或删除索引?

2021-12-21 00:00:00 indexing php mysql

我想知道是否有办法在 MySQL 上创建或销毁索引之前检查索引是否存在.几年前似乎有一个功能请求,但我找不到任何解决方案的文档.这需要在使用 MDB2 的 PHP 应用程序中完成.

I was wondering if there's a way to check if an index exists before creating it or destroying it on MySQL. It appears that there was a feature request for this a few years back, but I can't find any documentation for a solution. This needs to be done in a PHP app using MDB2.

推荐答案

这是我的 4 个班轮:

Here is my 4 liner:

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;

相关文章