Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
"Splicing" MySQL id fields
Old 11-25-2005, 10:40 PM "Splicing" MySQL id fields
Novice Talker

Posts: 9
Trades: 0
Hey,
I was wondering if there is an easy way to splice id fields in such a way that if you have the fields:

ID
1
2
3
4
5

and you remove field 2, then 3,4 and 5 would become 2,3, and 4. In other words:

ID >> ID
1 >> 1
2 >> 2
3 >> 3
4 >> 4
5 >>

If you remove '2' from the first.

Thankyou for your help.

Last edited by PiperPAM27; 11-25-2005 at 10:42 PM..
PiperPAM27 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-26-2005, 05:09 AM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
No
Imagine the problems it would create if you were using PK/FK relationships on the ID.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-26-2005, 07:28 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
You could probably do it by running the query "UPDATE table SET id = id -1 WHERE id > $theidyouaredeleting" after you've done the deletion.

As chris says though if you want other tables to refer to the data in your first table, all the numbers will be off by one unless you update the references too. It's far better just to leave gaps.

If you want to display numbered lists of your data, you can leave the id as they are and use PHP/ASP to print out a counter variable with each row, that way your lists appear contiguous, but your data integrity is maintained.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 11-26-2005, 02:10 PM
Novice Talker

Posts: 9
Trades: 0
I see your point, I hadn't thought of that. But suprisingly this still works as my references are all to other content in the table rather than the ID. I think that the bit of code in the last post will help me a lot. thanks.

[EDIT]I have another question, if I used that last bit of code to shift all the ID's after the removed table down, and ID is set to AUTO-INCREMENT, will the autoincrement still be on track(i.e. will it pick up at 5, or after the last ID it created--at 6.

Last edited by PiperPAM27; 11-26-2005 at 02:15 PM..
PiperPAM27 is offline
Reply With Quote
View Public Profile
 
Old 11-27-2005, 09:04 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
The autoincrement will probably pick up at 6, but you can reset it with "ALTER table AUTO_INCREMENT = $newvalue".

Again I woul stringly recommend you take a good look at your database design - you say you are referring to items in your table by other content, not the ID field. What happens if any of these fields have duplicate entries? What happens if they have awkward characters and escape sequences? You are far better off leaving the auto number column to do the job it was designed to do, and use the id column to refer to things since the auto numbering guarantees that it is unique.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 12-02-2005, 07:49 AM
Minaki's Avatar
Defies a Status

Posts: 1,626
Location: Guildford, UK
Trades: 0
I also agree with 0beron. This sounds like a bad idea and you could run into problems later down the line.

I don't know much about the way databases handle data internally, but I can imagine doing what you're proposing to do would probably create a lot more of a performance hit than you really need - you're shifting around a huge chunk of records when you only really need to be playing with one. You may not notice the performance hit now, but later down the line, as the number of records in the table increases, your application will get slower and slower.
__________________
Minaki Serinde MCP
"Wow, Linux is nearly on-par with Windows ME!"

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Minaki is offline
Reply With Quote
View Public Profile Visit Minaki's homepage!
 
Reply     « Reply to "Splicing" MySQL id fields
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29364 seconds with 12 queries