How can we check if a MySQL database exists?

352420 pts.
Tags:
Database
MySQL
MySQL Database
Can anyone tell me if it's possible to check if our MySQL database exists after we've made a connection? We already know how to check if a table exists in our database but now we need to see if the actual database exists. Thanks!

Answer Wiki

Thanks. We'll let you know when a new response is added.

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Brijesh
    Hi,

    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>;
    9,870 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following