|
| 1 | +# Relation Model |
| 2 | + |
| 3 | +## Database Structure |
| 4 | + |
| 5 | +Table (表) / Relation (关系): Cartesian product of domains. |
| 6 | + |
| 7 | +Relationship: Relationship in ER model. |
| 8 | + |
| 9 | +Row (行) / Tuple (元组) |
| 10 | + |
| 11 | +Tuple variable: Variable representing a tuple. |
| 12 | + |
| 13 | +Attribute (属性) |
| 14 | + |
| 15 | +Domain (域): Allowed attribute values. |
| 16 | + |
| 17 | +Atomic (原子): Domain elements cannot be divided. |
| 18 | + |
| 19 | +Null: Value unknown or does not exist, should be eliminated if possible. |
| 20 | + |
| 21 | +## Schema |
| 22 | + |
| 23 | +Database schema (数据库模式): Logical design of the database. |
| 24 | + |
| 25 | +Database instance (数据库实例): Snapshot of the data in the database at a given instant in time. |
| 26 | + |
| 27 | +Relation schema (关系模式): Relation is instance, then relation schema is type definition. |
| 28 | + |
| 29 | +Schema: R = (attribute, ...), definition of a schema. |
| 30 | + |
| 31 | +Instance: r(R) = instance(attribute, ...), instance is a relation on schema. |
| 32 | + |
| 33 | +Use more relations to avoid null. |
| 34 | + |
| 35 | +## Key |
| 36 | + |
| 37 | +A tuple in a relation must be unique. |
| 38 | + |
| 39 | +Superkey (超码): A set of one or more attributes that can uniquely identify a tuple in a relation. |
| 40 | + |
| 41 | +Candidate key (候选码): Minimum superkey. |
| 42 | + |
| 43 | +Primary key (主码): Chosen candidate key, as principal means of identifying tuples within a relation. |
| 44 | + |
| 45 | +Foreign key (外码):Attribute that is the primary key of another relation. The relation is referencing relation, the other relation is referenced relation. |
| 46 | + |
| 47 | +Schema diagram: Rectangles for relations, relation name, primary key, attributes from top to bottom, arrows for foreign key dependencies. |
| 48 | + |
| 49 | +## Query Language |
| 50 | + |
| 51 | +Query language: Language in which user request information from database. |
| 52 | + |
| 53 | +Procedural query language: User instruct system to get result. |
| 54 | + |
| 55 | +Non-procedural query language: User describe the desired information. |
| 56 | + |
| 57 | +## Fundamental Relational-Algebra |
| 58 | + |
| 59 | +Operations can be composed together. |
| 60 | + |
| 61 | +* Select (选择) |
| 62 | + |
| 63 | + $$\sigma_{predicate}(relation)$$ |
| 64 | + |
| 65 | + Select tuple that satisfy a given predicate. |
| 66 | + |
| 67 | +* Project (投影) |
| 68 | + |
| 69 | + $$\Pi_{attribute, ...}(relation)$$ |
| 70 | + |
| 71 | + Return relation with certain attributes left out. |
| 72 | + |
| 73 | +* Union (并) |
| 74 | + |
| 75 | + $$r{\cup}s$$ |
| 76 | + |
| 77 | + Removes duplicates because result is a set. |
| 78 | + |
| 79 | + Need to be compatible. |
| 80 | + |
| 81 | + Compatible: |
| 82 | + |
| 83 | + * r and s should be of the same arity, i.e. they have same number of attributes. |
| 84 | + |
| 85 | + * Corresponding domains of r and s should be the same. |
| 86 | + |
| 87 | +* Set-Difference (差) |
| 88 | + |
| 89 | + $$r-s$$ |
| 90 | + |
| 91 | + Need to be compatible. |
| 92 | + |
| 93 | +* Cartesian product (笛卡尔积) |
| 94 | + |
| 95 | + $$r{\times}s$$ |
| 96 | + |
| 97 | + Attributes should have distinct names, and relation name prefix can be dropped for simplicity if there is no ambiguity. |
| 98 | + |
| 99 | + The result relation schema is the concatenation of r's and s'. |
| 100 | + |
| 101 | + The result relation contains all tuples for which the sub-tuples satisfy. |
| 102 | + |
| 103 | +* Rename (重命名) |
| 104 | + |
| 105 | + $$\rho_{x(A_1, ...)}(E)$$ |
| 106 | + |
| 107 | + Return E evaluated and give E the name x, optionally rename attributes to A_1, ... if given. |
| 108 | + |
| 109 | +Positional notation: \$n for n-th attribute |
| 110 | + |
| 111 | +## Additional Relational-Algebra |
| 112 | + |
| 113 | +* Set-Intersection |
| 114 | + |
| 115 | + $$r{\cap}s$$ |
| 116 | + |
| 117 | + $$r{\cap}s=r-(r-s)$$ |
| 118 | + |
| 119 | +* Natural Join |
| 120 | + |
| 121 | + $$r{\bowtie}s$$ |
| 122 | + |
| 123 | + Cartesian product, select with common attribute, remove duplicate attribute. |
| 124 | + |
| 125 | + Associative |
| 126 | + |
| 127 | + Theta join |
| 128 | + |
| 129 | + Natural join with extra predicate for select. |
| 130 | + |
| 131 | + $$r{\bowtie}_{\theta}s$$ |
| 132 | + |
| 133 | +* Division |
| 134 | + |
| 135 | + $$r{\div}s$$ |
| 136 | + |
| 137 | + When r on R-S have all values in S. |
| 138 | + |
| 139 | + $r{\div}s=\Pi_{R-S}(r)-\Pi_{R-S}((\Pi_{R-S}(r){\times}s)-\Pi_{R-S,S}(r))$ ($\Pi_{R-S,S}(r)$ is only for attribute reordering) |
| 140 | + |
| 141 | +* Assignment |
| 142 | + |
| 143 | + $$name \leftarrow $$ |
| 144 | + |
| 145 | +## Extended Relational-Algebra |
| 146 | + |
| 147 | +* Generalized Projection |
| 148 | + |
| 149 | + $$\Pi_{F_1,F_2,...F_n}(E)$$ |
| 150 | + |
| 151 | + Allow function in projection. |
| 152 | + |
| 153 | +* Aggregation Function |
| 154 | + |
| 155 | + $$_{G_1,G_2,...,G_n}\mathcal{G}_{F_1(A_1) as H_1,F_2(A_2) as H_2,...,F_n(A_n) as H_n}(E)$$ |
| 156 | + |
| 157 | + Input a multiset and output a single value. |
| 158 | + |
| 159 | + Multiset: Elements not unique, but unordered. |
| 160 | + |
| 161 | + Function: avg, count, min, max, *-distint. |
| 162 | + |
| 163 | +* Outer join |
| 164 | + |
| 165 | + Pad result with null, instead of dropping. |
| 166 | + |
| 167 | + * Left outer join |
| 168 | + |
| 169 | + $$r{\leftouterjoin}s$$ |
| 170 | + |
| 171 | + Include all rows in left. |
| 172 | + |
| 173 | + $$(r{\bowtie}s)\cup(r-\Pi_R(r{\bowtie}s))\times{(null,...,null)}$$ |
| 174 | + |
| 175 | + * Right outer join |
| 176 | + |
| 177 | + $$r{\rightouterjoin}s$$ |
| 178 | + |
| 179 | + * Full outer join |
| 180 | + |
| 181 | + $$r{\fullouterjoin}s$$ |
| 182 | + |
| 183 | +## Null |
| 184 | + |
| 185 | +* Arithmetic operations involving null returns null. |
| 186 | + |
| 187 | +* Comparison operations involving null returns unknown. |
| 188 | + |
| 189 | +* Boolean operations: |
| 190 | + |
| 191 | + * true and unknown = unknown, false and unknown = false, unknown and unknown = unknown |
| 192 | + |
| 193 | + * true or unknown = true, false or unknown = unknown, unknown or unknown = unknown |
| 194 | + |
| 195 | + * not unknown = unknown |
| 196 | + |
| 197 | +* Select: Discard row when predicate returns unknown or false. |
| 198 | + |
| 199 | +* Join: According to select, common null returns unknown, so discarded. |
| 200 | + |
| 201 | +* Projection: Null is the same. (Kind of arbitrary) |
| 202 | + |
| 203 | +* Union, Intersection, Difference: Null is the same. (Kind of arbitrary too) |
| 204 | + |
| 205 | +* Generalized projection: As projection. |
| 206 | + |
| 207 | +* Aggregate: Null is the same when grouping, discarded when aggregating. |
| 208 | + |
| 209 | +* Outer join: As join. |
| 210 | + |
| 211 | +## Database Modification |
| 212 | + |
| 213 | +* Delete |
| 214 | + |
| 215 | + $$r{\leftarrow}r-E$$ |
| 216 | + |
| 217 | +* Insert |
| 218 | + |
| 219 | + $$r{\leftarrow}r{\cup}E$$ |
| 220 | + |
| 221 | +* Update |
| 222 | + |
| 223 | + $$r{\leftarrow}\Pi_{F_1,F_2,...,F_n}(\sigma_P(r))\cup\Pi_{F_1,F_2,...,F_n}(r-\sigma_P(r))$$ |
| 224 | + |
| 225 | + Where $F_i$ is the update function when modification is needed, or original attribute value otherwise. |
0 commit comments