Solved

MySQL If Database Not Exists Create DB - Else Alter Table

Posted on 2014-09-26
3
760 Views
Last Modified: 2014-09-30
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.
0
Comment
Question by:jrlittle86
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 26

Assisted Solution

by:Zberteoc
Zberteoc earned 250 total points
ID: 40347565
If you check the CREATE DATABASE syntax on mysql website:

http://dev.mysql.com/doc/refman/5.0/en/create-database.html

It actuallky is:

CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
    [create_specification] ...

Same goes for tables.
so actually you don't need the IF statement. You could do something like:
Create MyDatabase If Not Exists;

USE MyDatabase;	 
	 
Create Table1 If Not Exists(
...);
Create Table2 If Not Exists(
...);
...
Create TableN If Not Exists(
...);

Alter Table1;
Alter Table2;
...
Alter TableN;

Open in new window

One question, tough. When you say "doesn't work" what do you mean? Is there an error that you can post?
0
 
LVL 50

Accepted Solution

by:
Steve Bink earned 250 total points
ID: 40347703
The IF statement is only available in the context of compound statements, like stored procedures.  To do what you want, create a stored procedure in your script, then call it.  For example:
USE MyDatabase;
DROP PROCEDURE IF EXISTS MyStructure;
DELIMITER //
CREATE PROCEDURE MyStructure AS 
  BEGIN
    SELECT 1 INTO @localVar FROM information_schema.schemata WHERE schema_name='MyOtherDatabase';
    IF @localVar IS NULL THEN
      BEGIN
        /* Database does not exist */
        CREATE DATABASE MyOtherDatabase;
      END;
    ELSE
      BEGIN
      /* Database exists, do whatever here */
      END;
    END IF;
  END;
//
DELIMITER ;
CALL MyStructure;

Open in new window

Disclaimer: untested...just wrote off the top of my head.  You might need some additional syntax, etc.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 40347710
Correct. I forgot that about MySQL. :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question