mysql - Add index to table if it does not exists -


i want add index table using alter syntax , want check if index exists in table, , if not exists, add index.

 alter table tablename add index ix_table_xyz (column1); 

is there way this?

try this:

set @x := (select count(*) information_schema.statistics table_name = 'table' , index_name = 'ix_table_xyz' , table_schema = database()); set @sql := if( @x > 0, 'select ''index exists.''', 'alter table tablename add index ix_table_xyz (column1);'); prepare stmt @sql; execute stmt; 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -