Chapter 2 The Relational Model. 2-2 In This Chapter You Will Learn What a data model is and…

Preview:

DESCRIPTION

2-3 What Is A Data Model?   An integrated collection of concepts for describing data, relationships between data, and constraints on the data.   A data model attempts to represent the operation of a company or part of the company, that you wish to model.   It provides the essential concepts and notations that allow database designers and end-users to communicate their understanding of the company unambiguously and accurately.

Citation preview

Chapter 2Chapter 2The Relational ModelThe Relational Model

2-2

In This Chapter You Will LearnIn This Chapter You Will Learn

What a data model is and what its uses are

The terminology of the relational model

How tables are used to represent data

Properties of database tables

How to identify candidate, primary, and foreign keys

The meaning of entity integrity (實體完整限制式) and referential integrity (參照完整限制式)

2-3

What Is A Data Model?What Is A Data Model?

An integrated collection of concepts for describing data, relationships between data, and constraints on the data.

A data model attempts to represent the operation of a company or part of the company, that you wish to model.

It provides the essential concepts and notations that allow database designers and end-users to communicate their understanding of the company unambiguously and accurately.

2-4

Three Components Of A Three Components Of A Data ModelData Model

A Structure Part How the database is to be constructed

A Manipulative Part Types of data operations that are allowed

Updating or retrieving dataChanging the structure of the database

A Set Of Integrity RulesEnsures that the data is accurate

2-5

Terminology of Relational ModelTerminology of Relational Model

Relation (關聯、關係)Table with columns and rows

Columns ~ AttributesRows ~ Individual records

We use tables to hold information about the objects that we want to represent in the database.

Attribute (屬性)Named column of a tableCan appear in any order, and the table will still be

the same

2-6

A case example : StayHome Video Rental CompanyThe information on branches (分店 ) is represented

by the Branch relation, with columns for attributes branchNo, street, city, state, zipCode, and mgrStaffNo.

The information on staff is represented by the Staff relation, with columns for attributes staffNo, name, position, salary, and branchNo.

Terminology of Relational ModelTerminology of Relational Model

2-7

An example of the Branch and Staff relations

2-8

Domain (定義域)A set of allowable (possible) values for one or more

attributesEvery attribute in a relational database is associated

with a domainMay be distinct for each attribute. Or, two or more

attributes may be associated with the same domain

Terminology of Relational ModelTerminology of Relational Model

2-9

Domain ExampleDomain Example(For Some Attributes of (For Some Attributes of BranchBranch Relation Relation

2-10

Tuple (值組)A record of a relation In the Staff relation, each record contains five

values, one for each attribute. A Relational Database

A collection of normalized tables with distinct table names.

Terminology of Relational ModelTerminology of Relational Model

2-11

Seven Properties of Relational TablesSeven Properties of Relational Tables

1. Table name is distinct from all other table names in the database.

2. Each column has a distinct name in a table.3. Each cell of a table contains exactly one value.

To store several telephone number for a single branch in a single cell is wrong(No repeating groups.)

A table satisfying this property is said to be normalized or in first normal ( 正規化 ) form.

2-12

Seven Properties of Relational TablesSeven Properties of Relational Tables

4. Values of a column are all from the same domain

5. Each record is distinct.

There are no duplicate records.

6. Order of columns has no significance

(To users and programmers)

7. Order of records has no significance.

(To users and programmers)

2-13

Relational KeysRelational Keys

Candidate key (候選鍵) Primary key (主鍵) Foreign key (外部鍵、外來鍵、外鍵)

2-14

Candidate KeyCandidate Key ((候選鍵候選鍵)) A column, or a set of columns, that uniquely identifies a

record within a table. Uniqueness (唯一性) : In a table, the value of

the candidate key uniquely identifies a record Irreducibility (不可減少性,最小性 ) : No proper

subset of the candidate key has the uniqueness property The key cannot be made smaller or simpler for

unique identification There may be many candidate keys for a table When a key consists of more than one column, we call

it a composite key (組合鍵) .

2-15

Candidate KeyCandidate Key ((候選鍵候選鍵))

• Can city be a candidate key?• Can zipCode be a candidate key?• Can branchNo be a candidate key?• Can mgrStaffNo be a candidate key?

Branch relation

2-16

Candidate KeyCandidate Key ((候選鍵候選鍵))

Can Can actorNoactorNo be a be a candidate key?candidate key?

Role TableThe characters played by actors in videos

Can Can catalogNocatalogNo be a be a candidate key?candidate key?

actorNoactorNo + catalogNocatalogNo??

actorNoactorNo + + charactercharacter??

2-17

KeysKeys

Primary KeyPrimary Key ((主鍵主鍵)) The candidate key selected to identify records uniquely The candidate key selected to identify records uniquely

for a given table.for a given table. A table A table always hasalways has a primary key. a primary key. The Worst Case: The Worst Case:

The entire set of columns serve as the primary keyThe entire set of columns serve as the primary key

2-18

Rules To Select Primary KeyRules To Select Primary Key

With minimal number of attributes

Less likely to have its values changed (or never changed)

Less likely to lose uniqueness in the future

Always have values

With fewest characters (for those with textual attribute)

With the smallest maximum value (for numerical attribute)

Easiest to use from the users’ point of view

2-19

Foreign KeyForeign Key ((外部鍵、外索引外部鍵、外索引)) A column, or set of columns, within one table that

matches the primary key of some (possibly same) table.

When a column appears in more than one table, its

appearance represents a relationship between the

records of the two tables

2-20

Relational KeysRelational KeysParent/Home

Table

Primary Key

Foreign Key

Child Table

2-21

Functions Functions of Keysof Keys

1. Who is the manager of the branch at Portland, OR ?

2. Who are the managers of the branch at Seattle, WA ?

3. Who is Sally Adams’s manager ?

2-22

Relational Keys of COMPANY Database(PK and FK)

2-23

Representing Relational Databases(The StayHome Database)

Branch ( branchNo, street, city, state, zipCode, mgStaffNo)Staff ( staffNo, name, position, salary, branchNo)Video ( catalogNo, title, category, dailyRental, price, directorNo)Director ( directorNo, directorName)Actor ( actorNo, catalogNo)Role ( actorNo, catalogNo, character)Member ( memberNo, fName, lName, address)Registration ( branchNo, memberNo, staffNo, dateJoined)RentalAgreement ( rentalNo, dateOut, dateReturn, memberNo, videoNo)VideoForRent ( videoNo, available, catalogNo, branchNo)

dataexample

dataexample

2-24

Data example of StayHome DB (1)

2-25

Data example of StayHome DB (2)

2-26

Relational IntegrityRelational Integrity((關聯完整限制式、關聯的完整性關聯完整限制式、關聯的完整性)) Ensures that the data of a database is accurate

Constraints that apply to the entire database

Entity Integrity (實體完整限制式) Referential Integrity (參照完整限制式)

Before defining these two constraints, let’s look at

Null ( 虛值) Business Rules

2-27

NullNull ((虛值、空值虛值、空值)) Represents a value for a column that is currently

unknown or is not applicable ( 不適用 ) for a record. Deals with incomplete or exceptional data.

ExamplesA branch temporarily without a manager

2-28

Business Rules Business Rules (企業規則)(企業規則) Specify additional constraints that the data must satisfy. Represented in the form of rules that define or constrain

some operations of the organization.

Example A member can only rent a maximum of 10 videos

at any one time

2-29

Entity IntegrityEntity Integrity(實體完整限制式、實體的完整性)(實體完整限制式、實體的完整性) Applies to the primary key of a base table. In a base table, no column of a primary key can be null. A base table is a named table whose records are

physically stored in the database. Note: A view is a ‘virtual table’ that does not actually exist

in the database but is generated by the DBMS from some underlying base tables dynamically.

2-30

Referential IntegrityReferential Integrity(參照完整限制式、參照的完整性)(參照完整限制式、參照的完整性) If foreign key exists in a table,

either the foreign key value of a record in the table must match a candidate key value of some record in its parent/home table,

or the foreign key value must be wholly null. Example:

Not possible to create a staff record with branch number B300 (since B300 does not exist.)

Can create a new staff record with a null branch number if null value is allowed

Recommended