File notes/00_sum.md changed (mode: 100644) (index d631e60..f64e5b3) |
... |
... |
marks the separation between two lectures. |
44 |
44 |
|
|
45 |
45 |
The syllabus is at <http://spots.augusta.edu/caubert/db/>. |
The syllabus is at <http://spots.augusta.edu/caubert/db/>. |
46 |
46 |
|
|
|
47 |
|
All section starts with a list of ressources and ends with exercises, solution to the exercises, and problems. |
|
48 |
|
|
47 |
49 |
|
|
48 |
50 |
# Introduction |
# Introduction |
49 |
51 |
|
|
50 |
|
**Ressources:** |
|
|
52 |
|
## Ressources |
51 |
53 |
|
|
52 |
|
[@Textbook6, ch. 1.1--1.6]. |
|
|
54 |
|
[@Textbook6, ch. 1.1--1.6] |
53 |
55 |
|
|
54 |
56 |
[@Textbook7] |
[@Textbook7] |
55 |
57 |
|
|
56 |
|
<https://www.1keydata.com/datawarehousing/data-modeling-levels.html> |
|
57 |
|
|
|
58 |
58 |
|
|
59 |
59 |
## Database |
## Database |
60 |
60 |
|
|
|
... |
... |
But need to be carefull about **consistency** / **referential integrity**. |
167 |
167 |
Column Data Types ✔ |
Column Data Types ✔ |
168 |
168 |
---------------------- ------------ --------- ---------- |
---------------------- ------------ --------- ---------- |
169 |
169 |
|
|
170 |
|
<https://www.1keydata.com/datawarehousing/data-modeling-levels.html> |
|
171 |
|
|
|
172 |
170 |
|
|
173 |
171 |
Gradation, from really abstract specification that is easy to modify, to more solidified description of what needs to be coded. |
Gradation, from really abstract specification that is easy to modify, to more solidified description of what needs to be coded. |
174 |
172 |
We'll see when we'll study high-level models what that means. |
We'll see when we'll study high-level models what that means. |
|
... |
... |
b. Data-abstraction: DBMS provides a conceptual representation, and hides implem |
183 |
181 |
c. Support of Multiple Viems of the Data: view = subset of the database or virtual data. |
c. Support of Multiple Viems of the Data: view = subset of the database or virtual data. |
184 |
182 |
d. Sharing and Multiuser Transaction Processing: concurrency control using transactions (= series of instructions that is supposed to execute a logically correct database access if executed in its entirety. Isolation, atomicity (all or nothing). |
d. Sharing and Multiuser Transaction Processing: concurrency control using transactions (= series of instructions that is supposed to execute a logically correct database access if executed in its entirety. Isolation, atomicity (all or nothing). |
185 |
183 |
|
|
186 |
|
|
|
187 |
|
Next time ⇒ Chapter 3, Relational Data Model and Relational Database Constraints. |
|
188 |
|
Mathematical relations, set-theory, first-order predicate logic! |
|
189 |
|
|
|
190 |
184 |
## Exercises |
## Exercises |
191 |
185 |
|
|
192 |
186 |
Exercise +.# |
Exercise +.# |
|
... |
... |
Question -.# |
294 |
288 |
|
|
295 |
289 |
# The Relational Data Model and Relational Database Constraints |
# The Relational Data Model and Relational Database Constraints |
296 |
290 |
|
|
297 |
|
**Ressources:** |
|
298 |
|
|
|
299 |
|
- Textbook: Ch. 3 |
|
300 |
|
- <https://en.wikipedia.org/wiki/Relational_model> |
|
301 |
|
|
|
302 |
|
|
|
303 |
|
**Last Time:** |
|
304 |
|
Example (miniworld = University), to introduce vocabulary, utility of having multiple tables / files, describe possible interactions, highlight organization and how to conceive a DB (Specifications, Conceptual, Logical and Physical designs). Also to mention some of the features of DBMS (define / construct / manipulate / share), and characteristics of the DB approach. |
|
|
291 |
|
## Ressources |
305 |
292 |
|
|
|
293 |
|
[@Textbook6, ch. 3]. |
|
294 |
|
The [wikipedia page for Relational model](https://en.wikipedia.org/wiki/Relational_model) and the [category "Relational database management systems"](https://en.wikipedia.org/wiki/Category:Relational_database_management_systems). |
306 |
295 |
|
|
|
296 |
|
Next time ⇒ Chapter 3, Relational Data Model and Relational Database Constraints. |
|
297 |
|
Mathematical relations, set-theory, first-order predicate logic! |
307 |
298 |
|
|
308 |
299 |
## Concepts |
## Concepts |
309 |
300 |
|
|
|
... |
... |
Relational data model: |
316 |
307 |
|
|
317 |
308 |
### Domains, Attributes, Tuples and Relations |
### Domains, Attributes, Tuples and Relations |
318 |
309 |
|
|
319 |
|
- **Domain** (or type) = set of atomic (as far as the relation is concerned) values. Can be given in the form of a data type, can be named and carry a logical definition (i.e., List_of_major as an enumerated data type, instead of just `String`). |
|
|
310 |
|
- **Domain** (or type) = set of atomic (as far as the relation is concerned) values. Can be given in the form of a data type, can be named and carry a logical definition (i.e., `List_of_major` as an enumerated data type, instead of just `String`). |
320 |
311 |
- **Attribute** = Attribute name + attribute domain (but we'll just write the name). |
- **Attribute** = Attribute name + attribute domain (but we'll just write the name). |
321 |
|
- **Relation Schema** (or scheme) = description of a relation. RELATION_NAME(Attribute$_1$, ..., Attribute$_n$), where $n$ is the degre (arity) of the relation, and the domain of Attribute$_i$ is written dom(Attribute$_i$). |
|
|
312 |
|
- **Relation Schema** (or scheme) = description of a relation. `RELATION_NAME(Attribute$_1$, ..., Attribute$_n$)`, where $n$ is the degre (arity) of the relation, and the domain of Attribute$_i$ is written dom(Attribute$_i$). |
322 |
313 |
- **Tuple** t of the schema R(A$_1$, ..., A$_n$) is an ordered list of values <v$_1$, ..., v$_n$> where v$_i$ is in dom(A$_i$) or a special `NULL` value. |
- **Tuple** t of the schema R(A$_1$, ..., A$_n$) is an ordered list of values <v$_1$, ..., v$_n$> where v$_i$ is in dom(A$_i$) or a special `NULL` value. |
323 |
314 |
- **Relation** (or relation state) r of the schema R(A$_1$, ..., A_$n$), also written r(R), is the set of n-tuples {t$_1$, ..., t$_m$} where each t$_i$ is a tuple of the schema R(A$_1$, ..., A$_n$). |
- **Relation** (or relation state) r of the schema R(A$_1$, ..., A_$n$), also written r(R), is the set of n-tuples {t$_1$, ..., t$_m$} where each t$_i$ is a tuple of the schema R(A$_1$, ..., A$_n$). |
324 |
315 |
|
|
|
... |
... |
Relational data model: |
346 |
337 |
|
|
347 |
338 |
--- |
--- |
348 |
339 |
|
|
349 |
|
|
|
350 |
|
**Last Time:** |
|
351 |
|
Relational model, vocabulary (domain, attribute, tuple, relation, relation scheme, atomic value |
|
352 |
|
|
|
353 |
340 |
## Constraints |
## Constraints |
354 |
341 |
|
|
355 |
342 |
### Types of Constraints |
### Types of Constraints |
|
... |
... |
Transaction = series of retrievals and updates performed by an application progr |
444 |
431 |
|
|
445 |
432 |
a., b. and c. refers to the "remedies", on the next page. |
a., b. and c. refers to the "remedies", on the next page. |
446 |
433 |
|
|
447 |
|
#### Insert: |
|
|
434 |
|
#### Insert |
448 |
435 |
|
|
449 |
436 |
`Insert <109920, Honda, Accord, 2012> into CAR.` |
`Insert <109920, Honda, Accord, 2012> into CAR.` |
450 |
437 |
|
|
|
... |
... |
a. Reject (restrict) |
482 |
469 |
b. Cascade (propagate) |
b. Cascade (propagate) |
483 |
470 |
c. Set default, or set `NULL` |
c. Set default, or set `NULL` |
484 |
471 |
|
|
485 |
|
|
|
486 |
|
Conclusion: The Relational Data Model and Relational Database Constraints: Vocabulary, Relations to model everything, difference between data and meta-data, constraints. |
|
487 |
|
|
|
488 |
472 |
## Exercises |
## Exercises |
489 |
473 |
|
|
490 |
474 |
Exercise +.# |
Exercise +.# |
|
... |
... |
TICKETS(\underline{ShowTimeID}, Price) |
639 |
623 |
|
|
640 |
624 |
# SQL |
# SQL |
641 |
625 |
|
|
642 |
|
- Textbook: Ch. 4, 5 |
|
643 |
|
- On-line ressources: cf. homework + web-page |
|
|
626 |
|
## References |
|
627 |
|
|
|
628 |
|
[@Textbook6, ch. 4--5] describes `SQL`, not one of its implementation. |
|
629 |
|
Cf. #problem:installation for a list of useful links. |
644 |
630 |
|
|
645 |
631 |
|
|
646 |
632 |
## Actors |
## Actors |
|
... |
... |
TICKETS(\underline{ShowTimeID}, Price) |
648 |
634 |
### Technologies |
### Technologies |
649 |
635 |
|
|
650 |
636 |
- There are other models: Document, graph, and key-value models. "NoSQL" data-model, more flexible, but only defined by opposition. |
- There are other models: Document, graph, and key-value models. "NoSQL" data-model, more flexible, but only defined by opposition. |
651 |
|
- Most commons DBMS are relational database management system (RDBMS), some are multi-model DBMS. |
|
652 |
|
- Oracle |
|
653 |
|
- MySQL (MariaDB is a community-developed fork, used by Google, Mozilla and Wikimedia Foundation) |
|
654 |
|
- Microsoft SQL Server |
|
655 |
|
- PostgreSQL |
|
|
637 |
|
- Most commons DBMS are relational database management system (RDBMS): |
|
638 |
|
- [Oracle Database](http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html) |
|
639 |
|
- [MySQL](https://www.mysql.com/) and its fork, [MariaDB](https://mariadb.org/) |
|
640 |
|
- [Microsoft SQL Server](https://www.microsoft.com/en-us/sql-server/sql-server-2017) |
|
641 |
|
- [PostgreSQL](https://www.postgresql.org/) |
656 |
642 |
- IBM DB2 |
- IBM DB2 |
657 |
643 |
- Microsoft Access |
- Microsoft Access |
658 |
644 |
- SQLite |
- SQLite |
659 |
|
Most of them supports semi-structured data, i.e., other models. |
|
|
645 |
|
|
|
646 |
|
Most of them supports semi-structured data, i.e., models that are not strictly speaking relational, some are "multi-model DBMS". |
|
647 |
|
To compare their features, refer to <https://en.wikipedia.org/wiki/Comparison_of_relational_database_management_systems>. |
|
648 |
|
To compare their popularity, refer to <https://db-engines.com/en/ranking> or <https://www.eversql.com/most-popular-databases-in-2018-according-to-stackoverflow-survey/> (if you sum up MySQL and MariaDB, they are first in both). |
660 |
649 |
- Structured Query Language is **the** language for RDBMS, it is made of 4 sublanguages: |
- Structured Query Language is **the** language for RDBMS, it is made of 4 sublanguages: |
661 |
650 |
- **D**ata **Q**uery **L**anguage, |
- **D**ata **Q**uery **L**anguage, |
662 |
651 |
- **D**ata **D**efinition **L**anguage (schema creation and modification), |
- **D**ata **D**efinition **L**anguage (schema creation and modification), |
|
... |
... |
The three last sublanguages being dubbed "**D**ata **M**anipulation **L**anguage |
694 |
683 |
- SQL is case-insensitive, doesn't care about spaces and new lines |
- SQL is case-insensitive, doesn't care about spaces and new lines |
695 |
684 |
- Comments are with `--` or `/* ...*/` |
- Comments are with `--` or `/* ...*/` |
696 |
685 |
- Every statement ends with a `;` |
- Every statement ends with a `;` |
697 |
|
- Syntax is in Homework #2 |
|
|
686 |
|
- Syntax is in @problem:sqldoc. |
698 |
687 |
- Reserved words: <https://dev.mysql.com/doc/refman/5.7/en/keywords.html>, <https://mariadb.com/kb/en/library/reserved-words/> |
- Reserved words: <https://dev.mysql.com/doc/refman/5.7/en/keywords.html>, <https://mariadb.com/kb/en/library/reserved-words/> |
699 |
688 |
- We will stick to what's in MariaDB and MySQL here (no domain, limited data type definition) |
- We will stick to what's in MariaDB and MySQL here (no domain, limited data type definition) |
700 |
689 |
|
|
|
... |
... |
The three last sublanguages being dubbed "**D**ata **M**anipulation **L**anguage |
704 |
693 |
~~~{.sql} |
~~~{.sql} |
705 |
694 |
CREATE SCHEMA HW_FACULTY; |
CREATE SCHEMA HW_FACULTY; |
706 |
695 |
|
|
707 |
|
|
|
708 |
696 |
/* Or |
/* Or |
709 |
697 |
CREATE DATABASE HW_FACUTLY; |
CREATE DATABASE HW_FACUTLY; |
710 |
698 |
*/ |
*/ |
|
... |
... |
INSERT INTO PROF VALUES ( |
741 |
729 |
); |
); |
742 |
730 |
~~~ |
~~~ |
743 |
731 |
|
|
744 |
|
--- |
|
745 |
|
|
|
746 |
|
**Ressources:** |
|
747 |
|
|
|
748 |
|
- Textbook: Ch. 4, 5 (but warning, describe `SQL`, not one of its implementation) |
|
749 |
|
- On-line ressources: cf. homework + web-page |
|
750 |
|
|
|
751 |
|
Note: Use `DESCRIBE <TableName>` and `SELECT * FROM <TableName>` intensively to see where you are. |
|
|
732 |
|
Use `DESCRIBE <TableName>` and `SELECT * FROM <TableName>` intensively to see where you are. |
752 |
733 |
Use `DROP TABLE <TableName>` and `DROP SCHEMA <SchemaName>` for a "fresh start". |
Use `DROP TABLE <TableName>` and `DROP SCHEMA <SchemaName>` for a "fresh start". |
753 |
734 |
|
|
|
735 |
|
--- |
|
736 |
|
|
754 |
737 |
## Overview of Constraints |
## Overview of Constraints |
755 |
738 |
|
|
756 |
739 |
a. Primary Key |
a. Primary Key |
|
... |
... |
SELECT * FROM Table_set_null; |
1000 |
983 |
|
|
1001 |
984 |
--- |
--- |
1002 |
985 |
|
|
1003 |
|
Note: Use `DESCRIBE <Table>;`, `SHOW TABLES;` and `SELECT * FROM <Table>;` intensively to see where you are. |
|
1004 |
|
|
|
1005 |
|
|
|
1006 |
986 |
### Constructing and Populating a New Example |
### Constructing and Populating a New Example |
1007 |
987 |
|
|
1008 |
988 |
#### Construction |
#### Construction |
|
... |
... |
INSERT INTO STUDENT (Login, Name, Registered, Major) VALUES |
1082 |
1062 |
|
|
1083 |
1063 |
Note the date litterals. |
Note the date litterals. |
1084 |
1064 |
|
|
1085 |
|
(Small comment about MySQL / MariaDB difference |
|
|
1065 |
|
#### A Note on the Differences Between MySQL and MariaDB {#differenceMySQLMariaDB} |
1086 |
1066 |
|
|
1087 |
1067 |
- MySQL is completely case-insensitive (reserved words, tables, attributes), MariaDB isn't (case for tables matter). |
- MySQL is completely case-insensitive (reserved words, tables, attributes), MariaDB isn't (case for tables matter). |
1088 |
1068 |
- MySQL will always notify you if there is an error in a date attribute |
- MySQL will always notify you if there is an error in a date attribute |
1089 |
1069 |
|
|
1090 |
|
) |
|
|
1070 |
|
Refer to <https://mariadb.com/kb/en/library/mariadb-vs-mysql-features/> and <https://mariadb.com/kb/en/library/mariadb-vs-mysql-compatibility/> for a full list. |
|
1071 |
|
|
1091 |
1072 |
|
|
1092 |
1073 |
## A First Look at Conditions |
## A First Look at Conditions |
1093 |
1074 |
|
|
1094 |
|
Order of clause has no importance. |
|
|
1075 |
|
Order of clauses does not matter, not even for optimization purpose. |
|
1076 |
|
|
1095 |
1077 |
|
|
1096 |
1078 |
~~~{.sql} |
~~~{.sql} |
1097 |
1079 |
UPDATE <table> |
UPDATE <table> |
|
... |
... |
Conditions can |
1110 |
1092 |
- `condition1 OR condition2` |
- `condition1 OR condition2` |
1111 |
1093 |
- `NOT condition` |
- `NOT condition` |
1112 |
1094 |
- be trivial / empty |
- be trivial / empty |
1113 |
|
- use regular expressions (escape character is `\`.) |
|
|
1095 |
|
- use regular expressions (escape character is `\`). |
1114 |
1096 |
|
|
1115 |
1097 |
~~~{.sql} |
~~~{.sql} |
1116 |
1098 |
UPDATE Department SET Head = 'aturing' WHERE Code = 'MATH'; |
UPDATE Department SET Head = 'aturing' WHERE Code = 'MATH'; |
|
... |
... |
SELECT Name FROM PROF WHENE Login LIKE '_aubert'; |
1132 |
1114 |
|
|
1133 |
1115 |
--- |
--- |
1134 |
1116 |
|
|
1135 |
|
|
|
1136 |
|
1. Various Tools |
|
1137 |
|
a. DISTINCT / ALL and UNION (4.3.4) |
|
1138 |
|
b. ORDER BY (4.3.6) |
|
1139 |
|
c. Aggregate Functions (5.1.7) |
|
1140 |
|
d. Aliases for Columns |
|
1141 |
|
|
|
1142 |
|
2. Three-Valued Logic (5.1.1) |
|
1143 |
|
a. Meaning of `NULL` |
|
1144 |
|
b. Comparisons with unknown values |
|
1145 |
|
|
|
1146 |
|
3. More Select Queries |
|
1147 |
|
a. Select-project-join (4.3.1) |
|
1148 |
|
b. Aliases (4.3.2) |
|
1149 |
|
c. Nested Queries (5.1.2) |
|
1150 |
|
|
|
1151 |
|
|
|
1152 |
|
|
|
1153 |
1117 |
## Various Tools |
## Various Tools |
1154 |
1118 |
|
|
|
1119 |
|
For `DISTINCT`, `ALL` and `UNION`, cf. [@Textbook6, 4.3.4]. For `ORDER BY` , cf. [@Textbook6, 4.3.6]. |
|
1120 |
|
For aggregate functions, cf. [@Textbook6, 5.1.7]. |
|
1121 |
|
|
1155 |
1122 |
### DISTINCT / ALL and Union |
### DISTINCT / ALL and Union |
1156 |
1123 |
|
|
1157 |
1124 |
Note: SQL treats tables as multi-set, there can be repetitions in the tables. |
Note: SQL treats tables as multi-set, there can be repetitions in the tables. |
|
... |
... |
The default behaviour is `ALL`, but we can declare it explicitely. |
1168 |
1135 |
|
|
1169 |
1136 |
There is also `INTERSECT` and `EXCEPT` in the specification, but MariaDB and MySQL do not implement them. |
There is also `INTERSECT` and `EXCEPT` in the specification, but MariaDB and MySQL do not implement them. |
1170 |
1137 |
|
|
1171 |
|
|
|
1172 |
1138 |
### ORDER BY |
### ORDER BY |
1173 |
1139 |
|
|
1174 |
1140 |
You can have `ORDER BY` specifications: |
You can have `ORDER BY` specifications: |
|
... |
... |
SELECT Login AS Username FROM PROF; |
1201 |
1167 |
|
|
1202 |
1168 |
## Three-Valued Logic |
## Three-Valued Logic |
1203 |
1169 |
|
|
|
1170 |
|
Cf. [@Textbook6,5.1.1] |
|
1171 |
|
|
1204 |
1172 |
### Meaning of `NULL` |
### Meaning of `NULL` |
1205 |
1173 |
|
|
1206 |
1174 |
`NULL` is |
`NULL` is |
|
... |
... |
You can test if a value is `NULL` with `IS NULL`. |
1240 |
1208 |
|
|
1241 |
1209 |
## More Select Queries |
## More Select Queries |
1242 |
1210 |
|
|
|
1211 |
|
For select-project-join, cf. [@Textbook6, 4.3.1]. |
|
1212 |
|
For aliases, cf. [@Textbook6, 4.3.2]. |
|
1213 |
|
For nested queries, cf. [@Textbook6, 5.1.2]. |
|
1214 |
|
|
1243 |
1215 |
### Select-project-join (4.3.1) |
### Select-project-join (4.3.1) |
1244 |
1216 |
|
|
1245 |
1217 |
~~~{.sql} |
~~~{.sql} |
|
... |
... |
WHERE DEPARTMENT IN ( SELECT Major |
1302 |
1274 |
WHERE Login LIKE '%a'); |
WHERE Login LIKE '%a'); |
1303 |
1275 |
~~~ |
~~~ |
1304 |
1276 |
|
|
1305 |
|
Answer to questions: |
|
1306 |
|
|
|
1307 |
|
- Order of clause does not matter, not even for optimization purpose. |
|
1308 |
|
- `SELECT COUNT(DISTINCT Name) FROM STUDENT;` |
|
1309 |
|
- About Semantics / Explicit / Implicit Constraints, your textbook reads, pp. 67 - 69: |
|
1310 |
|
1. Constraints that are inherent in the data model. We call these inherent model-based constraints or implicit constraints. |
|
1311 |
|
2. 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. |
|
1312 |
|
3. 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. |
|
1313 |
|
|
|
1314 |
|
Check is indeed explicit, in that respect, since it can be specified in the data model. |
|
1315 |
|
But it's a terrible example, since Check does nothing, and has to be simulated with triggers or so (hence becoming a semantics constraints). |
|
1316 |
|
|
|
1317 |
|
- You can `CREATE` views <https://dev.mysql.com/doc/refman/5.7/en/create-view.html> |
|
1318 |
|
|
|
1319 |
|
|
|
1320 |
|
|
|
1321 |
|
3. More Select Queries |
|
1322 |
|
a. Select-project-join (4.3.1) |
|
1323 |
|
b. Aliases (4.3.2) |
|
1324 |
|
c. Nested Queries (5.1.2) |
|
1325 |
|
|
|
1326 |
|
|
|
1327 |
1277 |
|
|
1328 |
1278 |
## More Select Queries |
## More Select Queries |
1329 |
1279 |
|
|
|
... |
... |
Exercise +.# |
1408 |
1358 |
|
|
1409 |
1359 |
Data Type | Examples | |
Data Type | Examples | |
1410 |
1360 |
--- | --- | |
--- | --- | |
1411 |
|
**?** | $4$, $-32$ |
|
1412 |
|
Char(4) | **?** |
|
|
1361 |
|
| $4$, $-32$ |
|
1362 |
|
Char(4) | |
1413 |
1363 |
VarChar(10) | 'Train', 'Michelle' |
VarChar(10) | 'Train', 'Michelle' |
1414 |
|
Bit(4) | **?** |
|
1415 |
|
**?** | TRUE, UNKNOWN |
|
|
1364 |
|
Bit(4) | |
|
1365 |
|
| TRUE, UNKNOWN |
1416 |
1366 |
|
|
1417 |
1367 |
Exercise +.# |
Exercise +.# |
1418 |
1368 |
~ Explain what the following `SQL` statement does |
~ Explain what the following `SQL` statement does |
|
... |
... |
Exercise +.# |
1579 |
1529 |
~ Write a query that returns the number of row (i.e., of entries, of tuples) in a table named `BOOK`. |
~ Write a query that returns the number of row (i.e., of entries, of tuples) in a table named `BOOK`. |
1580 |
1530 |
|
|
1581 |
1531 |
Exercise +.# |
Exercise +.# |
1582 |
|
~ |
|
1583 |
|
Consider the following `SQL` code: |
|
|
1532 |
|
~ Consider the following `SQL` code: |
1584 |
1533 |
|
|
1585 |
|
~~~{.sqlmysql} |
|
|
1534 |
|
~~~{.sqlmysql} |
1586 |
1535 |
CREATE TABLE COMPUTER( |
CREATE TABLE COMPUTER( |
1587 |
1536 |
Id VARCHAR(20) PRIMARY KEY, |
Id VARCHAR(20) PRIMARY KEY, |
1588 |
1537 |
Model VARCHAR(20) |
Model VARCHAR(20) |
|
... |
... |
INSERT INTO CONNEXION VALUES |
1620 |
1569 |
('C', '14'); |
('C', '14'); |
1621 |
1570 |
~~~ |
~~~ |
1622 |
1571 |
|
|
1623 |
|
Write a query that returns … (in parenthesis, the values returned \emph{in this set-up}, but you have to be general) |
|
|
1572 |
|
Write a query that returns … (in parenthesis, the values returned *in this set-up*, but you have to be general) |
1624 |
1573 |
|
|
1625 |
1574 |
#. … the number of computers connected to the printer whose id is '13' (i.e., \(2\) ). |
#. … the number of computers connected to the printer whose id is '13' (i.e., \(2\) ). |
1626 |
1575 |
#. … the number of different models of printers (i.e., \(2\)). |
#. … the number of different models of printers (i.e., \(2\)). |
|
... |
... |
Exercise +.# |
1636 |
1585 |
Exercise +.# |
Exercise +.# |
1637 |
1586 |
~ Write a query that removes the default value for a `Pages` attribute in a `BOOK` table. |
~ Write a query that removes the default value for a `Pages` attribute in a `BOOK` table. |
1638 |
1587 |
|
|
|
1588 |
|
**Bogue, some kind of duplicate?** |
|
1589 |
|
|
|
1590 |
|
#### Exercises |
|
1591 |
|
|
|
1592 |
|
Question -.# |
|
1593 |
|
~ Have a look at the formal definition of the |
|
1594 |
|
|
|
1595 |
|
~~~{.sqlmysql} |
|
1596 |
|
ALTER |
|
1597 |
|
~~~ |
|
1598 |
|
|
|
1599 |
|
command, at <https://dev.mysql.com/doc/refman/5.7/en/alter-table.html> or <https://mariadb.com/kb/en/library/alter-table/>. |
|
1600 |
|
|
|
1601 |
|
Question -.# |
|
1602 |
|
~ Draw the relations corresponding to that database, including the domains and primary, as well as foreign, keys. |
|
1603 |
|
|
|
1604 |
|
Question -.# |
|
1605 |
|
~ Write a |
|
1606 |
|
|
|
1607 |
|
~~~{.sqlmysql} |
|
1608 |
|
SELECT |
|
1609 |
|
~~~ |
|
1610 |
|
|
|
1611 |
|
statement that returns the `Id` number of the person whose first name is "Samantha". |
|
1612 |
|
|
|
1613 |
|
Question -.# |
|
1614 |
|
~ Write a statement that violate the entity integrity constraint. What is the error message returned? |
|
1615 |
|
|
|
1616 |
|
Question -.# |
|
1617 |
|
~ Execute an `UPDATE` statement that violate the referential integrity constraint. What is the error message returned? |
|
1618 |
|
|
|
1619 |
|
Question -.# |
|
1620 |
|
~ Write a statement that violate another kind of constraint. Explain what constraint you are violating, and explain the error message. |
|
1621 |
|
|
|
1622 |
|
Solutions in comment. |
|
1623 |
|
|
|
1624 |
|
<!-- |
|
1625 |
|
SELECT Id FROM NAME WHERE FName = 'Samantha'; |
|
1626 |
|
% ERROR 1062 (23000): Duplicate entry '80' for key 'PRIMARY' |
|
1627 |
|
% ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`HW_2Q3`.`ADDRESS`, CONSTRAINT `fk_id` FOREIGN KEY (`Habitants`) REFERENCES `name` (`Id`)) |
|
1628 |
|
% ERROR 1136 (21S01): Column count doesn't match value count at row 1 |
|
1629 |
|
--> |
|
1630 |
|
|
1639 |
1631 |
## Solution to Exercises |
## Solution to Exercises |
1640 |
1632 |
|
|
1641 |
1633 |
Solution +.# |
Solution +.# |
|
... |
... |
Solution +.# |
1720 |
1712 |
Solution +.# |
Solution +.# |
1721 |
1713 |
~ All will print the duplicate, distinct will eliminate them. |
~ All will print the duplicate, distinct will eliminate them. |
1722 |
1714 |
|
|
1723 |
|
|
|
1724 |
|
|
|
1725 |
1715 |
Solution +.# |
Solution +.# |
1726 |
1716 |
~ It selects all the attributes. |
~ It selects all the attributes. |
1727 |
1717 |
|
|
|
... |
... |
Problem -.+.#installation |
1795 |
1785 |
|
|
1796 |
1786 |
In this problem, you will install the [MySQL](https://www.mysql.com/) DataBase Managment System and set it up. |
In this problem, you will install the [MySQL](https://www.mysql.com/) DataBase Managment System and set it up. |
1797 |
1787 |
On top of being open source and free, it is one of the most widespread and used DBMS, as well as one of the most well-documented. |
On top of being open source and free, it is one of the most widespread and used DBMS, as well as one of the most well-documented. |
1798 |
|
Linux users will use its community-developed fork, \href{https://mariadb.org/}{MariaDB}, but that won't make a difference in this course. |
|
|
1788 |
|
Linux users will use its community-developed fork, \href{https://mariadb.org/}{MariaDB}, but that won't make a difference in this course (up to [what's mentioned in this section](#differenceMySQLMariaDB)). |
|
1789 |
|
|
|
1790 |
|
**bogue: cross ref?** |
1799 |
1791 |
|
|
1800 |
1792 |
It is very important that you follow the steps below carefully. |
It is very important that you follow the steps below carefully. |
1801 |
1793 |
Read the messages printed on your screen, make sure a step was correctly executed before moving to the next one. |
Read the messages printed on your screen, make sure a step was correctly executed before moving to the next one. |
|
... |
... |
whereas single quotes are surrounding `testuser` and `localhost` (normally, copy |
1903 |
1895 |
|
|
1904 |
1896 |
We now log as the normal user called "testuser". |
We now log as the normal user called "testuser". |
1905 |
1897 |
|
|
1906 |
|
Linux users should type *as a normal user, i.e., not as root*, in their terminal the following, and Windows users should type in their command prompt the following (provided working directory is still |
|
1907 |
|
|
|
1908 |
|
~~~{.bash} |
|
1909 |
|
C:\Program Files\MySQL\MySQL Server 5.7\bin |
|
1910 |
|
~~~ |
|
1911 |
|
|
|
1912 |
|
: |
|
|
1898 |
|
Linux users should type *as a normal user, i.e., not as root*, in their terminal the following, and Windows users should type in their command prompt the following^[Provided the working directory is still `C:\Program Files\MySQL\MySQL Server 5.7\bin`. Cf. <https://dev.mysql.com/doc/mysql-windows-excerpt/5.7/en/mysql-installation-windows-path.html> to add the MySQL bin directory to your Windows system `PATH` environment variable.]: |
1913 |
1899 |
|
|
1914 |
1900 |
~~~{.bash} |
~~~{.bash} |
1915 |
1901 |
mysql -u testuser -p |
mysql -u testuser -p |
|
... |
... |
You're all set! All you have to do is to quit, using the command |
1951 |
1937 |
EXIT; |
EXIT; |
1952 |
1938 |
~~~ |
~~~ |
1953 |
1939 |
|
|
1954 |
|
**Add somewhere link to <https://dev.mysql.com/doc/mysql-windows-excerpt/5.7/en/mysql-installation-windows-path.html>** |
|
1955 |
|
|
|
1956 |
1940 |
### Creating, Listind and Dropping Databases |
### Creating, Listind and Dropping Databases |
1957 |
1941 |
|
|
1958 |
|
Problem-.+.#creatingSchema |
|
|
1942 |
|
Problem -.+.#creatingSchema |
1959 |
1943 |
|
|
1960 |
1944 |
*This exercise assume you successfully completed @problem:installation.* |
*This exercise assume you successfully completed @problem:installation.* |
1961 |
1945 |
|
|
|
... |
... |
In the future, we will refer to the commands 1 and 2 as "log-in as `testuser` an |
2008 |
1992 |
|
|
2009 |
1993 |
### Reading the Documentation |
### Reading the Documentation |
2010 |
1994 |
|
|
|
1995 |
|
Problem -.+.#sqldoc |
|
1996 |
|
|
2011 |
1997 |
The goal of this problem is to learn where to find the documentation of your DBMS, and to have a first look at the syntax of SQL commands. |
The goal of this problem is to learn where to find the documentation of your DBMS, and to have a first look at the syntax of SQL commands. |
2012 |
1998 |
|
|
2013 |
|
You can consult your textbook, Table~ 5.2, p. 140 (6th edition) or Table~ 7.2, p. 235 (7th edition), for a very quick summary of the most common commands. |
|
|
1999 |
|
You can consult your textbook, [@Textbook6, Table 5.2, p. 140] or [@Textbook7, Table 7.2, p. 235], for a very quick summary of the most common commands. |
2014 |
2000 |
Make sure you are familiar with the Backus–Naur form (BNF) notation commonly used: |
Make sure you are familiar with the Backus–Naur form (BNF) notation commonly used: |
2015 |
2001 |
|
|
2016 |
2002 |
- non-terminal symbols (i.e., variables, parameters) are enclosed in angled brackets, |
- non-terminal symbols (i.e., variables, parameters) are enclosed in angled brackets, |
|
... |
... |
and to examine carefully the message printed. You should read |
2121 |
2107 |
+------------+-------------+------+-----+---------+-------+ |
+------------+-------------+------+-----+---------+-------+ |
2122 |
2108 |
~~~ |
~~~ |
2123 |
2109 |
|
|
2124 |
|
If you believe there is a mistake, you can erase ("drop") the \emph{table} (not the whole database, as we did in @problem:creatingSchema) using |
|
|
2110 |
|
If you believe there is a mistake, you can erase ("drop") the *table* (not the whole database, as we did in @problem:creatingSchema) using |
2125 |
2111 |
|
|
2126 |
2112 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
2127 |
2113 |
DROP TABLE ADDRESS; |
DROP TABLE ADDRESS; |
|
... |
... |
And let's use our first update command: |
2179 |
2165 |
UPDATE ADDRESS SET Habitants = 3 WHERE Number = 120; |
UPDATE ADDRESS SET Habitants = 3 WHERE Number = 120; |
2180 |
2166 |
~~~ |
~~~ |
2181 |
2167 |
|
|
2182 |
|
#### Exercises |
|
2183 |
|
|
|
2184 |
|
Question -.# |
|
2185 |
|
~ Have a look at the formal definition of the |
|
2186 |
|
|
|
2187 |
|
~~~{.sqlmysql} |
|
2188 |
|
ALTER |
|
2189 |
|
~~~ |
|
2190 |
|
|
|
2191 |
|
command, at <https://dev.mysql.com/doc/refman/5.7/en/alter-table.html> or <https://mariadb.com/kb/en/library/alter-table/>. |
|
2192 |
|
|
|
2193 |
|
Question -.# |
|
2194 |
|
~ Draw the relations corresponding to that database, including the domains and primary, as well as foreign, keys. |
|
2195 |
|
|
|
2196 |
|
Question -.# |
|
2197 |
|
~ Write a |
|
2198 |
|
|
|
2199 |
|
~~~{.sqlmysql} |
|
2200 |
|
SELECT |
|
2201 |
|
~~~ |
|
2202 |
|
|
|
2203 |
|
statement that returns the |
|
2204 |
|
|
|
2205 |
|
~~~{.sqlmysql} |
|
2206 |
|
Id |
|
2207 |
|
~~~ |
|
2208 |
|
|
|
2209 |
|
number of the person whose first name is "Samantha". |
|
2210 |
|
|
|
2211 |
|
Question -.# |
|
2212 |
|
~ Write a statement that violate the entity integrity constraint. What is the error message returned? |
|
2213 |
|
|
|
2214 |
|
Question -.# |
|
2215 |
|
~ Execute an `UPDATE` statement that violate the referential integrity constraint. What is the error message returned? |
|
2216 |
|
|
|
2217 |
|
Question -.# |
|
2218 |
|
~ Write a statement that violate another kind of constraint. Explain what constraint you are violating, and explain the error message. |
|
2219 |
|
|
|
2220 |
|
Solutions in comment. |
|
2221 |
|
|
|
2222 |
|
<!-- |
|
2223 |
|
SELECT Id FROM NAME WHERE FName = 'Samantha'; |
|
2224 |
|
% ERROR 1062 (23000): Duplicate entry '80' for key 'PRIMARY' |
|
2225 |
|
% ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`HW_2Q3`.`ADDRESS`, CONSTRAINT `fk_id` FOREIGN KEY (`Habitants`) REFERENCES `name` (`Id`)) |
|
2226 |
|
% ERROR 1136 (21S01): Column count doesn't match value count at row 1 |
|
2227 |
|
--> |
|
2228 |
2168 |
|
|
2229 |
2169 |
### Repetiting Tuples in MySQL {#repetition} |
### Repetiting Tuples in MySQL {#repetition} |
2230 |
2170 |
|
|
|
... |
... |
Exercise from Textbook 7th Edition, 5.15, 5.16 |
2559 |
2499 |
|
|
2560 |
2500 |
# Design |
# Design |
2561 |
2501 |
|
|
|
2502 |
|
## Resources |
|
2503 |
|
|
|
2504 |
|
|
|
2505 |
|
- E.-R. models: Chapter 7 |
|
2506 |
|
- E.-R. to relational model: 9.1 |
|
2507 |
|
- Normalization: 15 |
|
2508 |
|
- UML: not so much in the textbook, but you can look at 7.8 and 10.3 |
|
2509 |
|
|
2562 |
2510 |
## Interest for High-Level Design |
## Interest for High-Level Design |
2563 |
2511 |
|
|
2564 |
2512 |
Show mistakes and limitations of previous relational models studies. |
Show mistakes and limitations of previous relational models studies. |
|
... |
... |
We will use multiple models: |
2581 |
2529 |
Column Data Types ✔ |
Column Data Types ✔ |
2582 |
2530 |
---------------------- ------------ --------- ---------- |
---------------------- ------------ --------- ---------- |
2583 |
2531 |
|
|
2584 |
|
<https://www.1keydata.com/datawarehousing/data-modeling-levels.html> |
|
2585 |
|
|
|
2586 |
2532 |
Remember that in Relational models, relations were representing entities and relationships, here the distinction is made in this table (entity vs relationship). |
Remember that in Relational models, relations were representing entities and relationships, here the distinction is made in this table (entity vs relationship). |
2587 |
2533 |
|
|
2588 |
2534 |
Remember that this model and Relational models are DBMS independant, and the CS is at the border between humans and computers. |
Remember that this model and Relational models are DBMS independant, and the CS is at the border between humans and computers. |
|
... |
... |
Take the time to introduce future topics + to give exam back. |
2605 |
2551 |
|
|
2606 |
2552 |
--- |
--- |
2607 |
2553 |
|
|
2608 |
|
**Textbook:** |
|
2609 |
|
|
|
2610 |
|
- E.-R. models: Chapter 7 |
|
2611 |
|
- E.-R. to relational model: 9.1 |
|
2612 |
|
- Normalization: 15 |
|
2613 |
|
- UML: not so much in the textbook, but you can look at 7.8 and 10.3 |
|
2614 |
2554 |
|
|
2615 |
2555 |
## Entity-Relationship (ER) model |
## Entity-Relationship (ER) model |
2616 |
2556 |
|
|
|
... |
... |
a. Foreign Key Approach: Chose one of the relation (preferably with total partic |
2845 |
2785 |
b. Merged Relation Approach: If both participations are total, just merge them. Primary key = just pick one, and add a `NOT NULL` constraint on the other. |
b. Merged Relation Approach: If both participations are total, just merge them. Primary key = just pick one, and add a `NOT NULL` constraint on the other. |
2846 |
2786 |
c. Cross-Reference or Relationship Relation Approach: Create a lookup table with two (or more!) foreign keys, pick one of them (or the one on the N side, or both if M:N, or all if N-ary) as the primary key. |
c. Cross-Reference or Relationship Relation Approach: Create a lookup table with two (or more!) foreign keys, pick one of them (or the one on the N side, or both if M:N, or all if N-ary) as the primary key. |
2847 |
2787 |
|
|
2848 |
|
+ Propagate option? Cascade, most of the time: weak entity type, lookup tables, etc. |
|
|
2788 |
|
**Bogue: + Propagate option? Cascade, most of the time: weak entity type, lookup tables, etc.** |
2849 |
2789 |
|
|
2850 |
2790 |
{ width=100% } |
{ width=100% } |
2851 |
2791 |
|
|
|
... |
... |
Key attribute | Primary key |
2868 |
2808 |
|
|
2869 |
2809 |
**Need to work on a better example, includings n-ary relationship, and propagate options.** |
**Need to work on a better example, includings n-ary relationship, and propagate options.** |
2870 |
2810 |
|
|
2871 |
|
**+ Propagate option? Cascade, most of the time: weak entity type, lookup tables, etc.** |
|
2872 |
|
|
|
2873 |
2811 |
|
|
2874 |
2812 |
## Guidelines and Normal Form |
## Guidelines and Normal Form |
2875 |
2813 |
|
|
|
... |
... |
Loosing information inadvertently |
2902 |
2840 |
|
|
2903 |
2841 |
Updated have to be consistent. |
Updated have to be consistent. |
2904 |
2842 |
|
|
2905 |
|
Example: |
|
2906 |
|
~~~ |
|
|
2843 |
|
(Bad!) Example: |
2907 |
2844 |
|
|
|
2845 |
|
~~~ |
2908 |
2846 |
---------- (Login, Name, AdvisoryName, AdvisorOffice, Major, MajorHead) |
---------- (Login, Name, AdvisoryName, AdvisorOffice, Major, MajorHead) |
2909 |
2847 |
|
|
2910 |
2848 |
-----------(Office, PhoneNumber, Building) |
-----------(Office, PhoneNumber, Building) |
|
... |
... |
CCL: every FD X → Y s.t. X is a proper subset of the primary key, or a non-pri |
3150 |
3088 |
One approach for analysis, design, implementation and deployment of databases and their applications. |
One approach for analysis, design, implementation and deployment of databases and their applications. |
3151 |
3089 |
Databases interact with multiple softwares and users, we need a common language. |
Databases interact with multiple softwares and users, we need a common language. |
3152 |
3090 |
|
|
3153 |
|
**U**nified **M**odeling **L**anguage (<http://uml.org>) is *a standard*: |
|
|
3091 |
|
[**U**nified **M**odeling **L**anguage](http://uml.org) is *a standard*: |
3154 |
3092 |
|
|
3155 |
3093 |
- Generic |
- Generic |
3156 |
3094 |
- Language-independent |
- Language-independent |
|
... |
... |
This last feature can be used for weak entities, but not only. |
3243 |
3181 |
|
|
3244 |
3182 |
Some of those subtleties depend on your need, and are subjective, but are important tool to design properly a database, and relieving the programmer from the burden of figuring out many details. |
Some of those subtleties depend on your need, and are subjective, but are important tool to design properly a database, and relieving the programmer from the burden of figuring out many details. |
3245 |
3183 |
|
|
3246 |
|
Sources: |
|
3247 |
|
|
|
3248 |
|
- <https://en.wikipedia.org/wiki/Unified_Modeling_Language> |
|
3249 |
|
- <https://creately.com/blog/diagrams/class-diagram-relationships/> |
|
3250 |
|
- Section 3.8 (7th Edition) or 7.8 (6th Edition) of your textbook. |
|
3251 |
|
|
|
3252 |
3184 |
**Example: cf. review session.** |
**Example: cf. review session.** |
3253 |
3185 |
|
|
3254 |
3186 |
|
|
|
... |
... |
Exercise +.# |
3450 |
3382 |
Exercise +.# |
Exercise +.# |
3451 |
3383 |
~ Consider the following diagram: |
~ Consider the following diagram: |
3452 |
3384 |
|
|
3453 |
|
\begin{tikzpicture}[scale = 0.8] |
|
|
3385 |
|
\begin{tikzpicture}[scale = 0.8] |
3454 |
3386 |
\begin{class}[text width=7cm]{Flight}{0,0} |
\begin{class}[text width=7cm]{Flight}{0,0} |
3455 |
3387 |
\attribute{flightNumber : Integer} |
\attribute{flightNumber : Integer} |
3456 |
3388 |
\attribute{departureTime : Date} |
\attribute{departureTime : Date} |
|
... |
... |
Your professor designed the following relational model at some point in his care |
3705 |
3637 |
|
|
3706 |
3638 |
Table Name and Attributes | Example of Value | |
Table Name and Attributes | Example of Value | |
3707 |
3639 |
--- | --- |
--- | --- |
3708 |
|
EXAM(\underline{Number, Date, Course) | < 1, '2018-02-14', 'CSCI3410'> |
|
3709 |
|
PROBLEM(\underline{Statement, Points, Length, Exam) | < 'Your professor designed…', 10, '00:10:00', 1> |
|
3710 |
|
STUDENT\_GRADE(\underline{Login, \underline{Exam, Grade) | < 'aalyx', 1, 83> |
|
|
3640 |
|
EXAM(\underline{Number}, Date, Course) | < 1, '2018-02-14', 'CSCI3410'> |
|
3641 |
|
PROBLEM(\underline{Statement}, Points, Length, Exam) | < 'Your professor designed…', 10, '00:10:00', 1> |
|
3642 |
|
STUDENT\_GRADE(\underline{Login}, \underline{Exam}, Grade) | < 'aalyx', 1, 83> |
3711 |
3643 |
|
|
3712 |
3644 |
The idea was to have |
The idea was to have |
3713 |
3645 |
|
|
|
... |
... |
Consider the ER schema for the MOVIES database: |
3729 |
3661 |
|
|
3730 |
3662 |
Assume that MOVIES is a populated database. |
Assume that MOVIES is a populated database. |
3731 |
3663 |
ACTOR is used as a gender-netral term. |
ACTOR is used as a gender-netral term. |
3732 |
|
Given the constraints shown in the ER schema, respond to the following statements with \emph{True} or \emph{False}. |
|
|
3664 |
|
Given the constraints shown in the ER schema, respond to the following statements with *True* or *False*. |
3733 |
3665 |
Justify each answer. |
Justify each answer. |
3734 |
3666 |
|
|
3735 |
3667 |
#. There are no actors in this database that have been in no movies. |
#. There are no actors in this database that have been in no movies. |
|
... |
... |
Problem +.# |
3792 |
3724 |
|
|
3793 |
3725 |
Consider the following E.R. schema: |
Consider the following E.R. schema: |
3794 |
3726 |
|
|
3795 |
|
\begin{figure} |
|
3796 |
3727 |
\begin{tikzpicture}[node distance=8em] |
\begin{tikzpicture}[node distance=8em] |
3797 |
3728 |
\node[entity] (COUNTRY) {COUNTRY}; |
\node[entity] (COUNTRY) {COUNTRY}; |
3798 |
3729 |
\node[attribute] (name) [left of=COUNTRY] {\key{Name}} edge (COUNTRY); |
\node[attribute] (name) [left of=COUNTRY] {\key{Name}} edge (COUNTRY); |
|
... |
... |
Based on the given primary key, is this relation in 1NF, 2NF, or 3NF? Why or why |
3853 |
3784 |
|
|
3854 |
3785 |
|
|
3855 |
3786 |
Problem +.# |
Problem +.# |
3856 |
|
Consider the following relation: |
|
|
3787 |
|
~ Consider the following relation: |
3857 |
3788 |
|
|
3858 |
3789 |
FLIGHT(From, \qquad To, \qquad Airline, \qquad Flight\#, \qquad DateHour, \qquad HeadQuarter, \qquad Pilot, \qquad TZDifference) |
FLIGHT(From, \qquad To, \qquad Airline, \qquad Flight\#, \qquad DateHour, \qquad HeadQuarter, \qquad Pilot, \qquad TZDifference) |
3859 |
3790 |
|
|
|
... |
... |
Each tuple in the relation BIKE contains information about a bike with a serial |
3882 |
3813 |
|
|
3883 |
3814 |
#. A retailer can't have two bikes of the same model from different batches. |
#. A retailer can't have two bikes of the same model from different batches. |
3884 |
3815 |
|
|
3885 |
|
\emph{solution:} \{Retailer, Model\} → Batch |
|
|
3816 |
|
*solution:* \{Retailer, Model\} → Batch |
3886 |
3817 |
|
|
3887 |
3818 |
#. The manufacturer and serial number uniquely identifies the bike and where it is sold. |
#. The manufacturer and serial number uniquely identifies the bike and where it is sold. |
3888 |
3819 |
#. A model number is registered by a manufacturer and therefore can't be used by another manufacturer. |
#. A model number is registered by a manufacturer and therefore can't be used by another manufacturer. |
|
... |
... |
compagny name &: String\\ |
4020 |
3951 |
|
|
4021 |
3952 |
|
|
4022 |
3953 |
Problem +.# |
Problem +.# |
4023 |
|
Consider the relation |
|
|
3954 |
|
~ Consider the relation |
|
3955 |
|
|
|
3956 |
|
CONTACT(Phone, Call\_center, Email, Zip, Brand, Website) |
4024 |
3957 |
|
|
4025 |
|
CONTACT(Phone, Call\_center, Email, Zip, Brand, Website) |
|
|
3958 |
|
and the following functional dependencies: |
4026 |
3959 |
|
|
4027 |
|
and the following functional dependencies: |
|
|
3960 |
|
**Bogue** |
4028 |
3961 |
|
|
4029 |
3962 |
||| |
||| |
4030 |
3963 |
---: | :---: | --- | |
---: | :---: | --- | |
|
... |
... |
and the following functional dependencies: |
4034 |
3967 |
\{Phone\} | → | \{Call\_center\} |
\{Phone\} | → | \{Call\_center\} |
4035 |
3968 |
|
|
4036 |
3969 |
Assume that \{Zip, Brand\} is the primary key. |
Assume that \{Zip, Brand\} is the primary key. |
4037 |
|
Normalize this relation to the second normal form, and \emph{then} to the third normal form. |
|
|
3970 |
|
Normalize this relation to the second normal form, and *then* to the third normal form. |
4038 |
3971 |
Give the relations, their primary keys, and functional dependencies for both steps. |
Give the relations, their primary keys, and functional dependencies for both steps. |
4039 |
3972 |
|
|
4040 |
3973 |
Problem +.#consultation |
Problem +.#consultation |
|
... |
... |
A tuple in the KEYBOARD relation contains information about a computer keyboard: |
4135 |
4068 |
|
|
4136 |
4069 |
|
|
4137 |
4070 |
Problem +.# |
Problem +.# |
|
4071 |
|
|
4138 |
4072 |
Look at the following relational model, and "reverse-engineer" it to obtain an E.-R. diagram: |
Look at the following relational model, and "reverse-engineer" it to obtain an E.-R. diagram: |
4139 |
4073 |
|
|
4140 |
4074 |
\begin{tikzpicture}[relation/.style={rectangle split, rectangle split parts=#1, rectangle split part align=base, draw, anchor=center, align=center, text height=3mm, text centered}] |
\begin{tikzpicture}[relation/.style={rectangle split, rectangle split parts=#1, rectangle split part align=base, draw, anchor=center, align=center, text height=3mm, text centered}] |
|
... |
... |
include/sol.sql |
4402 |
4336 |
|
|
4403 |
4337 |
# Databases Applications |
# Databases Applications |
4404 |
4338 |
|
|
4405 |
|
**Sources:** |
|
|
4339 |
|
## References |
4406 |
4340 |
|
|
4407 |
|
- Chapter 13 of the 6th Edition, Chapter 10 of the 7th Edition. |
|
4408 |
|
- <http://spots.augusta.edu/caubert/teaching/general/java/> |
|
4409 |
|
- Homework #7 (to come) |
|
|
4341 |
|
[@Textbook6, Ch. 13], [@Textbook7 , Chapter 10] |
|
4342 |
|
<http://spots.augusta.edu/caubert/teaching/general/java/> |
4410 |
4343 |
|
|
4411 |
4344 |
|
|
4412 |
4345 |
## Overview |
## Overview |
|
... |
... |
Exercise +.# |
4700 |
4633 |
|
|
4701 |
4634 |
|
|
4702 |
4635 |
Exercise +.# |
Exercise +.# |
4703 |
|
~ In the code below, there are \(5\) errors between line \(11\) and line \(30\). They are \emph{not} subtle Java errors (like misspelling a key word) and do not come from the DBMS (so you should assume that the password is correct, that the database exists, etc.). For each error, highlight it precisely and give a short explanation. |
|
|
4636 |
|
~ In the code below, there are \(5\) errors between line \(11\) and line \(30\). They are *not* subtle Java errors (like misspelling a key word) and do not come from the DBMS (so you should assume that the password is correct, that the database exists, etc.). For each error, highlight it precisely and give a short explanation. |
4704 |
4637 |
|
|
4705 |
|
~~~{.java} |
|
|
4638 |
|
~~~{.java .numberLines} |
4706 |
4639 |
import java.sql.*; |
import java.sql.*; |
4707 |
4640 |
|
|
4708 |
4641 |
public class MyProg{ |
public class MyProg{ |
4709 |
|
public static void main(String[] args) { |
|
4710 |
|
try ( |
|
4711 |
|
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" |
|
4712 |
|
+"HW_TestDB?user=testuser&password=password"); |
|
4713 |
|
Statement stmt = conn.createStatement(); |
|
4714 |
|
) { |
|
|
4642 |
|
public static void main(String[] args) { |
|
4643 |
|
try ( |
|
4644 |
|
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" |
|
4645 |
|
+"HW_TestDB?user=testuser&password=password"); |
|
4646 |
|
Statement stmt = conn.createStatement(); |
|
4647 |
|
) { |
4715 |
4648 |
|
|
4716 |
4649 |
/* Errors after this point.*/ |
/* Errors after this point.*/ |
4717 |
4650 |
|
|
4718 |
|
String strSelect = "SELECT title WHERE qty > 40 FROM disks;"; |
|
4719 |
|
ResultSet rset = stmt.executeUpdate(strSelect); |
|
|
4651 |
|
String strSelect = "SELECT title WHERE qty > 40 FROM disks;"; |
|
4652 |
|
ResultSet rset = stmt.executeUpdate(strSelect); |
4720 |
4653 |
|
|
4721 |
|
System.out.println("The records selected are: (listed last first):"); |
|
4722 |
|
rset.last(); |
|
|
4654 |
|
System.out.println("The records selected are: (listed last first):"); |
|
4655 |
|
rset.last(); |
4723 |
4656 |
|
|
4724 |
|
while(rset.previous()) { |
|
4725 |
|
String title = rset.getDouble("title"); |
|
4726 |
|
System.out.println(title + "\n"); |
|
4727 |
|
} |
|
|
4657 |
|
while(rset.previous()) { |
|
4658 |
|
String title = rset.getDouble("title"); |
|
4659 |
|
System.out.println(title + "\n"); |
|
4660 |
|
} |
4728 |
4661 |
|
|
4729 |
|
String sss = "SELECT title FROM disks WHERE Price <= ?"; |
|
4730 |
|
PreparedStatement ps = conn.prepareStatement(sss); |
|
4731 |
|
ResultSet result = ps.executeQuery(); |
|
|
4662 |
|
String sss = "SELECT title FROM disks WHERE Price <= ?"; |
|
4663 |
|
PreparedStatement ps = conn.prepareStatement(sss); |
|
4664 |
|
ResultSet result = ps.executeQuery(); |
4732 |
4665 |
|
|
4733 |
|
conn.close(); |
|
|
4666 |
|
conn.close(); |
4734 |
4667 |
|
|
4735 |
4668 |
/* Errors before this point.*/ |
/* Errors before this point.*/ |
4736 |
4669 |
|
|
4737 |
|
} catch(SQLException ex) { |
|
4738 |
|
ex.printStackTrace(); |
|
4739 |
|
} |
|
4740 |
|
} |
|
|
4670 |
|
} catch(SQLException ex) { |
|
4671 |
|
ex.printStackTrace(); |
|
4672 |
|
} |
|
4673 |
|
} |
4741 |
4674 |
} |
} |
4742 |
4675 |
~~~ |
~~~ |
4743 |
4676 |
|
|
|
... |
... |
List errors in program TO DO. |
4806 |
4739 |
|
|
4807 |
4740 |
## Problems |
## Problems |
4808 |
4741 |
|
|
4809 |
|
|
|
4810 |
|
This part will mainly hands on, and ask you to read code. |
|
4811 |
|
It is important that you learn how to get a working environment for a new technology quickly, for your understanding of this lecture, for the exam, and as a CS major. |
|
4812 |
|
I'll assume that you will have successfully completed those tasks , so don't wait and let me know if you had difficulties solving them. |
|
4813 |
|
|
|
4814 |
|
**References:** |
|
4815 |
|
|
|
4816 |
|
\begin{itemize} |
|
4817 |
|
\item This second part takes some inspiration from \url{https://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html}. If you experience troubles, \url{https://www.ntu.edu.sg/home/ehchua/programming/howto/ErrorMessages.html#JDBCErrors} might be a good read. |
|
4818 |
|
\item Section 13.3.2 of your textbook is a condensed, but good read. Many textbook on Java includes a part on Databases, cf. for instance the Chapter 16 of \emph{Starting Out with Java: Early Objects} (5th Edition) by Tony Gaddis. |
|
4819 |
|
\end{itemize} |
|
|
4742 |
|
- If you experience troubles, <https://www.ntu.edu.sg/home/ehchua/programming/howto/ErrorMessages.html#JDBCErrors> might be a good read. |
|
4743 |
|
- [@Textbook6, 13.3.2] is a condensed, but good read. Many textbook on Java includes a part on Databases, cf. for instance the [Gaddis2014, Chapter 16]. |
4820 |
4744 |
|
|
4821 |
4745 |
|
|
4822 |
4746 |
Problem +.# |
Problem +.# |
|
... |
... |
for linux, or (something like) |
4837 |
4761 |
|
|
4838 |
4762 |
for Windows. |
for Windows. |
4839 |
4763 |
|
|
4840 |
|
You just discovered MySQL's batch mode, that perform \emph{series} of instructions from a file. |
|
|
4764 |
|
You just discovered MySQL's batch mode, that perform *series* of instructions from a file. |
4841 |
4765 |
You can easily make sure that the database and the table were indeed created, and the values inserted. |
You can easily make sure that the database and the table were indeed created, and the values inserted. |
4842 |
4766 |
|
|
4843 |
4767 |
|
|
|
... |
... |
Problem +.# |
4845 |
4769 |
|
|
4846 |
4770 |
This exercise supposes you successfully completed Problem~ \ref{qu:batch}. |
This exercise supposes you successfully completed Problem~ \ref{qu:batch}. |
4847 |
4771 |
We will compile and execute your first database application, using Java and MySQL. |
We will compile and execute your first database application, using Java and MySQL. |
4848 |
|
\begin{itemize} |
|
4849 |
|
\item I will assume that you have MySQL installed and set-up as indicated in Homeworks \#1 and \#2. |
|
4850 |
|
\item I will assume that you have Java installed. If not, please refer to \url{http://spots.augusta.edu/caubert/teaching/general/java/} for a simple program and the instructions to compile and execute it. |
|
4851 |
|
\item We need to set up the \emph{driver} (or \emph{connector}) to make the java \texttt{sql} API and MySQL communicate. To do so, |
|
4852 |
|
\begin{itemize} |
|
4853 |
|
\item Go to \url{https://dev.mysql.com/downloads/connector/j/} |
|
4854 |
|
\item Click on "Download" in front of "Platform Independent (Architecture Independent), ZIP Archive" |
|
4855 |
|
\item Look for the (somewhat hidden) "No thanks, just start my download." |
|
4856 |
|
\item You will download a file named "mysql-connector-java-***.zip", where \texttt{***} is the version number. |
|
4857 |
|
\item Upon completion of the download, unzip the file, and locate the "mysql-connector-java-***-bin.jar" file. |
|
4858 |
|
\item Copy that file in \texttt{code/java/}. |
|
4859 |
|
\end{itemize} |
|
4860 |
|
\item Open a terminal in that same folder, and compile \texttt{FirstProg.java}, using |
|
4861 |
4772 |
|
|
4862 |
|
~~~{.bash} |
|
4863 |
|
javac FirstProg.java |
|
4864 |
|
~~~ |
|
|
4773 |
|
- I will assume that you have MySQL installed and set-up as indicated in Homeworks \#1 and \#2. |
|
4774 |
|
- I will assume that you have Java installed. If not, please refer to \url{http://spots.augusta.edu/caubert/teaching/general/java/} for a simple program and the instructions to compile and execute it. |
|
4775 |
|
- We need to set up the *driver* (or *connector*) to make the java \texttt{sql} API and MySQL communicate. To do so, |
|
4776 |
|
\begin{- ize} |
|
4777 |
|
- Go to \url{https://dev.mysql.com/downloads/connector/j/} |
|
4778 |
|
- Click on "Download" in front of "Platform Independent (Architecture Independent), ZIP Archive" |
|
4779 |
|
- Look for the (somewhat hidden) "No thanks, just start my download." |
|
4780 |
|
- You will download a file named "mysql-connector-java-***.zip", where `***` is the version number. |
|
4781 |
|
- Upon completion of the download, unzip the file, and locate the "mysql-connector-java-***-bin.jar" file. |
|
4782 |
|
- Copy that file in `code/java/`. |
|
4783 |
|
\end{- ize} |
|
4784 |
|
- Open a terminal in that same folder, and compile `FirstProg.java`, using |
4865 |
4785 |
|
|
4866 |
|
(or an equivalent command for windows). |
|
4867 |
|
Normally, nothing will be printed, but a \texttt{FirstProg.class} file will be created. |
|
4868 |
|
\item Now, execute that program, using |
|
|
4786 |
|
~~~{.bash} |
|
4787 |
|
javac FirstProg.java |
|
4788 |
|
~~~ |
4869 |
4789 |
|
|
4870 |
|
~~~{.bash} |
|
4871 |
|
java -cp .:mysql-connector-java-***-bin.jar FirstProg |
|
4872 |
|
~~~ |
|
|
4790 |
|
(or an equivalent command for windows). |
|
4791 |
|
Normally, nothing will be printed, but a `FirstProg.class` file will be created. |
|
4792 |
|
- Now, execute that program, using |
4873 |
4793 |
|
|
4874 |
|
in Linux, or |
|
|
4794 |
|
~~~{.bash} |
|
4795 |
|
java -cp .:mysql-connector-java-***-bin.jar FirstProg |
|
4796 |
|
~~~ |
4875 |
4797 |
|
|
4876 |
|
~~~{.bash} |
|
4877 |
|
java -cp .;mysql-connector-java-***-bin.jar FirstProg |
|
4878 |
|
~~~ |
|
|
4798 |
|
in Linux, or |
|
4799 |
|
|
|
4800 |
|
~~~{.bash} |
|
4801 |
|
java -cp .;mysql-connector-java-***-bin.jar FirstProg |
|
4802 |
|
~~~ |
4879 |
4803 |
|
|
4880 |
|
in Windows. |
|
4881 |
|
The \texttt{-cp} option lists the places where java should look for the class used in the program: we are explicitely asking java to use the \texttt{mysql-connector-java-***-bin.jar} executable to execute our \texttt{FirstProg} executable. |
|
4882 |
|
Try to execute \texttt{FirstProg} without that flag, and see what happens. |
|
4883 |
|
\end{itemize} |
|
|
4804 |
|
in Windows. |
|
4805 |
|
The `-cp` option lists the places where java should look for the class used in the program: we are explicitely asking java to use the `mysql-connector-java-***-bin.jar` executable to execute our `FirstProg` executable. |
|
4806 |
|
Try to execute `FirstProg` without that flag, and see what happens. |
4884 |
4807 |
|
|
4885 |
4808 |
|
|
4886 |
4809 |
Solution +.# |
Solution +.# |
|
... |
... |
Read, execute, break, edit, compile, patch, hack and (most importantly) understa |
4909 |
4832 |
|
|
4910 |
4833 |
# A Bit About Security |
# A Bit About Security |
4911 |
4834 |
|
|
4912 |
|
### Common Things |
|
|
4835 |
|
## Common Things |
4913 |
4836 |
|
|
4914 |
|
#### Threat model |
|
|
4837 |
|
### Threat model |
4915 |
4838 |
|
|
4916 |
4839 |
- Who is threatening you? |
- Who is threatening you? |
4917 |
4840 |
- What are the risks? |
- What are the risks? |
|
... |
... |
Read, execute, break, edit, compile, patch, hack and (most importantly) understa |
4923 |
4846 |
- "You are as strong as your weakest link." |
- "You are as strong as your weakest link." |
4924 |
4847 |
- Never trust the user or their computer. |
- Never trust the user or their computer. |
4925 |
4848 |
|
|
4926 |
|
#### Control Measures |
|
|
4849 |
|
### Control Measures |
4927 |
4850 |
|
|
4928 |
4851 |
- Access control (user account, passwords, restrictions) |
- Access control (user account, passwords, restrictions) |
4929 |
4852 |
- Inference control (can't access information about a particular "case") |
- Inference control (can't access information about a particular "case") |
|
... |
... |
Read, execute, break, edit, compile, patch, hack and (most importantly) understa |
4932 |
4855 |
|
|
4933 |
4856 |
**Insert short intro. to salting, cryptography.** |
**Insert short intro. to salting, cryptography.** |
4934 |
4857 |
|
|
4935 |
|
### Particular Things |
|
|
4858 |
|
## Particular Things |
4936 |
4859 |
|
|
4937 |
|
#### Attack |
|
|
4860 |
|
### Attack |
4938 |
4861 |
|
|
4939 |
4862 |
Attacks: buffer overflow, denial of service, weak authentication, privilege escalation, `SQL` injections. |
Attacks: buffer overflow, denial of service, weak authentication, privilege escalation, `SQL` injections. |
4940 |
4863 |
|
|
|
... |
... |
c. `admin'--` Line comment, privilege escalation |
4953 |
4876 |
|
|
4954 |
4877 |
Can also be used for DBMS fingerprinting. |
Can also be used for DBMS fingerprinting. |
4955 |
4878 |
|
|
4956 |
|
#### Protections |
|
|
4879 |
|
### Protections |
|
4880 |
|
|
|
4881 |
|
1. Backups: |
|
4882 |
|
|
|
4883 |
|
~~~{.bash} |
|
4884 |
|
mysqldump --all-databases - u testuser -p password - h localhost > dump.sql |
|
4885 |
|
~~~ |
4957 |
4886 |
|
|
4958 |
|
1. Backups: `mysqldump --all-databases - u testuser -p password - h localhost > dump.sql` |
|
4959 |
4887 |
2. Prepared Statemets (a.k.a. stored procedures) |
2. Prepared Statemets (a.k.a. stored procedures) |
4960 |
4888 |
3. White list input validation |
3. White list input validation |
4961 |
4889 |
4. Escaping |
4. Escaping |
|
... |
... |
Option b. was generally less expensive, but came with two drawbacks w.r.t. datab |
4996 |
4924 |
|
|
4997 |
4925 |
### A First Shift |
### A First Shift |
4998 |
4926 |
|
|
4999 |
|
- Google Big Table, 2004 (made public in … 2015!) |
|
5000 |
|
- Amazon DynamoDB, 2004 (used in Simple Storage Service (S3) in 2007) |
|
5001 |
|
- Facebook's Cassandra is sometimes mentioned, but it came later on, in 2008. |
|
|
4927 |
|
- [Google Big Table](https://cloud.google.com/bigtable/), 2004 (made public in … 2015!) [@DBLP:conf/osdi/ChangDGHWBCFG06] |
|
4928 |
|
- [Amazon DynamoDB](https://aws.amazon.com/dynamodb/), 2004 (used in Simple Storage Service (S3) in 2007) |
|
4929 |
|
- Facebook's Cassandra is sometimes mentioned, but it came later on, around 2009 [@Lakshman2009]. |
5002 |
4930 |
|
|
5003 |
4931 |
Particular, big company, with specific needs, but people interrested in solving some of their problems. |
Particular, big company, with specific needs, but people interrested in solving some of their problems. |
5004 |
4932 |
Now, people started to think that there could be other ways. |
Now, people started to think that there could be other ways. |
|
... |
... |
Multi-model | Support multiple data models | [Apache Ignite](https://ignite.apa |
5092 |
5020 |
|
|
5093 |
5021 |
|
|
5094 |
5022 |
Sources: |
Sources: |
5095 |
|
|
|
5096 |
|
- <https://en.wikipedia.org/wiki/NoSQL> |
|
5097 |
|
- NoSQL Distilled: A Brief Guide to the Emerging World of Polyglot Persistence, by Martin Fowler and Pramod J. Sadalage |
|
5098 |
|
- NoSQL for Mere Mortals, by Dan Sullivan |
|
5099 |
|
- Chapter 24 (7th Edition) of your textbook. |
|
5100 |
|
- <https://en.wikipedia.org/wiki/Bigtable> + ref <https://static.googleusercontent.com/media/research.google.com/en//archive/bigtable-osdi06.pdf> + <https://cloud.google.com/bigtable/> |
|
5101 |
|
- <https://en.wikipedia.org/wiki/Dynamo_(storage_system)> + <https://aws.amazon.com/dynamodb/> |
|
5102 |
|
- <https://en.wikipedia.org/wiki/Polyglot_persistence> |
|
5103 |
|
- <https://db.cs.cmu.edu/papers/2016/pavlo-newsql-sigmodrec2016.pdf> |
|
5104 |
|
- <https://en.wikipedia.org/wiki/ACID> |
|
|
5023 |
|
[@NoSQLDistilled], <https://en.wikipedia.org/wiki/NoSQL> |
|
5024 |
|
[@Sullivan2015], [@Textbook7, Chapter 24], [@DBLP:journals/sigmod/PavloA16] |
5105 |
5025 |
- <http://delivery.acm.org/10.1145/1780000/1773922/p35-lakshman.pdf?ip=134.224.220.1&id=1773922&acc=ACTIVE%20SERVICE&key=A79D83B43E50B5B8%2EA1A26A3EF7ED82C5%2E4D4702B0C3E38B35%2E4D4702B0C3E38B35&__acm__=1524060110_1b69882dcd91c4186c3613d6cebf5549> and <https://docs.datastax.com/en/articles/cassandra/cassandrathenandnow.html> |
- <http://delivery.acm.org/10.1145/1780000/1773922/p35-lakshman.pdf?ip=134.224.220.1&id=1773922&acc=ACTIVE%20SERVICE&key=A79D83B43E50B5B8%2EA1A26A3EF7ED82C5%2E4D4702B0C3E38B35%2E4D4702B0C3E38B35&__acm__=1524060110_1b69882dcd91c4186c3613d6cebf5549> and <https://docs.datastax.com/en/articles/cassandra/cassandrathenandnow.html> |
5106 |
5026 |
|
|
5107 |
5027 |
## MongoDB |
## MongoDB |
|
... |
... |
Solution +.# |
5421 |
5341 |
|
|
5422 |
5342 |
# Review Session |
# Review Session |
5423 |
5343 |
|
|
|
5344 |
|
# Tools and Ressources |
|
5345 |
|
|
|
5346 |
|
## Typesetting |
|
5347 |
|
|
|
5348 |
|
The source code for those notes is hosted at [rocketgit](https://rocketgit.com/user/caubert/CSCI_3410), typeset in markdown, and then compiled using [pandoc](http://pandoc.org/) and two filters, [pandoc-numbering](https://github.com/chdemko/pandoc-numbering) and [pandoc-citeproc](https://github.com/jgm/pandoc-citeproc). |
|
5349 |
|
|
|
5350 |
|
To compile it, you will need |
|
5351 |
|
|
|
5352 |
|
- To clone the source, using [git](https://git-scm.com/), and |
|
5353 |
|
|
|
5354 |
|
~~~{.bash} |
|
5355 |
|
git clone https://rocketgit.com/user/caubert/CSCI_3410 |
|
5356 |
|
~~~ |
|
5357 |
|
|
|
5358 |
|
- To install pandoc, cf. <http://pandoc.org/installing.html> |
|
5359 |
|
- To produce `pdf` output, you will need LaTeX (the "Installing pandoc" page contains instructions) |
|
5360 |
|
- To install pandoc-numbering. cf. <https://github.com/chdemko/pandoc-numbering#installation> |
|
5361 |
|
|
|
5362 |
|
## Various Ressources |
|
5363 |
|
|
|
5364 |
|
On top of the [references](references), were useful in the writing of those notes: |
|
5365 |
|
|
|
5366 |
|
- <https://www.1keydata.com/datawarehousing/data-modeling-levels.html> |
|
5367 |
|
- <https://en.wikipedia.org/wiki/Unified_Modeling_Language> |
|
5368 |
|
- <https://creately.com/blog/diagrams/class-diagram-relationships/> |
|
5369 |
|
- <https://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html> |
|
5370 |
|
|
|
5371 |
|
|
5424 |
5372 |
# References {#references} |
# References {#references} |