我在数据库中有一些表。该表包含 150 多个列,用于某些可能/可能不会被所有人使用的自定义字段操作。它不是创建静态的 150 个未使用的列字段,而是可以使用 add column
动态创建。
有人可以证明,哪个更好?何时使用动态,何时使用静态,为什么?
最佳答案
您可以寻找以下用于维护自定义字段的规范化,我已经在多个 web/window 应用程序中成功使用。
表格主表
FormId, Name: this will be identifier and will be passed from Form to DB query to identify itself
datatypeMaster 表
datatypeId, datatypeName
abobe 将定义您要支持的所有自定义字段,即客户可以创建自定义表单。
Form-Customer-CustomFieldMapping 表 说自定义字段主表 用于管理列定义和与客户的映射。
customerId, FormName/FormId, FieldName, customFieldId, datatypeId(FK:datatypeMaster), length ....
下一步是创建所有表来存储在 datatypeMaster 表中定义的所有数据类型,即每个数据类型一个表,一些我能想到的例子....
数字的自定义字段详细信息表,例如 CustomNumberFields
customFieldId FieldValue(numeric(max))
字符串的自定义字段详细信息表,例如 CustomTextFields
customFieldId FieldValue(varchar(max))
日期的自定义字段详细信息表说 CustomDateFields
customFieldId FieldValue(datetime)
等等....
现在,您可以将所有查询与 Inner Join 结合使用,以获取客户的所有自定义列。
获取自定义字段的示例查询
select MP.FieldName, CFN.FieldValue from Form-Customer-CustomFieldMapping MP
JOIN CustomNumberFields CFN on MP.CustomFieldId and CFN.CustomFieldId where formId=<<paramformId>> and customerId=<<paramcustomerId>>
UNION
select MP.FieldName, CFN.FieldValue from Form-Customer-CustomFieldMapping MP
JOIN CustomTextFields CFN on MP.CustomFieldId and CFN.CustomFieldId where formId=<<paramformId>> and customerId=<<paramcustomerId>>
UNION
select MP.FieldName, CFN.FieldValue from Form-Customer-CustomFieldMapping MP
JOIN CustomDateFields CFN on MP.CustomFieldId and CFN.CustomFieldId where formId=<<paramformId>> and customerId=<<paramcustomerId>>
关于mysql - 动态或静态列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27540111/