0% found this document useful (0 votes)
179 views25 pages

Week11 CM MDL CC225

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
179 views25 pages

Week11 CM MDL CC225

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 25
ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester MODULE 11: NORMALIZATION WEEK 11 Learning Outcome: After completing this course you are expected to demonstrate the following: 1. Defines normalizationa dn possible undesirable properties of relations and schemarefinement. It also introduce the functional dependency. A. Engage Cryptogram Instruction: Decode the message. Each letter in the phrase has been replaced with a random letter or number. ie 8 2 A I A I I A Oe I u A I I rn ae ae aa oo A aA oT A AOA a ea = Sea eae Normalization — Week 10 YouTube Link: https://www.youtube.com/watch?v=y030YWDLu0Q Module Video Filename: Module10 video Normalization . Explain According to studytonight.com, Database Normalization is a technique of organizing in the database. Normalization is a systematic approach of decomposing tables to eliminate data redundancy (repetition) and undesirable characteristics like Insertion, Update and Deletion Anomalies. It is a multi-step process that puts data into tabular form, removing duplicated data from the relation tables. Normalization is used for mainly two purposes, the dat: ‘© Eliminating redundant (useless) data. ‘* Ensuring data dependencies make sense i.e data is logically stored. Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 1 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester D. Elaborate Normalization is a database design technique that reduces data redundancy and eliminates undesirable character ike Insertion, Update and Deletion Anomalies. Normalization rules divide larger tables into smaller tables and links them using relationships. The purpose of Normalization in SQL is to eliminate redundant (repetitive) data and ensure data is stored logically. The inventor of the relational model Edgar Codd proposed the theory of normalization of data with the introduction of the First Normal Form, and he continued to extend theory with Second and Third Normal Form. Later he joined Raymond F. Boyce to develop the theory of Boyce-Codd Normal Form. Database Normal Forms Here is a list of Normal Forms ‘© NF (First Normal Form) ‘© 2NF (Second Normal Form) ‘© 3NF (Third Normal Form) ‘© BCNF (Boyce-Codd Normal Form) ‘© 4NF (Fourth Normal Form) © SNF (Fifth Normal Form) ‘© GNF (sixth Normal Form) The Theory of Data Normalization in SQL server is still being developed further. For example, there are discussions even on 6thNormal Form. However, in most practical applications, normalization achieves its best in 3rdNormal Form. The evolution of SQL Norm: theories is illustrated below- Figure 11.0 Database Normal Forms Types of Normal Forms There are the four types of normal forms: NormalForms >) f = ¢ = _ _ @) or Figure 11.1, Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. Delos Santos, MSCS Page2 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester Table 11.0 Normal Description Form ANF A relation is in 1NF if it contains an atomic value. one A relation willbe in 2NF IFit sn INF and all non-key attributes are fully functional dependent on the primary key BNF A relation will be in 3NF if it is in 2NF and no transition dependency exists. ‘A relation willbe in 4NF Wits in Boyce Codd normal form and has no ane multi-valued dependency. one ‘A relation is in SNF ifit is in 4NF and not contains any join dependency and joining should be lossless. First Normal Form (1NF) © Arelation will be INF 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! not in INF because of multi-valued attribute EMP_PHONE. Table 11.1 EMPLOYEE table EMP_ID EMP_NAME EMP_PHONE EMP_STATE 1a John 7272826385, up 9064738238 20 Harry 8574783832 Bihar 7390372389, * 2 sam ‘589800302 Punjab ‘The decomposition of the EMPLOYEE table into INF has been shown below: EMP_ID EMP_NAME EMP_PHONE EMP_STATE 14 John 7272826385 up 14 John 9064738238 up 20 Harry 8574783832 Bihar 2 sam 7390372389 Punjab 2 sam 8589830302 Punjab Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. DelosSantos, MSCS Page 3 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester ‘Second Normal Form (2NF) * Inthe 2NF, relational must be in 1NF. + Inthe 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. Ina school, a teacher can teach more than one subject. Table 11.2 TEACHER table TEACHER_ID SUBJECT TEACHER_AGE 25 Chemistry 30 25 Biology 30 a7 English 35 8 Math 38 8 ‘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: Table 11.3 TEACHER _DETAIL table TEACHER 1D TEACHER_AGE 25 30 a7 35 83 38 Table 11.4 TEACHER_SUBIECT table TEACHER ID SUBJECT 25 Chemistry 25 Biology 7 English 83 Math 83 Computer Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. DelosSantos, MSCS Page 4 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester Normal Form (3NF) + Arelation will be in 3NF if it is in 2NF and not contain any transitive partial dependency. ‘+ 3NF is used to reduce the data duplication. tis 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 third normal form if it holds at least one of the following conditions for every non-trivial function dependency X > Y. 1. Xisa super key. 2. Yis a prime attribute, i.e,, each element of Y is part of some candidate key. Example: Table 11.5 EMPLOYEE_DETAIL table emp_ip | EMP_NAME | EMP_ziP | EMP_STATE | EMP_CITY 222 Harry 201010 up Noida 333 Stephan 02228 us Boston aaa Lan 60007 us Chicago 555 Katharine 06389 UK Norwich 666 John 462007 mp Bhopal ‘Super key in the table above: {EMP_ID}, {EMP_ID, EMP_NAME}, {EMP_ID, EMP_NAME, EMP_ZIP}....s0 on Candidate key: {EMP_ID} Non-prime attributes: In the given table, all attributes except EMP_ID are non-prime. Here, EMP_STATE & EMP_CITY dependent on EMP_ZIP and EMP_ZIP dependent on EMP_ID. The non-prime attributes (EMP_STATE, EMP_CITY) transitively dependent on super key(EMP_ID). It violates the rule of third normal form. That's why we need to move the EMP_CITY and EMP_STATE to the new table, with EMP_ZIP as a Primary key. Table 11.6 EMPLOYEE table EMP_ID EMP_NAME EMP_ZIP 222 Harry 201010 333 Stephan 02228 444 Lan 60007 555 Katharine 06389 666 John 462007 Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. DelosSantos, MSCS Page 5S of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester Table 11.7 EMPLOYEE_ZIP table EMP_zIP EMP_STATE EMP_cITY 201010 up Noida 02228 us Boston 60007 us Chicago 06389 UK Norwich 462007 me Bhopal Boyce Codd normal form (BCNF) © BCNF is the advance version of 3NF. It is stricter than 3NF. © Atableis BCNF if every functional dependency X > Y, Xis 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. Table 11.8 EMPLOYEE table EmP_ID | EMP_COUNTRY | EMP_DEPT | DEPT_TYPE | EMP_DEPT_NO 264 India Designing 394 283 264 India Testing 394 300 364 UK Stores D283 232 364 UK Developing D283 549 In the above table Functional dependencies are as follows: EMP_ID > EMP_COUNTRY EMP_DEPT > {DEPT_TYPE, EMP_DEPT_NO} Candidate key: {EMP-1D, 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: Table 11.9 EMP_COUNTRY table EMP_ID EMP_COUNTRY 264 India 264 India Prepared and Validated By: Ms AlmiraSactiz | Dean Manuel Luis C. Delos Santos, MSCS Page6 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester Table 11.10 EMP_DEPT table EMP_DEPT DEPT_TYPE | EMP_DEPT_NO Designing 394 283 Testing 394 300 Stores 283 232 Developing 283 549 Table 11.11 EMP_DEPT_MAPPING table EMP_ID EMP_DEPT pasa 283 D394 300 283 232 283 sao Functional dependencies: EMP_ID > EMP_COUNTRY EMP_DEPT -> {DEPT_TYPE, EMP_DEPT_NO} Candidate keys: For the first table: EMP_ID For the second table: EMP_DEPT For the third table: {EMP_1D, EMP_DEPT} Now, this is in BCNF because left side part of both the functional dependencies is a key. Fourth normal form (4NF) © Arelation will be in 4NF dependency. ‘© For a dependency A > 8, if for a single value of A, multiple values of B exists, then the relation will be a multi-valued dependency. Boyce Codd normal form and has no multi-valued Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. DelosSantos, MSCS Page 7 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester Example Table 11.12 STUDENT table sTU_ID COURSE 21 Computer 21 Math 34 Chemistry Dancing 4 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: Table 11.13 Student_Course table sTU_ID COURSE 2. Computer 2. Math 34 Chemistry 74 Biology 59 Physics Table 11.14 STUDENT_HOBBY table STUD HOBBY 21 Dancing 21 Singing 34 Dancing 4 Cricket 59 Hockey Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. DelosSantos, MSCS Page 8 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester Fifth Normal Form (SNF) ‘© Arelation is in SNF if itis in 4NF and not contains any join dependency and joining should be lossless. ‘+ SNF is satisfied when all the tables are broker avoid redundancy. ‘+ SNF is also known as Project-join normal form (PJ/NF). 10 as many tables as possible in order to 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 avalid 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 SNF, we can decompose it into three relations P1, P2 & P3: Pa SEMESTER SUBJECT Semester 1 Computer Semester 1 Math Semester 1 Chemistry Semester 2 Math 2 SUBJECT LECTURER Computer Anshika Computer John Math John Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. DelosSantos, MSCS Page9 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester Math Akash Praveen 3 SEMESTER LECTURER Semester 1 Anshika Semester 1 John Semester 1 John Semester 2 Akash Semester 1 Praveen Refinement - Functional Dependencies Schema refinement is just a fancy term for saying polishing tables. It is the last step before con ig physical design/tuning with typical workload: 1) Requirement analysis : user needs 2) Conceptual design : high-level description, often using E/R diagrams 3) Logical design : from graphs to tables (relational schema) 4) Schema refinement : checking tables for redundancies and anomalies Let’s see an example of redundanci client's name is the primary key. and anomalies. Consider the following table where the employeelD |_FirstName_| LastName Hire Date. Client L Mary Marley 1/1/2005 James H. 2 Robert Crosby 2/4/2008 Clark L. 3 Robert, Crosby | 2/4/2008 TinaM. The table is presenting information on employees (sales reps) and their + Ifwe want to insert data, we notice th * each row requires an entry in the client field * we can’t insert data for newly hired sales reps until they've been assigned to one or more clients ‘* if sales reps are in a training process, even if they've been already hired, they can’t actually join the database because they need to have a delegated client... unless “dummy” clients are created. If we want to update data, we notice th. ‘* the sales reps name is repeated for each client. ‘* what if, for a given client, we misspelled the name of the sales reps Crosby instead of Cosby... how can we edit that without affecting all the sales reps called Crosby? Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 10 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester If we want to delete data, what if Mary doesn’t have a client anymore because she’s taking a year off? We are forced to either * create a dummy client ‘* incorrectly showing her with a client she no longer handled * delete Mary's record (even if however she’s still an employee) * notice we cannot have “null” as a client since primary field keys cannot store null. When we have to treat with schema refinement we often notice that the main problem is redundancy. In order to identify schemas with such problems, we'll introduce the notion of functional dependencies: a relationship that exists when one attribute uniquely determines another attribute. A functional dependency is simply a new type of constraint between two attributes. Say that Ris a relation with attributes X and Y, we say that there is a functional dependency X -> Y when Vis functionally dependent on x (where X is the determinant set and Y is the dependent attribute), Let’s illustrate a scenario where the designer didn’t take in consideration dependencies between columns. © Data (studiD, studName, address, courselD, courseName, grade) The follov ig structure is considerably better: © Student(studiD, studName, address) * Course (courselD, courseName) © Enrolled (studiD, courselD, grade) How do we pass from one to the other? That's what schema refinement does through functional dependencies. ‘A. unique way to represent a student is through his studID. Each student has his own address, hence we can say that studID determines address. We'll write this in the following way: © studiD -> address In the previous example, we actually have the following FDs: + studiD -> studName, address * courselD -> courseName ‘+ studlD, courselD -> grade Let’s have a look at the properties of functional dependencies in the case where X, Y and Z are attributes belonging to a table R: ity: if we assume that X ->Y and Y->Z, then it’s clear that X ->Z y: If Y is a subset of X, then X > Y ‘* augmentation: if x -> Y, then for any Z we'll have X, Z->Y,Z © union: if X-> Vandy ->z, then X ->Y,Z + decomposition: if x > Y, Zthen x -> Vand x ->Z The first 3 properties are called the Armstrong’s Axioms. Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 11 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester If F is a set of functional dependencies, F+ is the set of all FDs logically implied by F. Logically implied is just another way of saying obtained from the properties of functional dependencies ( the ones that we just enumerated). F+ is also calledthe closure of the set of functional ly implied by those present in F. dependencies. Isis the set of all dependencies lo; Let's illustrate the usage of those properties with an example. If we have the following set of FDs, can we conclude that A -> H is logically implied? * AB 2 Ac © GG->H * GG->I * B->H Let’s see which properties are applicable to our case: ‘+ We know that, by the transitivity property, if X > Y and Y ->Z then we have X >Z. ‘+ In our case we have A -> B and B -> H. * Hence, by transitivity, A -> H is logically implied. Which other dependencies are part of the closure? * CG >HI by the union rule ‘* AG >I by noticing that A > C holds, and then AG > CG by the augmentation rule and then AG > | by transitivity. Given a set of FDs, is there a faster way to compute if a dependency is logically implied? Let’s see through an example how we can ask this question in multiple ways: © Does F ={A->B,B ->C, CD ->E}imply A->E? * IsA->Einthe closure F+? + IsEinat? Before going on with a linear time algorithm, we notice that we've introduced a new notion, ‘A+. We call A+ the attribute closure of A with respect to F and it will help us figure out if A -> E is logically implied. 1) Assume that we create a temporary attribute closure of A called TMP and that to begin, TMP =A (the input of the FDs that you want to verify) 2) Let’s consider the first given dependency of F, A ->B. 3) Is Ainthe TMP? Yes, since as stated previously TMP = A; we continue. 4) If we continue, we union B with the current TMP, A. What we obtain is the new TMP, AB (since A union B = AB). 5) We now consider the second given dependency, B -> C. 6) IsB inthe TMP? Yes, since we now have AB in the TMP; we continue. 7) Ife continue, we union C with the current TMP, AB. What we obtain is the new TMP, ABC (since AB union C= ABC). 8) We consider the 3rd given dependency, CD ->E. 9) Is CD in the TMP? No, since we only have ABC in the current TMP, hence we stop. 10) The attribute closure of A is then A+ = TMP ={A, B, C} Now, to check if A -> E is in the closure F+, we can conclude that since E is NOT in A+, then A-> Eis NOTin Fs, Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 12 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester ‘We can generalize this into an algorithm: 1) Consider the input of your FDs as the first element of yourtemporary attribute closure TMP. 2) Consider each dependency x -> Y of the given set of FDs 3) Is part of TMP? If yes, continue to step 4. If no, continue to step 5. 4) Yes: Union TMP with Y. 5) No: Your attribute closure = TMP (your current temporary attribute closure from step 3). Conclusion: if an attribute is in your attribute closure, then it’s logically implied (it’s part of the closure of the set of functional dependencies). Now that we know how to quickly verify if a dependency is logically implied... how do we find all the dependencies that are logically implied? Given a set of FDs F, how do we find its closure, Fe? Let’s go through an example again: © Given F={A->B, B -> C}, compute F+ ‘The algorithm is pretty ple: 1) Build an empty matrix with all possible combinations of attributes as rows and columns A 8 c AB AC BC ABC, A 8 c AB AC BC ABC, 2) Compute the attribute closures of all attribute com Attribute closure At ABC Bt BC ce c (AB)+ ‘ABC (ac}+ ‘ABC (B0)+ BC (aac) ‘ABC 3) Fill the matrix from step 1) by putting a check mark when a row member Y (from the table defined in step 1) is part of a member of the attribute closure Y+ (from the table defined in step 2) Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. DelosSantos, MSCS Page 13 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester A 8 c AB AC BC ‘ABC A x x x x x x x B x x x c x AB x x x x x x x AC x x x x x x x BC x x x ‘ABC x x x x x x x Let’s look at some examples ‘+ row member A: A, B, C, AB, AC, BC and ABC are all attributes of the closure of A+, ABC. ‘* row member BC. A is not a member of attribute closure (BC)+ : we don’t put a check mark because there is no A in (BC}+. However, we check B and C and BC. ‘* row member C. A is not a member of (C+): we don’t check it because C+ contains only C. By having a check-mark at say the intersection of row A with column BC we mean that A -> BC is part of the closure F+. This is how we enumerate all the dependencies that are part of the closure F+. Functional dependencies can also be used to find all the candidate keys. By definition, a candidate key is a set of columns that can be uniquely used to identify a database record without any irrelevant/unrelated/superfiuous data. It is a reduction of the entire collection of attributes, hence a minimization. Since we are talking about a minimal subset, we can start with the complete set of attributes and then, following functional dependencies, minimize the set until we reach the candidate keys (a set of attributes that cannot be reduced). Let’s illustrate this once more by an example. Say we have F ={A->B, BC->E and ED -> Al. 1) We know that the set of all attributes is ABCDE. 2) Can we reduce the set by using the first given FD? If we follow A - > B, we can remove B from the main set because B depends on A, and ABCD already contains A, hence no need of any dependent superfluous attribute. We obtain ACDE. 3) Can we reduce the set by using the second FD? If we follow BC - > E, we can remove E from the main set because E depends on BC, and ABCDE already contains BC. We obtain ABD. 4) Can we reduce the set by using the third FD? If we follow ED - > A, we can remove A from the main set because A depends on ED, and ABCDE already contains ED. We obtain BODE, 5) We now have a new set of at ibutes: ACDE, ABCD and BCDE. Let’s call them X. 6) Can we simplify any attribute from X by using dependency A - > B? We can remove B from ABCD because ABCD already contains A, and B depends on A: we obtain ACD. Can we do the same for BCDE? No, because BCDE doesn't contain A. 7) Can we simplify any attribute from X by using BC - > E? We can remove E from BCDE because BCDE already contains BC and and E depends on BC: we obtain BCD. Can we do the same for ACDE? No, because ACDE doesn’t contain BC, 8) Can we simplify any attribute from x by using ED - > A? We can remove A from ACDE because ACDE already contains CD and A depends on CD: we obtain CDE. 9) We now have a new set of attributes: ACD, BCD and CDE. Let's call them ¥. Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 14 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester 10) Can we simplify any attribute from Y by using A -> B ? BCD cannot be simplified because it doesn't contain A, and the rest of attributes from Y don’t contain B. 11) Can we simplify any attribute from Y by using BC - > E ? CDE cannot be because it doesn’t contain BC, and the rest of attributes from Y don’t contain E. 12) Can we simplify any attribute from Y by using ED - > A ? ACD cannot be simplified because it doesn’t contain ED, and the rest of attributes from Y don’t contain ED. : the functional dependencies from F cannot be used to simplify the subsets from Y, hence they cannot be more minimized. They are our candidate keys: ACD, BCD and CDE, plified Conclusi 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, Emp_Address. Here Emp_ld 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_ld-> Emp_Name We can say that Emp_Name is functionally dependent on Emp_td. Types of Functional Dependency Functional Dependency NN Trivial ‘Non-trivial Functional Funetional Dependency Dependency Figure 11.2 1 functional dependency Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 15 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester * A> Bhas trivial functional dependency if B is a subset of A. ‘* The following dependencies are also trivial like: A> A,B > B Example: Consider a table with two columns Employee_td and Employee_Name. {Employee_id, Employee Name} -> Employee _Idis a trivial functional dependency as Employee_Id is a subset of {Employee_id, Employee Name}. ‘Also, Employee_Id > Employee_Id and Employee Name -> Employee Name are tri endencies too. I dep 2. Non-trivial functional dependency * A> Bhas a nontrivial functional dependency if B is not a subset of A. ‘© When A intersection B is NULL, then A > Bis called as complete non-trivial. Example: ID > Name, Name > DOB ‘Summary: * Normalization is a technique of organizing the data in the database. There are the four types of normal forms: INF, 2NF, 3NF and BCNF. © When we have to treat with schema refinement we often notice that the main problem is redundancy. In order to identify schemas with such problems, we'll introduce the notion of functional dependencies: a relationship that exists when one attribute uniquely determines another attribute. A functional dependency is simply a new type of constraint between two attributes. * Functional dependencies can also be used to find all the candidate keys. By definition, a candidate key is a set of columns that can be uniquely used to identify a database record without any irrelevant/unrelated/superfiuous data. It is a reduction of the entire collection of attributes, hence a minimization. * The two types of functional dependencies are trivial and non-trivial funi dependencies. nal Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 16 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester E. Evaluate ASSESSMENT: Instruction: Answer the questions below. Write your answer in the Answer Sheet (AS) provided for 2 points each. 1-4, The four types of Normal Forms 5-6. Two types of Functional Dependency 7-10. Typical workloads in schema refinement. Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 17 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester Hands-On Activity 11.1 = The Login Form Fields The login_page.inc.php file describes the form fields that will be pulled into the login page. The form is sticky—that is, the user entries are retained and redisplayed if the user makes a mistake that triggers an error message. Listing 3-10b shows the form fields. Listing 3-7b. Displaying the Form Fields in the Login Page (login_page.inc.php) bogin

email" type="text" name="email" size="30" maxlengt! "

énbsp; Between 8 and 12 characters.

énbsp;


The form fields are formatted by the following code, which we already added to the end of the main style sheet in the earlier “Tidy the Styles” section: #loginfields input { float:left; margin-bottom: 5px; } #loginfields label { margin-bottom: 5px; } #loginfields span { float:left; ) #loginfields submit { margin:Spx auto 5px auto; } span.left { text-align:left; } If the user’s login data does not match the data in the database table, an error message is displayed as shown in Figure 11.3, / a a WU ee the heat Pena out ep chck ie Repie bon se ese mene taermintion Login Nedaty ez apie At W012 Deng An VHS TDS Figure 11.3. One of the error messages displayed when the login is unsuccessful The login has a sticky form so that the user’s entries are retained when an error is encountered. No hint nas to which of the two entries was incorrect; this is a security feature. For instance, if the error message stated that the e-mail address was incorrect but the password was acceptable, a hacker would be able to concentrate on trying possible e-mail addresses. Prepared and Validated By: Ms AlmiraSactiz | Dean Manuel Luis C. Delos Santos, MSCS Page 18 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester The new database Jogindb will allow registered members to log in and log out of a members’ page. Non-registered users will therefore be prevented from accessing the member's page. This is not completely secure because someone could look over the member’s shoulder and discover the file name of the member's page in the browser’s address field. Using that name, she could then access the member’s page, as previously mentioned. This security loophole will be dealt with by using sessions. = Sessions Pages that need to be kept private will use the feature called sessions; therefore, sessions and their application will now be explained. Registered members should be able to log in once per visit to the web site. They should not have to log in several times to access several private pages. The login state should persist as long as that user is logged in. The time between login and logout is called a session. The login starts a session, and the logout closes the session. A session stores some information temporarily on the server, and additional information is stored temporarily on the user’s computer in a cookie. If a session exists, extra features will be available for the logged-in user. There is a way of using sessions without a cookie, but that is less secure. The European Union has ruled that web sites must state that they are using a cookie. The message declaring the use of a cookie can sometimes cause users to panic, but there is nothing to stop the web site adding a soothing message to the effect that the temporary cookie is quite safe to use. if users disable cookies, they won't be able to access the private pages even if they are authorized to access them. The alternative is to attach the session to the URL on each link. We will use the more secure method and employ a temporary cookie on the user’s computer. ‘The process when using sessions is as follows: 1. If auser is authorized to log in and he logs in successfully, a session is created. 2. When a user tries to access a private page, the private page checks to see if a session for that particular user exists. 3. If the session exists, the private page is opened. If the session does not exist, the user is directed back to the login page. 5. By using sessions, the server can be made to differentiate between types of users, such as non-members, registered members, and administrators. 6. Logged-in users can either move to other private pages or log out. When they log out, the session and its temporary cookie are destroyed, Asession is started as follows: Jelse( //cancel the session $_SESSION = array(); // Destroy the variables. séssion destroy(); // Destroy the session itself. setcookie(‘PHPSESSID', ", time()-3€00,'/', ", 0, 0);//Destroy the cookie header ("location: index.php") ; exit (); The act of logging in creates a session that saves variables in an array named $_SESSION. When the user has logged in and tries to open a private page, the code checks that a session has been set up for that user. if it has, the private page will be opened in the user’s browser. Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 19 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester In the following statements extracted from the code for login.php, the data is selected and then a session is created. Information is retained in a temporary cookie and in a temporary variable on the server for use in private pages. //Select three itens from a record that is defined by a combination of the // e-mail and password. The three items are user_id, fname, and user_level. $q = "SELECT user_id, fname, user_level FROM users WHERE (email: psword=SHAL('Sp'))"; Se! AND (/The selected data is assigned to a variable called $result $result = mysqli_query (§dbcon, $q); //Run the query. // tf a matching record (row) is found, the number of records (rows) found will be one if (@aysqli_mum_rovs(sresult) record 1) (//The user input matched the database // Start the session, fetch the record and insert the three values into the session 0 array session_start(); §_SESSION = mysqli_fetch_array (sresult, MYSQLT_ASSOC); Sessions create a unique user identity. This is called a UID (a unique identification number). The statement establishes a UID. It registers the user’s session on the server and saves the information for use on any private pages. The following is an example of the code at the start of a private page accessible only to the administrator with a user_level number 1): The code can be translated as follows: “If the session does not exist, or if the user level is not equal to 1, then the user will be sent back to the login page.” Our next step is to create a members-only page protected by the login session. + A Members-only Page Use the downloadable page members-page.php, or create the page by hand-coding it using Listing 3-12. The members’ page created for this tutorial is shown in Figure 11-4. Prepared and Validated By: Ms AlmiraSacriz | Dean Manuel Luis C. Delos Santos, MSCS Page 20 of 25 ASIAN INSTITUTE OF COMPUTER STUDIES Bachelor of Science in Computer Science Course Modules C225 - Information Management (Database System) 2 Year— 2" Semester Welcome tothe Members Page Mike orton . ere Retest: sane opr © An Wo Deity in at SACS IMLS Figure 11.4. The members-only page. Note that the member is welcomed by name Note the revised members’ header in Figure 11.4. Also note the session, the redirection to the login page and the included file for the new header shown in bold type in Listing 3-12. Listing 3-12. Creating the Members Page (members-page.php) Members page “container"> <2php include (*header-menbers.php"): ?> >
  • Scribd - Download on the App Store
  • Scribd - Download on the App Store
Language: