【问题标题】:How to update or change auto increment column value in Mysql如何更新或更改 Mysql 中的自动增量列值
【发布时间】:2019-10-01 03:30:43
【问题描述】:

我有一个表 eav_attribute 具有以下结构,

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

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

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

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

我尝试在添加列之前将设置 AUTO_INCREMENT 更改为 961。

ALTER TABLE eav_attribute AUTO_INCREMENT = 961;

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

【问题讨论】:

    标签: mysql sql database magento2 auto-increment


    【解决方案1】:

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

    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)
    

    我强烈建议你彻底测试一下……

    【讨论】:

      猜你喜欢
      • 2021-03-03
      • 2015-09-30
      • 2019-03-04
      • 1970-01-01
      • 2011-12-30
      • 2010-12-01
      • 1970-01-01
      • 2017-04-21
      • 1970-01-01
      相关资源
      最近更新 更多