Search code examples
mysqlkeyuniquealter

MySQL ALTER table Multiple Column Key


Is it possible to alter a table's multiple (compound) column key?

Example table:

CREATE TABLE `test_abc` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `one` varchar(64) NOT NULL,
  `two` mediumint(8) unsigned NOT NULL,
  `three` varchar(128) NOT NULL,
  `four` datetime(3) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_abc` (`one`,`three`,`two`,`four`)
) ENGINE=InnoDB;

I would like to alter the key:

UNIQUE KEY `uk_abc` (`one`,`three`,`two`,`four`)

to:

UNIQUE KEY `uk_abc` (`one`,`two`,`three`,`four`)

Solution

  • Use this please

    ALTER TABLE test_abc
       DROP INDEX `uk_abc`, 
       ADD UNIQUE KEY `uk_abc` (`one`,`two`,`three`,`four`)