Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

This sint specifically related to perl, but my perl program inserts data into mysql, one of the columns is an auto-increment field in mysql - i just enter it as "NULL" but what I want to do is start off the rows at a number like 234098 - is this at all possible by running an sql query or such ?

Any help greatly appreciated

Jon

Replies are listed 'Best First'.
Re: MySQL / Perl
by girth (Novice) on Dec 20, 2001 at 03:32 UTC
    auto_increment columns only increment if they are passed a NULL value and work by adding 1 to the maximum value in the column. If you insert '234098' into the column and then use NULL values for inserting the rest of the records, the column will auto_increment starting at '234099'.
Re: MySQL / Perl
by ehdonhon (Curate) on Dec 20, 2001 at 03:23 UTC

    Yes, it is very simple, all you need to do is insert a row into your table with the value 234097 explicitly stated in your auto incrementing column, and then delete the row. When you insert that row, it will update the last_insert_id value to 234097. The next time you insert a null value into that column, it will replace the null with last_insert_id + 1 and then update last_insert_id, so it should start counting up from 234098 as you requested.

      This is the way: but if it's important to have the min. value it's probably worth leaving the row instead of deleting it. IME, when one deletes the entire contents of the table, the auto_increment starts again from 1.

      § George Sherston
Re: MySQL / Perl
by aijin (Monk) on Dec 20, 2001 at 01:46 UTC
    Well, you could create a table in the DB which holds the last value for the auto-increment field.

    Then, when you're adding the data, grab the value in that table and use it as your starting point for the incrementation. I'm assuming you're writing record by record here. When you're done writing your records, write the last used value back into the table so that you can use it next time.

    -aijin.

      See my text below. You don't need to do this. mysql is already doing exactly this for you.