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 3759668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:21:08+00:00 2026-05-19T10:21:08+00:00

To elabortate, is it possible to enforce a rule where ONLY one record entry

  • 0

To elabortate,

is it possible to enforce a rule where ONLY one record entry can have a column named ‘IsPrimaryUser’ set to true, whereas all others grouped by another column are set to false. The condition for deciding which entry will have a true ‘IsPrimaryUser’ field will be the CompanyId column.

I am only interested in whether it can be done using check constraints. Obviously, there is a SQL approach to something like this.

Example:

User Table

int UserId | int CompanyId | bit IsPrimaryUser

Data:

UserId  |  CompanyId  | IsPrimaryUser
   1          1              1
   2          1              0
   3          1              0
   4          1              0
   5          2              1
   6          2              0
   7          2              0
   8          2              0
  • 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-19T10:21:08+00:00Added an answer on May 19, 2026 at 10:21 am

    Check Constraints only work on a single row, but you can use scalar UDFs within the constraint.

    You can breach the single-row check by using UDFs that check other rows in the table. Although unlike a trigger where you can access the DELETED virtual table and process individually, SQL Server seems to hold the records in a sort of transaction, and perform a CHECK on EACH row after the change, then finally accepting or aborting the CRUD in batch.

    See this test case

    Create table

    create table usertable (UserId int,
    CompanyId int, IsPrimaryUser int)
    

    Populate

    insert usertable select
    1, 1, 1 union all select
    2, 1, 0 union all select
    3, 1, 0 union all select
    4, 1, 0 union all select
    5, 2, 1 union all select
    6, 2, 0 union all select
    7, 2, 0 union all select
    8, 2, 0
    

    Scalar function helper

    create function dbo.anyprimaryuser(@userid int, @company int) returns bit as
    begin
    return
        case when exists (
        select * from usertable
        where companyid=@company and isprimaryuser=1 and userid<>@userid)
        then 1 else 0 end
    end
    

    The CHECK constraint

    alter table usertable add constraint usertable_ck1
    check (isprimaryuser=0 or dbo.anyprimaryuser(userid,companyid)=0)
    

    Tests

    insert usertable select 9,2,1 -- fail
    
    insert usertable select 9,2,0 -- ok
    
    insert usertable select 19,4,1 union all select 20,4,0 -- ok
    
    insert usertable select 19,3,1 union all select 20,3,0 union all select 21,3,1
    -- not ok, accepting the multi-row insert will breach the constraint
    
    update usertable set IsPrimaryUser=1-IsPrimaryUser where CompanyId=4
    -- ok! sets one and unsets the other in one go
    

    (note) I updated the answer after Martin’s comment below

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A single Biztalk Server can have multiple Host processes. Is it possible to create
Recently I have heard the bellow statement. Can someone please elaborate on it? With
I'm not exactly sure if the following scenario is possible. Using only UdpClient or
Is possible set 2 methods on before_validation in a model? before_validation :elaborate before_validation :download_remote_data,
I have a string with possible command line arguments (using an Read-Eval-Print-Loop program) and
Here is the doubt I have come up with ServletContext is one per web-app
One of the best features about Firefox and Firebug is that you can see
I'm trying to have a specific page on my site only accessible to people
Can you elaborate what goes behind the scene when we create DirectoryEntry instance? Code
I'm building a semi-elaborate RadGrid where within my NestedViewTemplate I want to have a

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.