我如何使用“如果存在"?用于在 MySQL 中创建或删除索引? [英] How can I employ "if exists" for creating or dropping an index in MySQL?

查看:32
本文介绍了我如何使用“如果存在"?用于在 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;

这篇关于我如何使用“如果存在"?用于在 MySQL 中创建或删除索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆