如果数据库存在则退出 MySQL 脚本

编程入门 行业动态 更新时间:2024-10-26 18:23:08
本文介绍了如果数据库存在则退出 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 的构造只能在存储程序中使用(另请参见 dev.mysql/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 dev.mysql/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 脚本

本文发布于:2023-10-19 18:18:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508362.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   数据库   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!