Skip to content Skip to sidebar Skip to footer

Exit Mysql Script If Database Exists

I have a MySQL script that I want to use only if the database doesn't exist to inject some initial demo data for development. If it does exist I just want to break out of the scrip

Solution 1:

Try this. Use INFORMATION_SCHEMA.SCHEMATA to check the existence of Database

If notexists (SELECT1FROM   INFORMATION_SCHEMA.SCHEMATA
               WHERE  SCHEMA_NAME ='demo-database')

CREATE DATABASE `demo-database`
.....

Solution 2:

What I believe now is that the construct with an if/else/endif can only be used in a stored program (see also https://dev.mysql.com/doc/refman/8.0/en/if.html)

I do not have a workaround other than creating a stored procedure that contains the code. In my situation I also have code I would to execute only on a test environment. I am creating only the stored procedure on test then as well.

Post a Comment for "Exit Mysql Script If Database Exists"