Check if the table exists, and if not, create it

I am using an Odbc connection with mysql server and wonder how I will check if a table exists in my database, and if not, create it.

+5
source share
3 answers

Use CREATE TABLE IF NOT EXISTS.

UPDATE

Not all RDBMSs are supported for recordingCREATE ... IF NOT EXISTS . MySQL does, but for those looking for a more portable solution, know that you have several (less efficient but functional) ways to achieve the same thing:

  • unconditionally a problem CREATE TABLE; the statement will not be executed if the table already exists, in which case just swallow the error and continue
  • SELECT tables ANSI information_schema, , ( @Derek - . @lexu ); , CREATE TABLE - ( information_schema.tables , , ..).
+8

create table if not exists NEW_TABLE_NAME , INFORMATION_SCHEMA. (, ).

...

:

select count(TABLE_NAME)
  from INFORMATION_SCHEMA 
 where TABLE_SCHEMA = 'your schema'
   and TABLE_NAME = 'your table'
group by TABLE_NAME

Mysql "", , , , .
, MySQL, !

+3

mysql, , - , , . . , where, , . .

doco mysql . mysql, , , .

+1

All Articles