mysql - 如何在 Mysql 中更新或更改自动递增列值

标签 mysql sql database magento2 auto-increment

我有一个表 eav_attribute,其结构如下,

enter image description here

我错误地从该表中删除了一条记录,该记录具有值为 961 的自动递增属性 id 列。

现在我希望该列再次具有相同的属性 ID 值。

但是当我插入该列时,它会自动增加值,即大约 1500。

我想添加属性 id 为 961 的新列

我试图在添加列之前将 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)

我强烈建议您彻底测试一下...

关于mysql - 如何在 Mysql 中更新或更改自动递增列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56124752/

相关文章:

mysql - 带有字母的 Microsoft Access 号码字段

sql - 如何获取SAS中两个日期之间的天数

安卓数据库: issue when binding parameters using selectionArgs

php - 将选择表单的值保存到mysql数据库

mysql - 使用 DMS 复制数据库时出错

mysql - 从 MySql 导出结果集为制表符分隔文件。带逗号的字符串在字符串周围有引号,为什么?

php - 如何更新每行条件匹配的表的第一行

MySQL:数据库结构选择-大数据-重复数据还是桥接

sql - 无法删除或截断 SQL Server 表

mysql - 从sql中删除字段