A few points:
In your example, the ezas123 variable has three values โโwith different data types, which means that the variable itself does not actually have a specific data type. This is likely to cause problems in the opposite direction and is likely to indicate that the data is rather poorly defined. I would see, including the restriction, that all values โโfor a given variable must have the same data type.
The Hogan SQL query states that whenever you specify values โโin the query you requested (that is, between variables with different data types), you will have to return the result to varchar or display it in the same way (starting with you cannot have values โโwith different types data in one output column). With that in mind, do you really need different data types, or does the varchar type work well for all the data you deal with?
If different types are needed, I would look to put all IntVal, FloatVal, DoubleVal, ... tables into one table. Then your table definitions might look something like this:
Variables ID NOT NULL ,Name NOT NULL ,DataType NOT NULL CHECK (DataType IN ('INT','FLOAT','DOUBLE','TEXT')) ,CONSTRAINT PK_Variables PRIMARY KEY (ID) ,CONSTRAINT UQ_Variables_1 UNIQUE (Name) ,CONSTRAINT UQ_Variables_2 UNIQUE (ID,DataType) Values IDvals NOT NULL ,ID NOT NULL ,DataType NOT NULL CHECK (DataType IN ('INT','FLOAT','DOUBLE','TEXT')) ,IntVal NULL ,FloatVal NULL ,DoubleVal NULL ,TextVal NULL ,CONSTRAINT PK_Values PRIMARY KEY (IDvals) ,CONSTRAINT FK_Values_Variable FOREIGN KEY (ID,DataType) REFERENCES Variables(ID,DataType) ,CONSTRAINT CH_Values CHECK ( NOT(DataType <> 'INT' AND IntVal IS NOT NULL) AND NOT(DataType <> 'FLOAT' AND FloatVal IS NOT NULL) AND NOT(DataType <> 'DOUBLE' AND DoubleVal IS NOT NULL) AND NOT(DataType <> 'TEXT' AND TextVal IS NOT NULL) )
- You may need a UNIQUE constraint for variables (ID, DataType) (DBMS?) So that you can make it an FK object;
- CHECK constraints ensure that only valid data types are used and that only columns of the correct value can be populated;
- The presence of a DataType in the values, as well as Variables, means that the combination of FK and CHECK can be used to ensure that all values โโfor a given variable have the same data type, instead of using triggers or application logic.
Then the query for the tables looks something like this:
SELECT v.name as Variable, COALESCE(cast(a.IntVal as varchar(max)), cast(a.FloatVal as varchar(max)), cast(a.DoubleVal as varchar(max)), cast(a.TextVal as varchar(max)), '') as Value FROM Variables V JOIN Values a on V.ID = a.ID AND v.DataType = a.DataType
It can also be written (perhaps more correctly) using CASE based on Variable.DataType used to select the appropriate column.
Having all the values โโin one table means fewer tables / constraints / indexes in the database and means that expanding the solution to store new data types means only adding new columns to the value table (and changing constraints), and not adding new tables.