当前位置:网站首页>drop、truncate和delete的用法区别
drop、truncate和delete的用法区别
2022-07-21 17:15:00 【上海市市花】
一、SQL中的语法
1、drop table 表名称 eg: drop table name
2、truncate table 表名称 eg: truncate table name
3、delete from 表名称 where 列名称 = 值 eg: delete from name where test=‘test’
二、drop,truncate,delete区别
1、drop (删除表):删除内容和定义,释放空间。简单来说就是把整个表去掉.以后要新增数据是不可能的,除非新增一个表。
drop语句将删除表的结构被依赖的约束(constrain),触发器(trigger)索引(index);依赖于该表的存储过程/函数将被保留,但其状态会变为:invalid。
2、truncate (清空表中的数据):删除内容、释放空间但不删除定义(保留表的数据结构)。与drop不同的是,只是清空表数据而已。
注意:truncate 不能删除行数据,要删就要把表清空。
3、delete (删除表中的数据):delete 语句用于删除表中的行。delete语句执行删除的过程是每次从表中删除一行,并且同时将该行的删除操作作为事务记录在日志中保存以便进行进行回滚操作。
truncate与不带where的delete :只删除数据,而不删除表的结构(定义)
4、truncate table 删除表中的所有行,但表结构及其列、约束、索引等保持不变。新行标识所用的计数值重置为该列的种子。如果想保留标识计数值,请改用delete。
如果要删除表定义及其数据,请使用 drop table 语句。
5、对于由foreign key约束引用的表,不能使用truncate table ,而应使用不带where子句的delete语句。由于truncate table 记录在日志中,所以它不能激活触发器。
6、执行速度,一般来说: drop> truncate > delete。
7、delete语句是数据库操作语言(dml),这个操作会放到 rollback segement 中,事务提交之后才生效;如果有相应的 trigger,执行的时候将被触发。
truncate、drop 是数据库定义语言(ddl),操作立即生效,原数据不放到 rollback segment 中,不能回滚,操作不触发 trigger。
边栏推荐
- v-7
- Verilog——串行四位加法器和超前四位加法器74HC283
- 自学golang【3.5go语言数组,range关键字】数组的定义方式,使用for循环遍历一维数组
- 十年交易员重磅推荐:简单易操作的突破回调策略
- 软考中级【数据库系统工程师】第1章:计算机系统知识,自学软考笔记,备考2022年5月份软考,计算机硬件系统CPU组成指令寄存器组总线输入输出的程序控制方式计算机体系结构与存储系统加密技术流水线技术
- 文件操作以及相关函数
- Self study golang [3.3go language loop statement] for loop syntax structure, omit initial conditions, omit incremental conditions, omit the application of end conditions
- The difference between RDB and AOF in the persistence mode of redis
- “珠峰”架构强势赋能 第三代荣威RX5/超混eRX5刷新产品力“天花板”
- 去除tinymce中粘贴的样式
猜你喜欢
随机推荐
Verilog - 74lvc161 counter
Verilog -- 74hc194 multifunctional bidirectional shift register
It takes several years for a trading system to be a successful trading system, and how many years for a profit to be stable?
Analysis of location cache module and detailed explanation of OCP monitoring and alarm
Verilog application - 24 second basketball counter
不断提升认知,从而达到交易的最高级别——稳定盈利(终)
MySQL transaction
VS2022无法使用scanf的解决办法,‘scanf‘: This function or variable may be unsafe. Consider using scanf_s instea
Calculation of storage capacity and address space
The difference between RDB and AOF in the persistence mode of redis
指针深度解刨《四》(指针和数组的 “亲密“ 关系)
STM32 Hal library SPI always reads FF. Solve the problem!
Verilog——串行四位加法器和超前四位加法器74HC283
How to open stdio in C h ? How to find the definition of printf?
胜率高达93.98%的TPS交易策略
FastJson、JackJson处理时间、日期问题注意事项
Verilog - Basketball 24s timer
自学golang【第三章:第一个go语言程序】使用goland创建第一个go程序,main函数与init函数,使用go实现运行windows命令,实现cmd的数据输出
MySQL stored procedure
文件操作《二》(5000字总结篇)