将记录从一个数据库复制到另一个数据库时,自动增量无法正常工作

The php code below writes a record from one (mysql) database to another. Before this copy action the AGREEMENTS table of the target database (with constant name CORP_DBNAME) is empty. The 'id' fields of both tables AGREEMENTS are of type AUTO_INCREMENT.

The copy action works, but not 100% correctly: the id field of the target database table is expected to be 1 (the reason why I copy all fields except id), but is 11 - the autoincrement of the id (10) in the original database (SUPPL_DBNAME). E.g., the copy action should add the record to the target database with the proper incremented id at all times. Why is this happening and how to fix this?

simplified php code:

$id = 10;

$corpdb = new CORPDatabase(CORP_DBNAME);
$suppldb = new CORPDatabase(SUPPL_DBNAME);

$sql = "INSERT INTO ".CORP_DBNAME.".AGREEMENTS ( otherpartyid, agreementdocumentpath, approvalcorporation, approvalsupplier, created ) "
    . "SELECT otherpartyid, agreementdocumentpath, approvalcorporation, approvalsupplier, created FROM ".SUPPL_DBNAME.".AGREEMENTS "
    . "WHERE ".SUPPL_DBNAME.".AGREEMENTS.id=".$id." ";

$corpdb->UpdateDBRecord($sql);

Try:

ALTER TABLE AGREEMENTS AUTO_INCREMENT = 1

If the table in question is completely empty, you can use:

TRUNCATE TABLE AGREEMENTS;

on the database that you're importing data into and the AUTO_INCREMENT should return to 1. Most likely happened because you deleted rows instead of truncating the table.