Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add new unique index to the existing database table?

I have database table but without the index, and I want to add index id to that table, that will be uniqe for each row, how can I do this using mysql?

like image 478
jcubic Avatar asked Dec 21 '22 01:12

jcubic


1 Answers

Assuming you don't have a key on the table already you can do this:

ALTER TABLE whatever ADD id Int NOT NULL AUTO_INCREMENT PRIMARY KEY;

And remember you can add FIRST to the end of that line to make it the first column which would be a good idea for an id.

like image 179
DarkAjax Avatar answered Dec 25 '22 23:12

DarkAjax