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¶
设集合
Attribute Types¶
设关系
关系理论第一范式
Attribute values are (normally) required to be atomic. 属性值是原子的,即无法分割。例如复合属性或者多值属性都不能成为关系的属性。
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¶
- Superkey:
is a superkey of if the values of are sufficient to identify a unique tuple in . 是 的超键,如果 的值足以唯一标识 中的一个元组。 - Candidate Key:
is a candidate key of if is a minimal superkey of . 是 的候选键,如果 是 的最小超键。 - Primary Key: A candidate key defined by the database designer. 主键是数据库设计者定义的候选键,通常用下划线标识。
Foreign Key¶
Assume relations
参照关系中外码的值必须在被参照关系中实际存在, 或为
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¶
is a relation is a predicate in the form of<attribute> op <attribute> or <constant>
, whereop
is a comparison operator.
Example

Projection¶
is a relation is an attribute of
The result is defined as the relation of
Example

Union¶
and are relations and must have the same number of attributes. 和 必须有相同数量的属性。- The attribute domains must be compatible. 属性域必须兼容。
Example

Set Difference¶
and are relations- Similar to Union,
and must have the same number of attributes and the attribute domains must be compatible.
Example

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


Rename¶
: rename the expression as . : rename the expression as and the attributes as .
Additional Relational-Algebra Operations¶
Four additional operators:
- Set Intersection:
交 - Natural Join:
自然连接 - Division:
除 - Assignment:
赋值
Set Intersection¶
and are relations and must have the same number of attributes and the attribute domains must be compatible.
Example

Natural Join¶
and are relations- Consider each pair of tuples
and , if and have the same value on each attribute in , then add a tuple to the result relation with the remaining attributes from and . 如果 和 在 上的每个属性上具有相同的值,则将一个元组添加到结果关系中,该元组具有来自 和 的剩余属性。
Example

Theta Join Operation
Division¶
and are relations on schema
Example

Assignment¶
Assignment is always made to a temporary relation.
Example
Write
Extended Relational-Algebra Operations¶
- Generalized Projection:
where is an expression. 广义投影 - Aggregation Functions: 聚合函数
Generalized Projection¶
Example
Given a relation
Aggregation Functions¶
is a relation is an attribute of is an aggregation function, such as- avg: average value
- sum: sum of values
- count: number of tuples
- max: maximum value
- min: minimum value
is a grouping attribute(can be empty)
Example

Info
Result of aggregation does not have a name. 聚合的结果没有名称,需要重命名或者在聚合操作中指定名称。
Example:
Modification of the Database¶
- Deletion
- Insertion
- Updating
Deletion¶
is a relation is a relational-algebra expression
Insertion¶
Updating¶
is a relation is an attribute of or an expression which gives the new value of the attribute