mysql中的if [not] exists
最近在MySQL数据库的基础上开发分布式的数据库,需要支持一个if [not] exists语法。学习了SQL语法解析部分,总结下:
1、在MySQL中,创建表时支持create table if not exists db.table_name ....
create table if not exists test1
(c1 int primary key,c2 varchar(50)
)engine = innodb;
如果要创建的表存在,则直接返回,不在重新创建该表。
如果表不存在,则创建该表。
2、drop一张表时,需要支持if exists语法:
drop table if exists test1;
如果要drop的表存在,则直接drop掉
如果要drop的表不存在,则会显示一个warnings,如下:
+-------+------+-----------------------------+
| Level | Code | Message |
+-------+------+-----------------------------+
| Note | 1051 | Unknown table 'mysql.test1' |
+-------+------+-----------------------------+
1 row in set (0.00 sec)
来一个总的流程,包括create/drop if [not] exists语句的提示:
Cluster[mysql]> create table test1-> (-> c1 int primary key,-> c2 varchar(50)-> )engine = innodb;
Query OK, 0 rows affected (0.06 sec)Cluster[mysql]> create table test1-> (-> c1 int primary key,-> c2 varchar(50)-> )engine = innodb;
ERROR 1050 (42S01): Table 'test1' already exists
Cluster[mysql]>
Cluster[mysql]>
Cluster[mysql]>
Cluster[mysql]>
Cluster[mysql]> create table if not exists test1-> (-> c1 int primary key,-> c2 varchar(50)-> )engine = innodb;
Query OK, 0 rows affected, 1 warning (0.02 sec)Cluster[mysql]> show warnings;
+-------+------+------------------------------+
| Level | Code | Message |
+-------+------+------------------------------+
| Note | 1050 | Table 'test1' already exists |
+-------+------+------------------------------+
1 row in set (0.00 sec)Cluster[mysql]> drop table test1;
Query OK, 0 rows affected (0.05 sec)Cluster[mysql]> drop table test1;
ERROR 1051 (42S02): Unknown table 'mysql.test1'
GreatDB Cluster[mysql]>
GreatDB Cluster[mysql]>
GreatDB Cluster[mysql]> drop table if exists test1;
Query OK, 0 rows affected, 1 warning (0.02 sec)Cluster[mysql]> show warnings;
+-------+------+-----------------------------+
| Level | Code | Message |
+-------+------+-----------------------------+
| Note | 1051 | Unknown table 'mysql.test1' |
+-------+------+-----------------------------+
1 row in set (0.00 sec)
mysql中的if [not] exists相关推荐
- Mysql中1050-Table XXX already exists解决方案
Mysql创建表格是时遇到[Err] 1050-Table 'xxx' already exists异常的解决方法: 在create table后面添加if not exists即可
- mysql 存过 if_mysql中 储存过程 if exists 该如何写呀
mysql中 储存过程 if exists 该怎么写呀? CREATE PROCEDURE test(in ip int(20),in guid varchar(32)) BEGIN if exist ...
- MySQL中exists与in的使用
exists对外表用loop逐条查询,每次查询都会查看exists的条件语句,当 exists里的条件语句能够返回记录行时(无论记录行是的多少,只要能返回),条件就为真,返回当前loop到的这条记录, ...
- mysql中使用join exists in时该注意的问题
2019独角兽企业重金招聘Python工程师标准>>> 这是个老生常谈的问题,平时在简单使用过程中,都能得到想要的结果,但是对于各自的性能,以及适用场景,我们该注意什么?假如现在有表 ...
- mysql中 !40000 DROP DATABASE IF EXISTS `top_server` 这中注释有什么作用?
需求描述: 今天在进行mysqldump实验,使用--add-drop-databases参数,于是在生成的SQL文件中,就出现了. /*!40000 DROP DATABASE IF EXISTS ...
- 浅析MySQL中exists,in ,=的使用
exists对外表用loop逐条查询,每次查询都会查看exists的条件语句,当 exists里的条件语句能够返回记录行时(无论记录行是的多少,只要能返回),条件就为真,返回当前loop到的这条记录, ...
- mysql中有exist吗_关于MySQL 中 EXISTS 的用法
需求: 在MySQL中 EXISTS 和 IN 的用法有什么关系和区别呢? 假定数据库中有两个表 分别为 表 a 和表 b: create tablea ( a_idint, a_namevarcha ...
- MySQL中IN和EXISTS的用法
exists对外表用loop逐条查询,每次查询都会查看exists的条件语句,当 exists里的条件语句能够返回记录行时(无论记录行是的多少,只要能返回),条件就为真,返回当前loop到的这条记录, ...
- MySQL中In与Exists的区别
MySQL中In与Exists的区别 1 例子 2 EXISTS和IN的介绍 2.1 exists 2.2 in 2.3 使用上的区别 3 EXISTS和IN的性能分析 4 总结 1 例子 有两个表需 ...
最新文章
- [转]在ubuntu上安装chrome浏览器
- Spring注入方法
- zabbix监控服务器日志文件,Zabbix对服务器资源进行监控及百度告警的整合
- 嘉宾及议程速览,第四范式2021发布会进入一周倒计时
- idea 编译显示source1.3不支持泛型(请使用source5或更高版本)
- Codeup墓地-问题 A: 还是畅通工程
- JavaScript数据结构与算法——集合
- [css] 让网页的字体变得清晰,变细用CSS怎么做?
- recv函数返回0_函数VLOOKUP与LOOKUP的PK,大战一触即发 Excel神技能!
- 整理的C++面试,这些是最为常见的
- 必不可少需要掌握的嵌入式知识(2) -- 经典数据结构总结之 (链表,队列)
- Python100道经典练习题(附PDF)
- 单片机C语言的编译模式,手把手教你学单片机的C语言程序设计六编译预处理.pdf...
- yandex定位插件
- 关于Aegisub的视频滤镜插件和字幕滤镜插件
- 15个国外便宜主机介绍
- powerDesigner 反向生成mysql数据库库
- 同步十进制加法计数器设计
- android好用拍照框架,Github挺好用的android图片选择框架(拍照+从相册)
- 2022新版域名防红系统源码