mysql中的if [not] exists
生活随笔
收集整理的這篇文章主要介紹了
mysql中的if [not] exists
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最近在MySQL數(shù)據(jù)庫的基礎(chǔ)上開發(fā)分布式的數(shù)據(jù)庫,需要支持一個if [not] exists語法。學(xué)習(xí)了SQL語法解析部分,總結(jié)下:
1、在MySQL中,創(chuàng)建表時支持create table if not exists db.table_name ....
create table if not exists test1 (c1 int primary key,c2 varchar(50) )engine = innodb;如果要創(chuàng)建的表存在,則直接返回,不在重新創(chuàng)建該表。
如果表不存在,則創(chuàng)建該表。
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)總結(jié)
以上是生活随笔為你收集整理的mysql中的if [not] exists的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用外挂只为“吃鸡”成功?为什么不试试正当
- 下一篇: jq 遍历map集合