Presentation is loading. Please wait.

Presentation is loading. Please wait.

Translating ER into Relations; Normalization

Similar presentations


Presentation on theme: "Translating ER into Relations; Normalization"— Presentation transcript:

1 Translating ER into Relations; Normalization
INF1343, Week 5 Translating ER into Relations; Normalization Yuri Takhteyev University of Toronto January 31, 2011 This presentation is licensed under Creative Commons Attribution License, v To view a copy of this license, visit This presentation incorporates images from the Crystal Clear icon collection by Everaldo Coelho, available under LGPL from

2 building room probably redundant student course might be redundant
code map url room number capacity probably redundant student last name first name course code title might be redundant session code start date

3 building room student course session code map url number capacity
code map url room number capacity room is in building course meets in room student last name first name course code title student enrolls in course course is offered in session session code start date

4 building room student course session code map url number capacity
code map url room number capacity student last name first name course code title session code start date

5 building room student course session code map url number capacity
code map url room number capacity student last name first name course code title session code start date

6 building room student course session enrollment code map url number
code map url room number capacity student last name first name enrollment course code title session code start date

7 building room course student session enrollment code map url number
code map url room number capacity enrollment course code title student last name first name session code start date

8 building room course student session course enrollment instance code
code map url room number capacity enrollment course code title course instance student last name first name session code start date

9 building room course student session course enrollment instance code
code map url room number capacity enrollment course code title course instance student last name first name session code start date

10 building room course student session course enrollment instance code
code map url room number capacity enrollment course code title course instance student last name first name session code start date

11 building room course student session course enrollment instance code
code map url room number capacity enrollment course instance course code title student last name first name session code start date

12 building room course student session course enrollment instance code
code map url room number capacity enrollment course instance course code title student last name first name session code start date

13 building room course student session enrollment course instance code
code map url room number capacity enrollment course instance final exam? course code title student last name first name session code start date

14 building room course student session enrollment course instance code
code map url room number capacity enrollment course instance final exam? course code title student last name first name session code start date

15 ER for M

16 Entities: → tables 1:M relationships: → primary and foreign keys
ER to Relations Entities: → tables 1:M relationships: → primary and foreign keys

17 “Create one table for each entity” (after breaking up M:M)
Step 1 “Create one table for each entity” (after breaking up M:M)

18 student first_name last_name student last name first name
last name first name student first_name last_name

19 student name domain last_name first_name student last name first name
last name first name student name domain last_name first_name

20 student name domain last_name varchar(100) first_name student
last name first name student name domain last_name varchar(100) first_name

21 student name domain NULL last_name varchar(100) YES first_name NO
last name first name student name domain NULL last_name varchar(100) YES first_name NO

22 (do this for every entity)

23 Creating primary and foreign keys to represent relationships
Steps 2-4 Creating primary and foreign keys to represent relationships

24 building name domain NULL code varchar(100) NO map_url YES room name
code map url room number capacity building name domain NULL code varchar(100) NO map_url YES room name domain NULL number integer NO capacity YES

25 building name domain NULL code varchar(100) NO map_url YES room name
code map url room number capacity building name domain NULL code varchar(100) NO map_url YES room name domain NULL number integer NO capacity YES building_???

26 Keys student name last_name first_name no key!

27 Keys student name last_name first_name student_no a candidate key

28 Keys another candidate key name last_name first_name student
student_no utorid another candidate key

29 Keys candidate #1 candidate #2 name last_name first_name student
student_no utorid candidate #1 candidate #2

30 Keys primary key candidate #1 candidate #1 candidate #2 name last_name
student name last_name first_name student_no* utorid primary key candidate #1 candidate #1 candidate #2

31 Multi-Column Keys city name province population a key!

32 Multi-Column Keys a three column key name province city country
population a three column key

33 Multi-Column Keys candidate #1 candidate #2 name province city country
population city_id candidate #1 candidate #2

34 “Natural” Keys building name code map_url a “natural” key e.g., “BL”

35 “Natural” Keys a “natural” key a “surrogate” key name code map_url
building name code map_url building_id a “natural” key e.g., “BL” a “surrogate” key e.g., 56

36 Step 2 “For each entity that is only on the ‘one’ end of one or more
relationships and not at the ‘many’ end of any relationship, create a single-column primary key... if no natural primary key is available.”

37 Step 2 “For each entity that is only on the ‘one’ end of one or more
relationships and not at the ‘many’ end of any relationship, create a single-column primary key... if no natural primary key is available.”

38 building room course student session enrollment course instance code
code map url room number capacity enrollment course instance final exam? course code title student last name first name session code start date

39 building room course student session enrollment course instance code
code map url room number capacity enrollment course instance final exam? course code title student last name first name session code start date

40 building room course student session enrollment course instance code
code map url room number capacity enrollment course instance final exam? course code title student last name first name session code start date

41 building room course student session enrollment course instance code
code map url room number capacity enrollment course instance final exam? course code title student last name first name session code start date

42 building room course student session enrollment course instance code
code map url room number capacity enrollment course instance final exam? course code title student last name first name session code start date

43 Step 2 “For each entity that is only on the ‘one’ end of one or more
relationships and not at the ‘many’ end of any relationship, create a single-column primary key... if no natural primary key is available.”

44 student name last_name first_name

45 student name last_name first_name student_id* let’s add it

46 student name type null key last_name varchar(100) YES first_name NO student_id int PRI

47 building room course student session enrollment course instance code
code map url room number capacity enrollment course instance final exam? course code title student last name first name session code start date

48 building room course student session enrollment course instance code*
code* map url room number capacity enrollment course instance final exam? course code* title student last name first name student_id* session code* start date

49 building room course student session enrollment course instance code
code map url building_id* room number capacity course code title course_id* enrollment course instance final exam? student last name first name student_id* session code start date session_id*

50 Step 3 “For each entity that is at the ‘many’ end of one or more
relationships, include the primary key of each parent entity in the table as foreign keys.”

51 Step 3 “For each entity that is at the ‘many’ end of one or more
relationships, include the primary key of each parent entity in the table as foreign keys.”

52 building room course student session enrollment course instance code
code map url building_id* room number capacity course code title course_id* enrollment course instance final exam? student last name first name student_id* session code start date session_id*

53 building room course student session enrollment course instance code
code map url building_id* room number capacity course code title course_id* enrollment course instance final exam? student last name first name student_id* session code start date session_id

54 Step 3 “For each entity that is at the ‘many’ end of one or more
relationships, include the primary key of each parent entity in the table as foreign keys.”

55 building room course student session enrollment course instance code
code map url building_id* room number capacity course code title course_id* enrollment course instance final exam? student last name first name student_id* session code start date session_id*

56 building room course student session enrollment course instance code
code map url building_id* room number capacity course code title course_id* enrollment course instance final exam? student last name first name student_id* session code start date session_id

57 building room course student session enrollment course instance code
code map url building_id* room number capacity course code title course_id* enrollment course instance final exam? student last name first name student_id* session code start date session_id*

58 building room course student session course instance enrollment code
code map url building_id* room number capacity building_id course instance final exam? course_id session_id course code title course_id* enrollment student_id student last name first name student_id* session code start date session_id*

59 building room course student session course instance enrollment code
code map url building_id* room number capacity building_id course instance final exam? course_id session_id course code title course_id* enrollment student_id student last name first name student_id* session code start date session_id*

60 Step 4a “If an entity at the ‘many’ end of one or more relationships, has a natural primary key, use that single key as a the primary key.”

61 ? building room course student session course instance enrollment code
code map url building_id* room number capacity building_id ? course instance final exam? course_id session_id course code title course_id* enrollment student_id student last name first name student_id* session code start date session_id*

62 Step 4b “Otherwise, concatenate the primary key of its parent with any
other column or columns needed for uniqueness to form the table’s primary key.”

63 building_id + number building room course student session course
code map url building_id* room number capacity building_id building_id + number course instance final exam? course_id session_id course code title course_id* enrollment student_id student last name first name student_id* session code start date session_id*

64 If necessary, add a field!
Step 4c If necessary, add a field!

65 building room course student session course instance enrollment code
code map url building_id* room code capacity building_id room_id* course instance final exam? course_id session_id course code title course_id* enrollment student_id student last name first name student_id* session code start date session_id*

66 building room course student session course instance enrollment code
code map url building_id* room code capacity building_id room_id* course instance final exam? course_id session_id course code title course_id* enrollment student_id student last name first name student_id* session code start date session_id*

67 Step 4d Go back to step 3!

68 building room course student session course instance enrollment code
code map url building_id* room code capacity building_id room_id* course instance final exam? course_id session_id room_id course code title course_id* enrollment student_id student last name first name student_id* session code start date session_id*

69 building room course student session course instance enrollment code
code map url building_id* room code capacity building_id room_id* course instance final exam? course_id* session_id* room_id* course code title course_id* enrollment student_id student last name first name student_id* session code start date session_id*

70 building room course student session course instance enrollment code
code map url building_id* room code capacity building_id room_id* course instance final exam? course_id session_id room_id instance_id* course code title course_id* enrollment student_id* instance_id* student last name first name student_id* session code start date session_id*

71 building room course student session course instance enrollment code
code map url building_id* room code capacity building_id room_id* course instance final exam? course_id session_id room_id instance_id* course code title course_id* enrollment student_id* instance_id* student last name first name student_id* session code start date session_id*

72 course_instance name type null key instance_id int NO PRI course_id session_id room_id final_exam bool

73 building room course student session course instance enrollment code
code map url building_id* room code capacity building_id room_id* course instance final exam? course_id session_id room_id instance_id* course code title course_id* enrollment student_id* instance_id* student last name first name student_id* session code start date session_id*

74 course_instance name type null key instance_id int NO PRI course_id session_id room_id YES final_exam bool

75 Step 5 Normalize!

76 Checking that the table does not have any well-known problems
Normalization Checking that the table does not have any well-known problems

77 What Problems? problem type cause solution update redundancy
decomposition insertion deletion

78 Normal Forms 5th Normal Form 4th Normal Form BC Normal Form
fixing weird issues 4th Normal Form BC Normal Form 3rd Normal Form you can do it! 2nd Normal Form 1st Normal Form trivial!

79 No multi-valued attributes
1NF No multi-valued attributes

80 pet_id owner names 1 Bob “Slim”, “the Serpent”, “Ribbon” 2 Gwen “Fluffy”, “Big Dog”

81 The Wrong Solution pet_id owner name1 name2 name3 1 Bob “Slim”
“the Serpent” “Ribbon” 2 Gwen “Fluffy” “Big Dog”

82 The Right Solution pet_id owner 1 Bob 2 Gwen pet_id names 1 Slim
the Serpent Ribbon 2 Fluffy Big Dog

83 Getting rid of “functional dependencies”
2NF and 3NF Getting rid of “functional dependencies”

84 “Each attribute must describe the key,
student id course id course title student name grade code grade points “Each attribute must describe the key, the whole key, and nothing but the key.” “So help me Codd!”

85 “Each attribute must describe the key,
partial dependency student id course id course title student name grade code full dependency grade points transitive dependency “Each attribute must describe the key, the whole key, and nothing but the key.” “So help me Codd!”

86 2NF and 3NF 2NF: no partial dependencies (“the whole key”) 3NF:
2NF + no transitive dependencies (“nothing but the key”)

87 Decomposition (break up tables into several)
Reaching 2NF and 3NF Decomposition (break up tables into several)

88 student id course id course title student name grade code grade points

89 student id course student id student name course id grade code grade points course id course title

90 student id course student id student name course id grade code course id course title grade code grade points

91 “all determinants must be candidate keys”
BCNF “all determinants must be candidate keys”

92 overlapping candidate keys
room id course time slot instructor id course code overlapping candidate keys

93 dependencies between keys
room id course time slot instructor id dependencies between keys

94 dependencies between keys
room id course time slot instructor id dependencies between keys

95 this is 3NF, but not BCNF (“so help me Codd”) instructor utorid course
time slot instructor id this is 3NF, but not BCNF (“so help me Codd”)

96 decompose it instructor id time slot course instructor id
instructor utorid decompose it

97 some relations cannot be decomposed into BCNF
student id student, course → project project → course course id course project id some relations cannot be decomposed into BCNF (“so help me Codd”)

98 Mistaking binary relationships for ternary
4NF Mistaking binary relationships for ternary

99 room course session code capacity code title code start date
code capacity course code title session code start date

100 room course session course instance code capacity code title code
code capacity course code title course instance session code start date

101 name student_id course_id internship_id

102 “We could break it up yet more”
5NF “We could break it up yet more”

103 Questions?


Download ppt "Translating ER into Relations; Normalization"

Similar presentations


Ads by Google