MySQL全面瓦解5:資料操作-DML
說明
DML(Data Manipulation Language)資料操作語言,是指對資料庫進行增刪改的操作指令,主要有INSERT、UPDATE、DELETE三種,代表插入、更新與刪除,這是學習MySQL必要掌握的基本知識。
與之前的章節一致,下方語法中 [] 中內容可以省略。
INSERT操作
逐行插入
語法格式如下:
1 insert into t_name[(column_name1,columnname_2,...)] values (val1,val2); 2 或者 3 insert into t_name set column_name1 = val1,column_name2 = val2;
1、欄位名稱和值需要保證數量一直,型別一直,位置一 一對應,否則可能導致異常。
2、not null的欄位需要保證有插入的值,否則會報非空的異常資訊。允許null的欄位如果不想輸入資料,欄位和值都不出現,或者value用null代替。
3、數值型別,值不需要用單引號括起來,其他的如字元型或日期型別,值需要用單引號括起來;
4、如果表名後面的column_name 省略不寫,則代表覆蓋該表的所有欄位。值的順序和表中欄位順序須保持一致。
5、上述第二種語法的寫法更繁瑣,現在比較少使用。
測試一下:
1 mysql> desc `user1`; 2 +---------+--------------+------+-----+---------+----------------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +---------+--------------+------+-----+---------+----------------+ 5 | id | bigint(20) | NO | PRI | NULL | auto_increment | 6 | name | varchar(20) | NO | | NULL | | 7 | age | int(11) | NO | | 0 | | 8 | address | varchar(255) | YES | | NULL | | 9 +---------+--------------+------+-----+---------+----------------+ 10 4 rows in set 11 12 mysql> insert into `user1`(name,age,address) values('brand',20,'fuzhou'); 13 Query OK, 1 row affected 14 15 mysql> insert into `user1`(age,address) values(20,'fuzhou'); 16 1364 - Field 'name' doesn't have a default value 17 18 mysql> insert into `user1` values('sol',21,'xiamen'); 19 1136 - Column count doesn't match value count at row 1 20 21 mysql> insert into `user1` values(null,'sol',21,'xiamen'); 22 Query OK, 1 row affected 23 24 mysql> select * from `user1`; 25 +----+-------+-----+---------+ 26 | id | name | age | address | 27 +----+-------+-----+---------+ 28 | 3 | brand | 20 | fuzhou | 29 | 4 | sol | 21 | xiamen | 30 +----+-------+-----+---------+ 31 2 rows in set
批量插入
語法格式如下:
1 insert into t_name [(column_name1,column_name2)] values (val1_1,val1_2),(val2_1,val2_2)...); 2 或者 3 insert into t_name [(column_name1,column_name2)] select o_name1,o_name2 from o_t_name [where condition];
1、上述第一個語法,values 後面的值個數需要同等配對 column的數量,可以設定多個,逗號隔開,提高資料插入效率。
2、第二個語法,select查詢的欄位和插入資料的欄位數量、順序、型別需要一致。 insert的欄位可以省略,代表插入t_name表所有欄位。條件可選。
測試一下:
1 mysql> insert into `user1`(name,age,address) values('brand',20,'fuzhou'),('sol',21,'xiamen'); 2 Query OK, 2 rows affected 3 Records: 2 Duplicates: 0 Warnings: 0 4 5 mysql> select * from `user1`; 6 +----+-------+-----+---------+ 7 | id | name | age | address | 8 +----+-------+-----+---------+ 9 | 5 | brand | 20 | fuzhou | 10 | 6 | sol | 21 | xiamen | 11 +----+-------+-----+---------+ 12 2 rows in set
1 mysql> desc `user2`; 2 +---------+--------------+------+-----+---------+----------------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +---------+--------------+------+-----+---------+----------------+ 5 | id | bigint(20) | NO | PRI | NULL | auto_increment | 6 | name | varchar(20) | NO | | NULL | | 7 | age | int(11) | NO | | 0 | | 8 | address | varchar(255) | YES | | NULL | | 9 | sex | int(11) | NO | | 1 | | 10 +---------+--------------+------+-----+---------+----------------+ 11 5 rows in set 12 13 mysql> insert into `user2` (name,age,address,sex) select name,age,address,null from `user1`; 14 Query OK, 2 rows affected 15 Records: 2 Duplicates: 0 Warnings: 0 16 17 mysql> select * from `user2`; 18 +----+-------+-----+---------+------+ 19 | id | name | age | address | sex | 20 +----+-------+-----+---------+------+ 21 | 7 | brand | 20 | fuzhou | 1 | 22 | 8 | sol | 21 | xiamen | 1 | 23 +----+-------+-----+---------+------+ 24 2 rows in set
UPDATE操作
資料更新
語法格式如下:
1 update t_name [[as] alias] set [ alias.]column_name1 = val1,[alias.]column_name2 = val2 [where condition];
1、alias 是別名的意思,別名越簡單識別性越強越好,容易辨認,方便操作,沒有別名情況下,表名就是別名
2、as alias 中as也是可選的,where 條件也是可選的,所以使用者可以選擇需要的,符合特定條件的部分資料進行更新。
測試一下:
1 mysql> select * from `user2`; 2 +----+-------+-----+---------+------+ 3 | id | name | age | address | sex | 4 +----+-------+-----+---------+------+ 5 | 7 | brand | 20 | fuzhou | NULL | 6 | 8 | sol | 21 | xiamen | NULL | 7 +----+-------+-----+---------+------+ 8 2 rows in set 9 10 mysql> update `user2` as u2 set u2.name = 'hero',u2.age=23,u2.sex=1 where id=7; 11 Query OK, 1 row affected 12 Rows matched: 1 Changed: 1 Warnings: 0 13 14 mysql> select * from `user2`; 15 +----+------+-----+---------+------+ 16 | id | name | age | address | sex | 17 +----+------+-----+---------+------+ 18 | 7 | hero | 23 | fuzhou | 1 | 19 | 8 | sol | 21 | xiamen | NULL | 20 +----+------+-----+---------+------+ 21 2 rows in set
還有一種方式是同時更新多個表,使用不同的別名以及一些條件去限制,不過不建議這麼做,操作易錯,並且不好維護。
DELETE操作
delete方式刪除
語法格式如下:
1 delete [alias] from t_name [[as] alias] [where condition];
1、跟上面一樣,alias代表別名,沒有別名情況下,表名就是別名
2、如果表設定了別名,則delete後面必須跟上別名,否則資料庫會報異常。
測試一下:
mysql> select * from `user2`; +----+------+-----+---------+------+ | id | name | age | address | sex | +----+------+-----+---------+------+ | 7 | hero | 23 | fuzhou | 1 | | 8 | sol | 21 | xiamen | NULL | +----+------+-----+---------+------+ 2 rows in set mysql> delete from `user2` as alias where sex=1; 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as alias where sex=1' at line 1 mysql> delete alias from `user2` as alias where sex=1; Query OK, 1 row affected mysql> select * from `user2`; +----+------+-----+---------+------+ | id | name | age | address | sex | +----+------+-----+---------+------+ | 8 | sol | 21 | xiamen | NULL | +----+------+-----+---------+------+ 1 row in set
3、如果刪除表中所有的資料,則後面不帶上where條件即可,不過要謹慎使用喲。
1 mysql> select * from `user2`; 2 +----+-------+-----+----------+-----+ 3 | id | name | age | address | sex | 4 +----+-------+-----+----------+-----+ 5 | 8 | sol | 21 | xiamen | 0 | 6 | 10 | brand | 21 | fuzhou | 1 | 7 | 11 | helen | 20 | quanzhou | 0 | 8 +----+-------+-----+----------+-----+ 9 3 rows in set 10 11 mysql> delete from `user2`; 12 Query OK, 3 rows affected 13 14 mysql> select * from `user2`; 15 Empty set
truncate方式刪除
語法格式如下:
1 truncate t_name;
1 mysql> select * from `user2`; 2 +----+-------+-----+----------+-----+ 3 | id | name | age | address | sex | 4 +----+-------+-----+----------+-----+ 5 | 12 | brand | 21 | fuzhou | 1 | 6 | 13 | helen | 20 | quanzhou | 0 | 7 | 14 | sol | 21 | xiamen | 0 | 8 +----+-------+-----+----------+-----+ 9 3 rows in set 10 11 mysql> truncate `user2`; 12 Query OK, 0 rows affected 13 14 mysql> select * from `user2`; 15 Empty set
看起來跟delete很像,但是重新插入資料會發現,他的自增主鍵會重新從1開始,但是delete的是直接在原來的所以自增值之後往上加。看下面id欄位。
1 mysql> insert into `user2` (name,age,address,sex) values('brand',21,'fuzhou',1),('helen',20,'quanzhou',0),('sol',21,'xiamen',0); 2 Query OK, 3 rows affected 3 Records: 3 Duplicates: 0 Warnings: 0 4 5 mysql> select * from `user2`; 6 +----+-------+-----+----------+-----+ 7 | id | name | age | address | sex | 8 +----+-------+-----+----------+-----+ 9 | 1 | brand | 21 | fuzhou | 1 | 10 | 2 | helen | 20 | quanzhou | 0 | 11 | 3 | sol | 21 | xiamen | 0 | 12 +----+-------+-----+----------+-----+ 13 3 rows in set
那 truncate 和 delete有什麼區別呢?我們來梳理下。
truncate和delete的比較
1、truncate 指的是清空表的資料、釋放表的空間,但不刪除表的架構定義(表結構)。因為不包含Where條件,所以不是刪除具體行,而是將整個表清空了。
2、而delete 語句是刪除表中的資料行,可以在後面帶上條件控制刪除的維度、範圍,它每次從表中刪除一行,會同時將該行的刪除操作作為事務儲存在日誌中,用於進行可能的回滾操作。
3、truncate 和 delete 一樣的地方是:只是刪除資料,涉及到的表結構及其列、約束、索引等均不會變。
4、如果被外來鍵 foreign key 約束,不能使用truncate ,只能使用不帶where子句的delete語句。
5、truncate 操作會記錄在日誌中,delete操作會放到 rollback segement 中,執行時要等事務被commit才會生效;所以delete 會觸發刪除觸發器(如果有的話),truncate 不會。
6、如果像上面我們測試的那樣,包含自增欄位,truncate方式清空之後,自增列的值會被初始化從1開始。
delete方式要分情況判斷(如果資料全部delete,資料庫未被重啟,則按照之前max+1;資料庫重啟了,則一樣會重新開始計算自增列的初始值)。
7、前面章節我們還學過drop,drop語句會刪除表包括 結構、資料、依賴該表的約束(constrain),觸發器(trigger)索引(index)等。
&n