The first drawback I see is that you need to grant your application service with ALTER rights.
This implies that your security model needs careful attention as the application will be able to not only add fields but to drop and rename them as well and create some tables (at least for MySQL).
其次,您如何区分每个用户所需的字段?或者用户A创建的字段是否可以被用户B访问?
请注意,列的基数也可能显著增长。如果每个用户添加了2个字段,我们已经谈论了200个字段。
我个人将采用两种办法之一或组合:
- Using a serialized field
我会在表格中添加一个文本字段,在其中存储序列化的字典或多个字典:
{
user_1: {key1: val1, key2, val2,...},
user_2: {key1: val1, key2, val2,...},
...
}
缺点是这些价值不容易搜索到。
- Using a multi-type name/value table
领域表:
user_id: int
field_name: varchar(100)
type: enum( INT , REAL , STRING )
价值观表:
field_id: int
row_id: int # the main table row id
int_value: int
float_value: float
text_value: text
当然,它需要一个联接并且实现起来有点复杂,但是更通用,如果正确索引,效率相当高。