Prevent duplicate values for a column or column pair.

Section: constraints and keys

Add a unique key

sql
sql
ALTER TABLE users ADD CONSTRAINT uq_users_email UNIQUE (email);
Explanation

Unique constraints are commonly used for logins, slugs, and external identifiers.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing constraints and keys
Create a table with a foreign key
Reference a parent table and enforce relational integrity.
OpenIn sheetsqlsame section
Drop a foreign key
Remove a foreign key constraint from a table.
OpenIn sheetsqlsame section
Create a table with a composite primary key
Use multiple columns as the logical primary key.
OpenIn sheetsqlsame section
Drop a database
Delete an entire schema and all objects inside it.
OpenIn sheetsqlsame section
Create a database
Create a schema if it does not already exist.
Create a users table
Create a table with an auto-increment primary key and timestamps.