CREATE TABLE new_table_name LIKE old_table_name with old_table_name's AUTO_INCREMENT values

最后都变了- 提交于 2019-12-06 06:57:50

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!

Your CREATE TABLE can specify the auto_increment to use:

mysql> create table foo (i int primary key auto_increment, s varchar(12)) auto_increment = 10;
Query OK, 0 rows affected (0.19 sec)

mysql> insert into foo (s) values ("s");
Query OK, 1 row affected (0.09 sec)

mysql> select * from foo;
+----+------+
| i  | s    |
+----+------+
| 10 | s    |
+----+------+
1 row in set (0.03 sec)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!