26
Recall • Why do we need transactions? • What is rollback and commit?

Introduction to mysql part 6

Embed Size (px)

Citation preview

Page 1: Introduction to mysql part 6

Recall

• Why do we need transactions?• What is rollback and commit?

Page 2: Introduction to mysql part 6

Introduction to MySQL

Database Normalization

Day 2,Week 7

Page 3: Introduction to mysql part 6

Relevance of normalization

• We could have store all the data in a single table like

• But there are several issues arise here

Emp_id Emp_name Emp_age Emp_email Fk_int_designation Vchr_Desig_roles

1000 Deepak 45 [email protected] Are Manager Handles all the district level operations

1001 Aneesh 23 [email protected] Sales person Will increase sales and ensure customer satisfaction

1002 Naveen 25 [email protected] Sales person Will increase sales and ensure customer satisfaction

1003 Jacob 25 [email protected] Administrator Handles all the district level operations

Page 4: Introduction to mysql part 6

Relevance of normalization

• We could have store all the data in a single table likeEmp_id Emp_name Emp_age Emp_email Fk_int_designation Vchr_Desig_roles

1000 Deepak 45 [email protected] Are Manager Handles all the district level operations

1001 Aneesh 23 [email protected] Sales person Will increase sales and ensure customer satisfaction

1002 Naveen 25 [email protected] Sales person Will increase sales and ensure customer satisfaction

1003 Jacob 25 [email protected] Administrator Handles all the district level operations

Here designation is repeated for all the rows and where ever it repeats the designation role also has to repeat. This redundancy takes a lot of space to store and

is inefficient

Page 5: Introduction to mysql part 6

Relevance of normalization

• We could have store all the data in a single table likeEmp_id Emp_name Emp_age Emp_email Fk_int_designation Vchr_Desig_roles

1000 Deepak 45 [email protected] Are Manager Handles all the district level operations

1001 Aneesh 23 [email protected] Sales person Will increase sales and ensure customer satisfaction

1002 Naveen 25 [email protected] Sales person Will increase sales and ensure customer satisfaction

1003 Jacob 25 [email protected] Administrator Handles all the district level operations

Suppose if i want to modify the roles of any designation, i will have to do the same for all records in the table. Its time consuming and not an effective way to follow

Page 6: Introduction to mysql part 6

Normalization

• Normalization is the process of moving data into related tables

• Will save data dependency and redundancy

• Save typing of repetitive data

• Reduce disk space

Page 7: Introduction to mysql part 6

Types of Normalization

• There are several levels of normalization– 1st Normalization– 2nd Normalization– 3rd Normalization

Page 8: Introduction to mysql part 6

First Normal Form

Page 9: Introduction to mysql part 6

First Normal Form

–Each field contains the smallest meaningful value– The table does not contain

repeating groups of fields or repeating data within the same field

• To attain 1st normalized form what you have to do is• Create a separate field/table for each set of related data. • Identify each set of related data with a primary key

Page 10: Introduction to mysql part 6

Table violating 1st Normal Form

Company (pk) BranchesBaabte Cochin, Calicut

baabtra Calicut,Trivandrum

It really is a bad setup that we are storing multiple values in a single column. For a table to be in 1st normalised form it should only have atomic values

Page 11: Introduction to mysql part 6

Table violating 1st Normal Form

Company (Pk) Branche1 Branch2Baabte Cochin Calicut

baabtra Calicut Trivandrum

It still in a bad set up , as we should not have multiple columns describing the same property of an entity.

Page 12: Introduction to mysql part 6

Table conforming to 1st Normal Form

Company (pk) Branch (pk)Baabte Cochin

Baabte Calicut

baabtra Calicut

baabtra Trivandrum

We have resulted in a 1st normalised table with primary key consists of two columns

Page 13: Introduction to mysql part 6

Second Normalisation

Page 14: Introduction to mysql part 6

Second Normalisation

–usually used in tables with a multiple-field primary key (composite key)–each non-key field relates to the

entire primary key–any field that does not relate to the

primary key is placed in a separate table

• To attain 1st normalized form what you have to do is• eliminate redundant data in a table • Create separate tables for sets of values that apply to multiple records

Page 15: Introduction to mysql part 6

Table violating 2nd Normal Form

Employee (pk) Skill (pk) WorkLocation

John IOS programmer Calicut

Mathew Java programmer Cochin

Thomas Android programmer Trivandrum

Alex PHP programmer Banglore

Ram Java programmer Mumbai

John PHP programmer Delhi

Soozan doNet programmer Calicut

Primary key= { Employee,Skill }

Page 16: Introduction to mysql part 6

Table violating 2nd Normal Form

Employee (pk) Skill (pk) WorkLocation

John IOS programmer Calicut

Mathew Java programmer Cochin

Thomas Android programmer Trivandrum

Alex PHP programmer Banglore

Ram Java programmer Mumbai

John PHP programmer Delhi

Soozan doNet programmer Calicut

Here non key attribute do not fully depends on compete primary key. IeWorkLocation depends only on Employee and doesn’t depend on Skill (partial

dependency)

Primary key= { Employee,Skill }

Page 17: Introduction to mysql part 6

Table conforming to 2nd Normal Form

Employee (pk) Skill (pk)

John IOS programmer

Mathew Java programmer

Thomas Android programmer

Alex PHP programmer

Ram Java programmer

John PHP programmer

Soozan doNet programmer

Employee (pk) WorkLocation

John Calicut

Mathew Cochin

Thomas Trivandrum

Alex Banglore

Ram Mumbai

John Delhi

Soozan Calicut

Page 18: Introduction to mysql part 6

Third Normalisation

Page 19: Introduction to mysql part 6

Third Normal Form

• Usually used in tables with a single field primary key

– records do not depend on anything other than a table's primary key

– each non-key field is a fact about the key

– Values in a record that are not part of that record's key do not belong in the table. In general, any time the contents of a group of fields may apply to more than a single record in the table, consider placing those fields in a separate table.

Page 20: Introduction to mysql part 6

EMPNO (Primary Key) FIRSTNAME LASTNAME WORKDEPTNO DEPTNAME

000290 John Parker OP11 Operations

000320 Ramlal Mehta SE21 Software Support

000310 Maude Setright OP11 Operations

Table violating 3rd Normal Form

Page 21: Introduction to mysql part 6

EMPNO (Primary Key) FIRSTNAME LASTNAME WORKDEPTNO DEPTNAME

000290 John Parker OP11 Operations

000320 Ramlal Mehta SE21 Software Support

000310 Maude Setright OP11 Operations

Table violating 3rd Normal Form

Here all the fields are not completely depend on Primary key.Ie Dept Name column only depends on DeptNo.And deptno depends on the EMPNO.

Ie its a transitive dependency. Which should be avoided

Page 22: Introduction to mysql part 6

Table conforming to 3rd Normal Form

EMPNO (Primary Key) FIRSTNAME LASTNAME WORKDEPTNO

000290 John Parker OP11

000320 Ramlal Mehta SE21

000310 Maude Setright OP11

WORKDEPTNO DEPTNAME

OP11 Operations

SE21 Software Support

OP11 Operations

Here all the fields are completely depend on Primary key.

Page 23: Introduction to mysql part 6

Questions?“A good question deserve a

good grade…”

Page 24: Introduction to mysql part 6

End of day2,Week 7

Page 25: Introduction to mysql part 6

If this presentation helped you, please visit our page facebook.com/baabtra and like it.

Thanks in advance. 

 www.baabtra.com | www.massbaab.com |www.baabte.com

Page 26: Introduction to mysql part 6

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]