Is there a way to tell if an index exists before trying to create it within an update script? We have an update script that we need to run that creates new tables and alters existing tables from the mysql command prompt. Is there a way to put an IF NOT EXISTS as part of a call to add an index like you can for creating a table? I was just trying to handle error conditions so that the script could finish running instead of erroring out and halting if the index already existed.
sample script "updates.sql"
CREATE TABLE IF NOT EXISTS `SomeNewTable` (
`id` char(255) NOT NULL default '',
`description` char(30) NOT NULL default '',
PRIMARY KEY (`id`),
) ENGINE=InnoDB;
ALTER TABLE `database`.`ExistingTable1` ADD INDEX `NewIndex`(`some_column`, `another_column`);
ALTER TABLE `database`.`ExistingTable2` ADD INDEX `SomeNewIndex` (`some_column`);
ALTER TABLE `database`.`ExistingTable3` ADD INDEX `AnotherNewIndex` (`some_column`);
from the MySQL prompt
mysql> source updates.sql
If NewIndex already existed on ExistingTable1, this script would never finish and try to add the other indexes. That's what I'm trying to get around.
I've looked at other posts, but they are all trying to look at the indexes programatically (at least the ones I've had time to look at). That doesn't help me here. Any advice is appreciated.
Thanks in advance.
sample script "updates.sql"
CREATE TABLE IF NOT EXISTS `SomeNewTable` (
`id` char(255) NOT NULL default '',
`description` char(30) NOT NULL default '',
PRIMARY KEY (`id`),
) ENGINE=InnoDB;
ALTER TABLE `database`.`ExistingTable1` ADD INDEX `NewIndex`(`some_column`, `another_column`);
ALTER TABLE `database`.`ExistingTable2` ADD INDEX `SomeNewIndex` (`some_column`);
ALTER TABLE `database`.`ExistingTable3` ADD INDEX `AnotherNewIndex` (`some_column`);
from the MySQL prompt
mysql> source updates.sql
If NewIndex already existed on ExistingTable1, this script would never finish and try to add the other indexes. That's what I'm trying to get around.
I've looked at other posts, but they are all trying to look at the indexes programatically (at least the ones I've had time to look at). That doesn't help me here. Any advice is appreciated.
Thanks in advance.