4

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?

Tom
  • 6,991
  • 13
  • 60
  • 78

2 Answers2

3

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
  • 8,588
  • 7
  • 35
  • 31
  • 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 Feb 18 '09 at 14:38
  • 3
    From mysql --help you're looking for the -f option: -f, --force Continue even if we get an sql error. – mluebke Feb 18 '09 at 19:59
0

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
  • 8,029
  • 3
  • 35
  • 46
  • 4
    Sometimes you have to update a production database that already has data via an automated way (such as when installing an update of the software). I came to this question exactly because of this, and it's very hard to see a way around this without having designed the schema to be easily versionable beforehand. – Rafael Almeida Apr 10 '13 at 14:02