Jim Little
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.SCHEMAT A 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.
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.SCHEMAT
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Correct. I forgot that about MySQL. :)