370 likes | 554 Views
Chapter 5: Relational Database. Logical Design. Learning Objectives. You should be able to … Transform any ERD into a series of relations complete with Primary keys Foreign keys Explain each of these concepts: Domain integrity Entity integrity Referential integrity
E N D
Chapter 5:Relational Database Logical Design
Learning Objectives You should be able to … • Transform any ERD into a series of relations complete with • Primary keys • Foreign keys • Explain each of these concepts: • Domain integrity • Entity integrity • Referential integrity • Describe 1NF, 2NF, 3NF • Redesign relations so that they are all 3NF
Relational Databases (Codd, 70) • Structural Aspect: viewed as Relations (Tables) (Physical structure hidden from user). • Integrity Constraints: Relations satisfy certain integrity constraints. • Manipulative Aspect: Set of Operators that derive a relation from relations.
Terminology (definitions) • Relation = Table • Tuples = Rows • Cardinality = Number of Rows of a Relation • Degree = Number of Columns of a Relation
More terminology • Primary Key: an attribute (or combination of attributes) that uniquely identifies each row in a relation • Composite Key: a primary key consisting of more than one attribute • Foreign Key: an attribute in a relation of a database that serves as the primary key of another relation in the same database • Referential integrity constraint: A rule that states that either each foreign key value must match a primary key value in the other relation or else the foreign key value must be null.
Relational Definitions Relation • Every relation has a unique name. • Every attribute value is atomic. • Every row is unique. • Attributes in tables have unique names. • The order of the columns is irrelevant. • The order of the rows is irrelevant. EMPLOYEE(EmployeeID, EmployeeName)
From ERD to Relation (Table) 1st step. EMPLOYEE (EmployeeID, EmployeeName) CERTIFICATE (Certificate_Number, Date_Completed, EmployeeID, Course_ID) COURSE (Course_ID, CourseTitle) What next ???
Pine ValleyM:N unresolved ORDER CUSTOMER PRODUCT
ORDER ORDER-LINE CUSTOMER PRODUCT Pine Valley ERDM:N resolved
Relational Schema for Pine Valley Furniture: One way of illustrating the Database Design
Another way to illustrate the Database Design CUSTOMER (CustomerID, CustomerName, CustomerAddress) ORDER (OrderID, OrderDate, CustomerID) ORDERLINE (OrderID, ProductID, Quantity) PRODUCT (ProductID, Description, Finish, Unit_Price, On_Hand)
Integrity Constraints • Domain Constraints • Allowable values for an attribute. • Entity Integrity • No primary key attribute may be null. • Operational Constraints • Business rules. • Referential Integrity • For example: Delete Rules • Restrict (default) • Cascade • Set-to-Null
Mapping a composite attribute (a) CUSTOMER entity type with composite attribute
Example of mapping a 1:M relationship Relationship between customers and orders
Example of mapping an M:N relationship Requests relationship (M:N)
Mapping a binary 1:1 relationship Binary 1:1 relationship
Mapping an associative entity with an identifier Associative entity (SHIPMENT)
Mapping a ternary relationship (a) Ternary relationship with associative entity
Well-Structured Relations AVOID/REDUCE • Insertion Anomaly • Deletion Anomaly • Modification Anomaly
E-R Diagrams -> Relations (Tables) • Binary 1:N Relationships • Two possible options a.Add the primary key attribute (or attributes) of the entity on the one side of the relationship as a foreign key in the relation on the right side The one side migrates to the many side. (default) b..Create another relation (3rd table) and include primary keys of all (both) relations as primary key of new relation • Binary or Unary 1:1 • Three possible options • Add the primary key of A as a foreign key of B • Add the primary key of B as a foreign key of A c.Create another relation (3rd table) and include primary keys of all (both) relations as primary key of new relation
E-R Diagrams to Relations • Represent Relationships (continued) • Binary M:N relationships • Create another table and include primary keys of all tables as primary key of new relation • Unary 1:N Relationships 2 options • Relationship between instances of a single entity type. Utilize a recursive foreign key • A foreign key in a table (relation) that references the primary key values of that same table (relation) • Create another table and include primary keys of both tables as primary key of new table (relation) • Unary M:N Relationships • Create a separate relation • Primary key of new relation is a composite of two attributes that both take their values from the same primary key
Transforming ER to Relations (Tables) • 1 : 1 (3 options) (FK either way, new table) • 1 : N (2 options) (FK on child, new table) • N : M (1 option) (new table) • Generalization 1 table for each sub-type + 1 for super-type 1 table for each sub-type 1 big table
Data Normalization • Primarily a tool to validate and improve a logical design so that it satisfies certain constraints that avoid unnecessary duplication of data. • The process of decomposing relations with anomalies to produce smaller, well-structured relations.
Functional Dependencies and Keys • Functional Dependency: The value of one attribute (the determinant) determines the value of another attribute. • Candidate Key: Each non-key field is functionally dependent on every candidate key.
First Normal Form • No multi-valued attributes. • Every attribute value is atomic. • Figure 5-2a (p.168). Is it in 1NF ?
Second Normal Form • 1NF and every non-key attribute is fully functionally dependent on the primary key. • Every non-key attribute must be defined by the entire key, not by only part of the key. • No partial functional dependencies. • Fig. 5-2b.(p.168) Is it in 2NF ?
Third Normal Form • 2NF and no transitive dependencies (functional dependency between non-key attributes.) • Fig. 5-24 (p.194)
Relation with transitive dependency (a) SALES relation with simple data
Removing a transitive dependency (a) Decomposing the SALES relation
Goals of Normalization • Reduce data redundancy • Improve “modify” activities: • insert, • update, • delete, • but not read • Price: degraded query, display, reporting
Steps in Normalization • 1NF: a table, without multivalued attributes • if not, then decompose • 2NF: 1NF and every non-key attribute is fully functionally dependent on the primary key • if not, then decompose • 3NF: 2NF and no transitive dependencies • if not, then decompose • GENERAL: • Each table should describe a single theme • Modification anomalies are minimized
Exercises • Go to http://coffee.kennesaw.edu and execute all the Animations under Database Design Hw # 3 (turn in your homework typed!)