1ALTER TABLE table_name ADD CONSTRAINT cst_name UNIQUE (col_name);
2ALTER TABLE table_name ADD CONSTRAINT cst_name UNIQUE (col1, col2); -- Multiple
3ALTER TABLE table_name ADD col_name NUMBER UNIQUE; -- New field
1This constraint ensures all values in a column are unique.
2Example 1 (MySQL): Adds a unique constraint to the id column when
3creating a new users table.
4CREATE TABLE users (
5id int NOT NULL,
6name varchar(255) NOT NULL,
7UNIQUE (id)
8);
9Example 2 (MySQL): Alters an existing column to add a UNIQUE
10constraint.
11ALTER TABLE users
12ADD UNIQUE (id);