关系语言分为三种:
定义:关系代数是一种抽象的查询语言,它用对关系的运算来表达查询
三大要素
关系代数运算符
运算符类型 | 符号表示 | 含义 |
---|---|---|
集合运算符 | \(\cup\) | 并 |
集合运算符 | \(-\) | 差 |
集合运算符 | \(\cap\) | 交 |
集合运算符 | \(\times\) | 笛卡尔积 |
关系运算符 | \(\sigma\) | 选择 |
关系运算符 | \(\prod\) | 投影 |
关系运算符 | ⋈ | 连接 |
关系运算符 | \(\div\) | 除 |
以两个关系 R 和 S 为例,集合之间的运算如下:
R 关系如下:
A B C \(a_1\) \(b_1\) \(c_1\) \(a_1\) \(b_2\) \(c_2\) \(a_2\) \(b_2\) \(c_1\) S 关系如下:
A B C \(a_1\) \(b_2\) \(c_2\) \(a_1\) \(b_3\) \(c_2\) \(a_2\) \(b_2\) \(c_1\)
A | B | C |
---|---|---|
\(a_1\) | \(b_1\) | \(c_1\) |
\(a_1\) | \(b_2\) | \(c_2\) |
\(a_2\) | \(b_2\) | \(c_1\) |
\(a_1\) | \(b_3\) | \(c_2\) |
A | B | C |
---|---|---|
\(a_1\) | \(b_2\) | \(c_2\) |
\(a_2\) | \(b_2\) | \(c_1\) |
A | B | C |
---|---|---|
\(a_1\) | \(b_1\) | \(c_1\) |
R.A | R.B | R.C | S.A | S.B | S.C |
---|---|---|---|---|---|
\(a_1\) | \(b_1\) | \(c_1\) | \(a_1\) | \(b_2\) | \(c_2\) |
\(a_1\) | \(b_1\) | \(c_1\) | \(a_1\) | \(b_3\) | \(c_2\) |
\(a_1\) | \(b_1\) | \(c_1\) | \(a_2\) | \(b_2\) | \(c_1\) |
\(a_1\) | \(b_2\) | \(c_2\) | \(a_1\) | \(b_2\) | \(c_2\) |
\(a_1\) | \(b_2\) | \(c_2\) | \(a_1\) | \(b_3\) | \(c_2\) |
\(a_1\) | \(b_2\) | \(c_2\) | \(a_2\) | \(b_2\) | \(c_1\) |
\(a_2\) | \(b_2\) | \(c_1\) | \(a_1\) | \(b_2\) | \(c_2\) |
\(a_2\) | \(b_2\) | \(c_1\) | \(a_1\) | \(b_3\) | \(c_2\) |
\(a_2\) | \(b_2\) | \(c_1\) | \(a_2\) | \(b_2\) | \(c_1\) |
例如:查询关系 R 中 A 属性值等于 \(a_1\) 的集合: \(\sigma_{A='a_1'}(R)\)
运算符类型 | 运算符符号 | 含义 |
---|---|---|
比较运算符 | \(>\) | 大于 |
比较运算符 | \(\geq\) | 大于等于 |
比较运算符 | \(<\) | 小于 |
比较运算符 | \(\leq\) | 小于等于 |
比较运算符 | \(=\) | 等于 |
比较运算符 | \(◇\) | 不等于 |
逻辑运算符 | ¬ | 非 |
逻辑运算符 | Ʌ | 与 |
逻辑运算符 | ∨ | 或 |
例如:对关系 R 中属性 A 和 B 进行投影:\(\prod _{A,B}(R)\) ,结果如下
A | B |
---|---|
\(a_1\) | \(b_1\) |
\(a_1\) | \(b_2\) |
\(a_2\) | \(b_2\) |
四种连接符号
悬浮元组:两个关系 R 和 S 在做自然连接时,关系 R 中某些元组有可能在 S 中不存在公共属性上值相等的元组,从而造成 R 中这些元组在操作时被舍弃了,这些被舍弃的元组称为悬浮元组
例:有两个关系 R 和 S ,其属性值分别如下:
R 关系如下:
A B C D \(\alpha\) \(1\) \(\alpha\) \(a\) \(\beta\) \(2\) \(\gamma\) \(a\) \(\gamma\) \(4\) \(\beta\) \(b\) \(\alpha\) \(1\) \(\gamma\) \(a\) \(\theta\) \(2\) \(\beta\) \(b\) S 关系如下:
B D E \(1\) \(a\) \(\alpha\) \(3\) \(a\) \(\beta\) \(1\) \(a\) \(\gamma\) \(2\) \(b\) \(\theta\) \(3\) \(b\) \(\mu\)
1 自然连接
A | B | C | D | E |
---|---|---|---|---|
\(\alpha\) | \(1\) | \(\alpha\) | \(a\) | \(\alpha\) |
\(\alpha\) | \(1\) | \(\alpha\) | \(a\) | \(\gamma\) |
\(\alpha\) | \(1\) | \(\gamma\) | \(a\) | \(\alpha\) |
\(\alpha\) | \(1\) | \(\gamma\) | \(a\) | \(\gamma\) |
\(\theta\) | \(2\) | \(\beta\) | \(b\) | \(\theta\) |
2 外连接
A | B | C | D | E |
---|---|---|---|---|
\(\alpha\) | \(1\) | \(\alpha\) | \(a\) | \(\alpha\) |
\(\alpha\) | \(1\) | \(\alpha\) | \(a\) | \(\gamma\) |
\(\alpha\) | \(1\) | \(\gamma\) | \(a\) | \(\alpha\) |
\(\alpha\) | \(1\) | \(\gamma\) | \(a\) | \(\gamma\) |
\(\theta\) | \(2\) | \(\beta\) | \(b\) | \(\theta\) |
\(\beta\) | \(2\) | \(\gamma\) | \(a\) | \(null\) |
\(\gamma\) | \(4\) | \(\beta\) | \(b\) | \(null\) |
\(null\) | \(3\) | \(null\) | \(a\) | \(\beta\) |
\(null\) | \(3\) | \(null\) | \(b\) | \(\mu\) |
3 左外连接
A | B | C | D | E |
---|---|---|---|---|
\(\alpha\) | \(1\) | \(\alpha\) | \(a\) | \(\alpha\) |
\(\alpha\) | \(1\) | \(\alpha\) | \(a\) | \(\gamma\) |
\(\alpha\) | \(1\) | \(\gamma\) | \(a\) | \(\alpha\) |
\(\alpha\) | \(1\) | \(\gamma\) | \(a\) | \(\gamma\) |
\(\theta\) | \(2\) | \(\beta\) | \(b\) | \(\theta\) |
\(\beta\) | \(2\) | \(\gamma\) | \(a\) | \(null\) |
\(\gamma\) | \(4\) | \(\beta\) | \(b\) | \(null\) |
4 右外连接
A | B | C | D | E |
---|---|---|---|---|
\(\alpha\) | \(1\) | \(\alpha\) | \(a\) | \(\alpha\) |
\(\alpha\) | \(1\) | \(\alpha\) | \(a\) | \(\gamma\) |
\(\alpha\) | \(1\) | \(\gamma\) | \(a\) | \(\alpha\) |
\(\alpha\) | \(1\) | \(\gamma\) | \(a\) | \(\gamma\) |
\(\theta\) | \(2\) | \(\beta\) | \(b\) | \(\theta\) |
\(null\) | \(3\) | \(null\) | \(a\) | \(\beta\) |
\(null\) | \(3\) | \(null\) | \(b\) | \(\mu\) |
设关系 R 如下:
姓名 选修课程 张三 计算机 张三 数据库 张三 网络 李四 网络 李四 计算机 王五 数据库 王五 网络 设关系 S 如下:
选修课程 数据库 网络
则 \(R \div S\) 为:
姓名 |
---|
张三 |
王五 |