SQL 数据库存储不同类型的值(在单个字段中或模拟为单个字段)
在数据库中,我希望能够将不同类型的值分配给变量表中的变量。那么我是否需要为每种值类型准备一个单独的值表?如果是这样,我不确定您如何实际将值链接到正确的表,从而链接到正确的值。我怎样才能实现我所追求的目标?
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...
我的目标是得到这样的结果:
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)
因此,当我查询数据库时,我将能够取回这组结果(其中值的类型不同)
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
快点说,您可以通过这种方式简化您的设计——只需让每个值表指向变量表即可。不需要链接表。我能想到的链接表的唯一原因是如果您想要一种“更简单”的方式来跨所有变量类型进行序列。如果不需要,则使用下面的设计:
您的 sql 如何简单:
Quick point you can simplify your design in this way -- Just have each of the Values tables point back at the variables table. There is no need for the linking table. The only reason for a linking table I can think of is if you want an "easier" way to have sequence accross all variable types. If this is not needed then use this design below:
How your sql is easy:
几点:
在您的示例中,变量 ezas123 具有三个具有不同数据类型的值,这意味着变量本身实际上没有定义的数据类型。这可能会导致下游出现问题,并且可能表明数据的定义相当糟糕。我会考虑加入一个限制,即给定变量的所有值必须具有相同的数据类型。
Hogan 的 SQL 查询指出,每当您按照请求的方式列出值(即跨具有不同数据类型的变量)时,您都必须将结果转换为 varchar 或类似的形式来显示它(因为您可以' t 在同一输出列中具有不同数据类型的值)。考虑到这一点,您真的需要不同的数据类型,还是 varchar 类型适合您正在处理的所有数据?
如果需要不同的类型,我会考虑将所有不同的 IntVal、FloatVal、DoubleVal 等列放入一张表中。然后,您的表定义可能类似于:
针对表的查询看起来像这样:
这也可以用基于 Variable.DataType 的 CASE 来编写(可能更正确),用于选择相关列。
将所有值放在一张表中意味着数据库中的表/约束/索引更少,并且意味着扩展解决方案以保存新数据类型仅意味着向值表添加新列(并修改约束),而不是添加新表。
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:
A query against the tables then looks something like:
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.