Mysql高手系列 - 第5天:DML操做彙總,肯定你都會?

這是Mysql系列第5篇。java

環境:mysql5.7.25,cmd命令中進行演示。mysql

DML(Data Manipulation Language)數據操做語言,以INSERT、UPDATE、DELETE三種指令爲核心,分別表明插入、更新與刪除,是必需要掌握的指令,DML和SQL中的select熟稱CRUD(增刪改查)。sql

文中涉及到的語法用[]包含的內容屬於可選項,下面作詳細說明。數據庫

插入操做

插入單行2種方式

方式1
insert into 表名[(字段,字段)] values (值,值);

說明:安全

值和字段須要一一對應數據結構

若是是字符型或日期類型,值須要用單引號引發來;若是是數值類型,不須要用單引號函數

字段和值的個數必須一致,位置對應spa

字段若是不能爲空,則必須插入值3d

能夠爲空的字段能夠不用插入值,但須要注意:字段和值都不寫;或字段寫上,值用null代替日誌

表名後面的字段能夠省略不寫,此時表示全部字段,順序和表中字段順序一致。

方式2
insert into 表名 set 字段 = 值,字段 = 值;

方式2不常見,建議使用方式1

批量插入2種方式

方式1
insert into 表名 [(字段,字段)] values (值,值),(值,值),(值,值);
方式2
insert into 表 [(字段,字段)]
數據來源select語句;

說明:

數據來源select語句能夠有不少種寫法,須要注意:select返回的結果和插入數據的字段數量、順序、類型須要一致。

關於select的寫法後面文章會詳細介紹。

如:

-- 刪除test1
drop table if exists test1;
-- 建立test1
create table test1(a int,b int);
-- 刪除test2
drop table if exists test2;
-- 建立test2
create table test2(c1 int,c2 int,c3 int);
-- 向test2中插入數據
insert into test2 values (100,101,102),(200,201,202),(300,301,302),(400,401,402);
-- 向test1中插入數據
insert into test1 (a,b) select 1,1 union all select 2,2 union all select 2,2;
-- 向test1插入數據,數據來源於test2表
insert into test1 (a,b) select c2,c3 from test2 where c1>=200;

select * from test1;

mysql> select * from test1;
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
|    2 |    2 |
|    2 |    2 |
|  201 |  202 |
|  301 |  302 |
|  401 |  402 |

mysql> select * from test2;
+------+------+------+
| c1   | c2   | c3   |
+------+------+------+
|  100 |  101 |  102 |
|  200 |  201 |  202 |
|  300 |  301 |  302 |
|  400 |  401 |  402 |
+------+------+------+
4 rows in set (0.00 sec)

數據更新

單表更新

語法:
update 表名 [[as] 別名] set [別名.]字段 = 值,[別名.]字段 = 值 [where條件];

有些表名可能名稱比較長,爲了方便操做,能夠給這個表名起個簡單的別名,更方便操做一些。

若是無別名的時候,表名就是別名。

示例:
mysql> update test1 t set t.a = 2;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 6  Changed: 4  Warnings: 0

mysql> update test1 as t set t.a = 3;
Query OK, 6 rows affected (0.00 sec)
Rows matched: 6  Changed: 6  Warnings: 0

mysql> update test1 set a = 1,b=2;
Query OK, 6 rows affected (0.00 sec)
Rows matched: 6  Changed: 6  Warnings: 0

多表更新

能夠同時更新多個表中的數據

語法:
update 表1 [[as] 別名1],表名2 [[as] 別名2]
set [別名.]字段 = 值,[別名.]字段 = 值
[where條件]
示例:
-- 無別名方式
update test1,test2 set test1.a = 2 ,test1.b = 2, test2.c1 = 10;
-- 無別名方式
update test1,test2 set test1.a = 2 ,test1.b = 2, test2.c1 = 10 where test1.a = test2.c1;
-- 別名方式更新
update test1 t1,test2 t2 set t1.a = 2 ,t1.b = 2, t2.c1 = 10 where t1.a = t2.c1;
-- 別名的方式更新多個表的多個字段
update test1 as t1,test2 t2 set t1.a = 2 ,t1.b = 2, t2.c1 = 10 where t1.a = t2.c1;

使用建議

建議採用單表方式更新,方便維護。

刪除數據操做

使用delete刪除

delete單表刪除
delete [別名] from 表名 [[as] 別名] [where條件];

注意:

若是無別名的時候,表名就是別名

若是有別名,delete後面必須寫別名

若是沒有別名,delete後面的別名能夠省略不寫。

示例
-- 刪除test1表全部記錄
delete from test1;
-- 刪除test1表全部記錄
delete test1 from test1;
-- 有別名的方式,刪除test1表全部記錄
delete t1 from test1 t1;
-- 有別名的方式刪除知足條件的記錄
delete t1 from test1 t1 where t1.a>100;

上面的4種寫法,你們能夠認真看一下。

多表刪除

能夠同時刪除多個表中的記錄,語法以下:

delete [別名1,別名2] from 表1 [[as] 別名1],表2 [[as] 別名2] [where條件];

說明:

別名能夠省略不寫,可是須要在delete後面跟上表名,多個表名之間用逗號隔開。

示例1
delete t1 from test1 t1,test2 t2 where t1.a=t2.c2;

刪除test1表中的記錄,條件是這些記錄的字段a在test.c2中存在的記錄

看一下運行效果:

-- 刪除test1
drop table if exists test1;
-- 建立test1
create table test1(a int,b int);
-- 刪除test2
drop table if exists test2;
-- 建立test2
create table test2(c1 int,c2 int,c3 int);
-- 向test2中插入數據
insert into test2 values (100,101,102),(200,201,202),(300,301,302),(400,401,402);
-- 向test1中插入數據
insert into test1 (a,b) select 1,1 union all select 2,2 union all select 2,2;
-- 向test1插入數據,數據來源於test2表
insert into test1 (a,b) select c2,c3 from test2 where c1>=200;

mysql> select * from test1;
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
|    2 |    2 |
|    2 |    2 |
|  201 |  202 |
|  301 |  302 |
|  401 |  402 |

mysql> select * from test2;
+------+------+------+
| c1   | c2   | c3   |
+------+------+------+
|  100 |  101 |  102 |
|  200 |  201 |  202 |
|  300 |  301 |  302 |
|  400 |  401 |  402 |
+------+------+------+
4 rows in set (0.00 sec)

mysql> delete t1 from test1 t1,test2 t2 where t1.a=t2.c2;
Query OK, 3 rows affected (0.00 sec)

mysql> select * from test1;
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
|    2 |    2 |
|    2 |    2 |
+------+------+
3 rows in set (0.00 sec)

從上面的輸出中能夠看到test1表中3條記錄被刪除了。

示例2
delete t2,t1 from test1 t1,test2 t2 where t1.a=t2.c2;

同時對2個表進行刪除,條件是test.a=test.c2的記錄

看一下運行效果:

-- 刪除test1
drop table if exists test1;
-- 建立test1
create table test1(a int,b int);
-- 刪除test2
drop table if exists test2;
-- 建立test2
create table test2(c1 int,c2 int,c3 int);
-- 向test2中插入數據
insert into test2 values (100,101,102),(200,201,202),(300,301,302),(400,401,402);
-- 向test1中插入數據
insert into test1 (a,b) select 1,1 union all select 2,2 union all select 2,2;
-- 向test1插入數據,數據來源於test2表
insert into test1 (a,b) select c2,c3 from test2 where c1>=200;

mysql> select * from test1;
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
|    2 |    2 |
|    2 |    2 |
|  201 |  202 |
|  301 |  302 |
|  401 |  402 |
+------+------+
6 rows in set (0.00 sec)

mysql> select * from test2;
+------+------+------+
| c1   | c2   | c3   |
+------+------+------+
|  100 |  101 |  102 |
|  200 |  201 |  202 |
|  300 |  301 |  302 |
|  400 |  401 |  402 |
+------+------+------+
4 rows in set (0.00 sec)

mysql> delete t2,t1 from test1 t1,test2 t2 where t1.a=t2.c2;
Query OK, 6 rows affected (0.00 sec)

mysql> select * from test1;
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
|    2 |    2 |
|    2 |    2 |
+------+------+
3 rows in set (0.00 sec)

mysql> select * from test2;
+------+------+------+
| c1   | c2   | c3   |
+------+------+------+
|  100 |  101 |  102 |
+------+------+------+
1 row in set (0.00 sec)

從輸出中能夠看出test1和test2總計6條記錄被刪除了。

平時咱們用的比較多的方式是delete from 表名這種語法,上面咱們介紹了再delete後面跟上表名的用法,你們能夠在回顧一下,加深記憶。

使用truncate刪除

語法
truncate 表名;

drop,truncate,delete區別

  • drop (刪除表):刪除內容和定義,釋放空間,簡單來講就是把整個表去掉,之後要新增數據是不可能的,除非新增一個表。

    drop語句將刪除表的結構被依賴的約束(constrain),觸發器(trigger)索引(index),依賴於該表的存儲過程/函數將被保留,但其狀態會變爲:invalid。

    若是要刪除表定義及其數據,請使用 drop table 語句。

  • truncate (清空表中的數據):刪除內容、釋放空間但不刪除定義(保留表的數據結構),與drop不一樣的是,只是清空表數據而已。

    注意:truncate不能刪除具體行數據,要刪就要把整個表清空了。

  • delete (刪除表中的數據):delete 語句用於刪除表中的行。delete語句執行刪除的過程是每次從表中刪除一行,而且同時將該行的刪除操做做爲事務記錄在日誌中保存,以便進行進行回滾操做。

    truncate與不帶where的delete :只刪除數據,而不刪除表的結構(定義)

    truncate table 刪除表中的全部行,但表結構及其列、約束、索引等保持不變。

    對於由foreign key約束引用的表,不能使用truncate table ,而應使用不帶where子句的delete語句。因爲truncate table 記錄在日誌中,因此它不能激活觸發器。

    delete語句是數據庫操做語言(dml),這個操做會放到 rollback segement 中,事務提交以後才生效;若是有相應的 trigger,執行的時候將被觸發。

    truncate、drop 是數據庫定義語言(ddl),操做當即生效,原數據不放到 rollback segment 中,不能回滾,操做不觸發 trigger。

    若是有自增列,truncate方式刪除以後,自增列的值會被初始化,delete方式要分狀況(若是數據庫被重啓了,自增列值也會被初始化,數據庫未被重啓,則不變)

  • 若是要刪除表定義及其數據,請使用 drop table 語句

  • 安全性:當心使用 drop 和 truncate,尤爲沒有備份的時候,不然哭都來不及

  • 刪除速度,通常來講: drop> truncate > delete

drop truncate delete
條件刪除 不支持 不支持 支持
刪除表結構 支持 不支持 不支持
事務的方式刪除 不支持 不支持 支持
觸發觸發器

Mysql系列目錄

  1. <a target="_blank" href="https://mp.weixin.qq.com/s?__biz=MzA5MTkxMDQ4MQ==&mid=2648933257&idx=1&sn=0f0086a2465a2fcae13d3fea65064803&chksm=88621bb7bf1592a1ac94fe4107ba1ef26a0fa97e1bf9aea7279009d8bd240f1ef7d27aa10393&token=1876080189&lang=zh_CN#rd">第1天:mysql基礎知識</a>
  2. <a target="_blank" href="https://mp.weixin.qq.com/s?__biz=MzA5MTkxMDQ4MQ==&mid=2648933270&idx=1&sn=409080e17352da2035b0bfdf63ccdfde&chksm=88621ba8bf1592beb2ef6106d6bf9f3eccd48d6814c7031f36e3c8be68821f17cf065129688c&token=1876080189&lang=zh_CN#rd"> 第2天:詳解mysql數據類型(重點)</a>
  3. <a target="_blank" href="https://mp.weixin.qq.com/s?__biz=MzA5MTkxMDQ4MQ==&mid=2648933279&idx=1&sn=f8591b95362cb3c352d895b1289d665a&chksm=88621ba1bf1592b72a43a62e3f310695e8b87f17932d052145622c3edbb70ef8cb987849fc3e&token=516655478&lang=zh_CN#rd"> 第3天:管理員必備技能(必須掌握)</a>
  4. <a target="_blank" href="https://mp.weixin.qq.com/s?__biz=MzA5MTkxMDQ4MQ==&mid=2648933289&idx=1&sn=c4f212c312ea86e08ad322caddd05e38&chksm=88621b97bf159281156ee3be510a1a15234531d2c97d66957e67377829ab23779809ea55bbde&token=1484565200&lang=zh_CN#rd"> 第4天:DDL常見操做</a>

mysql系列大概有20多篇,喜歡的請關注一下!

相關文章
相關標籤/搜索