Last time we built basic table structures. Now let's make them smarter! Constraints ensure your data stays clean and complete automatically.
Think of constraints as quality control rules - they prevent bad data from entering your tables.
Engagement Message
What is one type of bad data that a constraint could block?
NOT NULL is your first line of defense against incomplete data. It prevents anyone from leaving critical columns empty.
Without NOT NULL, someone could create a customer record with no name or email!
Engagement Message
Which would be worse: a customer with no name or no email address?
Adding NOT NULL is simple - just include it after the data type in your column definition:
Now these columns must always have values.
Engagement Message
What happens if someone tries to insert a row without providing the first_name?
DEFAULT values automatically fill in columns when no value is provided. They're perfect for timestamps, status flags, or standard settings.
Instead of forcing users to always specify a signup date, let DEFAULT handle it automatically. Here's how to add DEFAULT values to your columns:
CURRENT_DATE automatically inserts today's date.
Engagement Message
Can you think of other columns that would benefit from automatic defaults?
IDENTITY columns automatically generate unique numbers for each new row. They're perfect for primary keys when you don't have natural unique identifiers.
