Unique Constraint
ELI5 — The Vibe Check
A unique constraint tells the database 'no two rows can have the same value in this column.' Perfect for email addresses — you do not want two users with the same email. The database will flat-out reject duplicate inserts.
Real Talk
A UNIQUE constraint ensures that all values in a column (or combination of columns) are distinct across all rows in the table. Unlike a primary key, a column with a UNIQUE constraint can contain NULL values (in most databases). It automatically creates an index on the column.
Show Me The Code
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email TEXT UNIQUE NOT NULL
);
When You'll Hear This
"Put a unique constraint on the username column." / "The insert failed because of a unique constraint violation."
Related Terms
Index
A database index is like the index in the back of a book. Without it, the database reads every single row to find what you want.
Not Null
NOT NULL is how you tell the database 'this field MUST have a value — you cannot leave it blank.
Primary Key
A primary key is the unique ID that every row in a table must have. Like a social security number for your data — no two rows can have the same one.
Table
A database table is exactly like a spreadsheet tab. It has columns across the top (name, email, age) and rows going down (one per person).