Welcome to WebmasterWorld Guest from 54.145.44.134

Forum Moderators: open

Message Too Old, No Replies

Duplicate rows

     

StoutFiles

4:14 pm on Jan 6, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



If I have two rows of data in my database that are exactly the same, is it possible to call a delete statement that would only delete one row?

LifeinAsia

4:32 pm on Jan 6, 2010 (gmt 0)

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Do you have an identity field on the table?

StoutFiles

4:57 pm on Jan 6, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Nope. Exactly the same.

LifeinAsia

5:27 pm on Jan 6, 2010 (gmt 0)

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Easiest way would be to add an identity field.

Another option would be to manually remove one of the rows if you have a client viewer (e.g., Server Management Studio for MS SQL).

In MS SQL you could use a CURSOR to go through and delete one of the rows. Not sure is MySQL has anything similar (sort of like a looping routine).

A brute force method is to delete both rows and re-insert the data.

StoutFiles

5:56 pm on Jan 6, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Wish I could, it's an Oracle table. I was hoping maybe a LIMIT statement would work but no dice.

LifeinAsia

6:18 pm on Jan 6, 2010 (gmt 0)

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Why can't you add a field? I believe Oracle supports cursors, so any of the other 3 options should work.

StoutFiles

6:23 pm on Jan 6, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Can't use ALTER statements due to having a low version of Oracle. Not something I can change. :(

LifeinAsia

6:41 pm on Jan 6, 2010 (gmt 0)

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



ALTER TABLE is one of the most basic SQL commands... It must really be an ancient version! Does it store its data on stone tablets? :)

StoutFiles

1:25 pm on Jan 7, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



It's Oracle 7 on a VMS system. So yes, stone tablets are in full effect.

LifeinAsia

4:54 pm on Jan 7, 2010 (gmt 0)

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Wow- does something that old even have enough memory for 2 rows? :)

Anyway, I think your best option is to delete the offending rows and re-add the data.

syber

4:42 pm on Jan 10, 2010 (gmt 0)

10+ Year Member



You would have to use multiple statements:

SELECT INTO temptable <rows that are duplicates>
FROM mytable

DELETE FROM mytable <rows that are duplicates>

INSERT INTO mytable
SELECT DISTINCT *
FROM temptable

whoisgregg

12:49 am on Jan 13, 2010 (gmt 0)

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Once you get it cleaned up, you may want to put a UNIQUE key on multiple columns so that you don't insert duplicate rows in the first place. With this in place, you will get an error when you try to insert a row that already exists:

ALTER TABLE `table` ADD UNIQUE INDEX `unique_index` (`column_one`, `column_two`, `column_three`);

For MySQL, you can even let the creation of this index delete the duplicates, just make sure you backup your data first in case you don't have the correct columns in the index!

ALTER IGNORE TABLE `table` ADD UNIQUE INDEX `unique_index` (`column_one`, `column_two`, `column_three`);

LifeinAsia

12:54 am on Jan 13, 2010 (gmt 0)

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



I guess you missed the message mentioning that ALTER TABLE isn't an option? :)

whoisgregg

1:14 am on Jan 13, 2010 (gmt 0)

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Yes, yes I did miss that. Whoops. :/

/heading home after an apparently long day

StoutFiles

1:39 am on Jan 13, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



By adding another column I'd have to edit the Fortran and .sfo files that interact with the table...assuming I could easily add another column.

LifeinAsia

4:23 pm on Jan 13, 2010 (gmt 0)

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Are you sure about that? Just because a column is in a table doesn't mean you have to read it in every SELECT that you run.

I think you may be making things overly complicated- can you provide some more details about the whole process?

 

Featured Threads

Hot Threads This Week

Hot Threads This Month