20.删除表中的数据

20.1 实践删除表中的数据

1、命令语法:delete from <表名> where <表达式> 提示:一定要加 where 条件,否则整张表
都删了

实践 1 例如:删除表 test 中编号为 1 的记录
mysql> select * from test;
+----+------+-----------+-------------+
| id | age | name | shouji |
+----+------+-----------+-------------+
| 1 | NULL | oldgirl | NULL |
| 2 | NULL | 老男孩 | NULL |
| 3 | NULL | etiantian | NULL |
| 4 | 24 | pengchun | 13511111111 |
+----+------+-----------+-------------+
4 rows in set (0.00 sec)
mysql> delete from test where id=1;
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+----+------+-----------+-------------+
| id | age | name | shouji |
+----+------+-----------+-------------+
| 2 | NULL | 老男孩 | NULL |
| 3 | NULL | etiantian | NULL |
| 4 | 24 | pengchun | 13511111111 |
+----+------+-----------+-------------+
3 rows in set (0.00 sec)
实践 2 删除表中 name=pengchun 的行
mysql> select * from test;
+----+------+-----------+-------------+
| id | age | name | shouji |
+----+------+-----------+-------------+
| 2 | NULL | 老男孩 | NULL |
| 3 | NULL | etiantian | NULL |
| 4 | 24 | pengchun | 13511111111 |
+----+------+-----------+-------------+
3 rows in set (0.00 sec)
mysql> delete from test where name='pengchun';
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+----+------+-----------+--------+
| id | age | name | shouji |
+----+------+-----------+--------+
| 2 | NULL | 老男孩 | NULL |
| 3 | NULL | etiantian | NULL |
+----+------+-----------+--------+
2 rows in set (0.00 sec)​

20.2 通过 update 伪删除数据

在开发人员开发程序时,页面显示,一般是通过状态来判断的,举例:test 表如下数据

mysql> select * from test;
+----+------+-----------+--------+
| id | age | name | state
+----+------+-----------+--------+
| 2 | NULL | 老男孩 1 |
| 3 | NULL | etiantian | 1 |
+----+------+-----------+--------+
2 rows in set (0.00 sec)
页面正常显示的数据:select * from test where state=1;
删除上述 oldgirl 的记录:update test set state=0 where name=‘oldgirl’;

猜你喜欢

转载自www.cnblogs.com/hackerlin/p/12539904.html