Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > Oracle
| Search | Today's Posts | Mark Forums Read
Oracle General Oracle database discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Oracle section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 21st, 2004, 09:02 PM
Authorized User
Join Date: Jul 2003
Location: , , .
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to make data unique?

I want to creat a primary for a table,for example,there is a table named "test" which include field a1,a2,a3,...,I want to make field a1 and a2 to become a primary key,but there is a question,I make primary key failed,because combination of field a1 and a2 raise some same records.I want to know how to erase these same records? The same records are about 5000 records,I can't delete them manually,I want to delete them automatically,for example,if there is five same records,I delete four,and reserve one. How to realize this function? I am a newbie for Oracle,please give detail steps.

Old December 22nd, 2004, 10:04 AM
Registered User
Join Date: Sep 2003
Location: dammam, ad, Saudi Arabia.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts

Dear friend,
You can try this to delete all repeated records

delete test t1
where (select count(*) from test t2 where t1.a1=t2.a1 and t1.a2=t2.a2) >1;

and if you want to remain one record from the repeated records you can try this
tigger :

cursor c1 is
select a1,a2,count(*) from test group by a1,a2;
 for c1_r in c1 loop
   if c1_r.co >1 then
    for i in 1..(c1_r.co-1) loop
      delete test
       where test.a1=c1_co.a1 and test.a2=c1_co.a2 and rownum=1;
    end loop;
end loop;

Waheed rashwan
Old January 3rd, 2005, 12:55 AM
Authorized User
Join Date: Dec 2004
Location: , , India.
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts

Simply use,
delete from test where rowid not in (select min(rowid) from test group by a1, a2);

It is the most fastest and efficient way to delete all the duplicate records.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pulling unique data from 1 table insert into anoth shawn.bordeaux Classic ASP Basics 0 November 9th, 2008 10:11 PM
Is uniqueidentifier is always unique ? vinod_yadav1919 SQL Server 2000 4 May 30th, 2008 11:31 AM
Unique Records arholly Access 9 December 14th, 2006 08:22 AM
How do you make a Data Grid Hyperlink Column pass KarmenC ASP.NET 1.x and 2.0 Application Design 5 January 13th, 2005 11:43 AM
How to make hierarchical data "lay flat" using SQL tinlong SQL Language 1 August 1st, 2003 01:38 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.