FD集合F的闭包是可以从F推断出的所有FD的集合F + 。它也称为功能依赖的完整集合。用F +表示。
算法:属性封闭集
Algorithm to compute a+, the closure of a under F
Result:= a;
while (changes to Result) do
for each B → Y in F do
Begin
if B ⊆ Result then Result := Result ∪ Y
End
利用属性封闭
- 要测试给定的属性是否为超级键/候选键。
- 我们可以检查FD X→Y是否成立。
- 找出F +的另一种方法。
- 测试属性是否为超键。
例子 :
令R =(A,B,C,G,H,I),FD的集合为F = {A→B,A→C,CG→H,CG→I,B→H}找出(AG) + 。
结果= {A,G}
第一循环:
A → B includes B in the Result as A⊆ Result (which is AG), so Result := Result ∪ B. Hence Result = {A, G, B} A → C causes Result to become ABCG. CG → H causes the Result to become ABCGH. CG → I causes the Result to become ABCGHI. B → H causes Result to become ABCGHI.
第二循环:
A → B causes the Result to be unchanged i.e. ABCGHI (B is already part of the Result). A → C causes Result to be unchanged. CG → H causes the Result to be unchanged. CG → I causes the Result to be unchanged. B → H causes Result to be unchanged.
在第二个循环结束时,结果不变,因此退出循环。
(AG) + = {A,B,C,G,H,I}
结论: AG是超级键,因为它可以确定所有其他属性。
- 检查是否存在Fd:
例子 :
令R =(A,B,C,G,H,I),FD的集合为F = {A→B,A→C,CG→H,CG→I,B→H}检查HB→是否持有
结果= {H,B}
第一循环:
In A → B as A ⊆ Result (which is HB) so nothing will be added. In A → C nothing added. CG → H (CG ⊆ Result (which is HB) so nothing added) CG → I nothing added. B → H nothing added.
在第一个循环结束时,结果不变,因此退出循环。
结论: HB→我没有坚持。
- 查找FD闭包(F + )的另一种方法。
例子 :
给定一个关系R(A,B,C,D,E,F)和一组FD F:{A→BC,E→CF,B→E,CD→EF}找出{A,B}的闭包+
步骤1:结果= AB
步骤2:第一个循环
Result = ABC for A → BC, A ⊆ Result so Result = Result ∪ BC. Result = ABC for E→ CF, E ∉ Result so Result = Result. Result = ABCE for B → E, B ⊆Result so Result = Result ∪ E. Result = ABCE for CD → EF, CD ∉ Result so Result = Result.
步骤2之前的结果为AB,步骤2之后的结果为ABCE,这是不同的,因此重复与步骤2相同的结果。
步骤3:第二个循环
Result = ABCE for A → BC, A ⊆ Result so Result = Result ∪ BC. Result = ABCEF for E → CF, E ⊆ Result so Result = Result ∪ CF. Result = ABCEF for B → E, B ⊆ Result so Result = Result ∪ E. Result = ABCEF for CD → EF, CD ∉ Result so Result = Result.
步骤3之前的结果为ABCE,步骤3之后的结果为ABCEF,这是不同的,因此重复与步骤3相同的结果。
步骤4:第三个循环
Result = ABCEF for A → BC, A ⊆ Result so Result = Result ∪ BC. Result = ABCEF for E → CF, E ⊆ Result so Result = Result ∪ CF. Result = ABCEF for B → E, B ⊆ Result so Result = Result ∪ E. Result = ABCEF for CD → EF, CD ∉ Result so Result = Result.
步骤4之前的结果是ABCEF,步骤3之后的结果是ABCEF,因此相同。
因此{A,B} +的闭包为{A,B,C,E,F}。
结论:对于每个属性/属性集,我们都可以找到闭包。结果为F + 。