Mysql Innodb: Autoincrement non-Primary Key

后端 未结 3 918
栀梦
栀梦 2020-11-30 09:20

Is it possible to auto-increment a non-Primary Key?

Table \"book_comments\"

book_id     medium_int
timestamp   medium_int
user_id     medium_int
vote         


        
3条回答
  •  囚心锁ツ
    2020-11-30 10:00

    You've existing table. If you already gave a primary key to comment_id and the only purpose is to set auto_increment. You can drop the primary key to that column. MySQL allows a column with any key property can access auto_increment. So better try an index or unique key to comment_id like below.

    1.Remove Primary Key

    ALTER TABLE `book_comments` MODIFY `comment_id` INT(5) NOT NULL;
    
    ALTER TABLE `book_comments` DROP PRIMARY KEY ;
    
    1. Add AUTO_INCREMENT with UNIQUE_KEY property.

    ALTER TABLE 'brand_keywords' CHANGE COLUMN 'comment_id' 'comment_id' INT(5) NOT NULL AUTO_INCREMENT UNIQUE;

提交回复
热议问题