🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Unit-3_DBMS.docx

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Unit 3 **Functional Dependency** The functional dependency is a relationship that exists between two attributes. It typically exists between the primary key and non-key attribute within a table. X   →   Y The left side of FD is known as a determinant, the right side of the production is known as...

Unit 3 **Functional Dependency** The functional dependency is a relationship that exists between two attributes. It typically exists between the primary key and non-key attribute within a table. X   →   Y The left side of FD is known as a determinant, the right side of the production is known as a dependent. **For example:** Assume we have an employee table with attributes: Emp\_Id, Emp\_Name, and Emp\_Address. Here Emp\_Id attribute can uniquely identify the Emp\_Name attribute of employee table because if we know the Emp\_Id, we can tell that employee name associated with it. Functional dependency can be written as: Emp\_Id → Emp\_Name. We can say that Emp\_Name is functionally dependent on Emp\_Id. Types of Functional Dependencies in DBMS ---------------------------------------- 1. 2. 3. 4. #### 1. Trivial Functional Dependency :-In **Trivial Functional Dependency**, a dependent is always a subset of the determinant. i.e. If **X → Y** and **Y is the subset of X**, then it is called trivial functional dependency **roll\_no** **name** **age** -------------- ---------- --------- 42 abc 17 43 pqr 18 44 xyz 18 Here, **{roll\_no, name} → name** is a trivial functional dependency, since the dependent **name** is a subset of determinant set **{roll\_no, name}. **Similarly, **roll\_no → roll\_no **is also an example of trivial functional dependency.  #### 2. Non-trivial Functional Dependency In **Non-trivial functional dependency**, the dependent is strictly not a subset of the determinant. i.e. If **X → Y **and **Y** **is not a subset of X**, then it is called Non-trivial functional dependency. **Example:** **roll\_no** **name** **age** -------------- ---------- --------- 42 abc 17 43 pqr 18 44 xyz 18 Here, **roll\_no → name** is a non-trivial functional dependency, since the dependent **name** is **not a subset of **determinant** roll\_no. **Similarly, **{roll\_no, name} → age** is also a non-trivial functional dependency, since **age** is** not a subset of {roll\_no, name} ** #### 3. Multivalued Functional Dependency In **Multivalued functional dependency**, entities of the dependent set are **not dependent** **on each other. **i.e. If **a → {b, c}** and there exists **no functional dependency** between **b and c**, then it is called a **multivalued functional dependency.** **For example,** **roll\_no** **name** **age ** -------------- ---------- ---------- 42 abc 17  43 pqr 18 44 xyz 18 45 abc 19 Here, **roll\_no → {name, age} **is a multivalued functional dependency, since the dependents **name** & **age** are **not dependent** on each other(i.e. **name → age **or** age → name doesn't exist !**) #### 4. Transitive Functional Dependency In transitive functional dependency, dependent is indirectly dependent on determinant. i.e. If **a → b** & **b → c**, then according to axiom of transitivity, **a → c**. This is a **transitive functional dependency.** **For example,** **enrol\_no** **name** **dept** **building\_no** --------------- ---------- ---------- ------------------ 42 abc CO 4 43 pqr EC 2 44 xyz IT 1 45 abc EC 2 Here, **enrol\_no → dept** and **dept → building\_no. **Hence, according to the axiom of transitivity, **enrol\_no → building\_no** is a valid functional dependency. This is an indirect functional dependency, hence called Transitive functional dependency. #### 5. Fully Functional Dependency In full functional dependency an attribute or a set of attributes uniquely determines another attribute or set of attributes. If a relation R has attributes X, Y, Z with the dependencies X-\>Y and X-\>Z which states that those dependencies are fully functional. #### 6. Partial Functional Dependency In partial functional dependency a non-key attribute depends on a part of the composite key, rather than the whole key. If a relation R has attributes X, Y, Z where X and Y are the composite key and Z is non key attribute. Then X-\>Z is a partial functional dependency in RBDMS. Advantages of Functional Dependencies 1\. Data Normalization: - Data normalization is the process of organizing data in a database in order to minimize redundancy and increase data integrity. Functional dependencies play an important part in data normalization. With the help of functional dependencies we are able to identify the primary key, candidate key in a table which in turns helps in normalization. 2\. Query Optimization: - With the help of functional dependencies we are able to decide the connectivity between the tables and the necessary attributes need to be projected to retrieve the required data from the tables. This helps in query optimization and improves performance. ### 3. Consistency of Data: - Functional dependencies ensures the consistency of the data by removing any redundancies or inconsistencies that may exist in the data. Functional dependency ensures that the changes made in one attribute does not affect inconsistency in another set of attributes thus it maintains the consistency of the data in database. ### 4. Data Quality Improvement: - Functional dependencies ensure that the data in the database to be accurate, complete and updated. This helps to improve the overall quality of the data, as well as it eliminates errors and inaccuracies that might occur during data analysis and decision making, thus functional dependency helps in improving the quality of data in database. Inference Rule (IR): ==================== - The Armstrong\'s axioms are the basic inference rule. - Armstrong\'s axioms are used to conclude functional dependencies on a relational database. - The inference rule is a type of assertion. It can apply to a set of FD(functional dependency) to derive other FD. - Using the inference rule, we can derive additional functional dependency from the initial set. The Functional dependency has 6 types of inference rule: **1. Reflexive Rule (IR~1~)** In the reflexive rule, if Y is a subset of X, then X determines Y. If X ⊇ Y then X →    Y   **Example:** X = {a, b, c, d, e}  Y = {a, b, c}   **2. Augmentation Rule (IR~2~)** -------------------------------- The augmentation is also called as a partial dependency. In augmentation, if X determines Y, then XZ determines YZ for any Z. If X    →  Y then XZ   →   YZ   **Example:** For R(ABCD),  **if** A   →   B then AC  →   BC   **3. Transitive Rule (IR~3~)** ------------------------------ In the transitive rule, if X determines Y and Y determine Z, then X must also determine Z. If X   →   Y and Y  →  Z then X  →   Z **4. Union Rule (IR~4~)** ------------------------- Union rule says, if X determines Y and X determines Z, then X must also determine Y and Z. If X    →  Y and X   →  Z then X  →    YZ      **5. Decomposition Rule (IR~5~)** --------------------------------- Decomposition rule is also known as project rule. It is the reverse of union rule. This Rule says, if X determines Y and Z, then X determines Y and X determines Z separately. If X   →   YZ then X   →   Y and X  →    Z    **6. Pseudo transitive Rule (IR~6~)** ------------------------------------- In Pseudo transitive Rule, if X determines Y and YZ determines W, then XZ determines W. If X   →   Y and YZ   →   W then XZ   →   W    **What is Normalization?** -------------------------- - Normalization is the process of organizing the data in the database. - Normalization is used to minimize the redundancy from a relation or set of relations. It is also used to eliminate undesirable characteristics like Insertion, Update, and Deletion Anomalies. - Normalization divides the larger table into smaller and links them using relationships. - The normal form is used to reduce redundancy from the database table. Why do we need Normalization? The main reason for normalizing the relations is removing these anomalies. Failure to eliminate anomalies leads to data redundancy and can cause data integrity and other problems as the database grows. Normalization consists of a series of guidelines that helps to guide you in creating a good database structure. **Data modification anomalies can be categorized into three types:** - **Insertion Anomaly:** Insertion Anomaly refers to when one cannot insert a new tuple into a relationship due to lack of data. - **Deletion Anomaly:** The delete anomaly refers to the situation where the deletion of data results in the unintended loss of some other important data. - **Updatation Anomaly:** The update anomaly is when an update of a single data value requires multiple rows of data to be updated. First Normal Form (1NF) ======================= - A relation will be 1NF if it contains an atomic value. - It states that an attribute of a table cannot hold multiple values. It must hold only single-valued attribute. - First normal form disallows the multi-valued attribute, composite attribute, and their combinations. **Example:** Relation EMPLOYEE is not in 1NF because of multi-valued attribute EMP\_PHONE. **EMPLOYEE table:** --------------------------------------------------------------- **EMP\_ID** **EMP\_NAME** **EMP\_PHONE** **EMP\_STATE** ------------- --------------- ---------------- ---------------- 14 John 7272826385,\ UP 9064738238 20 Harry 8574783832 Bihar 12 Sam 7390372389,\ Punjab 8589830302 --------------------------------------------------------------- The decomposition of the EMPLOYEE table into 1NF has been shown below: **EMP\_ID** **EMP\_NAME** **EMP\_PHONE** **EMP\_STATE** ------------- --------------- ---------------- ---------------- 14 John 7272826385 UP 14 John 9064738238 UP 20 Harry 8574783832 Bihar 12 Sam 7390372389 Punjab 12 Sam 8589830302 Punjab Second Normal Form (2NF) ======================== - In the 2NF, relational must be in 1NF. - In the second normal form, all non-key attributes are fully functional dependent on the primary key **Example:** Let\'s assume, a school can store the data of teachers and the subjects they teach. In a school, a teacher can teach more than one subject. **TEACHER table** **TEACHER\_ID** **SUBJECT** **TEACHER\_AGE** ----------------- ------------- ------------------ 25 Chemistry 30 25 Biology 30 47 English 35 83 Math 38 83 Computer 38 In the given table, non-prime attribute TEACHER\_AGE is dependent on TEACHER\_ID which is a proper subset of a candidate key. That\'s why it violates the rule for 2NF. To convert the given table into 2NF, we decompose it into two tables: **TEACHER\_DETAIL table:** **TEACHER\_ID** **TEACHER\_AGE** ----------------- ------------------ 25 30 47 35 83 38 **TEACHER\_SUBJECT table:** **TEACHER\_ID** **SUBJECT** ----------------- ------------- 25 Chemistry 25 Biology 47 English 83 Math 83 Computer Third Normal Form (3NF) ======================= - A relation will be in 3NF if it is in 2NF and not contain any transitive partial dependency. 3NF is used to reduce the data duplication. It is also used to achieve the data integrity. - If there is no transitive dependency for non-prime attributes, then the relation must be in third normal form. A relation is in third normal form if it holds atleast one of the following conditions for every non-trivial function dependency X → Y. 1. X is a super key. 2. Y is a prime attribute, i.e., each element of Y is part of some candidate key. **EMP\_ID** **EMP\_NAME** **EMP\_ZIP** **EMP\_STATE** **EMP\_CITY** ------------- --------------- -------------- ---------------- --------------- 222 Harry 201010 UP Noida 333 Stephan 02228 US Boston 444 Lan 60007 US Chicago 555 Katharine 06389 UK Norwich 666 John 462007 MP Bhopal 1. **EMP\_ID** **EMP\_NAME** **EMP\_ZIP** ------------- --------------- -------------- 222 Harry 201010 333 Stephan 02228 444 Lan 60007 555 Katharine 06389 666 John 462007 **EMP\_ZIP** **EMP\_STATE** **EMP\_CITY** -------------- ---------------- --------------- 201010 UP Noida 02228 US Boston 60007 US Chicago 06389 UK Norwich 462007 MP Bhopal Boyce Codd normal form (BCNF) ============================= - BCNF is the advance version of 3NF. It is stricter than 3NF. - A table is in BCNF if every functional dependency X → Y, X is the super key of the table. - For BCNF, the table should be in 3NF, and for every FD, LHS is super key. **Example:** Let\'s assume there is a company where employees work in more than one department. **EMPLOYEE table:** **EMP\_ID** **EMP\_COUNTRY** **EMP\_DEPT** **DEPT\_TYPE** **EMP\_DEPT\_NO** ------------- ------------------ --------------- ---------------- ------------------- 264 India Designing D394 283 264 India Testing D394 300 364 UK Stores D283 232 364 UK Developing D283 549 **In the above table Functional dependencies are as follows:** 1. EMP\_ID  →  EMP\_COUNTRY   2. EMP\_DEPT  →   {DEPT\_TYPE, EMP\_DEPT\_NO}   **Candidate key: {EMP-ID, EMP-DEPT}** The table is not in BCNF because neither EMP\_DEPT nor EMP\_ID alone are keys. To convert the given table into BCNF, we decompose it into three tables: **EMP\_COUNTRY table:** **EMP\_ID** **EMP\_COUNTRY** ------------- ------------------ 264 India 264 India **EMP\_DEPT table:** **EMP\_DEPT** **DEPT\_TYPE** **EMP\_DEPT\_NO** --------------- ---------------- ------------------- Designing D394 283 Testing D394 300 Stores D283 232 Developing D283 549 **EMP\_DEPT\_MAPPING table:** **EMP\_ID** **EMP\_DEPT** ------------- --------------- D394 283 D394 300 D283 232 D283 549 Fourth normal form (4NF) ======================== - A relation will be in 4NF if it is in Boyce Codd normal form and has no multi-valued dependency. - For a dependency A → B, if for a single value of A, multiple values of B exists, then the relation will be a multi-valued dependency. ### **Example** **STUDENT** **STU\_ID** **COURSE** **HOBBY** ------------- ------------ ----------- 21 Computer Dancing 21 Math Singing 34 Chemistry Dancing 74 Biology Cricket 59 Physics Hockey The given STUDENT table is in 3NF, but the COURSE and HOBBY are two independent entity. Hence, there is no relationship between COURSE and HOBBY. In the STUDENT relation, a student with STU\_ID, **21** contains two courses, **Computer** and **Math** and two hobbies, **Dancing** and **Singing**. So there is a Multi-valued dependency on STU\_ID, which leads to unnecessary repetition of data. So to make the above table into 4NF, we can decompose it into two tables: **STUDENT\_COURSE** **STU\_ID** **COURSE** ------------- ------------ 21 Computer 21 Math 34 Chemistry 74 Biology 59 Physics **STUDENT\_HOBBY** **STU\_ID** **HOBBY** ------------- ----------- 21 Dancing 21 Singing 34 Dancing 74 Cricket 59 Hockey Fifth normal form (5NF) ======================= - A relation is in 5NF if it is in 4NF and not contains any join dependency and joining should be lossless. - 5NF is satisfied when all the tables are broken into as many tables as possible in order to avoid redundancy. - 5NF is also known as Project-join normal form (PJ/NF). ### **Example** **SUBJECT** **LECTURER** **SEMESTER** ------------- -------------- -------------- Computer Anshika Semester 1 Computer John Semester 1 Math John Semester 1 Math Akash Semester 2 Chemistry Praveen Semester 1 In the above table, John takes both Computer and Math class for Semester 1 but he doesn\'t take Math class for Semester 2. In this case, combination of all these fields required to identify a valid data. Suppose we add a new Semester as Semester 3 but do not know about the subject and who will be taking that subject so we leave Lecturer and Subject as NULL. But all three columns together acts as a primary key, so we can\'t leave other two columns blank. So to make the above table into 5NF, we can decompose it into three relations P1, P2 & P3: **P1** **SEMESTER** **SUBJECT** -------------- ------------- Semester 1 Computer Semester 1 Math Semester 1 Chemistry Semester 2 Math **P2** **SUBJECT** **LECTURER** ------------- -------------- Computer Anshika Computer John Math John Math Akash Chemistry Praveen **P3** **SEMSTER** **LECTURER** ------------- -------------- Semester 1 Anshika Semester 1 John Semester 1 John Semester 2 Akash Semester 1 Praveen

Use Quizgecko on...
Browser
Browser