SteelReed

Search

Search posts, questions…

Middle

If Java already validates input, do you still need NOT NULL, CHECK, FK and UNIQUE in the database?

Short answer

Yes. Java validation protects one entry point; database constraints protect every entry point and are the only thing that stops two concurrent requests from both writing.

DatabaseRequest BRequest ADatabaseRequest BRequest Aemail exists? noemail exists? noINSERT okINSERT rejected by UNIQUE

3 points to say:

  1. Java gives friendly errors; the DB enforces the real rule.
  2. Check-then-insert breaks under concurrency.
  3. CASCADE only within one unit of data.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.