I am writing a simple SQL script and need to create a new database if it doesn't exist, or leave the database alone if it does already exist (and not overwrite it or drop-n-swap its tables).
I need this to be ANSI-compliant; according to W3 Schools:
CREATE DATABASE my_db;
...is ANSI-compliant. But nowhere does it state what its behavior is if my_db
already exists.
So I ask: how do I write ANSI-compliant SQL that creates my_db
if it doesn't exist, or that doesn't do anything to it if it already does exist? Thanks in advance!
The command you're using is specified per vendor, and the specification (at least SQL:2008 (12.1MB zip archive)) does not mention what the implementation is for the creation of a database, leaving the details up to the individual vendor.
Its possible to have a RDBMS that could overwrite when issued a CREATE DATABASE
but in my personal experience unlikely. Your best option here is to test with each RDBMS vendor's product you intend to support.