如果数据库存在则退出 MySQL 脚本 [英] Exit MySQL script if database exists

查看:68
本文介绍了如果数据库存在则退出 MySQL 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MySQL 脚本,我想在数据库不存在时使用它来注入一些用于开发的初始演示数据.如果它确实存在,我只想跳出脚本.脚本开始像

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 script. The script starts like

CREATE DATABASE IF NOT EXISTS `demo-database`;
USE `demo-database`;

如果数据库存在,有没有办法在创建数据库的这里或上面退出,这样它就不会运行所有的表设置和插入?

Is there a way to exit here or above the create database if the database exists so that it wont run through all the table setups and inserts?

推荐答案

我现在相信带有 if/else/endif 的构造只能在存储程序中使用(另请参见 https://dev.mysql.com/doc/refman/8.0/en/if.html)

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.

这篇关于如果数据库存在则退出 MySQL 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆