3

我有一个实验室分析数据库,我正在研究 bas 数据布局。我已经看到了一些基于使用“共享主键”的类似要求的建议,但我没有看到仅外键的优势。我正在使用 PostgreSQL:下面列出的表

Sample
___________
sample_id (PK)
sample_type (where in the process the sample came from)
sample_timestamp (when was the sample taken)


Analysis
___________
analysis_id (PK)
sample_id (FK references sample)
method (what analytical method was performed)
analysis_timestamp (when did the analysis take place)
analysis_notes

gc
____________
analysis_id (shared Primary key)
gc_concentration_meoh (methanol concentration)
gc_concentration_benzene (benzene concentration)

spectrophotometer
_____________
analysis_id
spectro_nm (wavelength used)
spectro_abs (absorbance measured)

我可以使用这种设计,或者我可以将分析表中的字段移动到 gc 和分光光度计表中,并在样品、gc 和分光光度计表之间使用外键。我看到的这种设计的唯一优势是,我只需要有关执行了多少或哪些类型的分析的信息,而不必加入实际结果。但是,确保共享主键之间的引用完整性以及管理额外连接和触发器(删除级联等)的附加规则似乎比次要优势更令人头疼。我不是 DBA,而是科学家,所以请让我知道我缺少什么。

更新:共享主键(据我所知)就像一对一的外键,具有附加约束,即父表(分析)中的每个值必须出现在其中一个子表中一次,并且不超过一次。

4

2 回答 2

1

我已经看到了一些基于使用“共享主键”的类似要求的建议,但我没有看到仅外键的优势。

如果我已经理解了您上面的评论,那么优点是只有第一个实现了父中的每一行与一个子中的一行匹配的要求,并且在一个子中。这是一种方法。

create table analytical_methods (
  method_id integer primary key,
  method_name varchar(25) not null unique
);
insert into analytical_methods values
(1, 'gc'),(2, 'spec'), (3, 'Atomic Absorption'), (4, 'pH probe');

create table analysis (
  analysis_id integer primary key,
  sample_id integer not null, --references samples, not shown
  method_id integer not null references analytical_methods (method_id),
  analysis_timestamp timestamp not null,
  analysis_notes varchar(255),
  -- This unique constraint lets the pair of columns be the target of
  -- foreign key constraints from other tables.
  unique (analysis_id, method_id)
);

-- The combination of a) the default value and the check() constraint on 
-- method_id, and b) the foreign key constraint on the paired columns 
-- analysis_id and method_id guarantee that rows in this table match a 
-- gc row in the analysis table. 
--
-- If all the child tables have similar constraints, a row in analysis 
-- can match a row in one and only one child table.
create table gc (
  analysis_id integer primary key,
  method_id integer not null 
    default 1 
    check (method_id = 1),
  foreign key (analysis_id, method_id) 
    references analysis (analysis_id, method_id),
  gc_concentration_meoh integer not null,
  gc_concentration_benzene integer not null
);
于 2012-12-01T00:38:44.253 回答
0

就我而言,这种超类型/子类型模型看起来不是最佳选择。相反,我应该将分析表中的字段移动到所有子表中,并建立一系列简单的外键关系。超类型/子类型模型的优点是当使用超类型的主键作为另一个表中的外键时。由于我不这样做,额外的复杂层不会增加任何东西。

于 2012-12-01T01:06:55.247 回答