MySQL:截断表与从表中删除

标签 mysql sql database truncate sql-delete

我们什么时候使用 DELETE 命令与 TRUNCATE 命令?我试图在 Internet 上查找,但两个命令都删除了数据;我无法区分。

最佳答案

从表中删除

1. DELETE is a DML Command.
2. DELETE statement is executed using a row lock, each row in the table is locked for deletion.
3. We can specify filters in where clause
4. It deletes specified data if where condition exists.
5. Delete activates a trigger because the operation are logged individually.
6. Slower than truncate because, it keeps logs.
7. Rollback is possible.
8. LIMIT clause can also be used to set a limit on the number of rows to be deleted.
9. ORDER BY clause can be used in DELETE statement. In this case, the rows are deleted in the specified order.

截断表

1. TRUNCATE is a DDL command.
2. TRUNCATE TABLE always locks the table and page but not each row.
3. Cannot use Where Condition.
4. It Removes all the data reset the auto increment number.
5. TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions.
6. Faster in performance wise, because it doesn't keep any logs.
7. Rollback is possible.
8. Cannot use LIMIT and ORDER BY.

DELETETRUNCATE 都可以在与 TRANSACTION 一起使用时回滚。 如果存在自增主键,truncate 会重置计数器。

关于MySQL:截断表与从表中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27184221/

相关文章:

sql - 在 GROUP BY 查询中检索备用属性值?

php - 在获取中使用变量

java - 为什么记录集最初只向前

database - 更新 liferay 数据库中的用户信息

php - 无法将 Latin1 转换为 utf-8?不是编码员

mysql - 如何在函数中使用变量

mysql - 使用选择子查询将列更新到同一个表

sql - 复杂的枢轴

sql - 如何从另一个表中获取引用值?

database - 在小型 Web 应用程序中存储 session 中的数据库连接