3

私は研究室の分析データベースを持っており、バストデータのレイアウトに取り組んでいます。「共有主キー」を使用するための同様の要件に基づくいくつかの提案を見てきましたが、外部キーだけの利点はわかりません。私は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 ではなく科学者なので、何が足りないか教えてください。

更新:共有主キー(私が理解しているように)は、親テーブル(分析)の各値が子テーブルの1つに1回表示されなければならないという追加の制約を持つ1対1の外部キーのようなものです。一度。

4

2 に答える 2

1

「共有主キー」を使用するための同様の要件に基づくいくつかの提案を見てきましたが、外部キーだけの利点はわかりません。

上記のコメントを理解した場合、利点は、親の各行が1つの子の行と一致し、1つの子のみが一致するという要件を最初に実装することだけです。これを行う 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 に答える