300 likes | 669 Views
Database Design: Normalization. Reading: C&B, Chaps 13. In this lecture you will learn. Mathematical notions behind relational model Normalization. Introduction. Relations derived from ER model may be ‘faulty’ May cause data redundancy, and insert/delete/update anomalies
E N D
Database Design: Normalization Reading: C&B, Chaps 13
In this lecture you will learn • Mathematical notions behind relational model • Normalization Dept. of Computer Science, University of Aberdeen
Introduction • Relations derived from ER model may be ‘faulty’ • May cause data redundancy, and insert/delete/update anomalies • We use some mathematical (semantic?) properties of relations to • locate these faults and • fix them Dept. of Computer Science, University of Aberdeen
Mathematical notions behind relational model • Set – a collection of objects characterized by some defining property • E.g. a column in a database table such as last names of all staff • Cross Product of sets – one of the operations (X) on sets • E.g. consider two sets, set of all first names and set of all last names in the staff table • fName = {Mary, David} • lName = {Howe, Ford} • fNameXlName = {(Mary,Howe), (Mary,Ford), (David, Howe), (David, Ford)} • Relation – defined between two sets and is a subset of cross product between those two sets • E.g. FirstNameOf = {(Mary, Howe), (David, Ford)} Dept. of Computer Science, University of Aberdeen
Relational model • The name ‘relational model’ comes from this mathematical notion of relation • Where a relation is a set (collection) of tuples that have related objects such as first name and last name of the same person • E.g. (fName, lName) is a relation • We can have relations over any number of sets • E.g. (staffNo, fName, lName, position) • In general we can denote a relation as (A,B,C,D,….,Z) where A, B, C and Z are all its attribute sets Dept. of Computer Science, University of Aberdeen
Function • A function is a special kind of relation • In a relation (X,Y), if every value of X is associated with exactly one value of Y, then we say Y is a function of X. • E.g. the relation {(1,2),(2,4),(3,6),(4,8)} is a function, Y = 2*X for 0<X<5 Y X Only one arrow can start from any single value in X 1 2 3 4 2 4 6 8 Dept. of Computer Science, University of Aberdeen
Functional Dependency • If Y is a function of X • Y is dependent on X, • there is a relationship of functional dependency between Y and X • In databases, we work with relations in general form (A,B,C,D,……,Z) • Functional Dependency • Describes relationship between attributes in a relation. • If A and B are attributes of relation R, B is functionally dependent on A, if each value of A in R is associated with exactly one value of B in R. • We are interested in finding such functional dependencies among database relations Dept. of Computer Science, University of Aberdeen
Functional Dependency • Is a property of the meaning (or semantics) of the attributes in a relation. • Diagrammatic representation: • Determinant of a functional dependency refers to attribute or group of attributes on left-hand side of the arrow. • If the determinant can maintain the functional dependency with a minimum number of attributes, then we call it full functional dependency Dept. of Computer Science, University of Aberdeen
Data Redundancy • Major aim of relational database design is • to group attributes into relations to minimize data redundancy and • to reduce file storage space required by base relations. • Data redundancy is undesirable because of the following anomalies • ‘Insert’ anomalies • ‘Delete’ anomalies • ‘Update’ anomalies • We illustrate these anomalies with an example Dept. of Computer Science, University of Aberdeen
Data Redundancy Dept. of Computer Science, University of Aberdeen
Anomalies • Insert anomalies • Try to insert details for a new member of staff into StaffBranch • You also need to insert branch details that are consistent with existing details for the same branch • Hard to maintain data consistency with StaffBranch • Delete anomalies • Try to delete details for a member of staff from StaffBranch • You also loose branch details in that tuple (row) • Update anomalies • Try to update the value of one of the attributes of a branch • You also need to update that information in all the tuples about the same branch Dept. of Computer Science, University of Aberdeen
Decomposition of Relations • Staff and Branch relations which are obtained by decomposing StaffBranch do not suffer from these anomalies • Two important properties of decomposition • Lossless-join property enables us to find any instance of original relation from corresponding instances in the smaller relations. • Dependency preservation property enables us to enforce a constraint on original relation by enforcing some constraint on each of the smaller relations. Dept. of Computer Science, University of Aberdeen
The Process of Normalization • Formal technique for analyzing a relation based on its primary key and functional dependencies between its attributes. • Often executed as a series of steps. Each step corresponds to a specific normal form, which has known properties. • As normalization proceeds, relations become progressively more restricted (stronger) in format and also less vulnerable to update anomalies. • Given a relation, use the following cycle • Find out what normal form it is in • Transform the relation to the next higher form by decomposing it to form simpler relations • You may need to refine the relation further if decomposition resulted in undesirable properties Dept. of Computer Science, University of Aberdeen
Unnormalized Form (UNF) • A table that contains one or more repeating groups. • To create an unnormalized table: • transform data from information source (e.g. form) into table format with columns and rows. Example 1 – address and name fields are composite Dept. of Computer Science, University of Aberdeen
Another example of UNF Example 2 – repeating columns for each client & composite name field Dept. of Computer Science, University of Aberdeen
First Normal Form (1NF) • A relation in which intersection of each row and column contains one and only one value. • UNF to 1NF • Nominate an attribute or group of attributes to act as the key for the unnormalized table. • Identify repeating group(s) in unnormalized table which repeats for the key attribute(s). Dept. of Computer Science, University of Aberdeen
UNF to 1NF • Remove repeating group by: • entering appropriate data into the empty columns of rows containing repeating data (‘flattening’ the table). Or by • placing repeating data along with copy of the original key attribute(s) into a separate relation. Dept. of Computer Science, University of Aberdeen
Example 1 • Address field has been expressed in terms of constituent parts, such as street, city and postcode Name field has been expressed in terms of last name and first name Dept. of Computer Science, University of Aberdeen
Example 2 Table structure has been changed Data related to representative repeated Representative name expressed in terms of last name and first name Dept. of Computer Science, University of Aberdeen
Example 2 A new field ClientID introduced RepId and ClientID combination acts as the primary key Dept. of Computer Science, University of Aberdeen
Second Normal Form (2NF) • Based on concept of full functional dependency: • A and B are attributes of a relation R, • B is fully dependent on A (denoted A->B) if B is functionally dependent on A but not on any proper subset of A. • 2NF - A relation that is in 1NF and every non-primary-key attribute is fully functionally dependent on the primary key. Dept. of Computer Science, University of Aberdeen
1NF to 2NF • Identify primary key for the 1NF relation. • Identify functional dependencies in the relation. • If partial dependencies exist on the primary key remove them by placing them in a new relation along with copy of their determinant. Dept. of Computer Science, University of Aberdeen
Example 2NF Original table decomposed into smaller tables Each of them are in 2NF Dept. of Computer Science, University of Aberdeen
Third Normal Form (3NF) • Based on concept of transitive dependency: • A, B and C are attributes of a relation such that if A -> B and B -> C, • then C is transitively dependent on A through B. (Provided that A is not functionally dependent on B or C). • 3NF - A relation that is in 1NF and 2NF and in which no non-primary-key attribute is transitively dependent on the primary key. Dept. of Computer Science, University of Aberdeen
2NF to 3NF • Identify the primary key in the 2NF relation. • Identify functional dependencies in the relation. • If transitive dependencies exist on the primary key remove them by placing them in a new relation along with copy of their determinant. Dept. of Computer Science, University of Aberdeen
Normalization Flow UNF Remove repeating groups 1NF Remove partial dependencies 2NF Remove transitive dependencies 3NF More normalized forms Dept. of Computer Science, University of Aberdeen
Conclusion • Quality of the relations derived from ER models is unknown • Normalization is a systematic process of either assessing or converting these relations into progressively stricter normal forms • Advanced normal forms such as Boyce-Codd normal form (BNCF), 4NF and 5NF exist Dept. of Computer Science, University of Aberdeen