2010
05.19

For a project I was working on I needed a multi field (column) key. Both `id` and `server` were not unique, but put together they were. I already had `id` as my PRIMARY KEY, but that was not correct anymore. So the first thing to do was remove that KEY:

ALTER TABLE `clients` DROP PRIMARY KEY

And after that I created the `server` field:

ALTER TABLE `clients` ADD `server` VARCHAR(20) NOT NULL AFTER `uid` ;

According to this post there a few things to think about:

  1. a multiple PRIMARY KEY can’t use AUTO_INCREMENT
  2. each multiple field KEY is only allowed to spread 16 columns
  3. there is a maximum overall length of 256 Bytes

After considering this (and concluding there were no problems) I created the PRIMARY KEY:

ALTER TABLE `clients` ADD PRIMARY KEY (`uid`, `server`) ;

If we wanted to have an UNIQUE KEY we should executed:

ALTER TABLE `clients` ADD UNIQUE (`uid`, `server`) ;

No Comment.

Add Your Comment