Home  >  Article  >  Database  >  How to rearrange ids in mysql

How to rearrange ids in mysql

coldplay.xixi
coldplay.xixiOriginal
2020-10-12 11:08:415683browse

Mysql method to rearrange ids: 1. Use column counting [alter table tablename drop column id]; 2. Use increment arrangement [alter table tablenam auto_increment].

How to rearrange ids in mysql

Mysql's method of rearranging IDs:

When using mysql, there is usually an auto-increment in the table The id field, but when we want to clear the data in the table and re-add the data, we hope that the id will start counting from 1 again. We can use the following two methods:

Method 1:

alter table tablename drop column id;
alter table tablename add id mediumint(8) not null primary key auto_increment first;

Method 2:

alter table tablename auto_increment=0

Method 2 will not clear the existing data, and the operation is more flexible. It can not only reset the auto-increment value to zero, but is also suitable for resetting the auto-increment value and inserting new data after deleting a large number of consecutive rows. ; Or start from a new value, which of course cannot conflict with the existing value.

$sql="delete from $table_vote"; 
mysql_query($sql, $link); 
$sql="alter table $table_vote auto_increment=1"; 
mysql_query($sql, $link);

More related free learning recommendations: mysql tutorial(Video)

The above is the detailed content of How to rearrange ids in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn