(1)更新數(shù)據(jù)
(2)刪除數(shù)據(jù)
(免費(fèi)學(xué)習(xí)推薦:mysql視頻教程)
(1)、更新數(shù)據(jù)
MySQL中使用update語句更新表中的記錄,可以更新特定的行或者同事更新所有的行,基本語法如下:
update table_nameset column_name1 = value1,column_name2 = value2,.....,column_namen = valuenwhere (condition);
【例1】在person表中,更新id值為11的記錄,將age字段值改為15,將name字段值改為LimMing,SQL語句如下;
mysql> update person -> set age =15,name ='LiMing' -> where id =11;Query OK, 1 row affected (0.05 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from person where id =11;+----+--------+------+---------+| id | name | age | info |+----+--------+------+---------+| 11 | LiMing | 15 | student |+----+--------+------+---------+1 row in set (0.00 sec)
- 保證update以where子句結(jié)束,通過where子句指定被更新的記錄所需要滿足的條件,如果忽略where子句,MySQL將更新表中所有的行。
【例2】在person表中,更新age值為19-22的記錄,將info字段值都改為student,SQL語句如下:
mysql> select * from person where age between 19 and 22;+----+---------+------+------------+| id | name | age | info |+----+---------+------+------------+| 1 | Green | 21 | Lawyer || 2 | Suse | 22 | dancer || 4 | Willam | 20 | sports man || 7 | Dale | 22 | cook || 9 | Harry | 21 | magician || 10 | Harriet | 19 | pianist |+----+---------+------+------------+6 rows in set (0.00 sec)mysql> update person set info='student' where age between 19 and 22;Query OK, 0 rows affected (0.00 sec)Rows matched: 0 Changed: 0 Warnings: 0mysql> select * from person where age between 19 and 22;+----+---------+------+---------+| id | name | age | info |+----+---------+------+---------+| 1 | Green | 21 | student || 2 | Suse | 22 | student || 4 | Willam | 20 | student || 7 | Dale | 22 | student || 9 | Harry | 21 | student || 10 | Harriet | 19 | student |+----+---------+------+---------+6 rows in set (0.00 sec)
(2)、刪除數(shù)據(jù)
從數(shù)據(jù)表刪除數(shù)據(jù)使用delete語句,允許使用where子句指定刪除條件。delete語句的基本語法格式如下;
delete from table_name [where < condition>]
- table_name指定要執(zhí)行刪除操作的表。
- "where"為可選參數(shù),指定刪除條件,如果沒有,delete語句將刪除表中的所有記錄。
【例1】在person表中,刪除id等于11的記錄。
mysql> select * -> from person -> where id =11;+----+--------+------+---------+| id | name | age | info |+----+--------+------+---------+| 11 | LiMing | 15 | student |+----+--------+------+---------+1 row in set (0.00 sec)mysql> delete from person -> where id = 11;Query OK, 1 row affected (0.05 sec)mysql> select * -> from person -> where id = 11;Empty set (0.00 sec)
【例2】在person表中,使用delete語句同時(shí)刪除多條記錄,在前面update語句中將age字段值為19-22的記錄的info字段值修改為student,在這里刪除這些記錄,SQL語句如下:
mysql> select * from person where age between 19 and 22;+----+---------+------+---------+| id | name | age | info |+----+---------+------+---------+| 1 | Green | 21 | student || 2 | Suse | 22 | student || 4 | Willam | 20 | student || 7 | Dale | 22 | student || 9 | Harry | 21 | student || 10 | Harriet | 19 | student |+----+---------+------+---------+6 rows in set (0.00 sec)mysql> delete from person where age between 19 and 22;Query OK, 6 rows affected (0.05 sec)mysql> select * from person where age between 19 and 22;Empty set (0.00 sec)
【例3】刪除person表中所有記錄,SQL語句如下:
mysql> select * from person;+----+---------+------+-----------+| id | name | age | info |+----+---------+------+-----------+| 3 | Mary | 24 | Musician || 5 | Laura | 25 | NULL || 6 | Evans | 27 | secretary || 8 | Edison | 28 | singer || 12 | Beckham | 31 | police |+----+---------+------+-----------+5 rows in set (0.00 sec)mysql> delete from person;Query OK, 5 rows affected (0.05 sec)mysql> select * from person;Empty set (0.00 sec)
- 如果想刪除表中的所有記錄,還可以使用
truncate table
語句,truncate將直接刪除原來的表,并重新創(chuàng)建一個(gè)表,其語法格式為truncate table table_name
。truncate直接刪除表而不是刪除記錄,因此執(zhí)行速度比delete快。
相關(guān)免費(fèi)學(xué)習(xí)推薦:mysql數(shù)據(jù)庫(視頻)