Tải bản đầy đủ (.ppt) (35 trang)

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

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (225.9 KB, 35 trang )

<span class='text_page_counter'>(1)</span><div class='page_container' data-page=1>

Mơ hình dữ liệu



Bài giảng 3



</div>
<span class='text_page_counter'>(2)</span><div class='page_container' data-page=2>

Nội dung trình bày



<b><sub>Mơ hình mạng (Network Model)</sub></b>



<sub>Mơ hình phân cấp (Hierachical Model)</sub>


<sub>Mơ hình thực thể - kết hợp </sub>



(Entity-Relationship)



</div>
<span class='text_page_counter'>(3)</span><div class='page_container' data-page=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)



</div>
<span class='text_page_counter'>(4)</span><div class='page_container' data-page=4>

Ví dụ



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


S2 Jones 10 Paris


</div>
<span class='text_page_counter'>(5)</span><div class='page_container' data-page=5>

Ví dụ




300


400 300 400 200


200


S1 Smith 20 London


S3 Blake 30 Paris
S2 Jones 10 Paris


</div>
<span class='text_page_counter'>(6)</span><div class='page_container' data-page=6>

Nội dung trình bày



<sub>Mơ hình mạng (Network Model)</sub>



<b><sub>Mơ hình phân cấp (Hierachical Model)</sub></b>



<sub>Mơ hình thực thể - kết hợp </sub>



(Entity-Relationship)



</div>
<span class='text_page_counter'>(7)</span><div class='page_container' data-page=7>

Mơ hình phân cấp



Giống mơ hình mạng nhưng mỗi nút chỉ



</div>
<span class='text_page_counter'>(8)</span><div class='page_container' data-page=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


</div>
<span class='text_page_counter'>(9)</span><div class='page_container' data-page=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 <sub> S3 Blake 30 Paris 200</sub>
S1 Smith 20 London 300


S1 Smith 20 London 400


</div>
<span class='text_page_counter'>(10)</span><div class='page_container' data-page=10>

Nội dung trình bày



<sub>Mơ hình mạng (Network Model)</sub>



<sub>Mơ hình phân cấp (Hierachical Model)</sub>



<b><sub>Mơ hình thực thể - kết hợp </sub></b>




<b>(Entity-Relationship)</b>



</div>
<span class='text_page_counter'>(11)</span><div class='page_container' data-page=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).


</div>
<span class='text_page_counter'>(12)</span><div class='page_container' data-page=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.


<b>Employees</b>


</div>
<span class='text_page_counter'>(13)</span><div class='page_container' data-page=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


</div>
<span class='text_page_counter'>(14)</span><div class='page_container' data-page=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



</div>
<span class='text_page_counter'>(15)</span><div class='page_container' data-page=15>

Ví dụ 1 – Đáp án



<b>Students</b>
<b>Name</b> <b>Login</b>
<b>Id</b>


<b>Age</b>


<b>Gpa</b>


<b>Courses</b>


<b>Id</b> <b>Name</b> <b>Credit</b>


</div>
<span class='text_page_counter'>(16)</span><div class='page_container' data-page=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



</div>
<span class='text_page_counter'>(17)</span><div class='page_container' data-page=17>

Ví dụ 2 – Đáp án



<b>Patients</b>


<b>Name</b> <b>Addr</b> <b>Phone</b>
<b>Age</b>


<b>Drug</b>


<b>Name</b> <b>Manuf</b> <b>Exp</b>


</div>
<span class='text_page_counter'>(18)</span><div class='page_container' data-page=18>

Ràng buộc khóa



Xét mối quan hệ


<b>Works_In</b>

: 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



</div>
<span class='text_page_counter'>(19)</span><div class='page_container' data-page=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


</div>
<span class='text_page_counter'>(20)</span><div class='page_container' data-page=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



</div>
<span class='text_page_counter'>(21)</span><div class='page_container' data-page=21>

Ví dụ 3 – Đáp án




<b>Students</b>
<b>Name</b> <b>Login</b>
<b>Id</b>


<b>Age</b>


<b>Gpa</b>


<b>Courses</b>


<b>Id</b> <b>Name</b> <b>Credit</b>


<b>Enrolled_In</b>


</div>
<span class='text_page_counter'>(22)</span><div class='page_container' data-page=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.


<b>lot</b>
<b>name</b>



<b>age</b>
<b>pname</b>


<b>ssn</b> <b>cost</b>


</div>
<span class='text_page_counter'>(23)</span><div class='page_container' data-page=23>

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



<b>Contract_Emps</b>
<b>name</b>


<b>ssn</b>


<b>Employees</b>


<b>lot</b>


<b>hourly_wages</b>


<b>ISA</b>


<b>Hourly_Emps</b>


<b>contractid</b>
<b>hours_worked</b>


As in C++, or other


PLs, attributes are


inherited.




</div>
<span class='text_page_counter'>(24)</span><div class='page_container' data-page=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

<b>ISA</b>

:



</div>
<span class='text_page_counter'>(25)</span><div class='page_container' data-page=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


<i> Mối kết hợp Tam phân và Aggregation</i>:


 Monitors is a distinct relationship,


with a descriptive attribute.


</div>
<span class='text_page_counter'>(26)</span><div class='page_container' data-page=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


</div>
<span class='text_page_counter'>(27)</span><div class='page_container' data-page=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



</div>
<span class='text_page_counter'>(28)</span><div class='page_container' data-page=28>

Thực thể và Thuộc tính



 Liệu <b>address</b> 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


<b>address:</b>



 Nếu mỗi nhân viên có nhiều địa chỉ thì <b>address</b> 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à


</div>
<span class='text_page_counter'>(29)</span><div class='page_container' data-page=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


</div>
<span class='text_page_counter'>(30)</span><div class='page_container' data-page=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


</div>
<span class='text_page_counter'>(31)</span><div class='page_container' data-page=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


</div>
<span class='text_page_counter'>(32)</span><div class='page_container' data-page=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.


</div>
<span class='text_page_counter'>(33)</span><div class='page_container' data-page=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


</div>
<span class='text_page_counter'>(34)</span><div class='page_container' data-page=34>

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



Nhiều ràng buộc tồ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.


</div>
<span class='text_page_counter'>(35)</span><div class='page_container' data-page=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


</div>

<!--links-->

×