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]
Trang 1Mô hình dữ liệu
Bài giảng 3 Nguyễn Giang Sơn
Trang 2Nộ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)
Trang 3Mô hình mạng – Khái niệm
diễn bằng các nút)
cạnh)
Trang 5P2 Bolt Green 17 Paris P3 Screw Blue 17 Rome
Trang 6Nộ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)
Trang 7Mô hình phân cấp
có một nút cha
Trang 8Mô 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
Trang 9Mô 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 200S1 Smith 20 London 300
S1 Smith 20 London 400
S2 Jones 10 Paris 400 S1 Smith 20 London 200
Trang 10Nộ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)
Trang 11Tổng quan về thiết kế cơ sở dữ
A database `schema’ in the ER Model can be
represented pictorially (ER diagrams)
Can map an ER diagram into a relational schema
Trang 12Khá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
Employees
Trang 13Khá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
dinate Reports_To
subor-lot name
Employees
visor
super-ssn
lot
dname
budget did
since name
Works_In Departments Employees
ssn
Relationship Set
Trang 14 Have an Id, Name, Credit Hours
Sinh viên đăng ký môn học
Receive a grade
Trang 16 Name, Manufacturer , Expiration Date
Bệnh nhân điều trị theo đơn thuốc
Dosage, # Days
Trang 17Dosage #days
Trang 18since lot
name ssn
Manages Employees Departments
Key Constraint
Trang 19Participation 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!)
lot
budget did
since
budget did
since
Manages
since
Departments Employees
ssn
Works_In
Total w/key constraint Partial
Total Total
Trang 20 Have an Id, Name, Credit Hours
Students enroll in courses
Receive a grade
Trang 22Thự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
lot
name
age pname
Primary Key
for weak entity
Trang 23Quan hệ phân cấp ISA (`is a’)
Contract_Emps
name ssn
Trang 24Quan 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?
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
Trang 25 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
budget did
pid
started_on
pbudget
dname until
Departments Projects Sponsors
Employees
Monitors
lot
name ssn
Aggregation
Trang 26 Name, Address, Phone #
Walmart Stores carry products
Trang 27Thiết kế mức khái niệm dùng
mô hình ER
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)?
Trang 28Thự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
Trang 29Departments
Trang 30Thự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
budget Manages2
did Employees Departments
Departments
Manages3
Trang 31Nhị phân hoặc Tam phân
Dependents Covers
name Employees
Policies policyid cost
Beneficiary
age pname
Dependents
Policies Purchaser
name Employees
Bad design
Better design
Trang 32Nhị 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?
Trang 33 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
Trang 34Tó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
Trang 35Tó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