看看MySQL更新資料、刪除數據

coldplay.xixi
發布: 2021-03-21 14:14:46
轉載
1464 人瀏覽過

看看MySQL更新資料、刪除數據

(1)更新資料
(2)刪除資料

(免費學習推薦:##mysql視頻教學


(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快。
相關免費學習推薦:

mysql資料庫(影片)

以上是看看MySQL更新資料、刪除數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!