我的PostgreSQL 9.2.3
数据库中有大约 50 种产品类型。
其中有些是可分的,有些则不是。
更复杂的是,"volume"
不同的产品应该有不同的精度。
Product type 1: allowed 8 decimal places.
Product type 2: only integer values.
Product type 3: 2 decimal places
Product type 4: 2 decimal places
Product type 5: 3 ---- || ------
Product type 6: 4 ---- || ------
Product type 7: 16 ---- || -----
Product type 8: integer values
Product type 9: 4 decimal places
Product type 10: 5 decimal places
Product type 11: 12 decimal places
还有"orders"
一个表,一个用于所有产品的表,其中几乎所有列都是相同的(用户、承包商、订单日期、运费等)。唯一的例外是"volume"
列。
我应该使用什么数据类型?
我考虑过"numeric"
为所有产品类型使用字段 + 存储每种产品类型的精度信息,并将“订单寄存器”视图中的值转换为所需的精度,但这不会导致数据一致性失败吗?
在此解决方案中,无法检查例如“产品类型 8”的整数值是否为整数,我仅将其显示为整数。:(
另一种方法是为每种数据类型创建一个列,例如volume_int、volume_num_2、volume_num_5 等,这真的很糟糕。:(
...另一种解决方案是使用表继承,如下所示:
orders <- volume is integer
orders_product_1 (inherits orders) <- volume is numeric(24,8)
orders_product_2 (inherits orders) <- volume is integer
orders_product_3 (inherits orders) <- volume is numeric(20,2)
...但是创建大约 20 个继承的表似乎是形式对内容的胜利...
最好的解决方案是什么?也许还有另一种解决问题的方法?