Constraints

Constraints allow us to use underlying relational database features to help us maintain database integrity. We used constraints to prevent duplicate categories in our application. There are many other ways we can leverage constraints.

For instance, when we create a video, we need to make sure that our category exists. We might be tempted to solve this problem by simply performing a query, but such an approach would be unsafe due to race conditions. In most cases, we would expect it to work like this:

  1. The user sends a category ID through the form.
  2. We perform a query to check if the category ID exists in the database.
  3. If the category ID does exist in the database, we add the video with the category ID to the database.

However, someone ...

Get Programming Phoenix 1.4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.