chapter 2 e-r data model

26
資資資資資 1 Chapter 2 E-R Data Model

Upload: zeph-mcdonald

Post on 03-Jan-2016

78 views

Category:

Documents


3 download

DESCRIPTION

Chapter 2 E-R Data Model. Basic Concept. ER data model 將真實世界視為由實體 (entity) 與實體間的關係 (relationship) 所組成 ER data model 包含三種類別的符號 Entity sets Relationship sets Attribute. Entity Sets(1). entity 代表真實世界中的一個物件或事件,可為具體或抽象 ( 假日、帳目 ) Entity sets 是由一群具有相同型態,相同屬性的 entity 所組成. Entity Sets(2). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 2 E-R Data Model

資料庫系統 1

Chapter 2E-R Data Model

Page 2: Chapter 2 E-R Data Model

資料庫系統 2

Basic Concept

ER data model 將真實世界視為由實體 (entity)與實體間的關係 (relationship) 所組成

ER data model 包含三種類別的符號 Entity sets Relationship sets Attribute

Page 3: Chapter 2 E-R Data Model

資料庫系統 3

Entity Sets(1)

entity 代表真實世界中的一個物件或事件,可為具體或抽象 ( 假日、帳目 )

Entity sets 是由一群具有相同型態,相同屬性的 entity 所組成

Page 4: Chapter 2 E-R Data Model

資料庫系統 4

Entity Sets(2)

如一個銀行所有顧客的集合可以定義一個實體集合 customer

一個 entity 是由一組屬性 (attribute) 的集合所表示

每個 entity 的所屬每個 attribute 都有一個值(value)

每個 attribute 其值所允許的範圍稱為 domain或 value set

attribute 沒有數值時,其 value 為 null

Page 5: Chapter 2 E-R Data Model

資料庫系統 5

An Example of Entity and Attribute Customer 這個 entity sets 表示銀行所屬顧客

的資料 Customer 的 attribute 包含 customer-

id , customer-name , customer-birthmonth ,customer-money

某個顧客的 attribute value 為 D12345 , John ,5 , 1200

customer-birthmonth 的 domain 為 1~12 的整數, customer-money 的 domain 為正數浮點數

Page 6: Chapter 2 E-R Data Model

資料庫系統 6

The Characteristic of Attribute(1) Simple vs. Composite

Simple 是此 attribute 無法再分割為其他 attribute ,如存款金額

Composite 是此 attribute 可分割為其他 attribute ,如 name 可分割為 first-name 與 last-name

Single-valued vs. Multivalued Single-valued 只有單一值的屬性,如 name Multivalued 有複數個值的屬性,如 phone

Page 7: Chapter 2 E-R Data Model

資料庫系統 7

The Characteristic of Attribute(2) Derived 是由其他 attribute 所衍生出來的,如

age 是由 birthyear 所衍生

Page 8: Chapter 2 E-R Data Model

資料庫系統 8

Relationship Sets

• Relationship 表示實體之間的邏輯關係

123-45-4643 Mike L-1 100

487-42-1259 John L-2 500

953-46-7598 Tom L-3 850

521-97-2394 Smith L-4 250

684-08-0908 Louis L-5 1000

970-01-1505 Bob Loan

Customer

Page 9: Chapter 2 E-R Data Model

資料庫系統 9

Constraints of Relationships(1) 對映基數

One to one :在 A 的任一 entity 最多只能和 B 中一 entity 相連,反之亦然

One to many :在 A 的任一 entity 可和 B 中任意數的 entity 相連,但 B 任一 entity 最多只能和 A 中一entity 相連

Many to one : A 任一 entity 最多只能和 B 中一entity 相連,但在 B 的任一 entity 可和 A 中任意數的 entity 相連

Many to many :在 A 的任一 entity 可和 B 中任意數的 entity 相連,反之亦然

Page 10: Chapter 2 E-R Data Model

資料庫系統 10

Page 11: Chapter 2 E-R Data Model

資料庫系統 11

Constraints of Relationships(2) 參與限制

Total :在 A 中每個 entity 都至少有一個relationship 存在

Partial :在 A 中只有部分 entity 有 relationship 存在

Page 12: Chapter 2 E-R Data Model

資料庫系統 12

A 部分參與, B 完全參與 A 、 B 部分參與

Page 13: Chapter 2 E-R Data Model

資料庫系統 13

Key

在同一個 entity sets 中沒有任兩個 entity 的所有 attribute value 是完全一樣的

Key 是一組 attribute ,可用於區分不同的entity Superkey :由一個或多個 attribute 組成,如

{customer-id} 或 {customer-id, customer-name} Candidate key :沒有任何子集合是 superkey 的

superkey ,如 {customer-id} 或 {customer-name, customer-address}

Primary key :被選作識別 entity 的 candidate key

Page 14: Chapter 2 E-R Data Model

資料庫系統 14

Primary key 的選取原則

不會變動的 attribute 具有唯一性,不可重複

訂定編碼規則

Page 15: Chapter 2 E-R Data Model

資料庫系統 15

Weak entity set

Entity 沒有足夠的 attribute 來組成一個 PK ,需和其他 entity 產生關聯才有意義

例如 課程資料 course 這個 entity 有 attribute :

courseid 、 coursename 開課紀錄 courseoffering 這個 entity 有 attribute :

offeringid 、 semester 、 section courseoffering 單獨存在是沒有意義的,所以它是

weak entity set

Page 16: Chapter 2 E-R Data Model

資料庫系統 16

實作- DB System Management Bank System-customer Create table (entity set) Create field and set type (attribute) Primary key

Page 17: Chapter 2 E-R Data Model

資料庫系統 17

CREATE TABLE bank(BankID char(20), BankName char(50),Post int, Address char(50), Primary key(BankID));

Page 18: Chapter 2 E-R Data Model

資料庫系統 18

E-R diagram(1)

基本符號 長方形: entity sets 雙長方形: weak entity sets 菱形: relationship sets 雙直線: total participation 橢圓: attribute ,如是 PK 則加上 underline 雙橢圓: multivalued attribute 虛線橢圓: derived attribute

Page 19: Chapter 2 E-R Data Model

資料庫系統 19

E-R diagram(2)

直線:連接 attribute 、 entity sets 、 relationship sets

使用 與 表示 entity sets 與 relationship sets 的 one to one 、 one to many 、 many to one 、 many to many 的關係

Page 20: Chapter 2 E-R Data Model

資料庫系統 20

BorrowerCustomer

Lastname

loanid

address age

birthdayFirstname

name loan-id

amount

合成屬性

One to many

Branchid

name

address

三元關係

Page 21: Chapter 2 E-R Data Model

資料庫系統 21

限制表示方式

在直線上標示 l..h l 是最小基數

1 代表完全參與 h 是最大基數

1 代表最多只能有一個 relationship *表示沒有限制

1..* 和雙直線一樣意思

Page 22: Chapter 2 E-R Data Model

資料庫系統 22

BorrowerCustomer

Lastname

loanid

address age

birthdayFirstname

name loan-id

amount

0..* 1..1

Page 23: Chapter 2 E-R Data Model

資料庫系統 23

Schema Diagram

customer

id

firstname

lastname

birthday

address

borrower

customer-id

loan-id

loan

loan-id

amount

Page 24: Chapter 2 E-R Data Model

資料庫系統 24

Hint

不應該出現在 ER Diagram 功能 (Function) -查詢、列印 系統 (System) -選課系統 例子 (Example) -通識課、軍訓課、作業系統

Page 25: Chapter 2 E-R Data Model

資料庫系統 25

Homework(2)

請以選課系統為例子,劃出它的 ER diagram與 Schema Diagram , diagram 的 entity sets至少需包含 course , student , teacher

Page 26: Chapter 2 E-R Data Model

資料庫系統 26

Discussion

習題 2-17 你認為哪種作法比較好