Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CREATE TABLE new_table_name LIKE old_table_name with old_table_name's AUTO_INCREMENT values

Can I create a new table with an old table's autoincriment status in mysql client?

I think, that ALTER TABLE new_table_name AUTO_INCREMENT=@my_autoincr_iment helps me, but this construction must use with a constant value. I don't want to use a difficult script.

like image 703
Minor Avatar asked Oct 16 '25 14:10

Minor


1 Answers

mysql> create table new_table like old_table;

mysql> select @my_auto_increment:=auto_increment from information_schema.tables where table_name='old_table';

mysql> set @query = CONCAT("alter table new_table auto_increment = ", @my_auto_increment);

mysql> prepare stmt from @query;

mysql> execute stmt;

mysql> deallocate prepare stmt;

Thx to my brother!

like image 57
Minor Avatar answered Oct 19 '25 04:10

Minor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!