首页 > 代码库 > truncate和delete对于回滚的区别

truncate和delete对于回滚的区别

         1delete在事务中可以回滚

mysql>select * from t;

+----+

| id |

+----+

|  1 |

|  2 |

+----+

2 rows inset (0.00 sec)

 

mysql>begin;

Query OK,0 rows affected (0.00 sec)

 

mysql>delete from t where id=2;

Query OK,1 row affected (0.04 sec)

 

mysql>select * from t;

+----+

| id |

+----+

|  1 |

+----+

1 row inset (0.00 sec)

 

mysql>rollback;

Query OK,0 rows affected (0.01 sec)

 

mysql>select * from t;

+----+

| id |

+----+

|  1 |

|  2 |

+----+

2 rows inset (0.00 sec)

 

         2truncate在事务中不能回滚

mysql>begin;

Query OK,0 rows affected (0.00 sec)

 

mysql>truncate table t;

Query OK,0 rows affected (0.03 sec)

 

mysql>rollback;

Query OK,0 rows affected (0.00 sec)

 

mysql>select * from t;

Empty set(0.00 sec)

 

mysql>


truncate和delete对于回滚的区别