100
Relational Data Model 建建建建建建 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Embed Size (px)

Citation preview

Page 1: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Relational Data Model建置資料模式

(Based on Chapter 7 in Fundamentals of Database Systems

by Elmasri and Navathe, Ed. 4)

Page 2: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 2

Outline1 Relational Model Concepts2 Characteristics of Relations3 Relational Integrity Constraints

3.1 Key Constraints3.2 Entity Integrity Constraints3.3 Referential Integrity Constraints

4 Update Operations on Relations5 Relational Algebra Operations

5.1 SELECT and PROJECT 5.2 Set Operations5.3 JOIN Operations

5.4 Additional Relational Operations

Page 3: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 3

BASICS Data are stored in Relations(tables). A Relation is a mathematical concept

based on the ideas of sets. Relational Model has a strong theoret

ic foundation. The model was first proposed by Dr. E.

F. Codd of IBM in 1970.

Page 4: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 4

INFORMAL DEFINITIONS RELATION:

A table of values A relation may be thought of as a set of rows. A relation may alternatively be though of as

a set of columns. Each row of the relation may be given an ide

ntifier( 一筆資料的代號 ). Each column is called by its column name or

column header or attribute name.

Page 5: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 5

Page 6: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 6

FORMAL DEFINITIONS A Relation may be defined in

multiple ways.

The Schema of a Relation:R (A1, A2, .....An)

Relation R is defined over attributes A1, A2, ..…,An

The Schema of a Relation:R (A1, A2, .....An)

Relation R is defined over attributes A1, A2, ..…,An

Page 7: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 7

FORMAL DEFINITIONS (contd.) For Example

CUSTOMER (Cust-id, Cust-name, Address, Phone#)

Here, CUSTOMER is a relation defined over the four attributes Cust-id, Cust-name, Address, Phone#, each of which has a domain or a set of valid values. For example, the domain of Cust-id is 6 digit numbers.

Page 8: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 8

FORMAL DEFINITIONS (contd.) A tuple is an ordered set of values Each value is derived from an appropr

iate domain(合乎型態 ). Each row in the CUSTOMER table may

be called as a tuple in the table and would consist of four values.

Page 9: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 9

FORMAL DEFINITIONS (contd.) For example: <632895, "John Smith", "101 Main St.

Atlanta, GA 30332", "(404) 894-2000"> is a tuple belonging to the CUSTOMER relation.

Page 10: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 10

FORMAL DEFINITIONS (contd.) A relation may be regarded as a

set of tuples (rows). Columns in a table are also called

as attributes of the relation.

Page 11: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 11

DEFINITION SUMMARY

Informal Terms Table

ColumnRow

Value in a columnTable Definition

Populated Table( 內容 )

Formal Terms Relation

Attribute / DomainTuple

DomainSchema of a

relationExtension

Page 12: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 12

Notes: Whereas languages like SQL use the

informal terms of TABLE (e.g. CREATE TABLE), COLUMN (e.g. SYSCOLUMN variable), the relational database textbooks present the model and operations on it using the formal terms(I.e., relation).

Page 13: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 13

2 Characteristics of Relations Ordering of tuples in a relation

r(R): The tuples are not considered to be

ordered, even though they appear to be in the tabular form.

Page 14: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 14

2 Characteristics of Relations Ordering of attributes in a relation sch

ema R (and of values within each tuple): We will consider the attributes in R(A1, A2,

..., An) and the values in t=<v1, v2, ..., vn> to be ordered .

(However, a more general alternative definition of relation does not require this ordering).

Page 15: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 15

2 Characteristics of Relations Values in a tuple:

All values are considered atomic (indivisible)( 不可分的 ).

A special null value is used to represent values that are unknown or inapplicable to certain tuples.

Page 16: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 16

Notation( 表示法 ): We refer to component values of a tup

le t by t[Ai] = vi (the value of attribute Ai for tuple t).

Similarly, t[Au, Av, ..., Aw] refers to the subtuple of t containing the values of attributes Au, Av, ..., Aw, respectively.

Page 17: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 17

3 Relational Integrity Constraints

Constraints( 限制 ) are conditions that must hold on all valid relation instances.

There are three main types of constraints: Domain constraints( 欄位值合乎型態 ) Key constraints, ( 要有鍵值欄位 ) Entity integrity constraints, and ( 鍵值欄位不可

為空值 Null) Referential integrity constraints( 參考時必須參考

的到 )

Page 18: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 18

3.0 Domain Constraints Domain constraints specify that the v

alue of each attribute A must be atomic value from domain dom(A).( 值必須與型態一致 )

Page 19: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 19

3.1 Key Constraints Superkey of R:

A set of attributes SK of R such that no two tuples in any valid relation instance r(R) will have the same value for SK. That is, for any distinct tuples t1 and t2 in r(R), t1[SK] t2[SK].

Key of R: A "minimal" superkey; that is, a superkey K such

that removal of any attribute from K results in a set of attributes that is not a superkey.

Page 20: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 20

3.1 Key Constraints

Example: The CAR relation schema:

CAR(State, Reg#, SerialNo, Make, Model, Year) has two keys

Key1 = {State, Reg#}, Key2 = {SerialNo},

which are also superkeys. {SerialNo, Make} is a superkey but not

a key.

Page 21: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 21

3.1 Key Constraints If a relation has several candidate

keys, one is chosen arbitrarily to be the primary key.

The primary key attributes are underlined.

Page 22: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 22

Entity Integrity: t[PK] <> null for any tuple t in r(R)

The primary key attributes PK of each relation schema R in S cannot have null values in any tuple of r(R).

This is because primary key values are used to identify the individual tuples.

Page 23: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 23

Note: Other attributes of R may be

similarly constrained to disallow null values, even though they are not members of the primary key.

Page 24: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 24

3.3 Referential Integrity A constraint involving two relations (t

he previous constraints involve a single relation).

Used to specify a relationship among tuples in two relations: the referencing relation( 參考表格 ) and the referenced relation( 被參考表格 ).

Page 25: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 25

Foreign key Tuples in the referencing relation R1 have a

ttributes FK (called foreign key attributes) that reference the primary key attributes PK of the referenced relation R2.( 欄位的值為另一個表格的鍵值欄位 )

A tuple t1 in R1 is said to reference a tuple t2 in R2 if t1[FK] = t2[PK].(t1 參考 t2)

A referential integrity constraint can be displayed in a relational database schema as a directed arc from R1.FK to R2.

Page 26: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 26

Referential Integrity Referential integrity states that: For a

ny tuple t1 in R1 there exists a tuple t2 in R2 such that t1[FK] = t2[PK], otherwise, t1[FK] = NULL

簡言之,不能參考的到一個不存在的值

Page 27: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 27

Page 28: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 28

Page 29: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 29

4 Update Operations on Relations

There are three basic update operations on relations: INSERT a tuple. DELETE a tuple. MODIFY a tuple.

Integrity constraints should not be violated by the update operations.

Page 30: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 30

Several update operations may have to be grouped together. (transactions)

Updates may propagate to cause other updates automatically. This may be necessary to maintain integrity constraints.

Page 31: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 31

In case of integrity violation, several actions can be taken: cancel the operation that causes the violation

(REJECT option) perform the operation but inform the user of

the violation trigger additional updates so the violation is

corrected (CASCADE option, SET NULL option) execute a user-specified error-correction

routine

Page 32: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 32

5 The Relational Algebra( 關聯代數 ) 一組表示查詢的符號,用以表示查詢的

過程 Operations to manipulate relations. Used to specify retrieval requests (qu

eries). Query result is in the form of a relatio

n.

Page 33: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 33

5 The Relational Algebra Relational Operations: 5.1 SELECT and PROJECT

operations. 5.2 Set operations:

These include UNION ∪, INTERSECTION ∩, DIFFERENCE - , CARTESIAN PRODUCT .

5.3 JOIN operations . 5.4 Other relational operations:

DIVISION, OUTER JOIN, AGGREGATE FUNCTIONS.

Page 34: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 34

5.1 SELECT operation (denoted

by ): Selects the tuples (rows) from a

relation R that satisfy a certain selection condition c

Form of the operation: c(R) The condition c is an arbitrary

Boolean expression on the attributes of R

Page 35: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 35

5.1 SELECT operation (denoted

by ): Resulting relation has the same

attributes as R Resulting relation includes each

tuple in r(R) whose attribute values satisfy the condition c

Page 36: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 36

Boolean expression The Boolean expression specified in <selection con

dition> is made up of a number of clauses of the form:( 條件式的寫法 )

<attribute name> <comparison op> <constant value>, or <attribute name> <comparison op> <attribute name>

Where <attribute name> is the name of the attribute of R,

<comparison op> is normally one of the operators {=, <, , >, , }, and

<constant value> is a constant value from attribute domain.

Page 37: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 37

Boolean expression Clauses can be arbitrarily

connected by the Boolean operators AND, OR, and NOT to form a general selection condition.

Page 38: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 38

Examples: DNO=4(EMPLOYEE) SALARY>30000(EMPLOYEE) (DNO=4 AND SALARY>25000) OR (EMPLOYEE)

(DNO=5 AND SALARY>30000)

Page 39: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 39

5.1PROJECT operation (denoted by

):

Keeps only certain attributes (columns) from a relation R specified in an attribute list L( 所要取出的欄位 )

Form of operation: L(R) Resulting relation has only those attri

butes of R specified in L NAME,LNAME,SALARY(EMPLOYEE)

Page 40: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 40

Eliminates duplicate tuples Duplicate tuples are eliminated by the

operation. 重複資料刪除 The PROJECT operation eliminates du

plicate tuples in the resulting relation so that it remains a mathematical set (no duplicate elements)

Page 41: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 41

Example: SEX,SALARY(EMPLOYEE) If several male employees have

salary 30000, only a single tuple <M, 30000> is kept in the resulting relation.

Page 42: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 42

Page 43: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 43

Page 44: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 44

Sequences of operations: Several operations can be combined t

o form a relational algebra expression (query)

將運算組合成為查詢式子

Page 45: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 45

Example: Retrieve the names and salaries of em

ployees who work in department 4: FNAME,LNAME,SALARY (DNO=4 (EMPLOYEE) ) Relational algebra expression

Page 46: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 46

Sequences of operations Alternatively, we specify explicit inter

mediate relations for each step: DEPT4_EMPS DNO=4(EMPLOYEE) R FNAME,LNAME,SALARY(DEPT4_EMPS) 以多步驟表示查詢

Page 47: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 47

Sequences of operations Attributes can optionally be renamed

in the resulting left-hand-side relation :( 重新命名欄位 )

(this may be required for some operations that will be presented later) DEPT4_EMPS DNO=4(EMPLOYEE) R(FIRSTNAME,LASTNAME,SALARY)

FNAME,LNAME,SALARY (DEPT4_EMPS)

Page 48: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 48

Page 49: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 49

5.2 Set Operations( 集合運算 ) Binary operations from mathematical

set theory:( 將 tuple 看成集合元素 ) UNION: R1 R2, INTERSECTION: R1 R2, SET DIFFERENCE: R1 R2, CARTESIAN PRODUCT: R1 R2.

Page 50: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 50

Union compatibility For , , , the operand relations R1(A1, A2,

..., An) and R2(B1, B2, ..., Bn) must have the same number of attributes, and the domains of corresponding attributes must b

e compatible; that is, dom(Ai)=dom(Bi) for i=1, 2, ..., n.

This condition is called union compatibility. 類似的資料才能作集合運算

Page 51: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 51

Union compatibility The resulting relation for , , or

has the same attribute names as the first operand relation R1 (by convention).

Page 52: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 52

Page 53: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 53

CARTESIAN PRODUCT( 卡式積 ) CARTESIAN PRODUCT

R(A1, A2, ..., Am, B1, B2, ..., Bn) R1(A1, A2, ..., Am) X R2 (B1, B2, ..., Bn)

A tuple t exists in R for each combination of tuples t1 from R1 and t2 from R2 such that: t[A1, A2, ..., Am]=t1 and t[B1, B2, ..., Bn]=t2

If R1 has n1 tuples and R2 has n2 tuples, then R will have n1*n2 tuples.

瞭解關連 join 動作的媒介

Page 54: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 54

CARTESIAN PRODUCT CARTESIAN PRODUCT is a meaningles

s operation ( 沒有語意 ) on its own. It can combine related tuples from tw

o relations if followed by the appropriate SELECT operation .( 這就是 join 動作 )

Page 55: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 55

CARTESIAN PRODUCT Example:

Combine each DEPARTMENT tuple with the EMPLOYEE tuple of the manager.

DEP_EMP DEPARTMENT EMPLOYEE

DEPT_MANAGER MGRSSN=SSN(DEP_EMP)

Page 56: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 56

Page 57: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 57

5.3 JOIN Operations THETA Join( 條件是中不限等號 ) Equijoin( 條件式只能用等號 ) Outer Join( 不匹配的資料也選入 )

Page 58: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 58

THETA JOIN: Similar to a CARTESIAN PRODUCT foll

owed by a SELECT. The condition c is called a join conditi

on. R(A1, A2, ..., Am, B1, B2, ..., Bn)

R1(A1, A2, ..., Am) c R2 (B1, B2, ..., Bn)

Page 59: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 59

JOIN condition A join condition is of the form:

<condition> AND <condition> AND … AND <condition>

Where each condition is of the form Ai Bj , Ai is an attribute of R, Bj is an attribute of S, Ai and Bj have the same domain, and is one of the comparison operators {=, <, , >,

, }

Page 60: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 60

EQUIJOIN: The join condition c includes one or m

ore equality comparisons involving attributes from R1 and R2.

That is, c is of the form:(Ai=Bj) AND ... AND (Ah=Bk); 1<i,h<m, 1<j,k<n

Page 61: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 61

EQUIJOIN: In the above EQUIJOIN operation:

Ai, ..., Ah are called the join attributes of R1

Bj, ..., Bk are called the join attributes of R2

Page 62: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 62

Example of using EQUIJOIN: Retrieve each DEPARTMENT's

name and its manager's name: T DEPARTMENT MGRSSN=SSN

EMPLOYEE RESULT DNAME,FNAME,LNAME(T)

Page 63: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 63

NATURAL JOIN (*): In an EQUIJOIN R R1 c R2, the join

attribute of R2 appear redundantly in the result relation R.

In a NATURAL JOIN, the redundant join attributes of R2 are eliminated from R.

The equality condition is implied and need not be specified.

R R1 *(join attributes of R1),(join attributes of R2) R2

Page 64: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 64

Example: Retrieve each EMPLOYEE's name

and the name of the DEPARTMENT he/she works for: T EMPLOYEE *(DNO),(DNUMBER)

DEPARTMENT RESULT FNAME,LNAME,DNAME(T)

Page 65: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 65

NATURAL JOIN (*): If the join attributes have the same

names in both relations, they need not be specified and we can write R R1 * R2.

Page 66: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 66

Example: Retrieve each EMPLOYEE's name

and the name of his/her SUPERVISOR: SUPERVISOR(SUPERSSN,SFN,SLN)

SSN,FNAME,LNAME(EMPLOYEE) T EMPLOYEE * SUPERVISOR RESULT FNAME,LNAME,SFN,SLN(T)

Page 67: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 67

Note: In the original definition of

NATURAL JOIN, the join attributes were required to have the same names in both relations.

Page 68: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 68

Note: There can be a more than one set

of join attributes with a different meaning between the same two relations.

Page 69: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 69

For example:

JOIN ATTRIBUTES

EMPLOYEE.SSN= DEPARTMENT.MGRSSN

EMPLOYEE.DNO= DEPARTMENT.DNUMBER

RELATIONSHIP

EMPLOYEE manages the

DEPARTMENT

EMPLOYEE works forthe

DEPARTMENT

Page 70: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 70

Example: Retrieve each EMPLOYEE's name

and the name of the DEPARTMENT he/she works for: T EMPLOYEE DNO=DNUMBER

DEPARTMENT RESULT FNAME,LNAME,DNAME(T)

Page 71: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 71

Recursive closure operation

JOIN ATTRIBUTES

EMPLOYEE(1).SUPERSSN = EMPLOYEE(2).SSN

RELATIONSHIP

EMPLOYEE(2) supervises EMPLOYEE(1)

A relation can have a set of join attributes to join it with itself :

Page 72: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 72

Recursive closure operation

One can think of this as joining two distinct copies of the relation, although only one relation actually exists

In this case, renaming can be useful

Page 73: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 73

Example: Retrieve each EMPLOYEE's name

and the name of his/her SUPERVISOR: SUPERVISOR(SSSN,SFN,SLN)

SSN,FNAME,LNAME(EMPLOYEE) T EMPLOYEE SUPERSSN=SSSN

SUPERVISOR RESULT FNAME,LNAME,SFN,SLN(T)

Page 74: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 74

Complete Set of Relational Algebra Operations: 只要這些動作就夠了 All the operations discussed so far can be d

escribed as a sequence of only the operations SELECT, PROJECT, UNION, SET DIFFERENCE, and CARTESIAN PRODUCT.

Hence, the set { , , , , } is called a complete set of relational algebra operations.

Any query language equivalent to these operations is called relationally complete.

Page 75: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 75

Additional Operations For database applications, additional

operations are needed that were not part of the original relational algebra.

These include: Aggregate functions and grouping.( 聚集

函數,資料倉儲中的重要運算 ) OUTER JOIN and OUTER UNION.

Page 76: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 76

Division R(Z) ÷ S(X):

XZ Y= Z – X Result is in the form of T(Y), includes a tu

ple t if tuple tR appear in R with tR[Y]=t, and with tR[X] = tS for every tuple tS in S

Page 77: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 77

Page 78: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 78

Division Operation Example:

“Retrieve the names of employees who work on all the projects that ‘John Smith’ work on.”

SMITH FNAME=‘John’ AND LNAME=‘Smith’(EMPLOYEE) SMITH_PNOS PNO(WORK_ON * ESSN=SSN SMITH) SSN_PNOS PNO,ESSN(WORK_ON) SSNS(SSN) SSN_PNOS ÷ SMITH_PNOS RESULT FNAME,LNAME(SSNS * EMPLOYEE)

Page 79: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 79

5.4 Additional Relational Operations

Aggregate functions and grouping. OUTER JOIN and OUTER UNION

Page 80: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 80

AGGREGATE FUNCTIONS Functions such as SUM, COUNT,

AVERAGE, MIN, MAX are often applied to sets of values or sets of tuples in database applications<grouping attributes> F<function list> (R)

The grouping attributes are optional

Page 81: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 81

AGGREGATE FUNCTIONS Example 1:

Retrieve the average salary of all employees (no grouping needed):

R(AVGSAL) F AVERAGE SALARY (EMPLOYEE)

Page 82: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 82

AGGREGATE FUNCTIONS Example 2:

For each department, retrieve the department number, the number of employees, and the average salary (in the department):

R(DNO,NUMEMPS,AVGSAL)

DNO F COUNT SSN, AVERAGE SALARY (EMPLOYEE)

DNO is called the grouping attribute in the above example

Page 83: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 83

Page 84: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 84

OUTER JOIN In a regular EQUIJOIN or NATURAL JOIN

operation, tuples in R1 or R2 that do not have matching tuples in the other relation do not appear in the result

Some queries require all tuples in R1 (or R2 or both) to appear in the result

When no matching tuples are found, nulls are placed for the missing attributes

Page 85: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 85

OUTER JOIN LEFT OUTER JOIN:

R1 R2 lets every tuple in R1 appear in the result

RIGHT OUTER JOIN: R1 R2 lets every tuple in R2 appear in

the result FULL OUTER JOIN:

R1 R2 lets every tuple in R1 or R2 appear in the result

Page 86: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 86

Page 87: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 87

Example of Queries (1) Query 1

Retrieve the name and address of all employees who work for the ‘Research’ department.

Page 88: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 88

Answers of queries (1) RESEARCH_DEPT DNAME=‘Research’(DEPARTMENT) RESEARCH_DEPT_EMPS (RESEARCH_DEPT

DNUMBER=DNO EMPLOYEE) RESULT FNAME,LNAME,ADDRESS(RESEARCH_DEPT_EMP

S)

RESEARCH_DEPT DNAME=‘Research’(DEPARTMENT) RESEARCH_DEPT_EMPS (RESEARCH_DEPT

DNUMBER=DNO EMPLOYEE) RESULT FNAME,LNAME,ADDRESS(RESEARCH_DEPT_EMP

S)

Page 89: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 89

Example of Queries (2) Query 2

For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manage’s last name, address, and birthdate.

Page 90: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 90

Answers of queries (2) STAFFFORD_PROJS

PLOCATION=‘STAFFORD’ (PROJECT) CONTR_DEPT (STAFFORD_PROJS

DNUM=DNUMBER DEPARTMENT) PROJ_DEPT_MGR

(CONTR_DEPT MGRSSN=SSN EMPLOYEE) RESULT PNUMBER,DNUM,LNAME,ADDRESS,BDATE(PROJ_DEPT_

MGR)

STAFFFORD_PROJS PLOCATION=‘STAFFORD’ (PROJECT)

CONTR_DEPT (STAFFORD_PROJS DNUM=DNUMBER DEPARTMENT)

PROJ_DEPT_MGR (CONTR_DEPT MGRSSN=SSN EMPLOYEE)

RESULT PNUMBER,DNUM,LNAME,ADDRESS,BDATE(PROJ_DEPT_MGR)

Page 91: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 91

Example of Queries (3) Query 3

Find the names of employees who work on all the projects controlled by department number 5.

Page 92: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 92

Answers of queries (3) DEPT5_PROJS(PNO) PNUMBER(DNUM=5(PROJECT)) EMP_PROJ(SSN, PNO) ESSN,PNO(WORKS_ON) RESULT_EMP_SSNS EMP_PROJ DEPT5_PROJS RESULT LNAME,FNAME(RESULT_EMP_SSNS*EMPLOY

EE)

DEPT5_PROJS(PNO) PNUMBER(DNUM=5(PROJECT)) EMP_PROJ(SSN, PNO) ESSN,PNO(WORKS_ON) RESULT_EMP_SSNS EMP_PROJ DEPT5_PROJS RESULT LNAME,FNAME(RESULT_EMP_SSNS*EMPLOY

EE)

Page 93: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 93

Example of Queries (4) Query 4

Make a list of project numbers for projects that involve an employee whose last name is ‘Smith’, either as a worker or a manager of the department that controls the project.

Page 94: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 94

Answers of queries (4) SMITHS(ESSN) PUNMBER(LNAME=‘Smith’(EMPLOYEE)) SMITH_WORKER_PROJS PNO(WORKS_ON*SMITHS) MGRS LNAME,DNUMBER(EMPLOYEE SSN=MGRSSNDEPARTMENT) SMITH_MGRS LNAME=‘Smith’(MGRS) SMITH_MANAGED_DEPTS(DNUM) DNUMBER(SMITH_MG

RS) SMITH_MGR_PROJS(PNO) PNUMBER(SMITH_MANAGED_

DEPTS*PROJECT) RESULT (SMITH_WORKER_PROJSSMITH_MGR_PRO

JS)

SMITHS(ESSN) PUNMBER(LNAME=‘Smith’(EMPLOYEE)) SMITH_WORKER_PROJS PNO(WORKS_ON*SMITHS) MGRS LNAME,DNUMBER(EMPLOYEE SSN=MGRSSNDEPARTMENT) SMITH_MGRS LNAME=‘Smith’(MGRS) SMITH_MANAGED_DEPTS(DNUM) DNUMBER(SMITH_MG

RS) SMITH_MGR_PROJS(PNO) PNUMBER(SMITH_MANAGED_

DEPTS*PROJECT) RESULT (SMITH_WORKER_PROJSSMITH_MGR_PRO

JS)

Page 95: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 95

Example of Queries (5) Query 5

List the names of all employees with two or more dependents.

Page 96: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 96

Answers of queries (5)

T1(SSN, NO_OF_DEPS)

ESSNFCOUNT DEPENDENT_NAME(DEPENDENT) T2 NO_OF_DEPS2(T1) RESULT LNAME,FNAME(T2*EMPLOYEE)

T1(SSN, NO_OF_DEPS)

ESSNFCOUNT DEPENDENT_NAME(DEPENDENT) T2 NO_OF_DEPS2(T1) RESULT LNAME,FNAME(T2*EMPLOYEE)

Page 97: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 97

Example of Queries (6) Query 6

Retrieve the names of employees who have no dependents.

Page 98: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 98

Answers of queries (6) ALL_EMPS SSN(EMPLOYEE) EMPS_WITH_DEPS(SSN) ESSN(DEPENDENT) EMPS_WITHOUT_DEPS

(ALL_EMPS EMPS_WITH_DEPS) RESULT LNAME,FNAME(EMPS_WITHOUT_DEPS * EMPLOYEE)

ALL_EMPS SSN(EMPLOYEE) EMPS_WITH_DEPS(SSN) ESSN(DEPENDENT) EMPS_WITHOUT_DEPS

(ALL_EMPS EMPS_WITH_DEPS) RESULT LNAME,FNAME(EMPS_WITHOUT_DEPS * EMPLOYEE)

Page 99: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 99

Example of Queries (7) Query 7

List the names of managers who have at least one dependent.

Page 100: Relational Data Model 建置資料模式 (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

The Relational Data Model 100

Answers of queries (7) MGRS(SSN) MGRSSN(DEPARTMENT) EMPS_WITH_DEPS(SSN) ESSN(DEPENDENT) MGRS_WITH_DEPS (MGRS EMPS_WITH_DEPS) RESULT LNAME,FNAME(MGRS_WITH_DEPS * EMPLOYEE)

MGRS(SSN) MGRSSN(DEPARTMENT) EMPS_WITH_DEPS(SSN) ESSN(DEPENDENT) MGRS_WITH_DEPS (MGRS EMPS_WITH_DEPS) RESULT LNAME,FNAME(MGRS_WITH_DEPS * EMPLOYEE)