惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园 - 聂微东
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
MongoDB | Blog
MongoDB | Blog
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
The GitHub Blog
The GitHub Blog
博客园_首页
博客园 - 叶小钗
腾讯CDC
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
宝玉的分享
宝玉的分享
D
Docker

博客园 - suiyingjie

vs2010 将.mc编译为.rc文件 列出系统支持的区域设置 如何用windbg分析64位机上32位程序的dump文件 libcurl发送邮件C++类 (转) libcurl教程(转) VC 宏与预处理使用方法总结(转) 解决VS2010升级SP1后出现的warning C4005问题 nafxcw.lib与LIBCMT.lib在vs2008,VS2010中链接冲突解决方案 使用JAVASCRIPT去360DOC复制限制 如何自动获取网络变化通知(转) 用户中心 - 博客园 使用Symchk.exe 下载符号 A Crash Course on the Depths of Win32 Structured Exception Handling [转载] 从文件句柄获得全路径 Ip安全策略批处理脚本 c++资源之不完全导引 (转) 突破局域网中对用户上网的限制(转) [转载]HTML七种加密解密 - suiyingjie - 博客园 挂马代码大全 - suiyingjie - 博客园
MySQL判断索引是否存在的存储过程
suiyingjie · 2013-01-07 · via 博客园 - suiyingjie

创建mysql数据库时需要创建索引,但是mysql并不有drop index index_name if exists on table_name这样的语法,所以写一个存储过程来判断,如果存在就删除后再创建新的索引。存储过程如下:

--
-- procedure of delete index
--
drop procedure if exists Del_idx;
DELIMITER $$
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 $$

DELIMITER ;

使用时传入表名和索引名即可,如CALL Del_idx('tableA', 'indexA');