How add unique key to existing table (with non uniques rows)

MysqlUnique Key

Mysql Problem Overview


I want to add complex unique key to existing table. Key contains from 4 fields (user_id, game_id, date, time). But table have non unique rows. I understand that I can remove all duplicate dates and after that add complex key.

Maybe exist another solution without searching all duplicate data. (like add unique ignore etc).

UPD I searched, how can remove duplicate mysql rows - i think it's good solution. https://stackoverflow.com/questions/3383898/remove-duplicates-using-only-a-mysql-query

Mysql Solutions


Solution 1 - Mysql

You can do as yAnTar advised

ALTER TABLE TABLE_NAME ADD Id INT AUTO_INCREMENT PRIMARY KEY

OR

You can add a constraint

ALTER TABLE TABLE_NAME ADD CONSTRAINT constr_ID UNIQUE (user_id, game_id, date, time)

But I think to not lose your existing data, you can add an indentity column and then make a composite key.

Solution 2 - Mysql

The proper syntax would be - ALTER TABLE Table_Name ADD UNIQUE (column_name)

> Example

ALTER TABLE  0_value_addition_setup ADD UNIQUE (`value_code`)

Solution 3 - Mysql

I had to solve a similar problem. I inherited a large source table from MS Access with nearly 15000 records that did not have a primary key, which I had to normalize and make CakePHP compatible. One convention of CakePHP is that every table has a the primary key, that it is first column and that it is called 'id'. The following simple statement did the trick for me under MySQL 5.5:

ALTER TABLE `database_name`.`table_name` 
ADD COLUMN `id` INT NOT NULL AUTO_INCREMENT FIRST,
ADD PRIMARY KEY (`id`);

This added a new column 'id' of type integer in front of the existing data ("FIRST" keyword). The AUTO_INCREMENT keyword increments the ids starting with 1. Now every dataset has a unique numerical id. (Without the AUTO_INCREMENT statement all rows are populated with id = 0).

Solution 4 - Mysql

I am providing my solution with the assumption on your business logic. Basically in my design I will allow the table to store only one record for a user-game combination. So I will add a composite key to the table.

PRIMARY KEY (`user_id`,`game_id`)

Solution 5 - Mysql

Either create an auto-increment id or a UNIQUE id and add it to the natural key you are talking about with the 4 fields. this will make every row in the table unique...

Solution 6 - Mysql

> Set Multiple Unique key into table

ALTER TABLE table_name
ADD CONSTRAINT UC_table_name UNIQUE (field1,field2);

Solution 7 - Mysql

For MySQL:

ALTER TABLE MyTable ADD MyId INT AUTO_INCREMENT PRIMARY KEY;

Solution 8 - Mysql

If yourColumnName has some values doesn't unique, and now you wanna add an unique index for it. Try this:

CREATE UNIQUE INDEX [IDX_Name] ON yourTableName (yourColumnName) WHERE [id]>1963 --1963 is max(id)-1

Now, try to insert some values are exists for test.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyAnTarView Question on Stackoverflow
Solution 1 - MysqlIgor LozovskyView Answer on Stackoverflow
Solution 2 - MysqlranojanView Answer on Stackoverflow
Solution 3 - Mysqluser3149374View Answer on Stackoverflow
Solution 4 - MysqlSlowcoderView Answer on Stackoverflow
Solution 5 - MysqlHituptonyView Answer on Stackoverflow
Solution 6 - Mysqlprakash kumarView Answer on Stackoverflow
Solution 7 - MysqlVetteView Answer on Stackoverflow
Solution 8 - MysqlTona DinhView Answer on Stackoverflow