Add unique key constraint on existing column Sql query

When adding a unique key constraint to an existing column in SQL, the column must first be converted to a data type that can hold only unique values. This can be done using the ALTER TABLE command. Once the column has been converted to the appropriate data type, the UNIQUE KEY constraint can be added using the ADD CONSTRAINT command.


If you have a table named 'Employee' for example in your database and you want to add UNIQUE Constraint on that column. You can achieve that using the above query

Example: Add unique constraint on product_id columns

Below is a table called "products" that has one of the columns named product_id and we want to add a unique constraint to this column. We can do that using the below SQL query:

ALTER TABLE products
ADD UNIQUE (product_id);

Was this helpful?