CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

关系性数据库提出的背景

刚开始是没有数据库这概念的.当数据量比较少时用一个啥txt文本文件就差不多就能搞定了.但数据量稍微多一点时,而且数据保存在不同的文件中.比如啥txt,doc,excel等文件.此时要处理数据还是手动去查询更改就非常之麻烦.可能得再整个简单应用程序帮你做些事了.只给你提供一些简单的用户界面.封装背后的一些复杂操作.此时数据的组织和保存还是主要依靠操作系统的文件系统.应用程序只是简化了下用户的操作.当数据量非常多的时候会发现应用程序加文件系统的方式会存在很多明显的缺陷。

1970年,科德发表题为“大型共享数据库的关系模型”的论文,文中首次提出了数据库的关系模型。由于关系模型简单明了、具有坚实的数学理论基础,所以一经推出就受到了学术界和产业界的高度重视和广泛响应,并很快成为数据库市场的主流。20世纪80年代以来,计算机厂商推出的数据库管理系统几乎都支持关系模型,数据库领域当前的研究工作大都以关系模型为基础。

数据库中的关系代数

SELECT

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

从满足选择条件的关系中选择一个元组(tuples)的子集。

→ 充当过滤器,仅保留满足选择条件要求的元组。

→ 可以使用连接词/析取词组合多个条件。

R(a_id,b_id) R(a\_id , b\_id)

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

σa_id=a2(R) σ_{a\_id = 'a2'}(R)

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

PROJECTION

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

用仅包含指定属性的元组生成一个关系。

→ 可以重新排列属性的顺序。

→ 可以操作这些值。

R(a_id,b_id) R(a\_id , b\_id)

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

Πb_id100,a_id(σa_id=a2(R)) \Pi_{b\_id - 100 , a\_id}(\sigma _ {a\_id = 'a2'}(R))

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

UNION

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

生成一个包含只在一个或两个输入关系中出现过的关系。

R(a_id,b_id)                S(a_id,b_id) R(a\_id , b\_id) \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ S(a\_id , b\_id)

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

(R  S) (R\ \cup \ S)

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

INTERSECTION

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

生成仅包含在两个关系中都出现的元组的关系。

R(a_id,b_id)                S(a_id,b_id) R(a\_id , b\_id) \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ S(a\_id , b\_id)

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

(R  S) (R\ \cap \ S)

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

DEFFRENCE

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

生成一个由仅在第一个关系而未在第二个关系中出现的关系。

R(a_id,b_id)                S(a_id,b_id) R(a\_id , b\_id) \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ S(a\_id , b\_id)

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

(R  S) (R\ - \ S)

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

PRODUCT

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

笛卡尔积,生成所有可能的组合。

R(a_id,b_id)                S(a_id,b_id) R(a\_id , b\_id) \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ S(a\_id , b\_id)

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

(R × S) (R\ × \ S)

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

JOIN

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

从两个关系的笛卡尔积中选取属性间都存在的元组。

R(a_id,b_id)                S(a_id,b_id) R(a\_id , b\_id) \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ S(a\_id , b\_id)

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

(R  S) (R\ \bowtie \ S)

CMU15-445/645 Lecture#1 Course Introduction and the Relational Model

EXTRA OPERATORS

  • Rename(ρ)
  • Assignment(R←S)
  • Duplicate Elimination(δ)
  • Aggregation(γ)
  • Sorting(τ)
  • Division(R÷S)