CSAI 202 Database Systems Lecture Notes PDF
Document Details
Uploaded by DashingSocialRealism9938
Tags
Summary
This document provides lecture notes on database systems, focusing on relational database design, functional dependencies, and normalization techniques. It explains concepts like informal design guidelines and formal design concepts, along with examples and exercises to illustrate the principles for relational database design.
Full Transcript
CSAI 202 Outline Informal Design Guidelines for Relational Databases – Semantics of the Relation Attributes – Redundant Information in Tuples and Update Anomalies – Null Values in Tuples – Spurious Tuples Functional Dependencies (FDs) – Definition of FD – Infere...
CSAI 202 Outline Informal Design Guidelines for Relational Databases – Semantics of the Relation Attributes – Redundant Information in Tuples and Update Anomalies – Null Values in Tuples – Spurious Tuples Functional Dependencies (FDs) – Definition of FD – Inference Rules for FDs Normal Forms Based on Primary Keys – Normalization of Relations – Practical Use of Normal Forms – Definitions of Keys and Attributes Participating in Keys – 1NF, 2NF, 3NF 12/7/2023 Introduction to database systems 2 Introduction Most practical relational design projects take one of the following two approaches: Perform a conceptual schema design using a conceptual model (ER) and map the conceptual design into a set of relations Design the relations based on external knowledge derived from an existing implementation of files or reports 12/7/2023 Introduction to database systems 3 Informal Design Guidelines for Relational Databases Relational database design: The grouping of attributes to form "good" relation schemas Two levels of relation schemas: The logical "user view" level The storage "base relation" level Design is concerned mainly with base relations Criteria for "good" base relations: Discuss informal guidelines for good relational design Discuss formal concepts of functional dependencies and normal forms 1NF 2NF 3NF 12/7/2023 Introduction to database systems 4 Semantics of the Relation Attributes Refer to: the meaning resulting from the interpretation of attribute values in a tuple. The easier to explain the semantics of the relation , the better the design will be. 12/7/2023 Introduction to database systems 5 GUIDELINE 1 Design a relation schema so that it is easy to explain its meaning. Don’t combine multiple entity types and relationships into a single relation. Attributes of different entities (EMPLOYEEs, DEPARTMENTs, PROJECTs) should not be mixed in the same relation Only foreign keys should be used to refer to other entities Entity and relationship attributes should be kept apart as much as possible.. 12/7/2023 Introduction to database systems 6 Simplified Version of the COMPANY Database System 12/7/2023 Introduction to database systems 7 12/7/2023 Introduction to database systems 8 Redundant Information in Tuples and Update Anomalies One goal of schema design is to minimize the storage space used by the base relations. Mixing attributes of multiple entities may cause problems Information is stored redundantly wasting storage Problems with update anomalies: ▪ Insertion anomalies ▪ Deletion anomalies ▪ Modification anomalies 12/7/2023 Introduction to database systems 9 Note: Attributes of different entities are mixed in one relation. (Employee and Department) in EMP_DEPT (Employee 12/7/2023 and Project) in EMP_PROJ Introduction to database systems 10 12/7/2023 Introduction to database systems 11 EXAMPLE OF AN UPDATE ANOMALY Consider the relation: EMP_PROJ ( Emp#, Proj#, Ename, Pname, No_hours) Update Anomaly Changing the name of project number P1 from “Billing” to “Customer-Accounting” may cause this update to be made for all 100 employees working on project P1 12/7/2023 Introduction to database systems 12 EXAMPLE OF AN UPDATE ANOMALY (2) Insert Anomaly Cannot insert a project unless an employee is assigned to. Inversely- Cannot insert an employee unless he/she is assigned to a project. Delete Anomaly When a project is deleted, it will result in deleting all the employees who work on that project. Alternately, if an employee is the sole employee on a project, deleting that employee would result in deleting the corresponding project. 12/7/2023 Introduction to database systems 13 GUIDELINE 2 Design a schema that does not suffer from the insertion, deletion and update anomalies. If there are any present, then note them so that applications can be made to take them into account 12/7/2023 Introduction to database systems 14 GUIDELINE 3 Relations should be designed such that their tuples will have as few NULL values as possible Attributes that are NULL frequently could be placed in separate relations (with the primary key) 12/7/2023 Introduction to database systems 15 Null Values in Tuples Reasons for nulls: Attribute not applicable or invalid Attribute value unknown (may exist) Value known to exist, but unavailable NULLs can waste space at the storage level and may also lead to problems with understanding the meaning of the attributes. 12/7/2023 Introduction to database systems 16 Spurious Tuples Bad designs for a relational database may result in erroneous results for certain JOIN operations The "lossless join" property is used to guarantee meaningful results for join operations 12/7/2023 Introduction to database systems 17 GUIDELINE 4 Design relation schemas so that they can be joined with equality conditions on attributes that are related (PK,FK) pairs in a way that guarantees that no spurious tuples are generated 12/7/2023 Introduction to database systems 18 Lossless join example Ri is decomposed into A Ri B C R1 A B 1 2 R1 and R2 1 2 3 4 5 4 5 6 7 2 Ro is the join of R1 and 7 2 8 R2 B C R2 A B C 2 3 5 6 This is lossless join 1 4 2 5 3 6 2 8 since Ro has erroneous 7 2 8 1 2 8 tuples 7 2 3 Ro 12/7/2023 Introduction to database systems 19 Informal Guidelines GUIDELINE 1: Design a relation schema so that it is easy to explain its meaning. Don’t combine multiple entity types and relationships into a single relation. GUIDELINE 2: Design a schema that does not suffer from the insertion, deletion and update anomalies. If there are any present, then note them so that applications can be made to take them into account GUIDELINE 3: Relations should be designed such that their tuples will have as few NULL values as possible Attributes that are NULL frequently could be placed in separate relations (with the primary key) GUIDELINE 4: The relations should be designed to satisfy the lossless join condition. No spurious tuples should be generated by doing a natural-join of any relations 12/7/2023 Introduction to database systems 20 Used to define the goodness and badness of individual relation schemas. Functional Dependencies (1) Functional dependencies (FDs) are used to specify formal measures of the "goodness" of relational designs FDs and keys are used to define normal forms for relations FDs are constraints that are derived from the meaning and interrelationships of the data attributes. 12/7/2023 Introduction to database systems 22 Functional Dependencies (2) Functional dependence is a many to one relationship from one set of attributes to another Let r be a relation, and let X and Y be subsets of the attributes of r X → Y says “Y is functionally dependent on X”, or “X functionally determines Y” X is the determinant; Y the dependent 12/7/2023 Introduction to database systems 23 Functional Dependencies (3) A set of attributes X functionally determines a set of attributes Y if the value of X determines a unique value for Y X → Y holds if whenever two tuples have the same value for X, they must have the same value for Y If t1[X]=t2[X], then t1[Y]=t2[Y] in any relation instance r(R) X → Y in R specifies a constraint on all relation instances r(R) FDs are derived from the real-world constraints on the attributes 12/7/2023 Introduction to database systems 24 Examples of FD constraints Social Security Number determines employee name SSN → ENAME Project Number determines project name and location PNUMBER → {PNAME, PLOCATION} Employee SSN and project number determines the hours per week that the employee works on the project {SSN, PNUMBER} → HOURS 12/7/2023 Introduction to database systems 25 Functional Dependencies (4) An FD is a property of the attributes in the schema R The constraint must hold on every relation instance r(R) If K is a key of R, then K functionally determines all attributes in R (since we never have two distinct tuples with t1[K]=t2[K]) 12/7/2023 Introduction to database systems 26 Defining FDs from instances Note that in order to define the FDs, we need to understand the meaning of the attributes involved and the relationship between them. Given the instance of a relation, all we can conclude is that an FD may exist between certain attributes. What we can definitely conclude is – that certain FDs do not exist because there are tuples that show a violation of those dependencies. 12/7/2023 Introduction to database systems 27 Defining FDs from instances - Example Consider the data for attributes denoted A, B, C, D, and E in the Sample relation (next slide). Important to establish that sample data values shown in relation are representative of all possible values that can be held by attributes A, B, C, D, and E. Assume true despite the relatively small amount of data shown in this relation. 12/7/2023 Introduction to database systems 28 Defining FDs from instances - Example 12/7/2023 Introduction to database systems 29 Defining FDs from instances - Example 12/7/2023 Introduction to database systems 30 Identifying the Primary Key for a Relation using Functional Dependencies Main purpose of identifying a set of functional dependencies for a relation is to specify the set of integrity constraints that must hold on a relation. An important integrity constraint to consider first is the identification of candidate keys, one of which is selected to be the primary key for the relation. All attributes that are not part of a candidate key should be functionally dependent on the key. 12/7/2023 Introduction to database systems 31 Example - Using sample data to identify functional dependencies. 12/7/2023 Introduction to database systems 32 Example - Identifying Primary Key for Sample Relation Sample relation has four functional dependencies. The determinants in the Sample relation are A, B, C, and (A, B). However, the only determinant that functionally determines all the other attributes of the relation is (A, B). (A, B) is identified as the primary key for this relation. 12/7/2023 Introduction to database systems 33 Introduction Normalization: Process of decomposing unsatisfactory "bad" relations by breaking up their attributes into smaller relations. The normalization process takes a relation schema through a series of tests to certify if it satisfies a certain normal form. Normalization of data: process of analyzing the given relation schemas based on their FDs and PKs to achieve the following properties: Minimizing redundancy Minimizing the insertion , deletion and update anomalies 12/7/2023 Introduction to database systems 35 Introduction Normal form: Condition using keys and FDs of a relation to certify whether a relation schema is in a particular normal form 2NF, 3NF, BCNF based on keys and FDs of a relation schema 4NF based on keys, multi-valued dependencies 5NF based on keys, join dependencies : JDs The database designers need not normalize to the highest possible normal form (usually up to 3NF, and BCNF, 4NF rarely used in practice) Denormalization: The process of storing the join of higher normal form relations as a base relation—which is in a lower normal form 12/7/2023 Introduction to database systems 36 First Normal Form(1NF) Disallows: Composite attributes Multivalued attributes Nested relations ▪ attributes whose values for an individual tuple are non-atomic Considered to be part of the definition of relation 12/7/2023 Introduction to database systems 37 1NF with Redundancy 12/7/2023 Introduction to database systems 38 First Normal Form(1NF) There are three main techniques to achieve 1NF: Remove Dlocation (multivalued attribute) attribute and place it in separate relation. The PK of the new relation is {Dnum, Dloc} Expand the key so that there will be a separate tuple in the original relation for each location. The new PK is {Dum, Dloc} If a max number of values is known for the attribute then replace the multivalued attribute with number of attributes equal max number of possible values. 12/7/2023 Introduction to database systems 39 1NF by propagating the primary key 12/7/2023 Introduction to database systems 40 Definitions of Keys and Attributes Participating in Keys If a relation schema has more than one key, each is called a candidate key. One of the candidate keys is arbitrarily designated to be the primary key, and the others are called secondary keys. A Prime attribute must be a member of some candidate key A Nonprime attribute is not a prime attribute— that is, it is not a member of any candidate key. 12/7/2023 Introduction to database systems 41 Second Normal Form (2NF) Based on the concept of full functional dependency. Uses the concepts of FDs, primary key Definitions: Prime attribute - attribute that is member of some candidate key K Full functional dependency - a FD Y→Z where removal of any attribute from Y means the FD does not hold any more 12/7/2023 Introduction to database systems 42 Examples: 2NF {SSN, PNUMBER} → HOURS is a full FD since neither SSN → HOURS nor PNUMBER → HOURS hold {SSN, PNUMBER} → ENAME is not a full FD (it is called a partial dependency ) since SSN → ENAME also holds A relation schema R is in second normal form (2NF) if every non-prime attribute A in R is fully functionally dependent on (the primary key) (every key) of R R can be decomposed into 2NF relations via the process of 2NF normalization 12/7/2023 Introduction to database systems 43 2NF Example 12/7/2023 Introduction to database systems 44 2NF Example 2 A relation that is not in 2NF ACTIVITY Key: Student_ID, Activity Student_ID Activity Fee Activity → Fee Student_ID Activity Fee 222-22-2020 Swimming 30 232-22-2111 Golf 100 222-22-2020 Golf 100 255-24-2332 Hiking 50 Fee is determined by Activity 12/7/2023 Introduction to database systems 45 2NF Example 2 (cont.) Divide the relation into two relations that now meet 2NF STUDENT_ACTIVITY Key: Student_ID and Student_ID Activity Activity ACTIVITY_COST Key: Activity Activity Fee Activity → Fee Student_ID Activity 222-22-2020 Swimming Activity Fee 232-22-2111 Golf Swimming 30 222-22-2020 Golf Golf 100 255-24-2332 Hiking Hiking 50 12/7/2023 Introduction to database systems 46 Third Normal Form (3NF) Definition Transitive functional dependency – if there a set of attribute Y that are neither a primary or candidate key and both X → Y and Y → Z holds. Examples: SSN → DMGRSSN is a transitive FD ▪ Since SSN →DNUMBER and DNUMBER→DMGRSSN hold SSN → ENAME is non-transitive ▪ since there is no set of attributes Y where SSN→Y and Y→ENAME 12/7/2023 Introduction to database systems 47 3rd Normal Form A relation schema R is in third normal form (3NF) if it is in 2NF and no non-prime attribute A in R is transitively dependent on the primary key NOTE: In X → Y and Y → Z, with X as the primary key, we consider this a problem only if Y is not a candidate key. When Y is a candidate key, there is no problem with the transitive dependency. E.g., Consider EMP (SSN, Emp#, Salary ). ▪ Here, SSN → Emp# → Salary and Emp# is a candidate key. 12/7/2023 Introduction to database systems 48 3NF Example 12/7/2023 Introduction to database systems 49 Normal Forms Defined Informally 12/7/2023 Introduction to database systems 50 SUMMARY OF NORMAL FORMS based on Primary Keys 12/7/2023 Introduction to database systems 51 Exercise Consider the relation for published books: BOOK (Book_title, Authorname, Book_type, Listprice, Author- affil, Publisher) Suppose the following dependencies exist: Book_title → Book_type, Publisher Book_type → Listprice Authorname → Author-affil (Note: Author_affil stands for author’s affiliation). BOOK relation is in 1NF. Based on known dependencies, state which dependencies cause 2NF to be violated and which cause 3NF to be violated. 12/7/2023 Introduction to database systems 52 2NF Violations 2NF violations: Book_title → Book_type, Publisher Authorname → Author-affil Reason: they represent partial functional dependence on only a part of the (primary) key. 12/7/2023 Introduction to database systems 53 3NF Violation 3NF violation: Book_type → Listprice Reason: this is a functional dependency among attributes outside of the key. Hence it causes Listprice to be transitively dependent on the key Book_title via the attribute Book_type. 12/7/2023 Introduction to database systems 54 2NF 2NF: BOOK_INFO (Book_title, Book_type, List_price, Publisher) AUTHOR (Authorname, Author-affil) BOOK (Book_title, Author_name) 12/7/2023 Introduction to database systems 55 3NF BOOK_INFO is decomposed into two relations: 1) BOOK_INFO (Book_title, Book_type, Publisher ) 2) BOOK_VALUE (Book_type, List_price) The other two relations are fine since they are in 3NF. 3) AUTHOR (Authorname, Author-affil) 4) BOOK (Book_title, Author_name) 12/7/2023 Introduction to database systems 56 12/7/2023 Introduction to database systems 57