views:

52

answers:

1

Hi,

I have two mysql tables, one needs to start its auto-increment column id with the last value of the last inserted row in the other table (plus 1).

According to mysql manual you can restart the value of an auto increment column like this:

mysql> ALTER TABLE tbl AUTO_INCREMENT = 100;

However, this is not possible:

mysql> ALTER TABLE tb2 AUTO_INCREMENT = (SELECT MAX(id) FROM tbl1);

I need to perform something like this because I'm filling the tables using a script. Is there another way to achieve it?

+1  A: 

I think you are trying to use the auto-increment column for something it is not well suited to. If you care about the exact values that get inserted then it shouldn't be auto-increment.

Mark Byers