Oracle deletes only one duplicate data retained

Source: Internet
Author: User

Duplicate data may be in the following two cases: The first is that only some fields in the table are the same, and the second is that the records in the two rows are the same.

1. Deletion of duplicated data in some fields

1. Query duplicate data

Select Field 1, field 2, count (*) from table name group by field 1, field 2 having count (*)> 1
Example: Select owner from dba_tables group by owner having count (*)> 1;
Select owner from dba_tables group by owner having count (*) = 1; // no duplicate data is found.

2. Delete duplicate data

Delete from table name a where field 1, field 2 in (select field 1, field 2, count (*) from table name group by field 1, field 2 having count (*)> 1)
The efficiency of such deletion execution is very low. For large data volumes, the database may be suspended.
Another efficient method is to insert the queried duplicate data into a temporary table before deleting it.

Create table temporary TABLE
(
Select Field 1, field 2, count (*) as row_num
From table name
Group by field 1, field 2
Having count (*)> 1
);
The above statement creates a temporary table and inserts the queried data into it.
You can perform the following deletion operations:
Delete from table name
Where Field 1, field 2 in (select field 1, field 2 from temporary table );
 
3. Keep the latest record in the duplicate data

In Oracle, rowid is a hidden field that uniquely identifies each record. Therefore, you only need to keep the record with the largest rowid in the duplicate data.

1. Search for redundant duplicate records in the table. Duplicate records are determined based on a single field (Id).

Select * from table where Id in (select Id from table group byId having count (Id)> 1)

 

2. Delete unnecessary duplicate records in the table. Duplicate records are determined based on a single field (Id), leaving only the records with the smallest rowid

DELETE from table WHERE (id) IN (SELECT id FROM table group by id having count (id)> 1) and rowid not in (select min (ROWID) FROM table group by id having count (*)> 1 );

 

3. Search for redundant duplicate records in the table (multiple fields)

Select * from table a where (a. Id, a. seq) in (select Id, seq from table group by Id, seq having count (*)> 1)

 

4. Delete redundant record (multiple fields) in the table, leaving only the records with the smallest rowid

Delete from table a where (. id,. seq) in (select Id, seq from table group by Id, seq having count (*)> 1) and rowid not in (select min (rowid) from table group by Id, seq having count (*)> 1)

 

5. Search for redundant duplicate records (multiple fields) in the table, excluding records with the smallest rowid

Select * from table a where (. id,. seq) in (select Id, seq from table group by Id, seq having count (*)> 1) and rowid not in (select min (rowid) from table group by Id, seq having count (*)> 1)



Query duplicate data:

Select a. rowid, a. * from table name
Where a. rowid! = (
Select max (B. rowid) from table name B
Where a. Field 1 = B. Field 1 and a. Field 2 = B. Field 2 );

Example: selete from dba_tables

Where a. rowid! = (
Select max (rowid) from test B
Where a. owner = B. owner );

Delete from table name
Where a. rowid! = (
Select max (B. rowid) from table name B
Where a. Field 1 = B. Field 1 and a. Field 2 = B. Field 2)

Efficient query using temporary tables

Create table temporary table
(Select a. Field 1, a. Field 2, MAX (a. ROWID) as dataid from formal Table
Group by a. Field 1, a. Field 2 );
Delete from table name
Where a. rowid! =
(Select B. dataid from temporary Table B
Where a. Field 1 = B. Field 1 and
A. Field 2 = B. Field 2 );
Commit;

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.