【问题标题】:Java Hibernate AutoIncrement Identity set ID to start 0 mySQLJava Hibernate AutoIncrement Identity 设置 ID 启动 0 mySQL
【发布时间】:2013-12-10 06:24:44
【问题描述】:

我有一个像这样的简单类

public class IdClass
{
    private static final long serialVersionUID = 4594459221202802623L;
    private Integer id;
    public IdClass(){}
    public IdClass(Integer id){super();this.id = id;}   
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    public Integer getId(){return this.id;}
    public void setId(Integer id){this.id = id;}        
}

其中有autoIncrement in MYSQL table 一切正常,但我们需要删除表中的所有记录以便稍后重新开始删除我想知道是否可以将 ID 设置为再次从 0 或 1 开始...非常感谢..

【问题讨论】:

  • 您可能必须删除整个表才能执行此操作。

标签: java mysql hibernate auto-increment identity


【解决方案1】:

你可以通过三种方式做到这一点

  1. 直接重置自增值

    ALTER TABLE table_name AUTO_INCREMENT=1;

  2. 截断表

    TRUNCATE TABLE table_name;

    这将重置表的自动增量,并从该表中删除所有记录。

  3. 删除并重新创建

    Table DROP TABLE table_name;

    CREATE TABLE table_name { ... };

来源:

How To Reset MySQL Autoincrement Column

【讨论】:

  • 我只是删除了之前的所有记录,所以这有效 ALTER TABLE table_name AUTO_INCREMENT = 1;非常感谢。
猜你喜欢
  • 2012-07-15
  • 2020-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多