版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Chapter 8: Relational Database DesignChapter 8: Relational Database DesignFeatures of Good Relational DesignAtomic Domains and First Normal FormDecomposition Using Functional DependenciesFunctional Dependency TheoryAlgorithms for Functional DependenciesDecomposition Using Multivalued Dependencies Mo
2、re Normal FormDatabase-Design ProcessModeling Temporal DataCombine Schemas?Suppose we combine instructor and department into inst_dept(No connection to relationship set inst_dept)Result is possible repetition of informationA Combined Schema Without RepetitionConsider combining relations sec_class(se
3、c_id, building, room_number) and section(course_id, sec_id, semester, year) into one relationsection(course_id, sec_id, semester, year, building, room_number)No repetition in this caseWhat About Smaller Schemas?Suppose we had started with inst_dept. How would we know to split up (decompose) it into
4、instructor and department?Write a rule “if there were a schema (dept_name, building, budget), then dept_name would be a candidate key”Denote as a functional dependency: dept_name building, budgetIn inst_dept, because dept_name is not a candidate key, the building and budget of a department may have
5、to be repeated. This indicates the need to decompose inst_deptNot all decompositions are good. Suppose we decompose employee(ID, name, street, city, salary) intoemployee1 (ID, name)employee2 (name, street, city, salary)The next slide shows how we lose information - we cannot reconstruct the original
6、 employee relation - and so, this is a lossy decomposition.A Lossy DecompositionExample of Lossless-Join Decomposition Lossless join decompositionDecomposition of R = (A, B, C)R1 = (A, B)R2 = (B, C)AB12AB12rB,C(r)A (r) B (r)AB12CABB12CABCABA,B(r)First Normal FormDomain is atomic if its elements are
7、considered to be indivisible unitsExamples of non-atomic domains:Set of names, composite attributesIdentification numbers like CS101 that can be broken up into partsA relational schema R is in first normal form if the domains of all attributes of R are atomicNon-atomic values complicate storage and
8、encourage redundant (repeated) storage of dataExample: Set of accounts stored with each customer, and set of owners stored with each accountWe assume all relations are in first normal form (and revisit this in Chapter 22: Object Based Databases)First Normal Form (Contd)Atomicity is actually a proper
9、ty of how the elements of the domain are used.Example: Strings would normally be considered indivisible Suppose that students are given roll numbers which are strings of the form CS0012 or EE1127If the first two characters are extracted to find the department, the domain of roll numbers is not atomi
10、c.Doing so is a bad idea: leads to encoding of information in application program rather than in the database.Goal Devise a Theory for the FollowingDecide whether a particular relation R is in “good” form.In the case that a relation R is not in “good” form, decompose it into a set of relations R1, R
11、2, ., Rn such that each relation is in good form the decomposition is a lossless-join decompositionOur theory is based on:functional dependenciesmultivalued dependenciesFunctional DependenciesConstraints on the set of legal relations.Require that the value for a certain set of attributes determines
12、uniquely the value for another set of attributes.A functional dependency is a generalization of the notion of a key.Functional Dependencies (Cont.)Let R be a relation schema R and RThe functional dependency holds on R if and only if for any legal relations r(R), whenever any two tuples t1 and t2 of
13、r agree on the attributes , they also agree on the attributes . That is, t1 = t2 t1 = t2 Example: Consider r(A,B ) with the following instance of r.On this instance, A B does NOT hold, but B A does hold. 41 53 7Functional Dependencies (Cont.)K is a superkey for relation schema R if and only if K RK
14、is a candidate key for R if and only if K R, andfor no K, RFunctional dependencies allow us to express constraints that cannot be expressed using superkeys. Consider the schema: inst_dept (ID, name, salary, dept_name, building, budget ).We expect these functional dependencies to hold:dept_name build
15、ing and ID buildingbut would not expect the following to hold: dept_name salaryUse of Functional DependenciesWe use functional dependencies to:test relations to see if they are legal under a given set of functional dependencies. If a relation r is legal under a set F of functional dependencies, we s
16、ay that r satisfies F.specify constraints on the set of legal relationsWe say that F holds on R if all legal relations on R satisfy the set of functional dependencies F.Note: A specific instance of a relation schema may satisfy a functional dependency even if the functional dependency does not hold
17、on all legal instances. For example, a specific instance of instructor may, by chance, satisfy name ID.Functional Dependencies (Cont.)A functional dependency is trivial if it is satisfied by all instances of a relationExample: ID, name ID name nameIn general, is trivial if Closure of a Set of Functi
18、onal DependenciesGiven a set F of functional dependencies, there are certain other functional dependencies that are logically implied by F.For example: If A B and B C, then we can infer that A CThe set of all functional dependencies logically implied by F is the closure of F.We denote the closure of
19、 F by F+.F+ is a superset of F.Boyce-Codd Normal Form is trivial (i.e., ) is a superkey for RA relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F+ of the form where R and R, at least one of the following holds:Example schema not in
20、 BCNF: instr_dept (ID, name, salary, dept_name, building, budget )because dept_name building, budgetholds on instr_dept, but dept_name is not a superkeyDecomposing a Schema into BCNFSuppose we have a schema R and a non-trivial dependency causes a violation of BCNF.We decompose R into:(U )( R - ( - )
21、 )In our example, = dept_name = building, budgetand inst_dept is replaced by (U ) = ( dept_name, building, budget )( R - ( - ) ) = ( ID, name, salary, dept_name )BCNF and Dependency PreservationConstraints, including functional dependencies, are costly to check in practice unless they pertain to onl
22、y one relationIf it is sufficient to test only those dependencies on each individual relation of a decomposition in order to ensure that all functional dependencies hold, then that decomposition is dependency preserving.Because it is not always possible to achieve both BCNF and dependency preservati
23、on, we consider a weaker normal form, known as third normal form.Third Normal FormA relation schema R is in third normal form (3NF) if for all: in F+at least one of the following holds: is trivial (i.e., ) is a superkey for REach attribute A in is contained in a candidate key for R. (NOTE: each attr
24、ibute may be in a different candidate key)If a relation is in BCNF it is in 3NF (since in BCNF one of the first two conditions above must hold).Third condition is a minimal relaxation of BCNF to ensure dependency preservation (will see why later).Goals of NormalizationLet R be a relation scheme with
25、 a set F of functional dependencies.Decide whether a relation scheme R is in “good” form.In the case that a relation scheme R is not in “good” form, decompose it into a set of relation scheme R1, R2, ., Rn such that each relation scheme is in good form the decomposition is a lossless-join decomposit
26、ionPreferably, the decomposition should be dependency preserving.How good is BCNF?There are database schemas in BCNF that do not seem to be sufficiently normalized Consider a relation inst_info (ID, child_name, phone)where an instructor may have more than one phone and can have multiple childrenIDch
27、ild_namephone99999999999999999999DavidDavidWilliamWillian512-555-1234512-555-4321512-555-1234512-555-4321inst_infoThere are no non-trivial functional dependencies and therefore the relation is in BCNF Insertion anomalies i.e., if we add a phone 981-992-3443 to 99999, we need to add two tuples(99999,
28、 David, 981-992-3443)(99999, William, 981-992-3443)How good is BCNF? (Cont.)Therefore, it is better to decompose inst_info into:This suggests the need for higher normal forms, such as Fourth Normal Form (4NF), which we shall see later.How good is BCNF? (Cont.)IDchild_name99999999999999999999DavidDav
29、idWilliamWillianinst_childIDphone99999999999999999999512-555-1234512-555-4321512-555-1234512-555-4321inst_phoneFunctional-Dependency TheoryWe now consider the formal theory that tells us which functional dependencies are implied logically by a given set of functional dependencies.We then develop alg
30、orithms to generate lossless decompositions into BCNF and 3NFWe then develop algorithms to test if a decomposition is dependency-preservingClosure of a Set of Functional DependenciesGiven a set F set of functional dependencies, there are certain other functional dependencies that are logically impli
31、ed by F.For e.g.: If A B and B C, then we can infer that A CThe set of all functional dependencies logically implied by F is the closure of F.We denote the closure of F by F+.Closure of a Set of Functional DependenciesWe can find F+, the closure of F, by repeatedly applying Armstrongs Axioms:if , th
32、en (reflexivity)if , then (augmentation)if , and , then (transitivity)These rules are sound (generate only functional dependencies that actually hold), and complete (generate all functional dependencies that hold).ExampleR = (A, B, C, G, H, I)F = A B A CCG HCG I B Hsome members of F+A H by transitiv
33、ity from A B and B HAG I by augmenting A C with G, to get AG CG and then transitivity with CG I CG HI by augmenting CG I to infer CG CGI, and augmenting of CG H to infer CGI HI, and then transitivityProcedure for Computing F+To compute the closure of a set of functional dependencies F: F + = Frepeat
34、for each functional dependency f in F+ apply reflexivity and augmentation rules on f add the resulting functional dependencies to F +for each pair of functional dependencies f1and f2 in F + if f1 and f2 can be combined using transitivity then add the resulting functional dependency to F +until F + d
35、oes not change any further NOTE: We shall see an alternative procedure for this task laterClosure of Functional Dependencies (Cont.)Additional rules:If holds and holds, then holds (union)If holds, then holds and holds (decomposition)If holds and holds, then holds (pseudotransitivity)The above rules
36、can be inferred from Armstrongs axioms.Closure of Attribute SetsGiven a set of attributes a, define the closure of a under F (denoted by a+) as the set of attributes that are functionally determined by a under F Algorithm to compute a+, the closure of a under F result := a;while (changes to result)
37、dofor each in F dobeginif result then result := result endExample of Attribute Set ClosureR = (A, B, C, G, H, I)F = A BA C CG HCG IB H(AG)+1.result = AG2.result = ABCG(A C and A B)3.result = ABCGH(CG H and CG AGBC)4.result = ABCGHI(CG I and CG AGBCH)Is AG a candidate key? Is AG a super key?Does AG R
38、? = Is (AG)+ RIs any subset of AG a superkey?Does A R? = Is (A)+ RDoes G R? = Is (G)+ RUses of Attribute ClosureThere are several uses of the attribute closure algorithm:Testing for superkey:To test if is a superkey, we compute +, and check if + contains all attributes of R.Testing functional depend
39、enciesTo check if a functional dependency holds (or, in other words, is in F+), just check if +. That is, we compute + by using attribute closure, and then check if it contains . Is a simple and cheap test, and very usefulComputing closure of FFor each R, we find the closure +, and for each S +, we
40、output a functional dependency S.Canonical CoverSets of functional dependencies may have redundant dependencies that can be inferred from the othersFor example: A C is redundant in: A B, B C, A CParts of a functional dependency may be redundantE.g.: on RHS: A B, B C, A CD can be simplified to A B, B
41、 C, A D E.g.: on LHS: A B, B C, AC D can be simplified to A B, B C, A D Intuitively, a canonical cover of F is a “minimal” set of functional dependencies equivalent to F, having no redundant dependencies or redundant parts of dependencies Extraneous AttributesConsider a set F of functional dependenc
42、ies and the functional dependency in F.Attribute A is extraneous in if A and F logically implies (F ) ( A) .Attribute A is extraneous in if A and the set of functional dependencies (F ) ( A) logically implies F.Note: implication in the opposite direction is trivial in each of the cases above, since
43、a “stronger” functional dependency always implies a weaker oneExample: Given F = A C, AB C B is extraneous in AB C because A C, AB C logically implies A C (I.e. the result of dropping B from AB C).Example: Given F = A C, AB CDC is extraneous in AB CD since AB C can be inferred even after deleting CT
44、esting if an Attribute is ExtraneousConsider a set F of functional dependencies and the functional dependency in F.To test if attribute A is extraneous in compute ( A)+ using the dependencies in F check that ( A)+ contains ; if it does, A is extraneous in To test if attribute A is extraneous in comp
45、ute + using only the dependencies in F = (F ) ( A), check that + contains A; if it does, A is extraneous in Canonical CoverA canonical cover for F is a set of dependencies Fc such that F logically implies all dependencies in Fc, and Fc logically implies all dependencies in F, andNo functional depend
46、ency in Fc contains an extraneous attribute, andEach left side of functional dependency in Fc is unique.To compute a canonical cover for F:repeatUse the union rule to replace any dependencies in F 1 1 and 1 2 with 1 1 2 Find a functional dependency with an extraneous attribute either in or in /* Not
47、e: test for extraneous attributes done using Fc, not F*/ If an extraneous attribute is found, delete it from until F does not changeNote: Union rule may become applicable after some extraneous attributes have been deleted, so it has to be re-appliedComputing a Canonical CoverR = (A, B, C)F = A BC B
48、C A BAB CCombine A BC and A B into A BCSet is now A BC, B C, AB CA is extraneous in AB CCheck if the result of deleting A from AB C is implied by the other dependenciesYes: in fact, B C is already present!Set is now A BC, B CC is extraneous in A BC Check if A C is logically implied by A B and the ot
49、her dependenciesYes: using transitivity on A B and B C. Can use attribute closure of A in more complex casesThe canonical cover is: A BB CLossless-join DecompositionFor the case of R = (R1, R2), we require that for all possible relations r on schema Rr = R1 (r ) R2 (r ) A decomposition of R into R1
50、and R2 is lossless join if at least one of the following dependencies is in F+:R1 R2 R1R1 R2 R2The above functional dependencies are a sufficient condition for lossless join decomposition; the dependencies are a necessary condition only if all constraints are functional dependenciesExampleR = (A, B,
51、 C)F = A B, B C)Can be decomposed in two different waysR1 = (A, B), R2 = (B, C)Lossless-join decomposition: R1 R2 = B and B BCDependency preservingR1 = (A, B), R2 = (A, C)Lossless-join decomposition: R1 R2 = A and A ABNot dependency preserving (cannot check B C without computing R1 R2)Dependency Pre
52、servation Let Fi be the set of dependencies F + that include only attributes in Ri. A decomposition is dependency preserving, if (F1 F2 Fn )+ = F +If it is not, then checking updates for violation of functional dependencies may require computing joins, which is expensive.Testing for Dependency Prese
53、rvationTo check if a dependency is preserved in a decomposition of R into R1, R2, , Rn we apply the following test (with attribute closure done with respect to F)result = while (changes to result) dofor each Ri in the decompositiont = (result Ri)+ Riresult = result tIf result contains all attributes
54、 in , then the functional dependency is preserved.We apply the test on all dependencies in F to check if a decomposition is dependency preservingThis procedure takes polynomial time, instead of the exponential time required to compute F+ and (F1 F2 Fn)+ ExampleR = (A, B, C )F = A B B CKey = AR is no
55、t in BCNFDecomposition R1 = (A, B), R2 = (B, C)R1 and R2 in BCNFLossless-join decompositionDependency preservingTesting for BCNFTo check if a non-trivial dependency causes a violation of BCNF1. compute + (the attribute closure of ), and 2. verify that it includes all attributes of R, that is, it is
56、a superkey of R.Simplified test: To check if a relation schema R is in BCNF, it suffices to check only the dependencies in the given set F for violation of BCNF, rather than checking all dependencies in F+.If none of the dependencies in F causes a violation of BCNF, then none of the dependencies in
57、F+ will cause a violation of BCNF either.However, simplified test using only F is incorrect when testing a relation in a decomposition of RConsider R = (A, B, C, D, E), with F = A B, BC DDecompose R into R1 = (A,B) and R2 = (A,C,D, E) Neither of the dependencies in F contain only attributes from (A,
58、C,D,E) so we might be mislead into thinking R2 satisfies BCNF. In fact, dependency AC D in F+ shows R2 is not in BCNF. Testing Decomposition for BCNFTo check if a relation Ri in a decomposition of R is in BCNF, Either test Ri for BCNF with respect to the restriction of F to Ri (that is, all FDs in F
59、+ that contain only attributes from Ri)or use the original set of dependencies F that hold on R, but with the following test:for every set of attributes Ri, check that + (the attribute closure of ) either includes no attribute of Ri- , or includes all attributes of Ri.If the condition is violated by
60、 some in F, the dependency (+ - ) Rican be shown to hold on Ri, and Ri violates BCNF.We use above dependency to decompose RiBCNF Decomposition Algorithmresult := R ;done := false;compute F +;while (not done) doif (there is a schema Ri in result that is not in BCNF)then beginlet be a nontrivial funct
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 呼吸衰竭患者的口腔护理与呼吸湿化管理
- 妇产科腹部手术后预防深静脉血栓护理
- 妇科肿瘤放化疗患者的护理信息化建设
- 2026年警用装备服务中心招聘试题含答案
- 临床护理核心技能详解
- hadoop日志分析课程设计
- 农村房屋灭虫方案范本
- erp应用课程设计后端
- 护理硕士就业前景分析
- 刚架拱桥施工方案(3篇)
- 2025广西玉林市福绵区成均镇人民政府招聘村级防贫监测信息员1人(公共基础知识)测试题带答案解析
- 护理工作风险隐患与识别
- 瓦斯隧道设备管理制度(3篇)
- AI在腹膜透析方案优化中的应用
- DB21-T 4324-2025 城市更新改造地下管道无损检测技术规程
- 三年(2023-2025)中考化学真题分类汇编(全国):专题22 实验探究题(解析版)
- 早读课声浪跳动课件
- 项目部管理人员节后复工安全教育
- 2026年益阳师范高等专科学校单招职业技能测试必刷测试卷及答案1套
- 2025年贵州遵义市中考英语考试真题带答案
- 电力五防知识教育培训课件
评论
0/150
提交评论