Last time we learned to enforce data quality with NOT NULL and DEFAULT. Now let's tackle uniqueness! Sometimes you need to guarantee that certain values never appear twice.
Think about email addresses - would you want two customers with identical emails?
Engagement Message
What problems could duplicate emails cause in a customer database?
The UNIQUE constraint prevents duplicate values in a column. It's like having a bouncer that checks "Have I seen this value before?" and blocks repeats.
Once someone registers with "john@email.com", nobody else can use that same address.
Engagement Message
Besides email, what other customer information should be unique?
Adding UNIQUE is simple - just include it in your column definition:
Now both email and phone must be one-of-a-kind across all customers.
Engagement Message
What happens if you try inserting two customers with the same phone number?
Sometimes uniqueness spans multiple columns together. A student can take multiple courses, and a course can have multiple students, but each student-course combination should be unique.
This prevents accidentally enrolling the same student in the same course twice.
Engagement Message
Can you think of other situations where multiple columns together should be unique?
Multi-column uniqueness uses table-level constraints. List the columns in parentheses:
