File notes/lectures_notes.md changed (mode: 100644) (index 4aee7dc..ddb262f) |
... |
... |
SELECT Name FROM PROF |
1729 |
1729 |
WHERE Login LIKE '_aubert'; |
WHERE Login LIKE '_aubert'; |
1730 |
1730 |
~~~ |
~~~ |
1731 |
1731 |
|
|
|
1732 |
|
Note that `LIKE` is by default case-insensitive, both in [MariaDB](https://mariadb.com/kb/en/library/like/) and in [MySQL](https://dev.mysql.com/doc/refman/8.0/en/case-sensitivity.html). |
|
1733 |
|
The `COLLATE` operator can be used to force the search to be case-sensitive. |
|
1734 |
|
|
1732 |
1735 |
--- |
--- |
1733 |
1736 |
|
|
1734 |
1737 |
|
|
|
... |
... |
SELECT Fellow.Name AS 'Fellow of Ava' |
1985 |
1988 |
FROM STUDENT AS Me, STUDENT AS Fellow |
FROM STUDENT AS Me, STUDENT AS Fellow |
1986 |
1989 |
WHERE Me.Name = 'Ava Alyx' |
WHERE Me.Name = 'Ava Alyx' |
1987 |
1990 |
AND Fellow.Major = Me.Major |
AND Fellow.Major = Me.Major |
1988 |
|
AND NOT Fellow.Name = 'Ava Alyx'; |
|
|
1991 |
|
AND NOT Fellow.Name = Me.Name; |
1989 |
1992 |
~~~ |
~~~ |
1990 |
1993 |
|
|
1991 |
1994 |
`AND NOT Me = Fellow` would *not* work. |
`AND NOT Me = Fellow` would *not* work. |
|
... |
... |
A nested query is made of an outer query (`SELECT Login`…) and an inner query |
2007 |
2010 |
SELECT Login FROM GRADE |
SELECT Login FROM GRADE |
2008 |
2011 |
WHERE Grade >= |
WHERE Grade >= |
2009 |
2012 |
ALL (SELECT Grade FROM GRADE WHERE Grade IS NOT NULL); |
ALL (SELECT Grade FROM GRADE WHERE Grade IS NOT NULL); |
|
2013 |
|
~~~ |
|
2014 |
|
|
|
2015 |
|
Note that this query could be simplified, using `MAX`: |
|
2016 |
|
|
|
2017 |
|
~~~{.sqlmysql .numberLines} |
|
2018 |
|
SELECT Login FROM GRADE |
|
2019 |
|
WHERE Grade >= |
|
2020 |
|
(SELECT MAX(Grade) FROM GRADE); |
|
2021 |
|
~~~ |
2010 |
2022 |
|
|
|
2023 |
|
~~~{.sqlmysql .numberLines} |
2011 |
2024 |
SELECT Login |
SELECT Login |
2012 |
2025 |
FROM PROF |
FROM PROF |
2013 |
2026 |
WHERE DEPARTMENT IN ( SELECT Major |
WHERE DEPARTMENT IN ( SELECT Major |
|
... |
... |
After that last command, you should see |
2356 |
2369 |
~~~ |
~~~ |
2357 |
2370 |
|
|
2358 |
2371 |
|
|
2359 |
|
Finally, we can erasre the content of the table, then erase ("drop") the table, and finally the schema: |
|
|
2372 |
|
Finally, we can erase the content of the table, then erase ("drop") the table, and finally the schema: |
2360 |
2373 |
|
|
2361 |
2374 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
2362 |
2375 |
DELETE FROM TableTest; -- Delete the rows |
DELETE FROM TableTest; -- Delete the rows |
|
... |
... |
EXIT; |
2372 |
2385 |
|
|
2373 |
2386 |
## Exercises {-} |
## Exercises {-} |
2374 |
2387 |
|
|
|
2388 |
|
Exercise +.# |
|
2389 |
|
~ |
|
2390 |
|
|
|
2391 |
|
For each of the following, fill in the blanks: |
|
2392 |
|
|
|
2393 |
|
- In `SQL`, a relation is called a ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ . |
|
2394 |
|
- In `SQL`, every statement ends with ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟, and in-line comments start with a ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ . |
|
2395 |
|
- In `SQL`, there is no `string` datatype, so we have to use ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟. |
|
2396 |
|
- The Data Control Language of `SQL`'s role is to ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ ͟ . |
|
2397 |
|
|
2375 |
2398 |
Exercise +.# |
Exercise +.# |
2376 |
2399 |
|
|
2377 |
2400 |
: What does it mean to say that `SQL` is at the same time a "data definition language" and a "data manipulation language"? |
: What does it mean to say that `SQL` is at the same time a "data definition language" and a "data manipulation language"? |
|
... |
... |
Exercise +.# |
2406 |
2429 |
|
|
2407 |
2430 |
: Explain this query: `CREATE SCHEMA FACULTY;`{.sqlmysql}. |
: Explain this query: `CREATE SCHEMA FACULTY;`{.sqlmysql}. |
2408 |
2431 |
|
|
|
2432 |
|
Exercise +.#explainsql |
|
2433 |
|
~ |
|
2434 |
|
|
|
2435 |
|
Write code to |
|
2436 |
|
|
|
2437 |
|
- declare a first table with two attributes, one of which is the primary key, |
|
2438 |
|
- declare a second table with two attributes, one of which is the primary key, and the other references the primary key of the first table, |
|
2439 |
|
- insert one tuple in the first table, |
|
2440 |
|
- insert one tuple in the second table, referencing the only tuple of the first table, |
|
2441 |
|
|
|
2442 |
|
You are free to come up with an example (even very simple or cryptic) or to re-use an example from class. |
|
2443 |
|
|
|
2444 |
|
|
2409 |
2445 |
Exercise +.#explainsql |
Exercise +.#explainsql |
2410 |
2446 |
~ |
~ |
2411 |
2447 |
|
|
|
... |
... |
Exercise +.#twice_row_sql |
2603 |
2639 |
|
|
2604 |
2640 |
: Under which conditions does `SQL` allow you to enter the same row in a table twice? |
: Under which conditions does `SQL` allow you to enter the same row in a table twice? |
2605 |
2641 |
|
|
2606 |
|
#### Exercises {-} |
|
2607 |
|
|
|
2608 |
2642 |
## Solution to Exercises {-} |
## Solution to Exercises {-} |
2609 |
2643 |
|
|
|
2644 |
|
|
|
2645 |
|
Solution +.# |
|
2646 |
|
~ |
|
2647 |
|
The blanks can be filled as follow: |
|
2648 |
|
|
|
2649 |
|
- In `SQL`, a relation is called a ͟ ͟ ͟ ͟table ͟ ͟ ͟ ͟ ͟ ͟ ͟ . |
|
2650 |
|
- In `SQL`, every statement ends with ͟ ͟a semi-colon (`;`) ͟ ͟, and in-line comments start with a ͟ ͟ ͟two minus signs (`--`) ͟ ͟ . |
|
2651 |
|
- In `SQL`, there is no `string` datatype, so we have to use ͟ ͟ ͟VARCHAR(x) or CHAR(x) where x is an integer reflecting the maximum (or fixed) size of the string ͟ ͟ ͟ ͟ ͟ . |
|
2652 |
|
- The Data Control Language of `SQL`'s role is to ͟ ͟ control access to the data stored, by creating users and granting them rights ͟ ͟ . |
|
2653 |
|
|
2610 |
2654 |
Solution +.# |
Solution +.# |
2611 |
2655 |
|
|
2612 |
2656 |
: It can specify the conceptual and internal schema, **and** it can manipulate the data. |
: It can specify the conceptual and internal schema, **and** it can manipulate the data. |
|
... |
... |
Solution +.# |
2639 |
2683 |
|
|
2640 |
2684 |
: It creates a schema, i.e., a database, named `Faculty`{.sqlmysql}. |
: It creates a schema, i.e., a database, named `Faculty`{.sqlmysql}. |
2641 |
2685 |
|
|
|
2686 |
|
Solution +.# |
|
2687 |
|
~ |
|
2688 |
|
A simple and compact code could be: |
|
2689 |
|
|
|
2690 |
|
~~~{.sqlmysql} |
|
2691 |
|
-- You can ignore the first three lines. |
|
2692 |
|
DROP SCHEMA IF EXISTS HW_SHORT; |
|
2693 |
|
CREATE SCHEMA HW_SHORT; |
|
2694 |
|
USE HW_SHORT; |
|
2695 |
|
CREATE TABLE A(Att1 INT PRIMARY KEY, Att2 INT); |
|
2696 |
|
CREATE TABLE B(Att3 INT PRIMARY KEY, Att4 INT, FOREIGN KEY (Att4) REFERENCES A(Att1)); |
|
2697 |
|
INSERT INTO A VALUES (1, 2); |
|
2698 |
|
INSERT INTO B VALUES (3, 1); |
|
2699 |
|
~~~~ |
|
2700 |
|
|
2642 |
2701 |
Solution +.# |
Solution +.# |
2643 |
2702 |
|
|
2644 |
2703 |
: It removes the `UNIQUE`{.sqlmysql} constraint on the `Attribute1`{.sqlmysql} in the `TABLEA`{.sqlmysql} table. |
: It removes the `UNIQUE`{.sqlmysql} constraint on the `Attribute1`{.sqlmysql} in the `TABLEA`{.sqlmysql} table. |
|
... |
... |
Solution +.# |
2763 |
2822 |
- `FALSE AND UNKNOWN`{.sqlmysql} → `FALSE`{.sqlmysql} |
- `FALSE AND UNKNOWN`{.sqlmysql} → `FALSE`{.sqlmysql} |
2764 |
2823 |
- The other cases can be deduced by symmetry. |
- The other cases can be deduced by symmetry. |
2765 |
2824 |
|
|
2766 |
|
For a more compact presentation, refer to (#truth-tables). |
|
|
2825 |
|
For a more compact presentation, refer to [the three-valued truth table"](#truth-tables). |
2767 |
2826 |
|
|
2768 |
2827 |
Solution +.# |
Solution +.# |
2769 |
2828 |
|
|
|
... |
... |
Solution +.# |
2799 |
2858 |
|
|
2800 |
2859 |
Solution +.# |
Solution +.# |
2801 |
2860 |
|
|
2802 |
|
: Essentially, if there are no primary key in the relation, and if no attribute has the `UNIQUE`{.sqlmysql} constraint. Cf. also [%D %n (%T)](#problem:repetition). |
|
2803 |
|
|
|
|
2861 |
|
: Essentially, if there are no primary key in the relation, and if no attribute has the `UNIQUE`{.sqlmysql} constraint. Cf. also [this previous problem](#problem:repetition). |
|
2862 |
|
<!-- |
|
2863 |
|
Bug. |
|
2864 |
|
Link to Duplicate rows in SQL |
|
2865 |
|
For some reason, |
|
2866 |
|
[%D %n (%T)](#problem:repetition). |
|
2867 |
|
does not work as intended. |
|
2868 |
|
--> |
2804 |
2869 |
|
|
2805 |
2870 |
## Problems {-} |
## Problems {-} |
2806 |
2871 |
|
|
|
... |
... |
Problem (A simple database for books) +.#sqlBooks |
3351 |
3416 |
## Solution to Selected Problems {-} |
## Solution to Selected Problems {-} |
3352 |
3417 |
|
|
3353 |
3418 |
|
|
3354 |
|
Solution to [%D %n (%T)](#problem:ComputerSelectBis) |
|
3355 |
|
|
|
3356 |
|
~ |
|
3357 |
|
|
|
3358 |
|
~~~{.sqlmysql} |
|
3359 |
|
SELECT Model FROM COMPUTER WHERE Id = 'A'; |
|
3360 |
|
SELECT Type FROM PERIPHERAL WHERE Id = '14'; |
|
3361 |
|
SELECT Model FROM PERIPHERAL WHERE Type = 'printer'; |
|
3362 |
|
SELECT Model FROM PERIPHERAL WHERE Model LIKE 'IBM%'; |
|
3363 |
|
SELECT Model FROM PERIPHERAL, CONNEXION WHERE Computer = 'A' AND Peripheral = PERIPHERAL.Id; |
|
3364 |
|
SELECT COUNT(Computer) FROM CONNEXION, COMPUTER WHERE Model = 'Apple IIc Plus' AND Computer = COMPUTER.Id; |
|
3365 |
|
~~~ |
|
3366 |
|
|
|
3367 |
|
--- |
|
3368 |
|
|
|
3369 |
3419 |
Solution to [%D %n (%T)](#problem:address) |
Solution to [%D %n (%T)](#problem:address) |
3370 |
3420 |
~ |
~ |
3371 |
3421 |
|
|
|
... |
... |
Solution to [%D %n (%T)](#problem:coffee) |
3663 |
3713 |
|
|
3664 |
3714 |
--- |
--- |
3665 |
3715 |
|
|
|
3716 |
|
Solution to [%D %n (%T)](#problem:ComputerSelectBis) |
|
3717 |
|
|
|
3718 |
|
~ |
|
3719 |
|
|
|
3720 |
|
~~~{.sqlmysql} |
|
3721 |
|
SELECT Model FROM COMPUTER WHERE Id = 'A'; |
|
3722 |
|
SELECT Type FROM PERIPHERAL WHERE Id = '14'; |
|
3723 |
|
SELECT Model FROM PERIPHERAL WHERE Type = 'printer'; |
|
3724 |
|
SELECT Model FROM PERIPHERAL WHERE Model LIKE 'IBM%'; |
|
3725 |
|
SELECT Model FROM PERIPHERAL, CONNEXION WHERE Computer = 'A' AND Peripheral = PERIPHERAL.Id; |
|
3726 |
|
SELECT COUNT(Computer) FROM CONNEXION, COMPUTER WHERE Model = 'Apple IIc Plus' AND Computer = COMPUTER.Id; |
|
3727 |
|
~~~ |
|
3728 |
|
|
|
3729 |
|
--- |
|
3730 |
|
|
3666 |
3731 |
Solution to [%D %n (%T)](#problem:roleplaying) |
Solution to [%D %n (%T)](#problem:roleplaying) |
3667 |
3732 |
~ |
~ |
3668 |
3733 |
|
|