简体   繁体   中英

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. after I deleted a row (say Id=2), I want to insert new insertion with id=2. please see attatched image. 在此输入图像描述

Use alter table AUTO_INCREMENT

 ALTER TABLE tablename AUTO_INCREMENT = your_value

You can obtain the actual value for autoincremnt in this way ..

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 ..

or you can select the count() of the rows after deleteing your row and the sue the alter tbale ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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