SQL Unique constraint across multiple tables

前端 未结 6 1381
执念已碎
执念已碎 2020-12-01 18:09

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

6条回答
  •  萌比男神i
    2020-12-01 18:55

    One thought might be to combine the three tables:

    CREATE TABLE t_Generic(
    [AppName] [nvarchar](20) NOT NULL,
    [ItemName] [nvarchar](32) NOT NULL,
    [Type] [nvarchar](32) NOT NULL,
    [AnalogValue] [Float] NULL,
    [DiscreteValue] [bit] NULL,
    [MessageValue] [nvarchar](256) NULL,
    CONSTRAINT [uc_t_Generic] UNIQUE(AppName, ItemName)
    )
    

    Your application logic would have to enforce that only one value was populated, and you could use a Type field to keep track of what type that record is.

提交回复
热议问题