Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 157B Midterm 2 Revision Prof. Sin Min Lee. Q #1 (01) 1. Characterize the difference between the following pairs of terms a. Entity and entity class.

Similar presentations


Presentation on theme: "CS 157B Midterm 2 Revision Prof. Sin Min Lee. Q #1 (01) 1. Characterize the difference between the following pairs of terms a. Entity and entity class."— Presentation transcript:

1 CS 157B Midterm 2 Revision Prof. Sin Min Lee

2 Q #1 (01) 1. Characterize the difference between the following pairs of terms a. Entity and entity class b. Relationship and relationship type c. Attribute value and attribute d. Strong entity class and weak entity class 2. Create an ER model for the following enterprise: Each building in an organization has a different building name and address. The meeting rooms in each building have their own room numbers and seating capacities. Rooms may be reserved for meetings, and each meeting must start on the hour. The hour and length of use are recorded. Each reservation is made by a group in the company. Each group has a group number and a contact phone.

3 Q#1 (01): Solutions a. Entity and entity class An entity is an object and an entity class is a collection of objects. b. Relationship and relationship type A relationship is a specific instance of two related entities. A relationship type represents the possibility that two entities are related. c. Attribute value and attribute An attribute is a characteristic shared by entities of a class. An attribute value is the particular value of the attribute for a specific entity. d. Strong entity class and weak entity class A strong entity class is one with a key and a weak entity class has no key of its own.

4 Q #1 (01): Solutions

5 Q #1 (02) 1. Consider the following E-R diagram and attributes Buyer: id, name, address Property: id, askingPrice, salePrice, address StaffMember: id, lastName, firstName, phone Branch: id, name, address, phone a. Write sentences to describe the roles of staff members in the diagram. b. Write sentences to describe the roles of property in the diagram. 2. Explain the following constraints. a. Domain constraint b. Key constraint c. Cardinality constraint d. Participation constraint

6 Q#1 (02) Solution 1. a. Write sentences to describe the roles of staff members in the diagram. A staff member may sell many properties. A staff member may work at a branch. A staff member may mange a branch b. Write sentences to describe the roles of property in the diagram. A property may be bought by one buyer A property may be sold by many staff members 2. a. Domain constraint : restrictions on the allowable values of attributes - the type and the range of values possible for each attribute b. Key constraint : unique value in an entity class-most entity classes have one or more attributes that form a key of the class c. Cardinality constraint : the number of relationships that may exist at a time – (to-one and to-many) d. Participation constraint : whether at least one relationship must exist for each entity of a class.

7 Q #2 -01 Create a relational database schema for the following ER model. Group 1 Uses Is In 1 MM contact Has Room capacitytypenumber Building name Meeting lengthhour name date number phone 1 M address

8 Q#2 -01 : solution Building (name, address) Room (number, capacity, type, buildingName) Meeting (name, date, hour, length, roomNo, byGroup) Group (number, contact, phone)

9 Q #2 (02) 1. Create a relational database schema for the following EER model. ssn firstName lastName

10 Q #2 (02) 2. Suppose R(A,B,C,D,E,F,G,H) is a relation. Suppose {A,B}  {C,D,E,F,G,H}, C  {B,E,F}, and G  H. a. Which sets of attributes are the keys of R? b. Identify and eliminate any 2NF violations. c. Identify and eliminate any 3NF violations.

11 Q #2 (02): solution 1. One of the possible solutions Vehicle (vehicleId, weight, capacity, numberWheels) Car (vehicleId, numberDoors, amenities) Truck (vehicleId, weightCapacity, numberAxles) Motorcycle (vehicleId, engineType, usage) Owner (ssn, firstName, lastName) Has (ssn, vehicleId)

12 Q #2 (02): solution 2. a. Which sets of attributes are the keys of R? {A, B} and {A, C} b. Identify and eliminate any 2NF violations. C  {E, F} is a 2NF violation The revised schema is R9: (A, B, C, D, G, H), {A, C} is also a key R10: (C, E, F) c. Identify and eliminate any 3NF violations. G  H is a 3NF violation The revised schema is R10, R11: (A, B, C, D, G), {A, C} is also a key R12: (G, H)

13 Q#3 (01) 1. Write SQL statements to create the following two tables (including key constraints and referential constraints) 2. Write a SQL select statement to find the names of Employees who work in ‘Research’ department. 3. Write a SQL statement to insert a new department with Dnumber=6 and Dname=“sales”. DepartmentDNumberDNameMgrSSN 5Research333445555 4Administrator987654321 1Headquarter888665555 EmployeeSSNFNameLNameSexSalar y SuperSSNDno 123456789JohnSmithM3000033344555 5 5 FranklinWongM4000088866444 4 5 999887777AliciaZelayaF2500098765432 1 4 JenniferWallaceF4300088866555 5 4 666884444RameshNaraynM3800033344555 5 5 453453453JoyceEnglishF2500033344555 5 5 987987987AhmadJabbarM2500098765432 1 4 888665555JamesBorgM55000-1

14 Q #3 (01): Solutions 1. Create table Department ( Dnumberintprimary key, Dnamevarchar(20), MgrSSNint references Employee ); Create table Employee ( SSNint primary key, Fnamevarchar(20), Lnamevarchar(20), Sexchar(1), Salaryint, SuperSSNintreferences Employee, Dnointreferences Department ); 2. Select Fname, Lname from Employee, Department where Dname=‘Research’ and Dnumber = Dno; 3. Insert into Department (Dnumber, Dname) values (6, “Sales”);

15 Q #3 (02) 1. Write a SQL select statement to find the names of all employees whose salaries are greater than 30000. 2. Write a SQL select statement to find the names of departments for which female employees work. 3. Write a SQL statement to delete all the employees who work at Dno 4. DepartmentDNumberDNameMgrSSN 5Research333445555 4Administrator987654321 1Headquarter888665555 EmployeeSSNFNameLNameSexSalar y SuperSSNDno 123456789JohnSmithM3000033344555 5 5 FranklinWongM4000088866444 4 5 999887777AliciaZelayaF2500098765432 1 4 JenniferWallaceF4300088866555 5 4 666884444RameshNaraynM3800033344555 5 5 453453453JoyceEnglishF2500033344555 5 5 987987987AhmadJabbarM2500098765432 1 4 888665555JamesBorgM55000-1

16 QUIZ #3 (02): Solution 1. Select Fname, Lname From Employee Where Salary > 30000; 2. Select Dname From Employee, Department Where Sex=‘F’ and Dno=Dnumber; 3. Delete * From Employee Where Dno=4;

17 Manipulating Information with the Relational Algebra [Ch. 6.1] Relation is a set of tuples and that each tuple in a relation has the same number and types of attributes. Relational algebra includes : Selection Operators Projection Operators Set Operators Join and product Operations Relation is a set of tuples and that each tuple in a relation has the same number and types of attributes. Relational algebra includes : Selection Operators Projection Operators Set Operators Join and product Operations

18 Selection Operators (  ) Reduce the number of tuples in a set by selecting those that satisfy some criteria. Example :  lastName = ‘Doe’ (Customer) [ Select from Customer where lastName = ‘Doe’ ]Customer Reduce the number of tuples in a set by selecting those that satisfy some criteria. Example :  lastName = ‘Doe’ (Customer) [ Select from Customer where lastName = ‘Doe’ ]Customer Account Id Last Name First Name StreetCityStateZip Code Balance 101BlockJane345 Randolph CircleApopkaFL30458-$0.00 102HamiltonCherry3230 Dade St.Dade CityFL30555-$3.00 103HarrisonKatherine103 Landis HallBrattFL30457-$31.00 104BreauxCarroll76 Main St.ApopkaFL30458-$35.00 106MorehouseAnita9501 Lafayette St.HoumaLA44099-$0.00 111DoeJane123 Main St.ApopkaFL30458-$0.00 201GreavesJoseph14325 N. Bankside St.GodfreyIL43580-$0.00 444DoeJaneCawthon Dorm, room 142TallahasseeFL32306-$10.55

19 Projection Operators (  ) Reduce the size of each tuple in a set by eliminating specific attributes. Example :  lastName, firstNAme (Customer) [ project customer onto (lastName, firstName) ] Customer Customer Reduce the size of each tuple in a set by eliminating specific attributes. Example :  lastName, firstNAme (Customer) [ project customer onto (lastName, firstName) ] Customer Customer Account Id Last Name First Name StreetCityStateZip Code Balance 101BlockJane345 Randolph CircleApopkaFL30458-$0.00 102HamiltonCherry3230 Dade St.Dade CityFL30555-$3.00 103HarrisonKatherine103 Landis HallBrattFL30457-$31.00 104BreauxCarroll76 Main St.ApopkaFL30458-$35.00 106MorehouseAnita9501 Lafayette St.HoumaLA44099-$0.00 111DoeJane123 Main St.ApopkaFL30458-$0.00 201GreavesJoseph14325 N. Bankside St.GodfreyIL43580-$0.00 444DoeJaneCawthon Dorm, room 142TallahasseeFL32306-$10.55

20 Set Operators (   -) Manipulate two similar sets of tuples by combining or comparing. Example : Rental  PreviousRental Rental PreviousRental Manipulate two similar sets of tuples by combining or comparing. Example : Rental  PreviousRental Rental PreviousRental accountIdvideoIDdateRenteddateDuecost 1031011/3/991/4/99$1.59 1011132/22/992/25/99$3.00 1011142/2/992/25/99$3.00 10312312/1/9812/31/98$10.99 1011452/14/992/16/99$1.99 101909871/1/991/8/99$2.99 101997871/1/991/4/99$3.49 accountIdvideoIddateRenteddateReturnedcost 101 12/9/9812/10/98$2.49 1011121/13/981/4/98$1.99 1011131/15/99 $0.99 10211312/1/9812/3/98$2.49 11110112/4/9812/6/98$2.49 111997871/1/991/4/99$3.95 201113129/9812/14/98$3.99 201775641/14/991/24/99$3.35

21 Set Operators (   -)... con’t The union of two relations is a relation that contains the set of each tuple that is in at least one of the input relations. Partial result of the Rental  PreviousRental accountIdvideoIddateRenteddateDuecost 101909871/1/991/8/99$2.99 101997871/1/991/4/99$3.49 1031011/3/991/4/99$1.59 10312312/1/9812/31/98$10.99 11110112/4/9812/6/98$2.49 201775641/14/991/24/99$3.35

22 Set Operators (   -)... con’t The intersection of two relations is the set of all tuples that occur in both input relations. The intersection of the relations Rental  PreviousRental in the previous example will return an empty set. Another example would be the intersection between the video IDs of the two tables.  videoId (Rental)   videoId (PrevioutsRental) = Videotapes that are currently rented as well as those that have been rented before. The set of all videotapes that have been rented previously but are not currently rented is expressed as follows:  videoId (PreviousRental) -  videoId (Rental) The intersection of two relations is the set of all tuples that occur in both input relations. The intersection of the relations Rental  PreviousRental in the previous example will return an empty set. Another example would be the intersection between the video IDs of the two tables.  videoId (Rental)   videoId (PrevioutsRental) = Videotapes that are currently rented as well as those that have been rented before. The set of all videotapes that have been rented previously but are not currently rented is expressed as follows:  videoId (PreviousRental) -  videoId (Rental)

23 Join and Product Operations (  ) Increase the size of each tuple by adding attributes The Cartesian product produces a tuple of the new realtion for each combination of one tuple from the left operand and one tuple from the right operand. Example : Employee  TimeCard Employee TimeCard Increase the size of each tuple by adding attributes The Cartesian product produces a tuple of the new realtion for each combination of one tuple from the left operand and one tuple from the right operand. Example : Employee  TimeCard Employee TimeCard ssnlastNamefirstName 145-09-0967UnoJane 245-11-4554ToulouseJennifer 376-77-0099ThreatAyisha 479-98-0098FortuneBruce 588-99-0093FivozinskyBruce ssndatestartTimeendTimestoreIdpaid 145-09-096701/14/998:1512:003yes 245-11-455401/14/998:1512:003yes 376-77-009902/23/9914:0022:005yes 145-09-096701/16/998:1512:003yes 376-77-009901/03/9910:0014:005yes 376-77-009901/03/9915:0019:005yes

24 Join and Product Operations (  )... con’t The result of this operation has 30 tuples because there are 5 Employee and 6 TimeCard. Partial result of Cartesian product Employee  TimeCard The result of this operation has 30 tuples because there are 5 Employee and 6 TimeCard. Partial result of Cartesian product Employee  TimeCard Employee.ssnlastNamefirstNameTimeCard.ssnDatestartTimeendTimestoreIdpaid 145-09-0967UnoJane145-09-096701/14/998:1512:003no 245-11-4554ToulouseJie245-11-455401/14/998:1512:003no 145-09-0967UnoJane376-77-009902/23/9914:0022:005no 245-11-4554ToulouseJie145-09-96701/14/998:1512:003no

25 Join and Product Operations (  )... con’t A selection of those tuples where Employee.ssn equals TimeCard.ssn can be expressed by :  Employee.ssn = TimeCard.ssn (Employee  TimeCard) This type of product is called a join. The join operation puts together related objects from two relations. A Natural Join however is defined so that the shared attribute appears only once in the output table. Ref. textbook Table 6.6 [natural join] vs Table 6.7 [join] A selection of those tuples where Employee.ssn equals TimeCard.ssn can be expressed by :  Employee.ssn = TimeCard.ssn (Employee  TimeCard) This type of product is called a join. The join operation puts together related objects from two relations. A Natural Join however is defined so that the shared attribute appears only once in the output table. Ref. textbook Table 6.6 [natural join] vs Table 6.7 [join]

26 R= ( A, B, C ) F = {A  B, B  C } F + = {A  A, B  B, C  C, AB  AB, BC  BC, AC  AC, ABC  ABC, AB  A, AB  B, BC  B, BC  C, AC  A, AC  C, ABC  AB, ABC  BC, ABC  AC, ABC  A, ABC  B, ABC  C, A  B, … (1) ( given ) B  C, … (2) ( given ) A  C, … (3) ( transitivity on (1) and (2) ) AC  BC, … (4) ( augmentation on (1) ) AC  B,… (5) ( decomposition on (4) ) A  AB,… (6) ( augmentation on (1) ) AB  AC, AB  C, B  BC, A  AC, AB  BC, AB  ABC, AC  ABC, A  BC, A  ABC } R= ( A, B, C ) F = {A  B, B  C } F + = {A  A, B  B, C  C, AB  AB, BC  BC, AC  AC, ABC  ABC, AB  A, AB  B, BC  B, BC  C, AC  A, AC  C, ABC  AB, ABC  BC, ABC  AC, ABC  A, ABC  B, ABC  C, A  B, … (1) ( given ) B  C, … (2) ( given ) A  C, … (3) ( transitivity on (1) and (2) ) AC  BC, … (4) ( augmentation on (1) ) AC  B,… (5) ( decomposition on (4) ) A  AB,… (6) ( augmentation on (1) ) AB  AC, AB  C, B  BC, A  AC, AB  BC, AB  ABC, AC  ABC, A  BC, A  ABC } Using reflexivity, we can generate all trivial dependencies

27 Example: F= { A  B, B  C } A + = ABC B + = BC C + = C AB + = ABC

28 R= ( A, B, C, G, H, I ) F= {A  B, A  C, CG  H, CG  I, B  H } To compute AG + closure = AG closure = ABG ( A  B ) closure = ABCG ( A  C ) closure = ABCGH ( CG  H ) closure = ABCGHI ( CG  I ) Is AG a candidate key? AG  R A +  R ? G +  R ?

29 In general, suppose X  A violates BCNF, then one of the following holds X is a subset of some key K: we store ( X, A ) pairs redundantly. X is not a subset of any key: there is a chain K  X  A ( transitive dependency ) In general, suppose X  A violates BCNF, then one of the following holds X is a subset of some key K: we store ( X, A ) pairs redundantly. X is not a subset of any key: there is a chain K  X  A ( transitive dependency )

30 Third Normal Form The definition of 3NF is similar to that of BCNF, with the only difference being the third condition. Recall that a key for a relation is a minimal set of attributes that uniquely determines all other attributes. A must be part of a key (any key, if there are several). It is not enough for A to be part of a superkey, because this condition is satisfied by every attribute. The definition of 3NF is similar to that of BCNF, with the only difference being the third condition. Recall that a key for a relation is a minimal set of attributes that uniquely determines all other attributes. A must be part of a key (any key, if there are several). It is not enough for A to be part of a superkey, because this condition is satisfied by every attribute. A relation R is in 3NF if, for all X  A that holds over R A  X ( i.e., X  A is a trivial FD ), or X is a superkey, or A is part of some key for R If R is in BCNF, obviously it is in 3NF.

31 Suppose that a dependency X  A causes a violation of 3NF. There are two cases: X is a proper subset of some key K. Such a dependency is sometimes called a partial dependency. In this case, we store (X,A) pairs redundantly. X is not a proper subset of any key. Such a dependency is sometimes called a transitive dependency, because it means we have a chain of dependencies K  X  A. Suppose that a dependency X  A causes a violation of 3NF. There are two cases: X is a proper subset of some key K. Such a dependency is sometimes called a partial dependency. In this case, we store (X,A) pairs redundantly. X is not a proper subset of any key. Such a dependency is sometimes called a transitive dependency, because it means we have a chain of dependencies K  X  A.

32 Key Attributes XAttributes A Key Attributes AAttributes X Key Attributes AAttributes X Partial Dependencies Transitive Dependencies A not in a key A in a key

33 Motivation of 3NF By making an exception for certain dependencies involving key attributes, we can ensure that every relation schema can be decomposed into a collection of 3NF relations using only decompositions. Such a guarantee does not exist for BCNF relations. It weaken the BCNF requirements just enough to make this guarantee possible. Unlike BCNF, some redundancy is possible with 3NF. The problems associate with partial and transitive dependencies persist if there is a nontrivial dependency X  A and X is not a superkey, even if the relation is in 3NF because A is part of a key. Motivation of 3NF By making an exception for certain dependencies involving key attributes, we can ensure that every relation schema can be decomposed into a collection of 3NF relations using only decompositions. Such a guarantee does not exist for BCNF relations. It weaken the BCNF requirements just enough to make this guarantee possible. Unlike BCNF, some redundancy is possible with 3NF. The problems associate with partial and transitive dependencies persist if there is a nontrivial dependency X  A and X is not a superkey, even if the relation is in 3NF because A is part of a key.

34 Reserves Assume: sid  cardno (a sailor uses a unique credit card to pay for reservations). Reserves is not in 3NF sid is not a key and cardno is not part of a key In fact, (sid, bid, day) is the only key. (sid, cardno) pairs are redundantly.

35 Reserves Assume: sid  cardno, and cardno  sid (we know that credit cards also uniquely identify the owner). Reserves is in 3NF (cardno, sid, bid) is also a key for Reserves. sid  cardno does not violate 3NF.

36 Decomposition Decomposition is a tool that allows us to eliminate redundancy. It is important to check that a decomposition does not introduce new problems. A decomposition allows us to recover the original relation? Can we check integrity constraints efficiently? Decomposition is a tool that allows us to eliminate redundancy. It is important to check that a decomposition does not introduce new problems. A decomposition allows us to recover the original relation? Can we check integrity constraints efficiently?

37 A set of relation schemas { R 1, R 2, …, R n }, with n  2 is a decomposition of R if R 1  R 2  …  R n = R sid Supply status city part_id qty Supplier SP sid status city sid part_id qty and

38

39 Problems with decomposition 1. Some queries become more expensive. 2. Given instances of the decomposed relations, we may not be able to reconstruct the corresponding instance of the original relation – information loss. 3. Checking some dependencies may require joining the instances of the decomposed relations.

40 Lossless Join Decomposition The relation schemas { R 1, R 2, …, R n } is a lossless-join decomposition of R if: for all possible relations r on schema R, r =  R1 ( r )  R2 ( r ) …  Rn ( r ) The relation schemas { R 1, R 2, …, R n } is a lossless-join decomposition of R if: for all possible relations r on schema R, r =  R1 ( r )  R2 ( r ) …  Rn ( r )

41 Example: a lossless join decomposition sid sname major IN sid sname IM sid major Student IN IM ‘Student’ can be recovered by joining the instances of IN and IM

42 Example: a non-lossless join decomposition sid sname major IN IM Student IN IM sid major sname Student = IN  IM????

43 IN IM IN  IM The instance of ‘Student’ cannot be recovered by joining the instances of IM and NM. Therefore, such a decomposition is not a lossless join decomposition. Student

44 R- a relation schema F- set of functional dependencies on R The decomposition of R into relations with attribute sets R 1, R 2 is a lossless-join decomposition iff ( R 1  R 2 )  R 1  F + OR ( R 1  R 2 )  R 2  F + Theorem: i.e., R 1  R 2 is a superkey for R 1 or R 2. (the attributes common to R 1 and R 2 must contain a key for either R 1 or R 2 ).

45 Example R = ( A, B, C ) F = { A  B } R = { A, B } + { A, C } is a lossless join decomposition R = { A, B } + { B, C } is not a lossless join decomposition Example R = ( A, B, C ) F = { A  B } R = { A, B } + { A, C } is a lossless join decomposition R = { A, B } + { B, C } is not a lossless join decomposition

46 N = 5 R(A 1, A 2, A 3, A 4, A 5 ) M=3 R1(A 1, A 3 ) R2(A 2, A 4 ) R3(A 1, A 2, A 5 ) # of FD P=5 FD1, A 1 -> A 2 FD2, A 2, A 3 -> A 4 FD3, A 5 ->A 1 FD4, A 2, A 4 -> A 1, A 3 FD5, A 3 -> A 2, A 4

47 S-matrix 5x3-matrix A 1 A 2 A 3 A 4 A 5 R1a1 b(1,2) a3 b(1,4) b(1,5) R2b(2,1) a2 b(2,3) a4 b(2,5) R3a1 a2 b(3,3) b(3,4) a5

48 FD1. A 1 ->A 2 a1a2a3b(1,4)b(1,5) b(2,1)a2b(2,3) a4b(3,5) a1a2b(3,3)b(3,4) a5 FD2 A 2,A 3 -> A 4 a1a2a3b(1,4)b(1,5) b(2,1)a2b(2,3) a4b(3,5) a1a2b(3,3)b(3,4) a5

49 FD3 A 5 -> A 1 a1a2a3b(1,4)b(1,5) b(2,1)a2b(2,3) a4b(3,5) a1a2b(3,3)b(3,4) a5 FD4 A 2,A 4 -> A 1,A 3 a1a2a3b(1,4)b(1,5) b(2,1)a2b(2,3) a4b(3,5) a1a2b(3,3)b(3,4) a5

50 FD5 A 3 -> A 2, A 4 a1a2a3b(1,4)b(1,5) b(2,1)a2b(2,3) a4b(3,5) a1a2b(3,3)b(3,4) a5 // check if any single row has all “a” value // if not it is not a good decomposition

51 FD1, A 1 -> A 2 FD2, A 1, A 3 -> A 4, A 5 FD3, A 5 -> A 1, A 3 FD4, A 2, A 4 -> A 1, A 3 FD5, A 3 -> A 2, A 4 FD1. A1->A2 a1a2a3b(1,4)b(1,5) b(2,1)a2b(2,3) a4b(3,5) a1a2b(3,3)b(3,4) a5

52 FD2 A 1, A 3 -> A 4, A 5 a1a2a3b(1,4)b(1,5) b(2,1)a2b(2,3) a4b(3,5) a1a2b(3,3)b(3,4) a5 FD3 A 5 -> A 1, A 3 a1a2a3b(1,4)b(1,5) b(2,1)a2b(2,3) a4b(3,5) a1a2b(3,3)b(3,4) a5

53 FD4 A 2, A 4 -> A 1, A 3 a1a2a3b(1,4)b(1,5) b(2,1)a2b(2,3) a4b(3,5) a1a2b(3,3)b(3,4) a5 FD5 A 3 -> A 2, A 4 a1a2a3b(1,4)b(1,5) b(2,1)a2b(2,3) a4b(3,5) a1a2b(3,3)b(3,4) a5

54 R ( A1, A2, A3, A4, A5 ) S-Matrix 5x3 Because m = 3 and P = 5 A1 A2 A3 A4 A5 R1 a1 b(1,2) a3 b(1,4) b(1,5) R2 b(2,1) a2 b(2,3) a4 b(2,5) R3 a1 a2 b(3,3) b(3,4) a5 # of FD P = 5 FD 1 A1 A2 FD 2 A1, A3 A4, A5 FD 3 A5 A1 FD 4 A2, A4 A1, A3 FD 5 A3 A2, A4 n = 5 R( A1, A2, A3, A4, A5 ) m = 3 R1 ( A1, A3 ) R2 ( A2, A4 ) R3 ( A1, A2, A5 ) In Class Examples 02

55 FD1. A1 A2 A1 A2 A3 A4 A5 R1a1 a2 a3 b(1,4) b(1,5) R2 b(2,1) a2 b(2,3) a4 b(2,5) R3 a1 a2 b(3,3) b(3,4) a5 FD1. A1 A2 A1 A2 A3 A4 A5 R1a1 a2 a3 b(1,4) b(1,5) R2 b(2,1) a2 b(2,3) a4 b(2,5) R3 a1 a2 b(3,3) b(3,4) a5 FD2. A1, A3 A4, A5 A1 A2 A3 A4 A5 R1a1 a2 a3 b(1,4) b(1,5) R2 b(2,1) a2 b(2,3) a4 b(2,5) R3 a1 a2 b(3,3) b(3,4) a5 FD3. A5 A1 A1 A2 A3 A4 A5 R1a1 a2 a3 b(1,4) b(1,5) R2 b(2,1) a2 b(2,3) a4 b(2,5) R3 a1 a2 b(3,3) b(3,4) a5 FD4. A2, A4 A1, A3 A1 A2 A3 A4 A5 R1a1 a2 a3 b(1,4) b(1,5) R2 b(2,1) a2 b(2,3) a4 b(2,5) R3 a1 a2 b(3,3) b(3,4) a5 FD5. A3 A2, A4 A1 A2 A3 A4 A5 R1a1 a2 a3 b(1,4) b(1,5) R2 b(2,1) a2 b(2,3) a4 b(2,5) R3 a1 a2 b(3,3) b(3,4) a5

56 Normalization Consider algorithms for converting relations to BCNF or 3NF. If a relation schema is not in BCNF it is possible to obtain a lossless-join decomposition into a collection of BCNF relation schemas. Dependency-preserving is not guaranteed. 3NF There is always a dependency-preserving, lossless-join decomposition into a collection of 3NF relation schemas. Consider algorithms for converting relations to BCNF or 3NF. If a relation schema is not in BCNF it is possible to obtain a lossless-join decomposition into a collection of BCNF relation schemas. Dependency-preserving is not guaranteed. 3NF There is always a dependency-preserving, lossless-join decomposition into a collection of 3NF relation schemas.

57 BCNF Decomposition It is a lossless join decomposition. But not necessary dependency preserving It is a lossless join decomposition. But not necessary dependency preserving Suppose R is not in BCNF, A is an attribute, and X  A is a FD that violates the BCNF condition. 1. Remove A from R 2. Decompose R into XA and R-A 3. Repeat this process until all the relations become BCNF

58 3NF Synthesis Algorithm Note: result is lossless-join and dependency preserving Find a canonical cover F c for F ; result =  ; for each    in F c do if no schema in result contains  then add schema  to result; if no schema in result contains a candidate key for R then begin choose any candidate key  for R; add schema  to the result end

59

60

61 Design Goals Goal for a relational database design is: BCNF lossless join Dependency preservation If we cannot achieve this, we accept: 3NF lossless join Dependency preservation Goal for a relational database design is: BCNF lossless join Dependency preservation If we cannot achieve this, we accept: 3NF lossless join Dependency preservation


Download ppt "CS 157B Midterm 2 Revision Prof. Sin Min Lee. Q #1 (01) 1. Characterize the difference between the following pairs of terms a. Entity and entity class."

Similar presentations


Ads by Google