Skip to main content

关系代数

relational algebra

关系

R(U,D,dom,F)R(U)R(U,D,dom,F) \mid R(U)

notesR : (Relation)(Relation Schema)U : (Attributes)D : (Domains)Udom:mappingDUjF:referenceUkujF:referenceuk\text{notes} \\ R \space : \space \text{(Relation)(Relation Schema)} \\ U \space : \space \text{(Attributes)} \\ D \space : \space \text{(Domains)} \\ U \xrightarrow{dom:\text{mapping}} D \\ U_j \xrightarrow{F:\text{reference}} U_k \\ u_j \xrightarrow{F:\text{reference}} u_k

属性

属性是事物特征的抽象

域是属性取值的范围

第一范式

域应是原子数据

基本关系运算

笛卡尔积
投影
选择
\cup-×\timesπ\piσ\sigma

R=S|R|=|S|
RS={ttRtS}R \cup S=\{t \mid t \isin R \lor t \isin S\}

R=S|R|=|S|
RS={ttRtS}R-S=\{t \mid t \isin R \land t \notin S\}

笛卡尔积

R=m S=n|R|=m \space |S|=n
R×S={tt=<tm,tn>tmRtnS}R \times S=\{t \mid t=<t^m,t^n> \land t^m \isin R \land t^n \isin S\}

投影

πA(R)={t[A]tR}\pi_A(R)=\{t[A] \mid t \isin R\}
A=attributesA=\text{attributes}

选择

σF(R)={ttRF(t)=True}\sigma_F (R)=\{t \mid t \isin R \land F(t)=True\}

术语对照

属性
关系模式
元组
字段、数据项
记录类型
记录

扩展关系运算

theta连接
自然连接
广义投影
左外连接
右外连接
全外连接
\capXθY\Join \atop {X \theta Y}\Join÷\divπ\pi

RS={ttRtS}R \cap S=\{t \mid t \isin R \land t \isin S\}

theta连接

R=m S=n|R|=m \space |S|=n
RXθYS={tt=<tm,tn>tmRtnStm[X]θtn[Y]}R {\scriptstyle{\Join \atop X \theta Y}}S=\{t \mid t=<t^m,t^n> \land t^m \isin R \land t^n \isin S \land t^m[X] \theta t^n[Y]\}
RXθYS=σXθY(R×S)R {\scriptstyle{\Join \atop X \theta Y}} S=\sigma_{X \theta Y}(R \times S)
X=Y|X|=|Y|

自然连接

R=m S=n|R|=m \space |S|=n
RS={R×Sif  RASB={Φ}σR.C=S.C(R×S)S.Cif  RASB=CR \Join S = \begin{cases} R \times S & \text{if \space} R_A \cap S_B = \{\Phi\} \\ \sigma_{R.C=S.C} (R \times S)-S.C & \text{if \space} R_A \cap S_B = C \end{cases}

除法

R×S=TR \times S = T
T÷S=RT \div S = R

广义投影

πF1,,Fj(R)\pi_{F_1,\cdots,F_j}(R)

笛卡尔积

D1,,DnD1××DnCartesian product={(d1,,dn)n-tupledicomponentDi,0<in}Di(0<in)=mi D1××Dn=m1××mn=i=1nmiCardinal numberRD1××DnD_1,\cdots,D_n \\ \underbrace{D_1 \times \cdots \times D_n}_\text{Cartesian product}=\{\underbrace{(d_1, \cdots , d_n)}_\text{n-tuple} | \underbrace{d_i}_\text{component} \in D_i , 0 < i \le n\} \\ \forall |D_i(0 < i \le n)|=m_i\ \\ \exists |D_1 \times \cdots \times D_n|=m_1 \times \cdots \times m_n=\underbrace{\prod^{n}_{i=1}m_i}_\text{Cardinal number} \\ R \subseteq D_1 \times \cdots \times D_n

  • DD(集合)
  • Cartesian product(笛卡尔积)
  • n-tuple(nn元组)
  • component(分量)
  • Cardinal number(基数)(集合元素的个数、笛卡尔积元组的个数)
  • RR(关系)(笛卡尔积的子集在集合域上的(nn元)关系)
    • RR(关系的名字)
    • nn(Degree)(关系的目、度)
  • 存在一个did_i可唯一标识元组(d1,,dn)(d_1, \cdots , d_n)
    • did_i(Candidate Key)(候选码)
  • 存在多个候选码,选定一个作为主码(primary key)
  • 包含在任何候选码中的任何属性
    • 主属性(None-Key attribute)
  • 不包含在任何候选码中的属性
    • 非码属性
  • KkeyR1 KkeyRK \xrightarrow{\text{key}} R_1 \space K \xrightarrow{\xcancel{\text{key}}} R
    • KKRR的外码(Forign Key)
  • R(T,C,S)\exist R(T,C,S)
    • (T,C,S)(T,C,S)RR的全码(All-Key)