0

This is a general question. How can I avoid duplicate entries in some database tables? I am implementing a books database which has table for books, which references a table with authors and a table with publishers. Currently, for example, the publishers gets generated with duplicate values. For example:

id   name
1    Addison-Wesley
4    Addison-Wesley
7    Addison-Wesley

Same for authors. I would like that duplication is avoided and there would be only one entry for Addison-Wesley forever:

id
1   Addison-Wesley

I am using MySQL database. And this is a Spring Boot application with JPA dependencies. Do I have to programmatically check for duplicates? For example, in the above Publishers, I could check if "name" is in the database and thus avoid inserting it to the database table?

Omar Amaoun
  • 526
  • 1
  • 3
  • 20
M2evorah
  • 111
  • 1
  • 8
  • My suggestion to your " Do I have to programmatically check for duplicates?" is yes.. In my experience best way to go forward, if you don't want to use/set database integrity – user2880879 May 25 '20 at 01:30
  • 1
    Add `unique` into your entity to check the duplicated keys. check this post https://stackoverflow.com/questions/3126769/uniqueconstraint-annotation-in-java may answer your question – PatrickChen May 25 '20 at 02:20
  • You can also add `UNIQUE` constraint to your `name` column in database. – Oleg Spiridonov May 25 '20 at 13:36

0 Answers0