Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-19-2005, 12:29 PM   #1 (permalink)
Phantek
Registered User
 
Join Date: May 2005
Posts: 5
Phantek is on a distinguished road
Row Decrement

I'm pretty new to using mySQL so please excuse my ineptitude.

I have created an SQL table using phpMyAdmin. I am successfully able to create and delete rows through php scripts. In my table, the first field is "id" which I set as auto_increment. Simply, I want this field to list a number for each row, incrementing for each additional row.

My problem is when I want to delete a row. Although I can delete a row successfully, I am now stuck with a disordered incrementation struction. For example, I have rows 1,2,3,4,and 5. I delete row 3. Now I have 1,2,4,5. I want to be able to decrement each row id after the one that is deleted so I'd end up with 1,2,3,4. Anyone know how I can do this? Thanks.
Phantek is offline   Reply With Quote
Old 05-19-2005, 04:09 PM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
you have to drop the table; and re-enter your data. There's no performance advantage, to having contiguous rows either. If you want to reset the numbering to the current max though you can use
Code:
ALTER TABLE name_of_table AUTO_INCREMENT = 1;
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 05-19-2005, 04:42 PM   #3 (permalink)
Phantek
Registered User
 
Join Date: May 2005
Posts: 5
Phantek is on a distinguished road
There's really no way to just create a loop to decrement the id value of each row after the one that is deleted?
Phantek is offline   Reply With Quote
Old 05-19-2005, 06:19 PM   #4 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Nope sorry. It's really not that big a deal though. If you only delete from the top, then run that alter statement after every deleton and it won't skip numbers.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 05-19-2005, 06:42 PM   #5 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 746
DJMaze is on a distinguished road
Code:
UPDATE table_name SET id=id-1 WHERE id > 3 ORDER BY id
It's trivial though AND the next entry doesn't have 5 it will have 6 since the auto_increment is active.
ALSO the ORDER BY only works in MySQL 4.x to my knowledge.
DJMaze is offline   Reply With Quote
Old 05-20-2005, 03:40 AM   #6 (permalink)
Phantek
Registered User
 
Join Date: May 2005
Posts: 5
Phantek is on a distinguished road
Thanks for your help!
Phantek is offline   Reply With Quote
Old 05-20-2005, 11:59 AM   #7 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 287
technobard is on a distinguished road
Something to Think About: Meaning in Keys

While you can work out a way (and it sounds like you have) to keep the ids in sequence, there are a couple of problems.

1) The update will take longer and longer as your table grows. For large tables, you'll have to figure out an alternative to deal with the slow response times.

2) More importantly, why do you want to do this? As an ID column, my assumption is that this is the primary key. In an ideal world, the value of this column is meaningless. The only thing that matters is that the value is unique. As soon as you assign meaning to the column, you've created an administrative problem.

If possible, find another way to address "why" you're doing this.

Just something to think about.
__________________
It takes 2 points to draw a straight line, but at least 3 points to draw a conclusion.
technobard is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
making a 3 column table dynamically sde PHP 8 08-28-2004 08:14 PM


All times are GMT -8. The time now is 01:40 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting