mysqlalter-tablemysql-error-1091

MySQL: ALTER IGNORE TABLE with DROP INDEX


I have many databases and in each I have table names. In some I have UNIQUE KEY named name1 and in others the same UNIQUE KEY is named name2. I want to standard this name so I prepared 3 queries to run on each database:

ALTER IGNORE TABLE `names`
    DROP INDEX `name1`;
ALTER IGNORE TABLE `names`
    DROP INDEX `name2`;
ALTER TABLE `names`
    ADD UNIQUE `new_name` (`name`, `surname`);

But I got error:

SQL Error (1091): Can't DROP 'name1'; check that column/key exists

How can I make one set of queries to run on each database?


Solution

  • You can attempt to ignore errors if you're executing your SQL script file from command line:

    mysql -f -u username -p password -e 'source script.sql'
    

    EDIT

    Thanks to Ike, the correct way to do this is:

    mysql -f -u username -p password < script.sql