Submit Your Article Forum Rules

Results 1 to 7 of 7

Thread: MySql - Increment values reassignable?

  1. #1
    WebProWorld MVP jawn_tech's Avatar
    Join Date
    Jun 2004
    Posts
    1,531

    MySql - Increment values reassignable?

    A MySql database is set up where a user can input new row entries via a form. Each row has an id column which is auto-incremented by 1. Suppose a user enters a spam or nonsensical entry, and suppose that row generated gets an id# of 215. By the time admin catches it, rows are already at 300. Admin wants to delete row #215 from the db. Do the following rows keep their values, or does the row that was 216 become 215, and so forth? If not, is there a way to reassign the following rows so that there is not a gap for 215, therefore without breaking continuity in the numerical sequence of rows?
    Domain Name Registration and Website Hosting :: DesignerTrade

  2. #2
    Senior Member
    Join Date
    Nov 2004
    Posts
    145
    When you delete 215 the others will stay as they are, which is how you want it to be for tracking and auditing purposes. The last thing you would want is for your uniques ID to change depending on whether other rows are removed, it would make database integrity nearly impossible to maintain and tracking even worse.

  3. #3
    WebProWorld MVP jawn_tech's Avatar
    Join Date
    Jun 2004
    Posts
    1,531
    That makes sense. I'm just thinking ahead when I'm ready to add the feature when one can search the db, or view rows by a category field. I'm assuming I would need to set up a loop PHP that would display rows that match the query by that particular field. I'm assuming it should be that a blank id# would signal the loop to end...?

    Unless, since it's searching the db by a particular field, should the data in row 215 be deleted -- but should I leave the id# (215), as it wouln't turn up for any of the queries anyway? I just want to make sure a loop doesn't stop at 215 where a row used to be.
    Domain Name Registration and Website Hosting :: DesignerTrade

  4. #4
    Senior Member
    Join Date
    Nov 2004
    Posts
    145
    Two options come to mind right away. The first is you keep the row but have a status field, called approved, or active or something along those lines. If you want to keep the data but not show it just set that to 0 (all the others would be 1) and when you do a query to loop through the table just check with the where clause that its set to 1 (where active = 1). This will leave out that row.

    The second thing is to just delete the row and forget about it. The thing to watch out for is if any other rows are inserted into the database using that rows ID as a seconday key. So if you insert a record there and delete it (ID 215) are you referencing that ID of 215 in any other tables?

    Either way, when it comes to display the contents you will just do a query to pull them from the db and then loop through that query. Having any missing IDs wont matter as its just looping through the data pulled back. You would only have an issue if you were running a straight for loop (from 1 to last row) and pulling data based on that number, cuz when you get to 215 it wouldnt find anything. But thats a bad way to do it so you won't have to worry about it.

  5. #5
    WebProWorld MVP jawn_tech's Avatar
    Join Date
    Jun 2004
    Posts
    1,531
    Great advice, very appreciated. I don't mod this particular category for good reason, as you can see. ;)
    Domain Name Registration and Website Hosting :: DesignerTrade

  6. #6
    Senior Member
    Join Date
    Jul 2003
    Posts
    386

    Delete and Enjoy

    Quote Originally Posted by jawn_tech
    I'm just thinking ahead when I'm ready to add the feature when one can search the db, or view rows by a category field. I'm assuming I would need to set up a loop PHP that would display rows that match the query by that particular field. I'm assuming it should be that a blank id# would signal the loop to end...?
    The ID will probably never be used when you do any kind of search. If your table has 3 fields, ID, Category, Description, then your example above would be
    SELECT Description FROM myTable WHERE Category="Good Stuff";
    You wouldn't need to show the ID on screen. So you wouldn't select it. And whether or not you selected it, you asked for all records in the "Good Stuff" category. You don't need to "mention" the ID, it doesn't have any relevance at this point

    When the record is deleted, yes, it correctly leaves a gap in the numbering sequence - but it doesn't matter. Which, when you think about it, is why most database systems are designed to run that way...

    You would only have an issue if you were running a straight for loop (from 1 to last row) and pulling data based on that number, cuz when you get to 215 it wouldnt find anything. But thats a bad way to do it so you won't have to worry about it.
    If you are just running through your table, it would show
    213 This
    214 That
    216 The Other
    And 215 would not be missed.
    Pete Clark
    Find out what's happening in Spain at http://HotCosta.com

  7. #7
    Senior Member
    Join Date
    Nov 2004
    Posts
    145

    Re: Delete and Enjoy

    Quote Originally Posted by computergenius
    You would only have an issue if you were running a straight for loop (from 1 to last row) and pulling data based on that number, cuz when you get to 215 it wouldnt find anything. But thats a bad way to do it so you won't have to worry about it.
    If you are just running through your table, it would show
    213 This
    214 That
    216 The Other
    And 215 would not be missed.
    If you are running a straight for loop then the output generally won't be conditional, so formatting, text, TRs etc will all be outputted for each row, so even if their isn't a 215 everything that goes with it will be outputted, which will cause a space and problems.

Similar Threads

  1. Getting unique values from xml using XSL
    By deadplant in forum IT Discussion Forum
    Replies: 5
    Last Post: 09-21-2008, 06:59 AM
  2. PHP, MySQL, a form and 1 too many values!
    By bizgen in forum Database Discussion Forum
    Replies: 3
    Last Post: 05-28-2008, 03:58 PM
  3. multiple values from form in php
    By Jerry in forum Web Programming Discussion Forum
    Replies: 3
    Last Post: 09-20-2005, 02:34 PM
  4. Need help with passing values in URL using PHP & MySQL
    By Adamwlad in forum Database Discussion Forum
    Replies: 1
    Last Post: 01-26-2004, 12:53 PM
  5. transparent values for cells
    By webweaver in forum Graphics & Design Discussion Forum
    Replies: 9
    Last Post: 01-22-2004, 05:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •