Design Pattern for Custom Fields in Relational Database

前端 未结 4 631
礼貌的吻别
礼貌的吻别 2020-12-08 17:16

I have assigned a task to create (relatively) simple reporting system. In these system, user will be shown a table result of report. A table has some fields and each field g

4条回答
  •  被撕碎了的回忆
    2020-12-08 17:55

    Well, you have a very good point about storing data in the correct data types.
    And i agree that this does pose a problem for user-defined data systems.

    One way of solveing this problem is by adding tables for each data type group (ints, floating points, strings, binary and dates, instead of keeping the value in the ReportFieldValue table. However, this will make your life harder since you will have to select and join multiple tables in order to get a single result.

    another way would be to add a data type column in the ReportFieldValue and create a user defined function to dynamically cast the data from strings to the appropriate data type (using the value in the data type column), so that you can use that for sorting, searching etc`.

    Sql server also has a data type called sql_variant that should support multiple types, and though I've never worked with it it's documentation seems promising.

提交回复
热议问题