I'm using a Odbc connection to a mysql server, and was wondering how I would go about checking if a table exists within my database, and if not, create it.
Use CREATE TABLE IF NOT EXISTS
.
For the record, not all RDBMSs support CREATE ... IF NOT EXISTS
. MySQL does, but for those looking for a more portable solution, know that you have multiple (less efficient but otherwise functional) ways of achieving the same thing:
CREATE TABLE
; the statement will fail if the table already exists, in which case simply swallow the error and continueSELECT
from the tables
object in the ANSI information_schema
to see whether or not a given table already exists (as originally suggested by @Derek
-- see @lexu
's answer for the actual query); depending on the result, either issue CREATE TABLE
-- or do not (when accessing information_schema.tables
beware of issues such as table name case sensitivity, the possibility of your table appearing in multiple schemas, etc.)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With