|
24 | 24 | * a "surrogate key" is a primary key containing columns inserted just to
|
25 | 25 | have a unique key e.g. an "id" column
|
26 | 26 | * a "compound key" is a key containing more than one column
|
27 |
| - * a "foreign key" is one or more columns which can be used togother to |
| 27 | + * a "foreign key" is one or more columns which can be used together to |
28 | 28 | uniquely idenify a single row in another table
|
29 | 29 | * whose only function as a way of linking this record to a record in
|
30 | 30 | another table
|
@@ -124,24 +124,189 @@ will warn you when it does that.
|
124 | 124 |
|
125 | 125 | # Aside: Character sets and collations
|
126 | 126 |
|
127 |
| -QUESTION: is the lenght restriction on string types in bytes and if so how are |
128 |
| -multi-byte chars handled |
129 |
| - |
130 | 127 | * default character set is `latin1`
|
131 | 128 | * you can specify character set at the server, database table or column level
|
132 | 129 | * MySQL lets you mix and match character sets and encodings at all levels
|
133 | 130 |
|
134 |
| - See separate file on character sets, encodings, unicode etc. |
| 131 | + See my other notes on character sets, encodings, unicode etc. |
135 | 132 |
|
136 | 133 | ```sql
|
137 | 134 | SHOW CHARACTER SET;
|
138 | 135 | -- look at the `maxlen` column to see how many bytes required by each character
|
139 | 136 | set
|
140 | 137 | ```
|
141 | 138 |
|
142 |
| -END CHAP 2 |
| 139 | +# Chapter 3: Queries |
| 140 | + |
| 141 | +When query is sent f |
| 142 | + |
| 143 | +1. client (lib or tool) makes connection to server |
| 144 | +1. client sends query text |
| 145 | +1. query is checked |
| 146 | + * syntax correct |
| 147 | + * user has permission to access data |
| 148 | + * user has permission to execute the query (functions etc.) |
| 149 | +1. query is handed to the optimizer to create an "execution plan" |
| 150 | +1. server executes the execution plan |
| 151 | +1. server returns table of results to client |
| 152 | + |
| 153 | +## Query clauses |
| 154 | + |
| 155 | +Queries are made up of 6 clauses |
| 156 | + |
| 157 | +### 1. Select |
| 158 | + |
| 159 | +``` |
| 160 | +http://www.postgresqltutorial.com/postgresql-select-distinct/ |
| 161 | +``` |
| 162 | + |
| 163 | +* first clause in the syntax but almost the last clause to be evaluated |
| 164 | +* filters columns from the big "in memory table" that From clause will build |
| 165 | +* things included as a column in the results are |
| 166 | + * column names from table created by Join clause |
| 167 | + * literals: number, string etc. `"foo"`, `33` |
| 168 | + * expressions: `some_col * 3` |
| 169 | + * built-in function calls |
| 170 | + * user defined function calls |
| 171 | +* allows you to define "column aliases" via 2 ways (AS is optional) |
| 172 | + 1. `some_val result` |
| 173 | + 1. `some_val AS result` |
| 174 | +* has two forms (three in postgres) |
| 175 | + 1. `SELECT ALL ...` |
| 176 | + * ALL (the default) will return all candidate rows, including duplicates. |
| 177 | + 1. `SELECT DISTINCT <column list> ...` |
| 178 | + * DISTINCT eliminates duplicate rows from the result. (one row is kept |
| 179 | + from each group of duplicates) |
| 180 | + * If you specify multiple columns, the DISTINCT clause will evaluate |
| 181 | + the duplicate based on the combination of values of those columns. |
| 182 | + * note that the 'DISTINCT` does not apply to a single column |
| 183 | + 1. `SELECT DISTINCT ON (<expression>) <column list> FROM ...` |
| 184 | + * DISTINCT ON calcluates the result of `<expression>` for all rows and eliminates all but the first row for each group where the result is the same |
| 185 | + |
| 186 | +Gotchas |
| 187 | + |
| 188 | +* `SELECT DISTINCT` without an `ORDER BY` clause is a code smell! |
| 189 | + * the "first row" of each set is unpredictable unless ORDER BY is used to |
| 190 | + ensure that the desired row appears first. |
| 191 | +* Notice that the DISTINCT ON expression must match the leftmost expression in |
| 192 | + the ORDER BY clause. |
| 193 | + |
| 194 | +```sql |
| 195 | +-- * these functions are defined in SQL (alternatives: pgsql, C, python, ruby etc.) |
| 196 | +-- * functions persist longer than just each query so we have to drop them each time we run this script |
| 197 | +drop function IF EXISTS one(); |
| 198 | +CREATE FUNCTION one() RETURNS integer AS $$ |
| 199 | + SELECT 1 AS result; |
| 200 | +$$ LANGUAGE SQL; |
| 201 | + |
| 202 | +drop function IF EXISTS add_em(INTEGER, INTEGER); |
| 203 | +CREATE FUNCTION add_em(integer, integer) RETURNS integer AS $$ |
| 204 | + SELECT $1 + $2; |
| 205 | +$$ LANGUAGE SQL; |
| 206 | + |
| 207 | +SELECT id, |
| 208 | + bt_transaction_id, |
| 209 | + 'hello' AS literal_string, |
| 210 | + 'other' no_as_literal_string, |
| 211 | + 33 AS literal_int, |
| 212 | + 4 * 5 AS expression, |
| 213 | + one() AS user_defined_func, |
| 214 | + add_em(4,6) AS user_defined_func_2, |
| 215 | + round(3.1459) AS built_in_func |
| 216 | +FROM settled_transactions; |
| 217 | +``` |
| 218 | + |
| 219 | + |
| 220 | +### 2. From |
| 221 | + |
| 222 | +* identifies tables to pull data from and how they should be joined into a single table |
| 223 | +* types of table |
| 224 | + 1. permenant table |
| 225 | + * stored on disk |
| 226 | + 2. temporary table |
| 227 | + * created as teh result of a subquery |
| 228 | + * `SELECT a.foo, b.foo FROM (SELECT ... FROM ... WHERE ...) AS a;` |
| 229 | + 3. virtual table |
| 230 | + * views |
| 231 | + |
| 232 | +Subqueries |
| 233 | + |
| 234 | +* a query embedded in another query |
| 235 | +* returns a table that can be used by the outer query |
| 236 | +* it is very common to alias the returned table so it can easily be used by the |
| 237 | + outer query |
| 238 | +* covered more in chap 9 |
| 239 | + |
| 240 | +Syntax is: |
| 241 | + |
| 242 | +```sql |
| 243 | +SELECT ... FROM (subquery) AS subq_result WHERE ... |
| 244 | +SELECT a.foo, b.foo FROM (SELECT ... FROM ... WHERE ...) AS a; |
| 245 | +``` |
| 246 | + |
| 247 | +View |
| 248 | + |
| 249 | +* a query which is stored in the "data dictionary" |
| 250 | +* its data is not stored on disk |
| 251 | + |
| 252 | +```sql |
| 253 | +CREATE VIEW <view_name> AS <select statement> |
| 254 | +CREATE VIEW some_view AS SELECT a, b, c FROM ... |
| 255 | +``` |
| 256 | + |
| 257 | +Joins |
| 258 | + |
| 259 | +* specify how to combine the given permenant, virtual, temporary tables into |
| 260 | + one large table |
| 261 | +* it is very common to alias tables uses in a join |
| 262 | + |
| 263 | +```sql |
| 264 | +SELECT <things> FROM <table-a> AS a <join condition> <table-b> AS b ON <on-condition> |
| 265 | +-- again the AS keyword is optional |
| 266 | +``` |
| 267 | + |
| 268 | +UP TO START WHERE CLAUSE |
| 269 | +### 3. Where |
| 270 | + |
| 271 | +* filters unwanted rows from the big "in memory table" or "result set table" |
| 272 | + that From will build. |
| 273 | +* WHERE takes one or more "filter conditions" |
| 274 | +* each filter condition is combined using a logical AND, OR, NOT. |
| 275 | +* note that `=` is the equality symbol in a filter condition (not `==` as it is in most programming languages) |
| 276 | +``` |
| 277 | +WHERE <filter-condition> and|or|not <filter-condition> ... |
| 278 | +``` |
| 279 | + |
| 280 | +### 4. Order by |
| 281 | + |
| 282 | +* sort the rows of the final result set by one or more columns |
| 283 | + |
| 284 | + |
| 285 | +### 5. Group by |
| 286 | + |
| 287 | +* finds trends in data |
| 288 | +* groups rows together by common column values |
| 289 | +* HAVING filters grouped data the same way WHERE filters raw data |
| 290 | +* described more fully in chap 8 |
| 291 | +* each row in the result table that GROUP BY creates is a "group"! |
| 292 | +* GROUP BY is all about collapsing multiple rows in the results table - it does not change columns. |
| 293 | +* To collapse multiple rows into a single row we need to instruct the DB about how to do it for each column |
| 294 | + * some columns will have the same value for each row so the output value can just be the input |
| 295 | + * this is the case for the columns you specify in the GROUP BY clause (because we have chosen to use these columns to make our categories) |
| 296 | + * other columns will need a function to help with a signature a bit like |
| 297 | + * `function boil_down(column_values: Set<T>) -> <T>` |
| 298 | + * examples of "boil down" functions are |
| 299 | + * sum() |
| 300 | + * max() |
| 301 | + * min() |
| 302 | + |
| 303 | +### 6. Having |
| 304 | + |
| 305 | +* filters out unwanted groups |
| 306 | +* HAVING filters grouped data the same way WHERE filters raw data |
143 | 307 |
|
144 | 308 |
|
| 309 | +UP TO END OF SELECT CLAUSE IN CHAP 3 |
145 | 310 |
|
146 | 311 |
|
147 | 312 |
|
|
0 commit comments