日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql中的if [not] exists

發布時間:2023/12/20 数据库 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。