简体   繁体   English

定期向sql表添加列的好方法

[英]good approach for periodically adding columns to sql table

I am using mysql, I have a user table which holds some basic information like name, surname, age, gender, etc. I want to provide a feature called profile, where user will put other information like work location, alternative contact details, etc. These new columns are not fixed, in future there can me more columns. 我正在使用mysql,我有一个用户表,其中包含一些基本信息,例如姓名,姓氏,年龄,性别等。我想提供一个称为个人资料的功能,用户可以在其中放置其他信息,例如工作地点,其他联系方式等这些新的列不是固定的,将来我可以有更多的列。 As of my knowledge there can be two approaches : 据我所知,可以有两种方法:

1) Add new columns every time when required, this will put Null entries to previous rows for newly added columns. 1)每次在需要时添加新列,这会将Null条目放置到新添加列的上一行中。

2) Add a new table which will contain all the columns ids to column names, and one more table which will contain key value like structure for each user who has the entry for that particular new column as follows : 2)添加一个新表,该表将包含所有列ID到列名,以及另一个表,该表将包含键值,如结构,对于每个具有该特定新列条目的用户,如下所示:

Table1 : userId | 表1:userId | firstName | 名字| age | 年龄| .... ....

Table2 : columnId | 表2:columnId | columnName | columnName | dataType 数据类型

Table3 : userId | 表3:userId | columnId | columnId | value

Which would be the good approach for my scenario? 哪种方法适合我的情况?

I am tempted to vote to close this question for some combination of being too broad and opinion-based. 我倾向于以过于广泛和基于观点的某种组合来投票结束这个问题。 However, it seems like a very valid question, just one with no right answer. 但是,这似乎是一个非常有效的问题,只是一个没有正确答案的问题。

There are at least two more approaches not in your list: 您的列表中至少还有两种方法:

  • Store the additional fields in a JSON or XML string within each record. 将其他字段存储在每个记录中的JSON或XML字符串中。
  • Add the columns in an auxiliary table, using the same primary key. 使用相同的主键将列添加到辅助表中。

In order to decide which approach is best, you need to understand your problem a bit more. 为了确定哪种方法最好,您需要更多地了解您的问题。 For instance: 例如:

  • How many profiles are there? 有多少个人资料?
  • Are there performance considerations in adding new columns? 添加新列时是否有性能方面的考虑?
  • Will indexes be needed on the new columns? 在新列上是否需要索引?
  • Are the types of the new columns always the same? 新列的类型是否总是相同?
  • Are foreign key references needed for the new columns? 新列是否需要外键引用?
  • Are other constraints needed or desirable? 是否需要其他约束?
  • Are all profiles intended to have all the columns? 是否所有配置文件都打算包含所有列?
  • Will the number of profile columns exceed the maximum number of columns for a table? 概要文件的列数是否会超过表的最大列数?

There is no one correct answer to this question. 这个问题没有一个正确的答案。 The EAV (entity-attribute-value data model) approach (your second approach) would typically have all the columns being the same type and preclude foreign key constraints (there are somewhat cumbersome ways around both these issues). EAV(实体属性值数据模型)方法(您的第二种方法)通常会将所有列都设为相同类型,并排除外键约束(在这两个问题上都有一些麻烦的方法)。 On the other hand, if the number of profile columns exceeds the maximum for your database, then you might have no choice but to use some form of EAV. 另一方面,如果概要文件列的数量超过了数据库的最大值,那么您可能别无选择,只能使用某种形式的EAV。

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

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