Top Banner
ence: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 資資資資資 萬萬萬 萬萬萬萬萬萬 萬萬萬萬萬萬
20

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

資料庫設計

萬序恬台北醫學大學 資訊服務中心

Page 2: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2

DBMS 觀念複習:What Is a DBMS?

一組大量且整合度高的資料 將真實世界模組化

實體: Entities (e.g., 學生 , 課程 ) 關係: Relationships (e.g., 蔡一零選修電腦概

論 ) 資料庫管理系統 (Database Management

System , DBMS) : 負責儲存與管理資料的套裝軟體 .

Page 3: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3

DBMS 觀念複習: 檔案與 DBMS 的差異 為了處理大量的資料,必須對於資料的存取方式作特

別的設計,甚至必須設計到硬體的支援上, 例如主記憶體的配置,硬碟使用規劃等 (e.g., buffering, page-oriented access, 32-bit addressing, etc.)

查詢必須使用特殊的程式語法 於多人使用的環境下,仍舊必須保持資料的完整以及

統一性 系統當機的復原機制設計 安全性及登入設計

Page 4: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4

DBMS 觀念複習: Why Study Databases??

從使用電腦計算到利用電腦提供資訊 work smart!

資料越來越多,也越來越多樣 Datasets increasing in diversity and volume. Digital libraries, interactive video, Human

Genome project, EOS project 需要資料庫管理系統支援

DBMS 是電腦科學發展的重心項目之一 OS, languages, theory, AI, multimedia, logic

?

Page 5: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5

資料模型 (Data Models) 資料模型 (data model) :描述性資料的概念集

合 . 定義 (schema ) :使用資料模型對於特定資料

集合的描述 關聯式模型 (relational model ) 是最廣泛被使

用的資料模型 . 主要概念: relation-basically a table with rows

and columns. Every relation has a schema, which describes

the columns, or fields.

Page 6: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6

摘要程度 views→conceptual

(logical) schema→physical schema. Views: 描述使用者看到資料的

模樣

Conceptual schema ( 概念性定義 ): 定義邏輯性結構

Physical schema ( 實質性定義 ): 描述使用到的檔案及索引(indexes).

Schemas are defined using DDL; data is modified/queried using DML.

Physical Schema

Conceptual Schema

View 1 View 2 View 3

Page 7: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7

Example: University Database

Conceptual schema: Students(sid: string, name: string, login: string,

age: integer, gpa:real) Courses(cid: string, cname:string, credits:integer) Enrolled(sid:string, cid:string, grade:string)

Physical schema: Relations stored as unordered files. Index on first column of Students.

External Schema (View): Course_info(cid:string,enrollment:integer)

Page 8: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8

實體 - 關係模型

ER-model (Entity-Relationship Model)

Page 9: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9

Overview

概念性設計 (Conceptual design)─ ER Model ER Model 目標

• 定義企業中的實體 entities 與關係 relationships• 列出這些在資料庫儲存的實體以及關係的必備資訊• 定出需要遵守「商業法則」 business rules 或是特殊限制

ER Model 中的 database `schema’ ,可以使用ER diagrams ( 實體關係圖, ERD) 來表示

Page 10: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10

ER Model 基礎

實體 Entity: 真實世界的可獨立描述的物件, 屬性 attributes : 用以描述此物件的特性 An

entity is described (in DB) using a set of 實體集 Entity Set: 上述實體的集合 E.g., all

employees. 所有在實體集內的每一個實體都有相同的屬性選項 .

(Until we consider ISA hierarchies, anyway!) 每一個實體有一個「鍵」 key, 用以指標至此實體 (

鍵的分類見後說明 )

Employees

ssnname

lot

Page 11: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11

ER Model 基礎 (II)

關係 Relationship: 兩個以上的實體之間的關係 . E.g., 萬序恬在資訊服務中心上班 .

關係集 Relationship Set: 類似的關係的集合 相同的實體集合可能當中有不同的角色而有不同的

關係集合 ( 如圖二 )

lot

dname

budgetdid

sincename

Works_In DepartmentsEmployees

ssn

Reports_To

lot

name

Employees

subor-dinate

super-visor

ssn

Page 12: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12

完整性限制 (Integrity Constraints, ICs)

IC: 可以通用在每一筆資料庫內的資料的規則 e.g., domain constraints. 設定定義時必須遵守所有的 IC. 有任何關係變動時也要檢查是否符合 IC.

一筆合法 legal 的資料必須能滿足所有確認過的 IC. 資料庫管理系統應該不能讓不合法的資料輸入 .

如果DBMS 能檢查 ICs, 則儲存在這裡面的資料才能和真實世界的對應 . 當然也要避免輸入錯誤的資料 !

Page 13: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13

主鍵限制 Key Constraints

每一個員工可以在很多個單位服務,每一個單位有很多名員工 .

每一個部門只能有一位主管 , 根據管理規章→ key constraint.

Many-to-Many1-to-1 1-to Many Many-to-1

dname

budgetdid

since

lot

name

ssn

ManagesEmployees Departments

Page 14: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14

主鍵限制 (Primary Key)

主鍵定義 :1. 同一個 ( 組 )欄位內,沒有任何兩筆資料的值是重複

的。2. 其他的欄位都無法符合 1 的情形。 Part 2 false? A superkey. 如果有一個欄位以上符合 1 的情形,則管理者可以自行指定一個欄位為主鍵 primary key.

E.g., sid is a key for Students. (What about name?) The set {sid, gpa} is a superkey.

Page 15: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16

外部鍵 (Foreign Keys→Referential Integrity)

外部鍵 Foreign key : 一個 ( 組 )和主鍵相關的欄位可以指向另外一個資料表中的某一筆資料 ( 通常都是另外那筆資料的主鍵 ).

E.g. sid is a foreign key referring to Students: Enrolled(sid: string, cid: string, grade: string) If all foreign key constraints are enforced, referential

integrity is achieved, i.e., no dangling references. Can you name a data model w/o referential integrity?

• Links in HTML!

Page 16: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 18

參與限制 Participation Constraints

每一個部門現在都有一位主管嗎 ? 如果是,那就有 participation constraint: the participation

of Departments in Manages is said to be “total”.• total 都有• partial 部分有

ERD 表示:

lot

name dnamebudgetdid

sincename dname

budgetdid

since

Manages

since

DepartmentsEmployees

ssn

Works_In

Page 17: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19

使用 ER Model進行概念性設計 設計步驟 :

該作為實體?還是屬性? 該作為實體?還是關係? 確認關係:是二重 Binary 、三重 ternary 、還是集合

式 Aggregation ? ER Model 的限制 :

可以描述絕大部分的狀況 但是在現實社會,仍舊會有部分規則是無法使用 ERD 表

示出來的。

Page 18: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 20

實體與屬性 「地址 address 」應該是「員工資料表」的一個屬

性,還是另外一個實體 (和員工有關係的實體 )? 必須看我們要如何處理以及使用「地址」這個資訊

,以及這個資料的現實狀況 :• 如果每個員工都有很多個地址,那 address 應該要設

定為實體 entity (因為屬性值不能另外再設定它的屬性 ).

• 如果我們想要利用地址當中的城市或街道作為分類或是搜尋的條件, address 就必須設定為實體 entity (因為每一個屬性是單一且獨立不可再分的 ).

Page 19: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 25

總結 在資訊專案開發時,系統分析流程中的需求分析後就就進行概念設計 (Conceptual design) 高階描述資料如何被儲存

ER model 是最常被使用的方法 conceptual design 和人類思考模式相近 .

基本元素 : 實體 entities, 關係 relationships, and 屬性 attributes (of entities and relationships).

附加元素 : weak entities, ISA hierarchies, and aggregation.

附註 : ER model 仍舊有許多變異的用法 .

Page 20: Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 資料庫設計 萬序恬 台北醫學大學 資訊服務中心.

Reference: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 26

總結 (II)

ER 設計常常是主觀的 . 所以同樣的狀況,可能每個人設計出的模型都不一樣。分析各種不同則更是複雜。特別是大型企業,因為規則太複雜了。常會發生的設計問題如: Entity vs. attribute, entity vs. relationship, binary

or n-ary relationship, whether or not to use ISA hierarchies, and whether or not to use aggregation.

要確定是否為「好」的資料庫設計:需要進一步的分析與修正,有其他的技術可以支持,例如: FD information 或是正規劃 (normalization)