vendredi 2 janvier 2015

Check if column of a table has unique constraint


Vote count:

0




I need a query which can tell me if a column of a table has unique constraint or not. If doesn't have, I have to add unique constraint. Currently I am using below query to check if a table's column has unique constraint or not:



IF NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
WHERE TC.CONSTRAINT_TYPE = 'UNIQUE' AND CONSTRAINT_NAME = 'IX_Product_Users' AND TABLE_NAME = 'Product_Users')
BEGIN
ALTER TABLE Product_Users
ADD CONSTRAINT IX_Product_Users UNIQUE (EmpID)
END
GO


But I think this is a wrong way cause there might be possibility that unique constraint name is different. Is there any precise way to do this?



asked 1 min ago

DH__

2,268






Check if column of a table has unique constraint

Aucun commentaire:

Enregistrer un commentaire