How to design a database for User Defined Fields?

前端 未结 14 627
花落未央
花落未央 2020-11-27 08:37

My requirements are:

  • Need to be able to dynamically add User-Defined fields of any data type
  • Need to be able to query UDFs quickly
  • Need to be
14条回答
  •  我在风中等你
    2020-11-27 09:31

    I would most probably create a table of the following structure:

    • varchar Name
    • varchar Type
    • decimal NumberValue
    • varchar StringValue
    • date DateValue

    The exact types of course depend on your needs (and of course on the dbms you are using). You could also use the NumberValue (decimal) field for int's and booleans. You may need other types as well.

    You need some link to the Master records which own the value. It's probably easiest and fastest to create a user fields table for each master table and add a simple foreign key. This way you can filter master records by user fields easily and quickly.

    You may want to have some kind of meta data information. So you end up with the following:

    Table UdfMetaData

    • int id
    • varchar Name
    • varchar Type

    Table MasterUdfValues

    • int Master_FK
    • int MetaData_FK
    • decimal NumberValue
    • varchar StringValue
    • date DateValue

    Whatever you do, I would not change the table structure dynamically. It is a maintenance nightmare. I would also not use XML structures, they are much too slow.

提交回复
热议问题