Welcome to Dream.In.Code
Become an Expert!

Join 150,000 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,328 people online right now. Registration is fast and FREE... Join Now!




Deleting rows by insertion order in MySQL

 
Reply to this topicStart new topic

Deleting rows by insertion order in MySQL, Boy, I wish MySQL had ROWID

wzeller
3 Dec, 2006 - 11:43 PM
Post #1

New D.I.C Head
Group Icon

Joined: 9 Apr, 2006
Posts: 39


Dream Kudos: 25
My Contributions
Hello,

I have a database where a program worked nicely 390962 times, and then went nuts about 400,000 times before it was noticed.

It looks something up online and inserts the results into a table.

The first 390962 rows of the table are fine, and took about three months to build. (Which is why I don't wipe it all out and start over.) It did the next 404483 rows in about ten minutes and populated it with junk data.

The problem in the program has been corrected, but the database is still full of junk data.

Unfortunately, the table has no primary key so I can't tell it to simply delete everything where id > 390962.

If I was on Oracle, I could use rowid, but I understand MySQL doesn't have that.

Nothing has been previously deleted from the table, so I know that the whole table is still in insertion order.

I'm using MySQL 3.23.58.

Any ideas on how I can get rid of all the records past the 290962'nd one?

Thanks,
Wayne

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Deleting Rows By Insertion Order In MySQL
4 Dec, 2006 - 07:03 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,355



Thanked: 51 times
Dream Kudos: 25
My Contributions
Is there a time stamp you can use? Alternately, can you modify the table to insert an autonumber, and then use the resulting field value (not sure if you can in MySQL, but you can with other DBs...)?
User is online!Profile CardPM
+Quote Post

wzeller
RE: Deleting Rows By Insertion Order In MySQL
4 Dec, 2006 - 07:45 AM
Post #3

New D.I.C Head
Group Icon

Joined: 9 Apr, 2006
Posts: 39


Dream Kudos: 25
My Contributions
QUOTE(Amadeus @ 4 Dec, 2006 - 08:03 AM) *

Is there a time stamp you can use? Alternately, can you modify the table to insert an autonumber, and then use the resulting field value (not sure if you can in MySQL, but you can with other DBs...)?


Unfortunately there's no timestamp - just three fields, none of each have any particular order to them. (I'd like to get the guy who set up the table into a dark alley sometime.) ph34r.gif

I thought about adding an id field and setting it to autonumber and primary key, but I wasn't sure if that would mess up the insertion order or not. Right now, the only thing differentiating good data from bad is that all the bad data was entered last. If I do anything that might change the row order, I'll have to scrap everything and start over from scratch.

Is it possible to add a PK to an existing table while reliably maintaining the row order?

Wayne

This post has been edited by wzeller: 4 Dec, 2006 - 08:02 AM
User is offlineProfile CardPM
+Quote Post

psykoprogrammer
RE: Deleting Rows By Insertion Order In MySQL
4 Dec, 2006 - 07:50 AM
Post #4

D.I.C Head
Group Icon

Joined: 9 Oct, 2006
Posts: 68


Dream Kudos: 50
My Contributions
My suggestion would be to test this idea. In theory you should be able to add an autoincrement column without altering the row order.

Setup a test table... heh, even make a copy of this table to play with. Make note of some sample rows and their order. Take several samples. Then add the column with the PK autoincrement. See if it works.

Otherwise you may consider writing a script/app to pull in the records and start removing from the point specified.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 08:31PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month