2010-03-02 43 views

回答

4

的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我的兄弟!

1

你CREATE TABLE可以指定AUTO_INCREMENT使用方法:

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) 
+0

我不知道,怎麼發佈mysql的語法。你的例子不適合。 – Minor 2010-03-02 09:47:43

+0

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) :您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以獲得在第1行'@my_auto_increment'附近使用的正確語法 – Minor 2010-03-02 10:19:14