简体   繁体   English

删除行后重新调整auto_increment主键字段

[英]Readjust auto_increment primary key field after deletion of a row

I've following table configuration 我跟着表配置

CREATE TABLE Verses
(
ID int PRIMARY KEY AUTOINCREMENT,
verse_title varchar(255),
verse_desc varchar(255)
)

I want to keep id of all rows contiguously, and it is okay until I delete some rows. 我希望连续保留所有行的id,直到我删除一些行是没问题的。 after I deleted a row (say Id=2), I want to insert new insertion with id=2. 在我删除一行(比如Id = 2)后,我想插入id = 2的新插入。 please see attatched image. 请看附件图片。 在此输入图像描述

Use alter table AUTO_INCREMENT 使用alter table AUTO_INCREMENT

 ALTER TABLE tablename AUTO_INCREMENT = your_value

You can obtain the actual value for autoincremnt in this way .. 你可以用这种方式获得autoincremnt的实际值。

SELECT `AUTO_INCREMENT`
FROM  INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'your_database_name'
AND   TABLE_NAME   = 'your_table';

then you can use the alter table .. 然后你可以使用alter table ..

or you can select the count() of the rows after deleteing your row and the sue the alter tbale .. 或者你可以在删除你的行后选择行的count(),然后选择改变tbale。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM