File exercises/sum.md changed (mode: 100644) (index c6f2a50..c0cafe5) |
... |
... |
pandoc sum.md --pdf-engine=xelatex --toc --filter pandoc-numbering --top-level-d |
45 |
45 |
|
|
46 |
46 |
--> |
--> |
47 |
47 |
|
|
48 |
|
|
|
49 |
|
|
|
50 |
|
Problem +.# |
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
|
%%% 06 |
|
55 |
|
|
|
56 |
|
|
|
57 |
|
Exercise +.# |
|
58 |
|
~ Consider the relation \(R(A, B, C, D, E)\) and the functional dependencies \( \{A, B\} \to C, B \to D, C \to E\). |
|
59 |
|
Answer the following: |
|
60 |
|
|
|
61 |
|
1. \(A\) by itself is not a primary key, but what is the only key that contains \(A\)? |
|
62 |
|
2. List the non-prime attributes. |
|
63 |
|
3. This relation is not in 2NF: what transformation can you operate to obtain a 2NF? |
|
64 |
|
4. One of the relation you obtained at the previous step is likely not to be in 3NF. |
|
65 |
|
Can you normalize it? |
|
66 |
|
If yes, how? |
|
67 |
|
|
|
68 |
|
Solution +.# |
|
69 |
|
|
|
70 |
|
\(\{A, B\}\), |
|
71 |
|
|
|
72 |
|
\(C, D, E\), |
|
73 |
|
|
|
74 |
|
\(R_1(A, B, C, E)\) and \(R_2(B, D)\) |
|
75 |
|
|
|
76 |
|
\(R_1(A, B, C)\), \(R_2(C, E)\) and \(R_3(B, D)\) |
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
|
Exercise +.# |
|
81 |
|
~ What are the two different categories of U.M.L. diagram? |
|
82 |
|
|
|
83 |
|
|
|
84 |
|
Solution +.# |
|
85 |
|
~ Behaviour and structure |
|
86 |
|
|
|
87 |
|
|
|
88 |
|
Exercise +.# |
|
89 |
|
~ Can a \texttt{C++} developer working on Linux and a \texttt{Java} developer working on MacOS use the same class diagram as a basis to write their programs? |
|
90 |
|
Justify your answer. |
|
91 |
|
|
|
92 |
|
|
|
93 |
|
Solution +.# |
|
94 |
|
~ Yes, U.M.L. diagram is language-independent and platform-independent. |
|
95 |
|
|
|
96 |
|
|
|
97 |
|
Exercise +.# |
|
98 |
|
~ What kind of diagram should we use if we want to \(…\) |
|
99 |
|
|
|
100 |
|
1. describe the functional behavior of the system as seen by the user? |
|
101 |
|
2. capture the flow of messages in a software? |
|
102 |
|
3. represent the workflow of actions of an user? |
|
103 |
|
|
|
104 |
|
Solution +.# |
|
105 |
|
~ 1. Use-case |
|
106 |
|
2. Sequence diagram |
|
107 |
|
3. Activity diagram |
|
108 |
|
|
|
109 |
|
Exercise +.# |
|
110 |
|
~ Name two reasons why one would want to use a U.M.L. class diagram over an E.-R. diagram to represent a conceptual schema. |
|
111 |
|
|
|
112 |
|
|
|
113 |
|
Solution +.# |
|
114 |
|
~ To use direction for association, to have a common language with someone less knowledgeable of other diagrammatic notations. |
|
115 |
|
For the concept of integration. |
|
116 |
|
|
|
117 |
|
|
|
118 |
|
Exercise +.# |
|
119 |
|
~ Consider the following diagram: |
|
120 |
|
|
|
121 |
|
\begin{tikzpicture}[scale = 0.8] |
|
122 |
|
\begin{class}[text width=7cm]{Flight}{0,0} |
|
123 |
|
\attribute{flightNumber : Integer} |
|
124 |
|
\attribute{departureTime : Date} |
|
125 |
|
\attribute{flightDuration : Minutes} |
|
126 |
|
\attribute{departingAirport : String} |
|
127 |
|
\attribute{arrivingAirport : String} |
|
128 |
|
\operation{delayFlight(numberOfMinutes : Minutes)} |
|
129 |
|
\operation{getArrivalTime( ) : Date} |
|
130 |
|
\end{class} |
|
131 |
|
\begin{class}{Plane}{11, -0.8} |
|
132 |
|
\attribute{airPlaneType : String} |
|
133 |
|
\attribute{maximumSpeed : MPH} |
|
134 |
|
\attribute{maximumDistance : Miles} |
|
135 |
|
\attribute{tailID : String} |
|
136 |
|
\end{class} |
|
137 |
|
\association{Plane}{}{0..1}{Flight}{0..*}{} |
|
138 |
|
\end{tikzpicture} |
|
139 |
|
|
|
140 |
|
Give the number of attributes for both classes, and suggest two operations for the class that doesn't have any. |
|
141 |
|
Discuss the multiplicities: why did the designer picked those values? |
|
142 |
|
|
|
143 |
|
|
|
144 |
|
Solution +.# |
|
145 |
|
~ \(5\) and \(4\). |
|
146 |
|
|
|
147 |
|
getLastFlightNumber() : Integer |
|
148 |
|
|
|
149 |
|
stMaximumSpeed(MPH) : void |
|
150 |
|
|
|
151 |
|
A flight could be assigned to no plane, and a plane could not be assigned to a flight. |
|
152 |
|
|
|
153 |
|
A plane can be assigned to multiple (or no) flights, but a flight must have at most one plane (and could have none). |
|
154 |
|
|
|
155 |
|
|
|
156 |
|
Exercise +.# |
|
157 |
|
~ Briefly explain the difference between an aggregation and a composition association. |
|
158 |
|
|
|
159 |
|
|
|
160 |
|
Solution +.# |
|
161 |
|
~ Aggregation: associated class can have an existence of its own. |
|
162 |
|
|
|
163 |
|
Composition association: class doesn't exist without the association. |
|
164 |
|
|
|
165 |
|
|
|
166 |
|
Exercise +.# |
|
167 |
|
~ How is generalization (or inheritance) represented in a U.M.L. class diagram? |
|
168 |
|
Why is such a concept useful? |
|
169 |
|
|
|
170 |
|
|
|
171 |
|
Solution +.# |
|
172 |
|
~ \tikz{\draw[>=open triangle 60,->] (0,0) to (1,0);} |
|
173 |
|
Because it avoids redundancy. |
|
174 |
|
|
|
175 |
|
|
|
176 |
|
Exercise +.# |
|
177 |
|
~ Convert the following E.R. diagram into a U.M.L. class diagram: |
|
178 |
|
|
|
179 |
|
\begin{tikzpicture}[node distance=8em] |
|
180 |
|
\node[entity] (person) {PILOT}; |
|
181 |
|
\node[attribute] (pid) [left of=person] {\key{ID}} edge (person); |
|
182 |
|
\node[attribute] (name) [above left of=person] {Name} edge (person); |
|
183 |
|
\node[attribute] (phone) [above of=person] {Experience} edge (person); |
|
184 |
|
|
|
185 |
|
\node[relationship] (drives) [below of=person] {ASSIGNED\_TO} edge node[right, pos=0.1] {$N$} (person); |
|
186 |
|
\node[entity] (plane) [below of=drives] {PLANE} edge [total] node[right, pos=0.7] {$1$} (drives); |
|
187 |
|
\node[attribute] (make) [left of=plane] {\key{TailID}} edge (plane); |
|
188 |
|
\node[attribute] (maxS) [below left of =plane] {MaxSpeed} edge (plane); |
|
189 |
|
\node[attribute] (airPlaneType) [below of = plane] {AirPlaneType} edge (plane); |
|
190 |
|
\end{tikzpicture} |
|
191 |
|
|
|
192 |
|
Problem +.# |
|
193 |
|
|
|
194 |
|
\label{probMYSQLW} |
|
195 |
|
Consider the following E.R. schema for the CAR\_INFO database: |
|
196 |
|
|
|
197 |
|
\begin{tikzpicture}[node distance=8em] |
|
198 |
|
\node[entity] (person) {PERSON}; |
|
199 |
|
\node[attribute] (pid) [left of=person] {\key{ID}} edge (person); |
|
200 |
|
\node[attribute] (name) [above left of=person] {Name} edge (person); |
|
201 |
|
\node[multi attribute] (phone) [above of=person] {Phone} edge (person); |
|
202 |
|
\node[attribute] (address) [above right of=person] {Address} edge (person); |
|
203 |
|
\node[attribute] (street) [above right of=address] {Street} edge (address); |
|
204 |
|
\node[attribute] (city) [right of=address] {City} edge (address); |
|
205 |
|
%\node[derived attribute] (age) [right of=person] {Age} edge (person); |
|
206 |
|
|
|
207 |
|
\node[relationship] (drives) [below right of=person] {DRIVES} edge node[above, pos=0.1] {$1$} (person); |
|
208 |
|
\node[entity] (car) [below left of=drives] {CAR} edge node[above, pos=0.7] {$1$} (drives); |
|
209 |
|
\node[attribute] (make) [left of=car] {Make} edge (car); |
|
210 |
|
\node[attribute] (year) [below left of =car] {Year} edge (car); |
|
211 |
|
\node[attribute] (brand) [below of =car] {Brand} edge (car); |
|
212 |
|
|
|
213 |
|
\node[relationship] (seats) [below left of=person] {SEATS\_IN} edge node[above, pos=0.1] {$N$} (person); |
|
214 |
|
\draw (seats) edge node[above, pos=0.3] {$1$} (car); |
|
215 |
|
\node[attribute] (position) [left of=seats] {Position} edge (seats); |
|
216 |
|
|
|
217 |
|
\node[ident relationship] (insured) [right of=car] {INSURED} edge node[above, pos=0.3] {$1$} (car); |
|
218 |
|
\node[weak entity] (insurance) [right = 1cm of insured] {CAR\_INSURANCE} edge[total] node[above, pos=0.7] {$N$} (insured); |
|
219 |
|
\node[attribute] (amount) [above of =insurance] {Covered Amount} edge (insurance); |
|
220 |
|
\node[attribute] (policy) [above right of =insurance] {Policy Number} edge (insurance); |
|
221 |
|
\node[attribute] (company) [below right of =insurance] {Company Name} edge (insurance); |
|
222 |
|
\end{tikzpicture} |
|
223 |
|
|
|
224 |
|
Note that a car can have at most one driver, \(N\) passengers, \(N\) insurances, and that car insurances exist only if they are "tied up" to a car (i.e., they are weak entities, and their identifying relationship is called "Insured"). |
|
225 |
|
|
|
226 |
|
1. Find the key attribute for "Car", and the partial key for "Car Insurance". If you can't think of any, add a dummy attribute and make it be the key. |
|
227 |
|
2. Convert that E.-R. diagram to a relational database schema. |
|
228 |
|
3. Convert the E.-R. diagram to a U.M.L. class diagram. Comparing Figure 7.16 with Figure 7.2 from your textbook should guide you. |
|
229 |
|
|
|
230 |
|
Solution +.# |
|
231 |
|
~ For "Car", we need to create an attribute, like "vin". |
|
232 |
|
For "Car Insurance", "Policy Number" is perfect. |
|
233 |
|
|
|
234 |
|
\colorlet{lightgray}{gray!20} |
|
235 |
|
|
|
236 |
|
\begin{tikzpicture}[relation/.style={rectangle split, rectangle split parts=#1, rectangle split part align=base, draw, anchor=center, align=center, text height=3mm, text centered}]\hspace*{-0.3cm} |
|
237 |
|
|
|
238 |
|
% RELATIONS |
|
239 |
|
|
|
240 |
|
\node (phonetitle) {**PHONE**}; |
|
241 |
|
|
|
242 |
|
\node [relation=2, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of phonetitle.west, anchor=west] (phone) |
|
243 |
|
{\underline{id}% |
|
244 |
|
\nodepart{two} \underline{number} |
|
245 |
|
}; |
|
246 |
|
|
|
247 |
|
\node [below=1.3cm of phone.west, anchor=west] (persontitle) {**PERSON**}; |
|
248 |
|
|
|
249 |
|
\node [relation=6, rectangle split horizontal, rectangle split part fill={lightgray!50}, below=0.6cm of persontitle.west, anchor=west] (person) |
|
250 |
|
{\underline{id}% |
|
251 |
|
\nodepart{two} Name |
|
252 |
|
\nodepart{three} Street |
|
253 |
|
\nodepart{four} City |
|
254 |
|
\nodepart{five} Seat |
|
255 |
|
\nodepart{six}{Position} |
|
256 |
|
}; |
|
257 |
|
|
|
258 |
|
\node [below=1.1cm of person.west, anchor=west] (cartitle) {**CAR**}; |
|
259 |
|
|
|
260 |
|
\node [relation=5, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of cartitle.west, anchor=west] (car) |
|
261 |
|
{\underline{Vin}% |
|
262 |
|
\nodepart{two} Make |
|
263 |
|
\nodepart{three} Year |
|
264 |
|
\nodepart{four} Brand |
|
265 |
|
\nodepart{five} Driver |
|
266 |
|
}; |
|
267 |
|
|
|
268 |
|
\node [below=1.4cm of car.west, anchor=west] (carinsurancetitle) {**CAR INSURANCE**}; |
|
269 |
|
|
|
270 |
|
\node [relation=4, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of carinsurancetitle.west, anchor=west] (carinsurance) |
|
271 |
|
{\underline{Policy Number}% |
|
272 |
|
\nodepart{two} Covered Amount |
|
273 |
|
\nodepart{three} Company Name |
|
274 |
|
\nodepart{four} Insured Car |
|
275 |
|
}; |
|
276 |
|
|
|
277 |
|
% FOREIGN KEYS |
|
278 |
|
|
|
279 |
|
\draw[-latex] (phone.one south) -- ++(0,-0.2) --++(7,0) --++(0, -2) -| ($(person.one south) + (0.2,0)$); |
|
280 |
|
%\draw[-latex] (person.five south) -- ++(0,-0.2) --++(3,0) --++(0, -2) -| ($(car.one south) + (-0.2,0)$); |
|
281 |
|
\draw[-latex] (carinsurance.four south) -- ++(0,-0.2) --++(2,0) --++(0, 1.75) -| ($(car.one south) + (0.1,0)$); |
|
282 |
|
\draw[-latex] (car.five south) -- ++(0,-0.2) --++(2,0) --++(0, 1.5) -| ($(person.one south) + (-0.1,0)$); |
|
283 |
|
\end{tikzpicture} |
|
284 |
|
|
|
285 |
|
|
|
286 |
|
\begin{tikzpicture}[scale = 0.8] |
|
287 |
|
\node (person) at (0,0){\begin{tabular}{| r l |} |
|
288 |
|
\hline |
|
289 |
|
\multicolumn{2}{| c |}{**Person**}\\ |
|
290 |
|
\hline |
|
291 |
|
id &: String\\ |
|
292 |
|
name &: String\\ |
|
293 |
|
address &: Street\\ |
|
294 |
|
& City\\ |
|
295 |
|
\hline |
|
296 |
|
getAge()&: Int\\ |
|
297 |
|
\hline |
|
298 |
|
\end{tabular} |
|
299 |
|
}; |
|
300 |
|
\node (phone) at (8, 1){\begin{tabular}{| r l |} |
|
301 |
|
\hline |
|
302 |
|
\multicolumn{2}{| c |}{**Phone**}\\ |
|
303 |
|
\hline |
|
304 |
|
number &: String\\ |
|
305 |
|
\hline |
|
306 |
|
\end{tabular} |
|
307 |
|
}; |
|
308 |
|
\draw[open diamond-] (person) -- node[above, pos=0.1]{\(0..\star\)} node[above, pos=0.9]{\(1..1\)} (phone); |
|
309 |
|
\node (car) at (8, -3){\begin{tabular}{| r l |} |
|
310 |
|
\hline |
|
311 |
|
\multicolumn{2}{| c |}{**Car**}\\ |
|
312 |
|
\hline |
|
313 |
|
vin &: String\\ |
|
314 |
|
make &: String\\ |
|
315 |
|
year &: Year\\ |
|
316 |
|
brand &: String\\ |
|
317 |
|
\hline |
|
318 |
|
\end{tabular} |
|
319 |
|
}; |
|
320 |
|
\draw[-] ($(person)+(2.5,-0.5)$) -- node[above, pos=0.1]{\(0..1\)} node[above, pos=0.5]{drives} node[above, pos=0.9]{\(0..1\)} (car); |
|
321 |
|
%\draw[-] (person) -- node[below, pos=0.1]{\(0..1\)} node[below, pos=0.9]{\(0..4\)} ($(car)+(-2.5, 0.5)$); |
|
322 |
|
\node (seat) at (3.5, -5){\begin{tabular}{| r l |} |
|
323 |
|
\hline |
|
324 |
|
\multicolumn{2}{| c |}{**Seats In**}\\ |
|
325 |
|
\hline |
|
326 |
|
position &: String\\ |
|
327 |
|
\hline |
|
328 |
|
\end{tabular} |
|
329 |
|
}; |
|
330 |
|
\draw[dashed] (seat) -- (3.5, -1.6); |
|
331 |
|
|
|
332 |
|
\node (insu) at (14.3, -2){\begin{tabular}{| r l |} |
|
333 |
|
\hline |
|
334 |
|
\multicolumn{2}{| c |}{**Car Insurance**}\\ |
|
335 |
|
\hline |
|
336 |
|
policy number &: String\\ |
|
337 |
|
covered amount &: int\\ |
|
338 |
|
compagny name &: String\\ |
|
339 |
|
\hline |
|
340 |
|
\end{tabular} |
|
341 |
|
}; |
|
342 |
|
|
|
343 |
|
\draw[diamond -] (car) -- node[above, pos=0.1]{\(0..\star\)} node[above, pos=0.9]{\(1\)} (insu); |
|
344 |
|
\end{tikzpicture} |
|
345 |
|
|
|
346 |
|
|
|
347 |
|
|
|
348 |
|
Problem +.# |
|
349 |
|
|
|
350 |
|
In this exercise, we will install and explore the basic functionalities of MySQL Workbench, which is a cross-platform, open-source, and free graphical interface for database design. |
|
351 |
|
|
|
352 |
|
1. Install MySQL Workbench: use your package manager, or download the binaries from \url{https://dev.mysql.com/downloads/workbench/}. |
|
353 |
|
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". |
|
354 |
|
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". |
|
355 |
|
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. |
|
356 |
|
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". |
|
357 |
|
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. |
|
358 |
|
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. |
|
359 |
|
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. |
|
360 |
|
|
|
361 |
|
|
|
362 |
|
Problem +.# |
|
363 |
|
|
|
364 |
|
\emph{This problem requires you to have successfully completed Problem~ \ref{probMYSQLW} and Problem~ \ref{probERtoREL}.} |
|
365 |
|
|
|
366 |
|
Using the relational database schema you obtained in question~ \ref{ERtoREL} of Problem~ \ref{probERtoREL}, write the \texttt{SQL} implementation of that database. |
|
367 |
|
Then, using MySQL Workbench, use the "Reverse Engineering" function to obtain a E.E.R. diagram of your database, and compare it with the U.M.L. diagram you draw in question~ \ref{ERtoUML} of Problem~ \ref{probERtoREL}. |
|
368 |
|
Apart from the difference inherent to the nature of the diagram (i.e., U.M.L. Vs E.E.R.), how are they the same? How do they differ? Is the automated tool as efficient and accurate as you are? |
|
369 |
|
|
|
370 |
|
|
|
371 |
|
Solution +.# |
|
372 |
|
~ <!-- |
|
373 |
|
\inputminted{sql}{include/sol.sql} |
|
374 |
|
\includegraphics{include/mysql_workbench_drawing-crop} |
|
375 |
|
--> |
|
376 |
|
|
|
377 |
|
<!-- |
|
378 |
|
~~~include |
|
379 |
|
include/sol.sql |
|
380 |
|
~~~ |
|
381 |
|
|
|
382 |
|
--> |
|
383 |
|
|
|
384 |
|
**Code à inclure ici** |
|
385 |
|
|
|
386 |
|
 |
|
387 |
|
|
|
388 |
|
%%% 07 |
|
389 |
|
|
|
390 |
|
|
|
391 |
|
Exercise +.# |
|
392 |
|
~ What are the technologies that makes it possible for a Java application to communicate with a DBMS? |
|
393 |
|
|
|
394 |
|
|
|
395 |
|
Solution +.# |
|
396 |
|
~ API + driver |
|
397 |
|
|
|
398 |
|
|
|
399 |
|
Exercise +.# |
|
400 |
|
~ What JDBC method do you call to get a connection to a database? |
|
401 |
|
|
|
402 |
|
Solution +.# |
|
403 |
|
~ DriverManager.getConnection() |
|
404 |
|
|
|
405 |
|
|
|
406 |
|
Exercise +.# |
|
407 |
|
~ Briefly explain what the \texttt{next()} method from the \texttt{ResultSet} class does, and what is its return type. |
|
408 |
|
|
|
409 |
|
Solution +.# |
|
410 |
|
~ It checks if there is data to read, and if move the cursor reads it. |
|
411 |
|
It returns a Boolean. |
|
412 |
|
|
|
413 |
|
|
|
414 |
|
Exercise +.# |
|
415 |
|
~ How do you submit a `SELECT` statement to the DBMS? |
|
416 |
|
|
|
417 |
|
|
|
418 |
|
Solution +.# |
|
419 |
|
~ Using |
|
420 |
|
|
|
421 |
|
~~~{.java} |
|
422 |
|
.executeQuery(strSelect) |
|
423 |
|
~~~ |
|
424 |
|
|
|
425 |
|
Exercise +.# |
|
426 |
|
~ Where is a ResultSet object's cursor initially pointing? How do you move the cursor forward in the result set? |
|
427 |
|
|
|
428 |
|
|
|
429 |
|
Solution +.# |
|
430 |
|
~ Before the first line. |
|
431 |
|
|
|
432 |
|
~~~{.java} |
|
433 |
|
next() |
|
434 |
|
~~~ |
|
435 |
|
|
|
436 |
|
method |
|
437 |
|
|
|
438 |
|
Exercise +.# |
|
439 |
|
~ Give three navigation methods provided by `ResultSet`. |
|
440 |
|
|
|
441 |
|
|
|
442 |
|
Solution +.# |
|
443 |
|
~ `first()` |
|
444 |
|
`last()` |
|
445 |
|
`next()` |
|
446 |
|
`previous()` |
|
447 |
|
`relative(rows)` |
|
448 |
|
`absolute(row)` method. |
|
449 |
|
|
|
450 |
|
|
|
451 |
|
Exercise +.# |
|
452 |
|
~ Explain this JDBC URL format: |
|
453 |
|
|
|
454 |
|
~~~{.java} |
|
455 |
|
jdbc:mysql://localhost:3306/HW_NewDB?createDatabaseIfNotExist=true&useSSL=true |
|
456 |
|
~~~ |
|
457 |
|
|
|
458 |
|
Solution +.# |
|
459 |
|
~ Connect to localhost:3306 and create a new database if needed, and use secure connection. |
|
460 |
|
|
|
461 |
|
|
|
462 |
|
Exercise +.# |
|
463 |
|
~ In what class is the `getColumnName()} method? |
|
464 |
|
|
|
465 |
|
|
|
466 |
|
Solution +.# |
|
467 |
|
~ ResultSetMetaData |
|
468 |
|
|
|
469 |
|
|
|
470 |
|
Exercise +.# |
|
471 |
|
~ What is a prepared statement? |
|
472 |
|
|
|
473 |
|
|
|
474 |
|
Solution +.# |
|
475 |
|
~ A prepared statement is a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency. |
|
476 |
|
|
|
477 |
|
|
|
478 |
|
Exercise +.# |
|
479 |
|
~ In the code below, there are \(5\) errors between line \(11\) and line \(30\). They are \emph{not} subtle Java errors (like misspelling a key word) and do not come from the DBMS (so you should assume that the password is correct, that the database exists, etc.). For each error, highlight it precisely and give a short explanation. |
|
480 |
|
|
|
481 |
|
~~~{.java} |
|
482 |
|
import java.sql.*; |
|
483 |
|
|
|
484 |
|
public class MyProg{ |
|
485 |
|
public static void main(String[] args) { |
|
486 |
|
try ( |
|
487 |
|
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" |
|
488 |
|
+"HW_TestDB?user=testuser&password=password"); |
|
489 |
|
Statement stmt = conn.createStatement(); |
|
490 |
|
) { |
|
491 |
|
|
|
492 |
|
/* Errors after this point.*/ |
|
493 |
|
|
|
494 |
|
String strSelect = "SELECT title WHERE qty > 40 FROM disks;"; |
|
495 |
|
ResultSet rset = stmt.executeUpdate(strSelect); |
|
496 |
|
|
|
497 |
|
System.out.println("The records selected are: (listed last first):"); |
|
498 |
|
rset.last(); |
|
499 |
|
|
|
500 |
|
while(rset.previous()) { |
|
501 |
|
String title = rset.getDouble("title"); |
|
502 |
|
System.out.println(title + "\n"); |
|
503 |
|
} |
|
504 |
|
|
|
505 |
|
String sss = "SELECT title FROM disks WHERE Price <= ?"; |
|
506 |
|
PreparedStatement ps = conn.prepareStatement(sss); |
|
507 |
|
ResultSet result = ps.executeQuery(); |
|
508 |
|
|
|
509 |
|
conn.close(); |
|
510 |
|
|
|
511 |
|
/* Errors before this point.*/ |
|
512 |
|
|
|
513 |
|
} catch(SQLException ex) { |
|
514 |
|
ex.printStackTrace(); |
|
515 |
|
} |
|
516 |
|
} |
|
517 |
|
} |
|
518 |
|
~~~ |
|
519 |
|
|
|
520 |
|
% |
|
521 |
|
%METADATA |
|
522 |
|
% |
|
523 |
|
%ResultSet is array, other? |
|
524 |
|
% |
|
525 |
|
%Multiple queries |
|
526 |
|
% |
|
527 |
|
%qet datatype |
|
528 |
|
% |
|
529 |
|
%prepared statemernt. |
|
530 |
|
% |
|
531 |
|
%http://tutorials.jenkov.com/jdbc/resultset.html |
|
532 |
|
|
|
533 |
|
|
|
534 |
|
|
|
535 |
48 |
This part will mainly hands on, and ask you to read code. |
This part will mainly hands on, and ask you to read code. |
536 |
49 |
It is important that you learn how to get a working environment for a new technology quickly, for your understanding of this lecture, for the exam, and as a CS major. |
It is important that you learn how to get a working environment for a new technology quickly, for your understanding of this lecture, for the exam, and as a CS major. |
537 |
50 |
I'll assume that you will have successfully completed those tasks , so don't wait and let me know if you had difficulties solving them. |
I'll assume that you will have successfully completed those tasks , so don't wait and let me know if you had difficulties solving them. |
File notes/00_sum.md changed (mode: 100644) (index 53b9c2f..629165a) |
... |
... |
EXIT; |
1931 |
1931 |
|
|
1932 |
1932 |
Problem-.+.#creatingSchema |
Problem-.+.#creatingSchema |
1933 |
1933 |
|
|
1934 |
|
*This exercise assume you successfully completed @installation.* |
|
|
1934 |
|
*This exercise assume you successfully completed @problem:installation.* |
1935 |
1935 |
|
|
1936 |
1936 |
#. Connect to your MySQL DBMS as `testuser`: |
#. Connect to your MySQL DBMS as `testuser`: |
1937 |
1937 |
- **In windows**, open a command prompt (search for "cmd") and type |
- **In windows**, open a command prompt (search for "cmd") and type |
|
... |
... |
Since there are small variations from one implementation to the other, it's bett |
2021 |
2021 |
|
|
2022 |
2022 |
### Creating and Using a Table |
### Creating and Using a Table |
2023 |
2023 |
|
|
2024 |
|
*This exercise, and the following ones, assume you successfully completed @installation.* |
|
|
2024 |
|
*This exercise, and the following ones, assume you successfully completed @problem:installation.* |
2025 |
2025 |
|
|
2026 |
2026 |
Log in as `testuser` and create a database `HW_2Q3`. |
Log in as `testuser` and create a database `HW_2Q3`. |
2027 |
2027 |
|
|
|
... |
... |
and to examine carefully the message printed. You should read |
2095 |
2095 |
+------------+-------------+------+-----+---------+-------+ |
+------------+-------------+------+-----+---------+-------+ |
2096 |
2096 |
~~~ |
~~~ |
2097 |
2097 |
|
|
2098 |
|
If you believe there is a mistake, you can erase ("drop") the \emph{table} (not the whole database, as we did in @creatingSchema) using |
|
|
2098 |
|
If you believe there is a mistake, you can erase ("drop") the \emph{table} (not the whole database, as we did in @problem:creatingSchema) using |
2099 |
2099 |
|
|
2100 |
2100 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
2101 |
2101 |
DROP TABLE ADDRESS; |
DROP TABLE ADDRESS; |
|
... |
... |
Populate them with some data. |
2286 |
2286 |
## Solution to Selected Problems |
## Solution to Selected Problems |
2287 |
2287 |
|
|
2288 |
2288 |
### |
### |
2289 |
|
Solution for @#repetition |
|
|
2289 |
|
Solution for @problem:repetition |
2290 |
2290 |
~ |
~ |
2291 |
2291 |
|
|
2292 |
2292 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
|
... |
... |
Exercise +.# |
3219 |
3219 |
#. 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. |
3220 |
3220 |
|
|
3221 |
3221 |
|
|
|
3222 |
|
Exercise +.# |
|
3223 |
|
~ 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: |
|
3224 |
|
|
|
3225 |
|
#. \(A\) by itself is not a primary key, but what is the only key that contains \(A\)? |
|
3226 |
|
#. List the non-prime attributes. |
|
3227 |
|
#. This relation is not in 2NF: what transformation can you operate to obtain a 2NF? |
|
3228 |
|
#. One of the relation you obtained at the previous step is likely not to be in 3NF. Can you normalize it? If yes, how? |
|
3229 |
|
|
|
3230 |
|
Exercise +.# |
|
3231 |
|
~ What are the two different categories of U.M.L. diagram? |
|
3232 |
|
|
|
3233 |
|
Exercise +.# |
|
3234 |
|
~ Can a `C++` developer working on Linux and a `Java` developer working on MacOS use the same class diagram as a basis to write their programs? |
|
3235 |
|
Justify your answer. |
|
3236 |
|
|
|
3237 |
|
Exercise +.# |
|
3238 |
|
~ What kind of diagram should we use if we want to … |
|
3239 |
|
|
|
3240 |
|
#. describe the functional behavior of the system as seen by the user? |
|
3241 |
|
#. capture the flow of messages in a software? |
|
3242 |
|
#. represent the workflow of actions of an user? |
|
3243 |
|
|
|
3244 |
|
Exercise +.# |
|
3245 |
|
~ Name two reasons why one would want to use a U.M.L. class diagram over an E.-R. diagram to represent a conceptual schema. |
|
3246 |
|
|
|
3247 |
|
Exercise +.# |
|
3248 |
|
~ Consider the following diagram: |
|
3249 |
|
|
|
3250 |
|
\begin{tikzpicture}[scale = 0.8] |
|
3251 |
|
\begin{class}[text width=7cm]{Flight}{0,0} |
|
3252 |
|
\attribute{flightNumber : Integer} |
|
3253 |
|
\attribute{departureTime : Date} |
|
3254 |
|
\attribute{flightDuration : Minutes} |
|
3255 |
|
\attribute{departingAirport : String} |
|
3256 |
|
\attribute{arrivingAirport : String} |
|
3257 |
|
\operation{delayFlight(numberOfMinutes : Minutes)} |
|
3258 |
|
\operation{getArrivalTime( ) : Date} |
|
3259 |
|
\end{class} |
|
3260 |
|
\begin{class}{Plane}{11, -0.8} |
|
3261 |
|
\attribute{airPlaneType : String} |
|
3262 |
|
\attribute{maximumSpeed : MPH} |
|
3263 |
|
\attribute{maximumDistance : Miles} |
|
3264 |
|
\attribute{tailID : String} |
|
3265 |
|
\end{class} |
|
3266 |
|
\association{Plane}{}{0..1}{Flight}{0..*}{} |
|
3267 |
|
\end{tikzpicture} |
|
3268 |
|
|
|
3269 |
|
Give the number of attributes for both classes, and suggest two operations for the class that doesn't have any. Discuss the multiplicities: why did the designer picked those values? |
|
3270 |
|
|
|
3271 |
|
Exercise +.# |
|
3272 |
|
~ Briefly explain the difference between an aggregation and a composition association. |
|
3273 |
|
|
|
3274 |
|
|
|
3275 |
|
Exercise +.# |
|
3276 |
|
~ How is generalization (or inheritance) represented in a U.M.L. class diagram? |
|
3277 |
|
Why is such a concept useful? |
|
3278 |
|
|
|
3279 |
|
Exercise +.# |
|
3280 |
|
~ Convert the following E.R. diagram into a U.M.L. class diagram: |
|
3281 |
|
|
|
3282 |
|
\begin{tikzpicture}[node distance=8em] |
|
3283 |
|
\node[entity] (person) {PILOT}; |
|
3284 |
|
\node[attribute] (pid) [left of=person] {\key{ID}} edge (person); |
|
3285 |
|
\node[attribute] (name) [above left of=person] {Name} edge (person); |
|
3286 |
|
\node[attribute] (phone) [above of=person] {Experience} edge (person); |
|
3287 |
|
|
|
3288 |
|
\node[relationship] (drives) [below of=person] {ASSIGNED\_TO} edge node[right, pos=0.1] {$N$} (person); |
|
3289 |
|
\node[entity] (plane) [below of=drives] {PLANE} edge [total] node[right, pos=0.7] {$1$} (drives); |
|
3290 |
|
\node[attribute] (make) [left of=plane] {\key{TailID}} edge (plane); |
|
3291 |
|
\node[attribute] (maxS) [below left of =plane] {MaxSpeed} edge (plane); |
|
3292 |
|
\node[attribute] (airPlaneType) [below of = plane] {AirPlaneType} edge (plane); |
|
3293 |
|
\end{tikzpicture} |
|
3294 |
|
|
3222 |
3295 |
## Solution to Exercises |
## Solution to Exercises |
3223 |
3296 |
|
|
3224 |
3297 |
Solution +.# |
Solution +.# |
|
... |
... |
Solution +.# |
3376 |
3449 |
|
|
3377 |
3450 |
2. No. \(A\), \(B\) and \(D\) are non prime, \(B \to \{A,C\} \to D\) breaks the 3NF. |
2. No. \(A\), \(B\) and \(D\) are non prime, \(B \to \{A,C\} \to D\) breaks the 3NF. |
3378 |
3451 |
|
|
|
3452 |
|
Solution +.# |
|
3453 |
|
|
|
3454 |
|
\(\{A, B\}\), |
|
3455 |
|
|
|
3456 |
|
\(C, D, E\), |
|
3457 |
|
|
|
3458 |
|
\(R_1(A, B, C, E)\) and \(R_2(B, D)\) |
|
3459 |
|
|
|
3460 |
|
\(R_1(A, B, C)\), \(R_2(C, E)\) and \(R_3(B, D)\) |
|
3461 |
|
|
|
3462 |
|
Solution +.# |
|
3463 |
|
~ Behaviour and structure |
|
3464 |
|
|
|
3465 |
|
Solution +.# |
|
3466 |
|
~ Yes, U.M.L. diagram is language-independent and platform-independent. |
|
3467 |
|
|
|
3468 |
|
Solution +.# |
|
3469 |
|
~ 1. Use-case |
|
3470 |
|
2. Sequence diagram |
|
3471 |
|
3. Activity diagram |
|
3472 |
|
|
|
3473 |
|
Solution +.# |
|
3474 |
|
~ To use direction for association, to have a common language with someone less knowledgeable of other diagrammatic notations. |
|
3475 |
|
For the concept of integration. |
|
3476 |
|
|
|
3477 |
|
|
|
3478 |
|
Solution +.# |
|
3479 |
|
~ \(5\) and \(4\). |
|
3480 |
|
|
|
3481 |
|
getLastFlightNumber() : Integer |
|
3482 |
|
|
|
3483 |
|
stMaximumSpeed(MPH) : void |
|
3484 |
|
|
|
3485 |
|
A flight could be assigned to no plane, and a plane could not be assigned to a flight. |
|
3486 |
|
|
|
3487 |
|
A plane can be assigned to multiple (or no) flights, but a flight must have at most one plane (and could have none). |
|
3488 |
|
|
|
3489 |
|
Solution +.# |
|
3490 |
|
~ Aggregation: associated class can have an existence of its own. |
|
3491 |
|
|
|
3492 |
|
Composition association: class doesn't exist without the association. |
|
3493 |
|
|
|
3494 |
|
Solution +.# |
|
3495 |
|
~ \tikz{\draw[>=open triangle 60,->] (0,0) to (1,0);} |
|
3496 |
|
Because it avoids redundancy. |
|
3497 |
|
|
3379 |
3498 |
|
|
3380 |
3499 |
## Problems |
## Problems |
3381 |
3500 |
|
|
3382 |
|
Problem +.#Movie |
|
|
3501 |
|
Problem +.#movie |
3383 |
3502 |
|
|
3384 |
3503 |
Consider the ER schema for the MOVIES database: |
Consider the ER schema for the MOVIES database: |
3385 |
3504 |
|
|
|
... |
... |
Problem +.# |
3448 |
3567 |
|
|
3449 |
3568 |
Apply the ER-to-Relation mapping to your ER diagram from the previous problem. |
Apply the ER-to-Relation mapping to your ER diagram from the previous problem. |
3450 |
3569 |
|
|
3451 |
|
Problem +.#CarSale |
|
|
3570 |
|
Problem +.#carsale |
3452 |
3571 |
|
|
3453 |
3572 |
Consider the following relation, and its functional dependencies: |
Consider the following relation, and its functional dependencies: |
3454 |
3573 |
|
|
|
... |
... |
Email | → | \{Fname, Lname, Phone\} |
3507 |
3626 |
Normalize the relations to 2NF and 3NF. Show all relations at each stage (2NF and 3NF) of the normalization process. |
Normalize the relations to 2NF and 3NF. Show all relations at each stage (2NF and 3NF) of the normalization process. |
3508 |
3627 |
|
|
3509 |
3628 |
|
|
3510 |
|
Problem +.#Book |
|
|
3629 |
|
Problem +.#book |
3511 |
3630 |
|
|
3512 |
3631 |
Consider the following relation for published books: |
Consider the following relation for published books: |
3513 |
3632 |
|
|
|
... |
... |
Author\_name | → | Author\_affil |
3526 |
3645 |
- This relation is not in second normal form: explain why and normalize it. |
- This relation is not in second normal form: explain why and normalize it. |
3527 |
3646 |
- Is the relations you obtained at the previous step in third normal form? Explain why, and normalize them if needed. |
- Is the relations you obtained at the previous step in third normal form? Explain why, and normalize them if needed. |
3528 |
3647 |
|
|
|
3648 |
|
Problem +.#MYSQLW |
|
3649 |
|
|
|
3650 |
|
Consider the following E.R. schema for the CAR\_INFO database: |
|
3651 |
|
|
|
3652 |
|
\begin{tikzpicture}[node distance=8em] |
|
3653 |
|
\node[entity] (person) {PERSON}; |
|
3654 |
|
\node[attribute] (pid) [left of=person] {\key{ID}} edge (person); |
|
3655 |
|
\node[attribute] (name) [above left of=person] {Name} edge (person); |
|
3656 |
|
\node[multi attribute] (phone) [above of=person] {Phone} edge (person); |
|
3657 |
|
\node[attribute] (address) [above right of=person] {Address} edge (person); |
|
3658 |
|
\node[attribute] (street) [above right of=address] {Street} edge (address); |
|
3659 |
|
\node[attribute] (city) [right of=address] {City} edge (address); |
|
3660 |
|
%\node[derived attribute] (age) [right of=person] {Age} edge (person); |
|
3661 |
|
|
|
3662 |
|
\node[relationship] (drives) [below right of=person] {DRIVES} edge node[above, pos=0.1] {$1$} (person); |
|
3663 |
|
\node[entity] (car) [below left of=drives] {CAR} edge node[above, pos=0.7] {$1$} (drives); |
|
3664 |
|
\node[attribute] (make) [left of=car] {Make} edge (car); |
|
3665 |
|
\node[attribute] (year) [below left of =car] {Year} edge (car); |
|
3666 |
|
\node[attribute] (brand) [below of =car] {Brand} edge (car); |
|
3667 |
|
|
|
3668 |
|
\node[relationship] (seats) [below left of=person] {SEATS\_IN} edge node[above, pos=0.1] {$N$} (person); |
|
3669 |
|
\draw (seats) edge node[above, pos=0.3] {$1$} (car); |
|
3670 |
|
\node[attribute] (position) [left of=seats] {Position} edge (seats); |
|
3671 |
|
|
|
3672 |
|
\node[ident relationship] (insured) [right of=car] {INSURED} edge node[above, pos=0.3] {$1$} (car); |
|
3673 |
|
\node[weak entity] (insurance) [right = 1cm of insured] {CAR\_INSURANCE} edge[total] node[above, pos=0.7] {$N$} (insured); |
|
3674 |
|
\node[attribute] (amount) [above of =insurance] {Covered Amount} edge (insurance); |
|
3675 |
|
\node[attribute] (policy) [above right of =insurance] {Policy Number} edge (insurance); |
|
3676 |
|
\node[attribute] (company) [below right of =insurance] {Company Name} edge (insurance); |
|
3677 |
|
\end{tikzpicture} |
|
3678 |
|
|
|
3679 |
|
Note that a car can have at most one driver, \(N\) passengers, \(N\) insurances, and that car insurances exist only if they are "tied up" to a car (i.e., they are weak entities, and their identifying relationship is called "Insured"). |
|
3680 |
|
|
|
3681 |
|
1. Find the key attribute for "Car", and the partial key for "Car Insurance". If you can't think of any, add a dummy attribute and make it be the key. |
|
3682 |
|
2. Convert that E.-R. diagram to a relational database schema. |
|
3683 |
|
3. Convert the E.-R. diagram to a U.M.L. class diagram. Comparing Figure 7.16 with Figure 7.2 from your textbook should guide you. |
|
3684 |
|
|
|
3685 |
|
|
|
3686 |
|
Problem +.# |
|
3687 |
|
|
|
3688 |
|
In this exercise, we will install and explore the basic functionalities of MySQL Workbench, which is a cross-platform, open-source, and free graphical interface for database design. |
|
3689 |
|
|
|
3690 |
|
1. Install MySQL Workbench: use your package manager, or download the binaries from \url{https://dev.mysql.com/downloads/workbench/}. |
|
3691 |
|
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". |
|
3692 |
|
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". |
|
3693 |
|
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. |
|
3694 |
|
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". |
|
3695 |
|
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. |
|
3696 |
|
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. |
|
3697 |
|
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. |
|
3698 |
|
|
|
3699 |
|
|
|
3700 |
|
Problem +.#ReverseEng |
|
3701 |
|
|
|
3702 |
|
*This problem requires you to have successfully completed @probleme:MYSQLW and @problem:ERtoREL.* |
|
3703 |
|
|
|
3704 |
|
Using the relational database schema you obtained in question~\ref{ERtoREL} of @problem:ERtoREL, write the `SQL` implementation of that database. |
|
3705 |
|
Then, using MySQL Workbench, use the "Reverse Engineering" function to obtain a E.E.R. diagram of your database, and compare it with the U.M.L. diagram you draw in question~ \ref{ERtoUML} of @problem:ERtoREL. |
|
3706 |
|
Apart from the difference inherent to the nature of the diagram (i.e., U.M.L. Vs E.E.R.), how are they the same? How do they differ? Is the automated tool as efficient and accurate as you are? |
|
3707 |
|
|
3529 |
3708 |
## Solution to Selected Problems |
## Solution to Selected Problems |
3530 |
3709 |
|
|
3531 |
|
### Solution to the @Movie |
|
|
3710 |
|
### Solution to the @problem:movie |
3532 |
3711 |
|
|
3533 |
3712 |
#. true |
#. true |
3534 |
3713 |
#. true |
#. true |
|
... |
... |
Author\_name | → | Author\_affil |
3544 |
3723 |
#. true |
#. true |
3545 |
3724 |
#. false |
#. false |
3546 |
3725 |
|
|
3547 |
|
### Solution to @CarSale |
|
|
3726 |
|
### Solution to @problem:carsale |
3548 |
3727 |
|
|
3549 |
3728 |
This relation satisfies 1NF but not 2NF (Car\_no → Date\_sold and Salesman\_no → Commission so these two attributes are not fully functional dependent on the primary key) and not 3NF. |
This relation satisfies 1NF but not 2NF (Car\_no → Date\_sold and Salesman\_no → Commission so these two attributes are not fully functional dependent on the primary key) and not 3NF. |
3550 |
3729 |
|
|
|
... |
... |
Car\_Sale2(Car\_no, Salesman\_no)\\ |
3564 |
3743 |
Car\_Sale3(Salesman\_no,Commission) |
Car\_Sale3(Salesman\_no,Commission) |
3565 |
3744 |
\end{tabular} |
\end{tabular} |
3566 |
3745 |
|
|
3567 |
|
### Solution to @Bike |
|
|
3746 |
|
### Solution to @problem:bike |
3568 |
3747 |
|
|
3569 |
3748 |
- |
- |
3570 |
3749 |
#. \{ Manufacturer, Serial\_no \} → \{ Model, Batch, Wheel\_size, Retailer\} |
#. \{ Manufacturer, Serial\_no \} → \{ Model, Batch, Wheel\_size, Retailer\} |
|
... |
... |
Car\_Sale3(Salesman\_no,Commission) |
3575 |
3754 |
- \{Manufacturer, Serial\_no \} |
- \{Manufacturer, Serial\_no \} |
3576 |
3755 |
- If every attribute is atomic, it is in second nf. \{ Manufacturer, Serial\_no \} → Batch →Model breaks the 3NF. |
- If every attribute is atomic, it is in second nf. \{ Manufacturer, Serial\_no \} → Batch →Model breaks the 3NF. |
3577 |
3756 |
|
|
3578 |
|
### Solution to @Book |
|
|
3757 |
|
### Solution to @problem:book |
3579 |
3758 |
|
|
3580 |
3759 |
- \{Book Title, Author Name\} |
- \{Book Title, Author Name\} |
3581 |
3760 |
- If an attribute is composite or multi-valued. |
- If an attribute is composite or multi-valued. |
|
... |
... |
We can normalize it as |
3585 |
3764 |
- Because of \{Book title\} → \{ Book\_type\} → \{ List\_price\}\\ |
- Because of \{Book title\} → \{ Book\_type\} → \{ List\_price\}\\ |
3586 |
3765 |
(Book Title, Publisher, Book Type) and (Book Type, List Price), (Author Name, Author Affiliation), (Author Name, Book Title). |
(Book Title, Publisher, Book Type) and (Book Type, List Price), (Author Name, Author Affiliation), (Author Name, Book Title). |
3587 |
3766 |
|
|
|
3767 |
|
Solution for @probleme:MYSQLW |
|
3768 |
|
~ For "Car", we need to create an attribute, like "vin". |
|
3769 |
|
For "Car Insurance", "Policy Number" is perfect. |
|
3770 |
|
|
|
3771 |
|
\colorlet{lightgray}{gray!20} |
|
3772 |
|
|
|
3773 |
|
\begin{tikzpicture}[relation/.style={rectangle split, rectangle split parts=#1, rectangle split part align=base, draw, anchor=center, align=center, text height=3mm, text centered}]\hspace*{-0.3cm} |
|
3774 |
|
|
|
3775 |
|
% RELATIONS |
|
3776 |
|
|
|
3777 |
|
\node (phonetitle) {**PHONE**}; |
|
3778 |
|
|
|
3779 |
|
\node [relation=2, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of phonetitle.west, anchor=west] (phone) |
|
3780 |
|
{\underline{id}% |
|
3781 |
|
\nodepart{two} \underline{number} |
|
3782 |
|
}; |
|
3783 |
|
|
|
3784 |
|
\node [below=1.3cm of phone.west, anchor=west] (persontitle) {**PERSON**}; |
|
3785 |
|
|
|
3786 |
|
\node [relation=6, rectangle split horizontal, rectangle split part fill={lightgray!50}, below=0.6cm of persontitle.west, anchor=west] (person) |
|
3787 |
|
{\underline{id}% |
|
3788 |
|
\nodepart{two} Name |
|
3789 |
|
\nodepart{three} Street |
|
3790 |
|
\nodepart{four} City |
|
3791 |
|
\nodepart{five} Seat |
|
3792 |
|
\nodepart{six}{Position} |
|
3793 |
|
}; |
|
3794 |
|
|
|
3795 |
|
\node [below=1.1cm of person.west, anchor=west] (cartitle) {**CAR**}; |
|
3796 |
|
|
|
3797 |
|
\node [relation=5, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of cartitle.west, anchor=west] (car) |
|
3798 |
|
{\underline{Vin}% |
|
3799 |
|
\nodepart{two} Make |
|
3800 |
|
\nodepart{three} Year |
|
3801 |
|
\nodepart{four} Brand |
|
3802 |
|
\nodepart{five} Driver |
|
3803 |
|
}; |
|
3804 |
|
|
|
3805 |
|
\node [below=1.4cm of car.west, anchor=west] (carinsurancetitle) {**CAR INSURANCE**}; |
|
3806 |
|
|
|
3807 |
|
\node [relation=4, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of carinsurancetitle.west, anchor=west] (carinsurance) |
|
3808 |
|
{\underline{Policy Number}% |
|
3809 |
|
\nodepart{two} Covered Amount |
|
3810 |
|
\nodepart{three} Company Name |
|
3811 |
|
\nodepart{four} Insured Car |
|
3812 |
|
}; |
|
3813 |
|
|
|
3814 |
|
% FOREIGN KEYS |
|
3815 |
|
|
|
3816 |
|
\draw[-latex] (phone.one south) -- ++(0,-0.2) --++(7,0) --++(0, -2) -| ($(person.one south) + (0.2,0)$); |
|
3817 |
|
%\draw[-latex] (person.five south) -- ++(0,-0.2) --++(3,0) --++(0, -2) -| ($(car.one south) + (-0.2,0)$); |
|
3818 |
|
\draw[-latex] (carinsurance.four south) -- ++(0,-0.2) --++(2,0) --++(0, 1.75) -| ($(car.one south) + (0.1,0)$); |
|
3819 |
|
\draw[-latex] (car.five south) -- ++(0,-0.2) --++(2,0) --++(0, 1.5) -| ($(person.one south) + (-0.1,0)$); |
|
3820 |
|
\end{tikzpicture} |
|
3821 |
|
|
|
3822 |
|
|
|
3823 |
|
\begin{tikzpicture}[scale = 0.8] |
|
3824 |
|
\node (person) at (0,0){\begin{tabular}{| r l |} |
|
3825 |
|
\hline |
|
3826 |
|
\multicolumn{2}{| c |}{**Person**}\\ |
|
3827 |
|
\hline |
|
3828 |
|
id &: String\\ |
|
3829 |
|
name &: String\\ |
|
3830 |
|
address &: Street\\ |
|
3831 |
|
& City\\ |
|
3832 |
|
\hline |
|
3833 |
|
getAge()&: Int\\ |
|
3834 |
|
\hline |
|
3835 |
|
\end{tabular} |
|
3836 |
|
}; |
|
3837 |
|
\node (phone) at (8, 1){\begin{tabular}{| r l |} |
|
3838 |
|
\hline |
|
3839 |
|
\multicolumn{2}{| c |}{**Phone**}\\ |
|
3840 |
|
\hline |
|
3841 |
|
number &: String\\ |
|
3842 |
|
\hline |
|
3843 |
|
\end{tabular} |
|
3844 |
|
}; |
|
3845 |
|
\draw[open diamond-] (person) -- node[above, pos=0.1]{\(0..\star\)} node[above, pos=0.9]{\(1..1\)} (phone); |
|
3846 |
|
\node (car) at (8, -3){\begin{tabular}{| r l |} |
|
3847 |
|
\hline |
|
3848 |
|
\multicolumn{2}{| c |}{**Car**}\\ |
|
3849 |
|
\hline |
|
3850 |
|
vin &: String\\ |
|
3851 |
|
make &: String\\ |
|
3852 |
|
year &: Year\\ |
|
3853 |
|
brand &: String\\ |
|
3854 |
|
\hline |
|
3855 |
|
\end{tabular} |
|
3856 |
|
}; |
|
3857 |
|
\draw[-] ($(person)+(2.5,-0.5)$) -- node[above, pos=0.1]{\(0..1\)} node[above, pos=0.5]{drives} node[above, pos=0.9]{\(0..1\)} (car); |
|
3858 |
|
%\draw[-] (person) -- node[below, pos=0.1]{\(0..1\)} node[below, pos=0.9]{\(0..4\)} ($(car)+(-2.5, 0.5)$); |
|
3859 |
|
\node (seat) at (3.5, -5){\begin{tabular}{| r l |} |
|
3860 |
|
\hline |
|
3861 |
|
\multicolumn{2}{| c |}{**Seats In**}\\ |
|
3862 |
|
\hline |
|
3863 |
|
position &: String\\ |
|
3864 |
|
\hline |
|
3865 |
|
\end{tabular} |
|
3866 |
|
}; |
|
3867 |
|
\draw[dashed] (seat) -- (3.5, -1.6); |
|
3868 |
|
|
|
3869 |
|
\node (insu) at (14.3, -2){\begin{tabular}{| r l |} |
|
3870 |
|
\hline |
|
3871 |
|
\multicolumn{2}{| c |}{**Car Insurance**}\\ |
|
3872 |
|
\hline |
|
3873 |
|
policy number &: String\\ |
|
3874 |
|
covered amount &: int\\ |
|
3875 |
|
compagny name &: String\\ |
|
3876 |
|
\hline |
|
3877 |
|
\end{tabular} |
|
3878 |
|
}; |
|
3879 |
|
|
|
3880 |
|
\draw[diamond -] (car) -- node[above, pos=0.1]{\(0..\star\)} node[above, pos=0.9]{\(1\)} (insu); |
|
3881 |
|
\end{tikzpicture} |
|
3882 |
|
|
|
3883 |
|
|
|
3884 |
|
Solution to @problem:ReverseEng |
|
3885 |
|
~ <!-- |
|
3886 |
|
\inputminted{sql}{include/sol.sql} |
|
3887 |
|
\includegraphics{include/mysql_workbench_drawing-crop} |
|
3888 |
|
--> |
|
3889 |
|
|
|
3890 |
|
<!-- |
|
3891 |
|
~~~include |
|
3892 |
|
include/sol.sql |
|
3893 |
|
~~~ |
|
3894 |
|
|
|
3895 |
|
--> |
|
3896 |
|
|
|
3897 |
|
**Code à inclure ici** |
|
3898 |
|
|
|
3899 |
|
 |
|
3900 |
|
|
3588 |
3901 |
# Databases Applications |
# Databases Applications |
3589 |
3902 |
|
|
3590 |
3903 |
**Sources:** |
**Sources:** |
|
... |
... |
You can pass options when creating Statement objects to be able to read it both |
3848 |
4161 |
|
|
3849 |
4162 |
**COPY HOMEWORK HERE** |
**COPY HOMEWORK HERE** |
3850 |
4163 |
|
|
|
4164 |
|
## Exercises |
|
4165 |
|
|
|
4166 |
|
Exercise +.# |
|
4167 |
|
~ What are the technologies that makes it possible for a Java application to communicate with a DBMS? |
|
4168 |
|
|
|
4169 |
|
Exercise +.# |
|
4170 |
|
~ What JDBC method do you call to get a connection to a database? |
|
4171 |
|
|
|
4172 |
|
|
|
4173 |
|
Exercise +.# |
|
4174 |
|
~ Briefly explain what the `next()` method from the `ResultSet` class does, and give its return type. |
|
4175 |
|
|
|
4176 |
|
Exercise +.# |
|
4177 |
|
~ How do you submit a `SELECT` statement to the DBMS? |
|
4178 |
|
|
|
4179 |
|
Exercise +.# |
|
4180 |
|
~ Where is a `ResultSet` object's cursor initially pointing? How do you move the cursor forward in the result set? |
|
4181 |
|
|
|
4182 |
|
Exercise +.# |
|
4183 |
|
~ Give three navigation methods provided by `ResultSet`. |
|
4184 |
|
|
|
4185 |
|
Exercise +.# |
|
4186 |
|
~ Explain this JDBC URL format: |
|
4187 |
|
|
|
4188 |
|
~~~{.java} |
|
4189 |
|
jdbc:mysql://localhost:3306/HW_NewDB?createDatabaseIfNotExist=true&useSSL=true |
|
4190 |
|
~~~ |
|
4191 |
|
|
|
4192 |
|
Exercise +.# |
|
4193 |
|
~ In what class is the `getColumnName()} method? |
|
4194 |
|
|
|
4195 |
|
Exercise +.# |
|
4196 |
|
~ What is a prepared statement? |
|
4197 |
|
|
|
4198 |
|
|
|
4199 |
|
|
|
4200 |
|
Exercise +.# |
|
4201 |
|
~ In the code below, there are \(5\) errors between line \(11\) and line \(30\). They are \emph{not} subtle Java errors (like misspelling a key word) and do not come from the DBMS (so you should assume that the password is correct, that the database exists, etc.). For each error, highlight it precisely and give a short explanation. |
|
4202 |
|
|
|
4203 |
|
~~~{.java} |
|
4204 |
|
import java.sql.*; |
|
4205 |
|
|
|
4206 |
|
public class MyProg{ |
|
4207 |
|
public static void main(String[] args) { |
|
4208 |
|
try ( |
|
4209 |
|
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" |
|
4210 |
|
+"HW_TestDB?user=testuser&password=password"); |
|
4211 |
|
Statement stmt = conn.createStatement(); |
|
4212 |
|
) { |
|
4213 |
|
|
|
4214 |
|
/* Errors after this point.*/ |
|
4215 |
|
|
|
4216 |
|
String strSelect = "SELECT title WHERE qty > 40 FROM disks;"; |
|
4217 |
|
ResultSet rset = stmt.executeUpdate(strSelect); |
|
4218 |
|
|
|
4219 |
|
System.out.println("The records selected are: (listed last first):"); |
|
4220 |
|
rset.last(); |
|
4221 |
|
|
|
4222 |
|
while(rset.previous()) { |
|
4223 |
|
String title = rset.getDouble("title"); |
|
4224 |
|
System.out.println(title + "\n"); |
|
4225 |
|
} |
|
4226 |
|
|
|
4227 |
|
String sss = "SELECT title FROM disks WHERE Price <= ?"; |
|
4228 |
|
PreparedStatement ps = conn.prepareStatement(sss); |
|
4229 |
|
ResultSet result = ps.executeQuery(); |
|
4230 |
|
|
|
4231 |
|
conn.close(); |
|
4232 |
|
|
|
4233 |
|
/* Errors before this point.*/ |
|
4234 |
|
|
|
4235 |
|
} catch(SQLException ex) { |
|
4236 |
|
ex.printStackTrace(); |
|
4237 |
|
} |
|
4238 |
|
} |
|
4239 |
|
} |
|
4240 |
|
~~~ |
|
4241 |
|
|
|
4242 |
|
## Solution to Exercises |
|
4243 |
|
|
|
4244 |
|
|
|
4245 |
|
|
|
4246 |
|
Solution +.# |
|
4247 |
|
~ API + driver |
|
4248 |
|
|
|
4249 |
|
|
|
4250 |
|
|
|
4251 |
|
|
|
4252 |
|
Solution +.# |
|
4253 |
|
~ DriverManager.getConnection() |
|
4254 |
|
|
|
4255 |
|
Solution +.# |
|
4256 |
|
~ It checks if there is data to read, and if move the cursor reads it. |
|
4257 |
|
It returns a Boolean. |
|
4258 |
|
|
|
4259 |
|
|
|
4260 |
|
Solution +.# |
|
4261 |
|
~ Using |
|
4262 |
|
|
|
4263 |
|
~~~{.java} |
|
4264 |
|
.executeQuery(strSelect) |
|
4265 |
|
~~~ |
|
4266 |
|
|
|
4267 |
|
Solution +.# |
|
4268 |
|
~ Before the first line. Using the `next` method. |
|
4269 |
|
|
|
4270 |
|
Solution +.# |
|
4271 |
|
~ `first()` |
|
4272 |
|
`last()` |
|
4273 |
|
`next()` |
|
4274 |
|
`previous()` |
|
4275 |
|
`relative(rows)` |
|
4276 |
|
`absolute(row)` method. |
|
4277 |
|
|
|
4278 |
|
Solution +.# |
|
4279 |
|
~ Connect to localhost:3306 and create a new database if needed, and use secure connection. |
|
4280 |
|
|
|
4281 |
|
Solution +.# |
|
4282 |
|
~ ResultSetMetaData |
|
4283 |
|
|
|
4284 |
|
Solution +.# |
|
4285 |
|
~ A prepared statement is a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency. |
|
4286 |
|
|
|
4287 |
|
Solution +.# |
|
4288 |
|
List errors in program TO DO. |
|
4289 |
|
|
|
4290 |
|
<!-- |
|
4291 |
|
% |
|
4292 |
|
%METADATA |
|
4293 |
|
% |
|
4294 |
|
%ResultSet is array, other? |
|
4295 |
|
% |
|
4296 |
|
%Multiple queries |
|
4297 |
|
% |
|
4298 |
|
%qet datatype |
|
4299 |
|
% |
|
4300 |
|
%prepared statemernt. |
|
4301 |
|
% |
|
4302 |
|
%http://tutorials.jenkov.com/jdbc/resultset.html |
|
4303 |
|
--> |
|
4304 |
|
|
|
4305 |
|
|
3851 |
4306 |
--- |
--- |
3852 |
4307 |
|
|
3853 |
4308 |
# A Bit About Security |
# A Bit About Security |