Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a way to tell if an index exists before trying to create it?

Status
Not open for further replies.

ajdavis

Programmer
Jun 17, 2005
14
US
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.

 
You could start mysql using the "-f" or "--force" option. It tells the program to continue executing after SQL errors.
 
Ok, thanks. That seems simple enough. [thumbsup] I guess that shows my level of knowledge (or ignorance) of MySQL. I appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top