File notes/lectures_notes.md changed (mode: 100644) (index aa3bffc..8791dee) |
... |
... |
Exercise +.# |
729 |
729 |
|
|
730 |
730 |
: What, if any, is the difference between a superkey, a key, and a primary key? |
: What, if any, is the difference between a superkey, a key, and a primary key? |
731 |
731 |
|
|
|
732 |
|
Exercise +.# |
|
733 |
|
|
|
734 |
|
: Name the two kind of integrities that must be respected by the tuples in a relation. |
|
735 |
|
|
732 |
736 |
Exercise +.#entityintegrity |
Exercise +.#entityintegrity |
733 |
737 |
|
|
734 |
738 |
: What is entity integrity? Why is it useful? |
: What is entity integrity? Why is it useful? |
|
... |
... |
Exercise +.# |
739 |
743 |
|
|
740 |
744 |
Exercise +.# |
Exercise +.# |
741 |
745 |
|
|
|
746 |
|
: If in a relation $R_1$, an attribute $A_1$ is a foreign key referencing an attribute $A_2$ in a relation $R_2$, what does this implies about $A_2$? |
|
747 |
|
|
|
748 |
|
Exercise +.# |
|
749 |
|
|
742 |
750 |
: Give three examples of operations. |
: Give three examples of operations. |
743 |
751 |
|
|
|
752 |
|
Exercise +.# |
|
753 |
|
~ |
|
754 |
|
Consider the following two relations: |
|
755 |
|
|
|
756 |
|
COMPUTER(Owner, RAM, Year, Brand) |
|
757 |
|
OS(Name, Version, Architecture) |
|
758 |
|
|
|
759 |
|
For each, give a) The arity of the relation, b) A (preferably plausible) example of tuple to insert. |
|
760 |
|
|
744 |
761 |
Exercise +.# |
Exercise +.# |
745 |
762 |
|
|
746 |
763 |
: Define what is the domain constraint. |
: Define what is the domain constraint. |
|
... |
... |
Exercise +.# |
782 |
799 |
#. `Insert <'XB-124', 'GPalmer', '02/04/2018'> into ASSIGNED-TO` |
#. `Insert <'XB-124', 'GPalmer', '02/04/2018'> into ASSIGNED-TO` |
783 |
800 |
#. `Insert <'BTed, 'Bobby Ted', 'Senior'> and <'BTed', 'Bobby Ted Jr.', 'Junior'> into CONDUCTOR` |
#. `Insert <'BTed, 'Bobby Ted', 'Senior'> and <'BTed', 'Bobby Ted Jr.', 'Junior'> into CONDUCTOR` |
784 |
801 |
|
|
|
802 |
|
|
|
803 |
|
Exercise +.# |
|
804 |
|
~ Consider the following relation schema and state: |
|
805 |
|
|
|
806 |
|
<!-- Bug with table --> |
|
807 |
|
**A** | **B** | **C** | **D** | |
|
808 |
|
--- | --- | --- | --- | |
|
809 |
|
2 | Blue | Austin | `true`| |
|
810 |
|
1 | Yellow | Paris | `true` | |
|
811 |
|
1 | Purple | Pisa | `false`| |
|
812 |
|
2 | Yellow | Augusta | `true`| |
|
813 |
|
|
|
814 |
|
Assuming that this is all the data we will ever have, discuss whenever $\{A, B, C, D\}$, $\{A, B\}$ and $\{B\}$ are superkeys and/or keys. |
|
815 |
|
|
785 |
816 |
## Solution to Exercises {-} |
## Solution to Exercises {-} |
786 |
817 |
|
|
787 |
818 |
Solution +.# |
Solution +.# |
|
... |
... |
The primary key is one of the candidate key, i.e., the key that was chosen. |
812 |
843 |
|
|
813 |
844 |
Solution +.# |
Solution +.# |
814 |
845 |
|
|
|
846 |
|
: Referential integrity and entity integrity. |
|
847 |
|
|
|
848 |
|
Solution +.# |
|
849 |
|
|
815 |
850 |
: Entity integrity ensures that each row of a table has a unique and non-null primary key value. It allows to make sure that every tuple is different from the others, and helps to "pick" elements in the database. |
: Entity integrity ensures that each row of a table has a unique and non-null primary key value. It allows to make sure that every tuple is different from the others, and helps to "pick" elements in the database. |
816 |
851 |
|
|
817 |
852 |
Solution +.# |
Solution +.# |
|
... |
... |
Solution +.# |
820 |
855 |
|
|
821 |
856 |
Solution +.# |
Solution +.# |
822 |
857 |
|
|
|
858 |
|
: Then we know that $A_2$ is the primary key of $R_2$. |
|
859 |
|
|
|
860 |
|
Solution +.# |
|
861 |
|
|
823 |
862 |
: Reading from the database, performing `UPDATE`{.sqlmysql} or `DELETE`{.sqlmysql} operations. |
: Reading from the database, performing `UPDATE`{.sqlmysql} or `DELETE`{.sqlmysql} operations. |
824 |
863 |
|
|
|
864 |
|
Solution +.# |
|
865 |
|
~ |
|
866 |
|
|
|
867 |
|
a) COMPUTER has for arity $4$, and OS has for arity $3$. |
|
868 |
|
b) ("Linda McFather", 32, 2017, "Purism"), and ("Debian", "Stable", "amd64") |
|
869 |
|
|
825 |
870 |
Solution +.# |
Solution +.# |
826 |
871 |
|
|
827 |
872 |
: The requirement that each tuple must have for an attribute $A$ an atomic value from the domain dom($A$), or `NULL`{.sqlmysql}. |
: The requirement that each tuple must have for an attribute $A$ an atomic value from the domain dom($A$), or `NULL`{.sqlmysql}. |
|
... |
... |
Solution +.# |
850 |
895 |
#. No, it violates the referential integrity constraint: `'XB-124` and `'GPalmer'` are not values in `TRAIN.Ref` and `CONDUCTOR.CompanyID`. |
#. No, it violates the referential integrity constraint: `'XB-124` and `'GPalmer'` are not values in `TRAIN.Ref` and `CONDUCTOR.CompanyID`. |
851 |
896 |
#. No, it violates the key constraint: two tuples can't have the same value for the primary key. |
#. No, it violates the key constraint: two tuples can't have the same value for the primary key. |
852 |
897 |
|
|
|
898 |
|
Solution +.# |
|
899 |
|
~ |
|
900 |
|
|
|
901 |
|
- $\{A, B, C, D\}$ is a superkey (the set of all the attributes is always a superkey), but not a superkey, as removing e.g. $D$ would still make it a superkey. |
|
902 |
|
- $\{A, B\}$ is a superkey **and** a key, as neither $\{A\}$ nor $\{B\}$ are keys. |
|
903 |
|
- $\{A\}$ is not a key, and not a superkey: multiple tuples have the value $1$. |
|
904 |
|
|
853 |
905 |
## Problems {-} |
## Problems {-} |
854 |
906 |
|
|
855 |
907 |
Problem (Finding candidate key in a CLASS relation) +.#candidatekeyinclass |
Problem (Finding candidate key in a CLASS relation) +.#candidatekeyinclass |
|
... |
... |
Type and domains are two different things in some implementations, cf. for insta |
988 |
1040 |
The following commands are particularly useful: |
The following commands are particularly useful: |
989 |
1041 |
|
|
990 |
1042 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
991 |
|
SHOW SCHEMAS; -- List the schema |
|
992 |
|
SHOW TABLES; -- List the tables in a schema |
|
993 |
|
DESCRIBE <TableName>; -- Show the structure of a table |
|
994 |
|
SELECT * FROM <TableName> -- List all the rows in a table |
|
995 |
|
DROP TABLE <TableName>; -- "Drop" (erase) a table |
|
996 |
|
DROP SCHEMA <SchemaName>; -- Drop a schema |
|
|
1043 |
|
SHOW SCHEMAS; -- List the schemas. |
|
1044 |
|
SHOW TABLES; -- List the tables in a schema. |
|
1045 |
|
SHOW CREATE TABLE <TableName> -- Gives the command to "re-construct" TableName. |
|
1046 |
|
DESCRIBE <TableName>; -- Show the structure of TableName. |
|
1047 |
|
SELECT * FROM <TableName> -- List all the rows in TableName. |
|
1048 |
|
DROP TABLE <TableName>; -- "Drop" (erase) TableName. |
|
1049 |
|
DROP SCHEMA <SchemaName>; -- "Drop" (erase" SchemaName. |
997 |
1050 |
SHOW WARNINGS; -- After a warning was issued, show the content of the warning. |
SHOW WARNINGS; -- After a warning was issued, show the content of the warning. |
998 |
1051 |
~~~ |
~~~ |
999 |
1052 |
|
|
|
... |
... |
For `DISTINCT`, `ALL` and `UNION`, cf. [@Textbook6, 4.3.4] or [@Textbook7, 6.3. |
1502 |
1555 |
For `ORDER BY` , cf. [@Textbook6, 4.3.6] or [@Textbook7, 6.3.6]. |
For `ORDER BY` , cf. [@Textbook6, 4.3.6] or [@Textbook7, 6.3.6]. |
1503 |
1556 |
For aggregate functions, cf. [@Textbook6, 5.1.7] or [@Textbook7, 7.1.7]. |
For aggregate functions, cf. [@Textbook6, 5.1.7] or [@Textbook7, 7.1.7]. |
1504 |
1557 |
|
|
|
1558 |
|
|
|
1559 |
|
### Transactions |
|
1560 |
|
|
|
1561 |
|
We can save the current state, and start a series of transactions, with the command |
|
1562 |
|
|
|
1563 |
|
~~~{.sqlmysql} |
|
1564 |
|
START TRANSACTION; |
|
1565 |
|
~~~ |
|
1566 |
|
|
|
1567 |
|
All the commands that follows are "virtually" executed: you can undo them all using |
|
1568 |
|
|
|
1569 |
|
~~~{.sqlmysql} |
|
1570 |
|
ROLLBACK; |
|
1571 |
|
~~~ |
|
1572 |
|
|
|
1573 |
|
This puts you back in the state you were in before starting the transaction. |
|
1574 |
|
If you want all the commands you typed in-between to be actually enforced, you can use the command |
|
1575 |
|
|
|
1576 |
|
~~~{.sqlmysql} |
|
1577 |
|
COMMIT; |
|
1578 |
|
~~~ |
|
1579 |
|
|
|
1580 |
|
Nested transactions are technically possible, but they are counter-intuitive and should be avoided, cf. <https://www.sqlskills.com/blogs/paul/a-sql-server-dba-myth-a-day-2630-nested-transactions-are-real/>. |
|
1581 |
|
|
1505 |
1582 |
### DISTINCT / ALL |
### DISTINCT / ALL |
1506 |
1583 |
|
|
1507 |
1584 |
The result of a `SELECT` query, for instance, is a table, and SQL treats tables as multi-set, hence there can be repetitions in the result of a query, but we can remove them: |
The result of a `SELECT` query, for instance, is a table, and SQL treats tables as multi-set, hence there can be repetitions in the result of a query, but we can remove them: |
|
... |
... |
SELECT Login, Major FROM STUDENT |
1537 |
1614 |
ORDER BY Major, Name; |
ORDER BY Major, Name; |
1538 |
1615 |
~~~ |
~~~ |
1539 |
1616 |
|
|
|
1617 |
|
`ORDER BY` order by ascending order by default. |
|
1618 |
|
|
1540 |
1619 |
### Aggregate functions |
### Aggregate functions |
1541 |
1620 |
|
|
1542 |
1621 |
You can use `MAX`, `SUM`, `MIN`, `AVG`, `COUNT`: |
You can use `MAX`, `SUM`, `MIN`, `AVG`, `COUNT`: |
|
... |
... |
SELECT Login AS Username FROM PROF; |
1573 |
1652 |
+----------+ |
+----------+ |
1574 |
1653 |
~~~ |
~~~ |
1575 |
1654 |
|
|
|
1655 |
|
The purpose of aliases will be clearer as we study select-project-join queries. |
|
1656 |
|
|
1576 |
1657 |
## Three-Valued Logic |
## Three-Valued Logic |
1577 |
1658 |
|
|
1578 |
1659 |
Cf. [@Textbook6, 5.1.1], [@Textbook7, 7.1.1] |
Cf. [@Textbook6, 5.1.1], [@Textbook7, 7.1.1] |
|
... |
... |
You can test if a value is `NULL` with `IS NULL`. |
1633 |
1714 |
INSERT INTO DEPARTMENT Values ('Hist', 'History', NULL); |
INSERT INTO DEPARTMENT Values ('Hist', 'History', NULL); |
1634 |
1715 |
SELECT * FROM DEPARTMENT WHERE Head IS NULL; |
SELECT * FROM DEPARTMENT WHERE Head IS NULL; |
1635 |
1716 |
SELECT * FROM DEPARTMENT WHERE Head IS NOT NULL; |
SELECT * FROM DEPARTMENT WHERE Head IS NOT NULL; |
1636 |
|
|
|
1637 |
1717 |
SELECT COUNT(*) FROM GRADE WHERE Grade IS NULL; |
SELECT COUNT(*) FROM GRADE WHERE Grade IS NULL; |
1638 |
1718 |
~~~ |
~~~ |
1639 |
1719 |
|
|
|
... |
... |
We can further ask our DBMS to display the structure of the table we just create |
2029 |
2109 |
DESCRIBE TableTest; -- Can be abbreviated as DESC TableTest; |
DESCRIBE TableTest; -- Can be abbreviated as DESC TableTest; |
2030 |
2110 |
~~~ |
~~~ |
2031 |
2111 |
|
|
2032 |
|
And even ask to get back the code that would create the exact same structure: |
|
|
2112 |
|
And even ask to get back the code that would create the exact same structure (but without the data!): |
2033 |
2113 |
|
|
2034 |
2114 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
2035 |
2115 |
SHOW CREATE TABLE TableTest; |
SHOW CREATE TABLE TableTest; |
|
... |
... |
You're all set! All you have to do is to quit, using the command |
2078 |
2158 |
EXIT; |
EXIT; |
2079 |
2159 |
~~~ |
~~~ |
2080 |
2160 |
|
|
2081 |
|
|
|
2082 |
2161 |
## Exercises {-} |
## Exercises {-} |
2083 |
2162 |
|
|
2084 |
2163 |
Exercise +.# |
Exercise +.# |
|
... |
... |
Exercise +.# |
2134 |
2213 |
: Write a statement that inserts the values `"Thomas"` and `4` into the table `TRAINS`. |
: Write a statement that inserts the values `"Thomas"` and `4` into the table `TRAINS`. |
2135 |
2214 |
|
|
2136 |
2215 |
Exercise +.# |
Exercise +.# |
|
2216 |
|
~ |
2137 |
2217 |
|
|
2138 |
|
: If `PkgName` is the primary key in the table `MYTABLE`, what can you tell about the number of rows returned by the following statement? `SELECT * FROM MYTABLE WHERE PkgName = 'MySQL';`{.sqlmysql}. |
|
2139 |
|
|
|
2140 |
|
Exercise +.# |
|
2141 |
|
|
|
2142 |
|
: What is the difference between an implicit, an explicit, and a semantic constraint? |
|
2143 |
|
|
|
2144 |
|
Exercise +.#explainfk |
|
2145 |
|
|
|
2146 |
|
: What is a foreign key? Why is it useful? |
|
|
2218 |
|
If `PkgName` is the primary key in the table `MYTABLE`, what can you tell about the number of rows returned by the following statement? |
|
2219 |
|
`SELECT * FROM MYTABLE WHERE PkgName = 'MySQL';`{.sqlmysql}. |
2147 |
2220 |
|
|
2148 |
2221 |
Exercise +.# |
Exercise +.# |
2149 |
2222 |
|
|
|
... |
... |
Exercise +.# |
2151 |
2224 |
|
|
2152 |
2225 |
Exercise +.# |
Exercise +.# |
2153 |
2226 |
|
|
2154 |
|
: If a database designer is using the `ON UPDATE SET NULL` for a foreign key, what mechanism is he implementing (i.e., describe how the database will react a certain operation)? |
|
|
2227 |
|
: If a database designer is using the `ON UPDATE SET NULL` for a foreign key, what mechanism is (s)he implementing (i.e., describe how the database will react a certain operation)? |
2155 |
2228 |
|
|
2156 |
2229 |
Exercise +.# |
Exercise +.# |
2157 |
2230 |
~ |
~ |
|
... |
... |
Exercise +.# |
2161 |
2234 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
2162 |
2235 |
FOREIGN KEY (DptNumber) REFERENCES DEPARTMENT(Number) |
FOREIGN KEY (DptNumber) REFERENCES DEPARTMENT(Number) |
2163 |
2236 |
ON DELETE SET DEFAULT |
ON DELETE SET DEFAULT |
2164 |
|
ON UPDATE CASCADE |
|
|
2237 |
|
ON UPDATE CASCADE; |
2165 |
2238 |
~~~ |
~~~ |
2166 |
2239 |
|
|
2167 |
2240 |
What happen to the rows whose foreign key `DptNumber` are set to `3` if the row in the `DEPARTEMENT` table with primary key `Number` set to `3` is… |
What happen to the rows whose foreign key `DptNumber` are set to `3` if the row in the `DEPARTEMENT` table with primary key `Number` set to `3` is… |
|
... |
... |
Exercise +.# |
2175 |
2248 |
If the following is part of the design of a `WORKER` table: |
If the following is part of the design of a `WORKER` table: |
2176 |
2249 |
|
|
2177 |
2250 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
2178 |
|
FOREIGN KEY WORKER(DptNumber) REFERENCES DEPARTMENT(DptNumber) |
|
2179 |
|
ON UPDATE CASCADE |
|
|
2251 |
|
FOREIGN KEY WORKER(DptNumber) REFERENCES DEPARTMENT(DptNumber) |
|
2252 |
|
ON UPDATE CASCADE; |
2180 |
2253 |
~~~ |
~~~ |
2181 |
2254 |
|
|
2182 |
2255 |
What happen to the rows whose foreign key `DptNumber` are set to `3` if the row in the `DEPARTMENT` table with primary key `Number` set to `3` is… |
What happen to the rows whose foreign key `DptNumber` are set to `3` if the row in the `DEPARTMENT` table with primary key `Number` set to `3` is… |
|
... |
... |
Solution +.# |
2360 |
2433 |
Solution +.# |
Solution +.# |
2361 |
2434 |
|
|
2362 |
2435 |
: Yes. |
: Yes. |
2363 |
|
|
|
2364 |
|
Solution +.# |
|
2365 |
|
~ |
|
2366 |
|
[@Textbook6, pp. 67--69] or [@Textbook7, pp. 157--158] reads: |
|
2367 |
2436 |
|
|
2368 |
|
> Constraints on databases can generally be divided into three main categories: |
|
2369 |
|
> |
|
2370 |
|
>#. Constraints that are inherent in the data model. We call these **inherent model-based constraints** or **implicit constraints**. |
|
2371 |
|
>#. Constraints that can be directly expressed in schemas of the data model, typically by specifying them in the DDL (data definition language, see Section 2.3.1). We call these **schema-based constraints** or **explicit constraints**. |
|
2372 |
|
>#. Constraints that *cannot* be directly expressed in the schemas of the data model, and hence must be expressed and enforced by the application programs. We call these **application-based** or **semantic constraints** or **business rules**. |
|
2373 |
|
|
|
2374 |
|
Solution +.# |
|
2375 |
|
|
|
2376 |
|
: A forein key is an attribute (or column, field) referencing another attribute, which must be part of a primary key. It establishes connection between tables. |
|
2377 |
|
|
|
2378 |
2437 |
Solution +.# |
Solution +.# |
2379 |
2438 |
|
|
2380 |
2439 |
: We should use a referential triggered action clause, : `ON DELETE CASCADE`{.sqlmysql}. |
: We should use a referential triggered action clause, : `ON DELETE CASCADE`{.sqlmysql}. |
|
... |
... |
Solution +.# |
2397 |
2456 |
|
|
2398 |
2457 |
Solution +.# |
Solution +.# |
2399 |
2458 |
~ |
~ |
2400 |
|
|
|
|
2459 |
|
We could use the following: |
|
2460 |
|
|
2401 |
2461 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
2402 |
2462 |
SELECT Name, Address |
SELECT Name, Address |
2403 |
2463 |
FROM TOURIST |
FROM TOURIST |
|
... |
... |
Solution +.# |
2406 |
2466 |
|
|
2407 |
2467 |
Solution +.# |
Solution +.# |
2408 |
2468 |
|
|
2409 |
|
: It selects all the attributes. |
|
|
2469 |
|
: It selects all the attributes, it is a wildcard. |
2410 |
2470 |
|
|
2411 |
2471 |
Solution +.# |
Solution +.# |
2412 |
2472 |
|
|
|
... |
... |
Solution +.# |
2426 |
2486 |
|
|
2427 |
2487 |
Solution +.# |
Solution +.# |
2428 |
2488 |
|
|
2429 |
|
: You can't have the `WHERE`{.sqlmysql} before `FROM`{.sqlmysql}. |
|
|
2489 |
|
: You cannot have the `WHERE`{.sqlmysql} before `FROM`{.sqlmysql}. |
2430 |
2490 |
|
|
2431 |
2491 |
Solution +.# |
Solution +.# |
2432 |
2492 |
|
|
|
... |
... |
Solution +.# |
2447 |
2507 |
|
|
2448 |
2508 |
Solution +.# |
Solution +.# |
2449 |
2509 |
~ |
~ |
|
2510 |
|
We could use the following: |
2450 |
2511 |
|
|
2451 |
2512 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
2452 |
2513 |
UPDATE PROF SET Name = 'Hugo Pernot' |
UPDATE PROF SET Name = 'Hugo Pernot' |
|
... |
... |
Solution +.# |
2455 |
2516 |
|
|
2456 |
2517 |
Solution +.# |
Solution +.# |
2457 |
2518 |
|
|
2458 |
|
: Yes, we could update more than one tuple at a time. |
|
|
2519 |
|
: Yes, we can have select condition that does not use primary key. In that case, it could be the case that we update more than one tuple with such a command (which is not necessarily a bad thing). |
2459 |
2520 |
|
|
2460 |
2521 |
Solution +.# |
Solution +.# |
2461 |
2522 |
|
|
|
... |
... |
Problem (TRAIN table and more advanced SQL coding) +.#train |
2802 |
2863 |
@problem:train -- Question -.# |
@problem:train -- Question -.# |
2803 |
2864 |
~ |
~ |
2804 |
2865 |
For each of the following questions, write a `SELECT` statement that would answer it: |
For each of the following questions, write a `SELECT` statement that would answer it: |
|
2866 |
|
|
2805 |
2867 |
#. "What are the identification numbers of the trains?" |
#. "What are the identification numbers of the trains?" |
2806 |
2868 |
#. "What are the names of the conductors with a "Senior" experience level?" |
#. "What are the names of the conductors with a "Senior" experience level?" |
2807 |
2869 |
#. "What are the construction years of the "Surfliner" and "Regina" models that we have?" |
#. "What are the construction years of the "Surfliner" and "Regina" models that we have?" |
|
... |
... |
You can use **COFFEE**.1 to denote the first tuple (or row) in **COFFEE**, and s |
2888 |
2950 |
@problem:coffee -- Question -.# |
@problem:coffee -- Question -.# |
2889 |
2951 |
~ |
~ |
2890 |
2952 |
|
|
2891 |
|
Assuming that the referential triggered action clause `ON UPDATE CASCADE` is used for every foreign keys in this database, list the tuples modified by the following statements: |
|
|
2953 |
|
Assuming that the referential triggered action clause `ON UPDATE CASCADE`{.sglmysql} is used for every foreign keys in this database, list the tuples modified by the following statements: |
2892 |
2954 |
|
|
2893 |
2955 |
~~~{.sqlmysql .numberLines} |
~~~{.sqlmysql .numberLines} |
2894 |
2956 |
UPDATE CUSTOMER SET FavCoffee = 001 |
UPDATE CUSTOMER SET FavCoffee = 001 |