简体   繁体   中英

Make column auto_increment and reposition

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. 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.

设置一个已经存在的列AUTO_INCREMENT和一个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. 没有显示错误消息,很难说出错误是什么...请发布。
  2. ALTER TABLE t1 ADD col3 VARCHAR(60) AFTER col1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM