Top Banner
‹#› Faculty Name: Muhammad Aqeel Database Model There are three different types of Database Models Hierarchical Database Model A tree like structure in which any node (entity) may have any number of child nodes but each child node may have only one parent node. Network Database Model A graph like structure in which any node (entity) may have any number of child nodes and any child node may also have more than one parent node. Relational Database Model Relational Database model is based on mathematical theory of Relation and data is represented in the form of Tables. It has three components 1
61
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Database ModelThere are three different types of Database Models

Hierarchical Database Model

A tree like structure in which any node (entity) may have any number of child nodes but each child node may have only one parent node.

Network Database Model

A graph like structure in which any node (entity) may have any number of child nodes and any child node may also have more than one parent node.

Relational Database Model

Relational Database model is based on mathematical theory of Relation and data is represented in the form of Tables. It has three components

1

Page 2: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Components of Relational Database Model

DATA STRUCTURE:

Data are organized in the form of tables (Relations) with rows and columns.

Data Manipulation:

Powerful operations (using the SQL language) are used to manipulate data stored in the relations.

Data Integrity:

Facilities are included to specify business rules that maintain the integrity of data when they are manipulated.

2

Page 3: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

RelationDefinition: A relation is a named, two-dimensional table of

dataTable is made up of rows (records), and columns (attribute or field)

Not all tables qualify as relations

Requirements: Every relation has a unique name.

Every attribute value is atomic (not multivalued, not composite)

Every row is unique (can’t have two rows with exactly the same values for all their fields)

Attributes (columns) in tables have unique names

The order of the columns is irrelevant

The order of the rows is irrelevant

NOTE: all relations are in 1st Normal form 3

Page 4: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Correspondence with ER Model

Relations (tables) correspond with entity types and with One or many-to-many relationship types

Rows correspond with entity instances and with One or many-to-many relationship instances

Columns correspond with attributes

NOTE: The word relation (in relational database) is NOT the same same the word relationship (in ER model)

4

Page 5: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Key Fields

Keys are special attributes/fields that may be used as:

Primary Key An attribute (or combination of attributes) that uniquely identifies each row in a relation.

Examples include employee numbers, social security numbers, Roll number etc. This is how we can guarantee that all rows are unique

Foreign Key An attribute in a relation of a database that serves as the primary key of another relation in the same database and used to link or associates both relations. It can also be defined as

“Identifiers that enable a dependent relation (on the many side of a relationship) to refer to its parentrelation (on the one side of the relationship)

Keys can be simple (a single field) or composite(more than one field) 5

Page 6: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Schema for four relations (Pine Valley Furniture)

Primary Key

Foreign Key (implements 1:N relationship between customer and order)

Combined, these are a composite primary key (uniquely identifies the order line)…individually they are foreign keys (implement M:N relationship between order and product)

6

Page 7: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Constraints

Constraints are used to maintain the accuracy and

integrity of data stored in database. There are different

types of constraints.

e.g. Domain Constraints, Entity Integrity, Referential

Integrity, etc.

Domain Constraints

Allowable values for an attribute. See Table 5-1

fields MUST have data

Action Assertions

Business rules. Recall from Ch. 4

7

Page 8: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Referential Integrity Constraint

A rule that states that either each foreign key value must

match a primary key value in another relation or the foreign key

value must be null. Referential Integrity constraint maintains

consistency among rows of two relations.

For example: Delete Operation

Restrict – don’t allow delete of “parent” side if related rows

exist in “dependent” side

Cascade – automatically delete “dependent” side rows that

correspond with the “parent” side row to be deleted

Set-to-Null – set the foreign key in the dependent side to

null if deleting from the parent side

(not allowed for weak entities)8

Page 9: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Referential integrity constraints (Pine Valley Furniture)

Referential integrity constraints are

drawn via arrows from dependent to

parent table

9

Page 10: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Transforming EER Diagrams into Relations

Mapping Regular Entities to Relations

Simple attributes: E-R attributes map directly onto

the relation

Composite attributes: Use only their simple,

component attributes

Multi-valued Attribute - Becomes a separate relation

with a foreign key taken from the superior entity

10

Page 11: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

(a) CUSTOMER entity type with simple attributes

Mapping a regular entity

(b) CUSTOMER relation

11

Page 12: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

(a) CUSTOMER entity type with composite attribute

Mapping a composite attribute

(b) CUSTOMER relation with address detail

12

Page 13: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Mapping a multivalued attribute

1 – to – many relationship between original entity and new relation

(a)

Multivalued attribute becomes a separate relation with foreign key

(b)

13

Page 14: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Mapping Weak Entities

Becomes a separate relation with a

foreign key taken from the superior

entity

Primary key composed of:

Partial identifier of weak entity AND

Primary key of identifying relation (strong

entity)

Transforming EER Diagrams into Relations

14

Page 15: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Example of mapping a weak entity

(a) Weak entity DEPENDENT

15

Page 16: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

(b) Relations resulting from weak entity

NOTE: the domain constraint for the foreign key should NOT allow null value if DEPENDENT is a weak entity

Foreign key

Composite primary key

16

Page 17: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Mapping Binary Relationships

One-to-Many - Primary key on the one

side becomes a foreign key on the many

side

Many-to-Many - Create a new relation

with the primary keys of the two entities

as its primary key

One-to-One - Primary key on the

mandatory side becomes a foreign key on

the optional side

Transforming EER Diagrams into Relations

17

Page 18: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

(a) Relationship between customers and orders

Note the mandatory one

18

Page 19: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Mapping the relationship

Again, no null value in the foreign key…this is because of the mandatory minimum cardinality

Foreign key

19

Page 20: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

(a) ER diagram (M:N)

The Supplies relationship will need to become a separate relation

20

Page 21: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Three resulting relations

New intersection

relationForeign key

Foreign key

Composite primary key

21

Page 22: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

(a) Binary 1:1 relationship

22

Page 23: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Resulting relations

23

Page 24: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Mapping Associative EntitiesThis process may be divided into two steps:

Create three relations: one for each entity types that

participates in a relationship and third for associative entity, It may be called associative relation.

Associative Entity may have two possibilities for

identifier attribute:

If Identifier Not Assigned thenDefault primary key for the association relation is composed of the primary keys of the two entities (as in M:N relationship)

Identifier Assigned thenIt is natural and familiar to end-usersDefault identifier may not be unique

Transforming EER Diagrams into Relations

24

Page 25: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Mapping an associative entity

Identifier

Assigned25

Page 26: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Three resulting relations

26

Page 27: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Mapping Unary Relationships

One-to-Many - Recursive foreign key in

the same relation

Many-to-Many - Two relations:

One for the entity type

One for an associative relation in which

the primary key has two attributes, both

taken from the primary key of the entity

Transforming EER Diagrams into Relations

27

Page 28: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

(a) EMPLOYEE entity with Manages relationship

(b) EMPLOYEE relation with recursive foreign key

Mapping Unary Relationships

One-to-Many - Recursive foreign key in the same

relation

28

Page 29: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Mapping a unary M:N relationship

(a) Bill-of-materials relationships (M:N)

(b) ITEM and COMPONENT relations

Mapping Unary Relationships

Many-to-Many - Two relations:

One for the entity type

One for an associative relation in

which the primary key has two

attributes, both taken from the primary

key of the entity

29

Page 30: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Mapping Ternary (and n-ary) Relationships

One relation for each entity and one for the

associative entity

Associative entity has foreign keys to each entity

in the relationship

Transforming EER Diagrams into Relations

30

Page 31: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Mapping a ternary relationship

Ternary relationship with associative entity

31

Page 32: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Mapping the ternary relationship

Remember that the primary key MUST be

unique

32

Page 33: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Mapping Supertype/Subtype Relationships

One relation for supertype and for each subtype

Supertype attributes (including identifier and subtype discriminator) go into supertype relation

Subtype attributes go into each subtype; primary key of supertype relation also becomes primary key of subtype relation

1:1 relationship established between supertype and each subtype, with supertype as primary table

Transforming EER Diagrams into Relations

33

Page 34: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Supertype/subtype relationships

34

Page 35: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Mapping Supertype/subtype relationships to relations

35

Page 36: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

NORMALIZATIONWell-structured relation

A relation that contains minimal

redundancy and allows users to insert,

modify, and delete the rows in a table

without errors or inconsistencies.

Anomaly

An error or inconsistency that may

result when a user attempt to update a

table that contains redundant data. The

three types of anomalies are insertion,

deletion, and modification. 36

Page 37: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

The goal of well-structured relation is to

avoid anomalies

Insertion Anomaly – adding new rows forces user to create duplicate data

Deletion Anomaly – deleting rows may cause a loss of data that would be needed for other future rows

Modification Anomaly – changing data in a row forces changes to other rows because of duplication

37

Page 38: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Example

Is this a relation? Yes: unique rows and no multivalued attributes

What’s the primary key? Composite Key: Emp_ID, Course_Title

Followings are the possible anomalies in this relation38

Page 39: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Insertion – can’t enter a new employee without having

the employee take a class

Deletion – if we want to remove employee 140, we

lose information about the existence of a Tax Acc

class

Modification – giving a salary increase to employee

100 forces us to update multiple records

Why do these anomalies exist?

Because we’ve combined two themes (entity types)

into one relation. This results in duplication, and an

unnecessary dependency between the entities

General rule of thumb: a table should not pertain

to more than one entity type

39

Page 40: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Data Normalization

“The process of decomposing

relations with anomalies to produce

smaller, well-structured relations.”

A Primarily a tool to validate and improve

a logical design so that it satisfies certain

constraints that avoid unnecessary

duplication of data

40

Page 41: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

41

Page 42: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Functional Dependency:A constraint between two attributes or two sets of attributes.

For any relation R, attribute B is functionally dependent on

attribute A, if for every instance of A, the value of A uniquely

determines the value of B. It can be represented as

R: A B

For example

In student relation : Roll_No Name, dob, address

In customer relation: Customer_id Name, address

The value of one attribute determines the values of other attributes of relation. It may be possible that

An attribute may be functionally dependent on two (or more) attribute rather than on a single attribute.

42

Page 43: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Candidate Key:

An attribute, or combination of attributes, that uniquely identifies a row in a relation. One of the candidate keys will become the primary keyE.g. perhaps there is both credit card number and SS# in a table…in this case both are candidate keys

Each non-key field is functionally dependent on every candidate key

Employee

Emp_ID Emp_Name Address SSN Qualification Contact_no

43

Page 44: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

ht

First Normal FormA relation is in First Normal Form if it

contains no multivalued attributes.

Simply we can say that the value at the intersection of each row and column must be atomic

All relations are in 1st Normal Form

Surveys 5/22/200X

44

Page 45: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Second Normal FormA relation is in Second Normal Form if it is in

1st Normal form and every non key attribute is fully functionally dependent on the primary key.

Every non-key attribute must be defined by the entire key, not by only part of the key.

No partial functional dependencies in the relation.

45

Page 46: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Functional Dependencies in EMPLOYEE2

EmpID CourseTitle DateCompletedSalaryDeptNameName

Dependency on entire primary key

Dependency on only part of the key

EmpID, CourseTitle DateCompleted

EmpID Name, DeptName, Salary

Therefore, NOT in 2nd Normal Form!!46

Page 47: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Getting it into 2nd Normal Form

decomposed into two separate relations

EmpID SalaryDeptNameName

CourseTitle DateCompletedEmpID

Both are full functional dependencies

47

Page 48: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Transitive Dependency:

A functional dependency between two (or more) non-key attributes.

48

Page 49: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Relation with transitive dependency

CustID NameCustID SalespersonCustID Region

All this is OK(2nd NF)

BUT

CustID Salesperson Region

Transitive dependency(not 3rd NF)

49

Page 50: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Third Normal Form

A relation is in Third Normal Form if it is in 2nd

Normal form and no Transitive Dependenciesexist in the relation.

(one attribute functionally determines a second, which functionally determines a third)

50

Page 51: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Removing a transitive dependency

51

Page 52: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Relations in 3NF

Now, there are no transitive dependencies…Both relations are in 3rd NF

CustID Name

CustID Salesperson

Salesperson Region

52

Page 53: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Other Normal Forms

(from Appendix B)

Boyce-Codd NFAll determinants are candidate keys…there is no

determinant that is not a unique identifier

4th NFNo multivalued dependencies

5th NF No “lossless joins”

Domain-key NFThe “ultimate” NF…perfect elimination of all possible

anomalies

53

Page 54: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Boyce-Codd Normal Form (BCNF)

A relation is in Boyce-Codd Normal Form if and

only if every determinant key is a candidate key.

FOR EXAMPLE -In following figure

Advisor is a determinant key but it is not a

candidate key (not uniquely identifies a row in

relation)

54

Page 55: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

55

Page 56: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Boyce-Codd Normal Form (BCNF)

What happen if we delete a row of 798?Of course we lost information about Bach Advisor.

(Delete Anomaly)

If we want to replace Hawking advisor with

Edward then we have to change in all rows (two)

(Update Anomaly)

A new Advisor Babbage for Physics can only

insert if we have at least one student who works

under his supervision.

(Insert Anomaly)56

Page 57: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

57

Page 58: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

58

Page 59: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

59

Page 60: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Multi valued Dependency:

The type of dependency that exists when then are at least three attributes (e.g. A,B,C), and for each value if A there is a well-defined set of values of B and well-defined values of C. However, the set of values of B ins independent of set C and vice versa

60

Page 61: chap05-modified.pdf

‹#›Faculty Name: Muhammad Aqeel

Fourth Normal Form:

A relation is in Fourth Normal Form if it is in BCNF and contains no multivalued dependencies

61