Solved

Unique key by Id

Posted on 2007-03-30
2
203 Views
Last Modified: 2010-03-19
Hi Experts,

Here is my table:
Users (UserId, ApplicationId, UserName...)

I want the username to be unique by ApplicationId when I insert or update.
Can I create a unique key for the username to raise an error on insert or update ? If so, how can I limit the key to check a unique username by ApplicationId and not for  all the table ?

Thanks in advance for your help.
0
Comment
Question by:noulouk
  • 2
2 Comments
 
LVL 10

Expert Comment

by:ksaul
ID: 18826401
You could add a new non-clustered index, including both fields (ApplicationId, UserName) and check the unique box.
0
 
LVL 10

Accepted Solution

by:
ksaul earned 500 total points
ID: 18826447
Or with SQL:

CREATE UNIQUE NONCLUSTERED INDEX [idx_Application_UserName] ON [dbo].[Users]
(
      [ApplicationId] ASC,
      [UserName] ASC
) ON [PRIMARY]
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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