Chapter2 Session2
Chapter2 Session2
1
Outline
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)
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.
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:
Để 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.
Mapping
60
61