| | |
Deleting Same Tuples
Please support our Oracle advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Mar 2007
Posts: 32
Reputation:
Solved Threads: 0
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
•
•
Join Date: Feb 2007
Posts: 114
Reputation:
Solved Threads: 8
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
I'll let you figure out how to apply it to this problem
•
•
Join Date: Jan 2007
Posts: 30
Reputation:
Solved Threads: 0
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
www.danmoore.org
•
•
Join Date: Feb 2007
Posts: 114
Reputation:
Solved Threads: 8
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.
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.
Another approach would be :
Here common_column is the criteria for filtering out duplicates.
sql Syntax (Toggle Plain Text)
DELETE FROM TABLE_NAME T WHERE rowid NOT IN (SELECT max(rowid) FROM TABLE_NAME R 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 3:47 pm.
I don't accept change; I don't deserve to live.
•
•
•
•
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
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.
•
•
Join Date: Feb 2007
Posts: 114
Reputation:
Solved Threads: 8
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.

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.
![]() |
Similar Threads
- Error Deleting File or Folder (Windows NT / 2000 / XP)
- Editing registry after deleting application (Windows NT / 2000 / XP)
- Deleting subjects in a search engine window (Web Browsers)
- deleting registry key for error code 19 (Windows NT / 2000 / XP)
- Code not working for deleting recordfrom a file (Visual Basic 4 / 5 / 6)
- IE Cache not deleting a URL (Web Browsers)
Other Threads in the Oracle Forum
- Previous Thread: Whats your favorite Oracle "How To..." ?
- Next Thread: SQLCA Error
| Thread Tools | Search this Thread |
2009predictions acquisition amazon.com bartz bernanke cia citrix cloudcomputing crm database dell economy editor enterprise enterprise2.0 enterprisesoftware federalreserve forbes hp ibm intellipedia internet larryellison layoffs linux loughridge mediawiki michaeljackson microsoft neverland nortel notebooks oil operatingsystem oracle palm rimm saas salesforce sap seagate socialcomputing sql sun sybase technologystocks virtualiron virtualization vmware wiki wikipedia xen yahoo zoho






