跳转至

Relational Model

A relational database is a collection of one or more relations, which are based on the relational model.

Example

Structure of Relational Databases

Basic Structure

设集合 D1,D2,,Dn,关系 r 是笛卡尔积 D1×D2××Dn 的一个子集。

Attribute Types

设关系 r 为元组 (a1,a2,,an),其中 ai 是属性,Di 是属性的域。每个属性都有名称。

关系理论第一范式

Attribute values are (normally) required to be atomic. 属性值是原子的,即无法分割。例如复合属性或者多值属性都不能成为关系的属性。

null 属于每个域。

Concepts about Relation

A relation is concerned with two concepts: relation schema and relation instance. 关系包含两个概念:关系模式和关系实例。

  • Relation Schema: describes the structure of the relation.

    Example

    Student_schema = (sid: string, name: string, sex: string, age: int, dept: string)
    

    or

    Student_schema = (sid, name, sex, age, dept)
    
  • Relation Instance: corresponds to the snapshot of the data in the relation at a given instant in time.

The Properties of Relation

  • The order of tuples is irrelevant. 无序
  • No duplicated tuples in a relation. 无重复
  • Attribute values are atomic. 属性是原子的

Database

A database consists of a collection of relations.

Key

KR

  • Superkey: K is a superkey of R if the values of K are sufficient to identify a unique tuple in R. KR 的超键,如果 K 的值足以唯一标识 R 中的一个元组。
  • Candidate Key: K is a candidate key of R if K is a minimal superkey of R. KR 的候选键,如果 KR 的最小超键。
  • Primary Key: A candidate key defined by the database designer. 主键是数据库设计者定义的候选键,通常用下划线标识。

Foreign Key

Assume relations r and s: r(A,B,C), s(B,D). B in relation r is a foreign key referencing s, and r is a referencing relation, and s is a referenced relation.

参照关系中外码的值必须在被参照关系中实际存在, 或为 null

Query Language

Pure languages:

  • Relational Algebra: a procedural query language. 过程式查询语言,SQL 的基础。
  • Tuple Relational Calculus: 元组关系演算
  • Domain Relational Calculus: 域关系演算

Fundamental Relational-Algebra Operations

Six basic operators:

  • Selection: σ 选择
  • Projection: Π 投影
  • Union:
  • Set Difference:
  • Cartesian product: × 笛卡尔积
  • Rename: ρ 重命名

Selection

σp(r)={ttrp(t)}

  • r is a relation
  • p is a predicate in the form of <attribute> op <attribute> or <constant>, where op is a comparison operator.

Example

Projection

ΠA1,A2,,Ak(r)

  • r is a relation
  • Ai is an attribute of r

The result is defined as the relation of k columns obtained by erasing the columns that are not listed. 结果是通过删除未列出的列而获得的 k 列关系。并且进行 去重

Example

Union

rs={ttrts}

  • r and s are relations
  • r and s must have the same number of attributes. rs 必须有相同数量的属性。
  • The attribute domains must be compatible. 属性域必须兼容。

Example

Set Difference

rs={ttrts}

  • r and s are relations
  • Similar to Union, r and s must have the same number of attributes and the attribute domains must be compatible.

Example

Cartesian Product

r×s={(t1,t2)t1rt2s}

  • r and s are relations
  • If r and s are not disjoint, then rename will be used to avoid ambiguity between the attributes with the same name. 如果 rs 有相同名称的属性,则将使用 重命名 来避免具有相同名称的属性之间的歧义。

Example

rs 不相交:

rs 相交:

Rename

  • ρX(E): rename the expression E as X.
  • ρX(A1,A2,,An)(E): rename the expression E as X and the attributes as A1,A2,,An.

ρ returns a relation that is identical to E except that the relation is renamed as X. ρ 返回一个与 E 相同的关系,只是关系被重命名为 X

Additional Relational-Algebra Operations

Four additional operators:

  • Set Intersection:
  • Natural Join: 自然连接
  • Division: ÷
  • Assignment: 赋值

Set Intersection

rs={ttrts}=r(rs)

  • r and s are relations
  • r and s must have the same number of attributes and the attribute domains must be compatible.

Example

Natural Join

rs

  • r and s are relations
  • Consider each pair of tuples t1r and t2s, if t1 and t2 have the same value on each attribute in RS, then add a tuple to the result relation with the remaining attributes from t1 and t2. 如果 t1t2RS 上的每个属性上具有相同的值,则将一个元组添加到结果关系中,该元组具有来自 t1t2 的剩余属性。

Example

Theta Join Operation

rθs=σθ(r×s)

Division

r÷s={ttΠRS(r)us,t×ur}

  • r and s are relations on schema RandS

Example

Assignment

XE

Assignment is always made to a temporary relation.

Example

Write r÷s as:

temp1ΠRS(r)temp2ΠRS(temp1×sΠRS,S(r))resulttemp1temp2

Extended Relational-Algebra Operations

  • Generalized Projection: ΠA1,A2,,An(r) where Ai is an expression. 广义投影
  • Aggregation Functions: 聚合函数

Generalized Projection

ΠA1,A2,,An(r) where Ai is an expression.

Example

Given a relation credit_info(customer_name,limit,current_balance), we can use the following expression to calculate the available credit for each customer:

Πcustomer_name,limitcurrent_balance(credit_info)

Aggregation Functions

G1,G2,,GngF1(A1),F2(A2),,Fn(An)(r)

  • r is a relation
  • Ai is an attribute of r
  • Fi is an aggregation function, such as
    • avg: average value
    • sum: sum of values
    • count: number of tuples
    • max: maximum value
    • min: minimum value
  • Gi is a grouping attribute(can be empty)

Example

Info

Result of aggregation does not have a name. 聚合的结果没有名称,需要重命名或者在聚合操作中指定名称。

Example:

branch-namegsum(balance) as sum-balance(account)

Modification of the Database

  • Deletion
  • Insertion
  • Updating

Deletion

rrE

  • r is a relation
  • E is a relational-algebra expression

Insertion

rrE

Updating

rΠF1,F2,,Fn(r)

  • r is a relation
  • Fi is an attribute of r or an expression which gives the new value of the attribute