File notes/lectures_notes.md changed (mode: 100644) (index aa72ea4..979bd9e) |
... |
... |
To use the `DEFAULT` value, use |
1734 |
1734 |
INSERT INTO HURRICANE VALUES ("Test2", DEFAULT, NULL); |
INSERT INTO HURRICANE VALUES ("Test2", DEFAULT, NULL); |
1735 |
1735 |
``` |
``` |
1736 |
1736 |
|
|
1737 |
|
Note that, by default, the `DEFAULT` value is `NULL`, regardless of the datatype: |
|
|
1737 |
|
Note that, by default, the `DEFAULT` value is `NULL`, regardless of the datatype. |
|
1738 |
|
You can experiment it by running the following code: |
1738 |
1739 |
|
|
1739 |
1740 |
|
|
1740 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_DefaultTest.sql} |
|
|
1741 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_DefaultTest.sql snippet=demo1} |
1741 |
1742 |
``` |
``` |
1742 |
1743 |
|
|
1743 |
1744 |
### Editing Constraints |
### Editing Constraints |
|
... |
... |
Removing the default value: |
1807 |
1808 |
ALTER TABLE HURRICANE ALTER COLUMN WindSpeed DROP DEFAULT; |
ALTER TABLE HURRICANE ALTER COLUMN WindSpeed DROP DEFAULT; |
1808 |
1809 |
``` |
``` |
1809 |
1810 |
|
|
|
1811 |
|
Note that if you change the default value, it does *not* change the values you inserted retro-actively. |
|
1812 |
|
To resume on our previous example, the values inserted with `DEFAULT` as a value would still be `NULL` even after executing the following instruction: |
|
1813 |
|
|
|
1814 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_DefaultTest.sql snippet=modif1} |
|
1815 |
|
``` |
|
1816 |
|
|
1810 |
1817 |
#### Foreign key |
#### Foreign key |
1811 |
1818 |
|
|
1812 |
1819 |
Adding a foreign key constraint: |
Adding a foreign key constraint: |
|
... |
... |
And we can even specify the order (even the trivial one): |
2083 |
2090 |
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ProfExample.sql snippet=insert-3} |
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ProfExample.sql snippet=insert-3} |
2084 |
2091 |
``` |
``` |
2085 |
2092 |
|
|
2086 |
|
Note the date literals. |
|
|
2093 |
|
(Note the date literals) |
|
2094 |
|
|
|
2095 |
|
By default, the values that are not given are set to their respective `DEFAULT` values. |
|
2096 |
|
|
|
2097 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_DefaultTest.sql snippet=modif2} |
|
2098 |
|
``` |
2087 |
2099 |
|
|
2088 |
2100 |
### A Bit More on Foreign Keys |
### A Bit More on Foreign Keys |
2089 |
2101 |
|
|
|
... |
... |
The `COLLATE` operator can be used to force the search to be case-sensitive, as |
2147 |
2159 |
|
|
2148 |
2160 |
Cf. [@Textbook6, 5.1.1], [@Textbook7, 7.1.1] |
Cf. [@Textbook6, 5.1.1], [@Textbook7, 7.1.1] |
2149 |
2161 |
|
|
|
2162 |
|
The Boolean logic in SQL is three-valued: a statement can be `true`, `false` or `unknown`. |
|
2163 |
|
If you pick the following two commands: |
|
2164 |
|
|
|
2165 |
|
|
|
2166 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_DefaultTest.sql snippet=demo2} |
|
2167 |
|
``` |
|
2168 |
|
|
|
2169 |
|
you may believe that they will capture all the tuples in the `TEST` table, as the value for `TestA` is either `"A"` or not `"A"`, but you would be wrong. |
|
2170 |
|
If the value of `TestA` is `NULL`, then both conditions would fail, as SQL cannot say that the value is or is not `"A"`: it is simply undefined! |
|
2171 |
|
|
|
2172 |
|
|
2150 |
2173 |
### Meaning of `NULL` |
### Meaning of `NULL` |
2151 |
2174 |
|
|
2152 |
2175 |
`NULL` is |
`NULL` is |
2153 |
2176 |
|
|
2154 |
|
#. Unknown value ("Nobody knows") |
|
|
2177 |
|
#. Unknown value ("_Nobody knows_") |
2155 |
2178 |
|
|
2156 |
2179 |
> What is the date of birth of [Jack the Ripper](https://en.wikipedia.org/wiki/Jack_the_Ripper)? |
> What is the date of birth of [Jack the Ripper](https://en.wikipedia.org/wiki/Jack_the_Ripper)? |
2157 |
2180 |
|
|
2158 |
2181 |
> [Does P equal NP?](https://en.wikipedia.org/wiki/List_of_unsolved_problems_in_computer_science) |
> [Does P equal NP?](https://en.wikipedia.org/wiki/List_of_unsolved_problems_in_computer_science) |
2159 |
2182 |
|
|
2160 |
|
#. Unavailable / Withheld ("I do not have that information with me at the moment") |
|
|
2183 |
|
#. Unavailable / Withheld ("_I do not have that information with me at the moment_") |
2161 |
2184 |
|
|
2162 |
2185 |
> What is the number of english spies in France? |
> What is the number of english spies in France? |
2163 |
2186 |
|
|
|
... |
... |
Cf. [@Textbook6, 5.1.1], [@Textbook7, 7.1.1] |
2165 |
2188 |
|
|
2166 |
2189 |
> What is the identity of the Tiananmen Square person? |
> What is the identity of the Tiananmen Square person? |
2167 |
2190 |
|
|
2168 |
|
#. Not Applicable ("Your question does not make sense") |
|
|
2191 |
|
#. Not Applicable ("_Your question does not make sense_") |
2169 |
2192 |
|
|
2170 |
|
> What is the US SSN of a french person? |
|
|
2193 |
|
> What is the US SSN of a French person? |
2171 |
2194 |
|
|
2172 |
|
> What is the email address of an author of the XIXth century? |
|
|
2195 |
|
> What is the email address of an author of the XIX^th^ century? |
2173 |
2196 |
|
|
2174 |
2197 |
### Comparison with Unknown Values {#truth-tables} |
### Comparison with Unknown Values {#truth-tables} |
2175 |
2198 |
|
|
2176 |
2199 |
If `NULL` is involved in a comparison, the result evaluates to "**U**nknown." |
If `NULL` is involved in a comparison, the result evaluates to "**U**nknown." |
2177 |
|
If `NULL` is involved in a comparison, the result evaluates to "**U**nknown". |
|
2178 |
2200 |
|
|
2179 |
2201 |
|||| |
|||| |
2180 |
2202 |
:--: | :--: | :--: | :--: |
:--: | :--: | :--: | :--: |
|
... |
... |
You can test if a value is `NULL` with `IS NULL`. |
2205 |
2227 |
|
|
2206 |
2228 |
Note that you can not use `IS` to compare values: this key word is reserved to test if a value is (not) `NULL`, and nothing else. |
Note that you can not use `IS` to compare values: this key word is reserved to test if a value is (not) `NULL`, and nothing else. |
2207 |
2229 |
|
|
|
2230 |
|
This means that if you want to capture all the tuples, you cannot write |
|
2231 |
|
|
|
2232 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_DefaultTest.sql snippet=demo2} |
|
2233 |
|
``` |
|
2234 |
|
|
|
2235 |
|
but should have something like |
|
2236 |
|
|
|
2237 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_DefaultTest.sql snippet=demo3} |
|
2238 |
|
``` |
|
2239 |
|
|
|
2240 |
|
### Trivia |
|
2241 |
|
|
2208 |
2242 |
There are no `if…then…else` statements in `SQL`, but you can do something similar with `CASE` (cf. <https://dev.mysql.com/doc/refman/8.0/en/case.html>). |
There are no `if…then…else` statements in `SQL`, but you can do something similar with `CASE` (cf. <https://dev.mysql.com/doc/refman/8.0/en/case.html>). |
2209 |
2243 |
However, note that `SQL` is probably _not_ the right place to try to control the flow of execution. |
However, note that `SQL` is probably _not_ the right place to try to control the flow of execution. |
2210 |
2244 |
|
|
|
... |
... |
For aggregate functions, cf. [@Textbook6, 5.1.7] or [@Textbook7, 7.1.7]. |
2223 |
2257 |
Something that is not exactly a constraint, but that can be used to "qualify" domains, is the `AUTO_INCREMENT` feature of MySQL. |
Something that is not exactly a constraint, but that can be used to "qualify" domains, is the `AUTO_INCREMENT` feature of MySQL. |
2224 |
2258 |
Cf. <https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html>, you can have MySQL increment a particular attribute (most probably intended to be your primary key, or some form of counter) for you. |
Cf. <https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html>, you can have MySQL increment a particular attribute (most probably intended to be your primary key, or some form of counter) for you. |
2225 |
2259 |
|
|
|
2260 |
|
A typical example could be: |
|
2261 |
|
|
|
2262 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_AutoIncrement.sql snippet=demo1} |
|
2263 |
|
``` |
|
2264 |
|
|
|
2265 |
|
This way, the burden of having to keep track of the persons' ids is left to the program, and not to the person inserting data in the table. |
|
2266 |
|
|
|
2267 |
|
|
2226 |
2268 |
### Transactions |
### Transactions |
2227 |
2269 |
|
|
2228 |
2270 |
We can save the current state, and start a series of transactions, with the command |
We can save the current state, and start a series of transactions, with the command |