Link to home
Start Free TrialLog in
Avatar of Jim Little
Jim LittleFlag for United States of America

asked on

MySQL If Database Not Exists Create DB - Else Alter Table

I'm using MySQL 5.5.15. I have a .sql file I'm using as part of an installer.  I need to check whether the database exists or not.

If it doesn't exist then create a new database and tables.
If it DOES exist then update the database tables with additional columns.

I'd like to check this with a simple IF statement:

If NOT EXISTS(SELECT SCHEMA_NAME from INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'MyDatabase')
THEN
     Create MyDatabase;
     Create Tables;
Else
    Alter Table1;
    Alter Table2;
    ...
    Alter TableN;
End If;

Why doesn't this work?  The problem is the IF statement. It seems logical to me but I'm obviously thinking differently than MySQL.
SOLUTION
Avatar of Zberteoc
Zberteoc
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Correct. I forgot that about MySQL. :)