views:

1018

answers:

3

I have a MySQL script which is executed automatically under certain conditions. That script executes an ALTER TABLE command, because that column is needed in the database, but it may or may not have it...

Is it possible to make MySQL 4 execute the ALTER TABLE statement if the column doesn't exist or ignore the duplicate column error for this single command and allow the script execution to continue?

A: 

Try using:

ALTER IGNORE TABLE

That should ignore any errors that occur in the query.

Bart S.
That's not true. The IGNORE keyword specifically relates to duplicate unique key errors only.
Fred
+1  A: 

ALTER [IGNORE] TABLE will only ignore certain errors, like duplicate key errors when adding a new UNIQUE index, or SQL mode errors.

http://dev.mysql.com/doc/refman/4.1/en/alter-table.html

More details about the "script" you are using would help to answer the question. In python for example, the error would raise an exception which could then be caught and dealt with or ignored.

[EDIT] From the comment below, seems like you're looking for the mysql -f command line option.

mluebke
It's a MySQL import, done by running `mysql < file` from the command line. The import process is automated, so I'm trying to avoid "touching" the caller. It would be easy to write a bash to check for this, but I rather not modify the import script.
Tom
From mysql --help you're looking for the -f option: -f, --force Continue even if we get an sql error.
mluebke
A: 

You can first check the table schema before you attempt an addition of the column? However , I strongly suspect the design where you need to add columns on the fly. Something is not quite right. Can you explain the requirement in a little detail. I'm sure there are other cleaner way around this.

Learning