跳到主要内容

关系代数

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)U→dom:mappingDUj→F:referenceUkuj→F: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|
R∪S={t∣t∈R∨t∈S}R \cup S=\{t \mid t \isin R \lor t \isin S\}

差​

∣R∣=∣S∣|R|=|S|
R−S={t∣t∈R∧t∉S}R-S=\{t \mid t \isin R \land t \notin S\}

笛卡尔积​

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

投影​

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

选择​

σF(R)={t∣t∈R∧F(t)=True}\sigma_F (R)=\{t \mid t \isin R \land F(t)=True\}

术语对照​

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

扩展关系运算​

交
theta连接
自然连接
除
广义投影
左外连接
右外连接
全外连接
∩\cap⋈XθY\Join \atop {X \theta Y}⋈\Join÷\divπ\pi⟕⟕⟖⟖⟗⟗

交​

R∩S={t∣t∈R∧t∈S}R \cap S=\{t \mid t \isin R \land t \isin S\}

theta连接​

∣R∣=m ∣S∣=n|R|=m \space |S|=n
R⋈XθYS={t∣t=<tm,tn>∧tm∈R∧tn∈S∧tm[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]\}
R⋈Xθ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
R⋈S={R×Sif  RA∩SB={Φ}σR.C=S.C(R×S)−S.Cif  RA∩SB=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×⋯×Dn⏟Cartesian product={(d1,⋯ ,dn)⏟n-tuple∣di⏟component∈Di,0<i≤n}∀∣Di(0<i≤n)∣=mi ∃∣D1×⋯×Dn∣=m1×⋯×mn=∏i=1nmi⏟Cardinal numberR⊆D1×⋯×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)
  • 不包含在任何候选码中的属性
    • 非码属性
  • K→keyR1 K→keyRK \xrightarrow{\text{key}} R_1 \space K \xrightarrow{\xcancel{\text{key}}} R
    • KK是RR的外码(Forign Key)
  • ∃R(T,C,S)\exist R(T,C,S)
    • (T,C,S)(T,C,S)是RR的全码(All-Key)