mysql使用update代替delete做伪删除

使用update代替delete做伪删除

1.先要给生产的表,创建一个状态列

mysql> alter table student add state enum('0','1') default '1';

2.插入数据

mysql> insert into student(sname,sage,sgender) values('t1',20,'f'), ('t2',17,'m'),('t3',18,'m');
mysql> insert into student(sname,sage,sgender) values('t4',20,'f'), ('t5',17,'m'),('t6',18,'m');

3.删除数据  (把状态列改变为0)

mysql> update student set state='0' where sid=6;

4.查询数据 (生产中查询语句添加where条件,只查询state='1'的,这样等于0的就不会显示,达到伪删除的目的。)

mysql> select * from student where state='1';

+------------+-------+------+---------+---------------------+-------+
| sid       | sname | sage | sgender | cometime           | state |
+------------+-------+------+---------+---------------------+-------+
| 0000000001 | t1   |   20 | f       | 2019-12-14 17:21:31 | 1     |
| 0000000002 | t2 |   17 | m       | 2019-12-14 17:21:31 | 1     |
| 0000000003 | t3   |   18 | m       | 2019-12-14 17:21:31 | 1     |
| 0000000004 | t4   |   20 | f       | 2019-12-14 17:21:45 | 1     |
| 0000000005 | t5 |   17 | m       | 2019-12-14 17:21:45 | 1     |
+------------+-------+------+---------+---------------------+-------+

 

你可能感兴趣的:(mysql使用update代替delete做伪删除)