Mô hình dữ liệu

Chia sẻ bởi Nguyễn Ngọc Oanh | Ngày 19/03/2024 | 3

Chia sẻ tài liệu: mô hình dữ liệu thuộc Công nghệ thông tin

Nội dung tài liệu:

10 Mar 2003
Nguyen Giang Son
1
Mô hình dữ liệu
Bài giảng 3
Nguyễn Giang Sơn
10 Mar 2003
Nguyen Giang Son
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)
Mô hình quan hệ (Relation Model)
10 Mar 2003
Nguyen Giang Son
3
Mô hình mạng – Khái niệm
Dữ liệu = Tập hợp các bản ghi (biểu diễn bằng các nút)
Mối quan hệ = Sets (biểu diễn bằng các cạnh)
Mối quan hệ là rõ ràng.
10 Mar 2003
Nguyen Giang Son
4
Ví dụ
300
400
300
400
200
200
10 Mar 2003
Nguyen Giang Son
5
Ví dụ
300
400
300
400
200
200
10 Mar 2003
Nguyen Giang Son
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)
Mô hình quan hệ (Relation Model)
10 Mar 2003
Nguyen Giang Son
7
Mô hình phân cấp
Giống mô hình mạng nhưng mỗi nút chỉ có một nút cha
10 Mar 2003
Nguyen Giang Son
8
Mô hình phân cấp
10 Mar 2003
Nguyen Giang Son
9
Mô hình phân cấp
10 Mar 2003
Nguyen Giang Son
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)
Mô hình quan hệ (Relation Model)
10 Mar 2003
Nguyen Giang Son
11
Tổng quan về thiết kế cơ sở dữ 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).
Can map an ER diagram into a relational schema.
10 Mar 2003
Nguyen Giang Son
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.
Each attribute has a domain.
10 Mar 2003
Nguyen Giang Son
13
Khái niệm (tt.)
Mối quan hệ: Association among 2 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 sets, or in different “roles” in same set.
10 Mar 2003
Nguyen Giang Son
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
Receive a grade
10 Mar 2003
Nguyen Giang Son
15
Ví dụ 1 – Đáp án
Name
Id
Id
Enrolled_In
10 Mar 2003
Nguyen Giang Son
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
Dosage, # Days
10 Mar 2003
Nguyen Giang Son
17
Ví dụ 2 – Đáp án
10 Mar 2003
Nguyen Giang Son
18
Ràng buộc khóa
Xét mối quan hệ Works_In: 1 nhân viên có thể làm việc trong nhiều phòng; 1 phònh có nhiều nhân viên.
Ngược lại, mỗi phònh chỉ có nhiều nhất 1 trưởng phòng.
10 Mar 2003
Nguyen Giang Son
19
Participation Constraints
Mọi phòng ban đều có trưởng phòng?
Nếu đúng, đây là 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 Manages table (with a non-null ssn value!)
10 Mar 2003
Nguyen Giang Son
20
Ví dụ 3
Mô tả lược đồ của ví dụ 1 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
Receive a grade
10 Mar 2003
Nguyen Giang Son
21
Ví dụ 3 – Đáp án
10 Mar 2003
Nguyen Giang Son
22
Thực thể yếu
Một thực thể yếu (weak entity) có thể được nhận biết khi xét khóa chính của thực thể chủ nhân khác.
Tập thực thể chủ nhân và tập thực thể yếu cùng 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.
10 Mar 2003
Nguyen Giang Son
23
Quan hệ phân cấp ISA (`is a’)
As in C++, or other PLs, attributes are inherited.
If we declare A ISA B, every A entity is also considered to be a B entity.
10 Mar 2003
Nguyen Giang Son
24
Quan hệ phân cấp ISA (`is a’)
Ràng buộc chồng chéo: Liệu Joe vừa là Hourly_Emps vừa là Contract_Emps? (Allowed/disallowed)
Ràng buộc phủ: Có phải chỉ có hai loại nhân viên Hourly_Emps hoặc Contract_Emps? (Yes/no)
Lý do sử dụng ISA:
Cung cấp thêm thuộc tính cho lớp con subclass.
Nhận biết thực thể tham dự vào mối kết hợp.
10 Mar 2003
Nguyen Giang Son
25
Aggregation
Được dùng khi một mối kết hợp bao hàm nhiều thực thể hoặc 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 relationship set.
Mối kết hợp Tam phân và Aggregation:
Monitors is a distinct relationship,
with a descriptive attribute.
Also, can say that each sponsorship
is monitored by at most one employee.
budget
did
pid
started_on
pbudget
dname
until
Departments
Projects
Sponsors
Monitors
lot
name
ssn
Aggregation
10 Mar 2003
Nguyen Giang Son
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
Amount in factory/warehouses
Should we use aggregation or a tertiary relationship?
10 Mar 2003
Nguyen Giang Son
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ể hoặc thuộc tính?
Thực thể hoặc mối quan hệ?
Nhận biết mối quan hệ: Hai ngôi, ba ngôi hay kết tập (aggregation)?
10 Mar 2003
Nguyen Giang Son
28
Thực thể và Thuộc tính
Liệu address là thuộc tính của Employees hay là một thực thể (nối với Employees bằng một quan hệ)?
Phụ thuộc vào ngữ nghĩa và cấu trúc của dữ liệu address:
Nếu mỗi nhân viên có nhiều địa chỉ thì address phải là một thực thể (vì thuộc tính không thể là một tập giá trị).
Nếu địa chỉ là một dữ liệu có cấu trúc (city, street, etc.) và 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ố).
10 Mar 2003
Nguyen Giang Son
29
Thực thể và 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 descriptive attributes for each instance of this relationship.
name
Departments
ssn
lot
Employees
Works_In3
10 Mar 2003
Nguyen Giang Son
30
Thực thể và 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 depts?
budget
10 Mar 2003
Nguyen Giang Son
31
Nhị phân hoặc Tam phân
Nếu mỗi chính sách được sở hữu bởi chỉ một nhân viên:
Key constraint on Policies would mean policy can only cover 1 dependent!
age
pname
Dependents
Covers
age
pname
Dependents
Purchaser
Bad design
Better design
10 Mar 2003
Nguyen Giang Son
32
Nhị phân hoặc Tam phân (tt.)
Ví dụ trên minh họa trường hợp hai mối quan hệ hai ngôi thì tốt hơn 1 mối quan hệ ba ngôi.
Một ví dụ khác: mối quan hệ 3 ngôi Contracts giữa 3 thực thể Parts, Departments và Suppliers, với thuộc tính qty. Không có sự thay thế thỏa đáng mối quan hệ 3 ngôi 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.
How do we record qty?
10 Mar 2003
Nguyen Giang Son
33
Tóm tắt quá 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 hierarchies, and aggregation.
Note: There are many variations on ER model.
10 Mar 2003
Nguyen Giang Son
34
Tóm tắt mô hình ER
Nhiều ràng buộc toàn vẹn có thể 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.
Constraints play an important role in determining the best database design for an enterprise.
10 Mar 2003
Nguyen Giang Son
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 especially useful.
* Một số tài liệu cũ có thể bị lỗi font khi hiển thị do dùng bộ mã không phải Unikey ...

Người chia sẻ: Nguyễn Ngọc Oanh
Dung lượng: | Lượt tài: 0
Loại file:
Nguồn : Chưa rõ
(Tài liệu chưa được thẩm định)