The difference between truncate and delete in Oracle ____oracle

Source: Internet
Author: User

Truncate is a unique keyword in Oracle. Its role is to empty a table, and in the deletion of data, and delete some of the differences, so that they can remember:


1, in function, truncate is to empty the contents of a table, it is equivalent to delete from table_name.
2. Delete is a DML operation, truncate is a DDL operation; therefore, deleting the entire table's data with delete produces a lot of roolback, takes up a lot of rollback segments, and truncate does not.
3, in memory, delete data with delete, table space its deleted data table occupied by the space is still, easy to use later, in addition it is "false" delete, the equivalent of Windows with delete delete data is the data into the Recycle Bin, can also restore, Of course, if you reboot the system at this time (OS or RDBMS), it will not be able to recover.
With truncate to clear the data, the space occupied by the table in the memory table space is immediately released, which is equivalent to the deletion of data in Windows with Shift+delete and cannot be recovered.
4, Truncate adjust high water mark and delete not; truncate, the HWM of the table is returned to the location of initial and next (default) delete is not allowed.
5, truncate can only be table,view,synonym to Table,delete.
6. The object of the TRUNCATE table must be in this mode, or have permission to drop any TABLE, and delete is the object must be in this mode or be granted a delete on schema. The permissions for the table or delete any table.
7, in the outer layer, truncate or delete, its occupied space will be released.
8, truncate and delete delete data only, and drop deletes the entire table (structure and data).

Delete usage

Oracle Delete Statements
Version 10.2

Basic Delete Statements
1.Delete all Rows:delete <table_name>
Or
DELETE from <table_name>;
CREATE TABLE T as
SELECT *
From All_tables;

SELECT COUNT (*)
From T;

DELETE from T;

COMMIT;

SELECT COUNT (*)
From T;


2.Delete Selective Rows

DELETE from <table_name>
WHERE <condition>;
CREATE TABLE T as
SELECT *
From All_tables;

SELECT COUNT (*)
From T;

DELETE from T
WHERE table_name like '%map ';

COMMIT;

SELECT COUNT (*)
From T;


3.Delete from A SELECT Statement

DELETE from (<select statement>);
CREATE TABLE T as
SELECT *
From All_tables;

SELECT COUNT (*)
From T;

DELETE from (
SELECT * FROM T WHERE table_name like '%map ');

SELECT COUNT (*)
From T;


4.Delete with returning Clause

DELETE from (<select statement>);
CREATE TABLE T as
SELECT *
From All_tables;

Set Serveroutput on

DECLARE
R Urowid;
BEGIN
DELETE from T
WHERE rownum = 1
returning ROWID into R;

Dbms_output.put_line (R);
End;
/

5.Delete restricted to A Partition

DELETE from <table_name>
PARTITION <partition_name>;
DELETE from Sales PARTITION (q1_2001_invoices);
Delete from A Remote Database Delete from <table_name>@<database_link>
DELETE from t@remote_db;

When you delete large amounts of data (most of the data in a table),

Method:
1. Copy data that does not need to be deleted to a temporary table first
2, Trunc table
3, will not need to delete the data copied back.

Delete only loops once, (check and delete)
The cursor loops two times (check it out first, then cycle it again)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.