User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Oracle section within the Web Development category of DaniWeb, a massive community of 397,711 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,399 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Oracle advertiser:
Views: 2160 | Replies: 9
Reply
Join Date: Mar 2007
Posts: 30
Reputation: karan_21584 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
karan_21584 karan_21584 is offline Offline
Light Poster

Question Deleting Same Tuples

  #1  
Mar 23rd, 2007
hai, i m new to this site and my 1st post is this. I have a table which consist of some tuples. i didnt set any primary key. by mistakely i entered the same values for two tuples. i want to delete a single tuple without copying the table to another new table or using DISTINCT keyword... how its possible. My professor told there is an way... and ask me to find out. CAN anyone help me? thanks in advance....CLEAR MY DOUBT IF U GENIUS
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2007
Location: London
Posts: 114
Reputation: davidcairns is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
davidcairns davidcairns is offline Offline
Junior Poster

Re: Deleting Same Tuples

  #2  
Mar 23rd, 2007
You could use the rowid, it's probably the simplest solution. For info the rowid will uniquely identify a row within a table. It is a virtual column that Oracle maintains for all tables. Other than for situations where no primary or unique exists it shouldn't be relied upon too heavily though.

I'll let you figure out how to apply it to this problem
Reply With Quote  
Join Date: Mar 2007
Posts: 30
Reputation: karan_21584 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
karan_21584 karan_21584 is offline Offline
Light Poster

Re: Deleting Same Tuples

  #3  
Mar 26th, 2007
thanks Mr.Davidcairns..could u figure out how to apply it to this problem? plz..thanks in advance
Reply With Quote  
Join Date: Jan 2007
Location: Austin, TX
Posts: 30
Reputation: Memento is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Memento Memento is offline Offline
Light Poster

Re: Deleting Same Tuples

  #4  
Mar 26th, 2007
If you add the rowid pseudo column to your initial select statement you would see that it is different for both rows. The where clause of your update statement would then reference the rowid of the record you want to update.
Dan Moore
www.danmoore.org
Reply With Quote  
Join Date: Feb 2007
Location: London
Posts: 114
Reputation: davidcairns is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
davidcairns davidcairns is offline Offline
Junior Poster

Re: Deleting Same Tuples

  #5  
Mar 27th, 2007
For small sample sizes that is the best way, but for a more generic and scalable approach I would go with the following

DELETE
FROM tablename
WHERE rowid = (SELECT t.rowid
FROM tablename t,
(SELECT keyval, min(rowid) as keeprowid
FROM tablename
GROUP BY keyval) k
WHERE t.rowid(+) = k.keeprowid
AND k.keeprowid IS NULL
AND t.rowid = tablename.rowid)

Note that I used = rather than IN as the performance difference can be huge. This will work well on almost any table, but will probably work better in this case if a non unique index is applied to the keyval field. The keyval may actually be more than one field, it could if you wish be every field in the table.
Reply With Quote  
Join Date: Jan 2007
Location: Austin, TX
Posts: 30
Reputation: Memento is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Memento Memento is offline Offline
Light Poster

Re: Deleting Same Tuples

  #6  
Mar 27th, 2007
That is pretty slick... I'm going to have to save that :cheesy:.
Dan Moore
www.danmoore.org
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,806
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 338
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Deleting Same Tuples

  #7  
Apr 4th, 2007
Another approach would be :

  1. DELETE FROM TABLE_NAME T WHERE rowid NOT IN
  2. (SELECT max(rowid)
  3. FROM TABLE_NAME R
  4. WHERE R.common_column = T.common_column;) ;

Here common_column is the criteria for filtering out duplicates.
Last edited by ~s.o.s~ : Apr 4th, 2007 at 2:47 pm.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Feb 2007
Location: London
Posts: 114
Reputation: davidcairns is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
davidcairns davidcairns is offline Offline
Junior Poster

Re: Deleting Same Tuples

  #8  
Apr 10th, 2007
Technically though if 3 copies of a single row existed you would only delete 1 with that script, hence why I used the 1 to keep instead of 1 to delete

Also in large tables that will be much slower
Last edited by davidcairns : Apr 10th, 2007 at 5:08 pm.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,806
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 338
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Deleting Same Tuples

  #9  
Apr 10th, 2007
Originally Posted by davidcairns View Post
Technically though if 3 copies of a single row existed you would only delete 1 with that script, hence why I used the 1 to keep instead of 1 to delete
Nope. Take a look at it once again. I delete those tuples who don't satisfy the criteria of having the max ROWID. In any case when duplicate exists, only one row will have the max rowid among all the duplicates. Hence the query. Try running it and you will know.

> Also in large tables that will be much slower

Correlated queries are generally faster as compared to other types of queries. On what basis can you say that the query would be slow ?
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Feb 2007
Location: London
Posts: 114
Reputation: davidcairns is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
davidcairns davidcairns is offline Offline
Junior Poster

Re: Deleting Same Tuples

  #10  
Apr 11th, 2007
Fair enough, should read more carefully

In my experience with large databases providing an exact match is far more efficient than using IN or EXISTS clauses. This may be improved with adequate use of indexes, and the latest versions of Oracle may have made further improvements, but in essence it does hold. The larger the table the more pronounced this issue becomes. I have run this kind of things over tables with row counts in excess of 20 million in production.
Also joining using queries in the from clause is far more efficient in Oracle for at least versions 7, 8 and 9 (I have very limited experience working with 10).
Essentially limiting all queries to only those rows that actually need to be returned is efficient for any database.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Oracle Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Oracle Forum

All times are GMT -4. The time now is 1:56 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC