Skip navigation links

MySQL Forums :: Data Dictionary & Information Schema :: How to check with MYSQL if a database exists or not


Advanced Search

Re: How to check with MYSQL if a database exists or not
Posted by: Roland Bouman ()
Date: June 10, 2008 07:42AM

Hi!

first of all, the IF statement is stored procure syntax. So if this is not inside a stored procedure you should simply do:

SELECT IF(EXISTS (SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'Temp'), 'Yes','No')

Or simply:

SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'Temp'

(empty set if not exists)

If you just need to know if a db exists so you won't get an error when you try to create it, simply use:

CREATE DATABASE IF NOT EXISTS <name>;

Roland Bouman
http://rpbouman.blogspot.com/

Options: ReplyQuote


Subject Views Written By Posted
How to check with MYSQL if a database exists or not 42221 Venkata Anil Kumar Jayanti 06/10/2008 05:23AM
Re: How to check with MYSQL if a database exists or not 24815 Roland Bouman 06/10/2008 07:42AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.