zoukankan      html  css  js  c++  java
  • 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’;
  • 相关阅读:
    BZOJ2243: [SDOI2011]染色
    BZOJ3747: [POI2015]Kinoman
    BZOJ1293: [SCOI2009]生日礼物
    BZOJ3626 [LNOI2014]LCA
    BZOJ3514 Codechef MARCH14 GERALD07加强版
    BZOJ3295 [CQOI2011]动态逆序对
    BZOJ2588 [SPOJ10628]Count on a tree
    BZOJ1036 [ZJOI2008]树的统计Count
    CODEVS1490 [CTSC2008]网络管理
    BZOJ1070 [SCOI2007]修车
  • 原文地址:https://www.cnblogs.com/hackerlin/p/12539904.html
Copyright © 2011-2022 走看看