Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 960389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:10:33+00:00 2026-05-16T01:10:33+00:00

DBMS: MS Sql Server 2005, Standard I’d like to make a table constraint to

  • 0

DBMS: MS Sql Server 2005, Standard

I’d like to make a table constraint to have only one record have a particular value within a subset of the table (where the rows share a value in a particular column). Is this possible?

Example:
I have records in myTable which have a non-unique foreign key (fk1), and a bit column called isPrimary to mark out that this particular one should be used by our app for special logic.

in the abstract, it looks like this:

myTable
-------------
pk1       (int, not null)
name      (varchar(50), null)
fk1       (int, not null)
isPrimary (bit, not null)

I want to ensure that there is one and only one record with the isPrimary flag set to 1, for each unique value of fk1.

Data example:
This should be legal:

pk1     name     fk1    isPrimary
----    -----    -----  ----------
1       Bill     111    1
2       Tom      111    0
3       Dick     222    1
4       Harry    222    0

But this should not be (more than one where fk=111):

pk1     name     fk1    isPrimary
----    -----    -----  ----------
1       Bill     111    1
2       Tom      111    1
3       Dick     222    1
4       Harry    222    0

And neither should this (none where fk=222):

pk1     name     fk1    isPrimary
----    -----    -----  ----------
1       Bill     111    1
2       Tom      111    0
3       Dick     222    0
4       Harry    222    0

Is there a way to do this with a table constraint?

UPDATE
I’ve gone with Martin Smith’s answer for now, though I’ll be pushing for JohnFx’s refactor in an upcoming release, as it’s the best long-term solution. However I wanted to post my updated UDF based on Raze2dust’s answer, in case future readers decide that is a better fit for their needs.

CREATE FUNCTION [dbo].[OneIsPrimaryPerFK1](@fk1 INT, @dummyIsPrimary BIT)
RETURNS INT
AS 
BEGIN
    DECLARE @retval INT;
    DECLARE @primarySum INT;
    SET @retval = 0;
    DECLARE @TempTable TABLE (
    fk1 INT,
    PrimarySum INT)

INSERT INTO @TempTable
    SELECT fk1, SUM(CAST(isPrimary AS INT)) AS PrimarySum
    FROM FacAdmin
    WHERE fk1 = @fk1
    GROUP BY fk1;

    SELECT @primarySum = PrimarySum FROM @TempTable;
    IF(@primarySum=1)
        BEGIN
            SET @retval = 1
        END
    RETURN @retval
END;

Changes:

  1. Used @tempTable rather than

    tempTable (in memory v. written to disk) as required by udf

  2. passed @fk1 as a parameter so I can select for uniqueness within one
    group of fk1 values.
  3. tricky had to also pass isPrimary even though it isn’t
    necessary for the logic of the
    function, otherwise the SQL2005
    optimizer will not run the check
    constraint when isPrimary is
    updated.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:10:33+00:00Added an answer on May 16, 2026 at 1:10 am

    Using UDFs in check constraints can fail under snapshot isolation or multirow updates.

    Assuming that all your fk1 and pk1 values are currently (and will always be) positive you could create a computed column with the following definition

    CASE WHEN isPrimary = 1 THEN fk1 ELSE -pk1 END
    

    then add a unique constraint to that. Or if that assumption can’t be made then maybe

    CASE WHEN isPrimary = 0 THEN 1.0/pk1 ELSE fk1 END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I also have a very large table in SQL Server (2008 R2 Developer Edition)
How do you setup a SQL Server 2005 DBMS, so that you can store
I am using MS SQL Server 2005 as DBMS for my WinForms App. Data
I have a SQL Server 2008 DBMS, and I have created an EDM (generated
I have an asp.net website on a server and the db MS SQL 2005
I use SQL Server and when I create a new table I make a
Which of these queries is more efficient, and would a modern DBMS (like SQL
This is SQL Server question but I would appreciate the answers form other DBMS
I have been working with Microsoft SQL Server since 6.5 along with other database
I have a handful of raw SQL queries for SQL Server which use SCOPE_IDENTITY

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.