I tried writing query using exists
, but no success so far. Searching hasn't helped so far.
Asked
Active
Viewed 3,785 times
0

Jumper
- 129
- 3
- 9
-
Take a look at this thread http://stackoverflow.com/questions/972922/add-column-to-mysql-table-if-it-does-not-exist – Nicola Cossu Apr 27 '11 at 12:14
-
I suggest you post what you tried. – Jaydee Apr 27 '11 at 12:14
-
Why would you be altering a table when you are not sure that it exists? You should be able to interrogate the system catalog or the information schema to find out whether the table you are after exists or not. – Jonathan Leffler Apr 27 '11 at 13:20
1 Answers
2
If you attempt to alter a table that does not exist, the query will fail with an error: Table 'database.table' doesn't exist
MySQL does support ALTER IGNORE TABLE
, but that only turns errors into warnings if you're attempting to create a unique index while there are values in the table that violate that index.
If you would like to make sure that you do not produce any database queries, I would suggest ensuring the table's existence using SHOW TABLES LIKE 'tablename'
before running your ALTER TABLE
query.

TehShrike
- 9,855
- 2
- 33
- 28