Solved

Unique key by Id

Posted on 2007-03-30
2
204 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
[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
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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