MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old July 12th, 2000, 12:37 PM
ironlung_sw ironlung_sw is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 17 ironlung_sw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I have this table with 2 columns, id and content. Id is an auto-incrementing column. My php script produces a random number from 1 to number of rows in the table, and then displays the content for that row matching the random number to id. The problem is that if i delete a row, and the random number matches the id of the row deleted, that id will no longer exist. I could write some code to check for the id's existance, but is there another way to reset all of the id's in the id column?

Thanks in advance for any help!

Reply With Quote
  #2  
Old July 12th, 2000, 03:26 PM
Kyuzo Kyuzo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 1999
Location: Annapolis, Maryland US
Posts: 113 Kyuzo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
There is another way and you could even do it from an sql batch file, but would you want to?

The 5 easy pieces...

> alter table table_name modify id int not null;

> alter table table_name drop primary key;

> alter table table_name modify id int;

> update table_name set id=NULL;

> alter table table_name modify id int not null auto_increment primary key;

I believe that should do it, but my suggestion would be to read all the ids from the table into an array and then randomly choose one from that array, so you know it exists.

Better yet, try

> select id, content, content*0+RAND() as rand_col from table_name order by rand_col limit 1;

Kyuzo

[This message has been edited by Kyuzo (edited July 12, 2000).]

Reply With Quote
  #3  
Old July 13th, 2000, 12:10 PM
ironlung_sw ironlung_sw is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 17 ironlung_sw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks Kyuzo, that worked fine.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > deleting a row from a table with an auto-incrementing column


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway