首页 文章

使用下拉列表支持设计用户定义字段的模式

提问于
浏览
1

我有一个应用程序,支持我的customer表的用户定义字段,我已经有这样的事情:

CustomFields table

    Id            Name                  DataType
    --------------------------------------------
    1             Hobbies               1
    2             Number of Siblings    3

上表显示了一个包含应用程序范围内使用的UDF的表 . DataType列中的值转换为如下所示:

1: string
2: DateTime
3: Integer

下表包含UDF的值

CustomFieldsValues table

CustomerId      FieldId     StringValue       NumericValue      DateValue
1234            1           Fishing           NULL              NULL           
1234            2           NULL              6                 NULL
8834            1           Golfing           NULL              NULL           
8834            2           NULL              2                 NULL

现在,我想介绍一个新的“DataType”

4: DropDownList

这基本上类似于 string 数据类型,除了不是渲染文本框,我会改为使用下拉列表,其值将由管理员添加 .

我现在能想到的是另一张 table

FieldDropDownList table

FieldId         DropDownItem
1               Fishing
1               Golf
1               Swimming  
2               Random value #1
2               Random value #2
3               Random value #3

并且数据类型为4的所有自定义字段的值都将保存在 CustomFieldsValues 表的 StringValue 列中

我有什么建议和考虑因素吗?

实现下拉列表UDF的最有效方法是什么?

1 回答

  • 0

    我有什么建议和考虑因素吗?

    是 . 重新开始,让DBMS完成工作!那个 DataType 专栏是一个警告铃,说错了 . DBMS提供类型,类型安全和类型转换 .

    将UDF分隔为 CustomIntFieldsCustomStrFieldsCustomDateFields . 如果需要laster,您可以使用 UNION 将它们表示为单个视图:

    create view CustomFields as 
    select 's' as type, FieldID, Name from CustomStrFields UNION
    select 'i' as type, FieldID, Name from CustomIntFields UNION
    select 'd' as type, FieldID, Name from CustomDateFields;
    

    对于初学者来说,这将让DBMS代表您确保日期有日期和整数有数字 .

    DropDowns 表变为

    create table DropDowns
      ( DropDownID int  -- indicating the widget
      , type char(1) 
      , FieldID int
      );
    

    引用三个UDF表的并集 .

    此设计允许添加字段而不会自动显示在下拉列表中,这可能不是您想要的 . 如果每个字段只应出现在一个特定的下拉列表中,则下拉列表ID可以添加到三个字段表中,并从视图中驱动所有内容 .

    什么是最有效的方式

    这些东西都是非常静态和小的 . 我很难相信效率会成为一个问题 . 但我确实认为程序员和客户满意度会因预期的方式使用DBMS而更高 . :-)

相关问题