Funnyweb wrote:
Thanks for the information.
I should have explained that I was talking about writing .sql files.
You can check if a table (and some other things) exist by using IF EXISTS.
I gather from reading the links you quoted that there is no such test for
columns (fields) in a table?
As far as I know, that is correct. I don't think there is any solution
that can be written purely in .sql files.
If you have a scripting language, you could use the SHOW COLUMNS,
examine the output, and conditionally create the columns. But .sql
files are not a replacement for a more fully-featured scripting
language. That is, there's no SQL script equivalent to run a statement
conditionally based on the output of the previous statement.
One possibility is to run a script that contains ALTER TABLE ADD COLUMN
statements, which will fail if there is already a column by that name.
But you can run the script using the "mysql --force" so that the
command-line interpreter will continue even if some statements result in
an SQL error.
Regards,
Bill K.