1 / 102

1 、将一个系统的数据组织成关系模式有多种形式,为什么不将数据随便组织成关系模式?

1 、将一个系统的数据组织成关系模式有多种形式,为什么不将数据随便组织成关系模式?. 2 、如何评价关系模式的好坏?. 3 、如何将不好的关系模式转化成好的关系模式?. pno. sno. 供应. 供应商. 零件. E.g. 设:建立某数据库需要如下信息: 供应商号( sno), 供应商所在城市( city), 邮编( zip), 零件号( pno) 零件名 (pname), 供应某种零件的数量( qty)。. 方案之一: 用一个关系模式创建该数据库: FIRST( sno ,city,zip, pno ,pname,qty ).

koto
Download Presentation

1 、将一个系统的数据组织成关系模式有多种形式,为什么不将数据随便组织成关系模式?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1、将一个系统的数据组织成关系模式有多种形式,为什么不将数据随便组织成关系模式?1、将一个系统的数据组织成关系模式有多种形式,为什么不将数据随便组织成关系模式? 2、如何评价关系模式的好坏? 3、如何将不好的关系模式转化成好的关系模式?

  2. pno sno 供应 供应商 零件 E.g.设:建立某数据库需要如下信息: 供应商号(sno),供应商所在城市(city), 邮编(zip),零件号(pno)零件名(pname),供应某种零件的数量(qty)。

  3. 方案之一:用一个关系模式创建该数据库: FIRST(sno,city,zip,pno,pname,qty ) 方案之二:用两个关系模式,创建该数据库: 模式一:S(sno,city,zip) 模式二:SP(sno,pno,pname,qty) 方案之三:用三个关系模式,创建该数据库: 模式一:S(sno,city,zip) 模式二:SP(sno,pno,qty) 模式: P(pno,pname)

  4. 第4章:关系数据库设计理论 Relational Database Design • 不合理的关系模式带来的问题 • 函数依赖和范式的定义 • 多值依赖及第4范式 • 函数依赖理论 • 关系的规范化

  5. FIRST(sno city zip pno pname qty) s1 c1 z1 p1 nut 100 s1 c1 z1 p2 bolt 200 s2 c2 z2 p1 nut 160 s2 c2 z2 p3 worm 320 §1.不好的关系模式带来的问题 FIRST(sno,city,zip,pno,pname,qty) 存在 问题? 1)数据冗余度太大. 2)删除异常. 3)插入异常.

  6. S(sno,city,zip) s1 c1 z1 s2 c2 z2 P(pno,pname) p1 nut p2 bolt p3 worm SP(sno,pno,qty) s1 p1 100 s1 p2 200 s2 p1 160 s2 p3 320

  7. 2)若XY, YX,则记作X←→Y。若Y不函数依赖X,则记作 X Y 。 §2.函数依赖及范式的定义Functional Dependencies §2.1.函数依赖:属性间的依赖关系 1.函数依赖:设R(U)是属性集U 上的关系模式,X,YU。如果对于R(U)的任意一个关系r,以及r 的任意两个元组t1,t2,不存在:t1[x]=t2[x],而t1[Y]t2[Y],则称X函数决定Y,或者说Y函数依赖于X。记为:XY。 注:1)“XY”必须对R(U)的任何一个关系实例都成立。

  8. Functional Dependencies (Cont.) A B • 4 1 5 3 7 • Example1: Consider r(A,B) with the following instance of r。 • On this instance, BA hold.

  9. Functional Dependencies (Cont.) • Example2.A=(sno,cno),B=(grade) • A  B ==(sno,cno) grade

  10. Functional Dependencies (Cont.) • 函数依赖是现实世界数据关联的形式之一。 • Example3. • FIRST(sno,city,zip,pno,qty) sno  pno ? sno city ? sno  zip ? zip city ? (sno, pno) qty ? (sno, pno) zip ?

  11. Functional Dependencies (Cont.) • Example3.设一个关系模式需保存如下属性 • R={ SN,SD,MN,CN,G }。其中: • SN:学生,SD:系,MN:系负责人,CN:课程,G:成绩。 • 已知:现实世界规定如下: • a. 一个学生只属于一个系; • b. 一个系只有一名负责人; • c. 每个学生学过一门课,最多获得一个成绩。 F = { SNSD,SDMN, (SN,CN)G }

  12. Functional Dependencies(Cont.) • K is a candidate key(候选码)for R if and only if • K R, and • for no   K,  R • Example4. R=(SN,CN,GRADE) • (SN,CN)GRADE • SNGRADE • CN)GRADE

  13. 1)完全函数依赖:在R(U)中,如果XY,且对 X的任何一个真子集X’,都有X’ Y,则称Y对X完全函数依赖,记作:X f Y . 2)部份函数依赖:1)的条件下,如果存在X’Y,则称Y对X部份函数依赖,记作X P Y. A Y A Y f p Y (A, B) Y (A, B) Y Y B Y B 2.完全函数依赖与部分函数依赖

  14. sno zip pno Qty • Example5. R=(sno, city, zip, pno, qty) • (sno,pno)  qty? • (sno,pno) f qty • (sno,pno) zip? • sno zip? • (sno,pno) p zip

  15. 3.传递函数依赖:在R(U)中,如果X→Y,Y X, (Y  X),Y→Z,则称Z传递函数依赖于X,记为:X t Z. • Example6. R =(sno,city,zip,pno,qty) • sno  zip • zipcity • sno t city zip city sno

  16. 4.非平凡的函数依赖:若X→Y,且Y  X,则称 X→Y是非平凡的函数依赖。 • Example7.R=(sno,city,zip,pno,qty) • (sno,pno)qty 非平凡的函数依赖 • (sno,pno)pno平凡的函数依赖

  17. §2.2.范式的定义Normal Form 1.范式:指关系数据库中所有关系要满足的属性间依赖的要求。 要求不同,范式不同,根据要求从低到高,有六个范式: 1NF,2NF,3NF,BCNF,4NF,5NF。 2.1NF(1 Normal Form):即关系模式的各属性域必须是“原子” 的,则该关系模式是第一范式的.

  18. Example8. R=(sno,city,zip,pno,qty) • KEY=(Sno,pno) • ∵ 存在(sno,pno) P city,zip • ∴ R2NF 3.2NF:设R 是一个关系模式。若R1NF,且每一个非主属性完全函数依赖于码,则 R2NF。 理解:若R2NF,则R中不存在非主属性对码的部份函数依赖!!!

  19. S(sno,city,zip),KEY=(sno) • SP(sno,pno,qty),KEY=(sno,pno) • S2NF, SP2NF。 • 因而,整个关系数据库模式也是2NF的。

  20. (Y  X) 4.3NF:如果关系模式R是2NF的,且不存在键码X、属性组Y以及非主属性Z(Z  Y),使得 X→Y ,Y→Z 成立,则 R∈3NF 理解:若R∈3NF,则R中不存在非主属性对码的传递函数依赖!!!

  21. zip sno city • Example9.S(sno,city,zip),KEY=(sno) • S2NF • S不是3NF。 • S(sno,zip),KEY=(sno) • ZC(city,zip)Key=(zip) • S,ZC均属于是3NF。

  22. Example10.关系模式STJ(S,T,J) • 其中:S:学号;T:教师;J:课程。 • 规定: • a.一个教师教一门课,一门课可以有不止一个教师讲授; • b. 一个学生选一门课,就确定一位教师。 • STJ是几范式? • STJ∈3NF。 问题:如何形式化的确定R的键码???

  23. 5.BCNF:设:R∈1NF,如果对于R的每一个函数依赖X→Y(Y  X)X是超码,则:R∈BCNF。 理解:BCNF具有以下性质: a. 所有非主属性完全依赖于每个键码。 b. 所有主属性完全依赖于不包含它的键码。 c. 没有任何(主/非主)属性完全函数依赖于任一非主属性(组)。

  24. Example11. • R = (A, B, C, G, H, I), key1=(AB),key2=(CG) • F = { AB B, ABCC, ABCG H, ABG I ,CG H,CG A,CG I, CG B } • No {A H , B I, G A,H AI,HI A} in F 在函数依赖范畴内,BCNF达到最高规范化程度!!!

  25. 存在主属性J部分依赖于 不包含它的码(S, T) • Example12.STJ(S,T,J)PPT23 • F = { T→J ,(S,J)→T } • 其中:key=(S,J)or key=(S,T) • STJ∈3NF • 但STJ不是BCNF? why?

  26. §3.多值依赖及第4范式 Multivalued Dependencies and 4NF §3.1 Multivalued Dependencies • Exmaple13.Consider a database classes(course, teacher, book)某学校一门课由多名教师讲授,他们都是使用 同一套参考书。具体数据非规范地表示如下:

  27. Multivalued Dependencies (Cont.)

  28. course teacher book database database database database database database operating systems operating systems operating systems operating systems Avi Avi Hank Hank Sudarshan Sudarshan Avi Avi Jim Jim DB Concepts Ullman DB Concepts Ullman DB Concepts Ullman OS Concepts Shaw OS Concepts Shaw Multivalued Dependencies (Cont.) Classes(course,teacher,book)

  29. Multivalued Dependencies (Cont.) • classes(course, teacher, book)的键码? • classes∈BCNF • 但仍然存在信息重复(冗余) • 插入异常Insertion anomalies • if Sara is a new teacher that can teach database, two tuples need to be inserted (database, Sara, DB Concepts) (database, Sara, Ullman) why?

  30. Multivalued Dependencies (Cont.) A z1 a1 z2 a2 B a3 zb b1 C b2 zc zs c1 c2 zd Z=teacher Y=book X=course

  31. Multivalued Dependencies (MVDs) • 设:R是属性集U上的一个关系模式,X,Y和Z都是U的子集,并且Z = U–X–Y.多值依赖X  Y成立,当且仅当对于关系模式R的任意一个关系r,r在X上的一个值对应唯一一组Y值,且这组Y值与Z的值无关。

  32. course teacher book database database database database database database operating systems operating systems operating systems operating systems Avi Hank Avi Hank Sudarshan Sudarshan Avi Avi Jim Jim DB Concepts Ullman Ullman DB Concepts DB Concepts Ullman OS Concepts Shaw OS Concepts Shaw

  33. Multivalued Dependencies (MVDs) • Let R be a relation schema and let   R and  R. The multivalued dependency  holds on R if in any legal relation r(R), for all pairs for tuples t1 and t2 in r such that t1[] = t2 [], there exist tuples t3 and t4 in r such that: t1[] = t2 [] = t3 []= t4[] t3[] = t1 [] t3[R – ] = t2[R – ] t4 [] = t2[] t4[R – ] = t1[R – ]

  34. MVD (Cont.) • Tabular representation of 

  35. 多值依赖的相关结论Theory of MVDs • From the definition of multivalued dependency, we can derive the following rules: • If   , then    That is, every functional dependency is also a multivalued dependency • 多值依赖具有对称 性: • 若X →→Y,则必有X→→Z(Z = U-X-Y)。 • 若X→→Y,当YX或Z=U-X-Y=Φ,则称为X→→Y为平凡的多值依赖。否则就是非平凡的MVDs

  36. course teacher database database database operating systems operating systems Avi Hank Sudarshan Avi Jim • course teacher is trivial !!!! 平凡的 ■ 多值依赖的传递性:若X →→Y,Y→→Z(Z = U-X-Y)则X →→Z。

  37. §3.2 Fourth Normal Form(4NF) • 设关系模式R  1NF。如果对于R的每个非平凡多值依赖XY,X都是超码,则R 4NF。 • 理解:4NF是对属性依赖中的非平凡多值依赖的一种约束!!!!,将每个非平凡的多值依赖左端限制为超码,实际上说明这个多值依赖是函数依赖!

  38. A relation schema R is in 4NF with respect to a set D of functional and multivalued dependencies if for all multivalued dependencies in D+ of the form , where  R and  R, at least one of the following hold: •  is trivial (i.e.,   or   = R) •  is a superkey for schema R • If a relation is in 4NF it is in BCNF

  39. course teacher book database database database database database database operating systems operating systems operating systems operating systems Avi Avi Hank Hank Sudarshan Sudarshan Avi Avi Jim Jim DB Concepts Ullman DB Concepts Ullman DB Concepts Ullman OS Concepts Shaw OS Concepts Shaw Classes(course,teacher,book)不是4NF

  40. course teacher database database database operating systems operating systems Avi Hank Sudarshan Avi Jim (Course,teacher) ∈4NF

  41. course book database database operating systems operating systems DB Concepts Ullman OS Concepts Shaw (course,book)∈4NF

  42. 2NF 3NF BCNF 4NF 范式间关系的小结 1NF 消去非主属性对码的部分函数依赖 消去非主属性对码的传递函数依赖 消去主属性对码的部分和传递函数依赖 消去非平凡且非函数依赖的多值依赖

  43. §4.函数依赖理论Functional Dependencies 4.1. 函数依赖的逻辑蕴涵 设:F是R给定的一组函数依赖集合。若能够用逻辑推理的方法导出其它的函数依赖(如X→Y),则称F逻辑蕴涵X→Y。记为 F  X→Y • Exmaple14.If F={AB , BC}, then we can infer that AC • F  AC

  44. 4.2.函数依赖的闭包 • F及由F所蕴含的所有的函数据依赖的集合,称为F的闭包.The set of all functional dependencies logically implied by F is the closure of F. • We denote the closure of F by F+. • We can find all ofF+by applying Armstrong’s Axioms: • if   , then   (自反reflexivity) • if  , then   (增广augmentation) • if  , and   , then    (transitivity)

  45. Four rules infered by Armstrong’s Axioms: • 合并:若X→Y,X→Z,则 X→YZ • 分解:若X→YZ,则 X→Y, X→Z • 伪增广:若X→Y,WZ,有XW→YZ • 伪传递:由X→Y,WY→Z,有WX→Z. • These rules are • sound(generate only functional dependencies that actually hold) and • complete (generate all functional dependencies that hold).

  46. Closure of a Set of Functional Dependencies • Example15. R = (A, B, C, G, H, I)F = { A B, A C,CG H, CG I, B H} • some members of F+ • A H • by transitivity from A B and B H • AG I • by augmenting A C with G, to get AG CG and then transitivity with CG I • CG HI • from CG H and CG I • AGH • From A C and CG H • ………………..

  47. A→Ф,AB→Ф,AC→Ф,ABC→Ф,B→Ф,C→Ф A→A, AB→A, AC→A, ABC→A, B→B, C→C A→B, AB→B, AC→B, ABC→B, B→C, F+= A→C, AB→C, AC→C, ABC→C, B→BC, A→AB,AB→AB,AC→AB,ABC→AB,BC→Ф, A→AC,AB→AC,AC→AC,ABC→AC,BC→B, A→BC,AB→BC,AC→BC,ABC→BC,BC→C, A→ABC,AB→ABC,AC→ABC,ABC→ABC,BC→BC, Closure of a Set of Functional Dependencies • Example16. R(A,B,C),F={ A→B,B→C}

  48. 4.3.属性的闭包Closure of Attribute Sets • F为属性集U上给定的一组函数依赖,a,U。 则 a+F称为属性集a关于函数依赖集F的闭包。 • a+F = {| a→是所有由F给定和导出的函数依赖集合 }。 • Example16. R(A,B,C),F={A→B,B→C} • A+F ={ ABC }

  49. Closure of Attribute Sets • Algorithm to compute a+ F, the closure of a under F: result := a; while (changes to result) do for each  in F do if  result then result := result  a+ F =result

  50. Example of Attribute Set Closure • Example17. R = (A, B, C, G, H, I) • F = {A B,A C, CG H,CG I, B H} • (AG)+ • PROCEDURE: 1. result = AG • Result = ABCG (A C and A  B) 3. result = ABCGH (CG H and CG  AGBC) 4. result = ABCGHI (CG I and CG  AGBCH)

More Related