Mysql – unable to add index to column, duplicate entry

MySQL

I have a table that collects virtual machines usage metrics, I'm trying to add an index to timestamp column, to speed up queries, when selecting time, this is a sample of the table I have, it has other columns, I have only listed 3 here.

    id  timestamp            vm_name  
------  -------------------  ---------
     1  2015-05-05 18:21:50  i-2-20-VM   
     2  2015-05-05 18:21:50  i-2-21-VM   
     3  2015-05-05 18:21:50  i-2-22-VM   
     4  2015-05-05 18:21:50  i-2-23-VM   
     5  2015-05-05 18:21:50  i-2-24-VM   
     6  2015-05-05 18:21:50  i-2-25-VM   
     7  2015-05-05 18:21:50  i-2-26-VM   
     8  2015-05-05 18:21:50  i-2-27-VM   
     9  2015-05-05 18:21:50  i-2-28-VM   
    10  2015-05-05 18:21:50  i-2-29-VM   

As you can see, my application is inserting duplicate timestamp entry.

When attempting to add an index to timestamp column, I get the following error message.

Duplicate entry '2015-05-05 18:21:50' for key 'PRIMARY'

I'm not sure what do I need to change in my DB design to resolve this issue.

Your suggestions are highly appreciate it.

Best Answer

I think you are trying to create a primary key and not a simple index. (Primary key requires all values to be unique)

To add a index in mysql follow the syntax here: https://dev.mysql.com/doc/refman/5.7/en/create-index.html