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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:39:55+00:00 2026-05-21T20:39:55+00:00

In a database I want to be able to assign values of a varying

  • 0

In a database I want to be able to assign values of a varying type to variables in a variable table. So do I need a separate value table for each value type? If so, I am not sure how you would actually link Values to the right table and thus right value. How can I achieve what I am after?

Variables
    ID
    Name

VariableValuesLink
    ID
    IDVars
    IDVals

Values
    IDvals

ValuesValueLink
    ID
    IDvals
    IDval

ValuesInt
    IDval
    IntVal

ValuesFloat
    IDval
    FloatVal

ValuesDouble
    IDval
    DoubleVal

etc...
etc...
etc...
etc...

The aim is for me to get something like this:

Variable: 
    ezas123
Values:
    1 (Int)
    2.0 (Float)
    3.0 (Double)

Variable:
    QuickFox
Values:
    The (TinyText)
    Quick (TinyText)
    Brown (TinyText)
    Fox (TinyText)
    Jumped (TinyText)
    Over (TinyText)
    The (TinyText)
    Lazy (TinyText)
    Dog (TinyText)

Variable:
    Pangrams
Values:
    The Quick Brown Fox Jumped Over The Lazy Dog (Text)
    How quickly daft jumping zebras vex (Text)

So when I query the DB I would be able to get back this set of results (where the values are of a varying type)

Variable    Value
ezas123     1
ezas123     2.0
ezas123     3.0
QuickFox    The
QuickFox    Quick
QuickFox    Brown
QuickFox    Fox
QuickFox    Jumped
QuickFox    Over
QuickFox    The
QuickFox    Lazy
QuickFox    Dog
Pangrams    The Quick Brown Fox Jumped Over The Lazy Dog
Pangrams    How quickly daft jumping zebras vex
  • 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-05-21T20:39:55+00:00Added an answer on May 21, 2026 at 8:39 pm

    A couple of points:

    • In your example, the variable ezas123 has three values with different data types, meaning that the variable itself doesn’t actually have a defined data type. This will probably cause problems downstream and is likely to indicate that the data is pretty poorly defined. I’d look at including a restriction that all of the values for a given variable must have the same data type.

    • Hogan’s SQL query makes the point that whenever you list the values in the way you requested (i.e. across variables with different data types) you’ll be having to cast the result to varchar or similar to display it (since you can’t have values with different data types in the same output column). With that in mind, do you really need different data types, or would a varchar type work well for all the data you’re dealing with?

    If the different types are needed, I’d look at putting all of the different IntVal, FloatVal, DoubleVal, … columns into one table. Your table definitions could then look something like:

    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)
                                    )
    
    • The UNIQUE constraint on Variables(ID,DataType) will probably be required (DBMS?) to allow you to make it the subject of a FK;
    • The CHECK constraints ensure that only valid data types are being used and that the only the correct value columns can be populated;
    • Having DataType in Values as well as Variables means that a combination of FK and CHECK can be used to ensure that all values for a given variable have the same data type, rather than having to use triggers or application logic.

    A query against the tables then looks something like:

    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
    

    This could also be written (probably more correctly) with a CASE based on Variable.DataType being used to choose the relevant column.

    Having all of the values in one table means less tables/constraints/indexes in the database and means that extending the solution to hold new data types just means adding new columns to the Values table (and modifying the constraints) rather than adding new tables.

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

Sidebar

Related Questions

What I want to do here is to be able to assign the values
I want to be able to email a report daily from a glpi database
In cakephp I want to be able connect to a different database from one
I have a user database which I want the admin to be able view
I want to insert data into the postgresql database of OpenERP, I am able
Hi I need to be able to insert a random value between 1-1000 in
I have a table with 9 columns in database and I want to be
I have a database and want to lock it ... I know I can
I have a database i want to select data from data base of current
I have an sqlite database I want to distribute with my application. It's about

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.