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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:15:18+00:00 2026-06-06T19:15:18+00:00

Question: I want to add a unique constraint on a mapping table (n:n). I

  • 0

Question:

I want to add a unique constraint on a mapping table (n:n).
I want that new values may be inserted, but only if TEST_FK_UID, TEST_DateFrom and TEST_DateTo are not equal to an already existing entry.

The problem is the status field.
Status 1 means active..
Status != 1 means inactive/deleted..
.
.
So one may of course insert a new entry with the same FK, DateFrom and DateTo, IF – and only if – the status of the existing entry (all existing entries, as you can insert, delete, insert, delete, insert, delete, etc.) is != 1

Here is what I have so far:

CREATE TABLE dbo._________Test  
(
     TEST_UID uniqueidentifier NOT NULL 
    ,TEST_FK_UID uniqueidentifier NOT NULL 
    ,TEST_DateFrom DateTime NOT NULL 
    ,TEST_DateTo DateTime NOT NULL  
    ,TEST_Status int NOT NULL 
    ,UNIQUE(TEST_FK_UID, TEST_DateFrom, TEST_DateTo, TEST_Status) 
); 
  • 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-06T19:15:19+00:00Added an answer on June 6, 2026 at 7:15 pm

    It is very possible, like this
    (basic credit goes to: https://stackoverflow.com/users/103075):


    Edit:
    OK, pedantically seen it’s not a unique constraint, it’s a check constraint, but WTF – it has the same effect and works on SQL-Server 2005 as well, and the (conditional) condition is configurable per customer (replace SET @bNoCheckForThisCustomer = ‘false’ with a select to a configuration table) – that’s not possible with a unique index AFAIK … 😉


    Note this line:

    AND ZO_RMMIO_UID != @in_ZO_RMMIO_UID
    

    (ZO_RMMIO_UID is the unique primary key of the n:n mapping table)

    It’s important, since a check constraint seems to be similar to a onAfterInsert trigger.
    If this line is missing, it checks on itselfs as well, which leads to the function always returning true…

    IF  EXISTS (SELECT * FROM sys.check_constraints WHERE object_id = OBJECT_ID(N'[dbo].[CheckNoDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt]') AND parent_object_id = OBJECT_ID(N'[dbo].[T_ZO_AP_Raum_AP_Ref_Mietobjekt]'))
    ALTER TABLE [dbo].[T_ZO_AP_Raum_AP_Ref_Mietobjekt] DROP CONSTRAINT [CheckNoDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt]
    GO
    
    
    
    
    
    IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fu_InsertCheck_IsDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
    DROP FUNCTION [dbo].[fu_InsertCheck_IsDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt]
    GO
    
    
    
    
    
    -- ========================================================================
    -- Author:            Me
    -- Create date:       09.08.2010
    -- Last modified:     09.08.2010
    -- Description:   Conditionally check if row is a duplicate
    -- ========================================================================
    
    -- PRE:  UID, Valid RM_UID, Valid MIO_UID, 
    --       Valid datetime-from for db usr language, valid datetime-to for db usr language
    -- POST: True/False
    
    
    CREATE  FUNCTION [dbo].[fu_InsertCheck_IsDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt](@in_ZO_RMMIO_UID uniqueidentifier, @in_ZO_RMMIO_RM_UID AS uniqueidentifier, @in_ZO_RMMIO_MIO_UID as uniqueidentifier, @in_ZO_RMMIO_DatumVon AS datetime, @in_ZO_RMMIO_DatumBis AS datetime)
        RETURNS bit
    AS
        BEGIN   
    
            DECLARE @bIsDuplicate AS bit
            SET @bIsDuplicate = 'false'     
    
    
            DECLARE @bNoCheckForThisCustomer AS bit
            SET @bNoCheckForThisCustomer = 'false'
    
            IF @bNoCheckForThisCustomer = 'true'
                RETURN @bIsDuplicate 
    
    
    
    
            IF EXISTS
            (
                SELECT 
                     ZO_RMMIO_UID
                    ,ZO_RMMIO_RM_UID
                    ,ZO_RMMIO_MIO_UID
                FROM T_ZO_AP_Raum_AP_Ref_Mietobjekt 
                WHERE ZO_RMMIO_Status = 1 
                AND ZO_RMMIO_UID != @in_ZO_RMMIO_UID
                AND ZO_RMMIO_RM_UID = @in_ZO_RMMIO_RM_UID 
                AND ZO_RMMIO_MIO_UID = @in_ZO_RMMIO_MIO_UID 
                AND ZO_RMMIO_DatumVon = @in_ZO_RMMIO_DatumVon 
                AND ZO_RMMIO_DatumBis = @in_ZO_RMMIO_DatumBis 
            )
                SET @bIsDuplicate = 'true'
    
            RETURN @bIsDuplicate
        END
    
    
    GO
    
    
    
    
    ALTER TABLE [dbo].[T_ZO_AP_Raum_AP_Ref_Mietobjekt]  WITH NOCHECK ADD  CONSTRAINT [CheckNoDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt] 
    CHECK  
    (
        NOT 
        (
            dbo.fu_InsertCheck_IsDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt(ZO_RMMIO_UID, ZO_RMMIO_RM_UID, ZO_RMMIO_MIO_UID, ZO_RMMIO_DatumVon, ZO_RMMIO_DatumBis) = 1 
        )
    )
    GO
    
    
    ALTER TABLE [dbo].[T_ZO_AP_Raum_AP_Ref_Mietobjekt] CHECK CONSTRAINT [CheckNoDuplicate_T_ZO_AP_Raum_AP_Ref_Mietobjekt]
    GO
    

    And here a test case:

    CREATE TABLE [dbo].[T_ZO_AP_Raum_AP_Ref_Mietobjekt](
        [ZO_RMMIO_UID] [uniqueidentifier] NOT NULL,  -- <== PRIMARY KEY
        [ZO_RMMIO_RM_UID] [uniqueidentifier] NOT NULL,
        [ZO_RMMIO_MIO_UID] [uniqueidentifier] NOT NULL,
        [ZO_RMMIO_DatumVon] [datetime] NOT NULL,
        [ZO_RMMIO_DatumBis] [datetime] NOT NULL,
        [ZO_RMMIO_Status] [int] NOT NULL,
        [ZO_RMMIO_Bemerkung] [text] NULL
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    
    GO
    
    
    
    /*
    DELETE FROM T_ZO_AP_Raum_AP_Ref_Mietobjekt 
    WHERE ZO_RMMIO_Status = 1 
    AND ZO_RMMIO_RM_UID = '2007B6F5-9010-4979-AB39-00057DA353C0' 
    AND ZO_RMMIO_MIO_UID = 'FFA177E9-971E-4500-805D-00116F708E7B'
    */
    
    
    INSERT INTO T_ZO_AP_Raum_AP_Ref_Mietobjekt
    (
         ZO_RMMIO_UID
        ,ZO_RMMIO_RM_UID
        ,ZO_RMMIO_MIO_UID
        ,ZO_RMMIO_DatumVon
        ,ZO_RMMIO_DatumBis
        ,ZO_RMMIO_Status
        ,ZO_RMMIO_Bemerkung
    )
    VALUES
    (
         NEWID() --<ZO_RMMIO_UID, uniqueidentifier,>
        ,'2007B6F5-9010-4979-AB39-00057DA353C0' --<ZO_RMMIO_RM_UID, uniqueidentifier,>
        ,'FFA177E9-971E-4500-805D-00116F708E7B' --<ZO_RMMIO_MIO_UID, uniqueidentifier,>
        ,'01.01.2012' --<ZO_RMMIO_DatumVon, datetime,>
        ,'31.12.2999' --<ZO_RMMIO_DatumBis, datetime,>
        ,1 --<ZO_RMMIO_Status, int,>
        ,NULL--<ZO_RMMIO_Bemerkung, text,>
    )
    GO
    
    
    
    INSERT INTO T_ZO_AP_Raum_AP_Ref_Mietobjekt
    (
         ZO_RMMIO_UID
        ,ZO_RMMIO_RM_UID
        ,ZO_RMMIO_MIO_UID
        ,ZO_RMMIO_DatumVon
        ,ZO_RMMIO_DatumBis
        ,ZO_RMMIO_Status
        ,ZO_RMMIO_Bemerkung
    )
    VALUES
    (
         NEWID() --<ZO_RMMIO_UID, uniqueidentifier,>
        ,'2007B6F5-9010-4979-AB39-00057DA353C0' --<ZO_RMMIO_RM_UID, uniqueidentifier,>
        ,'FFA177E9-971E-4500-805D-00116F708E7B' --<ZO_RMMIO_MIO_UID, uniqueidentifier,>
        ,'01.01.2012' --<ZO_RMMIO_DatumVon, datetime,>
        ,'31.12.2999' --<ZO_RMMIO_DatumBis, datetime,>
        ,1 --<ZO_RMMIO_Status, int,>
        ,NULL--<ZO_RMMIO_Bemerkung, text,>
    )
    GO
    
    SELECT [ZO_RMMIO_UID]
          ,[ZO_RMMIO_RM_UID]
          ,[ZO_RMMIO_MIO_UID]
          ,[ZO_RMMIO_DatumVon]
          ,[ZO_RMMIO_DatumBis]
          ,[ZO_RMMIO_Status]
          ,[ZO_RMMIO_Bemerkung]
      FROM [T_ZO_AP_Raum_AP_Ref_Mietobjekt]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add unique hit counter to my website using PHP. So this
I want to add a unique hit counter to my website using PHP. This
I want add my page some div and another tags.I wanna give unique class
This isn't that complicated of a question, but I can't wrap my head around
I want to build an app that user can add a frame (a png
Here's what I want to do: a unique HTML page that contains: a lot
I need to count and present distinct/unique values in a dataGridView. I want to
Can I do ssh-add against the identifyFile that stay forever, currently it prevail only
Question: I want code for: syntax highlighting (of programming languages) Language: C# or assembly
Similar Stack Overflow Question I want users to be able to search through my

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.