Reputation: 40175
I am using PHP and have a constant declaring the table column details.
I currently $sql = "CREATE TABLE ' . $table_name . ' ' . TABLE_COLUMNS;
and odbc exec it, where TABLE_COLUMNS is a DEFINE() (constant literal string).
This is more of an SQL question than coding. I don't want to change any code other than the DEFINE; I just want (if possible) one SQL statement which creates the table if it does not exist, or updates it if it does, inserting columns as necessary, with either blank, or zer0 or some meaningful in the already extant rows (which must remain).
Can that be done, or do I have to write some code which knows what the columns were before and what the new ones are now? (and what about deleting columns?)
Upvotes: 0
Views: 144
Reputation: 116140
You won't get there with a single define. You should try to create the table. If that fails, try to create each of the columns. Not that adding columns one by one can be very time consuming on large tables, because MySQL is actually recreating the whole table and copying all the data.
It is better to get the current structure of the table, parse it, and add the required columns.
Please check this similar question.
Upvotes: 1