开发者

Make column auto_increment and reposition

开发者 https://www.devze.com 2023-02-03 00:33 出处:网络
Hey. I\'m currently learning to use MySql and have a couple of questions. 1) How do I update a column (empty table) to make it an AUTO_INCREMENT column? I tried

Hey. I'm currently learning to use MySql and have a couple of questions.

1) How do I update a column (empty table) to make it an AUTO_INCREMENT column? I tried

ALTER TABLE book UPDATE id AUTO_INCREMENT;

but I get an SQL Syntax error.

2) If I add a new column, how can I change its position in the table? For example, if I add a new column, it's added to the end. 开发者_JAVA技巧What if I want to move it to the beginning?

Thanks


Per the ALTER Syntax you probably want to be using the MODIFY keyword (not UPDATE).

Also, not sure if it will work (depending on your column defintiions), but this is how you'd go about it:

Try this:

ALTER TABLE book
MODIFY id AUTO_INCREMENT;

If it doesn't you'll need to drop the column first, then re-add it:

ALTER TABLE book
DROP COLUMN id;

ALTER TABLE book
ADD COLUMN id INT NOT NULL AUTO_INCREMENT FIRST; -- "FIRST": Add column to beginning

You may also want it to be the PRIMARY KEY, but I don't know what your table structure is so just a guess.


Make an already existing column AUTO_INCREMENT and a PRIMARY KEY:

ALTER TABLE t1 MODIFY id int(11) AUTO_INCREMENT PRIMARY KEY


Regarding the ordering, you can use:

ALTER TABLE table_name ADD column VARCHAR(60) FIRST;

to move it to the beginning.


  1. without showing the error-message, it's hard to say whats the error... please post it.
  2. ALTER TABLE t1 ADD col3 VARCHAR(60) AFTER col1;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号