topic database modelling s mckeever advanced databases 1

43
Topic Database Modelling S McKeever Advanced Databases 1

Post on 21-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Topic Database Modelling S McKeever Advanced Databases 1

Topic Database Modelling

S McKeeverAdvanced Databases

1

Page 2: Topic Database Modelling S McKeever Advanced Databases 1

3 phases of database design

S McKeeverAdvanced Databases 2

(1) Conceptual (2) Logical (3) Physical

Need a systematic approach to designing and building the dB

Page 3: Topic Database Modelling S McKeever Advanced Databases 1

Phase 1: Conceptual database design

S McKeeverAdvanced Databases 3

– Step 1.1 Identify entity types– Step 1.2 Identify relationship types– Step 1.3 Identify and associate

attributes with entity or relationship types

– Step 1.4 Determine attribute domains

– Step 1.5 Determine candidate, primary, and alternate key attributes

– Step 1.6 Consider use of enhanced modeling concepts (optional step)

– Step 1.7 Check model for redundancy

– Step 1.8 Validate conceptual model against user transactions

– Step 1.9 Review conceptual data model with user

Step 1: Build conceptual data model

Page 4: Topic Database Modelling S McKeever Advanced Databases 1

Phase 2: Logical database design

S McKeeverAdvanced Databases 4

– Step 2.1 Derive relations for logical data model

– Step 2.2 Validate relations using normalization

– Step 2.3 Validate relations against user transactions

– Step 2.4 Define integrity constraints

– Step 2.5 Review logical data model with user

– Step 2.6 Merge logical data models into global model (optional step)

– Step 2.7 Check for future growth

Step 2: Build and validate Logical database design

Page 5: Topic Database Modelling S McKeever Advanced Databases 1

Phase 3: Physical database design

S McKeeverAdvanced Databases 5

Step 3.1 Design base relations

Step 3.2 Design representation of derived data

Step 3.3 Design general constraints

• Step 3 Translate logical data model for target DBMS

Step 4 Design file organizations and indexes

Step 4.1 Analyze transactionsStep 4.2 Choose file organizationStep 4.3 Choose indexesStep 4.4 Estimate disk space requirements

Page 6: Topic Database Modelling S McKeever Advanced Databases 1

Phase 3: Physical database design (continued)

S McKeeverAdvanced Databases 6

• Step 5 Design user views• Step 6 Design security mechanisms • Step 7 Consider the introduction of controlled

redundancy • Step 8 Monitor and tune the operational system

Page 7: Topic Database Modelling S McKeever Advanced Databases 1

Our focus…

S McKeeverAdvanced Databases 7

• Will concentrate on the techniques for producing the Entity Relationship Diagram (ERD)

• This is the typical design diagram for explaining • The entities • Their attributes• Their relationships

• Key output from Conceptual data modelling• Greater detail as logical and physical design

progresses,…

Page 8: Topic Database Modelling S McKeever Advanced Databases 1

Entities

S McKeeverAdvanced Databases 8

Entity = “thing”

• “An entity is a business object that represents a group, or category of data.”

•Like a database table

Page 9: Topic Database Modelling S McKeever Advanced Databases 1

S McKeever

9

Entities diagramatically

Customer branches

(a customer can be in more thanOne branch)

Customer entity

Page 10: Topic Database Modelling S McKeever Advanced Databases 1

S McKeeverAdvanced Databases

10

CustID CustName CustAddr Credit_Lim Curr_Bal

Dunn0001 DunnesStores

Main Street,Dublin

£1000 £15.45

CustID BranID BranName BranAddr Credit_Lim Curr_Bal

Dunn0001 RATH0001 Rathmines Dublin 6 £200 £0.40

Dunn0001 JERV0001 Jervis St. Dublin 2 £300 £6.05

Dunn0001 DAWS0001

Dawson St.

Dublin 2 £300 £6.00

Dunn0001 WICK0001 Wicklow Wicklow £200 £3.00

Entities as table - with data

Customer

Customer branches

Page 11: Topic Database Modelling S McKeever Advanced Databases 1

S McKeeverAdvanced Databases

11

•The DB is recording information about your customers and their branches

•These are examples of real-world entities•An entity is any “object” about which we wish

to store information in the DB (table)•Entities are linked together by relationships•CustID, BranAddr, etc. are data items or

fields or attributes

So what does that mean ?

Page 12: Topic Database Modelling S McKeever Advanced Databases 1

S McKeeverAdvanced Databases

12

To set up that table in SQL...Create table Customer( CustID char(10) NOT NULL,CustName char(50),CustAddr char(100),Credit_Lim number(10),Debtor_Bal number(10));

select * from Customerwhere CustID = “Dunn0001”

Page 13: Topic Database Modelling S McKeever Advanced Databases 1

Attributes

S McKeeverAdvanced Databases 13

Attributes = “ways to describe the thing”

•“An attribute is a sub-group of information within an entity.”

•Like a database field•Shown as list under the entity name

Page 14: Topic Database Modelling S McKeever Advanced Databases 1

Keys

S McKeeverAdvanced Databases 14

CustID CustName CustAddr Credit_Lim Curr_Bal

Dunn0001 DunnesStores

Main Street,Dublin

£1000 £15.45

Customer

Primary key = uniquely identifies EACH row

What’s the primary key here?

Page 15: Topic Database Modelling S McKeever Advanced Databases 1

S McKeeverAdvanced Databases

15

What’s the primary key here?

(can be composite.. More than one field

CustID BranID BranName BranAddr Credit_Lim Curr_Bal

Dunn0001 RATH0001 Rathmines Dublin 6 £200 £0.40

Dunn0001 JERV0001 Jervis St. Dublin 2 £300 £6.05

Dunn0001 DAWS0001

Dawson St.

Dublin 2 £300 £6.00

Dunn0001 WICK0001 Wicklow Wicklow £200 £3.00

Customer branches

Page 16: Topic Database Modelling S McKeever Advanced Databases 1

Keys

S McKeeverAdvanced Databases 16

Foreign key = A column(s) one a table that matches the primary key on another table.

CustID CustName CustAddr Credit_Lim Curr_Bal

Dunn0001 DunnesStores

Main Street,Dublin

£1000 £15.45

CustID BranID BranName BranAddr Credit_Lim Curr_Bal

Dunn0001 RATH0001 Rathmines Dublin 6 £200 £0.40

Dunn0001 JERV0001 Jervis St. Dublin 2 £300 £6.05

Dunn0001 DAWS0001

Dawson St.

Dublin 2 £300 £6.00

Dunn0001 WICK0001 Wicklow Wicklow £200 £3.00

Page 17: Topic Database Modelling S McKeever Advanced Databases 1

S McKeeverAdvanced Databases

17

CustID CustName CustAddr Credit_Lim Curr_Bal

Dunn0001 DunnesStores

Main Street,Dublin

£1000 £15.45

CustID BranID BranName BranAddr Credit_Lim Curr_Bal

Dunn0001 RATH0001 Rathmines Dublin 6 £200 £0.40

Dunn0001 JERV0001 Jervis St. Dublin 2 £300 £6.05

Dunn0001 DAWS0001

Dawson St.

Dublin 2 £300 £6.00

Dunn0001 WICK0001 Wicklow Wicklow £200 £3.00

KeysForeign keys – enforce referential integrityWhat happens if I (try) to delete customer DUNN001 from the customer table?

X

Page 18: Topic Database Modelling S McKeever Advanced Databases 1

What does each key mean?

S McKeeverAdvanced Databases 18

Primary key = uniquely identifies EACH row e.g. student ID – everyone has a different one+Alternate key – could have been a primary key but isn’t. e.g. student ID (PK) and PPS ID (AK)=Candidate keys: The primary key and the alternative keys. Any column or a combination of columns that can qualify as unique key in database.e.g. student ID and PPS number

Foreign key = A column(s) one a table that matches the primary key on another table. E.g. student_school (on student table, links to table contains schools)

Page 19: Topic Database Modelling S McKeever Advanced Databases 1

Entity attribute display in data models

S McKeeverAdvanced Databases 19

Page 20: Topic Database Modelling S McKeever Advanced Databases 1

Entities and tables

S McKeeverAdvanced Databases 20

The words “Entities “ and “tables” are sometimes used interchangeably.

But they’re not quite the same thing….

Page 21: Topic Database Modelling S McKeever Advanced Databases 1

S McKeeverAdvanced Databases

21

Relationships between entitiesTo start defining relationships between entities, first describe the way that they are connected in real life

Let’s go back to the ERD in the quiz

There are four faculties in the college: science, applied arts, built environment, business. Each student is registered on a programme within a faculty. Programmes consist of a set of modules, with some module offered on more than one programme. A student on a programme has to register for the compulsory modules on tphe programme and also take a number of optional modules. All modules have a unique module ID, and an associated credit waiting (ECTS).  

Page 22: Topic Database Modelling S McKeever Advanced Databases 1

S McKeeverAdvanced Databases

22

Need to be able to say “how many” of the entitiesAre participating in the relationship

1:1? One to one1:M? One to manyN:M? Many to many

Relationships between entities

Page 23: Topic Database Modelling S McKeever Advanced Databases 1

Sample Entity Relationship Diagram – book store

S McKeeverAdvanced Databases 23

Note: The attributes are listedThe data types of the attributes are listedWhat stage of the design is this? Conceptual, logical or physical?

Imagine you’ve drawn this ERD

What was the business description of the data requirements given in the first place?

Page 24: Topic Database Modelling S McKeever Advanced Databases 1

Book store ERD

S McKeeverAdvanced Databases 24

Imagine you’ve drawn this ERD

What was the business description of the data requirements given in the first place?

Some thing like –

Need to store information about the book titles that we sell,

In our bookstore, we store information about authors, the titles that they write . An author can write many titles. If the author has died they will have an obituary. The bookstore has many ongoing sales promotions that include many titles

We categorise our titles along the usual lines – popular fiction, crime, health and so on

We also store information about our publishers and who the main sales contact is in the publisher

Page 25: Topic Database Modelling S McKeever Advanced Databases 1

Relationships between entities

S McKeeverAdvanced Databases 25

When two entities are “related”.. There should be a word to describe this relationship

e.g. Entities Relationshipstaff, branches Staff manage bank branches student, modules student attends classesnewspaper, rentalProperties newspapers advertise rentalProperties

You need to understand this “word” in order to figure out the information needed to define the relationship for the ERD

On an ERD, need to know how many of each entity takes part in the relationship – question both entities about “how many”?

e.g. For the Staff manages bank branches relationship

Staff can manage how many branches at most?? A staff person can manage one branchStaff must manage how many branches at min? A staff person can have no branch to manage

Page 26: Topic Database Modelling S McKeever Advanced Databases 1

Relationship cardinality

S McKeeverAdvanced Databases 26

“eh…What’s the cardinality of your mobile phone spend in euro each week?

Relationship cardinality is about assigning the 1:1, 1:n etc etc

“Can” and “Must” get you there..

Page 27: Topic Database Modelling S McKeever Advanced Databases 1

Maximum Cardinality

S McKeeverAdvanced Databases 27

Can….

Page 28: Topic Database Modelling S McKeever Advanced Databases 1

The 3 types of maximum cardinality

S McKeeverAdvanced Databases 28

One to one 1:1 An employee can be assigned one computer

One to many 1:n A mother can have many children

Many to many m:n An order can have many product and vv

How can you tell the difference between 1:m and m:ne.g. Student and module entities. What’s the maximum cardinality?

Page 29: Topic Database Modelling S McKeever Advanced Databases 1

Minimum cardinality

S McKeeverAdvanced Databases 29

Must…

Page 30: Topic Database Modelling S McKeever Advanced Databases 1

Minimal cardinality

S McKeeverAdvanced Databases 30

An employee must be assigned ? computer

A mother must have ? children

An order must have ? product

What’s the minimum cardinality* for our examples?

* zero (optional) or one (must have at least one)

Page 31: Topic Database Modelling S McKeever Advanced Databases 1

S McKeeverAdvanced Databases 31

Minimal cardinality

An employee must be assigned ? ComputerAns: Depends on the rule of the individual company. Probably zero

A mother must have ? childrenans: Definitely zero (optional!)

An order can have ? Productsans: must have at least one

What’s the minimum cardinality* for our examples?

* zero (optional) or one (must have at least one)

Page 32: Topic Database Modelling S McKeever Advanced Databases 1

Maximum and minimum cardinality

S McKeeverAdvanced Databases 32

Now we know how to get the maximum (can) and minimum –

But it has to be done BOTH ways to complete the relationship. We only did ONE direction

A computer can be assigned to one employee

A child can have one mother

A product can be on many orders

Maximum cardinality

A computer must be assigned to one employee -No

A child must have a mother? YES

A product must be on an order? NO

Minimim cardinality

Page 33: Topic Database Modelling S McKeever Advanced Databases 1

Maximum and minimum cardinality

S McKeeverAdvanced Databases 33

Once Maximum and Minimum cardinality has been figures outBetween the entities – then they can be notated in whatever designTool you’re using

Page 34: Topic Database Modelling S McKeever Advanced Databases 1

Just for reference - Data modelling notation:Erwin

S McKeeverAdvanced Databases 34

Max

Min Min

Max

Note: Solid line ____ versus Dashed line ---- in Erwin = identifying versus non identifying relationship between two entities

Page 35: Topic Database Modelling S McKeever Advanced Databases 1

Create relationships:n:m relationships – intersection table

S McKeeverAdvanced Databases 35

Page 36: Topic Database Modelling S McKeever Advanced Databases 1

Drawing the ERD with what you’ve learnt so far…

S McKeeverAdvanced Databases 36

Read the specification and make sure your understand it

Figure out what your entities are e.g. sales, book title, authors

Figure out which ones are related (don’t worry yet about the cardinality – helps to put the word in (e.g. “has”)

Figure out the attributes of each entity

Identify the primary key for each entity

Any foreign keys?

Identify the cardinality of the relationship

Page 37: Topic Database Modelling S McKeever Advanced Databases 1

Exercise

S McKeeverAdvanced Databases 37

A furniture company needs to develop a database to store information about its customers and sales

Customer details need to be captured, such as name, address. The company wants to be able to report on what their sales – what each customer bought (i.e. what furniture items). They would also like to know what the items are.

Try and sketch out the entities involved.

Page 38: Topic Database Modelling S McKeever Advanced Databases 1

S McKeeverAdvanced Databases

38

S McKeeverAdvanced Databases

38

EMPLOYEEsupervises

is supervised by

Recursive relationships

Page 39: Topic Database Modelling S McKeever Advanced Databases 1

For reference.. Summary of Erwin symbols

S McKeeverAdvanced Databases 39

Page 40: Topic Database Modelling S McKeever Advanced Databases 1

Summary of Erwin symbols (cont.)

S McKeeverAdvanced Databases 40

Page 41: Topic Database Modelling S McKeever Advanced Databases 1

Some examples

S McKeeverAdvanced Databases 41

Note.. The symbols right beside the entity tell you how many of “it” can and must apply to the other entity.

How many companies can and must an employee belong to?

(note: sometimes I I shown, sometimes I

Page 42: Topic Database Modelling S McKeever Advanced Databases 1

Some examples (continued)

S McKeeverAdvanced Databases 42

Page 43: Topic Database Modelling S McKeever Advanced Databases 1

Some examples (continued)

S McKeeverAdvanced Databases 43