> ## Documentation Index
> Fetch the complete documentation index at: https://villagesql.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# MySQL DELETE

> MySQL 的 DELETE 语句如何删除行，为什么您应该先把 WHERE 作为 SELECT 运行，以及 DELETE 与 TRUNCATE TABLE 的区别。

> [card] [VillageSQL is a drop-in replacement for MySQL with extensions.](https://villagesql.com/docs/en/8.4/stable/install-server.md)
>
> All examples on this page work on VillageSQL. Install Now →

`DELETE` 删除整行。在[事务](https://villagesql.com/docs/zh/tutorial/transactions.md)之外没有撤销，所以值得养成的习惯是先写 `WHERE`，看看它选出了什么。

这些示例使用来自 [INSERT](https://villagesql.com/docs/zh/tutorial/insert.md) 的 `sakila_practice` 数据库。开始之前，请再次运行那里的设置代码块，然后用 `mysql -u root -p sakila_practice` 打开客户端。

## DELETE 的工作原理

```sql theme={null}
DELETE FROM TABLE_NAME
WHERE CONDITION;
```

把 `TABLE_NAME` 换成表，把 `CONDITION` 换成挑选要删除的行的测试。

没有列列表。`DELETE` 删除的是整行，所以要清空 1 列，用的是把它设为 NULL 的 [UPDATE](https://villagesql.com/docs/zh/tutorial/update.md)。

## 示例

### 先看，再删

先把这个 `WHERE` 放进一条 `SELECT`，读一读您即将失去的行。

```sql theme={null}
SELECT member_id, first_name, last_name FROM member WHERE active = 0;
```

```text theme={null}
+-----------+------------+-----------+
| member_id | first_name | last_name |
+-----------+------------+-----------+
|         3 | Linda      | Williams  |
+-----------+------------+-----------+
1 row in set (0.00 sec)
```

一行，而且正是您想要的那一行。现在改变语句的开头，保持 `WHERE` 不动。

```sql theme={null}
DELETE FROM member WHERE active = 0;
```

```text theme={null}
Query OK, 1 row affected (0.00 sec)
```

```sql theme={null}
SELECT member_id, first_name, last_name, active FROM member;
```

```text theme={null}
+-----------+------------+-----------+--------+
| member_id | first_name | last_name | active |
+-----------+------------+-----------+--------+
|         1 | Mary       | Smith     |      1 |
|         2 | Patricia   | Johnson   |      1 |
+-----------+------------+-----------+--------+
2 rows in set (0.00 sec)
```

`DELETE` 只报告受影响的行数，没有别的。`UPDATE` 会打印带有 `Rows matched` 和 `Changed` 的第二行；`DELETE` 没有对应的东西。

### 什么都不匹配的 WHERE 不是错误

```sql theme={null}
DELETE FROM member WHERE member_id = 99;
```

```text theme={null}
Query OK, 0 rows affected (0.00 sec)
```

没有出错，也没有发生任何事。在一个您预期会生效的删除之后看到 `0 rows affected`，意味着 `WHERE` 是错的，而语句不会告诉您这一点。

## 没有 WHERE 它会清空表

```sql theme={null}
DELETE FROM member;
```

```text theme={null}
Query OK, 2 rows affected (0.00 sec)
```

```sql theme={null}
SELECT COUNT(*) AS members FROM member;
```

```text theme={null}
+---------+
| members |
+---------+
|       0 |
+---------+
1 row in set (0.00 sec)
```

安全更新模式拒绝这种形式，正如它拒绝 `WHERE` 中没有键的 `UPDATE` 一样。[UPDATE](https://villagesql.com/docs/zh/tutorial/update.md) 展示了它引发的 `ERROR 1175`，而 `mysql -u root -p --safe-updates sakila_practice` 会为整个会话打开它。

## TRUNCATE 以不同的方式清空表

表是空的，所以插入一行并看看它的 id。

```sql theme={null}
INSERT INTO member (first_name, last_name, joined) VALUES ('Sam', 'Reed', '2026-06-01');
```

```text theme={null}
Query OK, 1 row affected (0.00 sec)
```

```sql theme={null}
SELECT member_id, first_name FROM member;
```

```text theme={null}
+-----------+------------+
| member_id | first_name |
+-----------+------------+
|         4 | Sam        |
+-----------+------------+
1 row in set (0.00 sec)
```

计数器从 `DELETE` 删除的那些行接着往下数。`TRUNCATE TABLE` 不会。

```sql theme={null}
TRUNCATE TABLE member;
```

```text theme={null}
Query OK, 0 rows affected (0.00 sec)
```

```sql theme={null}
INSERT INTO member (first_name, last_name, joined) VALUES ('Sam', 'Reed', '2026-06-01');
```

```text theme={null}
Query OK, 1 row affected (0.00 sec)
```

```sql theme={null}
SELECT member_id, first_name FROM member;
```

```text theme={null}
+-----------+------------+
| member_id | first_name |
+-----------+------------+
|         1 | Sam        |
+-----------+------------+
1 row in set (0.00 sec)
```

`TRUNCATE` 删除表并重建一个空表，这就是为什么不管它删除了多少行都报告 `0 rows affected`，也是为什么计数器重新开始。它不接受 `WHERE`，不能回滚，也不触发 `DELETE` 触发器。用 `TRUNCATE` 把表重置为空，其他一切都用 `DELETE`。

## 总结

* 写 `DELETE FROM table WHERE condition`，并先把 `WHERE` 作为 `SELECT` 运行。
* 预期得到一个行数，没有 `Rows matched` 行。
* 把 `0 rows affected` 读作 `WHERE` 什么都没找到，而不是失败。
* 省略 `WHERE`，表就会被清空，没有确认步骤。
* 用 `TRUNCATE TABLE` 清空并重置表，并知道它无法撤销。

## 另请参阅

* [UPDATE](https://villagesql.com/docs/zh/tutorial/update.md) — 修改行，以及安全更新保护
* [WHERE](https://villagesql.com/docs/zh/tutorial/where.md) — 决定哪些行被删除的子句
* [事务](https://villagesql.com/docs/zh/tutorial/transactions.md) — 撤回 DELETE 的唯一方法
* [MySQL 中的软删除](https://villagesql.com/docs/zh/guides/soft-deletes.md) — 把行标记为已删除而不是真的删除


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.