Need MySQL 4 to ignore ALTER TABLE errors

11.1k Views Asked by At

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?

2

There are 2 best solutions below

2
On BEST ANSWER

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.

1
On

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.