当前位置:网站首页>Explain the difference between Oracle DELETE and TRUNCATE
Explain the difference between Oracle DELETE and TRUNCATE
2022-08-05 16:17:00 【HUAWEI CLOUD】
1. Grammar
delete from table aa truncate table aa code>Second, the difference
Conditions can be written after
delete from, but nottruncate.delete fromrecords are deleted one by one, and each deleted line of records will be entered into the log, andtruncatedeletes the entire page at one time, so onlyRecord page release, in short, delete fromupdate log,truncatebasically not, the transaction log space used is less.delete fromwill leave an empty page after deleting an empty table, andtruncatewill not leave any pages in the table.- When a
DELETEstatement is executed with row locks, the rows in the table are locked for deletion.truncatealways locks tables and pages, not individual rows.- If there is an auto-increment id column generated by
identity, afterdelete fromit still increases from the last number, that is, the seed remains unchanged, andtruncate, the seed will be restored to its original state.truncatedoes not fire the trigger fordeletebecause thetruncateaction does not record individual row deletions.
3. Summary
truncateanddeleteonly delete the data, not the structure (definition) of the table; The dropstatement will drop constraints (constrain), triggers (trigger), indexes (index); Stored procedures/functions that depend on this table will remain, but becomeinvalidstatus.- The
deletestatement isdml, this operation will be placed in rollback segement, it will take effect after the transaction is committed;If there is a correspondingtrigger, it will be triggered when executed;truncate,dropisddl, the operation is immediateEffective, the original data will not be placed in therollback segment, cannot be rolled back, and the operation will not triggertrigger. The deletestatement does not affect theextentoccupied by the table, and the high watermark remains in its original position; obviously thedropstatementFree all the space occupied by the table.Thetruncatestatement by default frees space tominextentsextents,unless reuse storage;truncatewill reset the high watermark (back to the beginning).- Speed, in general:
drop>truncate>delete. - Security: Be careful with
dropandtruncate, especially if there is no backup, otherwise it will be too late to cry. - In use, if you want to delete some data rows, use
delete, and pay attention to thewhereclause.The rollback segment should be large enough; want to drop the table.Of course withdrop; delete all data if you want to keep the table.If it has nothing to do with the transaction, usetruncate.If it is related to a transaction, or if you want to triggertrigger, usedelete; if it is to sort out the fragments inside the table, you can usetruncateto keep up with reuse stroage, and then re-import/insert the data.
边栏推荐
猜你喜欢
随机推荐
2018-9-21recording our team Fanhao-Shen Resignation
EasyCVR calls the stop real-time recording interface, how to solve the problem that the recording address is not returned?
数据库篇——hash索引
拉格朗日对偶问题
playwright录制脚本
【七夕限定盲盒抽奖】一文带你搞懂盲盒抽奖的页面配置
网络通信中的基本概念
SQL注入进阶篇(一)
JPA作持久层操作
分布式深度学习最佳入门(踩坑)指南
【案例】3d相册
注册不足一周 英国议会因数据安全争议停用TikTok
【工具】简道云零代码开发平台
Oracle导数据
学习笔记220—office2016无法登录账号,提示“很抱歉遇到一些临时服务器问题”?
解决FileZilla 报错“无法和 SFTP 服务器建立 FTP 连接,请选择合适的协议”
Redis Series 5: In-depth Analysis of Cluster Cluster Mode
1003 Emergency
【案例】一个熊在跑的动画animation
小伙伴面试之成都创宇知道









