0% found this document useful (0 votes)
44 views61 pages

Chapter2 Session2

The document discusses relational databases and their components. It describes the basic structure of a relational database as consisting of a collection of tables, with each table assigned a unique name. Tables contain rows called tuples and columns called attributes. Tables can be related through shared attributes. It also covers key concepts like relation schemes, database schemas, entity integrity, referential integrity, and different types of keys such as candidate keys, primary keys, and foreign keys. Common relationship types like one-to-one, one-to-many, and many-to-many are also summarized.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views61 pages

Chapter2 Session2

The document discusses relational databases and their components. It describes the basic structure of a relational database as consisting of a collection of tables, with each table assigned a unique name. Tables contain rows called tuples and columns called attributes. Tables can be related through shared attributes. It also covers key concepts like relation schemes, database schemas, entity integrity, referential integrity, and different types of keys such as candidate keys, primary keys, and foreign keys. Common relationship types like one-to-one, one-to-many, and many-to-many are also summarized.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 61

Database Systems

Chapter 2: Database Design


session 2:
Relational Data Model

1
Outline

1 Relational Data model

2 ER to Relational Mapping

2
Relational Database
Basic structure:
 A relational database consists of a collection of tables, each of
which is assigned a unique name.
 Tables are also known as =Relation.
 The columns in a relation are known as attributes. The order of
attributes is insignificant
 A tuple is a row of a relation. They are also called the records.
• Each tuple is unique.
• The order of tuples is insignificant
 Tables are related to each other through some shared attributes.

3
Relational Database
Tập hợp các giá trị được phép cho mỗi
thuộc tính được gọi là miền của thuộc tính
Giá trị thuộc tính (thông thường) được
yêu cầu là nguyên tử; đó là, không thể
phân chia.
Giá trị null là một giá trị đặc biệt có nghĩa
là giá trị không được biết hoặc không tồn
tại..
Giá trị null gây ra các biến chứng trong
định nghĩa của nhiều hoạt động

4
Relational Database
Example: Student Relation

5
Relational Database
Example: Instructor Relation attributes
(or columns)

tuples
(or rows)

6
Relation Scheme
It is a named of a relation defined by a set of attributes
and their corresponding domains.
Common convention:
 RelationName (attribute1, attribute2,….,attribute_n)
 The primary key is underlined.
Example:
 Instructor (ID, name, dept_name, salary)
 Branch(branchNo, street, city, postcode)

7
Relation Schema
 Characteristics of relation scheme

8
Relational Database Schema
 It is a sets of relation schema.
Example: the relational database schema for
COMPANY = {EMPLOYEE, DEPARTMENT, DEPENDENT, PROJECT}

9
Relational Database Schema
The relation schema normally represented as
follows:
EMPLOYEE (SSN, FNAME, LNAME, MINIT, BDATE, ADDRESS,
SEX, SALARY, SUPERSSN,DNO)
DEPARTMENT (DNUM, DNAME, MGRSSN, MGRSTTDATE)
Relation instance
 Is a tuple at a specific moment of time
 Eg: Branch (BranchNo, Street, City, PostalCode )
The relation instance for branch is:
(B005, 55 Jln Dobi, Johor Bahru, 80100)
(B006, 55 Jalan Perai, Johor Bahru, 80000)
 The relation instance change when tuple is updated,
deleted or inserted.

10
Relation Keys
Refers to the important attribute in an entity.
Determine the uniqueness of an row in given table.
Identifiers for each rows.
An attribute or more than one attributes can be
declared as keys depending on situations.
Types of keys:
 Superkey
 Candidate Key
 Alternative key
 Primary Key
 Foreign key

11
Superkey
Superkey is a an attribute, or set of attributes which can
uniquely identify a tuple in a relation.
Example: Student( StudentId, firstName, lastName, courseId)

The super key can be any of the following:


StudentId
StudentId, lastName
StudentId, firstName, lastName

12
Candidate Key
 A candidate key is a minimal superkey. A superkey that
does not contain a subset of attributes that is itself a
superkey. This means you cannot remove any fields from
candidate key, else it will not be able to uniquely identify
the rows.
There can be more than one candidate key in a table
When a key consists of more then one attribute it is
known as the composite key
Example: The candidate key for Student relation can be
any of the following:
StudentId
StudentId, lastName

13
Alternative Key
An alternate key is any candidate key that is not
primary key.
Alternate keys are sometimes referred as
secondary keys
The secondary key is defined as a key that is used
strictly for data retrieval purposes

14
Primary key
 A primary key is one of the candidate keys selected
to uniquely identify a tuple in a relation.
Which one?
 Primary keys must be chosen with care. Ex: name, SSN, ID,…
 The primary key should be chosen such that its attribute
values are never, or are very rarely, changed. Ex: address,
mobile,…
Each table must have primary key.
Cannot be NULL value to maintain Entity Integrity.
Example: for Student relation
 StudentId can be chosen to be the primary key

15
Foreign Key
A foreign key is an attribute (or a set of attributes) whose
values match the primary key values in related relation.
Referencing relation
Referenced relation
Example

• The attributeCourse(courseId,
courseId is thecourseName)
primary key of Course
Student(StudentId, firstName, lastName, courseId)
• The attribute courseId in Student relation is a foreign key
• Student relation is called referencing relation of the foreign key
constraint.
• Course relation is called the referenced relation

16
Foreign Key
Example

17
Foreign Key
Another Example
 Branch (branchNo, street, city, postCode)
 Staff (staffNo, fName, lName, position, sex, DOB, salary,
branchNo)
 Staff has a Foreign Key is branchNo references Branch (branchNo)

18
Relation Keys
Group discussion

19
Integrity rules
To have a good design, a database must have
integrity rules.
Constraint or restriction that apply to all instances
of the database.
Integrity rules consists of
  Entity Integrity
 Referential Integrity

20
Entity Integrity
Requirement
 All Primary Key entries are unique, and no part of a primary key
may be NULL.
Purpose
 Each row will have a unique identity, and foreign key values can
properly reference primary key values
Example
 In the Employee table, EmpNo is the primary key, it can not have a
duplicate number (All employees are UNIQUELY identified by their
EmpNo number). And it can not be NULL.
 The OrderDetail has a composite primary key OrderNo and
ProductNo so to insert a new row both values must be known.

21
Entity Integrity
 Other integrity rules that can be enforced in the
relational model are the NOT NULL and UNIQUE
constraints.
 The NOT NULL constraint can be placed on a column to
ensure that every row in the table has a value for that
column.
 The UNIQUE constraint is a restriction placed on a column to
ensure that no duplicate values exist for that column

22
Referential Integrity
 Requirement
 Every non-null foreign key value must reference an existing
primary key value in the referenced relation. Or
 The foreign key value can be null.
Purpose
 Makes it possible for an attribute NOT to have a
corresponding value, but will be impossible to have an invalid
entry.
 The enforcement of the referential integrity rules makes it
impossible to delete a row in one table whose primary keys
has mandatory matching foreign key values on another table.

23
Referential Integrity
 Example: Branch and Staff Relation.

24
Referential Integrity
 Example: Branch and Staff Relation.
 It is not possible to create a staff record in Staff Relation with
branchNo B025, unless there is already record for branch
B025 in Branch relation.
 However, we should be able to create new staff
record with NULL branch number to allow the situation
where a new member staff has joined the company but
has not yet assigned to a particular Branch.

25
Các mối quan hệ trong Cơ sở dữ
liệu quan hệ.
Mối quan hệ 1:M là lý tưởng mô hình quan hệ.
Do đó, loại mối quan hệ này phải là tiêu chuẩn
trong bất kỳ thiết kế cơ sở dữ liệu quan hệ nào..
Mối quan hệ 1: 1, nên hiếm trong bất kỳ thiết
kế cơ sở dữ liệu quan hệ nào.
Mối quan hệ M:N,không thể được thực hiện như
vậy trong mô hình quan hệ. Sau này trong phần
này, bạn sẽ thấy bất kỳ mối quan hệ M: N nào
cũng có thể được thay đổi thành hai mối quan hệ
1: M.

26
1-M Relationship
Ví dụ: Mối quan hệ 1:M của ERM giữa KHÓA HỌC và
CLASS.
 Mỗi khóa học có thể có nhiều CLASSes, nhưng mỗi lớp
tham khảo chỉ có một khóa học.

27
1-M Relationship
The implemented 1:M relationship between PAINTER and
PAINTING in relational database

28
1-1 Relationship
Example: The ERM’s 1:1 relationship between
PROFESSOR and DEPARTMENT
 one department chair—a professor—can chair only one
department, and one department can have only one
department chair.

29
1-1 Relationship
 The implemented 1:1 relationship between
PROFESSOR and DEPARTMENT in relational database

30
M-N relationship
 The ERM’s M:N relationship between STUDENT and
CLASS
 Each CLASS can have many STUDENTs, and each
STUDENT can take many CLASSes.

31
M-N relationship
 The ERM’s M:N relationship between STUDENT and CLASS
 Each CLASS can have many STUDENTs, and each
STUDENT can take many CLASSes.

In relational database, many-to-many (M:N) relationship can easily be


implemented by creating a composite entity (also referred to as a
bridge entity or an associative entity).

32
M-N relationship
 Cấu trúc thực thể tổng hợp bao gồm - dưới dạng
khóa nước ngoài - ít nhất là các phím chính của
các bảng sẽ được liên kết.
 Nhà thiết kế cơ sở dữ liệu có hai tùy chọn chính khi xác
định khóa chính của bảng tổng hợp:
• Sử dụng kết hợp các khóa nước ngoài hoặc.
• tạo khóa chính mới.
Thực thể tổng hợp được gọi là bảng liên kết khi
thực hiện một thực thể tổng hợp trong cơ sở dữ
liệu quan hệ.

33
M-N relationship
Converting the M:N relationship into two 1:M relationships

34
Relationships within the
Relational Database
The relational diagram for the Ch03_TinyCollege
database

35
ER Model to Relational mapping
Lập bản đồ ERM (Mô hình quan hệ thực thể) với mối
quan hệ.
 Bước 1: Lập bản đồ các loại thực thể thông thường.
 Bước 2: Lập bản đồ các loại thực thể yếu.
 Bước 3: Lập bản đồ các loại mối quan hệ nhị phân 1:1.
 Bước 4: Lập bản đồ các loại mối quan hệ nhị phân 1:N.
 Bước 5: Lập bản đồ các loại mối quan hệ nhị phân M:N.
 Bước 6: Lập bản đồ các loại Mối quan hệ Khôngary.
 Bước 7: Lập bản đồ các thuộc tính đa giá trị.
 Bước 8: Lập bản đồ các loại mối quan hệ n-ary.
 Bước 9: Lập bản đồ mối quan hệ Supertype/Subtype.

36
Step 1: Mapping strong/regular Entity
Đối với mỗi thực thể mạnh mẽ / thường xuyên, hãy
tạo một mối quan hệ bao gồm tất cả các thuộc tính
đơn giản.
 Khóa chính của thực thể trở thành chìa khóa chính của
mối quan hệ.
 Loại trừ thuộc tính đa giá trị khỏi mối quan hệ ánh
xạ.
 Example:

Nhân viên (EmployeeID, FirstName, LastName,


Giới tính) Lưu ý: Điện thoại là một thuộc tính đa giá
trị.
Step 1: Mapping strong/regular Entity

Khi một loại thực thể thông thường có thuộc tính


tổng hợp, chỉ các thành phần đơn giản của thuộc
tính tổng hợp được bao gồm trong mối quan hệ
mới làm thuộc tính của nó.
 Example:

Nhân viên (EmployeeID, FirstName, LastName,


House_Number, Street, City, PostalCode).
Step 2: Mapping of Weak Entity
 Tạo mối quan hệ riêng biệt và bao gồm tất cả các thuộc
tính đơn giản.
 Chìa khóa chính của mối quan hệ là sự kết hợp của tất cả các
thuộc tính chính từ chủ sở hữu và khóa một phần của thực
thể yếu, nếu có..

=> Người phụ thuộc (EmployeeID, DependentID, Tên,


DOB, Giới tính, Mối quan hệ).
Nhân viên (EmployeeID, FirstName, LastName, Giới tính).
Step 3: Mapping Binary 1:1
relationship
Bước 3: Lập bản đồ mối quan hệ Nhị phân
1:1.
Trước khi giải quyết mối quan hệ 1:1, chúng ta
cần biết mối quan hệ của nó tùy chọn.
Có ba khả năng mối quan hệ có thể:
1. bắt buộc ở cả hai đầu.
2. bắt buộc ở một đầu và tùy chọn ở đầu kia.
3. tùy chọn ở cả hai đầu
Step 3: Mapping Binary 1:1
relationship
1. Mandatory at both ends.
Kết hợp hai thực thể thành một khi mối quan hệ
là bắt buộc ở cả hai đầu.
 Việc lựa chọn loại thực thể nào phân loại cái kia phụ thuộc vào loại thực thể
quan trọng nhất (nhiều thuộc tính hơn, khóa tốt hơn, bản chất ngữ nghĩa
của chúng).
 Khóa của loại thực thể được nhập khẩu trở thành một thuộc tính bình
thường.
Khi không kết hợp mối quan hệ bắt buộc 1:1:
 Hai loại thực thể đại diện cho các thực thể khác nhau trong 'thế giới thực'’.
 các thực thể tham gia vào các mối quan hệ rất khác nhau với các thực thể khác.
 cân nhắc hiệu quả khi yêu cầu phản hồi nhanh hoặc các mẫu cập nhật khác nhau
xảy ra với hai loại thực thể khác nhau.
=> Chìa khóa chính của một loại thực thể đến khóa nước ngoài trong loại kia.
Step 3: Mapping Binary 1:1
relationship
Example: Hai loại thực thể; nhân viên và hợp đồng.
 Mỗi thành viên của nhân viên phải có một hợp đồng và mỗi hợp
đồng phải có một thành viên của nhân viên liên kết với nó.
 Do đó, đây là một mối quan hệ bắt buộc ở cả hai đầu..
Step 3: Mapping Binary 1:1
relationship
3 tùy chọn để lập bản đồ:
 Kết hợp hai thực thể thành một.
Nhân viên (EmployeeID, FirstName, LastName, Giới tính,
ContractID, StartDate, StartEnd, Vị trí, Mức lương).
hoặc giữ riêng biệt và một khóa nước ngoài được sử
dụng.
Hợp đồng nhân viên (EmployeeID, FirstName, LastName,
Giới tính, Hợp đồngID) (ContractID, StartDate, StartEnd, Vị trí,
Mức lương).
Or.
Nhân viên (EmployeeID, FirstName, LastName, Giới tính) .
Hợp đồng (ContractID, Startdate, StartEnd, Vị trí, Mức lương,
Nhân viênID).
Step 3: Mapping Binary 1:1
relationship
2. Bắt buộc ở một đầu và tùy chọn ở đầu kia.
Lấy khóa chính từ 'kết thúc bắt buộc' và thêm nó vào 'kết
thúc tùy chọn' làm khóa nước ngoài.
Loại thực thể của đầu tùy chọn có thể được đưa vào kết
thúc bắt buộc như trong ví dụ trước.
Tốt hơn là KHÔNG liệt kê kết thúc bắt buộc vào cuối tùy
chọn vì điều này sẽ tạo ra các mục null.
Với các loại thực thể A và B, trong đó A, B đang ở trong
một mối quan hệ mà A kết thúc nó tùy chọn, kết quả sẽ là:
A (khóa chính, thuộc tính,...,tương khóa B).
B (khóa chính, thuộc tính,...).
Step 3: Mapping Binary 1:1
relationship
2. Bắt buộc ở một đầu và tùy chọn ở đầu kia.
Ví dụ: Hợp đồng là tùy chọn (mỗi nhân viên có thể có nhiều
nhất một hợp đồng).

=> Nhân viên (EmployeeID, FirstName, LastName, Giới tính) .


=> Hợp đồng (ContractID, Startdate, StartEnd, Vị trí, Mức
lương, Nhân viênID).
Step 3: Mapping Binary 1:1
relationship
2. Tùy chọn ở cả hai đầu.
Sử dụng cách tiếp cận khóa nước ngoài.
Ví dụ: Mỗi nhân viên có thể thuê tối đa một chiếc xe, Mỗi
chiếc xe có thể được thuê bởi nhiều nhất một nhân viên..

=>Không thể kết hợp hai thực thể thành một.


Nhân viên (EmployeeID, FirstName, LastName, Giới tính) .
Xe hơi (RegID, Mô hình, Năm, Màu sắc, Nhân viênID).
Step 4: Mapping Binary 1:M
relationship
Chìa khóa chính ở 'một bên' của mối quan hệ được thêm vào
'nhiều bên' như một chìa khóa nước ngoài..
Example:

=> Department(DepartmentID, DempartmentName, Phone)


=> Employee(EmployeeID, FirstName, LastName, Gender,
DepartmentID)
Step 4: Mapping Binary 1:M
relationship
Mối quan hệ song song Xảy ra khi có hai hoặc nhiều mối quan
hệ giữa hai loại thực thể.
Example:

Để phân biệt giữa hai vai trò, chúng ta có thể đặt cho các
phím nước ngoài tên khác nhau.
=>Employee(EmployeeID, FirstName, LastName, Gender,
DepartmentID)
=>Department(DepartmentID, DepartmentName, ManagerID)
Step 5: Mapping Binary M:N
relationship
Tạo mối quan hệ mới chứa các khóa chính của cả hai loại thực
thể tham gia và thuộc tính mô tả (if any).
Các phím chính này tạo thành một khóa chính tổng hợp về mối
quan hệ mới.

=> WorksOn(EmployeeID, ProjectID, Hours)


Step 6: Mapping Unary
relationship
Các mối quan hệ không công chứng (hoặc
các mối quan hệ đệ quy) là một mối quan hệ
trong đó một mối quan hệ có thể tồn tại giữa
các sự kiện của cùng một tập hợp thực thể.
Example:

1-1 relationship 1-M relationship M-N relationship


Step 6: Mapping Unary
relationship
Mapping the 1:1 unary relationship “EMPLOYEE is married
to EMPLOYEE”
=> Employee( EmpNum, Emp_Lname, Emp_Fname,
Emp_Spouse)
Implementation

=>Khóa nước ngoài là chìa khóa chính của cùng một


bảng, nhưng được đặt tên khác..
Step 6: Mapping Unary
relationship
Mapping the 1:M unary relationship “EMPLOYEE manages
EMPLOYEE”
=> Employee( Emp_Code, Emp_Lname, Emp_Manager)
Implementation

=>Khóa nước ngoài là chìa khóa chính của cùng một


bảng, nhưng được đặt tên khác..
Step 6: Mapping Unary
relationship
Lập bản đồ mối quan hệ không công chứng M-
N“KHÓA HỌC YÊU CẦU KHÓA HỌC”.
=> Course( Crs_code, Dept_code, Crs_Description, Crs_credit)
=> PreReq( Crs_code, Pre_Take)
Step 7: Mapping Multivalued
Attributes
Tạo một liên hệ mới R cho mỗi thuộc tính đa giá trị.
Thêm khóa chính của thực thể gốc vào quan hệ mới
R dưới dạng khóa nước ngoài.
Chìa khóa chính của R là sự kết hợp của R và thực
thể ban đầu. Nếu thuộc tính đa giá trị là tổng hợp,
chúng tôi bao gồm các thành phần đơn giản của nó.

=> Phone(EmployeeID, PhoneNumber, type)


=>Employee(EmployeeID, FirstName, LastName, Gender)
Step 7: Mapping Multivalued
Attributes
 Another example: In ER model

Trong mối quan hệ shema:


=>Kỹ năng (SkillCode, Tiêu đề kỹ năng, Loại kỹ
năng).
=>Sở hữu (EmployeeID, SkillCode).
=>Nhân viên (EmployeeID, Tên nhân viên).
Step 8: Mapping n-ary Relationship
types
Lập bản đồ loại mối quan hệ Ternary (và n-ary):
 Một mối quan hệ cho mỗi thực thể và một cho thực thể
liên kết.
 Thực thể liên kết có chìa khóa nước ngoài cho mỗi
thực thể trong mối quan hệ.
.
Step 8: Mapping n-nary Relationship
types
Example

In relational schema


=>Vendor( VendorID,…)
=>Warehouse(WareHouseID,…)
=>Part(PartID,…)
=>Supply(VendorID, WareHouseID, PartID, shippingMode,
UnitCode)
Step 8: Mapping n-nary Relationship
types
Another example:

In relational schema


Mapping Supertype/Subtype
Relationships
Một mối quan hệ cho supertype và cho mỗi phân
nhóm.
Supertype Thuộc tính (bao gồm mã định danh và
phân biệt đối xử phân biệt đối xử) đi vào mối
quan hệ siêu kiểu.
Thuộc tính phân nhóm đi vào từng phân nhóm;
chìa khóa chính của mối quan hệ siêu kiểu cũng
trở thành chìa khóa chính của mối quan hệ phân
nhóm.
Mối quan hệ 1: 1 được thiết lập giữa siêu kiểu
và mỗi phân nhóm, với supertype là bảng chính.
59
Mapping Supertype/Subtype
Relationships
Example

Mapping

60
61

You might also like