看看MySQL更新数据、删除数据

coldplay.xixi
Lepaskan: 2021-03-21 14:14:46
ke hadapan
1464 orang telah melayarinya

看看MySQL更新数据、删除数据

(1)更新数据
(2)删除数据

(免费学习推荐:mysql视频教程


(1)、更新数据

MySQL中使用update语句更新表中的记录,可以更新特定的行或者同事更新所有的行,基本语法如下:

update table_nameset column_name1 = value1,column_name2 = value2,.....,column_namen = valuenwhere (condition);
Salin selepas log masuk

【例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)
Salin selepas log masuk
  • 保证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)
Salin selepas log masuk

(2)、删除数据

从数据表删除数据使用delete语句,允许使用where子句指定删除条件。delete语句的基本语法格式如下;

delete from table_name [where < condition>]
Salin selepas log masuk
  • 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)
Salin selepas log masuk

【例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)
Salin selepas log masuk

【例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)
Salin selepas log masuk
  • 如果想删除表中的所有记录,还可以使用truncate table语句,truncate将直接删除原来的表,并重新创建一个表,其语法格式为truncate table table_name。truncate直接删除表而不是删除记录,因此执行速度比delete快。

相关免费学习推荐:mysql数据库(视频)

Atas ialah kandungan terperinci 看看MySQL更新数据、删除数据. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:csdn.net
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!