共计 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 问一下细节