简体   繁体   English

MySQL Auto Increment Id的奇怪问题

[英]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. 我的表中有一个自动增量ID列,当我使用PHP插入记录时,它确实可以正常工作。 I have to delete the records from this table every hour using the DELETE statement. 我必须每小时使用DELETE语句删除此表中的记录。 I changed the PHP.ini file and restarted the machine. 我更改了PHP.ini文件并重新启动了机器。 For some reason Auto increment ID started from '1' again. 出于某种原因,自动增量ID再次从“1”开始。 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. 我在IIS下运行PHP 5.3.8和MySQL 5.5.21。 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. 这是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. 使用不带where子句的DELETE与TRUNCATING表相同,因此它们都重置了表的Next AutoIndex值。 (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 ) ); 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) (如果DELETE清除插入值,那么您只需要在DELETE / TRUNCATE之前缓存它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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