Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make an entire MySQL row unique

Tags:

mysql

unique

I have a table in MySQL with 30 columns and thousands of entries.
Is there a way I could make every row unique, that means if a row already exists, I should not be able to enter that row again.

I can't use primary or unique keys here, because individually every column might be repeated.
I want the row to be unique. for example:-There are is a table with

columns->name,age,height,weight.

In this column I can't make any one or two columns unique but I should not have two entries with all the same data.

like image 229
Rasmus Avatar asked Jul 20 '11 12:07

Rasmus


3 Answers

You can make a unique index that includes all of the columns in your table

ALTER TABLE buyers ADD UNIQUE idx_row_unique(first_name,last_name,...);

This way you can keep a unique AUTO INCREMENT primary key for join purposes, while still ensuring that all of the data in your table is unique.

like image 77
Ryan Gross Avatar answered Oct 11 '22 12:10

Ryan Gross


You can hash the data and set the hash value as your PK, this will ensure that the rows are unique.

like image 32
jacky luster Avatar answered Oct 11 '22 12:10

jacky luster


You may create UNIQUE key on all the columns, not individual unique keys on each column. This means that the combination of the values will be unique - exactly what you need. But please note, that if any column allows null value, if the column contains null value, it will be counted as unique, even if another row contains the same values, with null for the same value.

like image 39
Maxim Krizhanovsky Avatar answered Oct 11 '22 11:10

Maxim Krizhanovsky