oracle中delte、truncate和drop的区别

1.种类:首先,delete是dml语句,truncate和drop是ddl语句。

2.表和索引所占的空间

truncate表后,表和索引的大小恢复到所占空间初始大小。

delete不改变表和索引所占空间的大小。

drop将表和索引所占用的空间全部释放。

3.删除速度

drop > truncate > delete

4.结构

truncate与不带where条件的delete删除表数据,而不删除表的结构。

drop 语句删除表的结构,包括约束,触发器,索引。

依赖该表的存储过程和函数被保留,但是状态为invalid。

5.truncate与delete

truncate在功能上与不带where子句的delete相同,都是删除表中的全部行。但是truncate更快,且使用的系统和事务日志资源少。

delete语句每删除一行,并在事务日志中为所删除的每行记录一项。

truncate table通过释放存储表数据所使用的的数据页来删除数据,并且只在事务日志中记录页的释放。

6.对于由foreign key约束引用的表,不能用truncate table,而应该使用不带where子句的delete语句。由于truncate table不记录在日志中,所以它不能激活触发器。

7.delete

delete语句不影响表所占用的extent,high watermark保持不变。

8.truncate

只删除表数据,保留索引,约束等表结构。  缺省情况下将空间释放到minextents个extent,除非使用reuse storage;

9.drop

删除表结构以及表数据,并将所占用的空间全部释放。

ocp考题:

Choose two.

You execute this command:

TRUNCATE TABLE dept;

Which two are true?

A) It drops any triggers defined on the table.

B) It retains the indexes defined on the table.

C) It retains the integrity constraints defined on the table.

D) A ROLLBACK statement can be used to retrieve the deleted data.

E) It always retains the space used by the removed rows.

F) A FLASHBACK TABLE statement can be used to retrieve the deleted data.

Correct Answer: BC

Which statement is true about TRUNCATE and DELETE?

A.  For large tables TRUNCATE is faster than DELETE.

B.  For tables with multiple indexes and triggers is faster than TRUNCATE.

C.  You can never TRUNCATE a table if foreign key constraints will be violated.

D.  You can never tows from a table if foreign key constraints will be violated.

135. Which two statements are true about truncate and delete?  

A. the result of a delete can be undone by issuing a rollback

B. delete can use a where clause to determine which row(s) should be removed.

C. TRUNCATE can use a where clause to determine which row(s) should be removed.

D. truncate leavers any indexes on the table in an UNUSABLE STATE.

E. the result of a truncate can be undone by issuing a ROLLBACK.

答案:AB

猜你喜欢

转载自www.cnblogs.com/liang-ning/p/11988725.html