lecture on keys

53
1 What is a database?  A database consists of a collection of interrelated data.

Upload: sanket-purandare

Post on 07-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 1/53

1

What is a database?

A database consists of acollection of interrelated data.

Page 2: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 2/53

2

What is database management

system (DBMS)?

A system which is used for creating

new database, retrieving, inserting,deleting and updating databaseinformation, using a data definition

language (DDL), and a datamanipulation language (DML),respectively.

Page 3: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 3/53

3

A dvantages of Using

Database Systems Centralized control of a firm¶s data

Redundancy can be reduced(avoid keeping copies of the same data in

many places) Inconsistency can be avoided to some

extent(updating occurs at one place instead of

many places)

Page 4: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 4/53

The data can be shared

S ecurity restrictions can be applied(passwords)

Integrity of data can be maintained(e.g., an instructor has to exist beforebeing assigned to teach a class)

A dvantages of Using

Database Systems (cont·d)

Page 5: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 5/53

Data Models

³U nderlying the structure of a database is thedata model: A collection of conceptual tools for describing data, data relationship, datasemantics, and constraints.´

O bject-based model(1) entity-relationship model(2) object-oriented model

Record-based model(1) relational data model(2) network model(3) Hierarchy model

Page 6: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 6/53

E ntity-Relationship ( E -R) Model

C ustomer-name

C ustomer-street

customer account depositor

S ocial-security

C ustomer-city Account # Balance

A n example of E-R diagram

Page 7: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 7/53

The E- R model is based on a perception of

a real world that consists of a collection of basic objects, called entities , and of relationships among these objects.

An entity is a ³ thing´ or ³ object´ in the realworld that is distinguishable from other objects. For example, each person is an

entity, and each bank account can beconsidered to be an entity.

Page 8: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 8/53

Entities are described in a databaseby a set of attributes . For example,the attributes ³ account-#´ and ³ balance´ describe one particular account in abank.

O ne or more attributes may uniquelyidentify the entity. For example,³ account-#´ can uniquely identify aparticular account. ³ account-# ´ and³ balance ´ as a combination can alsouniquely identify a particular account.

Page 9: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 9/53

The set of all entities of the sametype is called an entity set .

For example: T he set of all accounts in

a bank is called A ccount . S imilarly,the set of all customers of a bank iscalled Customer .

Page 10: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 10/53

10

T he table representation of an entity set

An entity set is usually represented as a tablein the database. T he name of the table is thesame name as the entity set. For example:

SSN Name Street City

586-41-4125 Brown 2404 Maile Honolulu

332-18-6752 Yang 809 Dole S t. Honolulu

121-898-5569 Gregson 211 Main S t. C hicago

C ustomer

Page 11: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 11/53

11

T he table representation of an entity set

In the table, columns are called attributes ,rows are called tuples , observations , or records . Each row is an entity, and the tableis an entity set.C ustomer

SSN Name Street City

586-41-4125 Brown 2404 Maile Honolulu

332-18-6752 Yang 809 Dole S t. Honolulu

121-898-5569 Gregson 211 Main S t. C hicago

Page 12: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 12/53

12

A relationship is an associationamong entities in related entity sets.For example, a depositor relationship associates a customer

with each account that she has. Arelationship can also have its ownattributes.

Page 13: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 13/53

13

An E ntity Relationship diagram (E -R

diagram ) is built from the followingcomponents:

(1) Rectangles , which represent entity sets(2) Ellipses , which represent attributes(3) Diamonds , which represent

relationships among entity sets.

(4) Lines , which link attributes to entity setsand entity sets to relationships

Page 14: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 14/53

Page 15: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 15/53

15

O ne to O ne . An entity in A is

associated with at most one entity inB , and an entity in B is associatedwith at most one entity in A .

a 1

A B

a 2

a 3a 4

b1 b2

b3 b4

O ne to O ne

A B1 1

Page 16: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 16/53

16

O ne to O ne . An example.

A : Husband

B: Wife

Page 17: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 17/53

17

O ne to Many . An entity in A is

associated with any number of entities in B . An entity in B ,however, can be associated with at

most one entity in A .A B

a 1a 2a 3

b1

b2 b3 b4 b5

O ne to Many

A B1 *

Page 18: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 18/53

18

O ne to Many . An example.

A : Mother

B: C hild

Page 19: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 19/53

19

Many to O ne . An entity in A is

associated with at most one entity inB . An entity in B , however, can beassociated with any number of

entities in A .

a 1

A B

a 2a 3a 4

a 5

b1 b2 b3

Many to O ne

A B* 1

Page 20: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 20/53

20

Many to O ne . An example.

A : C hild

B: Mother

Page 21: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 21/53

21

a 1

A B

a 2a 3a 4

b1

b2 b3 b4

Many to Many

Many to Many . An entity in A is

associated with any number of entities in B , and an entity in B isassociated with any number of

entities in A .

A B* *

Page 22: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 22/53

22

Many to Many . An example.

A : Brother

B: S ister

Page 23: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 23/53

Page 24: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 24/53

24

T he role of an entity in a relationship isusually implicit, and is therefore notspecified. However, when the entity sets of a relationship are not distinct, then the roleof the entity should be specified.

For example,

Employee Worksfor worker

manag er

Page 25: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 25/53

Page 26: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 26/53

26

C ustomer-name

C ustomer-street

customer

SSN

C ustomer-city

In this particular case, the value of the

combination of any attribute(s) with SSN canuniquely identify a particular customer.

Page 27: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 27/53

27

K eys

Meanwhile, it is also obvious that some of these combinations may have redundantattributes.

T herefore, in theory, we systematically classifythese combinations of attributes using threetypes of Keys: superkey, candidate key, and

primary key.

Page 28: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 28/53

28

K eys

(1) Superkey : is a set of attributes whosevalue can uniquely identify an entity in theentity set.

A superkey contains one or more attributes.

Example:T

he combination of ³ SSN

´ and³ N

ame ´ is asuperkey of the following entity setcustomer .

Page 29: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 29/53

29

Because:

T he value of attributes ³ SSN ´ and ³ N ame ´ ,such as 558-36-1234 and S usan, canuniquely identify that particular customer incustomer entity set, which is the pool of allcustomers.

C ustomer-name

C ustomer-street

customer

SSN

C ustomer-city

Page 30: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 30/53

30

N otationally, we write,S

uperkey: (SSN

, name)O ther superkeys for the entity set customer :

S uperkey: ( SSN )S uperkey: ( SSN , S treet)S uperkey: ( SSN , C ity)S

uperkey: (SSN

,N

ame,S

treet)S uperkey: ( SSN , N ame, C ity)S uperkey: ( SSN , S treet, C ity)S uperkey: ( SSN , N ame, S treet, C ity)

Page 31: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 31/53

31

S uperkey is the broadest definition of unique

identifiers of an entity in an entity set.We are unsurprisingly very interested in the

most economical combination(s) of attributes that can uniquely identify anyparticular entity.

Therefore, we introduce

Candidate Keynext.

Page 32: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 32/53

32

(2) C andidate key : is a set of one or more attributeswhose value can uniquely identify an entity in theentity set, and any attribute in the candidate keycannot be omitted without destroying theuniqueness property of the candidate key. ( It isminimal superkey).

Example:(SSN, N ame ) is NOT a candidate key,because taking out ³ name´ still leaves ³ SSN ´

which can uniquely identify an entity. ³ SSN ´ is a candidate key of customer .C andidate key could have more than oneattributes.

Page 33: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 33/53

33

Also, while most entity sets have only one

candidate key, some entity sets could havemore than one candidate key.

Example : Both ³ SSN ´ and ³ License #´ arecandidate keys of D river entity set.

In building a database in a database

software, the software will only allow to useone candidate key to be the unique identifier of an entity for an entity set.

Page 34: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 34/53

34

Before picking the pk, we need to identify any candidate key or keys that we can find forthis table. A ck is a minimal super key;

m ini m al m eans that if you take away any one

attribute from

the set, it is no longer a superkey. If you add one attribute to the set, it is nolonger m ini m al (but it s still a super key). The word candidate sim ply m eans that this set of attributes could be used as the pri m ary key,since we don t want any m ore attributes in thepk than are necessary.

Page 35: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 35/53

35

A father and son with the same first and lastnames might be living together and have thesame phone number. Adding the streetaddress or the zip code wouldn¶t help todistinguish them. O n the other hand, we could

assume that there is some other way by whichthe three attribute values can be madeunique, perhaps with a middle name or initial

in the cFirstN

ame field. (U

.S

. father-sonPresidents George Bush and George W. Bushare distinguished in this way.) With thisassumption, our ck (and pk) works correctly.

Page 36: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 36/53

36

T he attribute set {cFirst N ame, cLast N ame,

cS treet} might also be a candidate key for the C ustomers table, if we make the sameassumption about first and last names. With

more than one ck, we try to pick the onethat is most descriptive of the individualand/or least likely to change over time.T here¶s not much difference in this example.

Page 37: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 37/53

Page 38: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 38/53

38

(3) P rimary Key : is the candidate key that ischosen by the database designer as the

unique identifier of an entity.T

he databasedesigner chooses only one candidate key asthe primary key in building the system.

Example : ³ SSN ´ and ³ License #´ are bothcandidate keys of D river entity set. T hedatabase designer can choose either one as

the primary key.In an E-R diagram, we usually underlinethe primary key attribute(s).

Page 39: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 39/53

39

O verall, superkey is the broadest uniqueidentifier; candidate key is a subset of

superkey; and primary key is a subset of candidate key.

In practice, we would first look for superkeys.T hen we look for candidate keys based on

experience and common sense. If there isonly one candidate key, it naturally will bedesignated as the primary key. If we findmore than one candidate key, then we candesignate any one of them as primary key.

Page 40: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 40/53

40

For example, think about the mental processof finding the primary key of the following

entity set.

VIN

Model

C ar

C olor

Year

Make

Page 41: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 41/53

41

In-class exercise:

Construct an E-

Rdiagram for a university¶s registrar¶s office.T he office maintains data about each class, including the

instructor, the enrollment, and the time and place of the classmeetings. For each student-class pair, a grade is recorded.S pecify the mapping cardinalities between all related entity sets.(hint: there are three entity sets, instructor, student, and class)

Page 42: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 42/53

42

ER-Di ag r a m of T a bles 1 throu g h 6:

Branch

Customer- na me

Customer-street

Accou n t-br an ch

customer

Customer-city

Depositor

Account

Accou n t-n umber

Ba lan ce

Borrower Loan

Loan -n umber

Amou n t

Loan -br an ch

Br an ch- na me

Br an ch-city

Assets

Man y to m an y

Man y to o n e

On e to m an y

Man y to m an y

*

** *

*

* 11

Page 43: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 43/53

43

Cr eating Tables f r om E -R

Diagr am

Two Steps:

1. Each entity set has its own table .

Ex a mple: account: (a ccou n t-n umber, b a lan ce)

branch: ( br an ch- na me, br an ch-city, a ssets)

N ote: P rimary key attribute(s) is (are) underlined.

Page 44: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 44/53

Page 45: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 45/53

45

O ne to O ne relationship

We can do either:

(1) put the primary key of one entity set into the table of theother side.

Ex ceptionIf one entity set occurs prior to the other entity set, you

can only put the primary key of the first entity set into thetable of the second entity set, and NOT the other way

around.

O r (2) create a new table that contains the primary keys of

both entity sets.

Page 46: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 46/53

46

O ne to Many or Many to O ne .

Put the primary key of the entity set on the³one´ side intothe table of the entity set on the ³many´ side.

Example: T he relation of account-branch is accomplished by putting theprimary key of branch into the table of account , and the relationship

does not require a separate table.N

ow the account table becomes :account: (a ccou n t-n umber, b a lan ce, br an ch-na me*)

³ branch-name´ is called F oreign Key in the account table and it isused to link the account table and the branch table.

Foreign key has an * beside it.

T he same thing happens to the ³ loan-branch´ relationship.

Page 47: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 47/53

47

O ne to many or Many to one relationships ( Ex ception ).

If the many entity set occurs first, you cannot put theprimary key from the one side into the many side.

For example, the following model describes the relationship betweencustomer order and shipment. It is a many-to-one relationship, meaningthat each customer order can be delivered once, while each deliverycan have several customer orders.

In this case, the following tables do not work.S hipment: (Shipme n t-n umber, «)

Order : (Order- n umber, Shipme n t-n umber*, «)

Shipme n tOrder * 1

T his field has to be left blank Until shipment occurredT his is wrong.

Order- S hipment

Page 48: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 48/53

48

In this case, we create a new table including theprimary keys on both sides.

Th a t is,

S hipment: (Shipme n t-n umber, «)

Order : (Order- n umber, «)

Order- S hipment : (Order- n umber*, Shipme n t-n umber*)

Page 49: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 49/53

49

Many to many relationshipR equires a separate table ( b r idge ) to representthe relationship. T he primary key of this bridgetable consists of the primary keys of the entitysets on both sides of the relationship.

Example: T he relationship ³ depositor´ is implemented bytable:

depositor: (customer- na me*, a ccou n t-n umber*)

T he same happens to the ³ borrower´ relationship.

Page 50: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 50/53

50

Revisit the Ex ample of Regist r a r ·sOffice

Co n struct an E-R di ag r a m for a un iversity¶s re g istr a r¶s office .The office m a in ta in s d a ta a bout e a ch cl a ss, i n cludi ng thein structor, the e n rollme n t, an d the time an d pl a ce of the cl a ssmeeti ng s. For e a ch stude n t-cl a ss p a ir, a g r a de is recorded .S pecify the m a ppi ng ca rdi na lities betwee n a ll rel a ted e n titysets .(hin t: there a re three e n tity sets, i n structor, stude n t, an d cl a ss)

Page 51: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 51/53

51

E-R Diagram and Mapping C ardinalities:

In structor In st_Cl a ss Cla ss Stud_Cl a ss Stude n t

Cla ss-time

Empl-ID Cla ss-Num Room

Gr a de

Stud-ID

1 * * *

N ote: A relationship sometimes has itown attribute(s). In such case, therelationship has to be a separate table.

Page 52: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 52/53

Page 53: Lecture on Keys

8/6/2019 Lecture on Keys

http://slidepdf.com/reader/full/lecture-on-keys 53/53