我有一个表eav_属性,它有一个下面的结构,
mysql - 如何在MySQL中更新或更改自动增量列值-LMLPHP
我错误地从该表中删除了一条具有值为961的自动增量属性ID列的记录。
现在,我希望该列再次具有相同的属性id值。
但当我插入该列时,它是用自动增量值相加的,即大约1500。
我想添加属性ID为961的新coulmn
在添加列之前,我试图将set auto_increment更改为961。

ALTER TABLE eav_attribute AUTO_INCREMENT = 961;

但它不起作用。请提供任何建议。

最佳答案

可以覆盖“自动增量”列。例如

MariaDB [sandbox]> drop table if exists t;
Query OK, 0 rows affected (0.14 sec)

MariaDB [sandbox]> create table t (id int auto_increment primary key,val varchar(1));
Query OK, 0 rows affected (0.27 sec)

MariaDB [sandbox]> insert into t (val) values
    -> ('a'),('b'),('C');
Query OK, 3 rows affected (0.03 sec)
Records: 3  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> select * from t;
+----+------+
| id | val  |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | C    |
+----+------+
3 rows in set (0.00 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> delete from t where val = 'b';
Query OK, 1 row affected (0.03 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> select * from t;
+----+------+
| id | val  |
+----+------+
|  1 | a    |
|  3 | C    |
+----+------+
2 rows in set (0.00 sec)

MariaDB [sandbox]> insert into t values (2,'b');
Query OK, 1 row affected (0.02 sec)

MariaDB [sandbox]> select * from t;
+----+------+
| id | val  |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | C    |
+----+------+
3 rows in set (0.00 sec)

MariaDB [sandbox]> show create table t;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                             |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t     | CREATE TABLE `t` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `val` varchar(1) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

我强烈建议你彻底测试…

08-27 12:53