34
May 15, 2022 Varun Arora, SICSR 2007-'09 1 Database Management System Keys and Normalization By: Varun Arora [email protected] www.varunarora.in

Database Management System

Embed Size (px)

DESCRIPTION

The basics of DBMS..

Citation preview

Page 1: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 1

Database Management System

Keys and Normalization

By:Varun [email protected]

Page 2: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 2

Keys

Keys (in ERD’s) are certain attributes which can be used to identify instances of that entity.

Can be of many types namely – Candidate Keys Super Keys or Composite Keys Primary Keys Foreign Keys

Are useful in any System.

Page 3: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 3

Candidate Keys

Candidate Keys are those attributes which can be used for the purpose of developing some higher level keys. (Super keys, Primary keys or foreign keys)

Example – Purchase Order Master Table.

Page 4: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 4

Super Keys

Super keys are those keys which are formed to uniquely identify the instances as well as the attributes of an entity and are formed as a result of the combination of one or more attributes of that entity.

Example – Purchase Order Details Table

Page 5: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 5

Primary Key

Primary key is that candidate key which is used to uniquely identify each and every attribute and instance of a particular entity.

Example – Purchase Order Master Table

Page 6: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 6

Foreign Key

A Foreign key is an attribute in some another entity which is already a primary key in some entity. Is used for the purpose of maintaining the integrity of the data.

Example – The Purchase Order Master and the Purchase Order Detail Relationship.

Page 7: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 7

Keys - Purpose

Purpose of Identifying attributes as some or the other kind of a key is – Unique Identification of records Easy Maintenance of the Integrity of Data Help in the searching of the data based on

their relationships of Primary – Foreign Key and thus facilitate easy querying at the top level. (this issue is excluded at the point of design.)

Page 8: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 8

Sample ER (Purchase Order)

ContainsPurchase Order Master Purchase Order Detail

Po_no (PK)Po_dtPo_amtSupp_cd…….

Po_no (FK)Item_cd (FK)quantityUnit_price…….

Po_no

Po_dt

Supp_cd

Po_amt

Po_no

Item_cd

quantity

Unit_price

1 M

Page 9: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 9

Extended E-R Features

ER Diagrams can be further used to generate views regarding overall hierarchy of the Entity Association (Relationships). This can be done using these concepts – Specialization Generalization.

Page 10: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 10

Specialization

It is a concept from which several sub-entities can be derived which have certain attributes different from the common entity (or parent entity) from which they are derived.

Example – Account ISA Savings or Account ISA Current Account

Page 11: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 11

Generalization

It is a concept wherein the entities having a common set of attributes are grouped together to form a common entity.

Example – Confirmed Ticket ISA Ticket or Waiting Ticket ISA Ticket.

Page 12: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 12

Normalization..Topics Functional Dependency 1st Normal Form 2nd Normal Form 3rd Normal Form 4th or the Boyce Codd Normal Form

(BCNF) Denormalization Need for Normalization (and

Denormalization)

Page 13: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 13

Functional Dependency A functional dependency, denoted by X-

to-Y (X->Y) between two sets of attributes X and Y that are subsets of R specifies a constraint on the possible tuples that can form a relation instance r of R. It therefore states that , for any tuples t1 and t2 if t1[X] = t2[X] then t1[Y] = t2[Y]. That is the

values of X Component uniquely determine the values of the Y component.

Page 14: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 14

Functional Dependency..Example

Roll No Name Marks %age

R001 Raj 300 50%

R002 Raj 360 60%

R003 Rahul 420 70%

In this table the values in the attributes ‘Name’‘Marks’ and ‘%age’ are functionally dependent onthe values in the attribute ‘Roll No’ because ifthe value of ‘Roll No’ changes so does the valueof the other attributes changes.

Page 15: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 15

Normalization

Normalization of data can be looked on as a process during which unsatisfactory relation schemas are decomposed by breaking up their attributes into smaller relation schemas that possess the desirable properties. The various stages or tests that can be

performed to achieve this are called as the Normal Forms and are available as 1NF, 2NF, 3NF, BCNF.

Page 16: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 16

1st Normal Form

A Relation is said to be in the 1st Normal Form when the “attributes contain values that are atomic in nature”. That is, it disallows the relations to contain relation within relations.

In general sense, a Relation is said to be in the 1st Normal Form when the attributes are single valued.

Page 17: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 17

1st Normal Form - Example

Ecode Ename Ecity Ephone

E001 Raj AhmedabadPuneMumbai

23445672344568

E002 Rahul Udaipur 23344452334455

E003 Ramesh Jaipur 2345678

Denormalized Data consisting of Attributes with more than one values.

Page 18: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 18

1st Normal Form - Example

ECode Ename Ecity Ephone

E001 Raj Ahmedabad 2344567

E001 Raj Pune 2344568

E001 Raj Mumbai

E002 Rahul Udaipur 2334445

E002 Rahul Udaipur 2334455

E003 Ramesh Jaipur 2345678

Attributes Containing Atomic Values

Page 19: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 19

2nd Normal Form

A Relation is said to be in the 2nd Normal Form if all the attributes of the Relation are fully functionally dependent on the whole attribute and not just a part of the attribute.

In general sense, it can be stated that a relation is in a 2nd Normal form if all attributes depend on the whole key and not just a part of the key.

Page 20: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 20

2nd Normal Form - Example

Ecode Ename Dept PrjCd PrjHrs

E001 Raj MKT P001 30

E002 Rahul FIN P002 40

E003 Ramesh FIN P002 40

E004 Mahesh MKT P003 40

Attributes which decide the uniqueness of the data but the absence of one makes theDependency partial not absent.

Contd..

Page 21: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 21

2nd Normal Form - Example

Ecode Ename Dept

E001 Raj MKT

E002 Rahul FIN

E003 Ramesh FIN

E004 Mahesh MKT

PrjCode Ecode PrjHrs

P001 E001 30

P002 E002 40

P002 E003 40

P003 E004 40

Full Functional Dependency on thePrjCode Attribute

Full Functional Dependency on theECode Attribute

Page 22: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 22

Third Normal Form A relation is said to be in the Third Normal

Form if there is no transitive functional dependency between non key attributes When one non key attribute can be

determined with one or more non key attributes there is said to be a transitive functional dependency.

Example – The drug administered is dependent on the drug which is in turn dependent on the composite key displayed.

Page 23: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 23

This non key attribute is dependent on the non key attribute Drug Admin so it is not in the Third Normal Form.

Source – www.sims.berkely.edu

Page 24: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 24

Here all the non key attributes are full functionally dependent on a single key attribute.

Source – www.sims.berkely.edu

Page 25: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 25

Here all the non key attributes are full functionally dependent on a single key attribute.

Source – www.sims.berkely.edu

Page 26: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 26

Boyce Codd Normal Form

Most 3NF Relations are also BCNF Relations.

A 3NF Relation is not in BCNF if –• Candidate keys in the relation are composite keys

(they are not single attributes)• There is more than one Candidate key in the

relation, and• The keys are not disjoint, that is some attributes in

the keys are common.

Page 27: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 27

Source – www.sims.berkely.edu

In this relation there is a single attribute Patient# which is a CandidateKey and it serves as the primary key also. There is no other attribute Which can serve as the Candidate key. And along with this the attribute is also disjoint

Page 28: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 28

Source – www.sims.berkely.edu

Page 29: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 29

Denormalization

It is the reverse process to Normalization wherein the redundancy in the relations is reduced.

Here redundancy is added to speed up the processing of the queries and reduce the load from the server when the queries become more and more complex and involve more than one table.

Example – Refer to the 2NF-3NF Conversion Example.

Page 30: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 30

The Relations are in the3NF State but to extractThe effect of a certain Drug a join has to be Formed on the two Relations which an be timeConsuming.

Source – www.sims.berkely.edu

Page 31: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 31

Denormalized Relation.

FD1 FD2

Source – www.sims.berkely.edu

Relation Transition3NF->2NF

Page 32: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 32

Importance of Normalization

Normalization of a Relation helps In Developing Individual and more simple

structures In reducing redundancy In Removing the complexity of remembering

the dependencies among attributes In making the entities independent.

Page 33: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 33

Importance of Denormalization

Denormalization is used to Improve the performance of the queries which

take more time by adding required redundancy. This helps reducing the amount of joins and makes the queries simple to run cause most of the data can be fetched from a single table.

Improve efficiency Making the Structure more simpler for the end

user.

Page 34: Database Management System

April 8, 2023 Varun Arora, SICSR 2007-'09 34

Un-Normalized Data Normalized Data

Normalization testsAnd Techniques

Lower Level ofNormalizationNormalized Data

+Redundancy

A Final Note …..