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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
CREATE TABLE IF NOT EXISTS.UPDATE
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 continueSELECTfrom thetablesobject in the ANSIinformation_schemato 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 issueCREATE TABLE— or do not (when accessinginformation_schema.tablesbeware of issues such as table name case sensitivity, the possibility of your table appearing in multiple schemas, etc.)