List of commits:
Subject Hash Author Date (UTC)
Finished correcting Chapter 1. Added a preamble and tweaked a lot (math, references, titles of subsubsections, etc.) acdd1bb083cd6985763afd9b77e1b7995dc1ebd7 aubert@math.cnrs.fr 2018-05-22 07:02:51
Cleaning latex dependencies: removed colors, fonts, etc., added header and footer. b4c1232d6091e12bc2cdf038529921143a55b758 aubert@math.cnrs.fr 2018-05-22 05:06:04
Putting all latex material aside, changing class. e43fb54e6ecb0ec4669ed16feba940b47ee3e35c aubert@math.cnrs.fr 2018-05-21 15:14:48
Gathering references, fixing many small typos, adding references in bib. 4f5281a0770f07d2f48f36462ab25f0a3945f4e0 aubert@math.cnrs.fr 2018-05-21 04:54:59
Finished merging exercises into lecture notes! 36e1f79080a8ac56837632bcac2c119fc3efa24f aubert@math.cnrs.fr 2018-05-20 18:50:54
Almost done with the homeworks! f82824a2ca9769a0ecf8fc75d6bf1cae1ef3c7a4 aubert@math.cnrs.fr 2018-05-20 05:23:43
Travail sur intégration des exos dans les notes. c5a6676f85953c7bec9bb2de99bed3552ecf52af aubert@math.cnrs.fr 2018-05-20 05:04:30
Les exercises compilent :-) d4230fce5210d1d8905a42d11634e49e82217541 aubert@math.cnrs.fr 2018-05-19 19:09:56
Progrès sur exercises. 40dd86cdc624209a2282d4437921f46325c2cfa1 aubert@math.cnrs.fr 2018-05-19 18:34:07
Working on exercises. 42549c8d28249f1ecf85dd21f6d004ae63c533ad aubert@math.cnrs.fr 2018-05-19 05:43:04
Working on incorporating the exercises. f32148faeb8b11425fe9a57770db9bc85ae02c34 aubert@math.cnrs.fr 2018-05-19 04:51:37
working on sum for exo. 5fb55d2c31b06161ade3a7cae707485d8c02447a aubert@math.cnrs.fr 2018-05-18 21:12:56
Adding various small notes. 5eac6e0b19441fca9a3f70f239d5cbbc4d769976 aubert@math.cnrs.fr 2018-05-17 20:37:14
First pass over! ea1e4f6ddabd91287d10384dc8f28565a762526b aubert@math.cnrs.fr 2018-05-17 19:26:28
Working on notes on normal forms. 58bfcedc6bd6336592a5f93a43ffc0aaf64fa8f6 aubert@math.cnrs.fr 2018-05-16 16:40:12
Initial commit, draft of lecture notes. 200d2739bca881d60c7c9381b16f5a4d6384ff29 aubert@math.cnrs.fr 2018-05-16 15:35:36
Commit acdd1bb083cd6985763afd9b77e1b7995dc1ebd7 - Finished correcting Chapter 1. Added a preamble and tweaked a lot (math, references, titles of subsubsections, etc.)
Author: aubert@math.cnrs.fr
Author date (UTC): 2018-05-22 07:02
Committer name: aubert@math.cnrs.fr
Committer date (UTC): 2018-05-22 07:02
Parent(s): b4c1232d6091e12bc2cdf038529921143a55b758
Signer:
Signing key:
Signing status: N
Tree: 13769154497f3ba0fcdd9bceedd21f02b6509a21
File Lines added Lines deleted
notes/00_sum.md 362 361
File notes/00_sum.md changed (mode: 100644) (index d4587ce..5d20a30)
... ... pandoc 00_sum.md --pdf-engine=xelatex --toc --filter pandoc-citeproc --top-level
24 24 --> -->
25 25
26 26
27 References are listed at the very end of this document, [here](#ref).
27 # Preamble {-}
28 28
29 We will be using, as a textbook, [@Textbook6; @Textbook7], you can take one edition or the other.
29 ## How to use this guide {-}
30 30
31 A
31 These lecture notes are written in an elusive style: their are a support for the explanation I will be making at the board.
32 They are not designed to be self-contained: they are rather a list of topics and reminders, along with handy examples, code and drawings.
33 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.
34
35 On top of the notes, you will find in this document:
36
37 - [References, at the very end of this document](#ref)
38 - and for each chapter,
39 - A list of additional resources, as well as pointers to your textbook (in its sixth, [@Textbook6], and seventh, [@Textbook7], editions),
40 - A list of questions,
41 - Solution to those questions,
42 - A list of problem,
43 - Sometimes, solution to some of those problems.
44
45 Any feedback is greatly appreciated.
46
47 For information purposes, a
32 48
33 49 --- ---
34 50
35 marks the separation between two lectures.
51 marks the (usual) separation between two lectures.
36 52
37 53 The syllabus is at <http://spots.augusta.edu/caubert/db/>. The syllabus is at <http://spots.augusta.edu/caubert/db/>.
38 54
39 All section starts with a list of ressources and ends with exercises, solution to the exercises, and problems.
55 ## Previous exams {-}
56
57 To give you a sense of what you will be asked to do during the exams, please find below a description of the exams given previous semesters.
58
59 ### Fall 2017 {-}
60
61 - Exam #1:
62 - Exam #2:
63 - Final:
64
65 ### Spring 2018 {-}
66
67 - Exam #1:
68 - Exam #2:
69 - Final:
70
71 ## Typesetting {-}
72
73 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).
74
75 To compile it, you will need
40 76
41 ## TODO
77 - To clone the source, using [git](https://git-scm.com/), and
42 78
43 - Color support?
79 ~~~{.bash}
80 git clone https://rocketgit.com/user/caubert/CSCI_3410
81 ~~~
82
83 - To install pandoc, cf. <http://pandoc.org/installing.html>
84 - To produce `pdf` output, you will need LaTeX (the "Installing pandoc" page contains instructions)
85 - To install pandoc-numbering. cf. <https://github.com/chdemko/pandoc-numbering#installation>
86
87 ## Various ressources {-}
88
89 On top of the [references](references) and of the "resources" listed at the beginning of each chapter, I consulted while writing those notes:
90
91 - <https://www.1keydata.com/datawarehousing/data-modeling-levels.html>
92 - <https://en.wikipedia.org/wiki/Unified_Modeling_Language>
93 - <https://creately.com/blog/diagrams/class-diagram-relationships/>
94 - <https://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html>
95
96 ## Personal todo {-}
97
98 - Add list of problems for exams.
44 99 - List drawing and sort them, then add drawing in Latex, find a way for a separate compilation. - List drawing and sort them, then add drawing in Latex, find a way for a separate compilation.
45 100 - Make separation of lecture in another way. - Make separation of lecture in another way.
46 101 - Add example environment? - Add example environment?
47 102 - Add title to problem, and solve their naming / referencing issue. - Add title to problem, and solve their naming / referencing issue.
48 103
49
50
51 104 # Introduction # Introduction
52 105
53 106 ## Ressources ## Ressources
54 107
55 [@Textbook6, ch. 1.1--1.6]
56
57 [@Textbook7]
108 - [@Textbook6, ch. 1.1--1.6]
109 - [@Textbook7, ch. 1.1--1.6]
58 110
59 111
60 112 ## Database ## Database
61 113
62 **A collection of related data** = data (= info, can be anything, really) + management (= logical, through **D**ata**b**ase **M**anagement **S**ystem).
114 A database (DB) is **a collection of related data**.
115 ~ Data (= information, can be anything, really) + management (= logical organization of the data), through **D**ata**b**ase **M**anagement **S**ystem).
63 116
64 a. Represent a mini-world / Universe of Disclosure (UoD).
65 b. Logically coherent, with meaning.
117 a. Represent a mini-world, a Universe of Disclosure (UoD).
118 b. Logically coherent, with a meaning.
66 119 c. Populated for a purpose. c. Populated for a purpose.
67 120
68 ## DBMS
121 ![A simplified database environment](before/book_screen/fig1.1.jpeg){#fig:lab}
122
123 Refer to the ["A simplified database environment"](#fig:lab) figure , where
124
125 - The program can be written in any language, be a web interface, etc.
126 - Most DBMS software include a Command-Line-Interface
127 - Sometimes, meta-data and data are closer than pictured (you can have "self-describing meta-data", that is, they can't be distinguished).
69 128
70 General purpose software
129 ## Database Management System (DBMS)
130
131 A DBMS is a *general purpose* software that is used to
71 132
72 133 a. Define (= datatype, constraints, structures, etc.) a. Define (= datatype, constraints, structures, etc.)
73 134 b. Construct (= storing the data) b. Construct (= storing the data)
74 135 c. Manipulate (= query, update, etc.) c. Manipulate (= query, update, etc.)
75 136 d. Share (=among users, softwares.) d. Share (=among users, softwares.)
76 137
77 ![A simplified database environment](before/book_screen/fig1.1.jpeg)
138 You can think of a tool to
139
140 a. specify a storage unit,
141 b. fill it,
142 c. allow to change its content, as well as its organization,
143 d. allow multiple users to access it at the same time.
78 144
79 145 ## Subtasks ## Subtasks
80 146
81 a. Organization (DB designer, focus here)
82 b. Modification, retrieval (end-user, several levels)
83 c. Administration (DB administrator)
84 d. ( + Software engineer, web developer, to help users).
147 a. Organization (DB designer)
148 b. Modification, retrieval (end-user)
149 c. Maintenance (DB administrator)
150 d. Software engineer, web developer, programers, …
151
152 The focus will be on design, but we will have to do a little bit of everything.
85 153
86 154 ## Design ## Design
87 155
88 ![The cycle of design](before/paper_screen/cycle_of_design.jpeg)
156 ![The cycle of design](before/paper_screen/cycle_of_design.jpeg){#fig:cycle}
157
158 Refer to the ["The cycle of design" figure](#fig:cycle}.
159
160 Add a line from SQL to Program, a line from NoSQL to CLI.
161 Make the arrow from Bus. Statements to NoSQL dashed?
89 162
90 163 --- ---
91 164
 
... ... Why are the files separated like that? Why don't we store the section with the c
149 222
150 223 But need to be carefull about **consistency** / **referential integrity**. But need to be carefull about **consistency** / **referential integrity**.
151 224
152 ### How is a Database Conceived?
225 ### How is a database conceived?
153 226
154 227 - Specification and analysis. "Each student number will be unique, but they can have the same name. We want to access the letter grade, but not the numerical grade", etc. - Specification and analysis. "Each student number will be unique, but they can have the same name. We want to access the letter grade, but not the numerical grade", etc.
155 228 - Conceptual design - Conceptual design
156 229 - Logical design - Logical design
157 230 - Physical design - Physical design
158 231
159 ---------------------- ------------ --------- ----------
160 Feature Conceptual Logical Physical
161 Entity Names ✔ ✔  
162 Entity Relationships ✔ ✔  
163 Attributes   ✔  
164 Primary Keys   ✔ ✔
165 Foreign Keys   ✔ ✔
166 Table Names     ✔
167 Column Names     ✔
168 Column Data Types     ✔
169 ---------------------- ------------ --------- ----------
170
171
172 232 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.
173 233 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.
234 It's easier to move things around early in the conception.
174 235
175 236
176 237 ## Characteristics of the Database Approach ## Characteristics of the Database Approach
177 238
178 a. Database = data + complete definiton / description of the structure and constraints. ⇒ (Data + Meta-data), or Self-describing data.
239 a. A database is more than just data: it also contains a complete description of the structure and constraints. ⇒ We have Data & Meta-data (or self-describing data).
179 240 b. Data-abstraction: DBMS provides a conceptual representation, and hides implementation details. b. Data-abstraction: DBMS provides a conceptual representation, and hides implementation details.
180 1. Program-data independence: changing the database doesn't require to change the DBMS. Compare with changing a custom data-type in a program.
181 2. Program-operation independence: an operation has an interface (or signature) and an implementation (or method)
182 c. Support of Multiple Viems of the Data: view = subset of the database or virtual data.
183 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).
241 1. Program-data independence: changing the database doesn't require to change the DBMS. Compare with changing a custom data-type in a program.
242 2. Program-operation independence: an operation has an interface (or signature) and an implementation (or method)
243 c. Support of multiple views of the data: view is a subset of the database, or virtual data.
244 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): [ACID principles](#sec:AcidVsCAP), that we will study later on.
184 245
185 246 ## Exercises ## Exercises
186 247
 
... ... Exercise +.#
193 254 Exercise +.# Exercise +.#
194 255 ~ What is the difference between a database and the meta-data of the database? ~ What is the difference between a database and the meta-data of the database?
195 256
196
197 257 Exercise +.# Exercise +.#
198 ~ In a DBMS environment, do I have to change my program if I want to change the structure of my data?
258 ~ Do I have to change my DBMS if I want to change the structure of my data?
199 259
200 260 Exercise +.# Exercise +.#
201 261 ~ What is a virtual data? How can I access it? ~ What is a virtual data? How can I access it?
202 262
203
204 263 Exercise +.# Exercise +.#
205 264 ~ Why do DBMS include concurrency control? ~ Why do DBMS include concurrency control?
206 265
207 266 ## Solution to Exercises ## Solution to Exercises
208 267
209
210 268 Solution +.# Solution +.#
211 269 ~ No, because it lacks a logical structure. ~ No, because it lacks a logical structure.
212 270
213
214 271 Solution +.# Solution +.#
215 272 ~ The conceptual design. ~ The conceptual design.
216 273
217
218 274 Solution +.# Solution +.#
219 ~ The data base contains the actual data, and the meta-data describes the organization of the database.
220
275 ~ The data is the information we want to store, the meta-data is its organization, how we are going to store it.
221 276
222 277 Solution +.# Solution +.#
223 ~ No, data and programs are indepent.
224
278 ~ No, data and programs are indepent. But actually, this is true only if the model doesn't change: shifting to a "less structured model", e.g., one of the NoSQL models, can require to change the DBMS.
225 279
226 280 Solution +.# Solution +.#
227 281 ~ It is a set of information that is derived from the database but not directly stored in it. It is accessed through queries. ~ It is a set of information that is derived from the database but not directly stored in it. It is accessed through queries.
228 282
229
230 283 Solution +.# Solution +.#
231 ~ To ensure that several users trying to update the same data will do so in a controlled manner.
284 ~ To ensure that several users trying to update the same data will do so in a controlled manner. To avoid inconsistency.
232 285
233 ## Problems
286 ## Problem
234 287
235 ### Campus Database
288 Problem (Campus Database) +.#campus
236 289
237 290 We want to define a `CAMPUS` database organized into three files as follows: We want to define a `CAMPUS` database organized into three files as follows:
238 291
 
... ... We want to define a `CAMPUS` database organized into three files as follows:
241 294 - A `PROF` file storing the name, phone number, email and room number where the office is located for each professor. - A `PROF` file storing the name, phone number, email and room number where the office is located for each professor.
242 295
243 296 Question -.# Question -.#
244 ~ Complete the following database catalog corresponding to the `CAMPUS` database (having a look at Figure 1.3, in your textbook, can be of some help):
245
246 **RELATIONS**
247
248 | Relation\_name | No\_of\_columns |
249 --- | ---
250 `BUILDING` | $2$
251 `ROOM` | $3$
252 `PROF` | $4$
253
254
255 **COLUMNS**
256
257
258 | Column\_name | Data\_type | Belongs\_to\_relation |
259 | --- | --- | ---
260 Building\_Name | Character($30$) | `Building`
261 GPS | Double ($1$) | `Building`
262 Building\_Name | Character($30$) | `ROOM`
263 Room\_Number | Integer($1$) | `ROOM`
264 Floor | Integer ($1$) | `ROOM`
265 Prof\_Name | Character ($30$) | `PROF`
266 Phone | Integer ($10$) | `PROF`
267 Email | Character ($30$) | `PROF`
268 Room\_Number | Integer ($1$) | `PROF`
269
297 ~ Look at [@Textbook6, Figure 1.3], or [@Textbook7, Figure 1.3] to understand how to write a database catalog. 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.
298 Then, write the database catalog corresponding to the `CAMPUS` database.
270 299
271 300 Question -.# Question -.#
272 ~ Invent data for such a database, with $2$ buildings, $3$ rooms and $2$ professors.
301 ~ Invent data for such a database, with two buildings, three rooms and two professors.
273 302
274 303 Question -.# Question -.#
275 304 ~ Answer the following, assuming all the knowledge you have of the situation comes from the `CAMPUS` database, which is an up-to-date and accurate representation of its miniworld: ~ Answer the following, assuming all the knowledge you have of the situation comes from the `CAMPUS` database, which is an up-to-date and accurate representation of its miniworld:
 
... ... Question -.#
284 313 8. Can you tell which professor has the highest evaluations? 8. Can you tell which professor has the highest evaluations?
285 314
286 315
287 ---
316 ## Solution to @problem:campus
317
318 The database catalog should be similar to the following:
319
320 **RELATIONS**
288 321
322 | Relation\_name | No\_of\_columns |
323 --- | ---
324 `BUILDING` | 3
325 `ROOM` | 3
326 `PROF` | 4
327
328 **COLUMNS**
329
330 | Column\_name | Data\_type | Belongs\_to\_relation |
331 | --- | --- | ---
332 Building\_Name | Character(30) | `Building`
333 GPSLat | Decimal(9,6) | `Building`
334 GPSLon | Decimal(9,6) | `Building`
335 Building\_Name | Character(30) | `ROOM`
336 Room\_Number | Integer(1) | `ROOM`
337 Floor | Integer (1) | `ROOM`
338 Prof\_Name | Character (30) | `PROF`
339 Phone | Integer (10) | `PROF`
340 Email | Character (30) | `PROF`
341 Room\_Number | Integer (1) | `PROF`
342
343 For the data, you could have:
344
345 `(Allgood Hall, 33.47520, -82.02503)`, `(Institut Galilé, 48.959001, 2.339999)`
346 `(Allgood Hall, 128, 1)`, `(Institut Galilé, 205, 3)`, `(Allgood Hall, 228, 2)`
347 `(Aubert, 839401, dae@ipn.net, 128)`, (`Mazza, 938130, Dm@fai.net, 205)`
348
349 If everything we knew about the campus came from that database, then
350
351 1. Yes, we could list all the professors.
352 2. No, we could not tell in which department is a professor.
353 3. No, we could not get the office hours of a professor.
354 4. Yes, we could list all the professors whose offices are in the same building.
355 5. Yes, we could list all the rooms.
356 6. 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).
357 7. Yes, we could list which professors are at the same floor.
358 8. No, we could not tell which professor has the highest evaluations.
289 359
290 360 # The Relational Data Model and Relational Database Constraints # The Relational Data Model and Relational Database Constraints
291 361
 
... ... Relational data model:
310 380
311 381 - **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`). - **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`).
312 382 - **Attribute** = Attribute name + attribute domain (but we'll just write the name). - **Attribute** = Attribute name + attribute domain (but we'll just write the name).
313 - **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$).
314 - **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.
315 - **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$).
383 - **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\)).
384 - **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.
385 - **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\)).
316 386
317 387 ### Characteristics of Relations ### Characteristics of Relations
318 388
 
... ... Relational data model:
340 410
341 411 ## Constraints ## Constraints
342 412
343 ### Types of Constraints
413 ### Types of constraints
344 414
345 415 Those are constraints on the tuples (there are constraints on the scheme, for instance, "a relation can't have two attributes with the same name"). Those are constraints on the tuples (there are constraints on the scheme, for instance, "a relation can't have two attributes with the same name").
346 416
 
... ... Constraints:
414 484 | INSURANCE(\underline{Policy\_Number}, Insured\_Car, Insured\_Driver\_State, Insured\_Driver\_Num, Rate) | INSURANCE(\underline{Policy\_Number}, Insured\_Car, Insured\_Driver\_State, Insured\_Driver\_Num, Rate)
415 485 | PRICE(\underline{Stock\_number}, Car\_Vin, Price, Margin) | PRICE(\underline{Stock\_number}, Car\_Vin, Price, Margin)
416 486
417 ## Transactions And Operations
487 ## Transactions and Operations
418 488
419 489 Operations are of two kinds: retrievals and updates. Operations are of two kinds: retrievals and updates.
420 490
 
... ... How things can go wrong:
464 534 - Change foreign key to a non-existing value (a.) - Change foreign key to a non-existing value (a.)
465 535
466 536
467 ### Dealing with Violations
537 ### Dealing with violations
468 538
469 539 a. Reject (restrict) a. Reject (restrict)
470 540 b. Cascade (propagate) b. Cascade (propagate)
 
... ... TICKETS(\underline{ShowTimeID}, Price)
624 694
625 695 # SQL # SQL
626 696
627 ## References
697 ## Resources
628 698
629 699 [@Textbook6, ch. 4--5] describes `SQL`, not one of its implementation. [@Textbook6, ch. 4--5] describes `SQL`, not one of its implementation.
630 700 Cf. #problem:installation for a list of useful links. Cf. #problem:installation for a list of useful links.
 
... ... The three last sublanguages being dubbed "**D**ata **M**anipulation **L**anguage
657 727
658 728 ### SQL ### SQL
659 729
660 #### Yet Another Vocabulary
730 #### Yet another vocabulary
661 731
662 732
663 733 | SQL | "Common" / Relational | | SQL | "Common" / Relational |
 
... ... The three last sublanguages being dubbed "**D**ata **M**anipulation **L**anguage
669 739 | Column | Attribute | | Column | Attribute |
670 740
671 741
672 #### Schema Elements
742 #### Schema elements
673 743
674 744 - Tables - Tables
675 745 - Type - Type
 
... ... SELECT * FROM Table_set_null;
984 1054
985 1055 --- ---
986 1056
987 ### Constructing and Populating a New Example
1057 ### Constructing and populating a new example
988 1058
989 1059 #### Construction #### Construction
990 1060
 
... ... INSERT INTO STUDENT (Login, Name, Registered, Major) VALUES
1063 1133
1064 1134 Note the date litterals. Note the date litterals.
1065 1135
1066 #### A Note on the Differences Between MySQL and MariaDB {#differenceMySQLMariaDB}
1136 #### A note on the differences between MySQL and MariaDB {#differenceMySQLMariaDB}
1067 1137
1068 1138 - 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).
1069 1139 - 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
 
... ... SELECT Login FROM GRADE WHERE Grade > 3.0 ORDER BY Grade DESC;
1148 1218 SELECT Login, Major FROM STUDENT ORDER BY Major, Name; SELECT Login, Major FROM STUDENT ORDER BY Major, Name;
1149 1219 ~~~ ~~~
1150 1220
1151 ### Aggregate Functions
1221 ### Aggregate functions
1152 1222
1153 1223 You can use `MAX`, `SUM`, `MIN`, `AVG`, `COUNT`: You can use `MAX`, `SUM`, `MIN`, `AVG`, `COUNT`:
1154 1224
 
... ... SELECT COUNT(Name) FROM STUDENT;
1160 1230 SELECT COUNT(DISTINCT Name) FROM STUDENT; SELECT COUNT(DISTINCT Name) FROM STUDENT;
1161 1231 ~~~ ~~~
1162 1232
1163 ### Aliases for Columns
1233 ### Aliases for columns
1164 1234
1165 1235 ~~~{.sql} ~~~{.sql}
1166 1236 SELECT Login AS Username FROM PROF; SELECT Login AS Username FROM PROF;
 
... ... We can have two join conditions!
1237 1307 SELECT PROF.Name FROM PROF, DEPARTMENT, STUDENT WHERE STUDENT.Name = 'Ava Alyx' AND STUDENT.Major = DEPARTMENT.Code AND DEPARTMENT.Head = PROF.Login; SELECT PROF.Name FROM PROF, DEPARTMENT, STUDENT WHERE STUDENT.Name = 'Ava Alyx' AND STUDENT.Major = DEPARTMENT.Code AND DEPARTMENT.Head = PROF.Login;
1238 1308 ~~~ ~~~
1239 1309
1240 ### Aliasing Tuples
1310 ### Aliasing tuples
1241 1311
1242 1312 ~~~{.sql} ~~~{.sql}
1243 1313 SELECT A.Name FROM PROF AS A, DEPARTMENT, STUDENT AS B WHERE B.Name = 'Ava Alyx' AND B.Major = DEPARTMENT.Code AND DEPARTMENT.Head = A.Login; SELECT A.Name FROM PROF AS A, DEPARTMENT, STUDENT AS B WHERE B.Name = 'Ava Alyx' AND B.Major = DEPARTMENT.Code AND DEPARTMENT.Head = A.Login;
 
... ... FROM STUDENT AS Me, STUDENT AS Fellow
1255 1325 WHERE Me.Name = 'Ava Alyx' AND Fellow.Major = Me.Major AND NOT Fellow.Name = 'Ava Alyx'; WHERE Me.Name = 'Ava Alyx' AND Fellow.Major = Me.Major AND NOT Fellow.Name = 'Ava Alyx';
1256 1326 ~~~ ~~~
1257 1327
1258 ### Nested Queries
1328 ### Nested queries
1259 1329
1260 1330 ~~~{.sql} ~~~{.sql}
1261 1331 SELECT Login FROM GRADE WHERE Grade > SELECT Login FROM GRADE WHERE Grade >
 
... ... Exercise +.#
1359 1429
1360 1430 Data Type | Examples | Data Type | Examples |
1361 1431 --- | --- | --- | --- |
1362   | $4$, $-32$
1432   | `4`, `-32`
1363 1433 Char(4) |   Char(4) |  
1364 VarChar(10) | 'Train', 'Michelle'
1434 VarChar(10) | `'Train'`, `'Michelle'`
1365 1435 Bit(4) |   Bit(4) |  
1366   | TRUE, UNKNOWN
1436   | `TRUE`, `UNKNOWN`
1367 1437
1368 1438 Exercise +.# Exercise +.#
1369 1439 ~ Explain what the following `SQL` statement does ~ Explain what the following `SQL` statement does
 
... ... Exercise +.#
1376 1446 ~ If I want to enter January 21, 2016, as a value for an attribute with the `DATE` datatype, what value should I enter? ~ If I want to enter January 21, 2016, as a value for an attribute with the `DATE` datatype, what value should I enter?
1377 1447
1378 1448 Exercise +.# Exercise +.#
1379 ~ Write a statement that inserts the values "Thomas" and "$4$" into the table `TRAINS`.
1449 ~ Write a statement that inserts the values `"Thomas"` and `4` into the table `TRAINS`.
1380 1450
1381 1451
1382 1452 Exercise +.# Exercise +.#
 
... ... Exercise +.#
1401 1471 FOREIGN KEY (DptNumber) REFERENCES DEPARTMENT(DptNumber) ON DELETE SET DEFAULT ON UPDATE CASCADE FOREIGN KEY (DptNumber) REFERENCES DEPARTMENT(DptNumber) ON DELETE SET DEFAULT ON UPDATE CASCADE
1402 1472 ~~~ ~~~
1403 1473
1404 What happen to the row whose foreign key `DptNumber` is set to $3$ if
1474 What happen to the row whose foreign key `DptNumber` is set to `3` if
1405 1475
1406 #. the row in the `DEPARTEMENT` table with primary key `DptNumber` set to \(3\) is deleted?
1407 #. the row in the `DEPARTEMENT` table with primary key `DptNumber` set to \(3\) as its value for `DptNumber` updated to \(5\)?
1476 #. the row in the `DEPARTEMENT` table with primary key `DptNumber` set to `3` is deleted?
1477 #. the row in the `DEPARTEMENT` table with primary key `DptNumber` set to `3` as its value for `DptNumber` updated to `5`?
1408 1478
1409 1479 Exercise +.# Exercise +.#
1410 1480 ~ If the following is part of the design of a `WORKER` table: ~ If the following is part of the design of a `WORKER` table:
 
... ... Exercise +.#
1413 1483 FOREIGN KEY WORKER(DptNumber) REFERENCES DEPARTMENT(DptNumber) ON UPDATE CASCADE FOREIGN KEY WORKER(DptNumber) REFERENCES DEPARTMENT(DptNumber) ON UPDATE CASCADE
1414 1484 ~~~ ~~~
1415 1485
1416 Admitting there is a row in the `WORKER` table whose foreign key `DptNumber` is set to $3$, what would happen if
1486 Admitting there is a row in the `WORKER` table whose foreign key `DptNumber` is set to `3`, what would happen if
1417 1487
1418 #. We tried to delete the row in the `DEPARTEMENT` table with primary key `DptNumber` set to \(3\)?
1419 #. We tried to change the value of `DptNumber` of the row in the `DEPARTEMENT` table with primary key `DptNumber` set to \(3\)?
1488 #. We tried to delete the row in the `DEPARTEMENT` table with primary key `DptNumber` set to `3`?
1489 #. We tried to change the value of `DptNumber` of the row in the `DEPARTEMENT` table with primary key `DptNumber` set to `3`?
1420 1490
1421 1491 Exercise +.# Exercise +.#
1422 1492 ~ Given a relation `TOURIST(Name, EntryDate, Address)`, write a `SQL` statement printing the name and address of all the tourists who entered the territory after the 15 September, 2012. ~ Given a relation `TOURIST(Name, EntryDate, Address)`, write a `SQL` statement printing the name and address of all the tourists who entered the territory after the 15 September, 2012.
 
... ... Solution +.#
1649 1719 <!-- Bogue table et solution, Color? --> <!-- Bogue table et solution, Color? -->
1650 1720
1651 1721 Data Type | Examples | Data Type | Examples |
1652 Int | $4$, $-32$
1653 Char(4) | 'trai', 'plol'
1654 VarChar(10) | 'Train', 'Michelle'
1655 Bit(4) | B'1010', B'0101'
1656 Boolean | TRUE, UNKNOWN
1722 Int | `4`, `-32`
1723 Char(4) | `'trai'`, `'plol'`
1724 VarChar(10) | `'Train'`, `'Michelle'`
1725 Bit(4) | `B'1010'`, `B'0101'`
1726 Boolean | `TRUE`, `UNKNOWN`
1657 1727
1658 1728 Solution +.# Solution +.#
1659 1729 ~ It creates a schema, i.e., a database, named "Faculty". ~ It creates a schema, i.e., a database, named "Faculty".
 
... ... Solution +.#
1780 1850
1781 1851 ## Problems ## Problems
1782 1852
1783 ### Setting Up MySQL
1853 ### Setting up MySQL
1784 1854
1785 1855 Problem -.+.#installation Problem -.+.#installation
1786 1856
 
... ... You're all set! All you have to do is to quit, using the command
1938 2008 EXIT; EXIT;
1939 2009 ~~~ ~~~
1940 2010
1941 ### Creating, Listind and Dropping Databases
2011 ### Creating, listind and dropping databases
1942 2012
1943 2013 Problem -.+.#creatingSchema Problem -.+.#creatingSchema
1944 2014
 
... ... Problem -.+.#creatingSchema
1991 2061
1992 2062 In the future, we will refer to the commands 1 and 2 as "log-in as `testuser` and create a database `HW_2Q1`". In the future, we will refer to the commands 1 and 2 as "log-in as `testuser` and create a database `HW_2Q1`".
1993 2063
1994 ### Reading the Documentation
2064 ### Reading the documentation
1995 2065
1996 2066 Problem -.+.#sqldoc Problem -.+.#sqldoc
1997 2067
 
... ... Make sure you are familiar with the Backus–Naur form (BNF) notation commonly u
2032 2102 Those are the commands implemented in the DBMS you are actually using. Those are the commands implemented in the DBMS you are actually using.
2033 2103 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. 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.
2034 2104
2035 ### Creating and Using a Table
2105 ### Creating and using a table
2036 2106
2037 2107 *This exercise, and the following ones, assume you successfully completed @problem:installation.* *This exercise, and the following ones, assume you successfully completed @problem:installation.*
2038 2108
2039 2109 Log in as `testuser` and create a database `HW_2Q3`. Log in as `testuser` and create a database `HW_2Q3`.
2040 2110
2041 #### Setting Up the Meta-Data
2111 #### Setting up the meta-data
2042 2112
2043 2113 We will first set-up our meta-data (i.e., the schema, the structure where our tables will be, and the tables themselves). We will first set-up our meta-data (i.e., the schema, the structure where our tables will be, and the tables themselves).
2044 2114
 
... ... And observe the modification:
2130 2200 DESC ADDRESS; DESC ADDRESS;
2131 2201 ~~~ ~~~
2132 2202
2133 #### Inserting Data
2203 #### Inserting data
2134 2204
2135 2205 This second part is about data, i.e., filling our tables with actual information. This second part is about data, i.e., filling our tables with actual information.
2136 2206
 
... ... UPDATE ADDRESS SET Habitants = 3 WHERE Number = 120;
2167 2237 ~~~ ~~~
2168 2238
2169 2239
2170 ### Repetiting Tuples in MySQL
2240 ### Repetiting tuples in MySQL
2171 2241
2172 2242 Problem +.#repetition Problem +.#repetition
2173 2243
 
... ... ALTER TABLE EXAMPLE ADD PRIMARY KEY (X);
2451 2521 ERROR 1062 (23000): Duplicate entry 'Train' for key 'PRIMARY' ERROR 1062 (23000): Duplicate entry 'Train' for key 'PRIMARY'
2452 2522 ~~~ ~~~
2453 2523
2454 ### Solution For @problem:profrevisited
2524 ### Solution for @problem:profrevisited
2455 2525
2456 2526
2457 2527 ~~~{.sqlmysql} ~~~{.sqlmysql}
 
... ... Take the time to introduce future topics + to give exam back.
2556 2626 --- ---
2557 2627
2558 2628
2559 ## Entity-Relationship (ER) model
2629 ## Entity-Relationship (ER) Model
2560 2630
2561 2631 Data = entity, relationships, attributes Data = entity, relationships, attributes
2562 2632
2563 ### Enties and Attributes
2633 ### Enties and attributes
2564 2634
2565 2635 - Entity = Thing, object, with independent existence. - Entity = Thing, object, with independent existence.
2566 2636 - Each entity has attributes (properties) - Each entity has attributes (properties)
 
... ... Attributes can be
2587 2657
2588 2658 \{Address(Street, Number, Apt, City, State, ZIP)\} \{Address(Street, Number, Apt, City, State, ZIP)\}
2589 2659
2590 ### Entity Types and Key Attributes
2660 ### Entity types and key attributes
2591 2661
2592 2662 - Entity = actual thing - Entity = actual thing
2593 2663 - Entity type = collection of entities with the same attributes - Entity type = collection of entities with the same attributes
2594 2664 - Entity set (or collection) = collection of all entities of a particular entity type. - Entity set (or collection) = collection of all entities of a particular entity type.
2595 2665
2596 #### Key Attributes
2666 #### Key attributes
2597 2667
2598 2668 A key attribute is an attribute whose value is distinct for each individual in the entity set. A key attribute is an attribute whose value is distinct for each individual in the entity set.
2599 2669
 
... ... A key attribute is an attribute whose value is distinct for each individual in t
2603 2673 - A composite attribute that is a key attribute should not still be a key attribute if we were to remove one of the attribute (similar to the minimality requirement). - A composite attribute that is a key attribute should not still be a key attribute if we were to remove one of the attribute (similar to the minimality requirement).
2604 2674 - An entity with no key is called a weak entity type (more about that later). - An entity with no key is called a weak entity type (more about that later).
2605 2675
2606 #### Drawing Entity Types
2676 #### Drawing entity types
2607 2677
2608 2678 - Entity = squared box - Entity = squared box
2609 2679 - Attribute = rounded box connected to a square box - Attribute = rounded box connected to a square box
 
... ... A key attribute is an attribute whose value is distinct for each individual in t
2617 2687
2618 2688 --- ---
2619 2689
2620 ### Relationships and Structural Constraints
2690 ### Relationships and structural constraints
2621 2691
2622 2692 Reminder: entity = actual thing, entity set = collection of entities, entity type = abstraction. Reminder: entity = actual thing, entity set = collection of entities, entity type = abstraction.
2623 2693
 
... ... Naming convention:
2638 2708 ![](before/paper_screen/Relationship_instance.jpeg){ width=100% } ![](before/paper_screen/Relationship_instance.jpeg){ width=100% }
2639 2709
2640 2710
2641 #### Recursive And Role Names
2711 #### Recursive and role names
2642 2712
2643 2713 Convenient, and sometimes mandatory, to give role names: Convenient, and sometimes mandatory, to give role names:
2644 2714
 
... ... Stress one aspect of the relationship.
2651 2721
2652 2722 Two constraints, together called "structural constraints", applies to relationship types. Two constraints, together called "structural constraints", applies to relationship types.
2653 2723
2654 ##### Cardinality Ratio
2724 ##### Cardinality ratio
2655 2725
2656 2726 **Maximum** number of relationships instances that an entity can participat it. **Maximum** number of relationships instances that an entity can participat it.
2657 2727
2658 For binary relations, can be 1:1, N:1, M:N (1 is "at most", M, N is "no maximum" => in E.-R. diagram, we don't count (yet)).
2728 For binary relations, can be \(1:1\), \(N:1\), \(M:N\) (\(1\) is "at most", \(M\), \(N\), \(O\), \(P\), is "no maximum" => in E.-R. diagram, we don't count (yet)).
2659 2729
2660 - MENTOR : MENTEE is 1:N ("a mentee has at most one mentor, one mentor can have multiple mentees")
2661 - PERSON : SSN is 1:1
2662 - COURSE : DEPARTMENT is N:1
2663 - STUDENT : TEAM is M:N
2730 - MENTOR : MENTEE is \(1:N\) ("a mentee has at most one mentor, one mentor can have multiple mentees")
2731 - PERSON : SSN is \(1:1\)
2732 - COURSE : DEPARTMENT is \(N:1\)
2733 - STUDENT : TEAM is \(M:N)
2664 2734
2665 2735 ![](before/paper_screen/Cardinality_ratio.jpeg){ width=100% } ![](before/paper_screen/Cardinality_ratio.jpeg){ width=100% }
2666 2736
2667 2737
2668 #### Participation Constraint
2738 #### Participation constraint
2669 2739
2670 2740 **Minimum** number of relationships instances that an entity can participat it, a.k.a. "minimum cardinality constraint". **Minimum** number of relationships instances that an entity can participat it, a.k.a. "minimum cardinality constraint".
2671 2741
 
... ... Total is drawn with a double line.
2677 2747
2678 2748 Typically, date attribute. Typically, date attribute.
2679 2749
2680 - TEACHING relation between PROF and CLASS (N:M) could have a "Quarter" attribute.
2681 - MENTORING relation between MENTOR and MENTEE (1:N) could have a "Since" attribute.
2682 - EMITED_DRIVING_LICENCE between DMV and PERSON (N:1) could have a "Date" attribute.
2750 - TEACHING relation between PROF and CLASS (\(N:M\)) could have a "Quarter" attribute.
2751 - MENTORING relation between MENTOR and MENTEE (\(1:N\)) could have a "Since" attribute.
2752 - EMITED_DRIVING_LICENCE between DMV and PERSON (\(N:1\)) could have a "Date" attribute.
2683 2753
2684 2754 We are dealing with moving entities, here! We are dealing with moving entities, here!
2685 Attributes on 1:1, 1:N, N:1 can be migrated (to the N side).
2755 Attributes on \(1:1\), \(1:N\), \(N:1\) can be migrated (to the \(N\) side).
2686 2756
2687 #### Relationships of Degree Higher Than 2
2757 #### Relationships of degree higher than two
2688 2758
2689 2759 To determine cardinality ratio: fix all but one, wonder how many can be in that relationship. To determine cardinality ratio: fix all but one, wonder how many can be in that relationship.
2690 2760
 
... ... To determine cardinality ratio: fix all but one, wonder how many can be in that
2692 2762
2693 2763 ![](before/paper_screen/N_ary_relationship02.jpeg){ width=100% } ![](before/paper_screen/N_ary_relationship02.jpeg){ width=100% }
2694 2764
2695 *Need to find a good 3-ary example*
2765 *Need to find a good \(3\)-ary example*
2696 2766
2697 2767 *There is another, large example on paper.* *There is another, large example on paper.*
2698 2768
2699 2769 --- ---
2700 2770
2701 2771
2702 ### Weak Entity Types
2772 ### Weak entity types
2703 2773
2704 2774 Two sorts of entity types: Two sorts of entity types:
2705 2775
 
... ... Choice between two representation: if pet is involved in other relationships!
2717 2787 - Owner can itself be weak! - Owner can itself be weak!
2718 2788 - The degree of the identifying relationship can be more than 2! - The degree of the identifying relationship can be more than 2!
2719 2789
2720 ### Alternative Notation
2790 ### Alternative notation
2721 2791
2722 2792 **Drawings** **Drawings**
2723 2793
 
... ... We have to map all of the following:
2767 2837 --- | --- --- | ---
2768 2838 Entity | Strong, Weak | Entity | Strong, Weak |
2769 2839 Attributes | Composite, Key, Atomic, Multi-valued, Partial Key | Attributes | Composite, Key, Atomic, Multi-valued, Partial Key |
2770 Relationships | Binary (1:1, N:1, 1:N, N:M), N-ary
2840 Relationships | Binary (\(1:1\), \(N:1\), \(1:N\), \(N:M\)), \(n\)-ary
2771 2841
2772 2842
2773 2843 Using four tools: Relations, Attributes, Primary Keys, Foreign Keys. Using four tools: Relations, Attributes, Primary Keys, Foreign Keys.
 
... ... Using four tools: Relations, Attributes, Primary Keys, Foreign Keys.
2778 2848 -- | ---------- | ---------------- -- | ---------- | ----------------
2779 2849 1 | Strong Entity | Relation with all the simple attributes. Decompose complex attributes. Pick a key to be the PK, if it is composite, take its elements. 1 | Strong Entity | Relation with all the simple attributes. Decompose complex attributes. Pick a key to be the PK, if it is composite, take its elements.
2780 2850 2 | Weak Entity | Relation with all the simple attributes. Decompose complex attributes. Add as a foreign key the primary key of the relation corresponding to the owner entity type. If the owner entity type is itself weak, start with it. 2 | Weak Entity | Relation with all the simple attributes. Decompose complex attributes. Add as a foreign key the primary key of the relation corresponding to the owner entity type. If the owner entity type is itself weak, start with it.
2781 3 | Binary 1:1 Relationship Types | Foreign Key, Merge Relations or Cross-Reference approach
2782 4 | Binary 1:N Relationship Types | Foreign Key or Cross-Reference approach
2783 5 | Binary M:N Relationship Types | Cross-Reference approach
2784 6 | N-ary Relationship Types | Cross-Reference approach
2851 3 | Binary \(1:1\) Relationship Types | Foreign Key, Merge Relations or Cross-Reference approach
2852 4 | Binary \(1:N\) Relationship Types | Foreign Key or Cross-Reference approach
2853 5 | Binary \(M:N\) Relationship Types | Cross-Reference approach
2854 6 | \(n\)-ary Relationship Types | Cross-Reference approach
2785 2855 7 | Multivalued Attributes | Create a new relation, whose primary key is the foreign key to the entity. 7 | Multivalued Attributes | Create a new relation, whose primary key is the foreign key to the entity.
2786 2856
2787 2857
2788 2858 a. Foreign Key Approach: Chose one of the relation (preferably with total participation constraint, or on the N side), add a foreign key and all the attributes of the relationship. a. Foreign Key Approach: Chose one of the relation (preferably with total participation constraint, or on the N side), add a foreign key and all the attributes of the relationship.
2789 2859 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.
2790 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.
2860 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.
2791 2861
2792 2862 **Bogue: + Propagate option? Cascade, most of the time: weak entity type, lookup tables, etc.** **Bogue: + Propagate option? Cascade, most of the time: weak entity type, lookup tables, etc.**
2793 2863
 
... ... c. Cross-Reference or Relationship Relation Approach: Create a lookup table with
2799 2869 E.R. Model | Relational Model | E.R. Model | Relational Model |
2800 2870 --- | --- --- | ---
2801 2871 Entity type | Entity relation Entity type | Entity relation
2802 1:1 or 1:N relationship type | Foreign key (or relationship relation)
2803 M:N relationship type | Relationship relation and two foreign keys
2804 n-ary relationship type | Relationship relation and n foreign keys
2872 \(1:1\) or \(1:N\) relationship type | Foreign key (or relationship relation)
2873 \(M:N\) relationship type | Relationship relation and two foreign keys
2874 \(n\)-ary relationship type | Relationship relation and n foreign keys
2805 2875 Simple attribute | Attribute Simple attribute | Attribute
2806 2876 Composite attribute | Set of simple component attributes Composite attribute | Set of simple component attributes
2807 2877 Multivalued attribute | Relation and foreign key Multivalued attribute | Relation and foreign key
 
... ... Key attribute | Primary key
2810 2880
2811 2881 **Worked on PB 2 and 3 of HW4, that needs to be adapted. Needs to be written more properly. Cf. Drawing in Lecture 16's notes.** **Worked on PB 2 and 3 of HW4, that needs to be adapted. Needs to be written more properly. Cf. Drawing in Lecture 16's notes.**
2812 2882
2813 **Need to work on a better example, includings n-ary relationship, and propagate options.**
2883 **Need to work on a better example, includings \(n\)-ary relationship, and propagate options.**
2814 2884
2815 2885
2816 2886 ## Guidelines and Normal Form ## Guidelines and Normal Form
 
... ... a. Information preservation
2824 2894 b. Minimum redundancy b. Minimum redundancy
2825 2895 c. Make queries easy c. Make queries easy
2826 2896
2827 ### General Rules
2897 ### General rules
2828 2898
2829 2899 #### Semantics #### Semantics
2830 2900
2831 2901 1 relation corresponds to 1 entity or 1 relationship type 1 relation corresponds to 1 entity or 1 relationship type
2832 2902
2833 #### No Anomalies
2903 #### No anomalies
2834 2904
2835 2905 1. Insertion Anomalies 1. Insertion Anomalies
2836 2906
 
... ... Updated have to be consistent.
2856 2926 2. Delete last student of advisor 2. Delete last student of advisor
2857 2927 3. Advisor change name. 3. Advisor change name.
2858 2928
2859 #### Null Should be Rare
2929 #### Null should be rare
2860 2930
2861 2931 `NULL` has 3 meanings, wastes space, and makes join / nested projections harder. `NULL` has 3 meanings, wastes space, and makes join / nested projections harder.
2862 2932
 
... ... BRAND(Name, Email)
2901 2971
2902 2972 Brand, Name and Name being the ID. Brand, Name and Name being the ID.
2903 2973
2904 ### Functional Dependencies
2974 ### Functional dependencies
2905 2975
2906 2976 Formal tool to assess how "good" a database is, a property of the relation schema. Formal tool to assess how "good" a database is, a property of the relation schema.
2907 2977
2908 #### Using Semantics of Attributes
2978 #### Using semantics of attributes
2909 2979
2910 2980 "What *should* be." "What *should* be."
2911 2981
 
... ... A FD : X → Y is a **transivive dependency** if there exist a set of attribute
3001 3071 --- ---
3002 3072
3003 3073
3004 ### Normal Forms and Keys
3005
3074 ### Normal forms and keys
3075
3006 3076 First, Second, Third, Fourth, Fifth normal form ("X"NF). First, Second, Third, Fourth, Fifth normal form ("X"NF).
3007 3077 Stronger than the Third, there is the Boyce-Codd NF (BCNF) Stronger than the Third, there is the Boyce-Codd NF (BCNF)
3008 3078
3009 If you satisfy $N$, you satisfy $N-1$, $N-2$, etc.
3079 If you satisfy \(N\), you satisfy $N-1$, $N-2$, etc.
3010 3080 The *normal form* of a relation is the highest normal form condition that it meets. The *normal form* of a relation is the highest normal form condition that it meets.
3011 3081
3012 #### Fist Normal Form
3082 #### Fist normal form
3013 3083
3014 3084 ##### Definition ##### Definition
3015 3085
 
... ... We will take this requirement to be part of the definition of 1NF, but some auth
3022 3092
3023 3093 **To be written** **To be written**
3024 3094
3025 #### Second Normal Form
3095 #### Second normal form
3026 3096
3027 3097 ##### Definition ##### Definition
3028 3098
 
... ... Refinment: note that if more than one attribute depends of the same subset $\{A'
3049 3119
3050 3120 Note also that if our primary key is a singleton, then there is nothing to do, we are in 2NF as soon as we are in 1NF. Note also that if our primary key is a singleton, then there is nothing to do, we are in 2NF as soon as we are in 1NF.
3051 3121
3052 #### Third Normal Form
3122 #### Third normal form
3053 3123
3054 3124 ##### Definition ##### Definition
3055 3125
 
... ... For each attribute $A$ of the relation whose primary key is $A_1, ..., A_n$:
3073 3143 **ADD EXAMPLES** **ADD EXAMPLES**
3074 3144
3075 3145
3076 #### Notes And Examples
3146 #### Notes and examples
3077 3147
3078 3148 CCL: every FD X → Y s.t. X is a proper subset of the primary key, or a non-prime attribute, is problematic. CCL: every FD X → Y s.t. X is a proper subset of the primary key, or a non-prime attribute, is problematic.
3079 3149
 
... ... That's a class diagram, there are other types of diagrams, they are not unrelate
3108 3178 For instance, using communication diagrams, deployment diagrams, and state chart diagrams, you can collect the requirements needed to draw a class diagram! For instance, using communication diagrams, deployment diagrams, and state chart diagrams, you can collect the requirements needed to draw a class diagram!
3109 3179 They each offer a viewpoint on a software that will help you in making sure the various pieces will fit together: it is a tool commonly used in software engineering, and useful in database design. They each offer a viewpoint on a software that will help you in making sure the various pieces will fit together: it is a tool commonly used in software engineering, and useful in database design.
3110 3180
3111 ### Types of Diagrams
3181 ### Types of diagrams
3112 3182
3113 3183 There are 14 different types of diagrams, divided between two categories: structural and behavioral. There are 14 different types of diagrams, divided between two categories: structural and behavioral.
3114 3184
 
... ... Then there is the sub-category of "Interaction diagrams":
3146 3216 This sub-category also comprise **Timing diagram** and **Interaction overview diagram**. This sub-category also comprise **Timing diagram** and **Interaction overview diagram**.
3147 3217
3148 3218
3149 ### Zoom on Classes Diagrams
3219 ### Zoom on classes diagrams
3150 3220
3151 3221 Looking at the "COMPANY conceptual schema in UML class diagram notation", and comparing it with the "ER schema diagram for the COMPANY database" from the textbook, can help you in writing your own "Rosetta Stone" between ER and UML diagram. Looking at the "COMPANY conceptual schema in UML class diagram notation", and comparing it with the "ER schema diagram for the COMPANY database" from the textbook, can help you in writing your own "Rosetta Stone" between ER and UML diagram.
3152 3222 Let us introduce some UML terminology for the class diagrams. Let us introduce some UML terminology for the class diagrams.
 
... ... Exercise +.#
3212 3282 ~ What is a self-referencing, or recursive, relationship type? Give two examples. ~ What is a self-referencing, or recursive, relationship type? Give two examples.
3213 3283
3214 3284 Exercise +.# Exercise +.#
3215 ~ What does it mean for a binary relationship type "Owner" between entity types "Person" and "Computer" to have a cardinality ration $M:N$?
3285 ~ What does it mean for a binary relationship type "Owner" between entity types "Person" and "Computer" to have a cardinality ration \(M:N\)?
3216 3286
3217 3287 Exercise +.# Exercise +.#
3218 3288 ~ What are the two possible structural constraints on a relationship type? ~ What are the two possible structural constraints on a relationship type?
 
... ... INSTRUCTOR | | OFFICE
3235 3305 COMPUTER | | OPERATING\_SYSTEM COMPUTER | | OPERATING\_SYSTEM
3236 3306
3237 3307 Exercise +.# Exercise +.#
3238 ~ Give an example of a binary relationship type of cardinality $1 : N$.
3308 ~ Give an example of a binary relationship type of cardinality \(1:N\).
3239 3309
3240 3310 Exercise +.# Exercise +.#
3241 ~ Give an example of a binary relationship type of cardinality $N:1$, and draw the corresponding diagram (you don't have to include details on the participating entity types).
3311 ~ Give an example of a binary relationship type of cardinality \(N:1\), and draw the corresponding diagram (you don't have to include details on the participating entity types).
3242 3312
3243 3313 Exercise +.# Exercise +.#
3244 3314 ~ Draw an ER diagram with a single entity type, with two stored attributes and one derived attribute. In your answer, it should be clear that the value for the derived attribute will always be obtained from the value(s) for the other attribute(s). ~ Draw an ER diagram with a single entity type, with two stored attributes and one derived attribute. In your answer, it should be clear that the value for the derived attribute will always be obtained from the value(s) for the other attribute(s).
 
... ... Exercise +.#
3252 3322
3253 3323 \begin{tikzpicture}[node distance=7em] \begin{tikzpicture}[node distance=7em]
3254 3324 \node[relationship] (contains){STAYS\_AT}; \node[relationship] (contains){STAYS\_AT};
3255 \node[entity] (person) [above = 1cm of contains] {PERSON} edge node[right] {$N$} (contains);;
3325 \node[entity] (person) [above = 1cm of contains] {PERSON} edge node[right] {\(N\)} (contains);;
3256 3326 \node[attribute] (ssn) [left of=person] {\key{SSN}} edge (person); \node[attribute] (ssn) [left of=person] {\key{SSN}} edge (person);
3257 3327 \node[attribute] (dob) [above = .5cm of person] {Date\_Of\_Birth} edge (person); \node[attribute] (dob) [above = .5cm of person] {Date\_Of\_Birth} edge (person);
3258 \node[entity] (burger) [below of= contains] {PLACE} edge node[right] {$1$} (contains);
3328 \node[entity] (burger) [below of= contains] {PLACE} edge node[right] {\(1\)} (contains);
3259 3329 \node[attribute] (dob) [below = .5cm of burger] {\key{Address}} edge (burger); \node[attribute] (dob) [below = .5cm of burger] {\key{Address}} edge (burger);
3260 3330 \node[attribute] (ssn) [left of=burger] {Rooms} edge (burger); \node[attribute] (ssn) [left of=burger] {Rooms} edge (burger);
3261 3331 \end{tikzpicture} \end{tikzpicture}
 
... ... Exercise +.#
3280 3350 Why is it a poor design to have a "Bike\_brand" attribute in such a relation? Why is it a poor design to have a "Bike\_brand" attribute in such a relation?
3281 3351 How should we store this information? How should we store this information?
3282 3352
3283
3284 3353 Exercise +.# Exercise +.#
3285 3354 ~ Consider the following relation: ~ Consider the following relation:
3286 3355
 
... ... Exercise +.#
3288 3357
3289 3358 Why is it a poor design to have a "Sibling\_On\_Campus" attribute in such a relation? Why is it a poor design to have a "Sibling\_On\_Campus" attribute in such a relation?
3290 3359 How should we store this information? How should we store this information?
3291
3360
3292 3361 Exercise +.# Exercise +.#
3293 3362 ~ Consider the following relational database schema: ~ Consider the following relational database schema:
3294 3363
3295 3364 STUDENT(\underline{Login}, Name, \(…\), Major, Major\_Head) STUDENT(\underline{Login}, Name, \(…\), Major, Major\_Head)
3296 3365 DEPARTMENT(\underline{Code}, Name, Major\_Head) DEPARTMENT(\underline{Code}, Name, Major\_Head)
3297
3298 Assuming that "Major" is a foreign key referencing "DEPARTMENT.Code", what is the problem with that schema? How could you address it?
3299 3366
3367 Assuming that "Major" is a foreign key referencing "DEPARTMENT.Code", what is the problem with that schema? How could you address it?
3300 3368
3301 3369 Exercise +.# Exercise +.#
3302 3370 ~ Consider the relation \(R(A , B, C, D, E, F)\) and the following functional dependencies: ~ Consider the relation \(R(A , B, C, D, E, F)\) and the following functional dependencies:
 
... ... Exercise +.#
3321 3389 #. If \(\{A, B\}\) is the only key, is \(\{A, B\} \to \{C,D\}, \{B, C\} \to D\) a 2NF? List the nonprime attributes and justify. #. If \(\{A, B\}\) is the only key, is \(\{A, B\} \to \{C,D\}, \{B, C\} \to D\) a 2NF? List the nonprime attributes and justify.
3322 3390 #. If \(\{A, B, C\}\) is the only key, is \(A \to \{B, D\}, \{A, B, C\} \to D\) a 2NF? List the nonprime attributes and justify. #. If \(\{A, B, C\}\) is the only key, is \(A \to \{B, D\}, \{A, B, C\} \to D\) a 2NF? List the nonprime attributes and justify.
3323 3391
3324
3325 3392 Exercise +.# Exercise +.#
3326 3393 ~ Consider the relation \(R(A, B, C, D, E, F)\) with candidate keys \(\{A, B\}\) and \(C\). Answer the following: ~ Consider the relation \(R(A, B, C, D, E, F)\) with candidate keys \(\{A, B\}\) and \(C\). Answer the following:
3327 3394
3328 3395 #. What are the prime attributes in \(R\)? #. What are the prime attributes in \(R\)?
3329 3396 #. Is \(\{C,D\} \to E\) a fully functional dependency? #. Is \(\{C,D\} \to E\) a fully functional dependency?
3330 3397 #. Write a set of functional dependencies containing at least one transitive depency, and justify your answer. #. Write a set of functional dependencies containing at least one transitive depency, and justify your answer.
3331
3332 3398
3333 3399 Exercise +.# Exercise +.#
3334 3400 ~ Consider the relation \(R(A , B, C, D, E)\) and the following functional dependencies: ~ Consider the relation \(R(A , B, C, D, E)\) and the following functional dependencies:
 
... ... Exercise +.#
3336 3402 #. \(C \to D, \{C, B\} \to A, A \to \{B, C, D\}, B \to E\) #. \(C \to D, \{C, B\} \to A, A \to \{B, C, D\}, B \to E\)
3337 3403 #. \(\ A \to \{C, D\}, C \to B, D \to E, \{E, C\} \to A\) #. \(\ A \to \{C, D\}, C \to B, D \to E, \{E, C\} \to A\)
3338 3404 #. \(\{A, B\} \to D, D \to \{B, C\}, E \to C\) #. \(\{A, B\} \to D, D \to \{B, C\}, E \to C\)
3339
3405
3340 3406 For each one, give one candidate key for \(R\). For each one, give one candidate key for \(R\).
3341 3407
3342 3408 Exercise +.# Exercise +.#
 
... ... Exercise +.#
3345 3411 #. If \(\{A, B\}\) is the primary key, is \(B \to E, C \to D\) a 2NF?% List the nonprime attributes and justify. #. If \(\{A, B\}\) is the primary key, is \(B \to E, C \to D\) a 2NF?% List the nonprime attributes and justify.
3346 3412 #. If \(\{A\}\) is the primary key, is \(B \to C, B \to D\) a 2NF?% List the nonprime attributes and justify. #. If \(\{A\}\) is the primary key, is \(B \to C, B \to D\) a 2NF?% List the nonprime attributes and justify.
3347 3413
3348
3349 3414 Exercise +.# Exercise +.#
3350 3415 ~ Consider the relation \(R(A, B, C, D, E, F)\), and let \(\{B, D\}\) be the primary key, and have additionnaly the functional dependencies \(\{A, D\} \to E, C \to F\). ~ Consider the relation \(R(A, B, C, D, E, F)\), and let \(\{B, D\}\) be the primary key, and have additionnaly the functional dependencies \(\{A, D\} \to E, C \to F\).
3351 3416 This relation is not in 3NF, can you tell why? This relation is not in 3NF, can you tell why?
3352 3417
3353
3354 3418 Exercise +.# Exercise +.#
3355 3419 ~ Consider the relation \(R(A, B, C, D)\) and answer the following: ~ Consider the relation \(R(A, B, C, D)\) and answer the following:
3356 3420
3357 3421 #. If \(A\) is the only key, is \(A \to \{B,C,D\}, \{A, B\} \to C, \{B, C\} \to D\) a 3NF? List the nonprime attributes and justify. #. If \(A\) is the only key, is \(A \to \{B,C,D\}, \{A, B\} \to C, \{B, C\} \to D\) a 3NF? List the nonprime attributes and justify.
3358 3422 #. If \(B\) is the only key, is \(B \to \{A, C, D\}, A \to \{C, D\}, \{A, C\} \to D\) a 3NF? List the nonprime attributes and justify. #. If \(B\) is the only key, is \(B \to \{A, C, D\}, A \to \{C, D\}, \{A, C\} \to D\) a 3NF? List the nonprime attributes and justify.
3359 3423
3360
3361 3424 Exercise +.# Exercise +.#
3362 3425 ~ Consider the relation \(R(A, B, C, D, E)\) and the functional dependencies \( \{A, B\} \to C, B \to D, C \to E\). Answer the following: ~ Consider the relation \(R(A, B, C, D, E)\) and the functional dependencies \( \{A, B\} \to C, B \to D, C \to E\). Answer the following:
3363 3426
 
... ... Exercise +.#
3410 3473 Exercise +.# Exercise +.#
3411 3474 ~ Briefly explain the difference between an aggregation and a composition association. ~ Briefly explain the difference between an aggregation and a composition association.
3412 3475
3413
3414 3476 Exercise +.# Exercise +.#
3415 3477 ~ How is generalization (or inheritance) represented in a U.M.L. class diagram? ~ How is generalization (or inheritance) represented in a U.M.L. class diagram?
3416 3478 Why is such a concept useful? Why is such a concept useful?
 
... ... Exercise +.#
3424 3486 \node[attribute] (name) [above left of=person] {Name} edge (person); \node[attribute] (name) [above left of=person] {Name} edge (person);
3425 3487 \node[attribute] (phone) [above of=person] {Experience} edge (person); \node[attribute] (phone) [above of=person] {Experience} edge (person);
3426 3488
3427 \node[relationship] (drives) [below of=person] {ASSIGNED\_TO} edge node[right, pos=0.1] {$N$} (person);
3428 \node[entity] (plane) [below of=drives] {PLANE} edge [total] node[right, pos=0.7] {$1$} (drives);
3489 \node[relationship] (drives) [below of=person] {ASSIGNED\_TO} edge node[right, pos=0.1] {\(N\)} (person);
3490 \node[entity] (plane) [below of=drives] {PLANE} edge [total] node[right, pos=0.7] {\(1\)} (drives);
3429 3491 \node[attribute] (make) [left of=plane] {\key{TailID}} edge (plane); \node[attribute] (make) [left of=plane] {\key{TailID}} edge (plane);
3430 3492 \node[attribute] (maxS) [below left of =plane] {MaxSpeed} edge (plane); \node[attribute] (maxS) [below left of =plane] {MaxSpeed} edge (plane);
3431 3493 \node[attribute] (airPlaneType) [below of = plane] {AirPlaneType} edge (plane); \node[attribute] (airPlaneType) [below of = plane] {AirPlaneType} edge (plane);
 
... ... Solution +.#
3477 3539 ~ Cardinality ration and participation constraints. ~ Cardinality ration and participation constraints.
3478 3540
3479 3541 Solution +.# Solution +.#
3480 ~ When the cardinality is $1 : N$, $1 : 1$ or $N : 1$.
3542 ~ When the cardinality is \(1:N\), \(1:1\) or \(N:1\).
3481 3543
3482 3544 Solution +.# Solution +.#
3483 3545 ~ For a weak entity attribute, it is the attribute that can uniquely identify weak entites that are related to the same owner entity. ~ For a weak entity attribute, it is the attribute that can uniquely identify weak entites that are related to the same owner entity.
 
... ... Solution +.#
3486 3548
3487 3549 Entity 1 | Cardinality Ratio | Entity 2 | Explanation | Entity 1 | Cardinality Ratio | Entity 2 | Explanation |
3488 3550 | --- | :---: | --- | --------| | --- | :---: | --- | --------|
3489 STUDENT | N:1 | MAJOR | "A student has one major, but multiple students can have the same major"
3490 CAR | 1:1 | TAG | "A car has exactly one tag, a tag belongs to one particular car."
3491 INSTRUCTOR | 1:N | LECTURE | "An instructor can teach multiple lecture, but a lecture is taught by only one person."
3492 INSTRUCTOR | 1:N | OFFICE | "An instructor can have multiple office, but an office belongs to only one instructor"
3493 COMPUTER | M:N | OS| "A computer can have multiple operating system, the same operating system can be installed on more than one computer."
3551 STUDENT | \(N:1\) | MAJOR | "A student has one major, but multiple students can have the same major"
3552 CAR | \(1:1\) | TAG | "A car has exactly one tag, a tag belongs to one particular car."
3553 INSTRUCTOR | \(1:N\) | LECTURE | "An instructor can teach multiple lecture, but a lecture is taught by only one person."
3554 INSTRUCTOR | \(1:N\) | OFFICE | "An instructor can have multiple office, but an office belongs to only one instructor"
3555 COMPUTER | \(M:N\) | OS| "A computer can have multiple operating system, the same operating system can be installed on more than one computer."
3494 3556
3495 3557 Some of those choices, of course, are is arguable (typically, almost any combination seems reasonnable for the INSTRUCTOR : OFFICE relation). Some of those choices, of course, are is arguable (typically, almost any combination seems reasonnable for the INSTRUCTOR : OFFICE relation).
3496 3558
 
... ... Solution +.#
3500 3562 Solution +.# Solution +.#
3501 3563 ~ \begin{tikzpicture}[node distance=7em] ~ \begin{tikzpicture}[node distance=7em]
3502 3564 \node[relationship] (contains){BELONGS}; \node[relationship] (contains){BELONGS};
3503 \node[entity] (burger) [left = 1cm of contains] {HAND} edge node[above] {$N$} (contains);
3504 \node[entity] (ingredient) [right = 1cm of contains] {PERSON} edge node[above]{$1$} (contains);
3565 \node[entity] (burger) [left = 1cm of contains] {HAND} edge node[above] {\(N\)} (contains);
3566 \node[entity] (ingredient) [right = 1cm of contains] {PERSON} edge node[above]{\(1\)} (contains);
3505 3567 \end{tikzpicture} \end{tikzpicture}
3506 3568
3507 3569 Solution +.# Solution +.#
3508 ~
3509 \begin{tikzpicture}[node distance=7em]
3570 ~ \begin{tikzpicture}[node distance=7em]
3510 3571 \node[entity] (person) {PERSON}; \node[entity] (person) {PERSON};
3511 3572 \node[attribute] (ssn) [left of=person] {\key{SSN}} edge (person); \node[attribute] (ssn) [left of=person] {\key{SSN}} edge (person);
3512 3573 \node[attribute] (dob) [above of=person] {Date\_Of\_Birth} edge (person); \node[attribute] (dob) [above of=person] {Date\_Of\_Birth} edge (person);
 
... ... Solution +.#
3516 3577 Solution +.# Solution +.#
3517 3578 ~ \begin{tikzpicture}[node distance=7em] ~ \begin{tikzpicture}[node distance=7em]
3518 3579 \node[relationship] (contains){CONTAINS}; \node[relationship] (contains){CONTAINS};
3519 \node[entity] (burger) [left = 1cm of contains] {BURGER} edge[total] node[above] {$N$} (contains);
3520 \node[entity] (ingredient) [right = 1cm of contains] {INGREDIENT} edge node[above]{$M$} (contains);
3580 \node[entity] (burger) [left = 1cm of contains] {BURGER} edge[total] node[above] {\(N\)} (contains);
3581 \node[entity] (ingredient) [right = 1cm of contains] {INGREDIENT} edge node[above]{\(M\)} (contains);
3521 3582 \end{tikzpicture} \end{tikzpicture}
3522 3583
3523 3584 Solution +.# Solution +.#
3524 3585 ~ \begin{tikzpicture}[node distance=7em] ~ \begin{tikzpicture}[node distance=7em]
3525 3586 \node[relationship] (contains){CONTAINS}; \node[relationship] (contains){CONTAINS};
3526 \node[entity] (burger) [left = 1cm of contains] {BURGER} edge[total] node[above] {$N$} (contains);
3527 \node[entity] (ingredient) [right = 1cm of contains] {INGREDIENT} edge node[above]{$M$} (contains);
3587 \node[entity] (burger) [left = 1cm of contains] {BURGER} edge[total] node[above] {\(N\)} (contains);
3588 \node[entity] (ingredient) [right = 1cm of contains] {INGREDIENT} edge node[above]{\(M\)} (contains);
3528 3589 \end{tikzpicture} \end{tikzpicture}
3529 3590
3530 3591 Solution +.# Solution +.#
 
... ... Solution +.#
3533 3594 Solution +.# Solution +.#
3534 3595 ~ When you have to invent a primary key or add a lot of `NULL` value to be able to add a tuple. I want to add a room in my DB, but the only place where rooms are listed are as an attribute on a Instructor table, so I have to "fake" an instructor to add a room. ~ When you have to invent a primary key or add a lot of `NULL` value to be able to add a tuple. I want to add a room in my DB, but the only place where rooms are listed are as an attribute on a Instructor table, so I have to "fake" an instructor to add a room.
3535 3596
3536
3537 3597 Solution +.# Solution +.#
3538 3598 ~ A delete anomaly exists when certain attributes are lost because of the deletion of other attributes. ~ A delete anomaly exists when certain attributes are lost because of the deletion of other attributes.
3539 3599
 
... ... Solution +.#
3560 3620
3561 3621 Solution +.# Solution +.#
3562 3622 ~ #. Yes. \(C\) and \(D\) are non prime, and they fully depend on \(\{A,B\}\). ~ #. Yes. \(C\) and \(D\) are non prime, and they fully depend on \(\{A,B\}\).
3563
3564 #. No. \(D\) is the only non prime, and it depends only on \(A\).
3623 #. No. \(D\) is the only non prime, and it depends only on \(A\).
3565 3624
3566 3625 Solution +.# Solution +.#
3567 3626 ~ #. \(A,B,C\) ~ #. \(A,B,C\)
3568 #. No, because we can remove \(D\),
3569 #. \(A \to D\), \(D \to E\) and \(A \to E\)
3627 #. No, because we can remove \(D\),
3628 #. \(A \to D\), \(D \to E\) and \(A \to E\)
3570 3629
3571 3630 Solution +.# Solution +.#
3572 3631 ~ #. \(\{B, C\}\), \(A\) ~ #. \(\{B, C\}\), \(A\)
 
... ... Solution +.#
3574 3633 #. \(\{A, D, E\}\), \(\{A, B, E\}\) #. \(\{A, D, E\}\), \(\{A, B, E\}\)
3575 3634
3576 3635 Solution +.# Solution +.#
3577 ~ 1. No. \(C, D, E\), and \(E\) has a partial relation to \(B\)
3578
3579 2. Yes. Since the primary key is a singleton, it is obvious.
3636 ~ #. No. \(C, D, E\), and \(E\) has a partial relation to \(B\)
3637 #. Yes. Since the primary key is a singleton, it is obvious.
3580 3638
3581 3639 Solution +.# Solution +.#
3582 3640 ~ \(\{B, D\} \to C \to F\) breaks the 3NF. ~ \(\{B, D\} \to C \to F\) breaks the 3NF.
3583 3641
3584 3642 Solution +.# Solution +.#
3585 ~ 1. No. \(B\), \(C\) and \(D\) are non prime, \(A \to \{B,C\} \to D\) breaks the 3NF.
3586
3587 2. No. \(A\), \(B\) and \(D\) are non prime, \(B \to \{A,C\} \to D\) breaks the 3NF.
3643 ~ #. No. \(B\), \(C\) and \(D\) are non prime, \(A \to \{B,C\} \to D\) breaks the 3NF.
3644 #. No. \(A\), \(B\) and \(D\) are non prime, \(B \to \{A,C\} \to D\) breaks the 3NF.
3588 3645
3589 3646 Solution +.# Solution +.#
3590
3591 \(\{A, B\}\),
3647 ~ \(\{A, B\}\),
3592 3648
3593 \(C, D, E\),
3649 \(C, D, E\),
3594 3650
3595 \(R_1(A, B, C, E)\) and \(R_2(B, D)\)
3651 \(R_1(A, B, C, E)\) and \(R_2(B, D)\)
3596 3652
3597 \(R_1(A, B, C)\), \(R_2(C, E)\) and \(R_3(B, D)\)
3653 \(R_1(A, B, C)\), \(R_2(C, E)\) and \(R_3(B, D)\)
3598 3654
3599 3655 Solution +.# Solution +.#
3600 3656 ~ Behaviour and structure ~ Behaviour and structure
 
... ... Solution +.#
3603 3659 ~ Yes, U.M.L. diagram is language-independent and platform-independent. ~ Yes, U.M.L. diagram is language-independent and platform-independent.
3604 3660
3605 3661 Solution +.# Solution +.#
3606 ~ 1. Use-case
3607 2. Sequence diagram
3608 3. Activity diagram
3662 ~ #. Use-case
3663 #. Sequence diagram
3664 #. Activity diagram
3609 3665
3610 3666 Solution +.# Solution +.#
3611 3667 ~ To use direction for association, to have a common language with someone less knowledgeable of other diagrammatic notations. ~ To use direction for association, to have a common language with someone less knowledgeable of other diagrammatic notations.
3612 3668 For the concept of integration. For the concept of integration.
3613 3669
3614
3615 3670 Solution +.# Solution +.#
3616 3671 ~ \(5\) and \(4\). ~ \(5\) and \(4\).
3617 3672
 
... ... Solution +.#
3632 3687 ~ \tikz{\draw[>=open triangle 60,->] (0,0) to (1,0);} ~ \tikz{\draw[>=open triangle 60,->] (0,0) to (1,0);}
3633 3688 Because it avoids redundancy. Because it avoids redundancy.
3634 3689
3635
3636 3690 ## Problems ## Problems
3637 3691
3638 3692 Problem +.# Problem +.#
 
... ... ACTOR is used as a gender-netral term.
3668 3722 Given the constraints shown in the ER schema, respond to the following statements with *True* or *False*. Given the constraints shown in the ER schema, respond to the following statements with *True* or *False*.
3669 3723 Justify each answer. Justify each answer.
3670 3724
3671 #. There are no actors in this database that have been in no movies.
3672 #. There might be actors who have acted in more than ten movies.
3673 #. Some actors could have done a lead role in multiple movies.
3674 #. A movie can have only a maximum of two lead actors.
3675 #. Every director have to have been an actor in some movie.
3676 #. No producer has ever been an actor.
3677 #. A producer cannot be an actor in some other movie.
3678 #. There could be movies with more than a dozen actors.
3679 #. Producers can be directors as well.
3680 #. A movie can have one director and one producer.
3681 #. A movie can have one director and several producers.
3682 #. There could be some actors who have done a lead role, directed a movie, and produced some movie.
3683 #. It is impossible for a director to play in the movie (s)he directed.
3725 #. There are no actors in this database that have been in no movies.
3726 #. There might be actors who have acted in more than ten movies.
3727 #. Some actors could have done a lead role in multiple movies.
3728 #. A movie can have only a maximum of two lead actors.
3729 #. Every director have to have been an actor in some movie.
3730 #. No producer has ever been an actor.
3731 #. A producer cannot be an actor in some other movie.
3732 #. There could be movies with more than a dozen actors.
3733 #. Producers can be directors as well.
3734 #. A movie can have one director and one producer.
3735 #. A movie can have one director and several producers.
3736 #. There could be some actors who have done a lead role, directed a movie, and produced some movie.
3737 #. It is impossible for a director to play in the movie (s)he directed.
3684 3738
3685 3739
3686 3740 Problem +.# Problem +.#
 
... ... OR
3704 3758 \node[attribute] (name) [above left of=person] {Name} edge (person); \node[attribute] (name) [above left of=person] {Name} edge (person);
3705 3759 \node[attribute] (address) [below left of=person] {address} edge (person); \node[attribute] (address) [below left of=person] {address} edge (person);
3706 3760
3707 \node[relationship] (owns) [above right of=person] {Owns} edge node[above, sloped]{$M$} (person);
3708 \node[entity] (car) [above right of=owns] {Car} edge node[above, sloped]{$N$} (owns);
3761 \node[relationship] (owns) [above right of=person] {Owns} edge node[above, sloped]{\(M\)} (person);
3762 \node[entity] (car) [above right of=owns] {Car} edge node[above, sloped]{\(N\)} (owns);
3709 3763 \node[attribute] (licence) [above left of=car] {\key{licence}} edge (car); \node[attribute] (licence) [above left of=car] {\key{licence}} edge (car);
3710 3764 \node[attribute] (model) [above of =car] {model} edge (car); \node[attribute] (model) [above of =car] {model} edge (car);
3711 3765 \node[attribute] (year) [above right of =car] {year} edge (car); \node[attribute] (year) [above right of =car] {year} edge (car);
3712 3766
3713 \node[relationship] (accident) [right = 5cm of person] {Accident} edge[total] node[above, sloped]{$M$} (person);
3767 \node[relationship] (accident) [right = 5cm of person] {Accident} edge[total] node[above, sloped]{\(M\)} (person);
3714 3768 \node[attribute] (report_number) [below of = accident] {\key{Report Number}} edge (accident); \node[attribute] (report_number) [below of = accident] {\key{Report Number}} edge (accident);
3715 3769 \node[attribute] (time) [right of = accident] {Time} edge (accident); \node[attribute] (time) [right of = accident] {Time} edge (accident);
3716 3770 \node[attribute] (place) [below right of = accident] {Place} edge (accident); \node[attribute] (place) [below right of = accident] {Place} edge (accident);
3717 3771 \node[attribute] (damage_amount) [above right of = accident] {Damage\_Amount} edge (accident); \node[attribute] (damage_amount) [above right of = accident] {Damage\_Amount} edge (accident);
3718 3772
3719 \draw (car) edge[total] node[above, sloped]{$N$} (accident);
3773 \draw (car) edge[total] node[above, sloped]{\(N\)} (accident);
3720 3774 \end{tikzpicture} \end{tikzpicture}
3721 3775
3722 3776 Problem +.#ERtoREL Problem +.#ERtoREL
 
... ... Consider the following E.R. schema:
3733 3787 \node[attribute] (name) [left of=COUNTRY] {\key{Name}} edge (COUNTRY); \node[attribute] (name) [left of=COUNTRY] {\key{Name}} edge (COUNTRY);
3734 3788 \node[attribute] (population) [below left of=COUNTRY] {Population} edge (COUNTRY); \node[attribute] (population) [below left of=COUNTRY] {Population} edge (COUNTRY);
3735 3789
3736 \node[relationship] (SPEAKS) [below right of=COUNTRY] {SPEAKS} edge node[right, pos=0.4] {$M$} (COUNTRY);
3737 \node[entity] (LANGUAGE) [below right of=SPEAKS] {LANGUAGE} edge node[right, pos=0.5] {$N$} (SPEAKS);
3790 \node[relationship] (SPEAKS) [below right of=COUNTRY] {SPEAKS} edge node[right, pos=0.4] {\(M\)} (COUNTRY);
3791 \node[entity] (LANGUAGE) [below right of=SPEAKS] {LANGUAGE} edge node[right, pos=0.5] {\(N\)} (SPEAKS);
3738 3792 \node[attribute] (code) [left of = LANGUAGE] {\key{Code}} edge (LANGUAGE); \node[attribute] (code) [left of = LANGUAGE] {\key{Code}} edge (LANGUAGE);
3739 3793 \node[attribute] (symbol) [right of = LANGUAGE] {Name} edge (LANGUAGE); \node[attribute] (symbol) [right of = LANGUAGE] {Name} edge (LANGUAGE);
3740 3794
3741 3795 \node[relationship] (BWF) [below of = LANGUAGE] {B\_W\_F}; \node[relationship] (BWF) [below of = LANGUAGE] {B\_W\_F};
3742 \draw (LANGUAGE) to node[left, pos=0.6] {$N$} (BWF.west);
3743 \draw (LANGUAGE) to node[right, pos=0.6] {$M$} (BWF.east);
3796 \draw (LANGUAGE) to node[left, pos=0.6] {\(N\)} (BWF.west);
3797 \draw (LANGUAGE) to node[right, pos=0.6] {\(M\)} (BWF.east);
3744 3798
3745 3799 %\node[relationship] (boundary) [above of = COUNTRY] {S\_B\_W}; %\node[relationship] (boundary) [above of = COUNTRY] {S\_B\_W};
3746 %\draw (COUNTRY) to node[left, pos=0.6] {$N$} (boundary.west);
3747 %\draw (COUNTRY) to node[right, pos=0.6] {$M$} (boundary.east);
3800 %\draw (COUNTRY) to node[left, pos=0.6] {\(N\)} (boundary.west);
3801 %\draw (COUNTRY) to node[right, pos=0.6] {\(M\)} (boundary.east);
3748 3802
3749 3803 %\node[relationship] (conversion) [below of =LANGUAGE] {CONVERSION}; %\node[relationship] (conversion) [below of =LANGUAGE] {CONVERSION};
3750 %\draw (LANGUAGE) to node[left, pos=0.6] {$N$} (conversion.west);
3751 %\draw (LANGUAGE) to node[right, pos=0.6] {$M$} (conversion.east);
3804 %\draw (LANGUAGE) to node[left, pos=0.6] {\(N\)} (conversion.west);
3805 %\draw (LANGUAGE) to node[right, pos=0.6] {\(M\)} (conversion.east);
3752 3806
3753 3807 %\node[attribute] (timestamp) [below left of=conversion] {\key{Timestamp}} edge (conversion); %\node[attribute] (timestamp) [below left of=conversion] {\key{Timestamp}} edge (conversion);
3754 3808 %\node[attribute] (rate) [below right of=conversion] {Exchange\_rate} edge (conversion); %\node[attribute] (rate) [below right of=conversion] {Exchange\_rate} edge (conversion);
3755 3809
3756 \node[ident relationship] (hasfor) [right of=COUNTRY] {SINGS} edge node[above, pos=0.4] {$1$} (COUNTRY);
3810 \node[ident relationship] (hasfor) [right of=COUNTRY] {SINGS} edge node[above, pos=0.4] {\(1\)} (COUNTRY);
3757 3811 \node[weak entity] (anthem) %[right of=hasfor] \node[weak entity] (anthem) %[right of=hasfor]
3758 3812 [right = 1cm of hasfor] {NATIONAL\_ANTHEM} edge[total] node[above, pos=0.6]{\(M\)} (hasfor); [right = 1cm of hasfor] {NATIONAL\_ANTHEM} edge[total] node[above, pos=0.6]{\(M\)} (hasfor);
3759 3813 \node[multi attribute] (color) [right =1cm of anthem] {Creator} edge (anthem); \node[multi attribute] (color) [right =1cm of anthem] {Creator} edge (anthem);
3760 3814 \node[attribute] (name) [below right of = anthem] {\pkey{Name}} edge (anthem); \node[attribute] (name) [below right of = anthem] {\pkey{Name}} edge (anthem);
3761 3815
3762 3816 \node[relationship] (WIN) [above right of =LANGUAGE] {W\_IN}; \node[relationship] (WIN) [above right of =LANGUAGE] {W\_IN};
3763 \draw (LANGUAGE) to node[left, pos=0.6] {$N$} (WIN);
3764 \draw (anthem) to node[right, pos=0.6] {$M$} (WIN);
3817 \draw (LANGUAGE) to node[left, pos=0.6] {\(N\)} (WIN);
3818 \draw (anthem) to node[right, pos=0.6] {\(M\)} (WIN);
3765 3819 \end{tikzpicture} \end{tikzpicture}
3766 3820
3767 3821 %"S\_B\_W" stands for "SHARES\_A\_BORDER\_WITH". %"S\_B\_W" stands for "SHARES\_A\_BORDER\_WITH".
 
... ... Specify key attributes of each entity type and structural constraints on each re
3881 3935 Note any unspecified requirements, and make appropriate assumptions to make the specification complete. Note any unspecified requirements, and make appropriate assumptions to make the specification complete.
3882 3936
3883 3937
3884
3885 3938 Problem +.# Problem +.#
3886 3939
3887 3940 Consider the UML diagram below, and convert it to the relational model. Consider the UML diagram below, and convert it to the relational model.
 
... ... Consider the following E.R. schema for the CAR\_INFO database:
4007 4060 \node[attribute] (city) [right of=address] {City} edge (address); \node[attribute] (city) [right of=address] {City} edge (address);
4008 4061 %\node[derived attribute] (age) [right of=person] {Age} edge (person); %\node[derived attribute] (age) [right of=person] {Age} edge (person);
4009 4062
4010 \node[relationship] (drives) [below right of=person] {DRIVES} edge node[above, pos=0.1] {$1$} (person);
4011 \node[entity] (car) [below left of=drives] {CAR} edge node[above, pos=0.7] {$1$} (drives);
4063 \node[relationship] (drives) [below right of=person] {DRIVES} edge node[above, pos=0.1] {\(1\)} (person);
4064 \node[entity] (car) [below left of=drives] {CAR} edge node[above, pos=0.7] {\(1\)} (drives);
4012 4065 \node[attribute] (make) [left of=car] {Make} edge (car); \node[attribute] (make) [left of=car] {Make} edge (car);
4013 4066 \node[attribute] (year) [below left of =car] {Year} edge (car); \node[attribute] (year) [below left of =car] {Year} edge (car);
4014 4067 \node[attribute] (brand) [below of =car] {Brand} edge (car); \node[attribute] (brand) [below of =car] {Brand} edge (car);
4015 4068
4016 \node[relationship] (seats) [below left of=person] {SEATS\_IN} edge node[above, pos=0.1] {$N$} (person);
4017 \draw (seats) edge node[above, pos=0.3] {$1$} (car);
4069 \node[relationship] (seats) [below left of=person] {SEATS\_IN} edge node[above, pos=0.1] {\(N\)} (person);
4070 \draw (seats) edge node[above, pos=0.3] {\(1\)} (car);
4018 4071 \node[attribute] (position) [left of=seats] {Position} edge (seats); \node[attribute] (position) [left of=seats] {Position} edge (seats);
4019 4072
4020 \node[ident relationship] (insured) [right of=car] {INSURED} edge node[above, pos=0.3] {$1$} (car);
4021 \node[weak entity] (insurance) [right = 1cm of insured] {CAR\_INSURANCE} edge[total] node[above, pos=0.7] {$N$} (insured);
4073 \node[ident relationship] (insured) [right of=car] {INSURED} edge node[above, pos=0.3] {\(1\)} (car);
4074 \node[weak entity] (insurance) [right = 1cm of insured] {CAR\_INSURANCE} edge[total] node[above, pos=0.7] {\(N\)} (insured);
4022 4075 \node[attribute] (amount) [above of =insurance] {Covered Amount} edge (insurance); \node[attribute] (amount) [above of =insurance] {Covered Amount} edge (insurance);
4023 4076 \node[attribute] (policy) [above right of =insurance] {Policy Number} edge (insurance); \node[attribute] (policy) [above right of =insurance] {Policy Number} edge (insurance);
4024 4077 \node[attribute] (company) [below right of =insurance] {Company Name} edge (insurance); \node[attribute] (company) [below right of =insurance] {Company Name} edge (insurance);
 
... ... In this exercise, we will install and explore the basic functionalities of MySQL
4039 4092 2. Once installed, execute the software. Under the panel "MySQL Connections", you should see your local installation listed as "Local instance 3306". Click on the top-right corner of that box, and then on "Edit Connections". Alternatively, click on "Database", on "Manage Connections", and then on "Local instance 3306". 2. Once installed, execute the software. Under the panel "MySQL Connections", you should see your local installation listed as "Local instance 3306". Click on the top-right corner of that box, and then on "Edit Connections". Alternatively, click on "Database", on "Manage Connections", and then on "Local instance 3306".
4040 4093 3. Check that all the parameters are correct. Normally, you only have to change the name of the user to "testuser", and leave the rest as it is. Click on "Test the connection", and enter your password (which should be "password") when prompted. If you receive a warning about "Incompatible/nonstandard server version or connection protocol detected", click on "Continue anyway". 3. Check that all the parameters are correct. Normally, you only have to change the name of the user to "testuser", and leave the rest as it is. Click on "Test the connection", and enter your password (which should be "password") when prompted. If you receive a warning about "Incompatible/nonstandard server version or connection protocol detected", click on "Continue anyway".
4041 4094 4. 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. 4. 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.
4042 5. Click on "Database", and then on "Reverse Engineering" (or hit \texttt{ctrl} + \texttt{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".
4095 5. 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".
4043 4096 6. 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. 6. 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.
4044 4097 7. 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. 7. 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.
4045 8. 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 \texttt{SQL} code needed to produce the table you just designed using the graphical tool.
4098 8. 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.
4046 4099
4047 4100
4048 4101 Problem +.#ReverseEng Problem +.#ReverseEng
 
... ... include/sol.sql
4337 4390
4338 4391 # Databases Applications # Databases Applications
4339 4392
4340 ## References
4393 ## Resources
4341 4394
4342 4395 [@Textbook6, Ch. 13], [@Textbook7 , Chapter 10] [@Textbook6, Ch. 13], [@Textbook7 , Chapter 10]
4343 4396 <http://spots.augusta.edu/caubert/teaching/general/java/> <http://spots.augusta.edu/caubert/teaching/general/java/>
 
... ... For a quick introduction to Java, cf. <http://spots.augusta.edu/caubert/teaching
4393 4446
4394 4447 ## A First Program ## A First Program
4395 4448
4396 ### The Java Code
4449 ### The Java code
4397 4450
4398 4451 ~~~{.java .numberLines} ~~~{.java .numberLines}
4399 4452 import java.sql.*; import java.sql.*;
 
... ... A couple of comments:
4441 4494 - We could use `1`, `2`, and `3` instead of `"title"`, `"price"` and `"qty"` in the `while` loop: `getString`, `getDouble` and `getInt` also take integers, corresponding to the position of the attribute in the result set. - We could use `1`, `2`, and `3` instead of `"title"`, `"price"` and `"qty"` in the `while` loop: `getString`, `getDouble` and `getInt` also take integers, corresponding to the position of the attribute in the result set.
4442 4495
4443 4496
4444 ### The Database
4497 ### The database
4445 4498
4446 4499 ~~~{.plain} ~~~{.plain}
4447 4500 MariaDB [HW_ebookshop]> SELECT * FROM books; MariaDB [HW_ebookshop]> SELECT * FROM books;
 
... ... MariaDB [HW_ebookshop]> SELECT * FROM books;
4457 4510 5 rows in set (0.00 sec) 5 rows in set (0.00 sec)
4458 4511 ~~~ ~~~
4459 4512
4460 ### The Result
4513 ### The result
4461 4514
4462 4515 If you store the program in `FirstProg.java`, compile it, with If you store the program in `FirstProg.java`, compile it, with
4463 4516
 
... ... A Teaspoon of Java, 55.55, 55
4481 4534 Total number of records = 2 Total number of records = 2
4482 4535 ~~~ ~~~
4483 4536
4484 ### A Variation
4537 ### A variation
4485 4538
4486 4539 If you were to replace the body of `try` in the previous program with If you were to replace the body of `try` in the previous program with
4487 4540
 
... ... The records selected are:
4518 4571
4519 4572 ## Mapping Datatypes ## Mapping Datatypes
4520 4573
4521
4522
4523 4574 `SQL` | `JAVA` | `SQL` | `JAVA` |
4524 4575 --- | --- --- | ---
4525 4576 `INTEGER` | `int` `INTEGER` | `int`
 
... ... For more details, consult <https://docs.oracle.com/javase/7/docs/api/java/sql/St
4549 4600
4550 4601 --- ---
4551 4602
4552 ### Passing Options
4603 ## A Second Program
4604
4605 ### Passing options
4553 4606
4554 4607 We can pass options when connecting to the database: We can pass options when connecting to the database:
4555 4608
 
... ... Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HW_DB
4564 4617
4565 4618 `createDatabaseIfNotExist` is about schema, actually. `createDatabaseIfNotExist` is about schema, actually.
4566 4619
4567 ### Creating a Table
4620 ### Creating a table
4568 4621
4569 4622 Use `stmt.execute` and a create statement. Use `stmt.execute` and a create statement.
4570 4623 You can use the `getMetaData()` of the `DatabaseMetaData` to obtain information about the tables. You can use the `getMetaData()` of the `DatabaseMetaData` to obtain information about the tables.
4571 4624
4572 ### Inserting Values
4625 ### Inserting values
4573 4626
4574 4627 Use `stmt.executeUpdate` (multiple insertion possible if `allowMultiQueries` was set to true). Use `stmt.executeUpdate` (multiple insertion possible if `allowMultiQueries` was set to true).
4575 4628 Another way of batch processing statements: Another way of batch processing statements:
 
... ... stmt.addBatch(insert4);
4580 4633 stmt.executeBatch(); stmt.executeBatch();
4581 4634 ~~~ ~~~
4582 4635
4583 ### Prepared Statements
4636 ### Prepared statements
4584 4637
4585 4638 "A query with a slot": parsed and stored on the database, but not executed. "A query with a slot": parsed and stored on the database, but not executed.
4586 4639 When the program gives values, it is executed. When the program gives values, it is executed.
 
... ... Compared to executing SQL statements directly, prepared statements have three ma
4591 4644 - Minimize bandwidth (send only the parameters, and not the whole query) - Minimize bandwidth (send only the parameters, and not the whole query)
4592 4645 - Protect against SQL injections - Protect against SQL injections
4593 4646
4594 ### Advanced Statements Objects
4647 ### Advanced Statements objects
4595 4648
4596 4649 You can pass options when creating Statement objects to be able to read it both ways, and to be able to update rows. You can pass options when creating Statement objects to be able to read it both ways, and to be able to update rows.
4597 4650
 
... ... Exercise +.#
4605 4658 Exercise +.# Exercise +.#
4606 4659 ~ What JDBC method do you call to get a connection to a database? ~ What JDBC method do you call to get a connection to a database?
4607 4660
4608
4609 4661 Exercise +.# Exercise +.#
4610 4662 ~ Briefly explain what the `next()` method from the `ResultSet` class does, and give its return type. ~ Briefly explain what the `next()` method from the `ResultSet` class does, and give its return type.
4611 4663
 
... ... Exercise +.#
4621 4673 Exercise +.# Exercise +.#
4622 4674 ~ Explain this JDBC URL format: ~ Explain this JDBC URL format:
4623 4675
4624 ~~~{.java}
4625 jdbc:mysql://localhost:3306/HW_NewDB?createDatabaseIfNotExist=true&useSSL=true
4626 ~~~
4676 ~~~{.java}
4677 jdbc:mysql://localhost:3306/HW_NewDB?createDatabaseIfNotExist=true&useSSL=true
4678 ~~~
4627 4679
4628 4680 Exercise +.# Exercise +.#
4629 ~ In what class is the `getColumnName()} method?
4681 ~ In what class is the `getColumnName` method?
4630 4682
4631 4683 Exercise +.# Exercise +.#
4632 4684 ~ What is a prepared statement? ~ What is a prepared statement?
4633 4685
4634
4635
4636 4686 Exercise +.# Exercise +.#
4637 ~ 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.
4687 ~ In the code below, there are five 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.
4638 4688
4639 4689 ~~~{.java .numberLines} ~~~{.java .numberLines}
4640 4690 import java.sql.*; import java.sql.*;
 
... ... public class MyProg{
4677 4727
4678 4728 ## Solution to Exercises ## Solution to Exercises
4679 4729
4680
4681
4682 4730 Solution +.# Solution +.#
4683 4731 ~ API + driver ~ API + driver
4684 4732
4685
4686
4687
4688 4733 Solution +.# Solution +.#
4689 4734 ~ DriverManager.getConnection() ~ DriverManager.getConnection()
4690 4735
 
... ... Solution +.#
4692 4737 ~ It checks if there is data to read, and if move the cursor reads it. ~ It checks if there is data to read, and if move the cursor reads it.
4693 4738 It returns a Boolean. It returns a Boolean.
4694 4739
4695
4696 4740 Solution +.# Solution +.#
4697 4741 ~ Using ~ Using
4698 4742
4699 ~~~{.java}
4700 .executeQuery(strSelect)
4701 ~~~
4743 ~~~{.java}
4744 .executeQuery(strSelect)
4745 ~~~
4702 4746
4703 4747 Solution +.# Solution +.#
4704 4748 ~ Before the first line. Using the `next` method. ~ Before the first line. Using the `next` method.
4705 4749
4706 4750 Solution +.# Solution +.#
4707 ~ `first()`
4708 `last()`
4709 `next()`
4710 `previous()`
4711 `relative(rows)`
4712 `absolute(row)` method.
4751 ~ `first`, `last`, `next`, `previous`, `relative`, `absolute` methods.
4713 4752
4714 4753 Solution +.# Solution +.#
4715 ~ Connect to localhost:3306 and create a new database if needed, and use secure connection.
4754 ~ Connect to `localhost:3306` and create a new database if needed, and use secure connection.
4716 4755
4717 4756 Solution +.# Solution +.#
4718 ~ ResultSetMetaData
4757 ~ `ResultSetMetaData`
4719 4758
4720 4759 Solution +.# Solution +.#
4721 4760 ~ 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.
 
... ... List errors in program TO DO.
4746 4785
4747 4786 Problem +.# Problem +.#
4748 4787
4749 In the archive, navigate to \texttt{code/sql/}, open and read \texttt{HW\_ebookshop.sql}.
4788 In the archive, navigate to `code/sql/`, open and read `HW\_ebookshop.sql`.
4750 4789
4751 4790 Then, open a terminal (or command-line interpreter), navigate to the folder where you stored that file (using `cd`), and type Then, open a terminal (or command-line interpreter), navigate to the folder where you stored that file (using `cd`), and type
4752 4791
 
... ... We will compile and execute your first database application, using Java and MySQ
4773 4812
4774 4813 - I will assume that you have MySQL installed and set-up as indicated in Homeworks \#1 and \#2. - I will assume that you have MySQL installed and set-up as indicated in Homeworks \#1 and \#2.
4775 4814 - 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. - 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.
4776 - We need to set up the *driver* (or *connector*) to make the java \texttt{sql} API and MySQL communicate. To do so,
4815 - We need to set up the *driver* (or *connector*) to make the java `sql` API and MySQL communicate. To do so,
4777 4816 \begin{- ize} \begin{- ize}
4778 4817 - Go to \url{https://dev.mysql.com/downloads/connector/j/} - Go to \url{https://dev.mysql.com/downloads/connector/j/}
4779 4818 - Click on "Download" in front of "Platform Independent (Architecture Independent), ZIP Archive" - Click on "Download" in front of "Platform Independent (Architecture Independent), ZIP Archive"
 
... ... Problem +.#
4822 4861 There are three other programs in that archive: There are three other programs in that archive:
4823 4862 \begin{description} \begin{description}
4824 4863 \item[Variation.java] is the program we looked at during class. \item[Variation.java] is the program we looked at during class.
4825 \item[NullProg.java] is an attempt to answer the question whenever SQL's `NULL` is JAVA's \texttt{NULL} when it comes to strings.
4864 \item[NullProg.java] is an attempt to answer the question whenever SQL's `NULL` is JAVA's `NULL` when it comes to strings.
4826 4865 \item[AdvancedProg.java] is a long and commented program that introduces multiple new tools and ideas. \item[AdvancedProg.java] is a long and commented program that introduces multiple new tools and ideas.
4827 4866 \end{description} \end{description}
4828 4867
 
... ... Read, execute, break, edit, compile, patch, hack and (most importantly) understa
4833 4872
4834 4873 # A Bit About Security # A Bit About Security
4835 4874
4836 ## Common Things
4875 ## Resources
4876
4877 ## Usual Aspects
4837 4878
4838 4879 ### Threat model ### Threat model
4839 4880
 
... ... Read, execute, break, edit, compile, patch, hack and (most importantly) understa
4847 4888 - "You are as strong as your weakest link." - "You are as strong as your weakest link."
4848 4889 - Never trust the user or their computer. - Never trust the user or their computer.
4849 4890
4850 ### Control Measures
4891 ### Control measures
4851 4892
4852 4893 - Access control (user account, passwords, restrictions) - Access control (user account, passwords, restrictions)
4853 4894 - 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
4856 4897
4857 4898 **Insert short intro. to salting, cryptography.** **Insert short intro. to salting, cryptography.**
4858 4899
4859 ## Particular Things
4900 ## Specificities Of Databases
4860 4901
4861 4902 ### Attack ### Attack
4862 4903
 
... ... Can also be used for DBMS fingerprinting.
4890 4931 4. Escaping 4. Escaping
4891 4932 5. Be up-to-date, desactivate the options you are not using, read newsfeeds, 5. Be up-to-date, desactivate the options you are not using, read newsfeeds,
4892 4933
4934 ## To Be Determined
4893 4935
4936 **bogue** : check SLO and add relevant topics here.
4894 4937
4895 4938 # NoSQL # NoSQL
4896 4939
4940 ## Resources
4941
4942 [@NoSQLDistilled], <https://en.wikipedia.org/wiki/NoSQL>
4943 [@Sullivan2015], [@Textbook7, Chapter 24], [@DBLP:journals/sigmod/PavloA16]
4944 - <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>
4945
4897 4946
4898 4947 ## A Bit of History ## A Bit of History
4899 4948
4900 4949 Inspired from [@NoSQLDistilled, Chap. 1] Inspired from [@NoSQLDistilled, Chap. 1]
4901 4950
4902 ### DB Applications and Application DB
4951 ### DB applications and application DB
4903 4952
4904 4953 When you write a DB application, you have two options: When you write a DB application, you have two options:
4905 4954
 
... ... With option b., you develop an "application database", and you have more freedom
4911 4960
4912 4961 But people were attached to SQL and kept using it. But people were attached to SQL and kept using it.
4913 4962
4914 ### Clusters, Clusters…
4963 ### Clusters, clusters…
4915 4964
4916 4965 Increase in everything (traffic, size of data, number of clients, etc.) meant "up or out", and there was two ways to increase the ressources: Increase in everything (traffic, size of data, number of clients, etc.) meant "up or out", and there was two ways to increase the ressources:
4917 4966
 
... ... Option b. was generally less expensive, but came with two drawbacks w.r.t. datab
4923 4972 1. Cost of licences, 1. Cost of licences,
4924 4973 2. Force to perform "unnatural acts": relational model are really not made to be distributed 2. Force to perform "unnatural acts": relational model are really not made to be distributed
4925 4974
4926 ### A First Shift
4975 ### A first shift
4927 4976
4928 4977 - [Google Big Table](https://cloud.google.com/bigtable/), 2004 (made public in … 2015!) [@DBLP:conf/osdi/ChangDGHWBCFG06] - [Google Big Table](https://cloud.google.com/bigtable/), 2004 (made public in … 2015!) [@DBLP:conf/osdi/ChangDGHWBCFG06]
4929 4978 - [Amazon DynamoDB](https://aws.amazon.com/dynamodb/), 2004 (used in Simple Storage Service (S3) in 2007) - [Amazon DynamoDB](https://aws.amazon.com/dynamodb/), 2004 (used in Simple Storage Service (S3) in 2007)
 
... ... Also, the data is now
4950 4999
4951 5000 for traditional relational DBMS. for traditional relational DBMS.
4952 5001
4953 ### Gathering Forces
5002 ### Gathering forces
4954 5003
4955 5004 Multiple attempts, going in multiple directions. Multiple attempts, going in multiple directions.
4956 5005 A meetup to discuss them coined the term "NoSQL" in an attempt to have a "twittable" hashtag, and it stayed (even it is as specific as describing a dog with "no-cat"). A meetup to discuss them coined the term "NoSQL" in an attempt to have a "twittable" hashtag, and it stayed (even it is as specific as describing a dog with "no-cat").
 
... ... Today, no official definition, but NoSQL often implies the followig:
4965 5014
4966 5015 Most importantly: polyglot persistence, "using different data storage technologies to handle varying data storage needs." Most importantly: polyglot persistence, "using different data storage technologies to handle varying data storage needs."
4967 5016
4968 ### The Future?
5017 ### The future?
4969 5018
4970 5019 A lot of enthusiasm, also because it "frees the data" (and, actually, the metadata, cf. application/ld+json, JavaScript Object Notation for Linked Data, schema.org, etc.). A lot of enthusiasm, also because it "frees the data" (and, actually, the metadata, cf. application/ld+json, JavaScript Object Notation for Linked Data, schema.org, etc.).
4971 5020 Some of it will last for sure: polyglot persistency, the possibility of being schema-less, being "distributed first", the possibility of sacrifying consistency for greater good, etc. Some of it will last for sure: polyglot persistency, the possibility of being schema-less, being "distributed first", the possibility of sacrifying consistency for greater good, etc.
 
... ... Vs
4997 5046 - Powerfull query language (join is missing from SQL, has to be implemented on the application-side) - Powerfull query language (join is missing from SQL, has to be implemented on the application-side)
4998 5047 - Structured data storage (can be too restrictive) - Structured data storage (can be too restrictive)
4999 5048
5000 ### ACID Vs CAP
5049 ### ACID Vs CAP {#sec:AcidVsCAP}
5001 5050
5002 5051 ACID is the guarantee of validity even in the event of errors, power failures, etc. ACID is the guarantee of validity even in the event of errors, power failures, etc.
5003 5052
 
... ... CAP (a.k.a. Brewer's theorem): Roughly, "In a distributed system, one has to cho
5010 5059
5011 5060 ## Categories of NoSQL Systems ## Categories of NoSQL Systems
5012 5061
5013 Model | Description | Examples |
5062 Model | Description | Examples |
5014 5063 --- | --- | --- --- | --- | ---
5015 5064 Document-based | Data is stored as "documents" (JSON, for instance), accessible via their id (other indexes available). | [Apache CouchDB](https://couchdb.apache.org/) (simble for web applications, and reliable), [MongoDB](https://www.mongodb.com/) (easy to operate), [Couchbase](https://www.couchbase.com/) (high concurrency, and high availability). Document-based | Data is stored as "documents" (JSON, for instance), accessible via their id (other indexes available). | [Apache CouchDB](https://couchdb.apache.org/) (simble for web applications, and reliable), [MongoDB](https://www.mongodb.com/) (easy to operate), [Couchbase](https://www.couchbase.com/) (high concurrency, and high availability).
5016 5065 Key-value stores | Fast access by the key to the value. Value can be a record, an object, a document, or be even more complex. | [Redis](https://redis.io/) (in-memory but persistent on disk database, stores everything in the RAM!) Key-value stores | Fast access by the key to the value. Value can be a record, an object, a document, or be even more complex. | [Redis](https://redis.io/) (in-memory but persistent on disk database, stores everything in the RAM!)
 
... ... Column-based (a.k.a. wide column) | Partition a table by colmuns into column fam
5018 5067 Graph-based | Data is represented as graphs, and related nodes can be found by traversing the edges using path expressions. | [Neo4J](https://neo4j.com/) (excellent for pattern recognition, and data mining). Graph-based | Data is represented as graphs, and related nodes can be found by traversing the edges using path expressions. | [Neo4J](https://neo4j.com/) (excellent for pattern recognition, and data mining).
5019 5068 Multi-model | Support multiple data models | [Apache Ignite](https://ignite.apache.org/), [ArangoDB](https://www.arangodb.com/), etc. Multi-model | Support multiple data models | [Apache Ignite](https://ignite.apache.org/), [ArangoDB](https://www.arangodb.com/), etc.
5020 5069
5021
5022
5023 Sources:
5024 [@NoSQLDistilled], <https://en.wikipedia.org/wiki/NoSQL>
5025 [@Sullivan2015], [@Textbook7, Chapter 24], [@DBLP:journals/sigmod/PavloA16]
5026 - <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>
5027
5028 5070 ## MongoDB ## MongoDB
5029 5071
5030 5072 ### Introduction ### Introduction
 
... ... MongoDB is
5036 5078 - cross-platform - cross-platform
5037 5079 - document-oriented (JSON-like documents with schemas). - document-oriented (JSON-like documents with schemas).
5038 5080
5039 And it has drivers for
5040
5041 - C
5042 - C++
5043 - C#
5044 - Hadoop Connector
5045 - Haskell
5046 - Java
5047 - node.js
5048 - PHP
5049 - Perl
5050 - Python
5051 - Ruby
5052 - Scala (Casbah)
5081 And [there are drivers](https://docs.mongodb.com/ecosystem/drivers/) for C, C++, C#, Hadoop Connector, Haskell, Java, node.js, PHP, Perl, Python, Ruby, Scala (Casbah).
5053 5082
5054 5083 The mongo shell is an interactive JavaScript interface to MongoDB. The mongo shell is an interactive JavaScript interface to MongoDB.
5055 5084
5056 ### Document-oriented Database
5085 ### Document-oriented database
5057 5086
5058 5087 Document-oriented database (document store) contains semi-structured data, it is a subclass of the key-value store: Document-oriented database (document store) contains semi-structured data, it is a subclass of the key-value store:
5059 5088
 
... ... An example of XML (Extensible Markup Languag) document (you can actually convert
5115 5144 - No predefined tags - No predefined tags
5116 5145 - Extensible - Extensible
5117 5146
5118 ### General Organization of MongoDB Databases
5147 ### General organization of MongoDB databases
5119 5148
5120 5149 RDBMS | MongoDB | RDBMS | MongoDB |
5121 5150 --- | --- --- | ---
 
... ... Bottom line: think about your usage, and the kind of queries you will perform.
5164 5193
5165 5194 "Schema-less" does not mean "organization-less"! "Schema-less" does not mean "organization-less"!
5166 5195
5167 ### First Elements of Syntax
5196 ### First elements of syntax
5168 5197
5169 5198 `db.<name of the collection>.<command>` (`db` is **not** the name of the database, it is just the prefix). `db.<name of the collection>.<command>` (`db` is **not** the name of the database, it is just the prefix).
5170 5199
 
... ... Possibility to mimic some features (unique attributes), but no referential key i
5194 5223 Most insert / update / detele will return success as soon as one node received your command, but you may tweak them so that success is returned only once the operation has been performed on the majority of the nodes. Most insert / update / detele will return success as soon as one node received your command, but you may tweak them so that success is returned only once the operation has been performed on the majority of the nodes.
5195 5224
5196 5225
5197 ### Set Up
5226 ### Set up
5198 5227
5199 5228 Install `mongodb` (non-official version, not maintained by MongoDB) and download <https://mongodb.github.io/mongo-java-driver/>. Install `mongodb` (non-official version, not maintained by MongoDB) and download <https://mongodb.github.io/mongo-java-driver/>.
5200 5229
 
... ... Exercise +.#
5339 5368 Solution +.# Solution +.#
5340 5369 ~ To be written. ~ To be written.
5341 5370
5342
5343 5371 # Review Session # Review Session
5344 5372
5345 # Tools and Ressources
5346
5347 ## Typesetting
5348
5349 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).
5350
5351 To compile it, you will need
5352
5353 - To clone the source, using [git](https://git-scm.com/), and
5354
5355 ~~~{.bash}
5356 git clone https://rocketgit.com/user/caubert/CSCI_3410
5357 ~~~
5358
5359 - To install pandoc, cf. <http://pandoc.org/installing.html>
5360 - To produce `pdf` output, you will need LaTeX (the "Installing pandoc" page contains instructions)
5361 - To install pandoc-numbering. cf. <https://github.com/chdemko/pandoc-numbering#installation>
5362
5363 ## Various Ressources
5364
5365 On top of the [references](references), were useful in the writing of those notes:
5366
5367 - <https://www.1keydata.com/datawarehousing/data-modeling-levels.html>
5368 - <https://en.wikipedia.org/wiki/Unified_Modeling_Language>
5369 - <https://creately.com/blog/diagrams/class-diagram-relationships/>
5370 - <https://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html>
5371
5372 5373
5373 5374 # References {#ref} # References {#ref}
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/caubert/CSCI_3410

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/caubert/CSCI_3410

Clone this repository using git:
git clone git://git.rocketgit.com/user/caubert/CSCI_3410

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main