views:

98

answers:

2

Hello! 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 wanna use a difficult script.

Drochite na carmu!

+1  A: 

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)
Frank Shearar
I don't know, how post mysql's syntax. Your exemple is't suitable.
Minor
mysql> `set @my_auto_increment=555;`Query OK, 0 rows affected (0.00 sec)mysql> `select @my_auto_increment;`+--------------------+ | @my_auto_increment | +--------------------+ | 555 |+--------------------+ 1 row in set (0.00 sec)mysql> `ALTER TABLE zz AUTO_INCREMENT=@my_auto_increment;`ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@my_auto_increment' at line 1
Minor
+1  A: 

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!

Minor