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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:29:18+00:00 2026-06-02T19:29:18+00:00

I am trying to create a unique constraint across multiple tables. I have found

  • 0

I am trying to create a unique constraint across multiple tables. I have found similar questions answered here but they don’t quite capture the spirit of what I am trying to do.

As an example, I have three tables, t_Analog, t_Discrete, t_Message

CREATE TABLE t_Analog(
    [AppName] [nvarchar](20) NOT NULL,
    [ItemName] [nvarchar](32) NOT NULL,
    [Value] [float] NOT NULL,
    CONSTRAINT [uc_t_Analog] UNIQUE(AppName, ItemName)
)

CREATE TABLE t_Discrete(
    [AppName] [nvarchar](20) NOT NULL,
    [ItemName] [nvarchar](32) NOT NULL,
    [Value] [bit] NOT NULL,
    CONSTRAINT [uc_t_Discrete] UNIQUE(AppName, ItemName)
)

CREATE TABLE t_Message(
    [AppName] [nvarchar](20) NOT NULL,
    [ItemName] [nvarchar](32) NOT NULL,
    [Value] [nvarchar](256) NOT NULL,
    CONSTRAINT [uc_t_Message] UNIQUE(AppName, ItemName)
)

My goal is to make AppName and ItemName unique across all 3 tables. For instance, an item name of Y in application X cannot exist in both analog and discrete tables.

Please note that this example is contrived, the actual data for each Type is different and large enough to make combining tables and adding a Type column pretty ugly.

If you have any suggestions on approaches to this, I would love to hear them!

—- BEGIN EDIT 2012-04-26 13:28 CST —-

Thank you all for your answers!

It seems there may be cause to modify the schema of this database, and that is fine.

Combining the tables into a single table is not really a viable option as there are on the order of 30 columns for each type that do not match (modifying these columns is, unfortunately, not an option). This could lead to large sections of columns not being used in each row, which seems like a bad idea.

Adding a 4th table, like John Sikora and others mention, may be an option but I would like to verify this first.

Modifying Schema to be:

CREATE TABLE t_AllItems(
    [id] [bigint] IDENTITY(1,1) NOT NULL,
    [itemType] [int] NOT NULL,
    [AppName] [nvarchar](20) NOT NULL,
    [ItemName] [nvarchar](32) NOT NULL,
    CONSTRAINT [pk_t_AllItems] PRIMARY KEY CLUSTERED ( [id] )
    CONSTRAINT [uc_t_AllItems] UNIQUE([id], [AppName], [ItemName])
) ON [PRIMARY]

CREATE TABLE t_Analog(
    [itemId] [bigint] NOT NULL,
    [Value] [float] NOT NULL,
    FOREIGN KEY (itemId) REFERENCES t_AllItems(id)
)

CREATE TABLE t_Discrete(
    [itemId] [bigint] NOT NULL,
    [Value] [bit] NOT NULL,
    FOREIGN KEY (itemId) REFERENCES t_AllItems(id)
)

CREATE TABLE t_Message(
    [itemId] [bigint] NOT NULL,
    [Value] [nvarchar](256) NOT NULL,
    FOREIGN KEY (itemId) REFERENCES t_AllItems(id)
)

I only have one question regarding this approach. Does this enforce uniqueness across the sub tables?

For instance, could there not exist an ‘Item’ that has ‘id’ 9 with tables t_Analog having ‘itemId’ of 9 with ‘value’ of 9.3 and, at the same time, t_Message have ‘itemId’ 9 with ‘Value’ of “foo”?

I may not fully understand this extra table approach but I am not against it.

Please correct me if I am wrong on this.

  • 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-02T19:29:20+00:00Added an answer on June 2, 2026 at 7:29 pm

    Add a 4th table specifically for these values you want to be unique then link these keys from this table into the others using a one to many relationship.
    For example you will have the unique table with an ID, AppName and ItemName to make up its 3 columns. Then have this table link to the others.

    For how to do this here is a good example
    Create a one to many relationship using SQL Server

    EDIT: This is what I would do but considering your server needs you can change what is needed:

    CREATE TABLE AllItems(
        [id] [int] IDENTITY(1,1) NOT NULL,
        [itemType] [int] NOT NULL,
        [AppName] [nvarchar](20) NOT NULL,
        [ItemName] [nvarchar](32) NOT NULL,
        CONSTRAINT [pk_AllItems] PRIMARY KEY CLUSTERED ( [id] ASC )
    ) ON [PRIMARY]
    
    CREATE TABLE Analog(
        [itemId] [int] NOT NULL,
        [Value] [float] NOT NULL
    )
    
    CREATE TABLE Discrete(
        [itemId] [int] NOT NULL,
        [Value] [bit] NOT NULL
    )
    
    CREATE TABLE Message(
        [itemId] [bigint] NOT NULL,
        [Value] [nvarchar](256) NOT NULL
    )
    
    ALTER TABLE [Analog] WITH CHECK 
        ADD CONSTRAINT [FK_Analog_AllItems] FOREIGN KEY([itemId])
    REFERENCES [AllItems] ([id])
    GO
    ALTER TABLE [Analog] CHECK CONSTRAINT [FK_Analog_AllItems]
    GO
    
    ALTER TABLE [Discrete] WITH CHECK 
        ADD CONSTRAINT [FK_Discrete_AllItems] FOREIGN KEY([itemId])
    REFERENCES [AllItems] ([id])
    GO
    ALTER TABLE [Discrete] CHECK CONSTRAINT [FK_Discrete_AllItems]
    GO
    
    ALTER TABLE [Message] WITH CHECK 
        ADD CONSTRAINT [FK_Message_AllItems] FOREIGN KEY([itemId])
    REFERENCES [AllItems] ([id])
    GO
    ALTER TABLE [Message] CHECK CONSTRAINT [FK_Message_AllItems]
    GO
    

    From what I can tell your syntax is fine, I simply changed it to this way simply because I am more familiar with it but either should work.

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

Sidebar

Related Questions

I am trying to create a unique constraint on two fields in a table.
I'm trying to create a unique constraint on my TranslationRequest model listed below. TranslationRequest
I'm using ormlite for Android and I'm trying to get a multiple column unique-constraint.
Ok so I am trying create a login script, here I am using PHP5
Trying to create a mapper for an Microsoft Office object to POCO's and found
I'm trying to create my first mysql stored procedure, but script doesn't work due
I'm trying to debug a problem with a create statement I have however the
I am trying to add records into two tables below, CREATE TABLE customer (Custno
I'm trying to create a unique column using Fluent Migrator. However, it doesn't work
I want to have a unique constraint on a column which I am going

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.