简体   繁体   English

SQl参考表-动态列

[英]SQl reference table - dynamic columns

I want to create a reference table. 我想创建一个参考表。 The number of rows and columns can grow (and become redundant) over time. 行和列的数量可以随着时间增长(并且变得多余)。 The column names may change over time as well. 列名称也可能随时间而变化。 Lets asume the value in Column 'Position' is 'IT Engineer', in row(n). 假设第(n)行的“位置”列中的值为“ IT工程师”。 For that specific row(n) further down in the column structure, lets say in column 'Behind Sheds', is a value I need to retrieve. 对于列结构中最下方的特定行(n),让我在“ Behind Sheds”列中说,这是我需要检索的值。 If the number of columns was fixed, it was no issue, but now the columns are added dynamically with T-SQL, which is fairly easy to do, even renaming these columns. 如果列数是固定的,那没问题,但是现在可以使用T-SQL动态添加列,这很容易做到,甚至重命名这些列。 My question is, is it good practice to add columns dynamicaly for the example above, or is there an better alternative, and how? 我的问题是,为上述示例动态添加列是一种好的做法,还是有更好的选择?如何?

Thanks. 谢谢。

可能最好使用带有字段的加法表之类的东西: person_id (主表的外键), person_param (例如“位置”), person_value (例如“ IT工程师”)?

I believe your issue is the right for implementing the EAV model. 我相信您的问题是实施EAV模型的正确选择。

Basically instead of having 20 columns, you have 3: 基本上没有20列,而是有3列:

  • Entity 实体
  • Attribute (otherwise known as column name) 属性(也称为列名)
  • Value (might actually be more values, of different types, and you just populate the right one) 价值(实际上可能是更多价值,不同类型的价值,而您只是填充了正确的价值)

That way, for 10 entities in the database, having 20 columns each, you populate 200 rows of data in an EAV table. 这样,对于数据库中的10个实体(每个实体具有20列),您可以在EAV表中填充200行数据。 It has less performance, it's harder to query, but it does allow flexibility, so you can make each entity have different sets of attributes even. 它具有较低的性能,较难查询,但确实具有灵活性,因此您甚至可以使每个实体具有不同的属性集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM