Here is the question I received on twitter.
“Can we create a unique constraint on table column on Existing Table?”
Of course Yes!
Here is how you can create a unique constraint on the table which already exist in our system.
-- Create Table
CREATE TABLE Table1 (ID INT, Col1 VARCHAR(100))
-- Alter Table Create Constraint
ALTER TABLE Table1
ADD CONSTRAINT UX_Constraint UNIQUE (Col1)
-- Clean up
DROP TABLE Table1
If your table already exists you can use above method to create the constraint. However, if you are about to create tables, you can just specify UNIQUE in the schema definition of Create Table itself. We will discuss about this in a future post.
Reference: Pinal Dave (https://blog.sqlauthority.com)