6

There are two tables:

maintable(id int primary key auto_increment, name char);

subtable(id int primary key, maintable_id int, index mianid (maintable_id), constraint mainid foreign key (maintable_id) references maintable(id) );

after some add and delete in maintable, i wanna reset the auto_increment of maintable, then i use "alter table maintable auto_increment = 1",but i just got "query ok ,0 rows affected". And "truncate maintable" was forbidden because it is referenced by subtable.

so my question is: how can i reset the auto_increment of maintable? appreciate for any thoughts!! thanks a lot !!!

ps: mysql5.6; InnoDB.

1 Answer 1

4

FIRST you remove all the reference records. After that do this

alter table maintable auto_increment=1;

It will work.

Please Refer Click here

2
  • thanks you! i do think this will work,but what if there are other tables referencing this table,then to reset the auto_increment of maintable, i need remove all all the references to it? Is there a better solution for this issue? or i just have to do so? Nov 26, 2013 at 10:30
  • @user3035623 Nop. There is no way for MySQL (or even for people) to know if YOU reference this field in another table, unless you have some triggers or constraints. In that case, you should (somehow) update your triggers (or create news) to check that condition. Jul 29, 2019 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.