读书笔记--SQL必知必会16--更新和删除数据

时间:2022-06-01 17:06:24

16.1 更新数据

使用UPDATE语句更新或修改表中的数据。必须有足够的安全权限。

  • 更新表中的特定行
  • 更新表中的所有行

使用UPDATE时一定要细心,不要省略WHERE子句。

SET命令用来将新值赋给被更新的列。

基本的UPDATE语句由三部分组成:

  • 要更新的表
  • 列名和他们的新值
  • 确定要更新那些行的过滤条件
MariaDB [sqlbzbh]> SELECT * FROM Customers;
+------------+---------------+----------------------+-----------+------------+----------+--------------+--------------------+-----------------------+
| cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email |
+------------+---------------+----------------------+-----------+------------+----------+--------------+--------------------+-----------------------+
| 1000000001 | Village Toys | 200 Maple Lane | Detroit | MI | 44444 | USA | John Smith | sales@villagetoys.com |
| 1000000002 | Kids Place | 333 South Lake Drive | Columbus | OH | 43333 | USA | Michelle Green | NULL |
| 1000000003 | Fun4All | 1 Sunny Place | Muncie | IN | 42222 | USA | Jim Jones | jjones@fun4all.com |
| 1000000004 | Fun4All | 829 Riverside Drive | Phoenix | AZ | 88888 | USA | Denise L. Stephens | dstephens@fun4all.com |
| 1000000005 | The Toy Store | 4545 53rd Street | Chicago | IL | 54545 | USA | Kim Howard | NULL |
| 1000000006 | Toy Land | 123 Any Street | New York | NY | 11111 | USA | NULL | NULL |
+------------+---------------+----------------------+-----------+------------+----------+--------------+--------------------+-----------------------+
6 rows in set (0.00 sec) MariaDB [sqlbzbh]>
MariaDB [sqlbzbh]> UPDATE Customers SET cust_email = 'kim@thetoystore.com' WHERE cust_id = '1000000005';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [sqlbzbh]>
MariaDB [sqlbzbh]> SELECT cust_email FROM Customers WHERE cust_id = '1000000005';
+---------------------+
| cust_email |
+---------------------+
| kim@thetoystore.com |
+---------------------+
1 row in set (0.00 sec) MariaDB [sqlbzbh]>

更新多个列时,只需要使用一条SET命令,每个‘列=值’对之间用逗号分隔(最后一列不用逗号)。

MariaDB [sqlbzbh]> UPDATE Customers SET cust_contact = 'Sam Roberts', cust_email = 'sam@toyland.com' WHERE cust_id = '1000000006';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [sqlbzbh]>
MariaDB [sqlbzbh]> SELECT cust_contact, cust_email FROM Customers WHERE cust_id = '1000000006';
+--------------+-----------------+
| cust_contact | cust_email |
+--------------+-----------------+
| Sam Roberts | sam@toyland.com |
+--------------+-----------------+
1 row in set (0.00 sec) MariaDB [sqlbzbh]>

要删除某个列的值,可设置它为NULL(假如表定义允许NULL值)。

注意:空字符串用''表示,是一个值,而NULL表示没有值。

MariaDB [sqlbzbh]> UPDATE Customers SET cust_email = NULL WHERE cust_id = '1000000005';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [sqlbzbh]>
MariaDB [sqlbzbh]> SELECT cust_email FROM Customers WHERE cust_id = '1000000005';
+------------+
| cust_email |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec) MariaDB [sqlbzbh]>

16.2 删除数据

使用DELETE语句从一个表中删除(去掉)数据。必须有足够的安全权限。

  • 从表中删除特定的行
  • 从表中删除所有行

DELETE不需要列名或通配符,因为DELETE删除的是整行而不是删除列。

DELETE删除的是表的内容而不是表,DELETE不删除表本身。

使用DELETE时一定要细心,不要省略WHERE子句。

MariaDB [sqlbzbh]> DELETE FROM Customers WHERE cust_id = '1000000006';
Query OK, 1 row affected (0.00 sec) MariaDB [sqlbzbh]>
MariaDB [sqlbzbh]> SELECT * FROM Customers WHERE cust_id = '1000000006';
Empty set (0.00 sec) MariaDB [sqlbzbh]>

如果要删除表中所有行,可使用TRUNCATE TABLE语句,速度更快。

MariaDB [sqlbzbh]> SELECT * FROM CustNew;
+------------+-----------+----------------+-----------+------------+----------+--------------+--------------+------------+
| cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email |
+------------+-----------+----------------+-----------+------------+----------+--------------+--------------+------------+
| 1000000006 | Toy Land | 123 Any Street | New York | NY | 11111 | USA | NULL | NULL |
+------------+-----------+----------------+-----------+------------+----------+--------------+--------------+------------+
1 row in set (0.00 sec) MariaDB [sqlbzbh]>
MariaDB [sqlbzbh]> TRUNCATE TABLE CustNew;
Query OK, 0 rows affected (0.01 sec) MariaDB [sqlbzbh]>
MariaDB [sqlbzbh]> SELECT * FROM CustNew;
Empty set (0.00 sec) MariaDB [sqlbzbh]>

16.3 更新和删除的指导原则

  • 无论怎样,小心使用UPDATE或DELETE语句。
  • 慎重使用不带WHERE的UPDATE或DELETE语句。
  • 保证每个表都有主键。
  • 更新或删除之前,先用SELECT语句进行测试,确保改动准确。
  • 强制实施引用完整性的数据库不允许删除其数据与其他表相关联的行。