Delete Problem With Id - Id as auto_increment, and identifier for a row

free web hosting
Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming

Delete Problem With Id - Id as auto_increment, and identifier for a row

serialmike
Whenever I use 'delete from table where id = 4' from a table with several rows up to 10, the 4th row is removed and the update simply shows the rest from 1 to 10 without 4. This is not what I want. I want the table to now read 1 through 4 to 9 so that futher delete actions will be in that sequence. I want to identify my row with the id (number). Can anybody help out here. Very urgent.
Serialmike.

Reply

shadowx
The reason this happens is because you havent told the database to update the ID fields, you've only told it to remove the fourth row and do nothing else. I cant think of an efficient and simple way to solve this, there could be a built in function for it but i dont know of one. The only solution i can think of is to manually update each ID field that comes after the deleted row with a "-1" instruction that should result in each ID being in sequence and you'd have to use a loop to do this for each field which could be very long.

Reply

serialmike
QUOTE(shadowx @ Dec 11 2007, 03:38 PM) *
The reason this happens is because you havent told the database to update the ID fields, you've only told it to remove the fourth row and do nothing else. I cant think of an efficient and simple way to solve this, there could be a built in function for it but i dont know of one. The only solution i can think of is to manually update each ID field that comes after the deleted row with a "-1" instruction that should result in each ID being in sequence and you'd have to use a loop to do this for each field which could be very long.


Shadowx,
You are very rigth in theory. I have thought along your idea, but it sounds complicated hence my coming to this board if there is a shorter approach to the problem. I still hope there is an inbuilt command or sql statement for this. It appears to be a common problem, and as such, would have attracted attension. Will you be kind enough to translate your idea into code (php)? Is foreach() a useful function for this?
Regards, serialmike

 

 

 


Reply

galexcd
There is only one fix that I can think of that doesn't require manually relabeling every row, but it is sloppy and I do not recommend it. If you have your sql query delete the ID column and remake it with auto_increment it will relabel every row properly. But like I said it is very sloppy and I do not recommend it because if you have many rows and you you delete a column that other pages may need and remake it it may cause some errors if somebody else is loading another page at exactly the right time.

Reply

jlhaslip
serialmike,

In order to answer this question more intelligently, we would need to see your database structure. Since the "id" is presumably the Primary Key for the Data Table you want 'sequentially' numbered, what other data sets are affected? Assuming this is a 'members' table, is this a Foreign key for other tables? Are members assigned to groups using this key? Is this key used to track their attendance at a forum? Or their posting activity?
The Database structure will let us see if their are complications to deleting and re-organising the Data.
If you simply need a 'count' of the members, can you use the count function?
One of the greatest advantages to using a database for recording information is the ability to use this 'key' to track data rows. The 'ID' number does not need to be sequential. You risk major contamination of the Database by affecting the 'id' of a dataset. Forewarning is given.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : delete, problem, id, id, auto, increment, identifier, row

  1. Auto Pruning An Sql Database With Php
    How can i do this? (4)
  2. How To Delete Uploaded Picture From A Folder
    (1)
    i wrote some code to upload the pictures to a folder and after it has uploaded, there's a page,
    say pics.php.. it displays all pictures of the folder to that page. now i want to add an option to
    delete the pictures. for example i want to add a link "Delete" under every pic, that when i click it
    deletes the pic from the folder and deletes the pic data from mysql. How i can delete the pic from
    the folder? thanks ....
  3. How To Delete A Row In Mysql.
    A very simple help required (4)
    Im using the following code, for very simple work.. it simply gets and print the comments which are
    stored in the database. Now it will show like comment 1. comment 2. comment 3. etc etc i want to
    add 'delete' option that i can delete any comment. it displays like comment 1.
    Delete comment 2. Delete comment 3. Delete
    So that when i click on delete, it simply deletes the comment and print the remaining comments. Its
    very simple. i hope you understand. QUOTE $result = mysql_query("sele....
  4. File String Delete?
    (2)
    Say i have a file file.txt... Can someone give me an example of how to delete a string from this
    file ie.. The file contains: wappy::sucks::at::php --- i want to delete sucks::....
  5. Requesting Auto Generating Id Tag In Php Code
    Php Coding (2)
    Hello...I'm designing a website in PHP where ppl can submit their links for "cool sites".
    Anyway, when somebody submit's a link to a website for example "http://www.google.com" it
    creates an id such as "index.php?id=1134411593". I dont want the links to be converted into
    id's. I want it to remain as "http://www.google.com". I have the following coding on
    ( echo " ). I'm a novice. Please Help!!!!
    Thanks... Plus I also want to add the date on when the link was submitted. Please follow our foru....
  6. Suppressing Mysql Error In Php
    How to suppress auto generated mysql err (4)
    I am testing my website on my local machine. It is still in development stage and I am using
    php/mysql. If there is a mysql connection error, I am checking for the connection variable and
    displaying a proper error message. But even before it displays my error message, it displays an
    error message on browser thrown by mysql (MySQL Connection Failed: Access denied for user). Can I
    suppress this message and display only my message?....
  7. Can Reset The Id Auto Increment?
    phpMyadmin (11)
    hi, Can we reset the id auto increment in the mysql database using by phpMyadmin? let's say the
    shoutbox . the id #number table.. somehow i deleted some rows.. and i wanted to re-order/reset the
    id number acendingly in order rather than skip out.. thks ....
  8. Increment A Mysql Column
    how to increment a MySQL column one unit (7)
    Hi, I have a column in a MySQL table which contains a counter of the views of the object described
    by this table. I would like to increment this value by one everytime the object is viewed. Obviously
    came into my mind the possibility of retrieving the value of this field, store it in a variable
    increment this value by one and perform an UPDATE query again with this new value. My question is if
    there is a MySQL option to update the field with its actual value plus an unit increment. I hope you
    understand the issue.....
  9. About Set Permisstions....
    auto set it, how? (4)
    I have some problem about set permissions by automatic.. for example... I created txt file with
    php... but when my file was added to server(trap17), it always set to 644 and i want to set it to
    666 . Anybody help me... PLEASE /biggrin.gif' border='0' style='vertical-align:middle'
    alt='biggrin.gif' /> ....

    1. Looking for delete, problem, id, id, auto, increment, identifier, row

Searching Video's for delete, problem, id, id, auto, increment, identifier, row
advertisement



Delete Problem With Id - Id as auto_increment, and identifier for a row



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE