Constraints: Rules the Database Enforces
Do not rely on every program to check data; make the database refuse bad data. Constraints are rules attached to columns:
NOT NULL: a value is required.UNIQUE: no two rows may share the value (an email address).CHECK (condition): the value must pass a test (price >= 0).DEFAULT value: used when none is given.PRIMARY KEYandFOREIGN KEY, which you already know.
Constraints protect data quality at the lowest level, where every application and every careless script must obey them.
Examplepython
Quick check
Which constraint stops two users sharing an email address?
Your turn: write the code
Create scores with a CHECK that value is between 0 and 100, then try to insert 150 and print refused when it fails.
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.