Search code examples
mysqlsqlprimary-keyauto-incrementalter

Setting one field of Composite primary key to AUTO_INCREMENT


How to set one of the field of a primary key(composed of two fields of data type varchar(10) and int) of int data type to AUTO_INCREMENT, after declaring the primary key. Can we do it using ALTER command?


Solution

  • ALTER TABLE mytable ADD PRIMARY KEY( id, name);

    ALTER TABLE mytable CHANGE id id INT(11) NOT NULL AUTO_INCREMENT;