Introduction
1. Constraints - Introduction
UNIQUE
NOT NULL
CHECK
DEFAULT
Summary

Instruction

Welcome to Part 5 of our course. So far, you've done pretty well. You already know

  • how to create tables with the right column types,
  • what primary keys are and
  • how to build tables based on ERD diagrams.
  • That's amazing. This time, you're going to learn about another thing which will make life much easier: constraints.

    Constraints are special rules for data in tables. They tell the database what values are allowed in specific columns. For instance, each salary should be greater than 0, each person should have a name in the database etc. If there is a value which violates the rules, the row won't be added. Constraints are used to keep data in the database correct.

    Let's get started!

Exercise

In this part of the course, we're going to work with a website for game lovers. The website enables users to add new games which they find in stores and provide basic information about them.

Because the content of the website is provided by its users, there are some problems: some games occur a few times, some data have negative values etc. We need to implement the proper mechanisms to prevent such situations. We'll start off with board games.

Take a look at the example code. Pay attention to the new elements: UNIQUE, NOT NULL, CHECK, DEFAULT. Try to guess what they mean. We'll explain everything in great detail in this part of the course. When you're done, press the Next exercise button to continue.

Stuck? Here's a hint!

Simply press the Next exercise button!

Console

Code editor

Result

TableConsole