Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: making a column unique?

I have a table that is in production. I realize that some of the columns should be unique. Is it safe to go into phpMyAdmin and change those columns to make it unique?

ALTER TABLE  `foo` ADD UNIQUE ( `bar` )
like image 668
StackOverflowNewbie Avatar asked Jul 04 '11 05:07

StackOverflowNewbie


2 Answers

Follow the below steps to apply unique column value from phpmyadmin panel:

Go to the table structure. Click on the unique keyword as like below -

enter image description here

Click on the ok from confirmation box -

enter image description here

Unique value constraint for column will apply. Or you can run mysql query:

ALTER TABLE user ADD UNIQUE(email);

like image 140
Rahul Mankar Avatar answered Sep 30 '22 12:09

Rahul Mankar


  1. You do not have duplicates -> will apply the key without issues
  2. You do have duplicates -> will give an error message, nothing happened to your data
  3. All is unique, except several rows with NULL in them, unique constraint is still applied, as NULL is not checked when checking for unique values (you can have the entire table have a NULL value in a unique field without any error message).

One more thing, if you have a prod DB, you must also have a dev DB which you can test on without fear, right?

like image 24
Itay Moav -Malimovka Avatar answered Sep 30 '22 13:09

Itay Moav -Malimovka