Loading initial values into auto_increment column

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bcm
    New Member
    • Apr 2007
    • 2

    #1

    Loading initial values into auto_increment column

    I am migrating a database to MySQL 5.0. Several tables should have their primary key as an auto_increment integer. The database being copied has records with the primary key beginning with zero (0). These keys are foreign keys so it is not desirable to change their values.

    Here is what I have been trying:
    (1) Truncate table <tablename>;
    (2) Alter table <tablename> modify recid integer not null default 0;
    (3) <insert rows into table>
    (4) Alter table <tablename> modify recid integer not null auto_increment;

    When I execute statement number 4, above, if the recid has been inserted with a value of zero, then MySql is changing the zero to one.

    How can I get MySql to let me load my values into the column and to set the auto_increment to the max(recid) + 1 ?
  • bcm
    New Member
    • Apr 2007
    • 2

    #2
    I believe the answer is to use the Sql-Mode setting NO_AUTO_VALUE_O N_ZERO.

    http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html

    http://www.xaprb.com/blog/2006/04/23/explicit-inserts-into-auto_increment-columns-in-mysql/

    Comment

    Working...