如何存储可以是数值范围或数值的数据?

How to store data that can be either a numeric range or a numeric value?

在我的 table 中,我需要存储一个物理量,该物理量可以作为数值或数字区间给出。下面的 table 说明了这个想法:

------------------------------
Isotope_ID   |  Atomic_Weight   
------------------------------
     1       |    1.00784
     2       | [6.938, 6.997] 
    ...      |       ...

这个table是unacceptable因为字段Atomic_Weight包含不同类型的值。在这种情况下,最佳做法是什么?

Edit1:表示原子量信息的三种可能方式:

  1. 值+(不确定性),例如1.00784 (9)
  2. 间隔,例如[6.938, 6.997]
  3. 最大 stable 同位素的质量数,例如 38

这三个子类型不能存储在一个字段中,因为这会违反 1 规范化形式。这就是示例 table 为 unacceptable 的原因。 我将尝试更清楚地重申我的问题:在我的数据库中存储有关原子量的信息(可以在三种不同的子类型之一中给出)的可能方法是什么?

我会选择包含三列的 table:

Isotope_ID, 
Atomic_Weight_From, 
Atomic_Weight_To. 

在只有一个值的情况下,Atomic_Weight_FromAtomic_Weight_To将包含相同的值。
这样你就可以让你的 table 尽可能干净,以及需要处理它的代码。

either as a numeric value or as a numeric interval

在间隔的情况下,您可以将单个值 x 存储为 [x,x].

在此应用程序中,单个值并不是精确 值。它们仅代表一定精度的测量。即使是间隔端点也只代表一定精度的测量值。

This table is unacceptable because the field Atomic_Weight contains values of different types.

关系模型不关心 "type" 中的值是什么。如果没有适合您的 DBMS "type",那么您必须将理想的 table & 列编码为一个或多个 table(s) and/or 列。

您可以将它们编码为字符串。但是 DBMS 不知道如何优化涉及其构成值的查询以及多列编码。而且你必须不断地对它们进行解码和编码以对每个部分进行操作。

Weight_string (isotope, weight)
    // VALUES (1, '1.00874'), (2, '[6.938, 6.997]')

What is the best practice in such cases?

主要模式是为每个非原始子类型设置一个 table,并将子类型的值编码为一个或多个列。 (如果一个子类型依次有一个子类型,重复。)

Weight_single (isotope, weight_single)
    // VALUES (1, 1.00874)
Weight_interval(isotope, weight_min, weight_max)
    // VALUES (2, 6.938, 6.997)

另一种模式是根据需要在尽可能多的列中对每个值进行编码,无论它们是否被使用。

Weight_1_row_NULL_unused(isotope, single, interval_min, interval_max,
    // VALUES (1, 1.00874, NULL, NULL), (2, NULL, 6.938, 6.997)

Weight_1_row_type_tag(isotope, type_tag, if_single, if_finite_min, if_finite_max)
    // VALUES (1, 'single', 1.00874, 0, 0),(2, 'interval', 0, 6.938, 6.997)

重新搜索 SQL subtypes/subtyping tables.