CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is...

43
CSIS 254 CSIS 254 Oracle Normalization Oracle Normalization
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Transcript of CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is...

Page 1: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

CSIS 254 CSIS 254 Oracle NormalizationOracle Normalization

Page 2: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Relational DatabasesRelational Databases(Review)(Review)

• In relational databases, In relational databases, allall data is stored in tables, data is stored in tables, which correspond roughly to entitieswhich correspond roughly to entities

• Each table is two-dimensional, consisting of rows Each table is two-dimensional, consisting of rows and columnsand columns

• Each row in a table, called a Each row in a table, called a t-uplet-uple, corresponds to , corresponds to an occurrence of the entityan occurrence of the entity

• Columns in each table contain similar data across Columns in each table contain similar data across all rows in the tableall rows in the table

Page 3: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Relational Database ExampleRelational Database Example

The following table is an example of a relational table The following table is an example of a relational table describing classes that students have taken at a mythical describing classes that students have taken at a mythical college used in the rest of this lessoncollege used in the rest of this lesson

Student Student Course Student Student Course

Id Name Id Course Name Grade Term Teacher Id Name Id Course Name Grade Term Teacher

0194327 Joe Adams CSIS-840 VB Concepts C Spr-02 Wilkins0194327 Joe Adams CSIS-840 VB Concepts C Spr-02 Wilkins

0194327 Joe Adams CSIS-824 Intro to C++ B Fal-02 Smythe0194327 Joe Adams CSIS-824 Intro to C++ B Fal-02 Smythe

1850243 Joe Adams CSIS-740 Oracle Admin A Spr-03 Wallace1850243 Joe Adams CSIS-740 Oracle Admin A Spr-03 Wallace

1850243 Jane Smith CSIS-941 Systems Des. B Fal-02 Evans1850243 Jane Smith CSIS-941 Systems Des. B Fal-02 Evans

1850243 Jane Smith CSIS-840 VB Concepts B Spr-02 Wolkins1850243 Jane Smith CSIS-840 VB Concepts B Spr-02 Wolkins

8502432 Ida Know CSIS-184 Networks A Sum-03 Farmer8502432 Ida Know CSIS-184 Networks A Sum-03 Farmer

7402943 Eunice Eye CSIS-824 PowerPoint W Spr-02 Simpson7402943 Eunice Eye CSIS-824 PowerPoint W Spr-02 Simpson

Page 4: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Relational Database ExampleRelational Database Example

• Each row (or t-uple) in the table describes a Class Each row (or t-uple) in the table describes a Class taken by a Student in a term at our collegetaken by a Student in a term at our college

• The data in each column is consistent throughout the The data in each column is consistent throughout the tabletable

• However, there are three inconsistencies in the table However, there are three inconsistencies in the table itself. Can you find them?itself. Can you find them?

Page 5: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Primary Keys Primary Keys (Review)(Review)

• Each row in a table has a Each row in a table has a primary keyprimary key, which is the , which is the column or set of columns identified to our DBMS column or set of columns identified to our DBMS that uniquely identifies it from every other row in that uniquely identifies it from every other row in the tablethe table

• No attribute value in a primary key can be NULLNo attribute value in a primary key can be NULL

• A table can have only one primary keyA table can have only one primary key

• If a primary key is not specified, Oracle supplies oneIf a primary key is not specified, Oracle supplies one

• What would be the primary key for our sample What would be the primary key for our sample database?database?

Page 6: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Foreign KeysForeign Keys(Review)(Review)

• An attribute (or group of attributes) in a table can An attribute (or group of attributes) in a table can also be a also be a foreign keyforeign key, meaning that it references , meaning that it references the primary key (or at least unique attribute) to the primary key (or at least unique attribute) to another tableanother table

• An example would be a An example would be a Customer IdCustomer Id attribute on attribute on an invoice header, which would reference the an invoice header, which would reference the customer account information for that invoicecustomer account information for that invoice

Page 7: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

NormalizationNormalizationLet’s begin our discussion of normalization by using Let’s begin our discussion of normalization by using an example -- we want to expand the sample relational an example -- we want to expand the sample relational table for our mythical college by tracking data for:table for our mythical college by tracking data for:

– students students – coursescourses– departmentsdepartments– teachers teachers – classes (courses offered during a term)classes (courses offered during a term)– teachers assigned to each classteachers assigned to each class– students enrolled in each classstudents enrolled in each class

Page 8: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Database Normalization ExampleDatabase Normalization Example

We might start off We might start off with an entity for with an entity for each Class that looks each Class that looks something like thissomething like this

CLASSCLASS

Course IdCourse IdTerm OfferedTerm OfferedDepartment NameDepartment NameCourse DescriptionCourse DescriptionClassroom (or “Internet”)Classroom (or “Internet”)Credits / HoursCredits / HoursTeacher IdTeacher IdTeacher NameTeacher NameStudent #1 DataStudent #1 DataStudent #2 DataStudent #2 Data … …..Student #30 DataStudent #30 Data

Page 9: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Database Normalization ExampleDatabase Normalization Example

The information The information stored for each stored for each student would bestudent would be

What problems can What problems can you see with this you see with this scheme?scheme?

CLASS (exploded)CLASS (exploded)……Student #1 DataStudent #1 Data

IdIdFull NameFull Namee-mail Addressese-mail AddressesGrade for ClassGrade for ClassGPAGPA

Student #2 DataStudent #2 DataIdIdFull NameFull Namee-mail Addressese-mail AddressesGrade for ClassGrade for ClassGPA.GPA.

Page 10: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Problems with Our ExampleProblems with Our Example

• We can’t have more than 30 students in a classWe can’t have more than 30 students in a class

• There’s lots of duplicate information in our tablesThere’s lots of duplicate information in our tables– This design would require many updates whenever This design would require many updates whenever

a change was made to data about a department, a a change was made to data about a department, a teacher, a student, etc.teacher, a student, etc.

• Does it make sense for us to have to know, for Does it make sense for us to have to know, for example, a course number in order to to look up a example, a course number in order to to look up a teacher’s name?teacher’s name?

Page 11: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Problems with Our ExampleProblems with Our Example(continued)(continued)

• Removing a class entity occurrence might remove Removing a class entity occurrence might remove valuable information from our databasevaluable information from our database

• We don’t have any data verification checksWe don’t have any data verification checks– We might wind up with inconsistent data across We might wind up with inconsistent data across

two or more records (is this necessarily bad if two or more records (is this necessarily bad if we are trying to take snapshots?)we are trying to take snapshots?)

Page 12: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Normalization Goal #1Normalization Goal #1Remove redundant dataRemove redundant data

• Duplicated data wastes disk spaceDuplicated data wastes disk space

• Duplicated data may not necessarily be consistent, Duplicated data may not necessarily be consistent, that is, stored that is, stored in exactly the same wayin exactly the same way

• Redundant data creates problems for our codersRedundant data creates problems for our coders

– Ideally, data should be stored (and changed) Ideally, data should be stored (and changed) in in exactly the same wayexactly the same way in all locations, which not in all locations, which not only is time consuming for the system’s only is time consuming for the system’s programmers, but also takes computer resources programmers, but also takes computer resources to perform once the system is implementedto perform once the system is implemented

Page 13: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Normalization Goal #2Normalization Goal #2

Remove dependency issuesRemove dependency issues

• It is not intuitive for a user of our new system to It is not intuitive for a user of our new system to look in the CLASS entity to find, for example, a look in the CLASS entity to find, for example, a student’s email address.student’s email address.

• It would probably make more sense to move this It would probably make more sense to move this information into a separate entity (i.e., a database information into a separate entity (i.e., a database table that defines students).table that defines students).

Page 14: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

NormalizationNormalization The Bottom LineThe Bottom Line

““In summary, normal forms insure that In summary, normal forms insure that we do not compromise the integrity of we do not compromise the integrity of our data by either creating false data our data by either creating false data

or destroying true data.”or destroying true data.”

Ensor & StevensonEnsor & Stevenson

Page 15: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Forms of NormalizationForms of Normalization

• To accomplish these goals, we have created a set To accomplish these goals, we have created a set of rules which define of rules which define normal forms normal forms oror levels levels..

• There are five normal forms, each progressively There are five normal forms, each progressively more restrictive, which are called more restrictive, which are called first normal first normal form (1NF)form (1NF), , second normal form (2NF)second normal form (2NF), …, …

• Most database designers only consider the first Most database designers only consider the first three forms in their work, as we willthree forms in their work, as we will

• As we shall see, there might be good reasons to As we shall see, there might be good reasons to deviate from these normal formsdeviate from these normal forms

Page 16: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

First Normal Form (1NF)First Normal Form (1NF)• A database is in A database is in first normal formfirst normal form (1NF) if each (1NF) if each

attribute of the database is simple, single-valued attribute of the database is simple, single-valued (atomic), and does not repeat(atomic), and does not repeat– Let’s assume column definitions are consistent across rowsLet’s assume column definitions are consistent across rows

• Method:Method:– Reduce all attributes into atomic componentsReduce all attributes into atomic components

– Eliminate duplicative columns (repeating groups) and Eliminate duplicative columns (repeating groups) and multi-valued attributes from the same tablemulti-valued attributes from the same table

– Create a separate table for each group of related dataCreate a separate table for each group of related data

– Identify each row with a unique column or set of columns Identify each row with a unique column or set of columns (a primary key)(a primary key)

Page 17: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Our Sample DatabaseOur Sample Database

Here’s what our Here’s what our database entity for database entity for classes at our college classes at our college currently looks likecurrently looks like

CLASSCLASS

Course IdCourse IdTerm OfferedTerm OfferedDepartment NameDepartment NameCourse DescriptionCourse DescriptionClassroom (or “Internet”)Classroom (or “Internet”)Credits / HoursCredits / HoursTeacher IdTeacher IdTeacher NameTeacher NameStudent #1 DataStudent #1 DataStudent #2 DataStudent #2 Data … …..Student #30 DataStudent #30 Data

Page 18: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Our Sample Database in 1NFOur Sample Database in 1NF

We should divide the We should divide the Course Id into a Course Id into a Department Id and Department Id and Course Number (e.g., Course Number (e.g., Course ID “CSIS-254” Course ID “CSIS-254” would be divided into would be divided into Department Id “CSIS”, Department Id “CSIS”, Course Number “254”)Course Number “254”)

(Won’t this make the (Won’t this make the Department Name Department Name redundant?)redundant?)

CLASSCLASSDepartment IdDepartment Id ( (addedadded))Course Number Course Number ((addedadded))Term OfferedTerm OfferedDepartment NameDepartment NameCourse DescriptionCourse DescriptionClassroom (or “Internet”)Classroom (or “Internet”)Credits / HoursCredits / HoursTeacher IdTeacher IdTeacher Last NameTeacher Last NameStudent #1 DataStudent #1 DataStudent #2 DataStudent #2 Data … …..Student #30 DataStudent #30 Data

Page 19: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Our Sample Database in 1NFOur Sample Database in 1NF

Next, break out Next, break out Student Ids, Student Ids, Names, e-mail Names, e-mail Address, and Address, and Grades into a Grades into a separate entity, separate entity, eliminating the eliminating the repeating Student repeating Student groups.groups.

Department IdDepartment IdCourse NumberCourse NumberTerm OfferedTerm OfferedStudent IdStudent Id Student Full NameStudent Full NameStudent e-mail AddressesStudent e-mail AddressesStudent Grade for ClassStudent Grade for ClassStudent GPAStudent GPA

CLASS / STUDENTCLASS / STUDENT

Page 20: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Our Sample Database in 1NFOur Sample Database in 1NF

We need to break We need to break down the down the Student’s Names Student’s Names into their simpler into their simpler componentscomponents

Department NumberDepartment NumberCourse NumberCourse NumberTerm OfferedTerm OfferedStudent IdStudent Id Student Full NameStudent Full Name

First NameFirst NameMiddle NameMiddle NameLast NameLast Name

Student e-mail AddressesStudent e-mail AddressesStudent Grade for ClassStudent Grade for ClassStudent GPAStudent GPA

CLASS / STUDENTCLASS / STUDENT

Page 21: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Our Sample Database in 1NFOur Sample Database in 1NF

Finally, we need to Finally, we need to break out Student e-break out Student e-mail Addresses into mail Addresses into another entity, another entity, where each where each occurrence occurrence represents a single represents a single e-mail addresse-mail address

Department IdDepartment IdCourse NumberCourse NumberTerm OfferedTerm OfferedStudent IdStudent Id Address Number or IdAddress Number or IdStudent e-mail AddressStudent e-mail Address

CLASS / STUDENTCLASS / STUDENT E-MAIL ADDRESSE-MAIL ADDRESS

Page 22: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Our Sample Database in 1NFOur Sample Database in 1NF

Department IdDepartment Id Course NumberCourse NumberTerm OfferedTerm OfferedDepartment NameDepartment NameCourse DescriptionCourse DescriptionClassroom (or “Internet”)Classroom (or “Internet”)Credits / HoursCredits / HoursTeacher IdTeacher IdTeacher Last NameTeacher Last Name

CLASSCLASS CLASS / STUDENTCLASS / STUDENT

Department IdDepartment IdCourse NumberCourse NumberTerm OfferedTerm OfferedStudent IdStudent Id Student Full NameStudent Full Name

First NameFirst NameMiddle NameMiddle NameLast NameLast Name

Student Grade for ClassStudent Grade for ClassStudent GPAStudent GPA

Page 23: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Our Sample Database in 1NFOur Sample Database in 1NF

CLASS / STUDENTCLASS / STUDENT

Department IdDepartment IdCourse NumberCourse NumberTerm OfferedTerm OfferedStudent IdStudent Id Student Full NameStudent Full Name

First NameFirst NameMiddle NameMiddle NameLast NameLast Name

Student Grade for ClassStudent Grade for ClassStudent GPAStudent GPA

CLASS / STUDENTCLASS / STUDENT E-MAIL ADDRESSE-MAIL ADDRESS

Department IdDepartment IdCourse NumberCourse NumberTerm OfferedTerm OfferedStudent IdStudent Id Address Number or IdAddress Number or IdStudent e-mail AddressStudent e-mail Address

Page 24: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

1NF Advantages1NF Advantages

• Removes limits artificially introduced into a Removes limits artificially introduced into a database design by using repeating groupsdatabase design by using repeating groups

• Ensures that attributes are broken into their most Ensures that attributes are broken into their most basic units and are not multi-valuedbasic units and are not multi-valued

Page 25: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

ExerciseExercise

Put the following Put the following table in 1NF, then table in 1NF, then draw an ERD for draw an ERD for your new systemyour new system

FAVORITE TV SHOWSFAVORITE TV SHOWS

TV Show NameTV Show NameCategoryCategoryMain Star Name #1Main Star Name #1Main Star Name #2Main Star Name #2Main Star Name #3Main Star Name #3Day and Time ShownDay and Time ShownNetworkNetworkChannelChannelMy Rating (1-10)My Rating (1-10)

Page 26: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

One Possible AnswerOne Possible Answer

FAVORITE TV SHOWSFAVORITE TV SHOWS

TV Show NameTV Show NameCategoryCategoryMy Rating (1-10)My Rating (1-10)

SHOW / STARSSHOW / STARSTV Show NameTV Show NameStar NumberStar NumberStar NameStar Name

SHOW TIMESSHOW TIMESTV Show NameTV Show NameSlot NumberSlot NumberDate and TimeDate and TimeNetworkNetworkChannelChannel

Page 27: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Second Normal Form (2NF)Second Normal Form (2NF)

• 2NF implies 1NF by definition2NF implies 1NF by definition

• All non-key attributes must be fully-dependent on All non-key attributes must be fully-dependent on every key attribute in the primary key every key attribute in the primary key – In other words, a non-key attribute cannot depend on only In other words, a non-key attribute cannot depend on only

part of the primary keypart of the primary key

– This restriction applies only to tables with composite keysThis restriction applies only to tables with composite keys

• 2NF reduces redundant data in a table by extracting 2NF reduces redundant data in a table by extracting it, placing it in new table(s), then creating it, placing it in new table(s), then creating relationships between those tables.relationships between those tables.

Page 28: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Second Normal Form (2NF)Second Normal Form (2NF)

• Method:Method:

– Remove subsets of data that appear in multiple Remove subsets of data that appear in multiple rows of a table, and place into separate tablesrows of a table, and place into separate tables

– Create relationships between these new tables Create relationships between these new tables and their predecessors through the use of and their predecessors through the use of foreign keys.foreign keys.

Page 29: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Our Sample Database in 2NFOur Sample Database in 2NF

We can break out We can break out the Department the Department Name from the Name from the CLASS entity, as it CLASS entity, as it will be the same for will be the same for each Class having each Class having the same the same DepartmentDepartment

Department IdDepartment IdDepartment NameDepartment Name

DEPARTMENTDEPARTMENT

Page 30: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Our Sample Database in 2NFOur Sample Database in 2NF

We also can break We also can break out the Course out the Course Description from Description from this entity, as it also this entity, as it also will be the same for will be the same for each Class each Class referencing the referencing the same Coursesame Course

Department IdDepartment IdCourse NumberCourse NumberCourse DescriptionCourse DescriptionCredits / HoursCredits / Hours

COURSECOURSE

Note that we’ve kept the Note that we’ve kept the Department Id in thisDepartment Id in thisentity. Why?entity. Why?

Page 31: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Our Sample Database in 2NFOur Sample Database in 2NF

We can also break We can also break out the information out the information about each Teacher, about each Teacher, since it also will be since it also will be the same for each the same for each Class that a Teacher Class that a Teacher conducts, conducts, irrespective of the irrespective of the ClassClass

Teacher IdTeacher IdTeacher Last NameTeacher Last Name

TEACHERTEACHER

Page 32: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Our Sample Database in 2NFOur Sample Database in 2NF

Our new CLASS / Our new CLASS / STUDENT entity can STUDENT entity can also have its student-also have its student-related attributes (names, related attributes (names, and GPA) broken out, and GPA) broken out, that is, attributes that do that is, attributes that do not change with the class not change with the class numbernumber

Student IdStudent IdStudent Full NameStudent Full Name

First NameFirst NameMiddle NameMiddle NameLast NameLast Name

Student GPAStudent GPA

STUDENTSTUDENT

Page 33: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Our Sample Database in 1NFOur Sample Database in 1NF

Student e-mail Student e-mail Addresses are not Addresses are not dependent upon dependent upon Department Id, Department Id, Course Number, or Course Number, or Term, so remove Term, so remove them from the e-them from the e-mail entitymail entity

Department IdDepartment Id (deleted) (deleted)Course NumberCourse Number (deleted) (deleted)Term OfferedTerm Offered (deleted (deleted))Student IdStudent Id Address Number or IdAddress Number or IdStudent e-mail AddressStudent e-mail Address

STUDENTSTUDENT E-MAIL ADDRESSE-MAIL ADDRESS

Page 34: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Our Sample Database in 2NFOur Sample Database in 2NF

Our final CLASS / Our final CLASS / STUDENT entity, minus STUDENT entity, minus all of the attributes that all of the attributes that have been moved to have been moved to other entities, looks likeother entities, looks like

Department IdDepartment IdCourse IdCourse IdStudent IdStudent IdTermTermStudent Grade for ClassStudent Grade for Class

CLASS / STUDENTCLASS / STUDENT

Page 35: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

2NF and Foreign Keys2NF and Foreign Keys

• To ensure data integrity, we would implement four To ensure data integrity, we would implement four foreign keys in our CLASS, CLASS / STUDENTforeign keys in our CLASS, CLASS / STUDENT– Department Id must reference an occurrence in Department Id must reference an occurrence in

DEPARTMENT entityDEPARTMENT entity

– Course Id must reference a row in COURSECourse Id must reference a row in COURSE

– Student Id must reference a row in STUDENTStudent Id must reference a row in STUDENT

– Teacher Id must reference a row in TEACHERTeacher Id must reference a row in TEACHER

• Would we implement a similar restriction on our Would we implement a similar restriction on our student e-mail address entity?student e-mail address entity?

Page 36: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

2NF Advantages2NF Advantages• All advantages of 1NFAll advantages of 1NF

• Common data is forced to be consistent, since it is Common data is forced to be consistent, since it is stored in only one place in the databasestored in only one place in the database

• We can store data about separate entities without We can store data about separate entities without implying the existence of othersimplying the existence of others– In our original database design, we can’t store In our original database design, we can’t store

information about Students, Teachers, or Departments information about Students, Teachers, or Departments if we don’t have any classes in which they are if we don’t have any classes in which they are involved.involved.

Page 37: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

ExerciseExercise

Convert the Convert the following following table into table into 2NF, and 2NF, and draw a new draw a new ERDERD

SALES ORDERSALES ORDER

Order Number Order Number Customer Account NumberCustomer Account Number Customer Account NameCustomer Account Name Customer AddressCustomer Address Date of Entry of OrderDate of Entry of Order Date of Requested ShipmentDate of Requested Shipment Item NumbersItem Numbers Item DescriptionsItem Descriptions Quantities OrderedQuantities Ordered Unit PricesUnit Prices Extended PricesExtended Prices Total Order PriceTotal Order Price

Page 38: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Third Normal Form (3NF)Third Normal Form (3NF)

• 3NF implies 2NF (which implies 1NF)3NF implies 2NF (which implies 1NF)

• A database is in thirdA database is in third normal form normal form (3NF) if the (3NF) if the data in every column of each row (occurrence) in data in every column of each row (occurrence) in a table (entity) is dependent ONLY upon each a table (entity) is dependent ONLY upon each column in the keycolumn in the key– In general, any time the contents of a group of fields In general, any time the contents of a group of fields

may apply to more than a single record in the table, may apply to more than a single record in the table, consider placing those fields in a separate table. consider placing those fields in a separate table.

– This means that derived attributes are not allowed in This means that derived attributes are not allowed in 3NF3NF

Page 39: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Third Normal Form (3NF)Third Normal Form (3NF)

• All attributes depend upon the key, the whole key, All attributes depend upon the key, the whole key, and nothing but the keyand nothing but the key

• Method:Method:– Remove all derived columnsRemove all derived columns– Move all remaining columns not dependent on Move all remaining columns not dependent on

the key into a new tablethe key into a new table

Page 40: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Our Sample Database in 3NFOur Sample Database in 3NF

Our STUDENT entity Our STUDENT entity cannot contain a GPA, cannot contain a GPA, since that is a derived since that is a derived attribute (the average of attribute (the average of all of the Grades all of the Grades received)received)

Student IdStudent IdStudent NamesStudent Names

First NameFirst NameMiddle NameMiddle NameLast NameLast Name

Student GPA (deleted)Student GPA (deleted)

STUDENTSTUDENT

Page 41: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Advantages of 3NFAdvantages of 3NF

• All advantages of 1NF and 2NFAll advantages of 1NF and 2NF

• Information is stored in one and only one place in Information is stored in one and only one place in the databasethe database

• All entities are now 2-dimensional, non-redundant, All entities are now 2-dimensional, non-redundant, and can be implemented in relational tablesand can be implemented in relational tables

Page 42: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Disadvantages of NormalizationDisadvantages of Normalization

• Proliferation of tables, resulting in increased Proliferation of tables, resulting in increased system complexitysystem complexity– Can be overcome with views for end-usersCan be overcome with views for end-users

• Performance hits through added tables and lack of Performance hits through added tables and lack of derived attributesderived attributes– May be partially offset by reduced computing May be partially offset by reduced computing

needs of maintaining data only onceneeds of maintaining data only once

• We will discuss these in detail next week...We will discuss these in detail next week...

Page 43: CSIS 254 Oracle Normalization. Relational Databases (Review) In relational databases, all data is stored in tables, which correspond roughly to entitiesIn.

Last SlideLast SlideNext Week’s AssignmentNext Week’s Assignment

• Draw a complete ERD for our normalized 3NF Draw a complete ERD for our normalized 3NF mythical college database. Does it make sense to you?mythical college database. Does it make sense to you?

• Normalize the two organizations / systems that you Normalize the two organizations / systems that you used in last week’s homework by updating their used in last week’s homework by updating their ERD’s (Engineering Method only).ERD’s (Engineering Method only).

• Introduce at least two derived attributes that you might Introduce at least two derived attributes that you might include in your design, and explain why.include in your design, and explain why.

• Prepare for a quiz next week on what we have covered Prepare for a quiz next week on what we have covered so far in class:so far in class:

Stages of SDLC, Entities, Attributes, Relationships, Stages of SDLC, Entities, Attributes, Relationships, Diagramming, and NormalizationDiagramming, and Normalization