?
Solved

Unique index dependent on a column

Posted on 2010-01-12
2
Medium Priority
?
264 Views
Last Modified: 2012-05-08
Folks,

Suppose I have the following table in SQL Server:
Name     IsRetired
------      -----------
John       0
Joe         0
John       0
John       1

I want to make Name unique IF IsRetired is 1.  So the example above is fine because John appears only once when IsRetired is 1.  However, I couldn't add another John and set IsRetired to 1.

is there a way to create a unique index for this?
0
Comment
Question by:sfun28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 2000 total points
ID: 26297998
I do not believe unique indexes can be partially unique. In your example, you have two John's with a IsRetired = 0.

You could handle this through a Insert/Update trigger, however that starts to embed business logic into the trigger, where it probably should not be.
0
 
LVL 1

Author Comment

by:sfun28
ID: 26298135
agreed.  thanks so much
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question