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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:42:02+00:00 2026-06-01T14:42:02+00:00

I have a SQL Server table: +—-+————-+——————-+ | ID | CompanyID | CompanyCode |

  • 0

I have a SQL Server table:

+----+-------------+-------------------+
| ID | CompanyID   | CompanyCode       |
+----+-------------+-------------------+
| 1  | 1           | AAAA-123          |
| 2  | 2           | BBBB-111          |
| 3  | 1           | AAAA-123          |
| 4  | 3           | CCCC-999          |
| 5  | 3           | CCCC-999          |
| 6  | 1           | AAAA-123          |
+----+-------------+-------------------+

The ID field is the PK.

The CompanyID and CompanyCode fields specify a unique company whereby a CompanyID will ALWAYS have the same CompanyCode (and vice versa) and no two Companies will ever have the same CompanyID and/or CompanyCode.

I would like to create a rule on the table that will never allow a record to be added to the table if/when a CompanyCode doesn’t match an existing CompanyCode where the CompanyID‘s match.

The following is an example of what I cannot allow to occur on the table:

+----+-------------+-------------------+
| ID | CompanyID   | CompanyCode       |
+----+-------------+-------------------+
| 1  | 1           | AAAA-123          |
| 2  | 1           | BBBB-111          |<<<< This record should not be allowed
| 3  | 1           | AAAA-123          |
| 4  | 3           | CCCC-999          |
| 5  | 3           | CCCC-999          |
| 6  | 1           | AAAA-123          |
+----+-------------+-------------------+

Notice the record with ID=2, the CompanyCode BBBB-111 doesn’t match the existing record that has a CompanyCode of AAAA-123.

I want this rule to exist on the table somehow – i.e. I don’t want this rule to be a business rule that queries and/or stored procedures have to manage.

I suppose you can say I’m wanting to enforce duplicate records (on CompanyID and CompanyCode) if and only if an existing CompanyID and/or CompanyCode exists in the table.

Is this possible to do at the table design level? Or am I stuck with having to manage this in my scripts?

Update

Although this isn’t really pertinent to my OP, from the feedback I received, I suppose I should give a little background on the CompanyID/CompanyCode table design.

First off, this is just a mock table design to try to explain my question – my real table has nothing to do with companies.

Second, my real table is a middle-man between two web-services where Service1 creates a DeviceID and Service2 collects a SerialNumber that exists in a micro-device. Also, my real table contains many other columns that this middle-man server has to process.

One of the strange things about this middle-man service is that the table that I’m talking about has to allow NULLS for both the DeviceID and SerialNumber – it all depends on which Service sent a record first…. I’m am talking too much and going way off topic of my original question, but I thought I had to clarify my table example since I was getting much flak in regards to my breaking normalization.

  • 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-06-01T14:42:04+00:00Added an answer on June 1, 2026 at 2:42 pm

    Okay, this isn’t the prettiest of code, but it does enforce the constraint, I think. The trick is to create an indexed view with two unique indexes defined on it:

    create table dbo.ABC (
        Col1 int not null,
        Col2 int not null
    )
    go
    create view dbo.ABC_Col1_Col2_dep
    with schemabinding
    as
        select Col1,Col2,COUNT_BIG(*) as Cnt
        from
            dbo.ABC
        group by
            Col1,Col2
    go
    create unique clustered index IX_Col1_UniqueCol2 on dbo.ABC_Col1_Col2_dep (Col1)
    go
    create unique nonclustered index IX_Col2_UniqueCol1 on dbo.ABC_Col1_Col2_dep (Col2)
    go
    

    Now we insert some initial data:

    insert into dbo.ABC (Col1,Col2)
    select 1,3 union all
    select 2,19 union all
    select 3,12
    

    We can add another row with exactly the same values for Col1 and Col2:

    insert into dbo.ABC (Col1,Col2)
    select 1,3
    

    But if we pick a value for Col2 that has been used for another Col1, or vice versa, we get errors:

    insert into dbo.ABC (Col1,Col2)
    select 2,3
    go
    insert into dbo.ABC (Col1,Col2)
    select 1,5
    

    The trick here was to observe that this query:

        select Col1,Col2,COUNT_BIG(*) as Cnt
        from
            dbo.ABC
        group by
            Col1,Col2
    

    will only have one row for a particular Col1 value, and only one row with a particular Col2 value, provided that the constraint you’re seeking to enforce has not been broken – but as soon as a non-matching row is inserted into the base table, this query returns multiple rows.

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

Sidebar

Related Questions

I have a SQL Server table with a CreatedDate field of type DateTimeOffset(2). A
I have an SQL Server 2005 table that has a varchar(250) field which contains
I have a SQL Server table that contains the following fields: SessionId (guid) Message
I have a SQL Server 2008 R2 table with nvarchar(4000) field. Data that stores
I have a SQL Server table with fields: id, city, country . I imported
I have a SQL SERVER Table which has only one field StartDate the records
I have a sql server table with 2 fields, ID (primary key) and Name
I have a SQL Server table with 2 columns, Code and CodeDesc. I want
I have a SQL Server table in production that has millions of rows, and
I have a sql server table: CREATE TABLE [Workflow].[MilestoneDate]( [MilestoneDateId] [int] IDENTITY(1,1) NOT NULL,

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.