File notes/lectures_notes.md changed (mode: 100644) (index 068ea8b..59efc24) |
... |
... |
Example of Use Case |
5910 |
5910 |
|
|
5911 |
5911 |
Exercise +.# |
Exercise +.# |
5912 |
5912 |
|
|
5913 |
|
: Name the three high-level models we will be looking at in this class (expanding the acronyms). |
|
|
5913 |
|
: Name the three high-level models we will be learning about in this class (expand the acronyms). |
5914 |
5914 |
|
|
5915 |
5915 |
Exercise +.# |
Exercise +.# |
5916 |
5916 |
|
|
|
... |
... |
Exercise +.# |
5922 |
5922 |
|
|
5923 |
5923 |
Exercise +.# |
Exercise +.# |
5924 |
5924 |
|
|
5925 |
|
: Draw the ER diagram for a "CELLPHONE" entity that has a composite attribute "Plan" (decomposed into "Carrier" and "Price"), a "Mobile\_identification\_number" key attribute, and a multi-valued "App\_Installed" attribute. |
|
|
5925 |
|
: Draw the ER diagram for a "CELLPHONE" entity that has a composite attribute "Plan" (decomposed into "Carrier" and "Price"), an "MIN" (Mobile Identification Number) key attribute, and a multi-valued "App\_Installed" attribute. |
5926 |
5926 |
|
|
5927 |
5927 |
Exercise +.# |
Exercise +.# |
5928 |
5928 |
|
|
5929 |
|
: Name one difference between a primary key in the relational model, and a key attribute in the ER model. |
|
|
5929 |
|
: Name one difference between a primary key in the relational model and a key attribute in the ER model. |
5930 |
5930 |
|
|
5931 |
5931 |
Exercise +.#derived |
Exercise +.#derived |
5932 |
5932 |
|
|
|
... |
... |
Exercise +.#derived |
5935 |
5935 |
|
|
5936 |
5936 |
Exercise +.# |
Exercise +.# |
5937 |
5937 |
|
|
5938 |
|
: Invent an entity type with at least one composite attribute and one atomic attribute, but no multi-valued attribute, identify a possible key attribute, and draw the entity type you obtained using the conventions we used in class. |
|
|
5938 |
|
: Invent an entity type with at least one composite attribute and one atomic attribute, but no multi-valued attributes. Identify a possible key attribute and draw the entity type you obtained using the conventions we used in class. |
5939 |
5939 |
|
|
5940 |
5940 |
Exercise +.# |
Exercise +.# |
5941 |
5941 |
|
|
|
... |
... |
Exercise +.#recursive |
5947 |
5947 |
|
|
5948 |
5948 |
Exercise +.# |
Exercise +.# |
5949 |
5949 |
|
|
5950 |
|
: What does it mean for a binary relationship type "Owner" between entity types "Person" and "Computer" to have a cardinality ration $M:N$? |
|
|
5950 |
|
: What does it mean for a binary relationship type "Owner" between entity types "Person" and "Computer" to have a cardinality ratio of $M:N$? |
5951 |
5951 |
|
|
5952 |
5952 |
Exercise +.# |
Exercise +.# |
5953 |
5953 |
|
|
|
... |
... |
Exercise +.# |
5958 |
5958 |
|
|
5959 |
5959 |
Draw a diagram to represent a relationship type $R$ between two entities types $A$ and $B$ such that: |
Draw a diagram to represent a relationship type $R$ between two entities types $A$ and $B$ such that: |
5960 |
5960 |
|
|
5961 |
|
- An entity in $A$ may or may not be in relationship $R$ with an entity in $B$, |
|
5962 |
|
- An entity in $B$ must be in relationship $R$ with an entity in $A$, |
|
5963 |
|
- An entity in $A$ can be in relationship through $R$ with at most one entity in $B$, |
|
5964 |
|
- An entity in $B$ can be in relationship through $R$ with any number of entities in $A$. |
|
|
5961 |
|
- An entity in $A$ may or may not be in relationship $R$ with an entity in $B$. |
|
5962 |
|
- An entity in $B$ must be in relationship $R$ with an entity in $A$. |
|
5963 |
|
- An entity in $A$ can be in relationship $R$ with at most one entity in $B$. |
|
5964 |
|
- An entity in $B$ can be in relationship $R$ with any number of entities in $A$. |
5965 |
5965 |
|
|
5966 |
5966 |
Exercise +.# |
Exercise +.# |
5967 |
5967 |
|
|
|
... |
... |
Exercise +.# |
5970 |
5970 |
|
|
5971 |
5971 |
Exercise +.#chair_relationship |
Exercise +.#chair_relationship |
5972 |
5972 |
|
|
5973 |
|
: What does it mean for a binary relationship type "Chair" between entity types "Professor" and "Department" to have a cardinality ratio 1:N? |
|
5974 |
|
Would it make sense to be have a total participation constraint on one side, and if yes, on which one? |
|
|
5973 |
|
: What does it mean for a binary relationship type "is the Chair of" between entity types "Professor" and "Department" to have a cardinality ratio of 1:N? |
|
5974 |
|
Would it make sense to be have a total participation constraint on one side, and if yes, on which side? |
5975 |
5975 |
|
|
5976 |
5976 |
Exercise +.# |
Exercise +.# |
5977 |
5977 |
|
|
|
... |
... |
Exercise +.# |
5996 |
5996 |
|
|
5997 |
5997 |
Exercise +.# |
Exercise +.# |
5998 |
5998 |
|
|
5999 |
|
: Give an example of a binary relationship type of cardinality $N:1$, and draw the corresponding diagram (you do not have to include details on the participating entity types). |
|
|
5999 |
|
: Give an example of a binary relationship type of cardinality $N:1$ and draw the corresponding diagram (you do not have to include details on the participating entity types). |
6000 |
6000 |
|
|
6001 |
6001 |
Exercise +.# |
Exercise +.# |
6002 |
6002 |
|
|
6003 |
|
: 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 can always be obtained from the value(s) for the other attribute(s). |
|
|
6003 |
|
: 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 can always be obtained from the value(s) of the other attribute(s). |
6004 |
6004 |
|
|
6005 |
6005 |
Exercise +.# |
Exercise +.# |
6006 |
6006 |
|
|
6007 |
|
: Draw an ER diagram expressing the total participation of an entity type "BURGER" in a binary relation "CONTAINS" between "BURGER" and "INGREDIENT". |
|
6008 |
|
What would be the ratio of such a relation? |
|
|
6007 |
|
: Draw an ER diagram expressing the total participation of an entity type "BURGER" in a binary relation "CONTAINS" with an entity type "INGREDIENT". |
|
6008 |
|
What would be the cardinality ratio of such a relation? |
6009 |
6009 |
|
|
6010 |
6010 |
Exercise +.# |
Exercise +.# |
6011 |
6011 |
|
|
6012 |
|
: Under what condition(s) can an attribute of a binary relationship type be migrated to become an attribute of one of the participating entity type? |
|
|
6012 |
|
: Under what condition(s) can an attribute of a binary relationship type be migrated to become an attribute of one of the participating entity types? |
6013 |
6013 |
|
|
6014 |
6014 |
Exercise +.# |
Exercise +.# |
6015 |
6015 |
|
|
6016 |
6016 |
: Suppose a "PRODUCES" relationship with an attribute "Amount" exists between a "PRODUCER" entity type and a "MOVIE" entity type, with ratio $1:M$. |
: Suppose a "PRODUCES" relationship with an attribute "Amount" exists between a "PRODUCER" entity type and a "MOVIE" entity type, with ratio $1:M$. |
6017 |
|
Migrate the "Amount" attribute to one of the entity type and draw the resulting diagram. |
|
|
6017 |
|
Migrate the "Amount" attribute to one of the entity types and draw the resulting diagram. |
6018 |
6018 |
|
|
6019 |
6019 |
Exercise +.# |
Exercise +.# |
6020 |
6020 |
|
|
6021 |
6021 |
: Suppose a "MEMBERSHIP relationship with an attribute "Level" (e.g., "silver", "platinium", etc.) exists between a "PERSON" entity type and a "CLUB" entity type, with ratio $M:1$. |
: Suppose a "MEMBERSHIP relationship with an attribute "Level" (e.g., "silver", "platinium", etc.) exists between a "PERSON" entity type and a "CLUB" entity type, with ratio $M:1$. |
6022 |
|
Migrate the "Level" attribute to one of the entity type and draw the resulting diagram. |
|
|
6022 |
|
Migrate the "Level" attribute to one of the entity types and draw the resulting diagram. |
6023 |
6023 |
|
|
6024 |
6024 |
Exercise +.# |
Exercise +.# |
6025 |
6025 |
~ |
~ |
6026 |
6026 |
|
|
6027 |
|
Below, you can specify roles names in your diagrams for added clarity, and remember to list **all** the constraints. |
|
6028 |
6027 |
Assume with have three entity types, "Lecture Notes", "Class" and "Professor." |
Assume with have three entity types, "Lecture Notes", "Class" and "Professor." |
6029 |
|
Give |
|
6030 |
6028 |
|
|
6031 |
|
#. One example of a ternary relationship between them, |
|
6032 |
|
#. Two examples of binary relationships between any two of them, |
|
6033 |
|
#. A question that could be answered using one model but not the other (and specify which relationship would be able to answer your question). |
|
|
6029 |
|
#. Draw a diagram of a ternary relationship between the three entities. |
|
6030 |
|
#. Draw a diagram that has two binary relationships from one of the three entities to the other two entities. |
|
6031 |
|
#. Come up with a question that could be answered using one model but not the other from the previous steps (specify which relationship would be able to answer your question). |
6034 |
6032 |
|
|
|
6033 |
|
You can specify role names in your diagrams for added clarity, and remember to list **all** the constraints. |
|
6034 |
|
|
6035 |
6035 |
Exercise +.#ternary |
Exercise +.#ternary |
6036 |
6036 |
|
|
6037 |
6037 |
: Can we always replace a ternary relationship with three binary relationships? Give an example. |
: Can we always replace a ternary relationship with three binary relationships? Give an example. |
|
... |
... |
Exercise +.# |
6239 |
6239 |
|
|
6240 |
6240 |
Solution +.# |
Solution +.# |
6241 |
6241 |
|
|
6242 |
|
: **U**nified **M**odeling **L**anguage, **E**ntity **R**elationship and **E**nhanced **E**ntity–**R**elationship models. |
|
|
6242 |
|
: The three high-level models we will be learning about are the **U**nified **M**odeling **L**anguage, **E**ntity **R**elationship, and **E**nhanced **E**ntity–**R**elationship models. |
6243 |
6243 |
|
|
6244 |
6244 |
Solution +.# |
Solution +.# |
6245 |
6245 |
|
|
6246 |
|
: Name / extension. |
|
6247 |
|
To have statistics about the extensions, to sort the username by length, etc. |
|
|
6246 |
|
: A useful decomposition of an email address attribute could be: the username part before the @ sign, and the domain part afterwards. |
|
6247 |
|
It might be useful to have statistics about the domains of the users or to sort the usernames by length, etc. |
6248 |
6248 |
|
|
6249 |
6249 |
Solution +.# |
Solution +.# |
6250 |
6250 |
|
|
|
... |
... |
Solution +.# |
6262 |
6262 |
Solution +.# |
Solution +.# |
6263 |
6263 |
|
|
6264 |
6264 |
~ |
~ |
6265 |
|
A derived attribute is an attribute whose value can be determined by the value of other attributes. For instance, |
|
6266 |
|
- The value of an "Age" attribute could be determined from the value of an "Date of birth" attribute and the current day. |
|
6267 |
|
- The value of an "State" attribute can be determined from the value of a "Zip code" attribute. |
|
6268 |
|
- The value of a "Body Mass Index" attribute could be calculated from the values of height and weight attributes. |
|
6269 |
|
- The value of a "Initials" attribute could be determined using the values of the "First Name", "Middle Name" and "Last Name" attributes. |
|
|
6265 |
|
A derived attribute is an attribute whose value can be determined by the value of other attributes. For instance: |
|
6266 |
|
- The value of an "Age" attribute could be determined from the value of an "Date of birth" attribute and the current day. |
|
6267 |
|
- The value of a "State" attribute can be determined from the value of a "Zip code" attribute. |
|
6268 |
|
- The value of a "Body Mass Index" attribute could be calculated from the values of height and weight attributes. |
|
6269 |
|
- The value of an "Initials" attribute could be determined using the values of the "First Name", "Middle Name", and "Last Name" attributes. |
6270 |
6270 |
|
|
6271 |
6271 |
Solution +.# |
Solution +.# |
6272 |
6272 |
|
|
|
... |
... |
Solution +.# |
6274 |
6274 |
|
|
6275 |
6275 |
Solution +.# |
Solution +.# |
6276 |
6276 |
|
|
6277 |
|
: The number of participating entity types. |
|
|
6277 |
|
: The degree of a realationship type is the number of its participating entity types. |
6278 |
6278 |
|
|
6279 |
6279 |
Solution +.# |
Solution +.# |
6280 |
6280 |
|
|
6281 |
|
: A relationship type where the same entity type participates more than once. |
|
6282 |
|
On seats, "is to the left of", on persons, "is married to". |
|
|
6281 |
|
: A self-referencing relationship type is where the same entity type participates more than once. |
|
6282 |
|
On a SEATS entity type, it would be an attribute like "is to the left of" or on a PERSONS entity type, it would be and attribute like "is married to". |
6283 |
6283 |
|
|
6284 |
6284 |
Solution +.# |
Solution +.# |
6285 |
6285 |
|
|
6286 |
|
: That a person can own multiple computers, and that a computer can have multiple owners. |
|
|
6286 |
|
: The cardinality ratio on the binary relationship type "Owner" between the entity types "Person" and "Computer" means that a person can own multiple computers, and a computer can have multiple owners. |
6287 |
6287 |
|
|
6288 |
6288 |
Solution +.# |
Solution +.# |
6289 |
6289 |
|
|
6290 |
|
: Cardinality ration and participation constraints. |
|
|
6290 |
|
: The two possible structural constraints on a relationship type are the cardinality ratio and participation constraints. |
6291 |
6291 |
|
|
6292 |
6292 |
Solution +.# |
Solution +.# |
6293 |
6293 |
~ |
~ |
6294 |
|
We would obtain the following drawing: |
|
|
6294 |
|
We would obtain the following diagram: |
6295 |
6295 |
|
|
6296 |
6296 |
 |
 |
6297 |
6297 |
|
|
6298 |
6298 |
Solution +.# |
Solution +.# |
6299 |
6299 |
|
|
6300 |
6300 |
: A key opens only one door, and every key must open at least one door. |
: A key opens only one door, and every key must open at least one door. |
6301 |
|
A door can be opened by multiple key, and some doors may not be opened by keys (think of doors that cannot be locked). |
|
|
6301 |
|
A door can be opened by multiple keys, and some doors may not be opened by any key (think of doors that do not have a lock). |
6302 |
6302 |
|
|
6303 |
6303 |
Solution +.# |
Solution +.# |
6304 |
6304 |
|
|
6305 |
|
: It means that a department can have at most one professor being its chair, but that a professor can be the chair of multiple departments. |
|
6306 |
|
It could make sense to require that every department has a chair, hence writing a double line between the Department entity and the Chair relationship, but it would not make sense to have a total participation constraint on the side of the professor (which would mean that every professor _has to_ chair a department). |
|
|
6305 |
|
: The binary relation type "is the Chair of" with a cardinality ratio of 1:N between entity types "Professor" and "Department" means that a department can have at most one professor as its chair, but that a professor can be the chair of multiple departments. |
|
6306 |
|
It could make sense to require that every department has a chair, hence writing a double line between the Department entity and the "is the Chair of" relationship, but it would not make sense to have a total participation constraint on the side of the professor (which would mean that every professor _has to_ be the chair of a department). |
6307 |
6307 |
|
|
6308 |
6308 |
Solution +.# |
Solution +.# |
6309 |
6309 |
|
|
6310 |
6310 |
: An operating system may be supported by many computers, but it is also possible that no computer supports it (think of an operating system in development, or developed for embeded devices). |
: An operating system may be supported by many computers, but it is also possible that no computer supports it (think of an operating system in development, or developed for embeded devices). |
6311 |
|
A computer must support at least one operating system, and can support multiple operating systems. |
|
|
6311 |
|
A computer must support at least one operating system and can support multiple operating systems. |
6312 |
6312 |
|
|
6313 |
6313 |
Solution +.# |
Solution +.# |
6314 |
6314 |
~ |
~ |
|
... |
... |
Solution +.# |
6319 |
6319 |
CAR | $1:1$ | TAG | "A car has exactly one tag, a tag belongs to one particular car." |
CAR | $1:1$ | TAG | "A car has exactly one tag, a tag belongs to one particular car." |
6320 |
6320 |
INSTRUCTOR | $1:N$ | LECTURE | "An instructor can teach multiple lecture, but a lecture is taught by only one person." |
INSTRUCTOR | $1:N$ | LECTURE | "An instructor can teach multiple lecture, but a lecture is taught by only one person." |
6321 |
6321 |
INSTRUCTOR | $1:N$ | OFFICE | "An instructor can have multiple office, but an office belongs to only one instructor" |
INSTRUCTOR | $1:N$ | OFFICE | "An instructor can have multiple office, but an office belongs to only one instructor" |
6322 |
|
COMPUTER | $M:N$ | OS| "A computer can have multiple operating system, the same operating system can be installed on more than one computer." |
|
|
6322 |
|
COMPUTER | $M:N$ | OPERATING_SYSTEM| "A computer can have multiple operating system, the same operating system can be installed on more than one computer." |
6323 |
6323 |
|
|
6324 |
|
Some of those choices, of course, are arguable (typically, almost any combination seems reasonnable for the INSTRUCTOR : OFFICE relation). |
|
|
6324 |
|
Some of these choices are debatable (typically, almost any combination seems reasonable for the INSTRUCTOR : OFFICE relation). |
6325 |
6325 |
|
|
6326 |
6326 |
Solution +.# |
Solution +.# |
6327 |
6327 |
|
|
6328 |
|
: SUPERVISION, a recursive relationship on EMPLOYEE. |
|
|
6328 |
|
: A binary of relationship of SUPERVISOR as a recursive relationship on EMPLOYEE. |
6329 |
6329 |
|
|
6330 |
6330 |
Solution +.# |
Solution +.# |
6331 |
6331 |
|
|
|
... |
... |
Solution +.# |
6341 |
6341 |
|
|
6342 |
6342 |
Solution +.# |
Solution +.# |
6343 |
6343 |
|
|
6344 |
|
: When the cardinality is $1:N$, $1:1$ or $N:1$, the attribute on the relationship can be migrated "to the $N$ side", or to either side, if there is none. |
|
6345 |
|
Note that for $n$-ary relationships, at most all but one ratio needs to be $1$ for the attribute to be allowed to migrate (and, again, "to the $N$ side", or to any side, if there is none). |
|
|
6344 |
|
: An attribute of a binary relationship type can be migrated to one of the participating entity types when the cardinality ratio is $1:N$, $1:1$, or $N:1$. It can be migrated "to the $N$ side" or, if there is no $N$ side, to either side. |
|
6345 |
|
Note that for $n$-ary relationships, at least one ratio needs to be $1$ for the attribute to be allowed to migrate (and "to the $N$ side", or, if there is no $N$ side, to any side). |
6346 |
6346 |
|
|
6347 |
6347 |
|
|
6348 |
6348 |
Solution +.# |
Solution +.# |
|
... |
... |
Solution +.# |
6361 |
6361 |
|
|
6362 |
6362 |
Solution +.# |
Solution +.# |
6363 |
6363 |
~ |
~ |
6364 |
|
#. A possible example of ternary relationship is: |
|
|
6364 |
|
|
|
6365 |
|
#. A possible example of ternary relationship is: |
6365 |
6366 |
|
|
6366 |
6367 |
 |
 |
6367 |
|
~ |
|
|
6368 |
|
~ |
6368 |
6369 |
|
|
6369 |
|
#. Two binary relationships could be: |
|
|
6370 |
|
#. One example of two binary relationships could be: |
6370 |
6371 |
|
|
6371 |
6372 |
 |
 |
6372 |
6373 |
~ |
~ |
|
... |
... |
Solution +.# |
6380 |
6381 |
|
|
6381 |
6382 |
> "What are the lecture notes refered to by Prof. X in their class Y?" |
> "What are the lecture notes refered to by Prof. X in their class Y?" |
6382 |
6383 |
|
|
6383 |
|
could not be answered using the binary relationships (since we not know what classes are taught by Prof. X). |
|
|
6384 |
|
could not be answered using the binary relationships (since we do not know what classes are taught by Prof. X). |
6384 |
6385 |
|
|
6385 |
6386 |
Solution +.# |
Solution +.# |
6386 |
6387 |
|
|
|
... |
... |
Solution +.# |
6451 |
6452 |
|
|
6452 |
6453 |
Solution +.# |
Solution +.# |
6453 |
6454 |
|
|
6454 |
|
: Just because a coincidence exists (i.e., "in my data set, no android user is color-blind") does not mean that it will always be true (i.e., "no color-blind person will ever use android"). Functional dependencies should come from a principled reasonning about the attributes, and not from the observation of the data. |
|
|
6455 |
|
: Just because a coincidence exists (i.e., "in my data set, no android user is color-blind") does not mean that it will always be true (i.e., "no color-blind person will ever use android"). Functional dependencies should come from a principled reasoning about the attributes, and not from the observation of the data. |
6455 |
6456 |
|
|
6456 |
6457 |
Solution +.# |
Solution +.# |
6457 |
6458 |
~ |
~ |
|
... |
... |
You can also have a look at the end of `code/java/AdvancedProg.java`, which crea |
8072 |
8073 |
|
|
8073 |
8074 |
Exercise +.# |
Exercise +.# |
8074 |
8075 |
|
|
8075 |
|
: What are the technologies that makes it possible for a Java application to communicate with a DBMS? |
|
|
8076 |
|
: What are the technologies that make it possible for a Java application to communicate with a DBMS? |
8076 |
8077 |
|
|
8077 |
8078 |
Exercise +.# |
Exercise +.# |
8078 |
8079 |
|
|
|
... |
... |
Exercise +.# |
8122 |
8123 |
|
|
8123 |
8124 |
Exercise +.# |
Exercise +.# |
8124 |
8125 |
|
|
8125 |
|
: In what class is the `getColumnName` method? |
|
|
8126 |
|
: In what class is the `getColumnName()` method? |
8126 |
8127 |
|
|
8127 |
8128 |
|
|
8128 |
8129 |
Exercise +.# |
Exercise +.# |
8129 |
8130 |
~ |
~ |
8130 |
8131 |
|
|
8131 |
|
Assuming `stmt`{.java} is a `Statement`{.java} object, in the statement |
|
|
8132 |
|
Assuming `stmt`{.java} is a `Statement`{.java} object, in the statement: |
8132 |
8133 |
|
|
8133 |
8134 |
```{.java} |
```{.java} |
8134 |
8135 |
modif = stmt.executeUpdate(strC); |
modif = stmt.executeUpdate(strC); |
8135 |
8136 |
``` |
``` |
8136 |
|
what is |
|
|
8137 |
|
What is... |
8137 |
8138 |
|
|
8138 |
|
#. the datatype of `modif`{.java}? |
|
8139 |
|
#. the datatype of `strC`{.java}? |
|
8140 |
|
#. a(n example of a) possible value for `strC`{.java}? |
|
|
8139 |
|
#. ... the datatype of `modif`{.java}? |
|
8140 |
|
#. ... the datatype of `strC`{.java}? |
|
8141 |
|
#. ... a possible value for `strC`{.java}? |
8141 |
8142 |
|
|
8142 |
8143 |
|
|
8143 |
8144 |
Exercise +.# |
Exercise +.# |
|
... |
... |
Exercise +.# |
8147 |
8148 |
|
|
8148 |
8149 |
Exercise +.# |
Exercise +.# |
8149 |
8150 |
|
|
8150 |
|
: Assuming `ps` is the prepared statement `INSERT INTO EXAM VALUES (?, ?)`, write the three statements needed to allocate "Quiz" and "5" to the two slots, and then to execute the prepared statement on the database. |
|
|
8151 |
|
: Assume `ps` is the prepared statement: |
|
8152 |
|
|
|
8153 |
|
```{.java} |
|
8154 |
|
INSERT INTO EXAM VALUES (?, ?); |
|
8155 |
|
``` |
|
8156 |
|
Write the three statements needed to allocate "Quiz" and "5" to the two slots and to execute the prepared statement in the database. |
8151 |
8157 |
|
|
8152 |
8158 |
Exercise +.#ErrorsInCode |
Exercise +.#ErrorsInCode |
8153 |
8159 |
~ |
~ |
8154 |
8160 |
|
|
8155 |
8161 |
In the code below, there are five errors between line 13 and line 32. |
In the code below, there are five errors between line 13 and line 32. |
8156 |
8162 |
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.). |
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.). |
8157 |
|
For each error, highlight it precisely and give a short explanation. |
|
|
8163 |
|
Highlight each error and explain why it is an error. |
8158 |
8164 |
|
|
8159 |
8165 |
```{.java .numberLines include=code/java/ProgWithErrors.java} |
```{.java .numberLines include=code/java/ProgWithErrors.java} |
8160 |
8166 |
``` |
``` |
8161 |
8167 |
|
|
8162 |
8168 |
Exercise +.# |
Exercise +.# |
8163 |
8169 |
|
|
8164 |
|
: Write a small program that determine whenever the `null` value from Java is equal to the `NULL` value in your DBMS. |
|
|
8170 |
|
: Write a program that determines if the `null` value from Java code is equal to the `NULL` value in the DBMS. |
8165 |
8171 |
|
|
8166 |
8172 |
## Solution to Exercises {-} |
## Solution to Exercises {-} |
8167 |
8173 |
|
|
8168 |
8174 |
Solution +.# |
Solution +.# |
8169 |
8175 |
|
|
8170 |
|
: API's and driver's to implement them. |
|
|
8176 |
|
: The technologies theat make it possible for a Java application to communicate with w DBMS are API's and the drivers to implement them. |
8171 |
8177 |
|
|
8172 |
8178 |
Solution +.# |
Solution +.# |
8173 |
8179 |
|
|
|
... |
... |
Solution +.# |
8175 |
8181 |
|
|
8176 |
8182 |
Solution +.# |
Solution +.# |
8177 |
8183 |
|
|
8178 |
|
: You can find them listed at <https://docs.oracle.com/javase/7/docs/api/java/sql/package-summary.html>. We have used `Connection`, `DatabaseMetaData`, `ResultSetMetaData`, `PreparedStatement`, and `Statement` to name a few. |
|
|
8184 |
|
: There are many classes in the SQL API if Java. There are `Connection`, `DatabaseMetaData`, `ResultSetMetaData`, `PreparedStatement`, and `Statement` to name a few. You can find them listed at <https://docs.oracle.com/javase/7/docs/api/java/sql/package-summary.html>. |
8179 |
8185 |
|
|
8180 |
8186 |
Solution +.# |
Solution +.# |
8181 |
8187 |
|
|
8182 |
|
: DriverManager.getConnection() |
|
|
8188 |
|
: The JDBC method that must be called to connect to a database is DriverManager.getConnection() |
8183 |
8189 |
|
|
8184 |
8190 |
Solution +.# |
Solution +.# |
8185 |
8191 |
|
|
8186 |
|
: A `Statement` object is used to create a `ResultSet` object, e.g. by calling the `executeQuery` method. |
|
|
8192 |
|
: The class of the object used to create a `ResultSet` object is the `Statement` class. A `Statement` object is used to create a `ResultSet` object, e.g. by calling the `executeQuery` method. |
8187 |
8193 |
|
|
8188 |
8194 |
Solution +.# |
Solution +.# |
8189 |
8195 |
|
|
8190 |
8196 |
: The `next()` method checks if there is data to read and, if there is, it moves the cursor to read it. |
: The `next()` method checks if there is data to read and, if there is, it moves the cursor to read it. |
8191 |
|
Its return type is a Boolean. |
|
|
8197 |
|
Its return type is Boolean. |
8192 |
8198 |
|
|
8193 |
8199 |
Solution +.# |
Solution +.# |
8194 |
8200 |
|
|
8195 |
|
: Using `.executeQuery(strSelect)`{.java} |
|
|
8201 |
|
: You submit a `SELECT`{.java} statement to the DBMS by using `.executeQuery(strSelect)`{.java}. |
8196 |
8202 |
|
|
8197 |
8203 |
Solution +.# |
Solution +.# |
8198 |
8204 |
|
|
8199 |
|
: The `executeUpdate` or `execute` methods can be used to perform an `INSERT` command from our program. |
|
|
8205 |
|
: The `executeUpdate()` or `execute()` methods can be used to perform an `INSERT` command from our program. |
8200 |
8206 |
|
|
8201 |
8207 |
Solution +.# |
Solution +.# |
8202 |
8208 |
|
|
8203 |
|
: Before the first line. Using the `next` method. |
|
|
8209 |
|
: The `ResultSet` object's cursor is initially pointing at the position before the first line. We move the cursor forward by using the `next()` method. |
8204 |
8210 |
|
|
8205 |
8211 |
Solution +.# |
Solution +.# |
8206 |
8212 |
|
|
8207 |
|
: `first`, `last`, `next`, `previous`, `relative`, `absolute` methods. |
|
|
8213 |
|
: There are many navigation methods provided by `ResulSet`. They are the `first()`, `last()`, `next()`, `previous()`, `relative()`, and `absolute()` methods. |
8208 |
8214 |
|
|
8209 |
8215 |
Solution +.# |
Solution +.# |
8210 |
8216 |
|
|
8211 |
|
: Connect to `localhost:3306` and create a new database if needed, and use secure connection. |
|
|
8217 |
|
: This JDBC URL format connects to `localhost:3306`, creates a new database if needed, and uses the secure SSL connection. |
8212 |
8218 |
|
|
8213 |
8219 |
|
|
8214 |
8220 |
Solution +.# |
Solution +.# |
8215 |
8221 |
|
|
8216 |
|
: In `modif = stmt.executeUpdate(strC);`{.java}, `modif` is an integer (the number of rows modified by the query), `strC` is a `String` (a `SQL` command), and an example of value is `DELETE FROM BOOKS Where Price > 0.5`. |
|
|
8222 |
|
: The `getColumnName()` method is in the `ResultSetMetaData` class. |
|
8223 |
|
|
8217 |
8224 |
|
|
8218 |
8225 |
Solution +.# |
Solution +.# |
8219 |
8226 |
|
|
8220 |
|
: `ResultSetMetaData` |
|
|
8227 |
|
: In the statement `modif = stmt.executeUpdate(strC);`{.java}... |
|
8228 |
|
|
|
8229 |
|
#. `modif` is an integer (the number of rows modified by the query). |
|
8230 |
|
#. `strC` is a `String` (a `SQL` command). |
|
8231 |
|
#. A possible value for `strC` is `DELETE FROM BOOKS Where Price > 0.5`. |
8221 |
8232 |
|
|
8222 |
8233 |
Solution +.# |
Solution +.# |
8223 |
8234 |
|
|
8224 |
|
: A prepared statement is a feature used to execute the same (or similar) `SQL` statements repeatedly with high efficiency. |
|
|
8235 |
|
: A prepared statement is a feature used to execute `SQL` statements repeatedly with high efficiency that protects against `SQL` injections. |
8225 |
8236 |
|
|
8226 |
8237 |
Solution +.# |
Solution +.# |
8227 |
8238 |
~ |
~ |
|
... |
... |
Solution +.# |
8237 |
8248 |
|
|
8238 |
8249 |
The errors are: |
The errors are: |
8239 |
8250 |
|
|
8240 |
|
- line 16, `stmt.executeUpdate(strSelect)`{.java}: `executeUpdate`{.java} cannot be used to perform `SELECT` statements. |
|
8241 |
|
- line 21, this error is subtle: we need to display the last record before using `previous()`{.java}, otherwise it would be just skipped. We can fix this using a `do`…`while` loop. |
|
8242 |
|
- line 22, `String title = rset.getDouble("title");`{.java}: `getDouble`{.java} returns a `double`{.java}, and hence cannot be stored as a `String`{.java}. |
|
8243 |
|
- line 28, `ps.executeQuery()`{.java}: the prepared statement did not received a value for the `?` argument. |
|
|
8251 |
|
- The first highlighted portion is found in the `ResultSet` object creation line and should only be this part: |
|
8252 |
|
|
|
8253 |
|
```{.java} |
|
8254 |
|
stmt.executeUpdate(strSelect); |
|
8255 |
|
``` |
|
8256 |
|
The error is that the `executeUpdate()` method cannot be used to perform `SELECT` statements. |
|
8257 |
|
|
|
8258 |
|
- The second highlighted portion is found in the while loop condition statement: |
|
8259 |
|
|
|
8260 |
|
```{.java} |
|
8261 |
|
rset.previous() |
|
8262 |
|
``` |
|
8263 |
|
|
|
8264 |
|
This error *is* subtle: we need to display the last record before using the `previous()`{.java} method, otherwise it would be skipped. We can fix this using a `do`…`while` loop. |
|
8265 |
|
|
|
8266 |
|
- The third highlighted portion is found in the creation statement for the `String` object named `title`: |
|
8267 |
|
|
|
8268 |
|
```{.java} |
|
8269 |
|
String title = rset.getDouble("title"); |
|
8270 |
|
``` |
|
8271 |
|
|
|
8272 |
|
The error is that the `getDouble()`{.java} method returns a `double`{.java}, which cannot be stored as a `String`{.java}. |
|
8273 |
|
|
|
8274 |
|
- The fourth highlighted portion is found in the creation statement for the `ResulSet` object named `result`: |
|
8275 |
|
|
|
8276 |
|
```{.java} |
|
8277 |
|
ps.executeQuery(); |
|
8278 |
|
``` |
|
8279 |
|
|
|
8280 |
|
The error here comes from the previous prepared statement that did not receive a value for the `?`. |
8244 |
8281 |
|
|
8245 |
|
You can find the program patched in `code/java/ProgWithErrorsPatched.java`, the (relevant) diff. is: |
|
|
8282 |
|
You can find the corrected program in `code/java/ProgWithErrorsPatched.java`, which looks like: |
8246 |
8283 |
|
|
8247 |
8284 |
```{.bash} |
```{.bash} |
8248 |
8285 |
16c16 |
16c16 |
|
... |
... |
Solution +.# |
8281 |
8318 |
Solution +.# |
Solution +.# |
8282 |
8319 |
~ |
~ |
8283 |
8320 |
|
|
8284 |
|
Here it is: |
|
|
8321 |
|
Here is what the program should look like: |
8285 |
8322 |
|
|
8286 |
8323 |
|
|
8287 |
8324 |
```{.java .numberLines include=code/java/TestForNull.java} |
```{.java .numberLines include=code/java/TestForNull.java} |
|
... |
... |
Solution to [%D %n (%T)](#problem:xmltoeraward) |
9287 |
9324 |
The next difficulty is the status of the award itself: should it be a relationship with many attributes, between the RESEARCHER and INSTITUTION entities? |
The next difficulty is the status of the award itself: should it be a relationship with many attributes, between the RESEARCHER and INSTITUTION entities? |
9288 |
9325 |
The issue with this approach is that an award can have multiple investigators, as shown in the example, and that this number can vary. Hence, fixing the arity and constraints on this relationship will be difficult. |
The issue with this approach is that an award can have multiple investigators, as shown in the example, and that this number can vary. Hence, fixing the arity and constraints on this relationship will be difficult. |
9289 |
9326 |
We could have a relation of arity `2`, and "duplicate it" if multiple researchers are involved in the same grant, but that seems like a poor choice (since all the information about the grant will need to be duplicated). |
We could have a relation of arity `2`, and "duplicate it" if multiple researchers are involved in the same grant, but that seems like a poor choice (since all the information about the grant will need to be duplicated). |
9290 |
|
Therefore, it seems more reasonnable to make the award an entity. |
|
|
9327 |
|
Therefore, it seems more reasonable to make the award an entity. |
9291 |
9328 |
|
|
9292 |
9329 |
How should we connect the AWARD entity with the RESEARCHER and INSTITUTION entities? |
How should we connect the AWARD entity with the RESEARCHER and INSTITUTION entities? |
9293 |
9330 |
A ternary relation has some drawbacks, since it would require some duplication when multiple investigators are working on the same award. |
A ternary relation has some drawbacks, since it would require some duplication when multiple investigators are working on the same award. |