<aside>
ℹ️
Relational Algebra:
- The formal language used to describe operations on relations
- relations are essentially “tables in DBs”
</aside>
Basic Operations
- Projection ($\pi$): 返回某些列(属性)
- Selection ($\sigma$): 返回某些行
- Set-difference ($-$): finds tuples in X, but not in Y
- Union ($\cup$): finds tuples that belong to table X or Y
- Intersection ($\cap$): finds tuples that belong to table X and Y
- Cartesian Product ($\times$): 相当于 for 两次,两两配对
- Rename ($\rho$): allows us to rename a relation and/or its attributes
Projection $\pi_l(r)$

<aside>
🧑💻
SQL: SELECT l FROM r
- Select and return all columns as specified in
l
- Remove any duplicated rows
</aside>
Python:
def projection (l: List, r: Table) -> Table:
cols = r[l, :]
return cols.drop_duplicate()
Selection $\sigma_c(r)$

<aside>
🧑💻
SQL: r WHERE c
- Filters rows from
r based on conditions c
- No need to remove duplicates
- Because $r$ cannot contain duplicates, so neither will its subset
</aside>
Python:
def selection (c: Condition, r: Table) -> Table:
rows = r[:, c]
return rows