[精品]支行(支行名称_第1页
[精品]支行(支行名称_第2页
[精品]支行(支行名称_第3页
[精品]支行(支行名称_第4页
[精品]支行(支行名称_第5页
已阅读5页,还剩59页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Chapter 7: Relational Database Design,Chapter 7: Relational Database Design,Features of Good Relational DesignAtomic Domains and First Normal FormDecomposition Using Functional DependenciesFunctional Dependency TheoryAlgorithms for Functional DependenciesDatabase-Design Process,The Banking Schema,Branch = (branch_name, branch_city, assets)支行(支行名称,部门所在城市,资产)customer = (customer_id, customer_name, customer_street, customer_city)顾客(顾客id, 顾客名,顾客所在街道,顾客所在城市)loan = (loan_number, amount) 贷款(贷款号,金额)account = (account_number, balance) 账户(账户号,余额)employee = (employee_id. employee_name, telephone_number, start_date)雇员(雇员id,雇员姓名,电话号码,开始工作日期)dependent_name = (employee_id, dname)家属姓名(雇员id, 家属姓名)account_branch = (account_number, branch_name)loan_branch = (loan_number, branch_name),The Banking Schema,borrower = (customer_id, loan_number)depositor = (customer_id, account_number)cust_banker = (customer_id, employee_id, type)works_for = (worker_employee_id, manager_employee_id)payment = (loan_number, payment_number, payment_date, payment_amount)savings_account = (account_number, interest_rate)checking_account = (account_number, overdraft_amount),Combine Schemas?,Suppose we combine borrower and loan to get bor_loan = (customer_id, loan_number, amount )Result is possible repetition of information (L-100 in example below),A Combined Schema Without Repetition,Consider combining loan_branch and loanloan_amt_br = (loan_number, amount, branch_name)No repetition (as suggested by example below),What About Smaller Schemas?,Suppose we had started with bor_loan. How would we know to split up (decompose) it into borrower and loan?Write a rule “if there were a schema (loan_number, amount), then loan_number would be a candidate key”Denote as a functional dependency: loan_number amountIn bor_loan, because loan_number is not a candidate key, the amount of a loan may have to be repeated. This indicates the need to decompose bor_loan.Not all decompositions are good. Suppose we decompose employee intoemployee1 = (employee_id, employee_name)employee2 = (employee_name, telephone_number, start_date)The next slide shows how we lose information - we cannot reconstruct the original employee relation - and so, this is a lossy decomposition.,A Lossy Decomposition,关系模型的形式化定义,1、关系模型的五元组定义:R R 关系名,U 属性组,D 域,DOM 映射(属性与域之间的联系),F 数据依赖(属性与属性之间的联系)2、关系模型的三元组定义:R 当且仅当 U 上的一个关系 r 满足 F 时,r 称为关系模式R的一个关系。,数据依赖,1、定义 数据依赖是通过一个关系中属性间值的相等与否体现出来的数据间的相互关系,它是现实世界属性间相互联系的抽象。2、种类 函数依赖 数据依赖 多值依赖 连接依赖,Functional Dependencies,定义:设R(U)是属性集U上的关系模式。X,Y是U的子集。对于R(U)的任意一个可能的关系r,如果r中不存在两个元组,它们在X上的属性值相等,而在Y上的属性值不等,则称“X函数确定Y”或“Y函数依赖于X”,记作XY。 X称为这个函数依赖的决定属性集。Constraints on the set of legal relations.Require that the value for a certain set of attributes determines 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 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 537,Functional Dependencies (Cont.),K is a superkey for relation schema R if and only if K RK 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:bor_loan = (customer_id, loan_number, amount ).We expect this functional dependency to hold:loan_number amountbut would not expect the following to hold: amount customer_name,Use of Functional Dependencies,We 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 say 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 on all legal instances. For example, a specific instance of loan may, by chance, satisfy amount customer_name.,Functional Dependencies (Cont.),A functional dependency is trivial (平凡)if it is satisfied by all instances of a relationExample: customer_name, loan_number customer_name customer_name customer_nameIn general, is trivial if ,Closure of a Set of Functional Dependencies,Given 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(函数依赖F的闭包).We denote the closure of F by F+.F+ is a superset of F.,完全依赖 在R(U)中,如果XY,并且对于X的任何一个真子集X都有XY,则称Y对X完全依赖,记作X Y。 部分依赖 若XY但Y不完全依赖于X,则称Y对X部分函数依赖,记作X Y。 传递依赖 在R(U)中,如果XY,YZ,且YX,ZY,YX,则称Z对X传递依赖。记作X Y。,f,p,函数依赖的种类,传递,规范化,【目的】通过研究关系之间的等价问题,找出一些方法来指导我们定义数据库的逻辑结构,使其具有好的性能(冗余小、数据完整性好、操作方便)。,关系规范化定义通常将结构较简单的关系取代结构较复杂的关系的过程称为关系的规范化。,Normalization (范式),范式表示符合某一种级别的关系模式的集合。 R为第几范式写成RxNF。范式的概念是由Codd给出的,并在19711972年提出了1NF、2NF、3NF的概念,1974年Codd和Boyce又共同提出了BCNF的概念,1976年Fagin又提出了4NF,后来又有人提出了5NF。对于各种范式之间的联系是: 5NF 4NF BCNF 3NF 2NF 1NF。一个低一级范式的关系模式,通过模式分解可以转换为若干个高一级范式的关系模式的集合,这种过程就叫规范化。,Goals of Normalization,Let R be a relation scheme with 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 decompositionPreferably, the decomposition should be dependency preserving.,7.2 First Normal Form,Domain is atomic if its elements are 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 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 9),First Normal Form (Contd),Atomicity is actually a property 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 atomic.Doing so is a bad idea: leads to encoding of information in application program rather than in the database.,2NF,若R1NF,且每个非主属性完全函数依赖于关键字,则R2NF。,Example: R(S_NO,C_NO,GRADE,TNAME,TAGE,OFFICE); F = (S_NO,C_NO)GRADE, C_NOTNAME, TNAMETAGE, TNAMEOFFICE ;请问R2NF?,分解: SC(S_NO,C_NO,GRADE) CTO(C_NO,TNAME,TAGE,OFFICE) FSC = (S_NO,C_NO)GRADE FCTO = C_NOTNAME, TNAMETAGE, TNAMEOFFICE ;,3NF,若R2NF,且R的每个非主属性都不传递依赖于关键字,则R3NF。,Example: SC(S_NO,C_NO,GRADE) CTO(C_NO,TNAME,TAGE,OFFICE) FSC = (S_NO,C_NO)GRADE FCTO = C_NOTNAME, TNAMETAGE, TNAMEOFFICE 请问SC3NF?CTO3NF?,分解: SC(S_NO,C_NO,GRADE)FSC = (S_NO,C_NO)GRADE CT(C_NO,TNAME)FCT = C_NOTNAME TO(TNAME,TAGE,OFFICE)FTO=TNAMETAGE,TNAMEOFFICE,BCNF,R1NF,若XY且YX时X必含有关键字,则RBCNF。一个满足BCNF的关系框架R:所有非主属性对每一候选关键字都是完全依赖;所有主属性对每一不包含它的候选关键字也是完全依赖;没有任何属性完全依赖于非候选关键字的任何一组属性。,消除了主属性对候选关键字的部分依赖和传递依赖。,分析:此关系有两个候选关键字(S-NO,C-NO)(NAME,C-NO)而S-NONAME因此RBCNF,Boyce-Codd Normal Form, is trivial (i.e., ) is a superkey for R,A 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 BCNF:bor_loan = ( customer_id, loan_number, amount )because loan_number amount holds on bor_loan but loan_number is not a superkey,BCNF and Dependency Preservation,Constraints, including functional dependencies, are costly to check in practice unless they pertain to only 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 preservation, we consider a weaker normal form, known as third normal form.,Functional-Dependency Theory,We now consider the formal theory that tells us which functional dependencies are implied logically by a given set of functional dependencies.We then develop algorithms to generate lossless decompositions into 3NFWe then develop algorithms to test if a decomposition is dependency-preserving,Closure of a Set of Functional Dependencies,Given a set F set 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 F by F+.,公理F1(自反性, reflexivity ):若XY,则XY或XX。F2(增广性, augmentation ):若XY,则XZYZ或XZY。F3(传递性, transitivity) ):若XY,YZ,则XZ。推理规则F5(伪传性, pseudotransitivity):若XY,YWZ,则XWZ。F6(合成性, union):若XY,XZ,则XYZ。F7(分解性, decomposition):若XYZ,则XY,XZ。,FD公理及推理规则,Example,R = (A, B, C, G, H, I)F = A B A CCG HCG I B Hsome members of F+A H by transitivity 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 transitivity,Procedure for Computing F+,To compute the closure of a set of functional dependencies F: F + = Frepeatfor 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 + does not change any furtherNOTE: We shall see an alternative procedure for this task later,Closure of Attribute Sets,Given 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) dofor each in F dobeginif result then result := result end,Example of Attribute Set Closure,R = (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),Example of Attribute Set Closure,Is AG a candidate key? Is AG a super key?Does AG R? = Is (AG)+ RIs any subset of AG a superkey?Does A R? = Is (A)+ RDoes G R? = Is (G)+ R,Uses of Attribute Closure,There 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 dependenciesTo 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 output a functional dependency S.,Canonical Cover,Sets of functional dependencies may have redundant dependencies that can be inferred from the othersFor example: A C is redundant in: A B, B CParts of a functional dependency may be redundantE.g.: on RHS: A B, B C, A CD can be simplified to A B, B 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 Attributes(无关属性),Consider a set F of functional dependencies 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 a “stronger” functional dependency always implies a weaker one,Extraneous Attributes,Example: 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 C,Testing if an Attribute is Extraneous,Consider 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 compute + using only the dependencies in F = (F ) ( A), check that + contains A; if it does, A is extraneous in ,Canonical Cover,A 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 dependency 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 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-applied,Lossless-join Decomposition,For 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 and R2 is lossless join if and only if at least one of the following dependencies is in F+:R1 R2 R1R1 R2 R2,Example,R = (A, B, 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 Preservation,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.,3NF Decomposition Algorithm,Let Fc be a canonical cover for F;i := 0;for each functional dependency in Fc doif none of the schemas Rj, 1 j i contains then begini := i + 1;Ri := endif none of the schemas Rj, 1 j i contains a candidate key for Rthen begini := i + 1;Ri := any candidate key for R;end return (R1, R2, ., Ri),3NF Decomposition: An Example,Relation schema:cust_banker_branch = (customer_id, employee_id, branch_name, type )The functional dependencies for this relation schema are:customer_id, employee_id branch_name, typeemployee_id branch_namecustomer_id, branch_name employee_idWe first compute a canonical coverbranch_name is extraneous in the r.h.s. of the 1st dependencyNo other attribute is extraneous, so we get FC = customer_id, employee_id type employee_id branch_name customer_id, branch_name employee_id,3NF Decompsition Example (Cont.),The for loop generates following 3NF schema: (customer_id, employee_id, type ) (employee_id, branch_name) (customer_id, branch_name, employee_id)Observe that (customer_id, employee_id, type ) contains a candidate key of the original schema, so no further relation schema needs be addedIf the FDs were considered in a different order, with the 2nd one considered after the 3rd, (employee_id, branch_name) would not be included in the decomposition because it is a subset of (customer_id, branch_name, employee_id),3NF Decompsition Example (Cont.),Minor extension of the 3NF decomposition algorithm: at end of for loop, detect and delete schemas, such as (employee_id, branch_name), which are subsets of other schemasresult will not depend on the order in which FDs are consideredThe resultant simplified 3NF schema is: (customer_id, employee_id, type) (customer_id, branch_name, employee_id),Overall Database Design Process,We have assumed schema R is givenR could have been generated when converting E-R diagram to a set of tables.R could have been a single relation containing all attributes that are of interest (called universal relation).Normalization breaks R into smaller relations.R could have been th

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论