Database Systems: Design, Implementation, and Management
Total Page:16
File Type:pdf, Size:1020Kb
Objectives Database Systems: Design, • In this chapter, you will learn: –What normalization (正規化) is and what role it Implementation, and plays in the database design process Management – About the normal forms 1NF, 2NF, 3NF, BCNF Eighth Edition – How normal forms can be transformed from lower normal forms to higher normal forms – That normalization and ER modeling are used concurrently to produce a good database design Chapter 5 – That some situations require denormalization (反 Normalization of Database Tables 正規化) to generate information efficiently Database Systems, 8th Edition 2 5.1 Database Tables and Database Tables and Normalization (continued) Normalization • Normalization (continued) • Normalization – 2NF is better than 1NF; 3NF is better than 2NF – Process for evaluating (評估) and correcting (修 正) table structures to minimize data – For most business database design purposes, redundancies 3NF is as high as needed in normalization • Reduces data anomalies (異常) – Highest level of normalization is not always most desirable – Works through a series of stages called normal forms: • Denormalization produces a lower normal form • First normal form (1NF) – Price paid (附出的代價) for increased • Second normal form (2NF) performance is greater data redundancy • Third normal form (3NF) Database Systems, 8th Edition 3 Database Systems, 8th Edition 4 5.2 The Need for Normalization • Example: company that manages building projects – Charges its clients by billing hours spent on each contract – Hourly billing rate is dependent on employee’s position – Periodically, report is generated that contains information such as displayed in Table 5.1 th 5 6 Database Systems, 8 Edition Table 5.1 A Sample Report Layout The Need for Normalization (continued) • Structure of data set in Figure 5.1 does not handle data very well • Table structure appears to work; report generated with ease • Report may yield (產生) different results depending on what data anomaly has occurred • Relational database environment suited to help designer avoid data integrity (完整性) problems • Check p. 155 Figure 5.1 Tabular representation of the report format 7 Database Systems, 8th Edition 8 5.3 The Normalization Process • Each table represents a single subject • No data item will be unnecessarily stored in more than one table • All attributes in a table are dependent on the primary key • Each table void of (排除) insertion, update, deletion anomalies Database Systems, 8th Edition 9 Database Systems, 8th Edition 10 The Normalization Process (continued) • Objective of normalization is to ensure all tables in at least 3NF • Higher forms not likely to be encountered in business environment • Normalization works one relation at a time • Progressively breaks table into new set of relations based on identified dependencies Table 5.3 Functional Dependency Concepts Database Systems, 8th Edition 11 Database Systems, 8th Edition 12 Conversion to First Normal Form Conversion to First Normal Form (continued) • Repeating group (重覆群組) • Step 1: Eliminate the Repeating Groups – Group of multiple entries of same type exist for – Eliminate nulls: each repeating group attribute any single key attribute occurrence contains an appropriate data value • Relational table must not contain repeating • Step 2: Identify the Primary Key groups – Must uniquely identify attribute value • Normalizing table structure will reduce data – New key must be composed redundancies • Step 3: Identify All Dependencies (相依性,功 • Normalization is three-step procedure 能相依性) – Dependencies depicted with a diagram Database Systems, 8th Edition 13 Database Systems, 8th Edition 14 Conversion to First Normal Form (continued) • Dependency diagram (相依圖): – Depicts all dependencies found within given table structure – Helpful in getting bird’s-eye view (全貌) of all relationships among table’s attributes – Makes it less likely that you will overlook (忽略) an important dependency Figure 5.2 A table in First Normal Form 15 Database Systems, 8th Edition 16 Conversion to First Normal Form (continued) • First normal form describes tabular format in which: – All key attributes are defined – There are no repeating groups in the table – All attributes are dependent on primary key • All relational tables satisfy 1NF requirements • Some tables contain partial dependencies (部份 相依性) – Dependencies based on part of the primary key – Should be used with caution Database Systems, 8th Edition 17 Database Systems, 8th Edition 18 Conversion to Second Normal Form • Step 1: Write Each Key Component on a Separate Line – Write each key component on separate line, then write original (composite) key on last line – Each component will become key in new table • Step 2: Assign Corresponding Dependent Attributes – Determine those attributes that are dependent on other attributes – At this point, most anomalies have been eliminated Database Systems, 8th Edition 19 Database Systems, 8th Edition 20 Conversion to Third Normal Form Conversion to Second Normal Form (continued) • Step 1: Identify Each New Determinant • Table is in second normal form (2NF) when: – For every transitive dependency, write its – It is in 1NF and determinant as PK for new table 決定因素,決定屬性 – It includes no partial dependencies: – Determinant ( ): any attribute whose value determines other values within a row • No attribute is dependent on only portion of primary key • Step 2: Identify the Dependent Attributes – Identify attributes dependent on each determinant identified in Step 1 • Identify dependency – Name table to reflect (反應) its contents and function Database Systems, 8th Edition 21 Database Systems, 8th Edition 22 Conversion to Third Normal Form (continued) • Step 3: Remove the Dependent Attributes from Transitive Dependencies (遞移相依性) – Eliminate all dependent attributes in transitive relationship(s) from each of the tables – Draw new dependency diagram to show all tables defined in Steps 1–3 – Check new tables as well as tables modified in Step 3 • Each table has determinant • No table contains inappropriate dependencies Database Systems, 8th Edition 23 Database Systems, 8th Edition 24 Conversion to Third Normal Form (continued) 5.4 Improving the Design • A table is in third normal form (3NF) when both • Table structures cleaned up to eliminate initial of the following are true: partial and transitive dependencies – It is in 2NF • Normalization cannot, by itself, be relied on to – It contains no transitive dependencies make good designs • It is valuable because its use helps eliminate data redundancies Database Systems, 8th Edition 25 Database Systems, 8th Edition 26 Current JOB_CHG_HOUR Improving the Design (continued) Surrogate Key • Issues to address in order to produce a good normalized set of tables: – Evaluate PK Assignments – Evaluate Naming Conventions – Refine Attribute Atomicity (單元性) – Identify New Attributes – Identify New Relationships – Refine Primary Keys as Required for Data Granularity (粒度,內容的詳細程度) – Maintain Historical Accuracy – Evaluate Using Derived Attributes Database Systems, 8th Edition 27 Database Systems, 8th Edition 28 Fig05-06b For Historical Accuracy For Historical Accuracy 29 Database Systems, 8th Edition 30 5.5 Surrogate Key Considerations 5.6 Higher-Level Normal Forms • When primary key is considered to be unsuitable, designers use surrogate keys (代理 • Tables in 3NF perform suitably in business 鍵) transactional databases • Data entries in Table 5.4 are inappropriate • Higher order normal forms useful on occasion because they duplicate existing records • Two special cases of 3NF: – No violation of entity or referential integrity – Boyce-Codd normal form (BCNF) – Fourth normal form (4NF) [跳過 5.6.2] Database Systems, 8th Edition 31 Database Systems, 8th Edition 32 The Boyce-Codd Normal Form (BCNF) The Boyce-Codd Normal Form (BCNF) (continued) • Every determinant in table is a candidate key • Most designers consider the BCNF as special – Has same characteristics as primary key, but for case of 3NF some reason, not chosen to be primary key • Table is in 3NF when it is in 2NF and there are no transitive dependencies • When table contains only one candidate key, the 3NF and the BCNF are equivalent • Table can be in 3NF and fail to meet BCNF – No partial dependencies, nor does it contain • BCNF can be violated only when table contains transitive dependencies more than one candidate key – A non-key attribute is the determinant of a key attribute Database Systems, 8th Edition 33 Database Systems, 8th Edition 34 Database Systems, 8th Edition 35 Database Systems, 8th Edition 36 5.7 Normalization and Database Design Normalization and Database Design • ER diagram • Normalization should be part of the design process – Identify relevant entities, their attributes, and their relationships • Make sure that proposed entities meet required – Identify additional entities and attributes normal form before table structures are created • Normalization procedures • Many real-world databases have been improperly – Focus on characteristics of specific (特定的) designed or burdened with anomalies entities • You may be asked to redesign and modify – Micro view of entities within ER diagram existing databases • Difficult to separate normalization process from ER modeling process Database Systems, 8th Edition 37 Database Systems, 8th Edition 38 Example about Project Management • Business rules – The company manages many projects – Each project requires the services of many employees – An employee may be assigned to several different projects – Some employees are not assigned