How do I apply unique constraint on two columns SQL Server?

sql-server

Solution

You can create a multi column unique constraint like this

ALTER TABLE MyTable ADD UNIQUE (Type, Name)

That will enforce the rules you describe.

Problem

I have two columns type and name in the database. I want to apply a unique constraint where the name is unique within each type. ``` Type Name A ABC R ABC B ABC ``` should be allowed whereas ``` Type Name A ABC A ABC ``` should not be allowed How to enforce this constraint where name is unique within each type? Thanks,

Original source

Related problems