title: MySQL create database if not exist
body: <p>I have a T-SQL query which create database if it does not exist yet:</p>
  
  <pre><code>IF (NOT EXISTS (SELECT name 
                  FROM master.dbo.sysdatabases 
                  WHERE ('[' + 'DBName' + ']' = 'DBName'
                     OR name = 'DBName')))
  BEGIN 
      CREATE DATABASE DBName
  
      PRINT 'DATABASE_CREATED' 
  END 
  ELSE 
      PRINT 'DATABASE_EXIST'
  </code></pre>
  
  <p>When I want use this in MySQL I get an error:</p>
  
  <blockquote>
    <p>'IF' is not valid input at this postion</p>
  </blockquote>
  
  <p>I change this script as </p>
  
  <pre><code>IF(SELECT COUNT(*) FROM SCHEMA_NAME 
     FROM INFORMATION_SCHEMA.SCHEMATA 
     WHERE SCHEMA_NAME = 'DBName') &gt; 0)
  THEN BEGIN 
      CREATE DATABASE DBName
  
      PRINT 'DATABASE_CREATED'
  ELSE 
      PRINT 'DATABASE_EXIST'`
  </code></pre>
  
  <p>but it still doesn't work</p>
  
  <p>How can I create this query in MySQL?</p>