简体   繁体   中英

strange issue with MySQL Auto Increment Id

I have an Auto increment ID column in my table and it does work fine when I insert the records using PHP. I have to delete the records from this table every hour using the DELETE statement. I changed the PHP.ini file and restarted the machine. For some reason Auto increment ID started from '1' again. There were no records in the table when I restarted the machine. I am using PHP 5.3.8 and MySQL 5.5.21 running under IIS. Please let me know if there are any suggestions. Here is my table schema.

    CREATE TABLE `test_table` (
  `test_id` int(11) NOT NULL AUTO_INCREMENT,
  `test_date` datetime DEFAULT NULL,
  `test_location` varchar(2000) NOT NULL,
  `test_summary` varchar(4000) NOT NULL,
  `create_dtm` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`test_id`)
) ENGINE=InnoDB 

Here is my insert query

$Sql = "INSERT INTO test_table(test_date, test_location, test_summary) VALUES ('" .sDate. "', '" .$location. "', '" .$summary. "')";

        $Result = mysql_query($Sql) or die(mysql_error());
        $new_id = MySql_Insert_Id();

Here is DELETE.

        $Sql1 = "DELETE FROM test_table";
        $Result1 = mysql_query($Sql1) or die(mysql_error());

Using a DELETE with no where clause is the same as TRUNCATING a table, hence they both reset the Next AutoIndex value for the table. (Which is what people would normally want / expect)

Could use something like the following to get around this in your case maybe:

mysql_query( sprintf( "ALTER TABLE tbl_name AUTO_INCREMENT = %d", mysql_insert_id() + 1 ) );

(If the DELETE clears the insert value then you will just need to cache it before your DELETE / TRUNCATE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM