Solved

Unique key by Id

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Modifying SQL 2008/2012 PARTITIONS 3 55
SQL Query 2 60
Record open by another user 6 50
SQL Backup skipping a few tables 7 36
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will useā€¦
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 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

12 Experts available now in Live!

Get 1:1 Help Now