当前位置:网站首页>mysql中的if [not] exists
mysql中的if [not] exists
2022-06-29 09:26:00 【一缕阳光a】
最近在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)边栏推荐
猜你喜欢

Function pointer, function pointer array, calculator + transfer table, etc

Web漏洞手动检测分析

Nacos registry cluster

Use of Azkaban in task scheduler

这个开源项目超哇塞,手写照片在线生成

This open source project is super wow, and handwritten photos are generated Online

十六制计数器和流水灯

Codeforces Round #659 (Div. 2)

点在多边形内外的判断

PGP在加密技术中的应用
随机推荐
这个开源项目超哇塞,手写照片在线生成
51nod1277 maximum value in string [KMP]
2019.10.16 training summary
Reverse thinking - short story
2019.10.20 training summary
mysql 8.0 一条insert语句的具体执行流程分析(二)
2019.10.27训练总结
Codeforces Round #659 (Div. 2)
2019-11-10训练总结
Ce projet Open source est super wow, des photos manuscrites sont générées en ligne
Download control 1 of custom control (downloadview1)
1146 topological order (25 points)
PGP在加密技术中的应用
Is flush stock trading software reliable and safe?
2021 team programming ladder competition - Simulation Competition
Nacos环境隔离
点在多边形内外的判断
2019.11.3学习总结
September 21, 2020 referer string segmentation boost gateway code organization level
指针数组、数组指针和传参的相关问题