1. 程式人生 > >Oracle中刪除表操作trunc delete和drop的說明

Oracle中刪除表操作trunc delete和drop的說明

相同點:
1.truncate和不帶where子句的delete, 以及drop都會刪除表內的資料。
2.drop,truncate都是DDL語句,執行後會自動提交。

不同點:
1. truncate和 delete只刪除資料不刪除表的結構(定義)
    drop語句將刪除表的結構被依賴的約束(constrain),觸發器(trigger),索引(index); 依賴於該表的儲存過程/函式將保留,但是變為invalid狀態.
2.delete語句是dml,這個操作會放到rollback segement中,事務提交之後才生效;如果有相應的trigger,執行的時候將被觸發.
   truncate,drop是ddl, 操作立即生效,原資料不放到rollback segment中,不能回滾. 操作不觸發trigger.
3.delete語句不影響表所佔用的extent, 高水線(high watermark)保持原位置不動
  顯然drop語句將表所佔用的空間全部釋放
  truncate 語句預設情況下見空間釋放到 minextents個 extent,除非使用reuse storage;   truncate會將高水線復位(回到最開始).
4.速度,一般來說: drop>; truncate >; delete
5.安全性:小心使用drop 和truncate,尤其沒有備份的時候.否則哭都來不及
使用上,想刪除部分資料行用delete,注意帶上where子句. 回滾段要足夠大.
想刪除表,當然用drop
想保留表而將所有資料刪除. 如果和事務無關,用truncate即可. 如果和事務有關,或者想觸發trigger,還是用delete.
如果是整理表內部的碎片,可以用truncate跟上reuse stroage,再重新匯入/插入資料

DML DCL DDL TCL說明:

1. DDL   

 Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples:

  CREATE - to create objects in the database

  ALTER - alters the structure of the database

  DROP - delete objects from the database

  TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed

  COMMENT - add comments to the data dictionary

  RENAME - rename an object

2. DML

  Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples:

  SELECT - retrieve data from the a database

  INSERT - insert data into a table

  UPDATE - updates existing data within a table

  DELETE - deletes all records from a table, the space for the records remain

  MERGE - UPSERT operation (insert or update)

  CALL - call a PL/SQL or Java subprogram

  EXPLAIN PLAN - explain access path to data

  LOCK TABLE - control concurrency

3. DCL

  Data Control Language (DCL) statements. Some examples:

  GRANT - gives user's access privileges to database

  REVOKE - withdraw access privileges given with the GRANT command

4. TCL

  Transaction Control (TCL) statements are used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions.

  COMMIT - save work done

  SAVEPOINT - identify a point in a transaction to which you can later roll back

  ROLLBACK - restore database to original since the last COMMIT

  SET TRANSACTION - Change transaction options like isolation level and what rollback segment to use

轉自:http://blog.sina.com.cn/s/blog_48b7aa030100llz7.html