gpt4 book ai didi

mysql - 如果表不存在则添加索引

转载 作者:可可西里 更新时间:2023-11-01 06:28:52 25 4
gpt4 key购买 nike

我想使用 ALTER 语法给表添加索引,但首先要检查它是否已经存在于表中,如果不存在才添加索引。

 ALTER TABLE tableName ADD INDEX IX_Table_XYZ (column1);

有什么办法吗?

最佳答案

像这样尝试:

set @x := (select count(*) from information_schema.statistics where table_name = 'table' and index_name = 'IX_Table_XYZ' and table_schema = database());
set @sql := if( @x > 0, 'select ''Index exists.''', 'Alter Table TableName ADD Index IX_Table_XYZ (column1);');
PREPARE stmt FROM @sql;
EXECUTE stmt;

关于mysql - 如果表不存在则添加索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30259196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com