Presentation is loading. Please wait.

Presentation is loading. Please wait.

Part3 Database Analysis and Design Techniques Chapter 06- Theory of Relational Database Lu Wei College of Software and Microelectronics Northwestern Polytechnical.

Similar presentations


Presentation on theme: "Part3 Database Analysis and Design Techniques Chapter 06- Theory of Relational Database Lu Wei College of Software and Microelectronics Northwestern Polytechnical."— Presentation transcript:

1 Part3 Database Analysis and Design Techniques Chapter 06- Theory of Relational Database Lu Wei College of Software and Microelectronics Northwestern Polytechnical University Database Systems

2 Lu Wei2 Outline Potential Problems in a Base Relation The concept of Normalization Functional Dependencies The process of Normalization First Normal Form (1NF) Second Normal Form (2NF) Third Normal Form (3NF) General Definitions of 2NF and 3NF

3 Lu Wei3 Outline Boyce-Codd Noraml Form (BCNF) Review of Normalization (1NF to BCNF) Fourth Noramal Form (4NF) Fifth Normal Form (5NF)

4 Lu Wei4 Outline Potential Problems in a Base Relation The concept of Normalization Functional Dependencies The process of Normalization First Normal Form (1NF) Second Normal Form (2NF) Third Normal Form (3NF) General Definitions of 2NF and 3NF

5 Lu Wei5 Potential Problems in a Base Relation

6 Lu Wei6 Potential Problems in a Base Relation In the figure above, The StaffBranch relation is an alternative format of the Staff and Branch relations. Problems –StaffBranch relation has redundant data; the details of a branch are repeated for every member of staff. In contrast, the branch information appears only once for each branch in the Branch relation and only the branch number (branchNo) is repeated in the Staff relation, to represent where each member of staff is located.

7 Lu Wei7 Potential Problems in a Base Relation Problems –Relations that contain redundant information may potentially suffer from update anomalies Types of update anomalies include –Insertion –Deletion –Modification

8 Lu Wei8 Potential Problems in a Base Relation tNotNameaddresscNocName T1WangbinA1C1N1 T1WangbinA1C2N2 T1WangbinA1C3N3 T2LinaA2C4N4 T2LinaA2C5N5 T3ZhaoleA3C6N6 Relation TeacherCourse Problems

9 Lu Wei9 Potential Problems in a Base Relation tNotNameaddress T1WangbinA1 T2LinaA2 T3ZhaoleA3 cNocNametName C1N1T1 C2N2T1 C3N3T1 C4N4T2 C5N2T2 C6N4T3 Relation Teacher Relation Course

10 Lu Wei10 Potential Problems in a Base Relation sNosNamedeptdeptAddrdeptMncNocNamescore s01LucyCSBuilding 1Zhaobinc01DB80 s01LucyCSBuilding 1Zhaobinc02OS90 s02JamISBuilding 2Qiansic01DB70 s02JamISBuilding 2Qiansic03DS85 s03SusanMathBuilding 3Sunwuc01DB75 s04TomCSBuilding 1Zhaobinc02OS85 Relation StuDeptCourse

11 Lu Wei11 Potential Problems in a Base Relation sNosNamedeptdeptAddrdeptMn s01LucyCSBuilding 1Zhaobin s02JamISBuilding 2Qiansi s03SusanMathBuilding 3Sunwu s04TomCSBuilding 1Zhaobin cNocName c01DB c02OS c03DS sNocNoscore s01c0180 s01c0290 s02c0170 s02c0385 s03c0175 s04c0285 Relation StuDept Relation Course Relation SC

12 Lu Wei12 Potential Problems in a Base Relation sNosNamedeptdeptAddrdeptMn s01LucyCSBuilding 1Zhaobin s02JamISBuilding 2Qiansi s03SusanMathBuilding 3Sunwu s04TomCSBuilding 1Zhaobin Are there problems in relation StuDept? Relation StuDept

13 Lu Wei13 Potential Problems in a Base Relation sNosNamedept s01LucyCS s02JamIS s03SusanMath s04TomCS Relation Student deptdeptAddrdeptMn CSBuilding 1Zhaobin ISBuilding 2Qiansi MathBuilding 3Sunwu Relation Department

14 Lu Wei14 Potential Problems in a Base Relation What problems do often exist in relations? Why are there problems in some relations but not in other relations? How to resolve these problems? How to measure the appropriateness of attributes groupings in relations? How to achieve a better relation schema? Normalization is helpful for resolving these problems.

15 Lu Wei15 Outline Potential Problems in a Base Relation The concept of Normalization Functional Dependencies The process of Normalization First Normal Form (1NF) Second Normal Form (2NF) Third Normal Form (3NF) General Definitions of 2NF and 3NF

16 Lu Wei16 The concept of Normalization Normalization –A technique for producing a set of relations with desirable properties, given the data requirements of an enterprise. –The process of normalization is a formal method that identifies relations based on their primary or candidate keys and the functional dependencies among their attributes.

17 Lu Wei17 The concept of Normalization –The process of normalization was first developed by E.F.Codd. –Normalization is often performed as a series of tests on a relation to determine whether it satisfies or violates the requirements of a given normal form. –1NF,2NF,3NF, BCNF –4NF,5NF

18 Lu Wei18 Outline Potential Problems in a Base Relation The concept of Normalization Functional Dependencies The process of Normalization First Normal Form (1NF) Second Normal Form (2NF) Third Normal Form (3NF) General Definitions of 2NF and 3NF

19 Lu Wei19 Functional Dependencies Data dependency –The relationships between attributes in a relation that descript the dependency or restriction on each other. The types of data dependency –Functional dependency(FD) –Multi-valued denpendency(MVD) –Join denpendency(JD)

20 Lu Wei20 Functional Dependencies Functional dependency(FD) –Describes the relationship between attributes in a relaiton. –If A and B are attributes of relation R, B is functionally dependent on A(denoted A  B), if each value of A is associated with exactly one value of B. A is determinant of B –Functional dependency is a property of the meaning or simantics of the attributes in a relation.

21 Lu Wei21 Functional Dependencies Functional dependency(FD) –If A  B and B  A, then we denote A  B Trivial functional dependency (平凡函数依赖) –If A  B and B is a subset of A(B  A) Nontrivial functional dependency –If A  B and B is not a subset of A(B  A)

22 Lu Wei22 Functional Dependencies Trivial dependencies are not very interesting in practical terms; we are normally more interested in nontrivial dependencies because they reprensent integrity constraints for the relation.

23 Lu Wei23 Functional Dependencies Identifying a functional dependency –When identifying functional dependencies between attributes in a relation it is important to distinguish clearly between the values held by an attribute at a given point in time and the set of all possible values that an attribute may hold at different times. –A functional dependency is a property of a relational schema(intension) and not a property of a particular instance of the schema(extension).

24 Lu Wei24 Functional Dependencies Generally we can identify –If the relationship between X and Y is one-to-one(1:1), then X  Y –If the relationship between X and Y is one-to- many(1:*), then Y  X –If the relationship between X and Y is many-to- many(*:*), then maybe there is no functional dependency between X and Y

25 Lu Wei25 R(U,D,dom,F) R-relation name U-attribute set D-domains of all attributes Dom-mapping between attributes and domains F-data dependecy between attributes D and dom is not important for relation schema, so we can simplify it as R(U,F) Functional Dependencies Formal description of a relation schema

26 Lu Wei26 Functional Dependencies Given relation schema R(U,F) U={sNo,sName,sDept,deptAddr,deptMn,cNo,cName,score} F={sNo  sName,sNo  sDept,sDept  deptAddr,deptMn,cN o  cName,(sNo,cNo)  score}

27 Lu Wei27 Inference Rules for Functional Dependencies The complete set of functional dependencies for a given relation can be very large. It is important to find an approach that can reduce that set to a manageable size.

28 Lu Wei28 Inference Rules for Functional Dependencies We want to identify a set of functional dependencies (represented as X) for a relation that is smaller than the complete set of functional dependencies(represented as Y) for that relation and has the property that every functional dependency in Y is implied by the functional dependencies in X.

29 Lu Wei29 Inference Rules for Functional Dependencies The set of all functional dependencies that are implied by a given set of functional dependencies X is called the closure of X, written X +. We need a set of rules to help compute X + from X. A set of inference rules, called Armstrong’s axioms, specifies how new functional dependencies can be inferred from given ones.

30 Lu Wei30 Inference Rules for Functional Dependencies Armstrong’s axioms are as follows: –Reflexivity: If B is a subset of A, then A  B –Augmentation: If A  B, then A,C  B,C –Transitivity: If A  B and B  C, then A  C. –Self-fetermination: A  A –Decomposition: If A  B,C, then A  B and A  C. –Union: If A  B and A  C, then A  B,C –Composition: If A  B and C  D, then A,C  B,D

31 Lu Wei31 Inference Rules for Functional Dependencies Minimal Sets of Functional Dependencies(X min ) –Every dependency in X has a single attribute on its right-hand side. –We cannot replace any dependency A  B in X with dependency C  B, where C is a proper subset of A, and still have a set of dependencies that is equivalent to X.(if X + = Y +, then X is equivalent to Y) –We cannot remove any dependency from X and still have a set of dependencies that is equivalent to X. X min is equivalent to X

32 Lu Wei32 Inference Rules for Functional Dependencies A minimal set of dependencies should be in a standard form with no redundancies. There can be several minimal covers for a set of functional dependencies. Try to identify the minimal set of a functional dependency using Armstrong axioms.

33 Lu Wei33 Inference Rules for Functional Dependencies Given relation schema R(U,F) U={sNo,sName,sDept,deptAddr,deptMn,cNo,cName,score} F={sNo  sName, sNo  sDept, sNo  deptMn, sDept  deptAddr, deptMn, cNo  cName,(sNo,cNo)  score} Is F a minimal cover? If F is not a minimal cover, try to give a F min. F’={sNo  sName, sNo  sDept, sDept  deptAddr, sDept  deptMn, cNo  cName, (sNo,cNo)  score}

34 Lu Wei34 Outline Potential Problems in a Base Relation The concept of Normalization Functional Dependencies The process of Normalization First Normal Form (1NF) Second Normal Form (2NF) Third Normal Form (3NF) Boyce-Codd Noraml Form (BCNF)

35 Lu Wei35 The process of Normalization Normalization is a Formal technique for analyzing a relation based on its primary key and the functional dependencies between the attributes of that relation. Normalization is often executed as a series of steps. Each step corresponds to a specific normal form, which has known properties.

36 Lu Wei36 The process of Normalization

37 Lu Wei37 Outline Potential Problems in a Base Relation The concept of Normalization Functional Dependencies The process of Normalization First Normal Form (1NF) Second Normal Form (2NF) Third Normal Form (3NF) General Definitions of 2NF and 3NF

38 Lu Wei38 First Normal Form (1NF) Definition of 1NF –A relation in which the intersection of each row and column contains one and only one value. For the relational data model, it is important to recognize that it is only 1NF that is critical in creating relations. All the subsequent normal forms are optional.

39 Lu Wei39 First Normal Form (1NF) UNF(Unnormalized form) to 1NF –Flattening the table ( 平修表 ): Entering appropriate data into the empty columns of rows containing the repeating data. –Placing the repeating data along with a copy of the original key attribute(s) into a separate relation. –Add columns to restrict each cell contains only one value.

40 Lu Wei40 First Normal Form (1NF) Examples sNosNameaddrphoneNo s01LucyNo.1 xxx 02988451234 02886654321 s02JamNo.2 xxx02988451234 s03SusanNo.3 xxx 02988451234 02168654321 Student

41 Lu Wei41 First Normal Form (1NF) sNosNameaddrphoneNo s01LucyNo.1 xxx02988451234 s01LucyNo.1 xxx02886654321 s02JamNo.2 xxx02988451234 s03SusanNo.3 xxx02988451234 s03SusanNo.3 xxx02168654321 Student

42 Lu Wei42 First Normal Form (1NF) sNosNameaddr s01LucyNo.1 xxx s02JamNo.2 xxx s03SusanNo.3 xxx sNophoneNo s0102988451234 s0102886654321 s0202988451234 s0302988451234 s0302168654321 StudentBaseInfo StudentPhone

43 Lu Wei43 First Normal Form (1NF) sNosNameaddrphoneNo1phoneNo2 s01LucyNo.1 xxx0288665432102988451234 s02JamNo.2 xxx02988451234 s03SusanNo.3 xxx0216865432102988451234 Student

44 Lu Wei44 First Normal Form (1NF) sNosNameaddrphoneNo s01LucyNo.1 xxx 02988451234 02886654321 s02JamNo.2 xxx02988451234 s03SusanNo.3 xxx 02988451234 02168654321 139918xxxxxx Add columns to restrict each cell contains only one value if we can identify the number of values may appear in a cell. Or adding columns cannot resolve the problem.

45 Lu Wei45 First Normal Form (1NF) clientNocNamepropertyNopAddressrentStartrentFinishrentownerNooName CR76John Kay PG4 PG16 6 LawSt, Glasgow 5 Nov Dr, Glasgow 1-Jul-00 1-Sep-01 31-Aug-01 1-sep-02 350 450 C040 C093 Tina Tony CR56 Aline Stewart PG4 PG36 PG16 6 LawSt, Glasgow 2 Man Rd, Glasgow 5 Nov Dr, Glasgow 1-Sep-99 10-Oct-00 1-Nov-02 10-Jun-00 1-Dec-01 10-Aug-03 350 375 450 C040 C093 Tina Tony ClientRental

46 Lu Wei46 First Normal Form (1NF) clientNocNamepropertyNopAddressrentStartrentFinishrentownerNooName CR76John KayPG4 6 LawSt, Glasgow 1-Jul-0031-Aug-01350C040Tina CR76John KayPG16 5 Nov Dr, Glasgow 1-Sep-011-sep-02450C093Tony CR56 Aline Stewart PG4 6 LawSt, Glasgow 1-Sep-9910-Jun-00350C040Tina CR56 Aline Stewart PG36 2 Man Rd, Glasgow 10-Oct-001-Dec-01375C093Tony CR56 Aline Stewart PG16 5 Nov Dr, Glasgow 1-Nov-0210-Aug-03450C093Tony ClientRental

47 Lu Wei47 First Normal Form (1NF) clientNopropertyNopAddressrentStartrentFinishrentownerNooName CR76PG4 6 LawSt, Glasgow 1-Jul-0031-Aug-01350C040Tina CR76PG16 5 Nov Dr, Glasgow 1-Sep-011-sep-02450C093Tony CR56PG4 6 LawSt, Glasgow 1-Sep-9910-Jun-00350C040Tina CR56 PG36 2 Man Rd, Glasgow 10-Oct-001-Dec-01375C093Tony CR56 PG16 5 Nov Dr, Glasgow 1-Nov-0210-Aug-03450C093Tony clientNocName CR76John Kay CR56Aline Stewart Client PropertyRentalOwner

48 Lu Wei48 Outline Potential Problems in a Base Relation The concept of Normalization Functional Dependencies The process of Normalization First Normal Form (1NF) Second Normal Form (2NF) Third Normal Form (3NF) General Definitions of 2NF and 3NF

49 Lu Wei49 Second Normal Form (2NF) Problems in a relation that is in 1NF sNocNoscorecredit s01c01802 s01c02903 s02c01702 s02c03851 s03c01752 s04c02853

50 Lu Wei50 Second Normal Form (2NF) 2NF is based on the concept of full functional dependency so we introduce full and partial functional dependency in first.

51 Lu Wei51 Second Normal Form (2NF) Full/Partial Functional Dependency –Indicates that if A and B are attributes of a relation, B is fully functionally dependent on A if B is functionally dependent on A, but not on any proper subset of A. –X  Y is a FD in R(U,F), if X’ X, X’  Y, then Y is fullly functionally dependent on X. Or Y is partially functionally dependent on X. –We can write Partial/Full functional dependency as X  Y P F

52 Lu Wei52 Second Normal Form (2NF) Definition of 2NF –A relation that is in 1NF and every non-primary-key attribute is fully functionally dependent on the primary key. –2NF applies to relations with composite keys, that is, relations with a primary key composed of two or more attributes. –A relation with a single attribute primary key is automatically in at least 2NF.

53 Lu Wei53 Second Normal Form (2NF) The normalization of 1NF relations to 2NF involves the removal of partial dependencies. –If a partial dependency exists, we remove the partially functionally dependent attribute(s) from the relation by placing them in a new relation along with a copy of their determinant.

54 Lu Wei54 Second Normal Form (2NF) Examples sNocNoscorecredit s01c01802 s01c02903 s02c01702 s02c03851 s03c01752 s04c02853 Given relation schema R(U,F) U={sNo,cNo,score,credit} F={ (sNo,cNo)  score, cNo  credit} R

55 Lu Wei55 Second Normal Form (2NF) sNocNoscore s01c0180 s01c0290 s02c0170 s02c0385 s03c0175 s04c0285 We can decompose R to R1 and R2 R1: U={sNo,cNo,score} F={(sNo,cNo)  score} R2: U={cNo,credit} F={ cNo  credit} cNocredit c012 c023 c031 R1R2

56 Lu Wei56 Second Normal Form (2NF) clientNocNamepropertyNopAddressrentStartrentFinishrentownerNooName ClientRental fd1Primary key Partial dependency fd2 fd3 Partial dependency

57 Lu Wei57 Second Normal Form (2NF) propertyNopAddressrentownerNooName clientNocName clientNopropertyNopAddressrentStartrentFinish Client Rental PropertyOwner clientNocNamepropertyNopAddressrentStartrentFinishrentownerNooName ClientRental

58 Lu Wei58 Outline Potential Problems in a Base Relation The concept of Normalization Functional Dependencies The process of Normalization First Normal Form (1NF) Second Normal Form (2NF) Third Normal Form (3NF) General Definitions of 2NF and 3NF

59 Lu Wei59 Third Normal Form (3NF) Problems in a relation that is in 2NF sNosNamedeptdeptAddrdeptMn s01LucyCSBuilding 1Zhaobin s02JamISBuilding 2Qiansi s03SusanMathBuilding 3Sunwu s04TomCSBuilding 1Zhaobin StuDept

60 Lu Wei60 Third Normal Form (3NF) 3NF is based on the concept of transitive dependency so we introduce transitive dependency in first.

61 Lu Wei61 Third Normal Form (3NF) Transitive Dependency –A condition where A, B, and C are attributes of a relation such that if A  B and B  C(B  C), then C is transitively dependent on A via B(provided that A is not functionally dependent on B or C).

62 Lu Wei62 Third Normal Form (3NF) Definition of 3NF –A relation that is in 1NF and 2NF and in which no non-primary-key attribute is transitively dependent on the primary key.

63 Lu Wei63 Third Normal Form (3NF) The normalization of 2NF relations to 3NF involves the removal of transitive dependencies. –If a transitive dependency exists, we remove the transitively dependent attribute(s) from the relation by placing them in a new relation along with a copy of their determinant.

64 Lu Wei64 Third Normal Form (3NF) Examples sNosNamedeptdeptAddrdeptMn s01LucyCSBuilding 1Zhaobin s02JamISBuilding 2Qiansi s03SusanMathBuilding 3Sunwu s04TomCSBuilding 1Zhaobin Relation StuDept

65 Lu Wei65 Third Normal Form (3NF) Relation StuDept(U,F) U={sNo,sName,dept,deptAddr,deptMn} F={ sNo  sName,sNo  dept,dept  deptAddr, dept  deptMn} We can decompose StuDept to Student and Department Student: U={sNo,sName,dept} F={sNo  sName, sNo  dept} Department: U={dept,deptAddr,deptMn} F={ dept  deptAddr, dept  deptMn}

66 Lu Wei66 Third Normal Form (3NF) sNosNamedept s01LucyCS s02JamIS s03SusanMath s04TomCS Relation Student deptdeptAddrdeptMn CSBuilding 1Zhaobin ISBuilding 2Qiansi MathBuilding 3Sunwu Relation Department

67 Lu Wei67 Third Normal Form (3NF) clientNocNamepropertyNopAddressrentStartrentFinishrentownerNooName ClientRental fd1Primary key Partial dependency fd2 fd3 Partial dependency fd4 Transitive dependency

68 Lu Wei68 Third Normal Form (3NF) propertyNopAddressrentownerNooName clientNocName clientNopropertyNopAddressrentStartrentFinish Client Rental PropertyOwner clientNocNamepropertyNopAddressrentStartrentFinishrentownerNooName ClientRental

69 Lu Wei69 Third Normal Form (3NF) propertyNopAddressrentownerNooName PropertyOwner propertyNopAddressrentownerNo PropertyForRent Owner ownerNooName

70 Lu Wei70 Third Normal Form (3NF) Review (1NF to 3NF)

71 Lu Wei71 Third Normal Form (3NF) sNosNamedeptdeptAddrdeptMncNocNamescore s01LucyCSBuilding 1Zhaobin C01 c02 DB OS 80 90 s02JamISBuilding 2Qiansi C01 c03 DB DS 70 85 s03SusanMathBuilding 3Sunwuc01DB75 s04TomCSBuilding 1Zhaobinc02OS85 StuDeptCourse

72 Lu Wei72 Third Normal Form (3NF) sNosNamedeptdeptAddrdeptMncNocNamescore s01LucyCSBuilding 1Zhaobinc01DB80 s01LucyCSBuilding 1Zhaobinc02OS90 s02JamISBuilding 2Qiansic01DB70 s02JamISBuilding 2Qiansic03DS85 s03SusanMathBuilding 3Sunwuc01DB75 s04TomCSBuilding 1Zhaobinc02OS85 StuDeptCourse

73 Lu Wei73 Third Normal Form (3NF) sNosNamedeptdeptAddrdeptMn s01LucyCSBuilding 1Zhaobin s02JamISBuilding 2Qiansi s03SusanMathBuilding 3Sunwu s04TomCSBuilding 1Zhaobin cNocName c01DB c02OS c03DS sNocNoscore s01c0180 s01c0290 s02c0170 s02c0385 s03c0175 s04c0285 StuDept Course SC

74 Lu Wei74 Third Normal Form (3NF) sNosNamedept s01LucyCS s02JamIS s03SusanMath s04TomCS Student deptdeptAddrdeptMn CSBuilding 1Zhaobin ISBuilding 2Qiansi MathBuilding 3Sunwu Department cNocName c01DB c02OS c03DS sNocNoscore s01c0180 s01c0290 s02c0170 s02c0385 s03c0175 s04c0285 Course SC

75 Lu Wei75 Third Normal Form (3NF) StuDept Course SC StuDeptCourse StudentDepartment 1NF 2NF 3NF

76 Lu Wei76 Third Normal Form (3NF) ClientRental PropertyOwner ClientRental PropertyForRent Owner 1NF 2NF 3NF

77 Lu Wei77 Outline Potential Problems in a Base Relation The concept of Normalization Functional Dependencies The process of Normalization First Normal Form (1NF) Second Normal Form (2NF) Third Normal Form (3NF) General Definitions of 2NF and 3NF

78 Lu Wei78 General Definitions of 2NF and 3NF The 2NF and 3NF we discuss above disallow partial or transitive dependencies on the primary key or relations to avoid the update anomalies. However, these definitions do not take into account other candidate keys of a relation, if any exist. We can present more general definitions for 2NF and 3NF that take into account candidate keys of a relation.

79 Lu Wei79 General Definitions of 2NF and 3NF For the general definitions, we define that a primary-key attribute is part of any candidate key. Definition of 2NF in general –A relation that is in first normal form and every non- primary-key attribute is fully functionally dependent on any candidate key. Definition of 3NF in general –A relation that is in first and second normal form and in which no non-primary-key attribute is transitively dependent on any candidate key.

80 Lu Wei80 General Definitions of 2NF and 3NF Using the general definition of 2NF and 3NF can make the process of normalization more complex, but it can identify hidden redundancy in relations that could be missed. In fact, it is often the case that whether we use the definitions based on primary keys or the general definitions of 2NF and 3NF, the decomposition of relations is the same.

81 Lu Wei81 Outline Boyce-Codd Noraml Form (BCNF) Review of Normalization (1NF to BCNF) Fourth Noramal Form (4NF) Fifth Normal Form (5NF)

82 Lu Wei82 Boyce-Codd Noraml Form (BCNF) Partial and transitive dependencies are the most important reasons for update anomalies. The 3NF disallow partial and transitive dependencies on the primary key so if all database relations is in 3NF we can think the result may be good. But 3NF does not remove the transitive dependencies of primary-key attributes on andidate keys and this can still cause redundancy.

83 Lu Wei83 Boyce-Codd Noraml Form (BCNF) Definnition of BCNF –A relation is in BCNF, if and only if, every determinant is a candidate key. –Or no attribute in a relation is transitively dependent on any candidate key. –The difference between 3NF and BCNF is that for a runctional dependency A  B, 3NF allows this dependency in a relation if B is a primary-key attribute and A is not a candidate key, whereas BCNF insists that for this dependency to remain in a relation.

84 Lu Wei84 Boyce-Codd Noraml Form (BCNF) Examples Relation SCP(sNo,cNo,place) sNo represent student number. cNo represent course number. place represent the student’s place in a course competition( 名次 ). No students are in the same place( 没有并列名次 ). F={ (sNo,cNo)  place,(cNo,place)  sNo} (sNo,cNo) and (cNo,place) are all candidate keys. SCP  3NF SCP  BCNF

85 Lu Wei85 Boyce-Codd Noraml Form (BCNF) Relation STC(sNo,tNo,cNo) sNo represent student number. cNo represent course number. tNo represent teacher number. Suppose every teacher teach one and only one course. Some teachers can teach the same course. F={ (sNo,cNo)  tNo,(sNo,tNo)  cNo, tNo  cNo} (sNo,cNo) and (sNo,tNo) are all candidate keys. SCP  3NF SCP  BCNF

86 Lu Wei86 Boyce-Codd Noraml Form (BCNF) We can decompose STC to SC and ST ST: U={sNo, cNo} SC: U={sNo, tNo} When we decompose STC to SC and ST, some functional dependencies may not be preserved.

87 Lu Wei87 Boyce-Codd Noraml Form (BCNF) clientNointerviewDateinterviewTimestaffNoroomNo CR7613-May-0210:30SG5G101 CR5613-May-0212:00SG5G101 CR7413-May-0212:00SG37G102 CR561-Jul-0210:30SG5G102 The members of staff involved in interviewing clients are allocated to a specific room on the day of interview. However, a room may be allocated to several members of staff as required throughout a working day. A client is only interviewed once on a given date, but may be requested to attend further interviews at later dates. ClientInterview

88 Lu Wei88 Boyce-Codd Noraml Form (BCNF) ClientInterview(clientNo, interviewDate, interviewTime, staffNo, roomNo) The ClientInterview relation has three candidate keys: (clientNo,interviewDate), (staffNo,interviewDate,interviewTime), (roomNo,interviewDate,interviewTime) The ClientInterview relation has the following function dependencies: fd1: clientNo,interviewDate  interviewTime, staffNo, roomNo fd2: staffNo,interviewDate,interviewTime  clientNo fd3: roomNo,interviewDate,interviewTime  staffNo, clientNo fd4: staffNo,interviewDate  roomNo ClientInterview  3NF ClientInterview  BCNF

89 Lu Wei89 Boyce-Codd Noraml Form (BCNF) To transform the ClientInterview relation to BCNF, we must remove the violating functional dependency by creating two new relations. Interview (clientNo,interviewDate,interviewTime,staffNo) StaffRoom (staffNo,interviewDate,roomNo)

90 Lu Wei90 Boyce-Codd Noraml Form (BCNF) Violation of BCNF is quite rare, since it may only happen under specific conditions. The potential to violate BCNF may occur in a relation that: –Contains two (or more) composite candidate keys; –The candidate keys overlap, that is have at least one attribute in common.

91 Lu Wei91 Outline Boyce-Codd Noraml Form (BCNF) Review of Normalization (1NF to BCNF) Fourth Noramal Form (4NF) Fifth Normal Form (5NF)

92 Lu Wei92 Review of Normalization (1NF to BCNF)

93 Lu Wei93 Review of Normalization (1NF to BCNF)

94 Lu Wei94 Review of Normalization (1NF to BCNF)

95 Lu Wei95 Review of Normalization (1NF to BCNF) StaffPropertyInspection (propertyNo, iDate, iTime, pAddress, comments, staffNo, sName, carReg) PropertyInspect (propertyNo, iDate, iTime, comments, staffNo, sName, carReg) Property (propertyNo, pAddress) Staff (staffNo, sName) PropertyInspect (propertyNo, iDate, iTime, comments, staffNo, carReg)

96 Lu Wei96 Review of Normalization (1NF to BCNF) Property (propertyNo, pAddress) Staff (staffNo, sName) Inspection (propertyNo, iDate, iTime, comments, staffNo) StaffCar (staffNo, iDate, carReg) PropertyInspect (propertyNo, iDate, iTime, comments, staffNo, carReg) fd1’: propertyNo, iDate  iTime, comments, staffNo, carReg fd4: staffNo, iDate  carReg fd5’: carReg, iDate, iTime  propertyNo, comments, staffNo fd6’: staffNo, iDate, iTime  propertyNo, comments

97 Lu Wei97 Review of Normalization (1NF to BCNF)

98 Lu Wei98 Review of Normalization (1NF to BCNF) We can decompose any relation that is not in BCNF into BCNF. However, it may not always be desirable to transform a relation into BCNF because some function dependencies may not be preserved when we perform the decomposition. When we decompose a relation stop at 3NF, all denpendencies can be preserved.

99 Lu Wei99 Review of Normalization (1NF to BCNF) When a relation is in BCNF, it has been decomposed completely in the category of functional dependency and anomalies of insertion and modification should have been removed.

100 Lu Wei100 Review of Normalization (1NF to BCNF) Exercises

101 Lu Wei101 Outline Boyce-Codd Noraml Form (BCNF) Review of Normalization (1NF to BCNF) Fourth Noramal Form (4NF) Fifth Normal Form (5NF)

102 Lu Wei102 Fourth Noramal Form (4NF) Although BCNF removes anomalies due to functional dependencies, another type of dependency called a multi-valued dependency (MVD) can also cause data redundancy. Possible existence of multi-valued dependencies in a relation is due to 1NF, which disallows an attribute in a tuple from having a set of values.

103 Lu Wei103 Fourth Noramal Form (4NF) Example branchNostaffNoownerNo B003S001O001 B003S002O001 B003S001O002 B003S002O002 BranchStaffOwner

104 Lu Wei104 Fourth Noramal Form (4NF) courseteacherreferences physics 李勇 王军 普通物理学 光学原理 物理习题集 math 李勇 张萍 数学分析 高等数学 CourseTeaRef

105 Lu Wei105 Fourth Noramal Form (4NF) courseteacherreferences physics 李勇普通物理学 physics 李勇光学原理 physics 李勇物理习题集 physics 王军普通物理学 physics 王军光学原理 physics 王军物理习题集 math 李勇数学分析 ……… CourseTeaRef

106 Lu Wei106 Fourth Noramal Form (4NF) Multi-valued dependency (MVD) –Dependency between attributes (for example, A, B, and C) in a relation, such that for each value of A there is a set of values for B and a set of values for C. However, the set of values for B and C are independent of each other. –MVD can be written as A →→B A →→C

107 Lu Wei107 Fourth Noramal Form (4NF) Trivial/nontrivial MVD –A MVD A →→B in relation R is defined as being trivial if (a), B is a subset of A or (b), A ∪ B=R. –A MVD is defined as being nontrivial if neither (a) nor (b) is satisfied.

108 Lu Wei108 Fourth Noramal Form (4NF) Definition of 4NF –A relation that is in BCNF and contains no nontrivial MVDs. The normalization of BCNF relations to 4NF involves the removal of the MVD from the relation by placing the attribute(s) in a new relation along with a copy of the determinant(s).

109 Lu Wei109 Fourth Noramal Form (4NF) branchNostaffNo B003S001 B003S002 BranchStaff branchNoownerNo B003O001 B003O002 BranchOwner

110 Lu Wei110 Outline Boyce-Codd Noraml Form (BCNF) Review of Normalization (1NF to BCNF) Fourth Noramal Form (4NF) Fifth Normal Form (5NF)

111 Lu Wei111 Fifth Normal Form (5NF) A relation decompose into two relations must have the lossless-join property, which ensures that no spurious tuples are generated when relations are reunited through a natural join operation. However, there are requirements to decompose a relation into more than two relations. Although rare, these cases are managed by join dependency and fifth normal form (5NF).

112 Lu Wei112 Fifth Normal Form (5NF) Lossless-join dependency –A property of decomposition, which ensures that no spurious( 谬误 ) tuples are generated when relations are reunited through a natural join operation. Join dependency (JD) –Decributes a type of dependency. For example, for a relation R with subsets of the attributes of R denoted as A, B,…,Z, a relation R satisfies a join dependency if, and only if, every legal value of R is equal to the join of its projections on A,B,…,Z.

113 Lu Wei113 Fifth Normal Form (5NF) Definition of 5NF –A relation that has no join dependency.

114 Lu Wei114 Fifth Normal Form (5NF)

115 Lu Wei115 Fifth Normal Form (5NF)

116 Lu Wei116 Summary In this chapter you should have learned: –The purpose of normalization –The problems associated with redundant data –The concept of functional dependency –How to undertake the process of normalization –How to identify the most commonly used forms: 1NF, 2NF, 3NF and BCNF –How to use the forms to normalize relation.


Download ppt "Part3 Database Analysis and Design Techniques Chapter 06- Theory of Relational Database Lu Wei College of Software and Microelectronics Northwestern Polytechnical."

Similar presentations


Ads by Google