File notes/lectures_notes.md changed (mode: 100644) (index bedffa0..043625d) |
... |
... |
pandoc-numbering: |
33 |
33 |
|
|
34 |
34 |
## How to use this guide {-} |
## How to use this guide {-} |
35 |
35 |
|
|
36 |
|
These lecture notes are written in an elusive style: their are a support for the explanation I will be making at the board. |
|
|
36 |
|
These lecture notes are written in an elusive style: their are a support for the explanations I will be making at the board. |
37 |
37 |
They are not designed to be self-contained: they are rather a list of topics and reminders, along with handy examples, code and drawings. |
They are not designed to be self-contained: they are rather a list of topics and reminders, along with handy examples, code and drawings. |
38 |
38 |
Reading them before coming to the lecture will help you getting a sense of the next topic we will be discussing, but you may sometimes have trouble deciphering their … *unique* style. |
Reading them before coming to the lecture will help you getting a sense of the next topic we will be discussing, but you may sometimes have trouble deciphering their … *unique* style. |
39 |
39 |
|
|
|
... |
... |
On top of the notes, you will find in this document: |
52 |
52 |
- Sometimes, solution to some of those problems. |
- Sometimes, solution to some of those problems. |
53 |
53 |
|
|
54 |
54 |
Any feedback is greatly appreciated. |
Any feedback is greatly appreciated. |
|
55 |
|
Please refer to <http://spots.augusta.edu/caubert/db/ln/README.html#contributing> for how to contribute to those notes. |
55 |
56 |
|
|
56 |
57 |
The syllabus is at <http://spots.augusta.edu/caubert/db/>, and the webpage for this notes is at <http://spots.augusta.edu/caubert/db/ln/>. |
The syllabus is at <http://spots.augusta.edu/caubert/db/>, and the webpage for this notes is at <http://spots.augusta.edu/caubert/db/ln/>. |
57 |
58 |
|
|
|
... |
... |
A typical (metting twice a week, ±17 weeks, ±30 classes) semester is divided a |
71 |
72 |
- Lecture 21--22: Unified Modeling Language Diagram |
- Lecture 21--22: Unified Modeling Language Diagram |
72 |
73 |
- Lecture 23--24: Review session and Exam #2 |
- Lecture 23--24: Review session and Exam #2 |
73 |
74 |
- Lecture 25: Intro to Database Programming and to Java |
- Lecture 25: Intro to Database Programming and to Java |
74 |
|
- Lecture 26--28: Database Programming |
|
|
75 |
|
- Lecture 26--28: Database Applications |
75 |
76 |
- Lecture 29: Introduction to NoSQL |
- Lecture 29: Introduction to NoSQL |
76 |
77 |
- Lecture 30: Introduction to MongoDB |
- Lecture 30: Introduction to MongoDB |
77 |
78 |
|
|
78 |
|
For information purposes, a |
|
|
79 |
|
For information purposes, a straight line like this: |
79 |
80 |
|
|
80 |
81 |
--- |
--- |
81 |
82 |
|
|
|
... |
... |
To give you a sense of what you will be asked to do during the exams, please fin |
95 |
96 |
- A variation on [@Textbook6, Exercise 3.11], [@Textbook7, Exercise 5.11] |
- A variation on [@Textbook6, Exercise 3.11], [@Textbook7, Exercise 5.11] |
96 |
97 |
- [%D %n (%T)](#problem:train) |
- [%D %n (%T)](#problem:train) |
97 |
98 |
- Exam #2: |
- Exam #2: |
|
99 |
|
- Six small exercises, |
|
100 |
|
- [%D %n (%T)](#problem:BusinessToEr) |
|
101 |
|
- [%D %n (%T)](#problem:bike) |
|
102 |
|
- [%D %n (%T)](#problem:NormalizeContact) |
|
103 |
|
- A variation on [%D %n (%T)](#problem:ERtoRELCountry) |
98 |
104 |
- Final: |
- Final: |
|
105 |
|
- A variation on [%D %n (%T)](#exercise:ErrorsInCode) |
|
106 |
|
- A variation on [%D %n (%T)](#problem:coffee) |
|
107 |
|
- A variation on [%D %n (%T)](#problem:UMLtoRELDriver): students were asked to draw the ER diagram for that schema. |
|
108 |
|
- [%D %n (%T)](#problem:book) |
|
109 |
|
- [%D %n (%T)](#problem:consultation) |
99 |
110 |
|
|
100 |
111 |
### Spring 2018 {-} |
### Spring 2018 {-} |
101 |
112 |
|
|
102 |
113 |
- Exam #1: |
- Exam #1: |
103 |
|
- [%D %n (%T)](#problem:cinema), except that I gave some of the relatins and attributes, to held student getting started with the problem. |
|
104 |
|
- [%D %n (%T)](#designforprof) |
|
|
114 |
|
- [%D %n (%T)](#problem:cinema), except that I gave some of the relatins and attributes, to help getting started with the problem. |
|
115 |
|
- [%D %n (%T)](#problem:designforprof) |
105 |
116 |
- [%D %n (%T)](#problem:train) |
- [%D %n (%T)](#problem:train) |
106 |
117 |
- [%D %n (%T)](#problem:coffee) |
- [%D %n (%T)](#problem:coffee) |
107 |
118 |
- Exam #2: |
- Exam #2: |
|
119 |
|
- A variation on [%D %n (%T)](#problem:NormalizeContact) |
|
120 |
|
- A variation on [%D %n (%T)](#problem:consultation) |
|
121 |
|
- [%D %n (%T)](#problem:roleplaying) |
|
122 |
|
- A variation on [%D %n (%T)](#problem:BusinessToDependecies-KEYBOARD) |
|
123 |
|
- [%D %n (%T)](#problem:ERtoRELCountry) |
108 |
124 |
- Final: |
- Final: |
|
125 |
|
- Take the relational model of the solution of [%D %n (%T)](#problem:cinema), and "reverse-engineer" it to obtain a ER diagram (this problem was harder than I expected) |
|
126 |
|
- Six small exercises (@exercise:deletion, @exercise:polyglot, @exercise:schemaless, @exercise:denormalization, @exercise:mismatch) |
|
127 |
|
- [%D %n (%T)](#problem:NormalizeFlight) |
|
128 |
|
- A variation on [%D %n (%T)](#problem:BusinessToEr) |
|
129 |
|
- A variation on [%D %n (%T)](#problem:UMLtoRELDriver): students were asked to draw the ER diagram for that schema. |
|
130 |
|
|
109 |
131 |
|
|
110 |
132 |
## Typesetting and Acknowledgments {-} |
## Typesetting and Acknowledgments {-} |
111 |
133 |
|
|
112 |
|
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). |
|
113 |
|
The document uses [Linux Libertine fonts](http://libertine-fonts.org/), and the drawings use various [LaTeX](https://www.latex-project.org/) packages, including [PGF, TikZ](https://sourceforge.net/projects/pgf/) and [tikz-er2](https://bitbucket.org/pavel_calado/tikz-er2/raw/da9f9f7f169647cad6d91df7975400b1605ae67a/tikz-er2.sty). |
|
|
134 |
|
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 multiple filters ([pandoc-numbering](https://github.com/chdemko/pandoc-numbering), [pandoc-citeproc](https://github.com/jgm/pandoc-citeproc), [pandoc-include-code](https://github.com/owickstrom/pandoc-include-code)). |
|
135 |
|
The document uses [Linux Libertine fonts](http://libertine-fonts.org/)^[At least, the pdf version.], and the drawings use various [LaTeX](https://www.latex-project.org/) packages, including [PGF, TikZ](https://sourceforge.net/projects/pgf/), [tikz-er2](https://bitbucket.org/pavel_calado/tikz-er2/raw/da9f9f7f169647cad6d91df7975400b1605ae67a/tikz-er2.sty) and [tikz-dependency](https://ctan.org/pkg/tikz-dependency). |
114 |
136 |
The help from the [TeX - LaTeX Stack Exchange](https://tex.stackexchange.com/) community greatly improved this document. |
The help from the [TeX - LaTeX Stack Exchange](https://tex.stackexchange.com/) community greatly improved this document. |
115 |
137 |
The u͟n͟d͟e͟r͟l͟i͟n͟e͟ text is obtained from <https://yaytext.com/underline/>, the unicode symbols are searched in <http://milde.users.sourceforge.net/LUCR/Math/unimathsymbols.html>. |
The u͟n͟d͟e͟r͟l͟i͟n͟e͟ text is obtained from <https://yaytext.com/underline/>, the unicode symbols are searched in <http://milde.users.sourceforge.net/LUCR/Math/unimathsymbols.html>. |
116 |
138 |
|
|
117 |
|
To clone and compile this document, refer to the `README.md` file, at <https://rocketgit.com/user/caubert/CSCI_3410/source/tree/branch/master/blob/README.md> or <http://spots.augusta.edu/caubert/teaching/2019/spring/csci3410/CSCI_3410_2019_F_README.html>. |
|
|
139 |
|
To clone and compile this document, refer to the `README.md` file, at <https://rocketgit.com/user/caubert/CSCI_3410/source/tree/branch/master/blob/README.md> or <http://spots.augusta.edu/caubert/db/ln/README.html>. |
118 |
140 |
|
|
119 |
141 |
## Various Resources {-} |
## Various Resources {-} |
120 |
142 |
|
|
|
... |
... |
On top of the [references](references) and of the "resources" listed at the begi |
124 |
146 |
- <https://en.wikipedia.org/wiki/Unified_Modeling_Language> |
- <https://en.wikipedia.org/wiki/Unified_Modeling_Language> |
125 |
147 |
- <https://creately.com/blog/diagrams/class-diagram-relationships/> |
- <https://creately.com/blog/diagrams/class-diagram-relationships/> |
126 |
148 |
- <https://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html> |
- <https://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html> |
127 |
|
- <https://tex.stackexchange.com/a/429610/> |
|
128 |
149 |
|
|
129 |
150 |
## Copyright {-} |
## Copyright {-} |
130 |
151 |
|
|
131 |
152 |
This work is under [Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/) or later. |
This work is under [Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/) or later. |
132 |
153 |
|
|
133 |
|
Some figures and ressources are borrowed from other sources (like [@Textbook6] or [@Textbook7]), in which case it's indicated clearly. |
|
|
154 |
|
Some figures and ressources are borrowed from other sources, like [@Textbook6], [@Textbook7], or the resources mentionned previously, in which case it is indicated clearly. |
134 |
155 |
|
|
135 |
156 |
|
|
136 |
157 |
## Softwares {-} |
## Softwares {-} |
137 |
158 |
|
|
138 |
|
- <https://sequelpro.com/> [Open Source](https://github.com/sequelpro/sequelpro/blob/master/LICENSE) and free for Mac OS X. |
|
|
159 |
|
_I would like here to include some of the softwares I believe could make your semester easier, but still have to work on it._ |
139 |
160 |
|
|
|
161 |
|
<!-- |
|
162 |
|
- <https://sequelpro.com/> [Open Source](https://github.com/sequelpro/sequelpro/blob/master/LICENSE) and free for Mac OS X. |
|
163 |
|
--> |
140 |
164 |
|
|
141 |
165 |
# Introduction |
# Introduction |
142 |
166 |
|
|
|
... |
... |
A database (DB) is **a collection of related data**. |
160 |
184 |
Refer to the ["A simplified database environment"](#fig:lab) figure , where |
Refer to the ["A simplified database environment"](#fig:lab) figure , where |
161 |
185 |
|
|
162 |
186 |
- The program can be written in any language, be a web interface, etc. |
- The program can be written in any language, be a web interface, etc. |
163 |
|
- Most DBMS software include a Command-Line-Interface |
|
|
187 |
|
- Most DBMS software include a Command-Line Interface (CLI), |
164 |
188 |
- Sometimes, meta-data and data are closer than pictured (you can have "self-describing meta-data", that is, they can't be distinguished). |
- Sometimes, meta-data and data are closer than pictured (you can have "self-describing meta-data", that is, they can't be distinguished). |
165 |
189 |
|
|
166 |
190 |
## Database Management System (DBMS) |
## Database Management System (DBMS) |
|
... |
... |
Refer to the ["A simplified database environment"](#fig:lab) figure , where |
168 |
192 |
A DBMS is a *general purpose* software that is used to |
A DBMS is a *general purpose* software that is used to |
169 |
193 |
|
|
170 |
194 |
#. Define (= datatype, constraints, structures, etc.) |
#. Define (= datatype, constraints, structures, etc.) |
171 |
|
#. Construct (= storing the data) |
|
172 |
|
#. Manipulate (= query, update, etc.) |
|
173 |
|
#. Share (=among users, softwares.) |
|
|
195 |
|
#. Construct / Create (= storing the data) |
|
196 |
|
#. Manipulate / Maintain (= query, update, etc.) |
|
197 |
|
#. Share / Control access (= among users, softwares.) |
174 |
198 |
|
|
175 |
199 |
You can think of a tool to |
You can think of a tool to |
176 |
200 |
|
|
|
... |
... |
Problem (A database catalog for a campus) +.#campus |
353 |
377 |
|
|
354 |
378 |
@problem:campus -- Question -.# |
@problem:campus -- Question -.# |
355 |
379 |
|
|
356 |
|
: Look at [@Textbook6, Figure 1.3], or [@Textbook7, Figure 1.3] to understand how to write a database catalog. |
|
357 |
|
It is made of two part: a table containing the relations' name and their number of columns, and a table containing the columns' name, their data type, and the relation to which they belong. |
|
358 |
|
Then, write the database catalog corresponding to the `CAMPUS` database. |
|
|
380 |
|
: A database catalog is made of two part: a table containing the relations' name and their number of columns, and a table containing the columns' name, their data type, and the relation to which they belong. |
|
381 |
|
(You can see an example in [@Textbook6, Figure 1.3], or [@Textbook7, Figure 1.3]). |
|
382 |
|
Write the database catalog corresponding to the `CAMPUS` database. |
359 |
383 |
|
|
360 |
384 |
@problem:campus -- Question -.# |
@problem:campus -- Question -.# |
361 |
385 |
|
|
|
... |
... |
Solution to @problem:campus |
418 |
442 |
#. No, we could not get the office hours of a professor. |
#. No, we could not get the office hours of a professor. |
419 |
443 |
#. Yes, we could list all the professors whose offices are in the same building. |
#. Yes, we could list all the professors whose offices are in the same building. |
420 |
444 |
#. Yes, we could list all the rooms. |
#. Yes, we could list all the rooms. |
421 |
|
#. If a new professor arrives, and has to share his office with another professor, we would not have to revise our database catalog (it's fine for two professor to have the same room number, in our model). |
|
|
445 |
|
#. If a new professor arrives, and has to share his office with another professor, we would not have to revise our database catalog (it is fine for two professor to have the same room number, in our model). |
422 |
446 |
#. Yes, we could list which professors are at the same floor. |
#. Yes, we could list which professors are at the same floor. |
423 |
447 |
#. No, we could not tell which professor has the highest evaluations. |
#. No, we could not tell which professor has the highest evaluations. |
424 |
448 |
|
|
|
... |
... |
Exercise +.# |
646 |
670 |
|
|
647 |
671 |
Exercise +.#entityintegrity |
Exercise +.#entityintegrity |
648 |
672 |
|
|
649 |
|
: Whait is entity integrity? Why is it useful? |
|
|
673 |
|
: What is entity integrity? Why is it useful? |
650 |
674 |
|
|
651 |
675 |
Exercise +.# |
Exercise +.# |
652 |
676 |
|
|
|
... |
... |
Problem (Discovering the documentation) +.#sqldoc |
2433 |
2457 |
- <https://dev.mysql.com/doc/refman/8.0/en/sql-syntax.html> |
- <https://dev.mysql.com/doc/refman/8.0/en/sql-syntax.html> |
2434 |
2458 |
|
|
2435 |
2459 |
Those are the commands implemented in the DBMS you are actually using. |
Those are the commands implemented in the DBMS you are actually using. |
2436 |
|
Since there are small variations from one implementation to the other, it's better to take one of this link as a reference in the future. |
|
|
2460 |
|
Since there are small variations from one implementation to the other, it is better to take one of this link as a reference in the future. |
2437 |
2461 |
|
|
2438 |
2462 |
--- |
--- |
2439 |
2463 |
|
|
|
... |
... |
Problem (Duplicate rows in SQL) +.#repetition |
2578 |
2602 |
|
|
2579 |
2603 |
--- |
--- |
2580 |
2604 |
|
|
2581 |
|
Problem (Revisiting the `PROF` table) +.#profrevisited |
|
|
2605 |
|
Problem (Revisiting the PROF table) +.#profrevisited |
2582 |
2606 |
~ |
~ |
2583 |
2607 |
|
|
2584 |
2608 |
Create the `PROF`, `STUDENT`, `DEPARTMENT` and `GRADE` tables as in the ["Constructing and populating a new example"](#sec:profexample) section. |
Create the `PROF`, `STUDENT`, `DEPARTMENT` and `GRADE` tables as in the ["Constructing and populating a new example"](#sec:profexample) section. |
|
... |
... |
Problem (Improving a role-playing game) +.#roleplaying |
2887 |
2911 |
But you friend simply can't figure out how to do that. |
But you friend simply can't figure out how to do that. |
2888 |
2912 |
|
|
2889 |
2913 |
Can you provide a *relational model* (no need to write the `SQL` code, but remember to indicate the primary and foreign keys) that would solve all of your friend's troubles? |
Can you provide a *relational model* (no need to write the `SQL` code, but remember to indicate the primary and foreign keys) that would solve all of your friend's troubles? |
2890 |
|
|
|
2891 |
|
## Solution to Selected Problems |
|
|
2914 |
|
|
|
2915 |
|
## Solution to Selected Problems {-} |
2892 |
2916 |
|
|
2893 |
2917 |
Solution to [%D %n (%T)](#problem:address) |
Solution to [%D %n (%T)](#problem:address) |
2894 |
2918 |
~ |
~ |
|
... |
... |
Solution to [%D %n (%T)](#problem:address) |
3035 |
3059 |
@problem:address -- Solution to Q. -.# |
@problem:address -- Solution to Q. -.# |
3036 |
3060 |
~ |
~ |
3037 |
3061 |
|
|
|
3062 |
|
The query |
|
3063 |
|
|
3038 |
3064 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
3039 |
3065 |
INSERT INTO NAME VALUE ('Hi'); |
INSERT INTO NAME VALUE ('Hi'); |
3040 |
3066 |
~~~ |
~~~ |
|
... |
... |
Solution to [%D %n (%T)](#problem:address) |
3056 |
3082 |
--- |
--- |
3057 |
3083 |
|
|
3058 |
3084 |
Solution to [%D %n (%T)](#problem:repetition) |
Solution to [%D %n (%T)](#problem:repetition) |
3059 |
|
~ |
|
|
3085 |
|
~ |
3060 |
3086 |
As a preamble, we create our own table: |
As a preamble, we create our own table: |
3061 |
3087 |
|
|
3062 |
3088 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
|
... |
... |
Solution to [%D %n (%T)](#problem:repetition) |
3068 |
3094 |
Y INT); |
Y INT); |
3069 |
3095 |
~~~ |
~~~ |
3070 |
3096 |
|
|
3071 |
|
@problem:repetition -- Solution to Q. -.# |
|
3072 |
|
~ |
|
3073 |
|
The command is |
|
3074 |
|
|
|
3075 |
|
~~~{.sqlmysql} |
|
3076 |
|
INSERT INTO EXAMPLE VALUES('Train', 4); |
|
3077 |
|
~~~ |
|
3078 |
|
|
|
3079 |
|
If we execute this command twice, then SQL is OK with it, and insert the same tuple twice. |
|
3080 |
|
|
|
3081 |
|
~~~{.sqlmysql} |
|
3082 |
|
SELECT * FROM EXAMPLE; |
|
3083 |
|
~~~ |
|
3084 |
|
|
|
3085 |
|
displays |
|
3086 |
|
|
|
3087 |
|
~~~{.bash} |
|
3088 |
|
+-------+------+ |
|
3089 |
|
| X | Y | |
|
3090 |
|
+-------+------+ |
|
3091 |
|
| Train | 4 | |
|
3092 |
|
| Train | 4 | |
|
3093 |
|
+-------+------+ |
|
3094 |
|
~~~ |
|
3095 |
|
|
|
3096 |
|
This is an illustration of the fact that a table in sql is *not* a set, as opposed to a relation in the relation model. |
|
3097 |
|
|
|
3098 |
|
@problem:repetition -- Solution to Q. -.# |
|
3099 |
|
~ |
|
3100 |
|
|
|
3101 |
|
The command |
|
3102 |
|
|
|
3103 |
|
~~~{.sqlmysql} |
|
3104 |
|
ALTER TABLE EXAMPLE ADD PRIMARY KEY (X); |
|
3105 |
|
~~~ |
|
3106 |
|
|
|
3107 |
|
returns |
|
|
3097 |
|
@problem:repetition -- Solution to Q. -.# |
|
3098 |
|
~ |
|
3099 |
|
The command is |
|
3100 |
|
|
|
3101 |
|
~~~{.sqlmysql} |
|
3102 |
|
INSERT INTO EXAMPLE VALUES('Train', 4); |
|
3103 |
|
~~~ |
3108 |
3104 |
|
|
3109 |
|
~~~{.bash} |
|
3110 |
|
ERROR 1062 (23000): Duplicate entry 'Train' for key 'PRIMARY' |
|
3111 |
|
~~~ |
|
|
3105 |
|
If we execute this command twice, then SQL is OK with it, and insert the same tuple twice. |
|
3106 |
|
|
|
3107 |
|
~~~{.sqlmysql} |
|
3108 |
|
SELECT * FROM EXAMPLE; |
|
3109 |
|
~~~ |
|
3110 |
|
|
|
3111 |
|
displays |
|
3112 |
|
|
|
3113 |
|
~~~{.bash} |
|
3114 |
|
+-------+------+ |
|
3115 |
|
| X | Y | |
|
3116 |
|
+-------+------+ |
|
3117 |
|
| Train | 4 | |
|
3118 |
|
| Train | 4 | |
|
3119 |
|
+-------+------+ |
|
3120 |
|
~~~ |
3112 |
3121 |
|
|
3113 |
|
We tried to declare that `X` was a primary key, but SQL disagreed, since two rows have the same value for that attribute. |
|
|
3122 |
|
This is an illustration of the fact that a table in sql is *not* a set, as opposed to a relation in the relation model. |
3114 |
3123 |
|
|
3115 |
|
@problem:repetition -- Solution to Q. -.# |
|
3116 |
|
: Once the table is empty, `X` indeed is a primary key, so SQL stops complaining and let us make it a primary key. |
|
3117 |
|
|
|
3118 |
|
@problem:repetition -- Solution to Q. -.# |
|
3119 |
|
~ |
|
3120 |
|
We can't introduce the same value twice: |
|
|
3124 |
|
@problem:repetition -- Solution to Q. -.# |
|
3125 |
|
~ |
3121 |
3126 |
|
|
3122 |
|
~~~{.sqlmysql} |
|
3123 |
|
INSERT INTO EXAMPLE VALUES('Train', 4); |
|
3124 |
|
~~~ |
|
|
3127 |
|
The command |
3125 |
3128 |
|
|
3126 |
|
returns, the second time: |
|
|
3129 |
|
~~~{.sqlmysql} |
|
3130 |
|
ALTER TABLE EXAMPLE ADD PRIMARY KEY (X); |
|
3131 |
|
~~~ |
|
3132 |
|
|
|
3133 |
|
returns |
|
3134 |
|
|
|
3135 |
|
~~~{.bash} |
|
3136 |
|
ERROR 1062 (23000): Duplicate entry 'Train' for key 'PRIMARY' |
|
3137 |
|
~~~ |
|
3138 |
|
|
|
3139 |
|
We tried to declare that `X` was a primary key, but SQL disagreed, since two rows have the same value for that attribute. |
|
3140 |
|
|
|
3141 |
|
@problem:repetition -- Solution to Q. -.# |
|
3142 |
|
: Once the table is empty, `X` indeed is a primary key, so SQL stops complaining and let us make it a primary key. |
3127 |
3143 |
|
|
3128 |
|
~~~{.bash} |
|
3129 |
|
ERROR 1062 (23000): Duplicate entry 'Train' for key 'PRIMARY' |
|
3130 |
|
~~~ |
|
|
3144 |
|
@problem:repetition -- Solution to Q. -.# |
|
3145 |
|
~ |
|
3146 |
|
We can't introduce the same value twice: |
3131 |
3147 |
|
|
3132 |
|
which is, by the way, exactly the same error message as when we tried to add the primary key in the first place! |
|
3133 |
|
|
|
|
3148 |
|
~~~{.sqlmysql} |
|
3149 |
|
INSERT INTO EXAMPLE VALUES('Train', 4); |
|
3150 |
|
~~~ |
|
3151 |
|
|
|
3152 |
|
returns, the second time: |
|
3153 |
|
|
|
3154 |
|
~~~{.bash} |
|
3155 |
|
ERROR 1062 (23000): Duplicate entry 'Train' for key 'PRIMARY' |
|
3156 |
|
~~~ |
|
3157 |
|
|
|
3158 |
|
which is, by the way, exactly the same error message as when we tried to add the primary key in the first place! |
3134 |
3159 |
|
|
3135 |
3160 |
--- |
--- |
3136 |
3161 |
|
|
|
... |
... |
Exercise +.# |
3998 |
4023 |
|
|
3999 |
4024 |
: What is insertion anomaly? Give an example. |
: What is insertion anomaly? Give an example. |
4000 |
4025 |
|
|
4001 |
|
Exercise +.# |
|
|
4026 |
|
Exercise +.#deletion |
4002 |
4027 |
|
|
4003 |
4028 |
: What is deletion anomaly? Is it a desirable feature? |
: What is deletion anomaly? Is it a desirable feature? |
4004 |
4029 |
|
|
|
... |
... |
Solution +.# |
4244 |
4269 |
|
|
4245 |
4270 |
Solution +.# |
Solution +.# |
4246 |
4271 |
|
|
4247 |
|
: A delete anomaly exists when certain attributes are lost because of the deletion of other attributes. |
|
|
4272 |
|
: A delete anomaly exists when certain attributes are lost because of the deletion of other attributes. It is not desirable, since it can lead to the loss of information. |
4248 |
4273 |
|
|
4249 |
4274 |
Solution +.# |
Solution +.# |
4250 |
4275 |
|
|
|
... |
... |
Because it avoids redundancy. |
4360 |
4385 |
|
|
4361 |
4386 |
## Problems {-} |
## Problems {-} |
4362 |
4387 |
|
|
4363 |
|
Problem (Design for your Professor) +.#designforprof |
|
|
4388 |
|
Problem (Design for your professor) +.#designforprof |
4364 |
4389 |
~ |
~ |
4365 |
4390 |
|
|
4366 |
4391 |
Your professor designed the following relational model at some point in his career, to help him organizing his exams and the students grades: |
Your professor designed the following relational model at some point in his career, to help him organizing his exams and the students grades: |
|
... |
... |
Problem (Reading the MOVIES database ER schema) +.#movie |
4413 |
4438 |
|
|
4414 |
4439 |
--- |
--- |
4415 |
4440 |
|
|
4416 |
|
Problem (ER diagram for Car Insurance) +.#car-insurance |
|
|
4441 |
|
Problem (ER diagram for car insurance) +.#car-insurance |
4417 |
4442 |
~ |
~ |
4418 |
4443 |
|
|
4419 |
4444 |
Draw the ER diagram for the following situation: |
Draw the ER diagram for the following situation: |
|
... |
... |
Problem (ER diagram for Car Insurance) +.#car-insurance |
4426 |
4451 |
|
|
4427 |
4452 |
--- |
--- |
4428 |
4453 |
|
|
4429 |
|
Problem (ER-to-Relation mapping for Car Insurance) +.#ERtoREL |
|
|
4454 |
|
Problem (ER-to-Relation mapping for car insurance) +.#ERtoREL |
4430 |
4455 |
~ |
~ |
4431 |
4456 |
|
|
4432 |
4457 |
Apply the ER-to-Relation mapping to your ER diagram from the previous problem. |
Apply the ER-to-Relation mapping to your ER diagram from the previous problem. |
4433 |
4458 |
|
|
4434 |
4459 |
--- |
--- |
4435 |
4460 |
|
|
4436 |
|
Problem (ER-to-Relation mapping for Country) + .# |
|
|
4461 |
|
Problem (ER-to-Relation mapping for Country) +.#ERtoRELCountry |
4437 |
4462 |
~ |
~ |
4438 |
4463 |
|
|
4439 |
4464 |
Consider the following E.R. schema: |
Consider the following E.R. schema: |
|
... |
... |
Based on the given primary key, is this relation in 1NF, 2NF, or 3NF? Why or why |
4472 |
4497 |
|
|
4473 |
4498 |
--- |
--- |
4474 |
4499 |
|
|
4475 |
|
Problem (Normalizing the FLIGHT relation) +.# |
|
|
4500 |
|
Problem (Normalizing the FLIGHT relation) +.#NormalizeFlight |
4476 |
4501 |
~ |
~ |
4477 |
4502 |
|
|
4478 |
4503 |
Consider the following relation: |
Consider the following relation: |
4479 |
4504 |
|
|
4480 |
4505 |
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) |
4481 |
4506 |
|
|
4482 |
|
A tuple in the FLIGHT relation contains information about an airplane flight: the airports of departure and arrival, the airline carrier, the number of the flight, its time of departure, the headquarter of the company chartering the flight, the name of the pilot(s), and the time zone difference between the departure and arrival airports. |
|
|
4507 |
|
A tuple in the FLIGHT relation contains information about an airplane flight: the airports of departure and arrival, the airline carrier, the number of the flight, its time of departure, the headquarter of the company chartering the flight, the name of the pilot(s), and the time zone difference between the departure and afrrival airports. |
4483 |
4508 |
|
|
4484 |
4509 |
The "Pilot" attribute is multi-valued (so that between $1$ and $4$ pilot's names can be stored in it). |
The "Pilot" attribute is multi-valued (so that between $1$ and $4$ pilot's names can be stored in it). |
4485 |
4510 |
Given an airline and a flight number, one can determine the departure and arrival airports, as well as the date and hour and the pilot(s). |
Given an airline and a flight number, one can determine the departure and arrival airports, as well as the date and hour and the pilot(s). |
|
... |
... |
Normalize the relations to 2NF and 3NF. Show all relations at each stage (2NF an |
4537 |
4562 |
|
|
4538 |
4563 |
--- |
--- |
4539 |
4564 |
|
|
4540 |
|
Problem (Normal Form of the BOOK relation) +.#book |
|
|
4565 |
|
Problem (Normal form of the BOOK relation) +.#book |
4541 |
4566 |
~ |
~ |
4542 |
4567 |
|
|
4543 |
4568 |
Consider the following relation for published books: |
Consider the following relation for published books: |
|
... |
... |
Author\_name | → | Author\_affil |
4560 |
4585 |
|
|
4561 |
4586 |
--- |
--- |
4562 |
4587 |
|
|
4563 |
|
Problem (From Business Statements to E.R. Diagram -- UNIVERSITY) +.#BusinessToEr |
|
|
4588 |
|
Problem (From business statements to E.R. diagram -- UNIVERSITY) +.#BusinessToEr |
4564 |
4589 |
~ |
~ |
4565 |
4590 |
|
|
4566 |
4591 |
Consider the following requirements for a UNIVERSITY database, used to keep track of students' transcripts. |
Consider the following requirements for a UNIVERSITY database, used to keep track of students' transcripts. |
|
... |
... |
Problem (From Business Statements to E.R. Diagram -- UNIVERSITY) +.#BusinessToEr |
4578 |
4603 |
|
|
4579 |
4604 |
--- |
--- |
4580 |
4605 |
|
|
4581 |
|
Problem (From UML to Relational Model -- DRIVER) +.#UMLtoRELDriver |
|
|
4606 |
|
Problem (From UML to relational model -- DRIVER) +.#UMLtoRELDriver |
4582 |
4607 |
~ |
~ |
4583 |
4608 |
|
|
4584 |
4609 |
Consider the UML diagram below, and convert it to the relational model. |
Consider the UML diagram below, and convert it to the relational model. |
|
... |
... |
Problem (From UML to Relational Model -- DRIVER) +.#UMLtoRELDriver |
4588 |
4613 |
|
|
4589 |
4614 |
--- |
--- |
4590 |
4615 |
|
|
4591 |
|
Problem (Normal Form of the CONTACT Relation) +.# |
|
|
4616 |
|
Problem (Normal form of the CONTACT relation) +.#NormalizeContact |
4592 |
4617 |
~ |
~ |
4593 |
4618 |
|
|
4594 |
4619 |
Consider the relation |
Consider the relation |
|
... |
... |
Give the relations, their primary keys, and functional dependencies for both ste |
4611 |
4636 |
|
|
4612 |
4637 |
--- |
--- |
4613 |
4638 |
|
|
4614 |
|
Problem (CONSULTATION Relation: Justification, Primary Key and Normal Form) +.#consultation |
|
|
4639 |
|
Problem (CONSULTATION relation: justification, primary key and normal form) +.#consultation |
4615 |
4640 |
~ |
~ |
4616 |
4641 |
|
|
4617 |
4642 |
Consider the relation |
Consider the relation |
|
... |
... |
Normalize it to the third normal form if necessary. |
4635 |
4660 |
|
|
4636 |
4661 |
--- |
--- |
4637 |
4662 |
|
|
4638 |
|
Problem (From E.R. to Relational Schema and UML class diagram -- CAR\_INFO) +.#carinfo |
|
|
4663 |
|
Problem (From E.R. to relational schema and UML class diagram -- CAR\_INFO) +.#carinfo |
4639 |
4664 |
~ |
~ |
4640 |
4665 |
|
|
4641 |
4666 |
Consider the following E.R. schema for the CAR\_INFO database: |
Consider the following E.R. schema for the CAR\_INFO database: |
|
... |
... |
Problem (Discovering MySQL Workbench) +.#mysqlw |
4661 |
4686 |
#. Now, click on the box "Local instance 3306", and enter your password. A new tab appears, you can see the list of schemas in the bottom part of the left panel. |
#. Now, click on the box "Local instance 3306", and enter your password. A new tab appears, you can see the list of schemas in the bottom part of the left panel. |
4662 |
4687 |
#. Click on "Database", and then on "Reverse Engineering" (or hit `ctrl` + `r`), click on "next", enter your password, and click on "next". You should see the list of the schemas stored in your database. Select one (any one, we are just exploring the functionalities at that point), click on "next", and then click on "execute", "next", and "close". |
#. Click on "Database", and then on "Reverse Engineering" (or hit `ctrl` + `r`), click on "next", enter your password, and click on "next". You should see the list of the schemas stored in your database. Select one (any one, we are just exploring the functionalities at that point), click on "next", and then click on "execute", "next", and "close". |
4663 |
4688 |
#. You're back on the previous view, but you should now see "E.E.R. diagram" on the top of the middle panel. Click on "E.E.R. diagram" twice, scroll down if needed, and you should see the E.E.R. diagram. |
#. You're back on the previous view, but you should now see "E.E.R. diagram" on the top of the middle panel. Click on "E.E.R. diagram" twice, scroll down if needed, and you should see the E.E.R. diagram. |
4664 |
|
#. This diagram isn't exaclty an E.R. diagram, and it's not a U.M.L. diagram either. Yet, you should still be able to understand parts of it, and should try to modify it. Make some relations mandatory, change their name, add an attribute, change the name of another, insert a couple of elements in an entity, add a row in a table, etc. Make sure you understand the meaning of the lines between the entities. |
|
|
4689 |
|
#. This diagram isn't exaclty an E.R. diagram, and it is not a U.M.L. diagram either. Yet, you should still be able to understand parts of it, and should try to modify it. Make some relations mandatory, change their name, add an attribute, change the name of another, insert a couple of elements in an entity, add a row in a table, etc. Make sure you understand the meaning of the lines between the entities. |
4665 |
4690 |
#. Once you're done, try to "Forward Engineer" by hitting "Ctrl" + "G". Click on "next" twice, enter your password, click on lick on "next" once more, and you should see the `SQL` code needed to produce the table you just designed using the graphical tool. |
#. Once you're done, try to "Forward Engineer" by hitting "Ctrl" + "G". Click on "next" twice, enter your password, click on lick on "next" once more, and you should see the `SQL` code needed to produce the table you just designed using the graphical tool. |
4666 |
4691 |
|
|
4667 |
4692 |
--- |
--- |
4668 |
4693 |
|
|
4669 |
|
Problem (Using MySQL Workbench's Reverse Engineering) +.#reverseeng |
|
|
4694 |
|
Problem (Using MySQL Workbench's reverse engineering) +.#reverseeng |
4670 |
4695 |
~ |
~ |
4671 |
4696 |
|
|
4672 |
4697 |
*This problem requires you to have successfully completed @problem:mysqlw and @problem:UMLtoRELDriver.* |
*This problem requires you to have successfully completed @problem:mysqlw and @problem:UMLtoRELDriver.* |
|
... |
... |
Problem (Using MySQL Workbench's Reverse Engineering) +.#reverseeng |
4679 |
4704 |
|
|
4680 |
4705 |
--- |
--- |
4681 |
4706 |
|
|
4682 |
|
Problem (From Business Statements to Dependencies -- KEYBOARD) +.#BusinessToDependecies-KEYBOARD |
|
|
4707 |
|
Problem (From business statements to dependencies -- KEYBOARD) +.#BusinessToDependecies-KEYBOARD |
4683 |
4708 |
~ |
~ |
4684 |
4709 |
|
|
4685 |
4710 |
This exercise asks you to convert business statements into dependencies. |
This exercise asks you to convert business statements into dependencies. |
|
... |
... |
Problem (From Business Statements to Dependencies -- KEYBOARD) +.#BusinessToDepe |
4699 |
4724 |
|
|
4700 |
4725 |
--- |
--- |
4701 |
4726 |
|
|
4702 |
|
Problem (Reverse Engineering by Hand) +.#Reverse-Engineering-ACTOR |
|
|
4727 |
|
Problem (Reverse engineering by hand) +.#Reverse-Engineering-ACTOR |
4703 |
4728 |
~ |
~ |
4704 |
4729 |
|
|
4705 |
4730 |
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: |
|
... |
... |
Problem (Reverse Engineering by Hand) +.#Reverse-Engineering-ACTOR |
4707 |
4732 |
 |
 |
4708 |
4733 |
|
|
4709 |
4734 |
|
|
4710 |
|
## Solution to Selected Problems |
|
|
4735 |
|
## Solution to Selected Problems {-} |
4711 |
4736 |
|
|
4712 |
4737 |
Solution to [%D %n (%T)](#problem:movie) |
Solution to [%D %n (%T)](#problem:movie) |
4713 |
4738 |
~ |
~ |
|
... |
... |
Exercise +.# |
5144 |
5169 |
|
|
5145 |
5170 |
: What is a prepared statement? |
: What is a prepared statement? |
5146 |
5171 |
|
|
5147 |
|
Exercise +.# |
|
|
5172 |
|
Exercise +.#ErrorsInCode |
5148 |
5173 |
~ |
~ |
5149 |
5174 |
|
|
5150 |
5175 |
In the code below, there are five errors between line 13 and line 32. |
In the code below, there are five errors between line 13 and line 32. |
|
... |
... |
Solution +.# |
5198 |
5223 |
: A prepared statement is a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency. |
: A prepared statement is a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency. |
5199 |
5224 |
|
|
5200 |
5225 |
Solution +.# |
Solution +.# |
|
5226 |
|
~ |
|
5227 |
|
|
|
5228 |
|
The errors are the following: |
|
5229 |
|
|
|
5230 |
|
- l. 13: The query is incorrect (`WHERE`{.sqlmysql} should come before `FROM`{.sqylmysql}), |
|
5231 |
|
- TO be written |
5201 |
5232 |
|
|
5202 |
|
: List errors in program TO DO. |
|
5203 |
5233 |
<!-- |
<!-- |
5204 |
5234 |
% |
% |
5205 |
5235 |
%METADATA |
%METADATA |
|
... |
... |
Problem (MySQL's batch mode and HW_EBOOKSHOP.sql) +.#mysql_batch |
5278 |
5308 |
|
|
5279 |
5309 |
--- |
--- |
5280 |
5310 |
|
|
5281 |
|
Problem +.# (Fist Database Application) |
|
|
5311 |
|
Problem (First Database Application) +.#db_application |
5282 |
5312 |
~ |
~ |
5283 |
5313 |
|
|
5284 |
5314 |
This exercise supposes you successfully completed @problem:mysql_batch. |
This exercise supposes you successfully completed @problem:mysql_batch. |
|
... |
... |
Problem +.# (Fist Database Application) |
5310 |
5340 |
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. |
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. |
5311 |
5341 |
Try to execute `FirstProg` without that flag, and see what happens. |
Try to execute `FirstProg` without that flag, and see what happens. |
5312 |
5342 |
|
|
5313 |
|
Solution +.# |
|
|
5343 |
|
Problem +.# (Advanced Java Programming) |
|
5344 |
|
~ |
|
5345 |
|
|
|
5346 |
|
Read, execute, break, edit, compile, patch, hack and (most importantly) understand the following program: |
|
5347 |
|
|
|
5348 |
|
```{.sqlmysql .numberLines include=code/java/AdvancedProg.java} |
|
5349 |
|
``` |
|
5350 |
|
|
|
5351 |
|
## Solution to Selected Problems {-} |
|
5352 |
|
|
|
5353 |
|
Solution to @problem:db_application |
5314 |
5354 |
~ |
~ |
5315 |
5355 |
|
|
|
5356 |
|
Without the flag, we obtain: |
|
5357 |
|
|
5316 |
5358 |
~~~{.bash} |
~~~{.bash} |
5317 |
5359 |
$ java FirstProg |
$ java FirstProg |
5318 |
5360 |
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/HW_EBOOKSHOP |
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/HW_EBOOKSHOP |
|
... |
... |
Solution +.# |
5321 |
5363 |
at FirstProg.main(FirstProg.java:9) |
at FirstProg.main(FirstProg.java:9) |
5322 |
5364 |
~~~ |
~~~ |
5323 |
5365 |
|
|
5324 |
|
Problem +.# (Advanced Java Programming) |
|
5325 |
|
~ |
|
5326 |
|
|
|
5327 |
|
Read, execute, break, edit, compile, patch, hack and (most importantly) understand the following program: |
|
5328 |
|
|
|
5329 |
|
```{.sqlmysql .numberLines include=code/java/AdvancedProg.java} |
|
5330 |
|
``` |
|
5331 |
|
|
|
5332 |
5366 |
--- |
--- |
5333 |
5367 |
|
|
5334 |
5368 |
# A Bit About Security |
# A Bit About Security |
|
... |
... |
Less join (resources expensive), but need more storage, more functions, to subst |
5808 |
5842 |
|
|
5809 |
5843 |
## Exercises {-} |
## Exercises {-} |
5810 |
5844 |
|
|
5811 |
|
Exercise +.# |
|
|
5845 |
|
Exercise +.#polyglot |
5812 |
5846 |
|
|
5813 |
5847 |
: What is polyglot persistence? |
: What is polyglot persistence? |
5814 |
5848 |
Is it useful? |
Is it useful? |
5815 |
5849 |
|
|
5816 |
|
Exercise +.# |
|
|
5850 |
|
Exercise +.#schemaless |
5817 |
5851 |
|
|
5818 |
5852 |
: What does it mean to be "schemaless"? |
: What does it mean to be "schemaless"? |
5819 |
5853 |
What does it imply? |
What does it imply? |
5820 |
5854 |
|
|
5821 |
|
Exercise +.# |
|
|
5855 |
|
Exercise +.#denormalization |
5822 |
5856 |
|
|
5823 |
5857 |
: What is denormalization? When could that be useful? |
: What is denormalization? When could that be useful? |
5824 |
5858 |
|
|
5825 |
|
Exercise +.# |
|
|
5859 |
|
Exercise +.#mismatch |
5826 |
5860 |
|
|
5827 |
5861 |
: What is the (object-relational) impedance mismatch? Is it an issue that can't be overcome? |
: What is the (object-relational) impedance mismatch? Is it an issue that can't be overcome? |
5828 |
5862 |
|
|
5829 |
5863 |
## Solution to Exercises {-} |
## Solution to Exercises {-} |
5830 |
5864 |
|
|
5831 |
5865 |
Solution +.# |
Solution +.# |
5832 |
|
~ To be written. |
|
|
5866 |
|
~ It is the task of picking the right DBMS for the task, and to involve multiple DBMS in a single application. Yes, it is useful. |
|
5867 |
|
Per [wikipedia](https://en.wikipedia.org/wiki/Polyglot_persistence), "Polyglot persistence is the concept of using different data storage technologies to handle different data storage needs within a given software application.". |
|
5868 |
|
|
|
5869 |
|
Solution +.# |
|
5870 |
|
~ That a table can contain documents, or tuples, with different attributes. |
|
5871 |
|
It implies more responsabilities. |
|
5872 |
|
|
|
5873 |
|
Solution +.# |
|
5874 |
|
~ To duplicate data about other entities in some entities. |
|
5875 |
|
It is useful when joining is expensive. |
|
5876 |
|
|
|
5877 |
|
Solution +.# |
|
5878 |
|
~ Data-base and object-oriented principles are different and it requires work to make them work together. |
|
5879 |
|
This correspondance, or matching, can be implemented in the application, or lead to the design of new DBMS. |
5833 |
5880 |
|
|
5834 |
5881 |
# References {#ref} |
# References {#ref} |