Preventing Duplicate records in MySQL

341 Views Asked by At

I am currently working on a evolutionary algorithm which will try to find an optimum configuration of parameters for a given problem.

I'm trying to create records in a MySQL table which will store a configuration where each parameter will be represented as a table column. If a configuration has already been attempted then I would like to prevent it from being duplicated.

Is there a mechanism to do this at the database/ORM level or should I create programmatic logic to prevent duplicate entries?

1

There are 1 best solutions below

9
Rahul On BEST ANSWER

Is there a mechanism to do this at the database/ORM level

Yes, declare or define the Configuration column as Primary Key or Unique Key while you are creating your table to avoid duplication.

I mentioned configuration column cause I am not sure about your table structure but if it involves multiple column then you can make it a composite key always. Why you are worried about key lookup. if you design your table properly with proper index then you shouldn't worries about that.

Say your col1, col2, col3, col4 is part of configuration and you want them to be unique. Then make them as composite primary key saying

primary key (col1, col2, col3, col4)