mysql auto-increment skipping values

Discussion in 'MySQL' started by siddharth007, Nov 21, 2012.

  1. #1
    Hi all.I am using a bash script to populate a db table with some data in text file.(Using Load data infile command) Everything is imported nicely from the text file to the db table.However i have an auto increment column in the table(with auto_increment=1) and it skips some values every time the script runs.Something like this happens

    # script runs for the first time
    ID
    1
    2
    3
    4
    5
    # Second run of script
    8
    9
    10
    11
    12


    it is skipping 6 and 7. Multiple records are being inserted into table with each execution of the script.Is it some kind of bug in mysql which is causing it?? Any solutions for the problem.I am using mysql-server-5.5.24 and os is Ubuntu-12.04
     
    siddharth007, Nov 21, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    Are you sure nothing is deleting records? Autoincrement won't fill in for deleted records, their numbers are still "used".
     
    Rukbat, Nov 22, 2012 IP
  3. siddharth007

    siddharth007 Greenhorn

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Hey.Thanks for the reply.My script is deleting no records.However i have found a workaround for this problem.It is something like this

    # Script runs the first time

    ID
    1
    2
    3
    4
    5

    # Five records are inserted

    # i reset the auto_increment value to 1 using alter table command(through the same script). However it sets the auto_increment value to (max_id+1 which is 6). So when the next insert takes place it is like
    .
    .
    .
    6
    7
    8
    9
    10

    It fixes my problem.However i have to run an extra query in my script.If you insert a large number of records using LOAD DATA INFILE,i think some auto_increment values are skipped.May be its a bug in mysql.:)
     
    siddharth007, Nov 23, 2012 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    Rukbat, Nov 23, 2012 IP