it is possible to reorder/reset database table id?

Discussion in 'Databases' started by chonghua, Aug 19, 2009.

  1. #1
    hi likewise as the subject says, it is possible to reset table id? for example, table forum ids.. resetting id 3,5,6,8.. etc to 1,2,3,4.. so on..

    could anyway give me a query code on how to do it if it is possible then?

    thank you in advance..
     
    chonghua, Aug 19, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well, if you have foreign keys this won't work well.. It would mean that threads would link to the wrong forum_id etc. But if you don't, you can just drop the primary key and then recreate it. Or rename it and add a new primary key.
     
    premiumscripts, Aug 19, 2009 IP
  3. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Unless you're a crazy numerologist who thinks certain numbers have mystical healing powers, you shouldn't do this. The id numbers that have been assigned should mean nothing and have no value to you nor anyone else.

    They exist only to provide a way to make each row of data unique and thereby easy to reference within the database itself. Don't make your id numbers have external meaning, instead make a new field to hold whatever external meaning it is you want to capture.
     
    plog, Aug 19, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    Best way would be to duplicate the table, and copy all the rows from one table to the other. Delete the original table, and rename the new one.

    You can easily copy all the data, less the id column in a single statement. You will need to explicitely name the columns so that you don't copy the id's.

    Do a backup before running this, but these queries should do exactly what you want. As stated above if you have foreign keys, this will not work.

    CREATE TABLE new_table LIKE old_table;
    INSERT INTO new_table (name, other_columns) SELECT name, other columns FROM old_table;
    DROP old_table;
    RENAME TABLE new_table TO old_table;

    There is logical reasoning to do this if you have deleted a lot of rows from a very large table.
     
    jestep, Aug 20, 2009 IP
  5. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #5
    Then you should use OPTIMIZE TABLE.
     
    mwasif, Aug 21, 2009 IP
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    Yes, this will clear out the free space and correct the cardinality, but won't reorder the identity column. If you're running out of available numbers recreating the table is the only way to reset the identity column.
     
    jestep, Aug 21, 2009 IP
  7. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Well, you won't normally run out of numbers :) use UNSIGNED integers or bigints if you reach the limit.
     
    premiumscripts, Aug 21, 2009 IP
  8. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #8
    I agree. Unisgned INT is far more than I have ever needed.
     
    jestep, Aug 21, 2009 IP
  9. rrobert915

    rrobert915 Peon

    Messages:
    61
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I dont think there is a way to do it but integer is having enough values than you need
     
    rrobert915, Aug 23, 2009 IP
  10. ardianbudi

    ardianbudi Well-Known Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    103
    #10
    i'm afraid it cannot be done. but you can try to optimize the database. It also depend to the database engine.
     
    ardianbudi, Aug 23, 2009 IP
  11. renownedmedia

    renownedmedia Well-Known Member

    Messages:
    65
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #11
    I agree that you should never change your PK's of your database table. The whole normalization concept says that they should never change.

    If you are using them for your URLs or something that can be seen by the end user, you might want to consider adding another column to the table for storing this, perhaps as a human-readable string.

    If, however, you've got a bunch of unordered records (DELETEing and INSERTing will do this), you can clean up the order and make your SELECTs faster by issuing this command:

    ALTER TABLE `tablename` ORDER BY `id`
    Code (markup):
     
    renownedmedia, Aug 24, 2009 IP
    jestep likes this.
  12. thgames

    thgames Peon

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I agree with you. Do not need to change or reorder id, you should add new field and order database following to the new one.
     
    thgames, Oct 8, 2009 IP