473,809 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP/MySQL Delete Row...Automatic Shift Numbers Down?

Hello all!
I'll make it short and sweet...

I have a database, it looks something like this:

id data
--- -------------------------
0 Some Data
1 Some Other Data
2 Some More Other Data
Note:
id = INT NOT NULL PRIMARY KEY
data = TEXT NOT NULL

Now, I:
DELETE FROM whatever WHERE id='1';

Then the ID numbers go "0, 2". Is there a way to _automatically_ shift all
the ID numbers, after the one deleted, to go in order again? I can write a
script to update every row, I just want to know if there is a PHP/MySQL
function that automatically does it.

Thanks!
- Merlin
Jul 17 '05 #1
5 7987
Merlin wrote:
Then the ID numbers go "0, 2".**Is*there*a *way*to*automat ically*shift*al l
the ID numbers, after the one deleted, to go in order again?**I*can*w rite
a script to update every row, I just want to know if there is a PHP/MySQL
function that automatically does it.


I'm not quite sure exactly why you'd want to do this but you'd need to do it
manually like so:

DELETE FROM whatever WHERE id = X ;
UPDATE whatever SET id = id - 1 WHERE id > X;

and subsitute X for the number.

Note that if you're referring to this id number in other tables you are
going to have a much more difficult time as you also need to update allthe
values in all of those tables as well.

Unless you have a specific need to do so, is there really any reason todo
this? If there isn't, you'd probably be better off using an auto
incrementing primary key so you don't need to work out what the id value
should be each time you do an insert.

--
Chris Hope
The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2
Merlin wrote:
id = INT NOT NULL PRIMARY KEY
data = TEXT NOT NULL [...] Then the ID numbers go "0, 2". Is there a way to _automatically_ shift all
the ID numbers, after the one deleted, to go in order again? I can write a
script to update every row, I just want to know if there is a PHP/MySQL
function that automatically does it.


I am sure you do not want to take care of id's by yourself. Normally
id's are created as AUTO_INCREMENT:

my_id BIGINT NOT NULL AUTO_INCREMENT

Now MySQL takes care about this. Adding a new item to the db increases
my_id by 1.

Rgds,
Frank

--
http://landseer-stuttgart.de
Jul 17 '05 #3
On Sat, 12 Jun 2004 00:08:37 GMT, "Merlin" <.@.com> wrote:
I'll make it short and sweet...

I have a database, it looks something like this:

id data
--- -------------------------
0 Some Data
1 Some Other Data
2 Some More Other Data
Note:
id = INT NOT NULL PRIMARY KEY
data = TEXT NOT NULL

Now, I:
DELETE FROM whatever WHERE id='1';

Then the ID numbers go "0, 2". Is there a way to _automatically_ shift all
the ID numbers, after the one deleted, to go in order again? I can write a
script to update every row, I just want to know if there is a PHP/MySQL
function that automatically does it.


Automatically? No, since MySQL doesn't support triggers or similar constructs.
Besides, why would you want to anyway? Deleting a row shouldn't change the
identity of other rows.

--
Andy Hassall <an**@andyh.co. uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #4
In message <ud************ *************** *****@4ax.com>, Andy Hassall
<an**@andyh.co. uk> writes
On Sat, 12 Jun 2004 00:08:37 GMT, "Merlin" <.@.com> wrote:
I'll make it short and sweet...

I have a database, it looks something like this:

id data
--- -------------------------
0 Some Data
1 Some Other Data
2 Some More Other Data
Note:
id = INT NOT NULL PRIMARY KEY
data = TEXT NOT NULL

Now, I:
DELETE FROM whatever WHERE id='1';

Then the ID numbers go "0, 2". Is there a way to _automatically_ shift all
the ID numbers, after the one deleted, to go in order again?
They will still go in order - it's just there will be a gap or two.
I can write a
script to update every row, I just want to know if there is a PHP/MySQL
function that automatically does it.


Automatically? No, since MySQL doesn't support triggers or similar constructs.
Besides, why would you want to anyway? Deleting a row shouldn't change the
identity of other rows.


Some database designs use triggers to delete rows in other tables using
a deleted key as a foreign key - they need to be darn sure they really
want to delete it...

--
Five Cats
Email to: cats_spam at uk2 dot net
Jul 17 '05 #5
I noticed that Message-ID:
<9Q************ ****@newsread1. news.pas.earthl ink.net> from Merlin
contained the following:
Now, I:
DELETE FROM whatever WHERE id='1';

Then the ID numbers go "0, 2". Is there a way to _automatically_ shift all
the ID numbers, after the one deleted, to go in order again? I can write a
script to update every row, I just want to know if there is a PHP/MySQL
function that automatically does it.


The ID ( or primary key) simply has to be unique. It doesn't have to be
in order, it doesn't even have to be a number, any random unique string
will do. Furthermore, it should not change

Any order in the results from a database is specified by you, as it
should be. For instance if you wish to sort the results in the date they
were entered you should incorporate a field including a timestamp and
order the results on that.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2443
by: el_roachmeister | last post by:
I have been using flat files for a while but thought I should learn mysql. There are two things I dont like about mysql compared to using flatfiles. They are: 1) When creating a table why do I need to define the type of data and length so strictly, i,e int , varchar etc? Can I make a flexible table with data of any type and length? 2) When creating a record I am using autoincrement ID. This works great except when I delete a record...
0
2216
by: dohnut | last post by:
Here's one for some bored problem solver :) I ran across this earlier today and fixed it, but don't exactly know why. (that usually only happens in C :) I'm using Perl version 5.8.0 btw. Ok, let's start here: I end up with an array that comes from a fetchrow_array() call to MySQL. I return the array to a function.
0
1867
by: Ola Ogunneye | last post by:
--=_7B255FAE.1F7E0C30 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Hello all, Let me start out by saying that I am still a newbie. I just freshly installed mysql version 4.0.13. The following are the steps that I took.
0
2691
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ===================================
0
3953
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest version of this document can be found at: http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download
388
21953
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's worth the $25USD. I'm just looking for a book on Pointers, because from what I've read it's one of the toughest topics to understand. thanks in advanced.
1
3384
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer uses his own php shopping cart to receive customer orders. The configuration was done via cPanel with no external modifications - which produced no protests when built, ran and connected with no
6
38534
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get through this without much trouble. Programming knowledge is not required. Index What is SQL? Why MySQL? Installing MySQL. Using the MySQL command line interface
5
4965
by: N2Deep | last post by:
I'm new at trying to write VBA, and have been working on this delete loop for a while, and could use some help. I want the macro to start in cell B1, if it finds the string "Contract Type" in cell B1 I want it to delete that entire row and exit (Contract Type will only ever appear in cell B1). If it doesn't find string "Contract Type" in above, I would like it to move down the B column one cell at a time....deleting all rows with an empty...
0
9601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10637
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10379
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7660
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.