• 中文
    • English
  • 注册
  • 查看作者
  • 4-5:MySQL 单表删除记录DELETE

    删除记录(单标删除)的语法结构是:

    DELETE FROM tbl_name [WHERE where_condition]

    比如删除id为6的用户记录:

    mysql> DELETE FROM users WHERE id = 6;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> SELECT * FROM users;
    +----+----------+----------------------------------+-----+------+
    | id | username | password                         | age | sex  |
    +----+----------+----------------------------------+-----+------+
    |  1 | Tom      | 123                              |  29 |    0 |
    |  2 | John     | 123                              |  38 |    0 |
    |  3 | Tom      | 123                              |  27 |    0 |
    |  4 | John     | 123                              |  35 |    0 |
    |  5 | John     | 123                              |  10 |    0 |
    |  7 | Rose     | 202cb962ac59075b964b07152d234b70 |   8 |    0 |
    |  8 | BEn      | 456                              |  17 |    0 |
    +----+----------+----------------------------------+-----+------+
    7 rows in set (0.00 sec)

    可以看到id为6的用户已经被删除,如果此时再插入一条记录,id会为9,而不是6:

    mysql> INSERT users VALUEs(NULL,'111','222',33,NULL);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> SELECT * FROM users;
    +----+----------+----------------------------------+-----+------+
    | id | username | password                         | age | sex  |
    +----+----------+----------------------------------+-----+------+
    |  1 | Tom      | 123                              |  29 |    0 |
    |  2 | John     | 123                              |  38 |    0 |
    |  3 | Tom      | 123                              |  27 |    0 |
    |  4 | John     | 123                              |  35 |    0 |
    |  5 | John     | 123                              |  10 |    0 |
    |  7 | Rose     | 202cb962ac59075b964b07152d234b70 |   8 |    0 |
    |  8 | BEn      | 456                              |  17 |    0 |
    |  9 | 111      | 222                              |  33 | NULL |
    +----+----------+----------------------------------+-----+------+
    8 rows in set (0.00 sec)

  • 0
  • 0
  • 0
  • 3.1k
  • 请登录之后再进行评论

    登录

    赞助本站

    • 支付宝
    • 微信
    • QQ

    感谢一直支持本站的所有人!

    单栏布局 侧栏位置: