Solved

Unique key by Id

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now