Monday 21 March 2011

How to create a Unique index based on two columns

I had a requirement to have a constraint on a database table to not allow duplicate document no's in the table. This was complicated by the fact you could have the same document no in two different records in the table if the databaseID of the record was different. The constraint should also only be in place if there is a Value in the document No column.

Here is the SQL Query to add the constraint in SQL 2008.



If you are using SQL 2005 then you can achieve this by creating a view and adding a constraint to the view.

No comments:

Post a Comment