72
A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of Memphis Presentation by: Amita Goyal Chin, Ph.D. Virginia Commonwealth University John Wiley & Sons, Inc. Modified by Walter Chen Dept. of Civil Engineering National Taipei University of Technology For classroom teaching purpose

A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

A Short Introduction to DBMS

Fundamentals of Database Management Systems

byMark L. Gillenson, Ph.D.

University of Memphis

Presentation by: Amita Goyal Chin, Ph.D.Virginia Commonwealth University

John Wiley & Sons, Inc.

Modified by Walter ChenDept. of Civil EngineeringNational Taipei University of TechnologyFor classroom teaching purpose

Page 2: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

2

What is Data? A single piece of data is a single fact about

something that interests us. A fact can be any characteristic of an

object. 一項事實可以是一個物件的任何特徵

Salesperson Number

Salesperson Name

City

State

Office Number

Commission Percentage

Year of Hire

137 Baker Detroit MI 1284 10 1995 Figure 2.1 Facts about salesperson Baker.

Page 3: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

3

Records and Files

Entity - a “thing” or “object” in our environment that we want to keep track of. 實體

Entity set - A collection of entities of the same type (e.g., all of the company’s employees). 實體群

Salesperson Number

Salesperson Name

City

State

Office Number

Commission Percentage

Year of Hire

119 Taylor New York NY 1211 15 2003 137 Baker Detroit MI 1284 10 1995 186 Adams Dallas TX 1253 15 2001 204 Dickens Dallas TX 1209 10 1998 255 Lincoln Atlanta GA 1268 20 2003 361 Carlyle Detroit MI 1227 20 2001 420 Green Tucson AZ 1263 10 1993

Figure 2.2 Salesperson file.

Page 4: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

4

Records and Files

Attribute - a property of, a characteristic of, or a fact that we know about an entity. 屬性

Some attributes have unique values within an entity set. 有些屬性有獨特的值

Salesperson Number

Salesperson Name

City

State

Office Number

Commission Percentage

Year of Hire

119 Taylor New York NY 1211 15 2003 137 Baker Detroit MI 1284 10 1995 186 Adams Dallas TX 1253 15 2001 204 Dickens Dallas TX 1209 10 1998 255 Lincoln Atlanta GA 1268 20 2003 361 Carlyle Detroit MI 1227 20 2001 420 Green Tucson AZ 1263 10 1993

Figure 2.2 Salesperson file.

Page 5: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

5

Records and Files

Record - each row of a structure like above 紀錄

Fields - the columns, representing the facts 欄位

File - the entire structure 檔案

Salesperson Number

Salesperson Name

City

State

Office Number

Commission Percentage

Year of Hire

119 Taylor New York NY 1211 15 2003 137 Baker Detroit MI 1284 10 1995 186 Adams Dallas TX 1253 15 2001 204 Dickens Dallas TX 1209 10 1998 255 Lincoln Atlanta GA 1268 20 2003 361 Carlyle Detroit MI 1227 20 2001 420 Green Tucson AZ 1263 10 1993

Figure 2.2 Salesperson file.

keyfield

Page 6: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

6

Retrieving and Manipulating Data Four fundamental operations can be

performed on stored data: Retrieve or Read - looking at a record’s

contents without changing it 讀取資料 Insert - adding a new record to the file, as

when a new salesperson is hired 插入資料 Delete - deleting a record from the file, as when

a salesperson leaves the company 刪除資料 Update - changing one or more of a record’s

field values 更新資料

Page 7: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

7

Essence of Data Modeling Exploring the different ways that entities

can relate to each other as they always do in the real world 真實世界中實體是彼此關連的

Devising a way of recording, of diagramming, the entities and the ways in which they interrelate in the business environment 畫出實體間的關係

Page 8: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

8

Entity-Relationship (E-R) Model A diagramming technique 一種繪圖的方式 Diagrams entities (with attributes) and the

relationship between the entities. 描述實體之間的關係

There are many variations of E-R diagrams in use. 有許多不同的 E-R 圖畫法

Page 9: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

9

E-R Model Entity (and its attributes)

Rectangular shape 長方形代表實體 Salesperson = a type of entity Name of entity is in caps above the

separator line. 實體的名稱放在分隔線上方

Page 10: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

10

E-R Model Entity (and its attributes)

Entity type’s attributes are shown below the separator line. 屬性放在分隔線下方

An asterisk denotes attribute(s) that constitute the entity type’s unique identifier. 星號標示關鍵屬性

Page 11: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

11

Relationships 關係 Associations between entities 代表實體之

間的關係 Different kinds:

Binary relationships 二元關係 Unary relationships 一元關係 Ternary relationships 三元關係

Page 12: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

12

Binary Relationships 二元關係

Simplest kind of relationship 最簡單的一種 Relationship between two entity types 用在兩個屬性之間 Diamond-shaped box represents relationship 菱形代表關

係 A salesperson “sells” products or products are sold by

salespersons 推銷員「賣」產品

Page 13: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

13

Cardinality 基數 Represents the maximum number of

entities that can be involved in a particular relationship. 代表關係中可以擁有的最多實體數目 One-to-One Binary Relationship 一對一二元關

係 One-to-Many Binary Relationship 一對多二元

關係 Many-to-Many Binary Relationship 多對多二元

關係

Page 14: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

14

One-to-One Binary Relationship 一對一二元關係

1-1 A single occurrence of one entity type can

be associated with a single occurrence of the other entity type and vice versa. 一個實體種類的單一個體僅可以連結另一個實體種類的單一個體

Page 15: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

15

One-to-Many Binary Relationship 一對多二元關係

1-M 一對多 Use “crow’s foot” to represent the multiple

association. 利用烏鴉腳符號來表示 “many” = the maximum number of

occurrences that can be involved, means a number that can be 1, 2, 3, ... n. 代表可以允許的最多個體數目

Page 16: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

16

Many-to-Many Binary Relationship 多對多二元關係

M-M 多對多 “many” can be either an exact number or

have a known maximum. 「多」可以是確切的數字或有已知的最大值

Page 17: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

17

Cardinality 基數

Page 18: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

18

Intersection Data 交叉表格 Describes the relationship between two

entities. 表達實體之間的關係 Used with many-to-many relationships. 用

在多對多關係中 Represented on E-R diagram in a special

five-sided intersection data box, which is attached to the relationship diamond between the two entity boxes. 用五邊形來表示,連接在菱形關係上

Page 19: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

19

Many-to-Many Binary Relationship with Intersection Data 多對多二元關係及交叉資料

For example, we know not only that salesperson 137 sold some of product 24013 but also how many units of that product that salesperson sold. 舉例來說,我們除了知道推銷員 137 賣了一些編號 24013 的產品,而且知道他賣了多少個

Page 20: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

20

Associative Entity 衍生實體 Entities can have attributes; many-to-many

relationships can have attributes. 多對多的關係也可以有屬性

Many-to-many relationship may be treated similarly to entities in an E-R diagram. 可以將交叉表當作一個實體

Page 21: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

21

Unary Relationships 一元關係 Associate occurrences of an entity type with other

occurrences of the same entity type. 一個實體種類的出現伴隨同樣實體種類的另一個出現

Cardinality: 基數 One-to-One Unary Relationship 一對一的一元

關係 One-to-Many Unary Relationship 一對多的一

元關係 Many-to-Many Unary Relationship 多對多的一

元關係

Page 22: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

22

Unary Relationships 一元關係

一對一之一元關係

一對多之一元關係

多對多之一元關係

Page 23: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

23

Ternary Relationship 三元關係 Involves three different entity types. 牽涉三種不同實體

種類

Page 24: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

24

Ternary Relationship: The General Hardware Company E-R Diagram 「通用五金公司」的 ER圖

Customer Employee, a dependent entity, is distinguished by a diagonal hash mark in each corner of its attribute area. 依賴實體: Customer Employee在四個角落有斜線

依賴實體

Page 25: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

25

The Database Concept 資料庫的概念

Data as a Manageable Resource 資料是可管理的資源

Data Integration and Data Redundancy 資料整合和資料重複

Multiple Relationships 多項關係性 Data Control Issues 資料管控問題 Data Independence 資料獨立

Page 26: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

26

Database Management System 資料庫管理系統

A software utility for storing and retrieving data that gives the end-user the impression that the data is well integrated even though the data can be stored with no redundancy at all. 一個軟體應用程式用來存取資料,給予使用者資料是良好整合的印象(雖然資料並不重複)

Page 27: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

27

Major DBMS Approaches 主要資料庫管理系統的架構

Hierarchical – navigational 階層式 Network – navigational 網路式 Relational - became commercially viable in

about 1980. 關連式:大約在 1980 年代取得商業應用

Object-oriented - useful for a variety of niche applications. 物件導向式:在一些特殊應用領域很有用

Page 28: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

28

Hierarchical & Network 階層式和網路式

Called navigational approaches because of the way that programs have to “navigate” through hierarchies and networks of data to find needed data. 稱為「航行式」的方法,因為程式需要能在資料階層或網路中尋覓資料

Developed in the 1960s and 在大約 1960年代開發出來

Somewhat similar in structure 在結構上類似

Page 29: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

29

Hierarchical & Network 階層式和網路式

Suitable only for mainframe computers 用於大型電腦主機

were an elegant solution to the redundancy/integration problem at the time 當時是解決資料重複和整合的優良解決方案

Complex, difficult to work 複雜、難以使用 Now considered legacy systems 現在視為

傳統舊式系統

Page 30: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

30

Relational Database 關連式資料庫

Became commercially viable in about 1980 大約在 1980 年代取得商業應用

Soon became the preferred DBMS approach and it has remained so ever since. 快速成為截至目前為止的資料庫管理系統的主流

Page 31: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

31

Relational Database Model 關連式資料庫模型

In 1970, E. F. Codd published “A Relational Model of Data for Large Shared Data Banks” in CACM. 最早於1970 年由 E. F. Codd 在論文中提出

In the early 1980s, commercially viable relational database management systems became available. 在 1980 年代,商用的關連式資料庫管理系統開始出現

Page 32: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

32

The Relational Database Concept 關連式資料庫概念

Data appears to be stored in what we have been referring to as simple, linear files. 資料似乎是儲存在簡單、線性檔案中

Relational databases are based on mathematics. 關連式資料庫源自數學

A relational database is a collection of relations that, as a group, contain the data that describes a particular business environment. 一個關連式資料庫是一群關係的組合,整體用來描述一個特定的商用環境

Page 33: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

33

Relational Terminology 關連術語

Relations - what we have been referring to as simple linear files. Also called tables. 關係:即我們所稱的簡單線性檔案,又稱為表格

Row = record (files) = tuple (relation) 橫列

Column = field (files) = attribute (relation) 欄位

Page 34: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

34

Relational Database Terminology關連式資料庫術語

Attribute (or Column or Field)

Tuple (or Row or Record)

Student Number

Student Name

Class

Major

03657 Robert Shaw Senior Biology 05114 Gloria Stuart Freshman English 05950 Fred Simpson Junior Mathematics 12746 W. Shin Junior English 15887 Pedro Marcos Senior History 19462 H. Yamato Sophomore French 21682 Mary Jones Freshman Chemistry 24276 Steven Baker Sophomore History

Page 35: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

35

Primary Key 關鍵索引

A relation always has a unique primary key. 一個關係永遠有一個獨特的關鍵索引

A primary key (also called “the key”) is an attribute or a group of attributes whose values are unique throughout all of the rows of the relation. 關鍵索引可以是單一或多個屬性的集合,只要它是獨特的

Page 36: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

36

Primary Key 關鍵索引Salesperson Number

Salesperson Name

Commission Percentage

Year Of Hire

137 Baker 10 1995 186 Adams 15 2001 204 Dickens 10 1998 361 Carlyle 20 2001

(a) SALESPERSON relation. Customer Number

Customer Name

Salesperson Number

HQ City

0121 Main St. Hardware 137 New York 0839 Jane’s Stores 186 Chicago 0933 ABC Home Stores 137 Los Angeles 1047 Acme Hardware Store 137 Los Angeles 1525 Fred’s Tool Stores 361 Atlanta 1700 XYZ Stores 361 Washington 1826 City Hardware 137 New York 2198 Western Hardware 204 New York 2267 Central Stores 186 New York

(b) CUSTOMER relation.

Page 37: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

37

Primary Key 關鍵索引 The number of attributes involved in the

primary key is always the minimum number of attributes that provide the uniqueness quality. 關鍵索引中所需的屬性數目是保證該關鍵索引為獨特所需的最小值

In the worst case, all of the relation’s attributes combined could serve as the primary key. 在最糟的情況下,使用所有的屬性當作關鍵索引

Page 38: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

38

Foreign Key 外部關鍵索引

An attribute or group of attributes that serves as the primary key of one relation and also appears in another relation (foreign key in this relation). 外部關鍵索引是一個或一組屬性,它既是一個關係的關鍵索引,又出現在另一個關係中

Page 39: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

39

Foreign Key 外部關鍵索引 Crucial in relational database, because the

foreign key is the mechanism that ties relations together to represent unary, binary, and ternary relationships. 外部關鍵索引是用來表達一元、二元、三元關係的工具,因此非常重要

Foreign key attribute must have same domain of values as Primary key attribute in other relation. 一個外部關鍵索引所能使用的「值域」必須與它在另一個關係中作為關鍵索引時相同

Page 40: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

40

Binary Relationships 二元關係

One-to-One 一對一 One-to-Many 一對多 Many-to-Many 多對多

Page 41: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

41

One-to-One Binary Relationship 一對一二元關係

Page 42: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

42

One-to-Many Binary Relationship 一對多二元關係

Salesperson Customer The Salesperson Number

foreign key in the CUSTOMER relation effectively establishes the one-to-many relationship between salespersons and customers. 由customer 表格中的Salesperson Number這個外部關鍵索引,建立salesperson 和 customer表格之間的一對多的關係

Page 43: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

43

Many-to-Many Relationship 多對多關係

Page 44: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

44

Intersection Data 交叉資料

Page 45: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

45

Unary One-to-Many Relationships 一元的一對多關係

A salesperson reports to exactly one sales manager, but each salesperson who does serve as a sales manager typically has several salespersons reporting to him. 一個銷售員只向一位經理報告,但是一位經理(也是銷售員)通常管理好幾位銷售員

There is a one-to-many relationship within salespersons. 這是銷售員的一對多的關係

Salesperson (also a sales manager) Salesperson

Page 46: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

46

Unary One-to-Many Relationships 一元的一對多關係 A unary relationship because there is only one

entity type involved. 「一元」代表只有一種實體 A one-to-many because among the individual

entity occurrences, that is, among the salespersons, a particular salesperson reports to one salesperson who is his sales manager, while a salesperson who is a sales manager may have several salespersons reporting to her. 「一對多」因為在銷售員實體中,一位銷售員向一位經理報告,但是一位經理(也是銷售員)通常管理好幾位銷售員

Page 47: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

47

General Hardware Co. Salesperson Reporting Hierarchy 通用五金公司銷售

員組織階層

Page 48: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

48

One-to-Many Unary Relationship 一對多一元關係

Salesperson Number

Salesperson Name

Commission Percentage

Year Of Hire

Sales Manager Number

137 Baker 10 1995 186 142 Smith 15 2001 137 170 Taylor 18 1992 439 186 Adams 15 2001 198 Wang 20 1990 267 204 Dickens 10 1998 267 267 Perez 22 2000 285 285 Costello 10 1996 323 McNamara 15 1995 137 361 Carlyle 20 2001 483 388 Goldberg 20 1997 483 411 Davidson 18 1992 137 439 Warren 10 1996 186 446 Albert 10 2001 483 483 Jones 15 1995 285

SALESPERSON Relation

Requires the addition of one column to the relation representing the single entity involved in the unary relationship. 需要額外加一欄代表單一實體參與一元關係

Page 49: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

49

Unary Many-to-Many Relationships 一元多對多關係

A special case, an example of which has come to be known as the bill of materials problem. 代表問題是零件的組成問題

Every entity occurrence can be related to many other occurrences. 每一個實體的「發生」可以連接到許多其他相同實體種類的「發生」或「出現」

Product Product

Page 50: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

50

General Hardware Company’s Product Set 通用五金公司產品組合

Wrench Model A (#11) Deluxe Wrench Set (#43) Wrench Model B (#14) Supreme Tool Wrench Model C (#17) Set (#53) Master Wrench Set (#44) Wrench Model D (#19) Hammer Model A (#22) Grand Tool Hammer Model B (#24) Deluxe Hammer Set (#48) Set (#56) Hammer Model C (#28) Drill Model A (#31) Drill Model B (#35) Figure 6.5 General Hardware Co. product bill of materials.

Tools and sets of tools are sold. 銷售單一工具和整組工具 Many-to-many nature of products. 產品實體的多對多關係

Page 51: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

51

Ternary Relationships 三元關係 Involves three different

entity types. 牽涉三個實體

Page 52: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

52

General Hardware Co.: Ternary Relationship 通用五金公司:三元關係

Salesperson Number

Salesperson Name

Commission Percentage

Year Of Hire

137 Baker 10 1995 186 Adams 15 2001 204 Dickens 10 1998 361 Carlyle 20 2001

(a) SALESPERSON relation.

Customer Number

Customer Name

HQ City

0121 Main St. Hardware New York 0839 Jane’s Stores Chicago 0933 ABC Home Stores Los Angeles 1047 Acme Hardware Store Los Angeles 1525 Fred’s Tool Stores Atlanta 1700 XYZ Stores Washington 1826 City Hardware New York 2198 Western Hardware New York 2267 Central Stores New York

(b) CUSTOMER relation

Product Number

Product Name

Unit Price

16386 Wrench 12.95 19440 Hammer 17.50 21765 Drill 32.99 24013 Saw 26.25 26722 Pliers 11.50

(c) PRODUCT relation.

Salesperson Number

Customer Number

Product Number

Date

Quantity

137 0839 24013 2/21/2002 25 361 1700 16386 2/27/2002 70 137 2267 19440 3/1/2002 40 204 1047 19440 3/1/2002 15 186 0839 26722 3/12/2002 35 137 1700 16386 3/17/2002 65 361 0121 21765 3/21/2002 40 204 2267 19440 4/03/2002 30 204 0839 19440 4/17/2002 20 (d) SALES relation.

Page 53: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

53

Referential Integrity 參考完整性 Revolves around the circumstance of

trying to refer to data in one relation in the database, based on values in another relation. 試圖根據另一個關係的值去查照資料庫中某一關係的資料

Page 54: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

54

Referential Integrity - Record Deletion 參考完整性:資料刪除

Salesperson Number

Salesperson Name

Commission Percentage

Year Of Hire

137 Baker 10 1995 186 Adams 15 2001 204 Dickens 10 1998 361 Carlyle 20 2001

(a) SALESPERSON relation. Customer Number

Customer Name

Salesperson Number

HQ City

0121 Main St. Hardware 137 New York 0839 Jane’s Stores 186 Chicago 0933 ABC Home Stores 137 Los Angeles 1047 Acme Hardware Store 137 Los Angeles 1525 Fred’s Tool Stores 361 Atlanta 1700 XYZ Stores 361 Washington 1826 City Hardware 137 New York 2198 Western Hardware 204 New York 2267 Central Stores 186 New York

(b) CUSTOMER relation

A problem arises, e.g., because a deleted record, a salesperson record, is on the “one side” of a one-to-many relationship. 刪除銷售員會有問題,因為刪除的資料是在一對多的「一」邊

不再知道 137是誰

Page 55: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

55

Referential Integrity – Insertion 參考完整性:插入資料

Insertion - if a new record is inserted into the “one side” (SALESPERSON relation) of the one-to-many relationship, there is no problem. 新資料若插入在一對多的「一」邊(銷售員表格)沒有問題

If a new customer record is inserted into the “many side” (CUSTOMER relation) of the one-to-many relationship and it happens to include a salesperson number that does not have a match in the SALESPERSON relation—that would cause the same kind of problem as the deletion example. 若新資料插入在一對多的「多」邊(顧客表格)而剛好沒有「一」邊(銷售員表格)的對應,問題來了

Page 56: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

56

Referential Integrity – Update 更參考完整性:新資料

Updating a foreign key value. 更新外部關鍵索引的值

For example, a salesperson number in the CUSTOMER relation with a new salesperson number that has no match in the SALESPERSON relation. 在顧客表格中更新銷售員編號,但是在銷售員表格中卻沒有對應的編號

Page 57: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

57

DBMS & Referential Integrity 資料庫管理系統和參考完整性

Early relational DBMSs did not provide any control mechanisms for referential integrity. 早期的 DBMS沒有提供管控參考完整性的功能

Modern relational DBMSs provide sophisticated control mechanisms for referential integrity: 現代的 DBMS 有複雜的參考完整性管控功能 Delete rules 刪除規則 Insert rules 插入規則 Update rules 更新規則

Page 58: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

58

Converting E-R Diagrams into Relational Tables 將 ER 圖轉換成為關

連表格 Each entity will convert to a table. 每一個實

體轉換成一個表格 Each many-to-many relationship or

associative entity will convert to a table. 多對多關係(或夥伴實體)也轉換成一個表格

During the conversion, certain rules must be followed to ensure that foreign keys appear in their proper places in the tables. 在轉換過程中,根據適當規則來決定何處要放外部關鍵索引

Page 59: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

59

Converting a Simple Entity 轉換一個簡單的實體

The table simply contains the attributes that were specified in the entity box. 表格中只是儲存實體裡的屬性資料

Salesperson Number is underlined to indicate that it is the unique identifier of the entity and the primary key of the table. 將 Salesperson Number加底線表示它是獨特的關鍵字,也是表格的關鍵索引

Salesperson Number

Salesperson Name

Commission Percentage

Year of Hire

SALESPERSON

Page 60: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

60

Converting Entities in Binary Relationships: One-to-One 轉換二元關

係的實體:一對一

一個表格 下限是 0 另當別論

Page 61: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

61

Converting Entities in Binary Relationships: One-to-Many 轉換二元關

係的實體:一對多

The unique identifier of the entity on the “one side” of the one-to-many relationship is placed as a foreign key in the table representing the entity on the “many side.” 把「一邊」的關鍵索引放到「多邊」的表格去當作外部關鍵索引

So, the Salesperson Number attribute is placed in the CUSTOMER table as a foreign key. 所以 Salesperson Number 放在 customer 表格當作外部關鍵索引

Page 62: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

62

Converting Entities in Binary Relationships: One-to-Many 轉換二元關

係的實體:一對多

Page 63: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

63

Converting Entities in Binary Relationships: Many-to-Many 轉換二元

關係的實體:多對多

E-R diagram with the many-to-many binary relationship and the equivalent diagram using an associative entity. 左邊是多對多二元關係的 ER 圖,右邊利用夥伴實體來表達,兩種表達方式是相同的

Page 64: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

64

Converting Entities in Binary Relationships: Many-to-Many 轉換二元

關係的實體:多對多 An E-R diagram with two entities in a many-to-

many relationship converts to three relational tables. 二元的多對多關係需要三個表格

Each of the two entities converts to a table with its own attributes but with no foreign keys (regarding this relationship). 兩個實體各轉換成一個表格,而且其中沒有外部關鍵索引

In addition, there must be a third “many-to-many” table for the many-to-many relationship. 此外,再加上第三個表格表達多對多的關係

Page 65: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

65

Converting Entities in Binary Relationships: Many-to-Many 轉換二元

關係的實體:多對多

P r o d u c tN u m b e r

P r o d u c tN a m e U n i t P r i c e

P R O D U C T

The primary key of SALE is the combination of the unique identifiers of the two entities in the many-to-many relationship. Additional attributes are the intersection data. 在 sale 表格中的關鍵索引是多對多關係中兩個實體的關鍵索引的組合,其他屬性是交叉資料

Page 66: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

66

Converting Entities in Unary Relationships: One-to-One 轉換一元關

係的實體:一對一 With only one entity type

involved and with a one-to-one relationship, the conversion requires only one table. 一元的一對一只需要一個表格

Page 67: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

67

Converting Entities in Unary Relationships: One-to-Many 轉換一元關

係的實體:一對多 Very similar to the one-

to-one unary case. 一元的一對多也是一個表格

Salesperson Number

Salesperson Name

Commission Percentage

Year of Hire

Manager

SALESPERSON

Page 68: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

68

Converting Entities in Unary Relationships: Many-to-Many 轉換一元

關係的實體:多對多

This relationship requires two tables in the conversion. 一元的多對多需要兩個表格

The PRODUCT table has no foreign keys. 注意PRODUCT 表格中沒有外部關鍵索引

Page 69: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

69

Converting Entities in Unary Relationships: Many-to-Many 轉換一元

關係的實體:多對多

A second table is created since in the conversion of a many-to-many relationship of any degree — unary, binary, or ternary — the number of tables will be equal to the number of entity types (one, two, or three, respectively) plus one more table for the many-to-many relationship. 產生第二個表格代表多對多關係,因為在多對多的關係中(一元、二元、三元),表格數總需要加一

Page 70: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

70

Converting Entities in Ternary Relationships 轉換三元關係的

實體

The primary key of the SALE table is the combination of the unique identifiers of the three entities involved, plus the Date attribute. 在 sale 表格中的關鍵索引是三個實體的關鍵索引的組合,加上日期屬性

Page 71: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

71

Designing the General Hardware Company Database 設計通用五金公司

資料庫

Page 72: A Short Introduction to DBMSwaltchen/files/dsap/db-short.pdf · A Short Introduction to DBMS Fundamentals of Database Management Systems by Mark L. Gillenson, Ph.D. University of

72

“Copyright 2004 John Wiley & Sons, Inc. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without express permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages caused by the use of these programs or from the use of the information contained herein.”