我应该创建一个代理密钥吗?

时间:2018-07-31 15:30:33

标签: sql database

我正在从一个数据集中对材料进行各种描述符测试。有时,单个材料-描述符组合将有多个值。 (例如, SKU ABC-001 已通过结果 进行了易燃性 测试 WUXH005 WUXH008

按原样,所有自然主键候选都包括结果,这似乎是不合理的。可以保证材料描述符结果是唯一的。我应该添加整数代理键,还是像下面这样更有意义的代理?

{SKU}-{test shorthand}#{SKU-test index}

例如

ABC-001-Flam#1 
ABC-001-Flam#2

1 个答案:

答案 0 :(得分:1)

为什么不使用多个表?

Material -- contains all the info about the material
m_id -- The material id, primary key
...

Tests
t_id -- The test id, primary key
m_id -- Foreign key to the material table
... -- descriptions et whatnot

Results
r_id -- the specific result id, primary key
t_id -- Foreign key to the test table
... -- descriptions et whatnot

要选择数据,请使用几个联接:

select m.*, r.*
from materials m
inner join tests t
on t.m_id = m.m_id
inner join results r
on r.t_id = t.t_id