2

I have a "user" table that contains id (PK, auto_increment), name, email (unique), cpf (unique), among other fields less important now. To add a user, I have data of all these tables mentioned but, obviously, id. CPF is a kind of Brazilian identity document.

I need to add a user if it doesn't exist on my table, and I can't overwrite. I googled, and discovered about the INSERT IGNORE. But how could this know that a user exists or not if I don't have the id, and this is my only primary key? How could I "teach" to the INSERT that these unique fields also can be compared?

Thanks!

Keoma Borges
  • 683
  • 2
  • 12
  • 27

1 Answers1

1

You will need to create a unique index on the cpf column. Then, your insert ignore query will check for duplicate cpf identifier on every insertion. And for more information on what would be a better approach to deal with duplicate insertions , see this : "INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE"

Community
  • 1
  • 1
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175