?
Solved

Unique key by Id

Posted on 2007-03-30
2
Medium Priority
?
207 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 2000 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
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.

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