introduction to database systems mô hình dữ liệu bài giảng 3 nguyễn giang sơn nguyen giang son 1 the slides for this text are organized into several modules each lecture contains about enough material

35 13 0
introduction to database systems mô hình dữ liệu bài giảng 3 nguyễn giang sơn nguyen giang son 1 the slides for this text are organized into several modules each lecture contains about enough material

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

thông tin từng trường là quan trọng (vd. Ta muốn tuyển chọn nhân viên theo thành phố), thì address phải là một thực thể (vì giá trị thuộc tính phải là nguyên tố)... Thực thể và Thuộc t[r]

(1)

Mơ hình liệu

Bài giảng 3

(2)

Nội dung trình bày

Mơ hình mạng (Network Model)

 Mơ hình phân cấp (Hierachical Model)  Mơ hình thực thể - kết hợp

(Entity-Relationship)

(3)

Mô hình mạng – Khái niệm

 Dữ liệu = Tập hợp ghi (biểu

diễn nút)

 Mối quan hệ = Sets (biểu diễn

cạnh)

(4)

Ví dụ

300 200 400 300 400 200 S1 Smith 20 London S3 Blake 30 Paris

S2 Jones 10 Paris

(5)

Ví dụ

300

400 300 400 200

200

S1 Smith 20 London

S3 Blake 30 Paris S2 Jones 10 Paris

(6)

Nội dung trình bày

 Mơ hình mạng (Network Model)

Mơ hình phân cấp (Hierachical Model)

 Mơ hình thực thể - kết hợp

(Entity-Relationship)

(7)

Mơ hình phân cấp

 Giống mơ hình mạng nút

(8)

Mơ hình phân cấp

P1 Nut Red 12 London P2 Bolt Green 17 Paris

P3 Screw Blue 17 Rome

P4 Screw Red 14 London S2 Jones 10 Paris 400 S2 Jones 10 Paris 300

S3 Blake 30 Paris 200 S1 Smith 20 London 300

S1 Smith 20 London 200

(9)

Mơ hình phân cấp

P1 Nut Red 12 London P2 Bolt Green 17 Paris

P3 Screw Blue 17 Rome

P4 Screw Red 14 London S2 Jones 10 Paris 300 S3 Blake 30 Paris 200 S1 Smith 20 London 300

S1 Smith 20 London 400

(10)

Nội dung trình bày

 Mơ hình mạng (Network Model)

 Mơ hình phân cấp (Hierachical Model)

Mơ hình thực thể - kết hợp

(Entity-Relationship)

(11)

Tổng quan thiết kế sở liệu

 Thiết kế mức khái niệm: (Mơ hình ER được dùng

mức này.)

 What are the entities and relationships in the enterprise?  What information about these entities and relationships

should we store in the database?

 What are the integrity constraints or business rules that hold?

 A database `schema’ in the ER Model can be represented pictorially (ER diagrams)

(12)

Khái niệm

 Thực thể: Real-world object distinguishable

from other objects An entity is described

(in DB) using a set of attributes

 Tập thực thể: A collection of similar entities

E.g., all employees

 All entities in an entity set have the same set of attributes (Until we consider ISA hierarchies, anyway!)

 Each entity set has a key

Employees

(13)

Khái niệm (tt.)

 Mối quan hệ: Association among or more entities

E.g., Attishoo works in Pharmacy department.

 Tập mối quan hệ: Collection of similar relationships.

 An n-ary relationship set R relates n entity sets E1 En; each relationship in R involves entities e1 E1, , en En

Same entity set could participate in different relationship

(14)

Ví dụ 1

 Thiết kế lược đồ ER theo thông tin sau:

 Sinh viên

 Have an Id, Name, Login, Age, Gpa

 Môn học

 Have an Id, Name, Credit Hours

 Sinh viên đăng ký mơn học

(15)

Ví dụ – Đáp án

Students Name Login Id

Age

Gpa

Courses

Id Name Credit

(16)

Ví dụ 2

 Thiết kế lược đồ ER theo thông tin sau:

 Bệnh nhân

 Name, Address, Phone #, Age

 Thuốc

 Name, Manufacturer , Expiration Date

 Bệnh nhân điều trị theo đơn thuốc

(17)

Ví dụ – Đáp án

Patients

Name Addr Phone Age

Drug

Name Manuf Exp

(18)

Ràng buộc khóa

 Xét mối quan hệ Works_In: nhân viên làm việc nhiều phịng; phịnh có nhiều nhân viên.

 Ngược lại,

phịnh có nhiều

(19)

Participation Constraints

 Mọi phòng ban có trưởng phịng?

 Nếu đúng, ràng buộc participation constraint: the participation

of Departments in Manages is said to be total (vs partial)

Every did value in Departments table must appear in a row of the

(20)

Ví dụ 3

 Mơ tả lược đồ ví dụ với đầy đủ ràng

buộc:

 Students

 Have an Id, Name, Login, Age, Gpa

 Courses

 Have an Id, Name, Credit Hours

 Students enroll in courses

(21)

Ví dụ – Đáp án

Students Name Login Id

Age

Gpa

Courses

Id Name Credit

Enrolled_In

(22)

Thực thể yếu

 Một thực thể yếu (weak entity) nhận biết

khi xét khóa thực thể chủ nhân khác

 Tập thực thể chủ nhân tập thực thể yếu tham gia vào

1 mối kết hợp 1-nhiêu (1 chủ nhân, nhiều thực thể yếu)

 Tập thực thể yếu phải tham gia đầy đủ vào tập kết hợp

identifying relationship set

lot name

age pname

ssn cost

(23)

Quan hệ phân cấp ISA (`is a’)

Contract_Emps name

ssn

Employees

lot

hourly_wages

ISA

Hourly_Emps

contractid hours_worked

 As in C++, or other PLs, attributes are inherited.

(24)

Quan hệ phân cấp ISA (`is a’)

 Ràng buộc chồng chéo: Liệu Joe vừa

Hourly_Emps vừa Contract_Emps?

(Allowed/disallowed)

 Ràng buộc phủ: Có phải có hai loại nhân

viên Hourly_Emps Contract_Emps?

(Yes/no)

 Lý sử dụng ISA:

(25)

Aggregation

 Được dùng

mối kết hợp bao

hàm nhiều thực thể mối kết hợp khác

 Aggregation allows

us to treat a

relationship set as an entity set for

purposes of participation in

(other) relationships

 Monitors mapped to

table like any other

Mối kết hợp Tam phân Aggregation:

 Monitors is a distinct relationship,

with a descriptive attribute

(26)

Ví dụ 4

 Thiết kế lược đồ ER theo thông tin sau:

 Walmart Stores

 Store Id, Address, Phone #

 Products

 Product Id, Description, Price

 Manufacturers

 Name, Address, Phone #

 Walmart Stores carry products

 Amount in store

 Manufacturers make products

(27)

Thiết kế mức khái niệm dùng mơ hình ER

 Các lựa chọn:

 Thực thể thuộc tính?  Thực thể mối quan hệ?

 Nhận biết mối quan hệ: Hai ngôi, ba

(28)

Thực thể Thuộc tính

 Liệu address thuộc tính Employees

một thực thể (nối với Employees quan hệ)?

 Phụ thuộc vào ngữ nghĩa cấu trúc liệu

address:

 Nếu nhân viên có nhiều địa address phải

thực thể (vì thuộc tính khơng thể tập giá trị)

 Nếu địa liệu có cấu trúc (city, street, etc.)

(29)

Thực thể Thuộc tính (tt.)

 Works_In2 does not

allow an employee to work in a department for two or more

periods

 Similar to the problem

of wanting to record several addresses for an employee: we want to record several

values of the

(30)

Thực thể Mối Kết Hợp  First ER diagram OK if a

manager gets a separate discretionary budget for each dept

 Redundancy of dbudget,

which is stored for each dept managed by the manager

 Misleading: suggests

dbudget tied to managed dept

 What if a manager gets a

discretionary budget that covers all managed

(31)

Nhị phân Tam phân

 Nếu

sách sở hữu một nhân viên:

 Key constraint

on Policies would mean policy can only cover

(32)

Nhị phân Tam phân (tt.)

 Ví dụ minh họa trường hợp hai mối quan

hệ hai ngơi tốt mối quan hệ ba ngơi.

 Một ví dụ khác: mối quan hệ Contracts

giữa thực thể Parts, Departments

Suppliers, với thuộc tính qty Khơng có thay thỏa đáng mối quan hệ này:

 S “can-supply” P, D “needs” P, and D “deals-with”

S does not imply that D has agreed to buy P from S

(33)

Tóm tắt q trình thiết kế mức khái niệm

 Thiết kế mức khái niệm phù hợp với phân tích yêu

cầu

 Yields a high-level description of data to be stored

 Mơ hình ER

 Constructs are expressive, close to the way people think

about their applications

 Basic constructs: entities, relationships, and

attributes (of entities and relationships)

 Some additional constructs: weak entities, ISA

(34)

Tóm tắt mơ hình ER

 Nhiều ràng buộc tồn vẹn biểu diễn

trong mơ hình ER: key constraints,

participation constraints, and overlap/covering

constraints for ISA hierarchies Some foreign

key constraints are also implicit in the definition of a relationship set.

 Some constraints (notably, functional

dependencies) cannot be expressed in the ER model

(35)

Tóm tắt mơ hình ER (tt.)

 ER design is subjective There are often many ways

to model a given scenario! Analyzing alternatives can be tricky, especially for a large enterprise Common choices include:

 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

 Ensuring good database design: resulting relational

schema should be analyzed and refined further FD information and normalization techniques are

Ngày đăng: 19/04/2021, 16:52

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan