共計 3588 個字符,預計需要花費 9 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
這篇文章主要介紹了 MySQL 中如何更新數據以及刪除數據,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓丸趣 TV 小編帶著大家一起了解一下。
(1)、更新數據
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 子句結束,通過 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)、刪除數據
從數據表刪除數據使用 delete 語句,允許使用 where 子句指定刪除條件。delete 語句的基本語法格式如下;
delete from table_name [where condition]
table_name 指定要執行刪除操作的表。
where 為可選參數,指定刪除條件,如果沒有,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 語句同時刪除多條記錄,在前面 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 將直接刪除原來的表,并重新創建一個表,其語法格式為 truncate table table_name。truncate 直接刪除表而不是刪除記錄,因此執行速度比 delete 快。
感謝你能夠認真閱讀完這篇文章,希望丸趣 TV 小編分享的“MySQL 中如何更新數據以及刪除數據”這篇文章對大家有幫助,同時也希望大家多多支持丸趣 TV,關注丸趣 TV 行業資訊頻道,更多相關知識等著你來學習!
向 AI 問一下細節