首页 > 其他分享 >TRUNCATE、DELETE、DROP的区别

TRUNCATE、DELETE、DROP的区别

时间:2023-03-04 18:13:54浏览次数:37  
标签:回滚 DROP 删除 TRUNCATE drop user table DELETE delete

Truncate、delete、drop区别

一、Truncate使用

truncate user_table; 清空表的数据,但不破坏表结构

二、delete使用

delete from user_table where id = 1; 删除指定数据,要跟where条件(不过业务中大多是逻辑删除,并非物理删除)

三、drop使用(删库跑路)

drop table user_table; # 删表,没一点
drop database test_platform; # 删库,全没

补充

速度上,drop> truncate > delete
对于业务上的需求,我们删除一般会提前定义一个字段为del,通过改变他的值来模拟删除操作
目的就是不展示给用户看,其实他还在库中

  • 挽救程度:
    如果进行误操作的话,delete是有机会进行恢复的:也只有delete操作会进入事务,并可以回滚,truncate和drop就真的只能跑路了
    select * from user_table;
    start transaction;    # 开启事务 
    delete from user_table where id = 1;
    rollback;  # 回滚事务
    commit;  # 提交事务(提交事务后不可以回滚)
    

标签:回滚,DROP,删除,TRUNCATE,drop,user,table,DELETE,delete
From: https://www.cnblogs.com/N-A-N/p/17005743.html

相关文章