from week 3 tutorial

20
From week 3 Tutorial CUSTOMER Makes ORDER IsRequest edIn PART CId CName CAddr CPhone OId PaidStatus ODate PDesc PName PId

Upload: melyssa-garrett

Post on 02-Jan-2016

16 views

Category:

Documents


0 download

DESCRIPTION

CPhone. Makes. IsRequestedIn. From week 3 Tutorial. CAddr. CUSTOMER. CId. CName. PaidStatus. OId. ORDER. ODate. PId. PName. PART. PDesc. CAddr. CUSTOMER. CUSTPHONE. CId. CName. CPhone. Makes. Makes. CUSTOMER. IsRequestedIn. CId. CName. CAddr. CUSTPHONE. CPhone. CId. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: From week 3 Tutorial

From week 3 Tutorial

CUSTOMER Makes

ORDER

IsRequestedInPART

CId CName

CAddr CPhone

OId

PaidStatus

ODate

PDesc

PNamePId

Page 2: From week 3 Tutorial

Mapping Infinite Multi-valued Attributes

Makes

ORDER

IsRequestedInPART

OId

PaidStatus

ODate

PDesc

PNamePId

CUSTOMER

CId CName

CAddr

CPhone

CUSTPHONE Makes

CId CName CAddr

CUSTOMER

CPhone

CUSTPHONE

CId

Page 3: From week 3 Tutorial

Mapping relationships

PId PName PDesc

PART

ODate

ORDER

OId PaidStatus PId

CPhone

CUSTOMER Makes

CId CName

CAddr

IsRequestedInPART

ORDEROId

PaidStatus

ODate

PDesc

PNamePId

CUSTPHONE Makes

Page 4: From week 3 Tutorial

Mapping relationships

CId CName

CUSTOMER

CAddr

IsRequestedInPART

CPhone

PNamePId

CUSTPHONE Makes CUSTOMER Makes

ORDER

CId CName

CAddr

OId

PaidStatus

ODate

CId

PId PName PDesc

PART

ODate

ORDER

OId PaidStatus PId

PDesc

Page 5: From week 3 Tutorial

Final Schema

CId CName

CUSTOMER

CAddr

CId

PId PName PDesc

PART

ODate

ORDER

OId PaidStatus PId

CIdCPhone

CUSTPHONE

Page 6: From week 3 Tutorial

SQL Data Definition

CREATE TABLE Part (PId INTEGER,

PName CHAR(20),

PDesc CHAR(30),

PRIMARY KEY (PId))

PId PName PDesc

PART

CId CName

CUSTOMER

CAddr

CREATE TABLE Customer (Cid INTEGER,

CName CHAR(20),

CAddr CHAR(40),

PRIMARY KEY (Cid))

Page 7: From week 3 Tutorial

SQL Data Definition

CREATE TABLE Order (OId INTEGER, ODate DATE, PaidStatus CHAR(1), PId INTEGER, CId INTEGER, PRIMARY KEY (OId), FOREIGN KEY (PId) REFERENCES Part, FOREIGN KEY (Cid) REFERENCES Customer)

CId CName

CUSTOMER

CAddr

CId

PId PName PDesc

PART

ODate

ORDER

OId PaidStatus PId

Page 8: From week 3 Tutorial

Joined Relations

Var1 Var2

a c

b d

Var3 Var4

b c

e f

12 3

4

Var1 Var2 Var3 Var4

1 a c b c

2 a c e f

3 b d b c

4 b d e f

Page 9: From week 3 Tutorial

JOINTID Var1

1 a

2 b

3 c

SID Var2

1 d

3 e

5 f

TID Var1 SID Var2

1 a 1 d

1 a 3 e

1 a 5 f

The Cartesian Product is

Table 1 Table 2

2 b 1 d

2 b 3 e

2 b 5 f

3 c 1 d

3 c 3 e

3 c 5 f

The Relations for (TID=SID) is:

Page 10: From week 3 Tutorial

JOINTID Var1

1 a

2 b

3 c

SID Var2

1 d

3 e

5 f

TID Var1 SID Var2

1 a 1 d

3 c 3 e

The Cartesian Product is

Table 1 Table 2

The Cartesian Product for (TID-SID) is:

Page 11: From week 3 Tutorial

Functional Dependencies and Keys

• Functional Dependency: The value of one attribute (the determinant) determines the value of another attribute

• Candidate Key:– A unique identifier. One of the candidate keys

will become the primary key• E.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

Page 12: From week 3 Tutorial

Steps in normalization

Page 13: From week 3 Tutorial

First Normal Form

• No multi-valued attributes• Every attribute value is atomic• multi-valued attributes it is not a

relation

Page 14: From week 3 Tutorial

Second Normal Form

• 1NF PLUS every non-key attribute is fully functionally dependent on the ENTIRE primary key– Every non-key attribute must be defined by

the entire key, not by only part of the key

– No partial functional dependencies

Page 15: From week 3 Tutorial

Functional Dependencies

Order_ID Order_Date, Customer_ID, Customer_Name, Customer_Address

Therefore, NOT in 2nd Normal Form

Customer_ID Customer_Name, Customer_Address

Product_ID Product_Description, Product_Finish, Unit_Price

Order_ID, Product_ID Order_Quantity

Full Dependency

Transitive Dependencies

Partial Dependencies Partial Dependencies

Page 16: From week 3 Tutorial

Getting it into Second Normal Form: Removing Partial Dependencies

Partial Dependencies are removed, but there are still transitive dependencies

Page 17: From week 3 Tutorial

Third Normal Form

• 2NF PLUS no transitive dependencies (functional dependencies on non-primary-key attributes)

• Note: this is called transitive, because the primary key is a determinant for another attribute, which in turn is a determinant for a third

• Solution: non-key determinant with transitive dependencies go into a new table; non-key determinant becomes primary key in the new table and stays as foreign key in the old table

Page 18: From week 3 Tutorial

Getting it into Third Normal Form

Transitive dependencies are removed

Page 19: From week 3 Tutorial

Boyce-Codd Normal Form (BCNF)

Customer

Customer-id

Customer-street

Customer-city

Branch

Branch-ID

Branch-name

GetLoan

Loan-Number

Customer-name

Branch-city

Amount

Page 20: From week 3 Tutorial

Boyce-Codd Normal Form (BCNF)• Customer-schema = (customer-id, customer-name, customer-street,

customer-city)customer-id -> customer-name customer-street customer-

city

• Branch-schema = (branch-id, branch-name, branch-city)branch-id -> branch-name branch-city

• Loan-info-schema (branch-id, customer-id, loan-number, amount)loan-number -> amount branch-id

e.g. (Melbourne101, c1234, L-44, $1000)(Melbourne101, c1235, L-44, $1000)

NOT in BCNF

Loan-schema (loan-number, branch-id, amount)Borrower-schema (customer-id, loan-number)