📅  最后修改于: 2020-12-12 02:56:04             🧑  作者: Mango
关系代数是一种过程查询语言。它给出了逐步的过程以获得查询结果。它使用运算符执行查询。
Notation: σ p(r)
哪里:
σ用于选择预测
r用于关联
p用作命题逻辑公式,可以使用诸如AND AND和NOT之类的连接器。这些关系可以用作关系运算符,例如=,≠,≥,<,>,≤。
例如:贷款关系
BRANCH_NAME | LOAN_NO | AMOUNT |
---|---|---|
Downtown | L-17 | 1000 |
Redwood | L-23 | 2000 |
Perryride | L-15 | 1500 |
Downtown | L-14 | 1500 |
Mianus | L-13 | 500 |
Roundhill | L-11 | 900 |
Perryride | L-16 | 1300 |
输入:
σ BRANCH_NAME="perryride" (LOAN)
输出:
BRANCH_NAME | LOAN_NO | AMOUNT |
---|---|---|
Perryride | L-15 | 1500 |
Perryride | L-16 | 1300 |
Notation: ∏ A1, A2, An (r)
哪里
A1 , A2 , A3用作关系r的属性名称。
示例:客户关系
NAME | STREET | CITY |
---|---|---|
Jones | Main | Harrison |
Smith | North | Rye |
Hays | Main | Harrison |
Curry | North | Rye |
Johnson | Alma | Brooklyn |
Brooks | Senator | Brooklyn |
输入:
∏ NAME, CITY (CUSTOMER)
输出:
NAME | CITY |
---|---|
Jones | Harrison |
Smith | Rye |
Hays | Harrison |
Curry | Rye |
Johnson | Brooklyn |
Brooks | Brooklyn |
Notation: R ∪ S
联合操作必须满足以下条件:
存款人关系
CUSTOMER_NAME | ACCOUNT_NO |
---|---|
Johnson | A-101 |
Smith | A-121 |
Mayes | A-321 |
Turner | A-176 |
Johnson | A-273 |
Jones | A-472 |
Lindsay | A-284 |
借阅关系
CUSTOMER_NAME | LOAN_NO |
---|---|
Jones | L-17 |
Smith | L-23 |
Hayes | L-15 |
Jackson | L-14 |
Curry | L-93 |
Smith | L-11 |
Williams | L-17 |
输入:
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)
输出:
CUSTOMER_NAME |
---|
Johnson |
Smith |
Hayes |
Turner |
Jones |
Lindsay |
Jackson |
Curry |
Williams |
Mayes |
Notation: R ∩ S
示例:使用上面的DEPOSITOR表和BORROW表
输入:
∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)
输出:
CUSTOMER_NAME |
---|
Smith |
Jones |
Notation: R - S
示例:使用上面的DEPOSITOR表和BORROW表
输入:
∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)
输出:
CUSTOMER_NAME |
---|
Jackson |
Hayes |
Willians |
Curry |
Notation: E X D
雇员
EMP_ID | EMP_NAME | EMP_DEPT |
---|---|---|
1 | Smith | A |
2 | Harry | C |
3 | John | B |
部
DEPT_NO | DEPT_NAME |
---|---|
A | Marketing |
B | Sales |
C | Legal |
输入:
EMPLOYEE X DEPARTMENT
输出:
EMP_ID | EMP_NAME | EMP_DEPT | DEPT_NO | DEPT_NAME |
---|---|---|---|---|
1 | Smith | A | A | Marketing |
1 | Smith | A | B | Sales |
1 | Smith | A | C | Legal |
2 | Harry | C | A | Marketing |
2 | Harry | C | B | Sales |
2 | Harry | C | C | Legal |
3 | John | B | A | Marketing |
3 | John | B | B | Sales |
3 | John | B | C | Legal |
重命名操作用于重命名输出关系。用rho (ρ)表示。
示例:我们可以使用重命名运算符将STUDENT关系重命名为STUDENT1。
ρ(STUDENT1, STUDENT)
注意:除了这些常见的运算,关系代数还可以用于Join运算中。