說明
DML(Data Manipulation Language)數(shù)據(jù)操作語言,是指對數(shù)據(jù)庫進行增刪改的操作指令,主要有INSERT、UPDATE、DELETE三種,代表插入、更新與刪除,這是學(xué)習(xí)MySQL必要掌握的基本知識。
方語法中 [] 中內(nèi)容可以省略。
INSERT操作
逐行插入
語法格式如下:
insert into t_name[(column_name1,columnname_2,...)] values (val1,val2);
或者
insert into t_name set column_name1 = val1,column_name2 = val2;
1、字段名稱和值需要保證數(shù)量一直,類型一直,位置一 一對應(yīng),否則可能導(dǎo)致異常。
2、not null的字段需要保證有插入的值,否則會報非空的異常信息。允許null的字段如果不想輸入數(shù)據(jù),字段和值都不出現(xiàn),或者value用null代替。
3、數(shù)值類型,值不需要用單引號括起來,其他的如字符型或日期類型,值需要用單引號括起來;
4、如果表名后面的column_name 省略不寫,則代表覆蓋該表的所有字段。值的順序和表中字段順序須保持一致。
5、上述第二種語法的寫法更繁瑣,現(xiàn)在比較少使用。
測試一下:
mysql> desc `user1`;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| age | int(11) | NO | | 0 | |
| address | varchar(255) | YES | | NULL | |
+---------+--------------+------+-----+---------+----------------+
4 rows in set
mysql> insert into `user1`(name,age,address) values('brand',20,'fuzhou');
Query OK, 1 row affected
mysql> insert into `user1`(age,address) values(20,'fuzhou');
1364 - Field 'name' doesn't have a default value
mysql> insert into `user1` values('sol',21,'xiamen');
1136 - Column count doesn't match value count at row 1
mysql> insert into `user1` values(null,'sol',21,'xiamen');
Query OK, 1 row affected
mysql> select * from `user1`;
+----+-------+-----+---------+
| id | name | age | address |
+----+-------+-----+---------+
| 3 | brand | 20 | fuzhou |
| 4 | sol | 21 | xiamen |
+----+-------+-----+---------+
2 rows in set
批量插入
語法格式如下:
insert into t_name [(column_name1,column_name2)] values (val1_1,val1_2),(val2_1,val2_2)...);
或者
insert into t_name [(column_name1,column_name2)] select o_name1,o_name2 from o_t_name [where condition];
1、上述第一個語法,values 后面的值個數(shù)需要同等配對 column的數(shù)量,可以設(shè)置多個,逗號隔開,提高數(shù)據(jù)插入效率。
2、第二個語法,select查詢的字段和插入數(shù)據(jù)的字段數(shù)量、順序、類型需要一致。 insert的字段可以省略,代表插入t_name表所有字段。條件可選。
測試一下:
mysql> insert into `user1`(name,age,address) values('brand',20,'fuzhou'),('sol',21,'xiamen');
Query OK, 2 rows affected
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from `user1`;
+----+-------+-----+---------+
| id | name | age | address |
+----+-------+-----+---------+
| 5 | brand | 20 | fuzhou |
| 6 | sol | 21 | xiamen |
+----+-------+-----+---------+
2 rows in set
mysql> desc `user2`;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| age | int(11) | NO | | 0 | |
| address | varchar(255) | YES | | NULL | |
| sex | int(11) | NO | | 1 | |
+---------+--------------+------+-----+---------+----------------+
5 rows in set
mysql> insert into `user2` (name,age,address,sex) select name,age,address,null from `user1`;
Query OK, 2 rows affected
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from `user2`;
+----+-------+-----+---------+------+
| id | name | age | address | sex |
+----+-------+-----+---------+------+
| 7 | brand | 20 | fuzhou | 1 |
| 8 | sol | 21 | xiamen | 1 |
+----+-------+-----+---------+------+
2 rows in set
UPDATE操作
數(shù)據(jù)更新
語法格式如下:
update t_name [[as] alias] set [ alias.]column_name1 = val1,[alias.]column_name2 = val2 [where condition];
1、alias 是別名的意思,別名越簡單識別性越強越好,容易辨認(rèn),方便操作,沒有別名情況下,表名就是別名
2、as alias 中as也是可選的,where 條件也是可選的,所以用戶可以選擇需要的,符合特定條件的部分?jǐn)?shù)據(jù)進行更新。
測試一下:
mysql> select * from `user2`;
+----+-------+-----+---------+------+
| id | name | age | address | sex |
+----+-------+-----+---------+------+
| 7 | brand | 20 | fuzhou | NULL |
| 8 | sol | 21 | xiamen | NULL |
+----+-------+-----+---------+------+
2 rows in set
mysql> update `user2` as u2 set u2.name = 'hero',u2.age=23,u2.sex=1 where id=7;
Query OK, 1 row affected
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from `user2`;
+----+------+-----+---------+------+
| id | name | age | address | sex |
+----+------+-----+---------+------+
| 7 | hero | 23 | fuzhou | 1 |
| 8 | sol | 21 | xiamen | NULL |
+----+------+-----+---------+------+
2 rows in set
還有一種方式是同時更新多個表,使用不同的別名以及一些條件去限制,不過不建議這么做,操作易錯,并且不好維護。
DELETE操作
delete方式刪除
語法格式如下:
delete [alias] from t_name [[as] alias] [where condition];
1、跟上面一樣,alias代表別名,沒有別名情況下,表名就是別名
2、如果表設(shè)置了別名,則delete后面必須跟上別名,否則數(shù)據(jù)庫會報異常。
測試一下:
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、如果刪除表中所有的數(shù)據(jù),則后面不帶上where條件即可,不過要謹(jǐn)慎使用喲。
mysql> select * from `user2`;
+----+-------+-----+----------+-----+
| id | name | age | address | sex |
+----+-------+-----+----------+-----+
| 8 | sol | 21 | xiamen | 0 |
| 10 | brand | 21 | fuzhou | 1 |
| 11 | helen | 20 | quanzhou | 0 |
+----+-------+-----+----------+-----+
3 rows in set
mysql> delete from `user2`;
Query OK, 3 rows affected
mysql> select * from `user2`;
Empty set
truncate方式刪除
語法格式如下:
mysql> select * from `user2`;
+----+-------+-----+----------+-----+
| id | name | age | address | sex |
+----+-------+-----+----------+-----+
| 12 | brand | 21 | fuzhou | 1 |
| 13 | helen | 20 | quanzhou | 0 |
| 14 | sol | 21 | xiamen | 0 |
+----+-------+-----+----------+-----+
3 rows in set
mysql> truncate `user2`;
Query OK, 0 rows affected
mysql> select * from `user2`;
Empty set
看起來跟delete很像,但是重新插入數(shù)據(jù)會發(fā)現(xiàn),他的自增主鍵會重新從1開始,但是delete的是直接在原來的所以自增值之后往上加??聪旅鎖d字段。
mysql> insert into `user2` (name,age,address,sex) values('brand',21,'fuzhou',1),('helen',20,'quanzhou',0),('sol',21,'xiamen',0);
Query OK, 3 rows affected
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from `user2`;
+----+-------+-----+----------+-----+
| id | name | age | address | sex |
+----+-------+-----+----------+-----+
| 1 | brand | 21 | fuzhou | 1 |
| 2 | helen | 20 | quanzhou | 0 |
| 3 | sol | 21 | xiamen | 0 |
+----+-------+-----+----------+-----+
3 rows in set
那 truncate 和 delete有什么區(qū)別呢?我們來梳理下。
truncate和delete的比較
1、truncate 指的是清空表的數(shù)據(jù)、釋放表的空間,但不刪除表的架構(gòu)定義(表結(jié)構(gòu))。因為不包含Where條件,所以不是刪除具體行,而是將整個表清空了。
2、而delete 語句是刪除表中的數(shù)據(jù)行,可以在后面帶上條件控制刪除的維度、范圍,它每次從表中刪除一行,會同時將該行的刪除操作作為事務(wù)保存在日志中,用于進行可能的回滾操作。
3、truncate 和 delete 一樣的地方是:只是刪除數(shù)據(jù),涉及到的表結(jié)構(gòu)及其列、約束、索引等均不會變。
4、如果被外鍵 foreign key 約束,不能使用truncate ,只能使用不帶where子句的delete語句。
5、truncate 操作會記錄在日志中,delete操作會放到 rollback segement 中,執(zhí)行時要等事務(wù)被commit才會生效;所以delete 會觸發(fā)刪除觸發(fā)器(如果有的話),truncate 不會。
6、如果像上面我們測試的那樣,包含自增字段,truncate方式清空之后,自增列的值會被初始化從1開始。
delete方式要分情況判斷(如果數(shù)據(jù)全部delete,數(shù)據(jù)庫未被重啟,則按照之前max+1;數(shù)據(jù)庫重啟了,則一樣會重新開始計算自增列的初始值)。
7、還有drop,drop語句會刪除表包括 結(jié)構(gòu)、數(shù)據(jù)、依賴該表的約束(constrain),觸發(fā)器(trigger)索引(index)等。
以上就是MySQL數(shù)據(jù)操作-DML語句的使用的詳細(xì)內(nèi)容,更多關(guān)于MySQL dml語句的資料請關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:- MySQL的DML語言操作實例
- 詳解mysql DML語句的使用
- MySQL DML語句整理匯總
- 快速學(xué)習(xí)MySQL基礎(chǔ)知識
- MySQL學(xué)習(xí)之?dāng)?shù)據(jù)庫操作DML詳解小白篇