sqldatabase-designrelational-databasetable-relationships

SQL Database Table relation based on multiple conditions


I am working on a database to store information that we want to use to standardise our workflow. The project is for an engineering company, and the information that I want to store is IO data from the hardware modules.

I have made a "Main" table that has all the "required" fields that need to be filled in, and I want to store the rest of the data in separate tables. So far so good.

Now we come to the challenge: In the "main" table I define if a signal is Analogue or Digital (And more, but let's keep it with this for now). The Analogue and Digital data have very different data sets, so I wanted to store them in their own tables, an Analogue values table and a Digital values Table. The main table will have a field stating if that signal is Analogue or Digital (SignalType). However, I cannot figure out how to make the relation between the tables based on the value of the SignalType field. I have an FK field called TagName, this is the unique programming name we are using, but making a 1:1 relation to both tables feels wrong, since the TagName key will only be present in one of the tables.

Basic Idea of what I attempt to do

I hope someone can help

As described already, I tried making the tables with a 1:1 relation to the TagName field. But this does not create what I wanted to happen. I would want something like a "conditional relation" between the tables per row, based on the value of the SignalType field. But I cannot figure out how to. I did look into cross-reference tables, but I cannot get my head around how I would implement that in this situation.


Solution

  • The solution in data modeling is called inheritance... The design of your data model does not show this...

    The data model for inherited with exclusivity between childs is as described on the picture below : enter image description here

    Then technically in SQL you must have :

    As an example in SQL :

    CREATE TABLE T_BASIC_IO_INFO_BIO
    (BIO_ID    INT IDENTITY PRIMARY KEY,
     ...)
    
    CREATE TABLE T_IO_INFO_ANALOG_IIA
    (BIO_ID    INT PRIMARY KEY REFERENCES T_BASIC_IO_INFO_BIO (BIO_ID),
     ...)
    
    CREATE TABLE T_IO_INFO_DIGITAL_IID
    (BIO_ID    INT PRIMARY KEY REFERENCES T_BASIC_IO_INFO_BIO (BIO_ID),
     ...)
    
    CREATE TRIGGER E_IU_IIA
    ON T_IO_INFO_ANALOG_IIA
    AFTER INSERT, UPDATE
    AS
    ...
    IF EXISTS(SELECT * 
              FROM   T_IO_INFO_ANALOG_IIA AS A 
                     JOIN NEW 
                        ON A.BIO_ID = NEW.BIO_ID 
                     JOIN T_IO_INFO_DIGITAL_IID AS D 
                        ON A.BIO_ID = D.BIO_ID)
       --> raise an error to rollback
    ...
    
    
    CREATE TRIGGER E_IU_IID
    ON T_IO_INFO_DIGITAL_IID
    AFTER INSERT, UPDATE
    AS
    ...
    IF EXISTS(SELECT * 
              FROM   T_IO_INFO_DIGITAL_IID AS D 
                     JOIN NEW 
                        ON D.BIO_ID = NEW.BIO_ID 
                     JOIN T_IO_INFO_ANALOG_IIA AS A 
                        ON A.BIO_ID = D.BIO_ID)
       --> raise an error to rollback
    ...