- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
这没什么大不了的,但我想知道是否可以做到。现在,当我插入一条新记录时,自动增量并设置为主键的 id 字段将其值增加一个:
id | first_name | last_name
------------------------------
1 | John | Smith
2 | Jane | Doe
但是,如果我删除记录 2,我插入的下一条记录的 ID 将为 3。有什么方法可以让它的 ID 自动为 2(无需手动插入)?
最佳答案
auto_increment
计数器(至少,对于 InnoDB) 保存在表元数据中,并且独立于表中的数据:它是递增的当插入一些数据时,仅此而已。
所以,不,不可能让您的 auto_increment
列获得最大值 + 1 —— 或者至少在插入时不会。
不过,我想一个解决方案可能是更改
表,以强制auto_increment
计数器为一个新值。不确定您是否可以将它设置为比当前值更低的值,尽管(这正是您想要做的)。
有关更多信息,请参阅 the manual page of alter table
上的这一段(引用,强调我的):
To change the value of the
AUTO_INCREMENT
counter to be used for new rows, do this:ALTER TABLE t2 AUTO_INCREMENT = value;
You cannot reset the counter to a value less than or equal to any that have already been used.
For MyISAM, if the value is less than or equal to the maximum value currently in theAUTO_INCREMENT
column, the value is reset to the current maximum plus one.
For InnoDB, if the value is less than the current maximum value in the column, no error occurs and the current sequence value is not changed.
所以,根据手册:
auto_increment
的值
不过,请注意使用 alter table
可能不应该太频繁...
关于mysql - 如何在记录删除后从上次停止的地方创建一个 id auto_increment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2531055/
之前看到一些类似的问题,想弄清楚。 在this article ,据说异步调用“没有线程”。 然而,in another one ,据说 Here, however, we’re running th
我为我公司的平板电脑(SAMSUNG Galaxy Tab Active2)开发了一个简单的软件,自上周五(2018 年 11 月 23 日)以来,它无法正常工作。我检查了该应用程序的所有功能,其中有
我正在使用 hadoop map-reduce 作业进行一些文本处理。我的工作已完成 99.2%,并停留在上一个 map 工作上。 map 输出的最后几行如下所示。上次发生此问题时,我尝试打印出从 m
上次 Ubuntu 更新后有人对 Docker 有疑问吗?我有 2 个由 docker-compose 在 ubuntu18.04 中运行的项目。两个项目都运行良好,但在 *.yml 中进行 0 次更
SQL Developer 在表中显示公共(public)同义词的创建和上次 DDL 时间: CREATED 15-AUG-09 LAST_DDL_TIME 15-AUG-09 O
我正在使用带有以下插件的 mavenized 库项目: com.jayway.maven.plugins.android.generation2
我正在使用 Jssor Slider 作为轮播。在 Google Chrome 上次更新到版本 45.0.2454.85 之前,它一直运行良好。想法是它不显示图像。目前,它们位于标签中,可在 Safa
我是一名优秀的程序员,十分优秀!