File exercises/sum.md added (mode: 100644) (index 0000000..d4438fe) |
|
1 |
|
--- |
|
2 |
|
documentclass: scrreprt |
|
3 |
|
papersize: letter |
|
4 |
|
link-citations: true |
|
5 |
|
title: CSCI 3410 - Database Systems |
|
6 |
|
subtitle: Lecture Notes (Draft) |
|
7 |
|
author: Clément Aubert |
|
8 |
|
institute: Augusta University |
|
9 |
|
dir: ltr |
|
10 |
|
lang: en |
|
11 |
|
numbersections: true |
|
12 |
|
keywords: |
|
13 |
|
- Computer Science |
|
14 |
|
- Database |
|
15 |
|
- MySQL programming |
|
16 |
|
header-includes: |
|
17 |
|
- \usepackage[table]{xcolor} |
|
18 |
|
- \setromanfont[Ligatures={Common,TeX}]{Linux Libertine O} |
|
19 |
|
- \setmainfont[Ligatures={Common,TeX}]{Linux Libertine O} |
|
20 |
|
- \setmonofont{Latin Modern Mono Light} |
|
21 |
|
- \setmonofont[SmallCapsFont={Latin Modern Mono Caps}]{Latin Modern Mono Light} |
|
22 |
|
- \usepackage{xunicode} |
|
23 |
|
- \usepackage{fvextra} |
|
24 |
|
- \DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}} |
|
25 |
|
- \definecolor{lgray}{HTML}{e6e7e8} |
|
26 |
|
--- |
|
27 |
|
|
|
28 |
|
<!-- |
|
29 |
|
|
|
30 |
|
pandoc sum.md --pdf-engine=xelatex --toc --filter pandoc-numbering --top-level-division=chapter -M date="`date "+%B %e, %Y"`" -o sum.pdf |
|
31 |
|
|
|
32 |
|
pandoc sum.md --pdf-engine=xelatex --toc --filter pandoc-numbering --top-level-division=chapter -M date="`date "+%B %e, %Y"`" -s -o sum.tex && xelatex sum.tex |
|
33 |
|
|
|
34 |
|
--> |
|
35 |
|
|
|
36 |
|
Exercise +.# |
|
37 |
|
|
|
38 |
|
What does it mean to say that `SQL` is at the same time a "data definition language" and a "data manipulation language"? |
|
39 |
|
|
|
40 |
|
|
|
41 |
|
Solution +.# |
|
42 |
|
|
|
43 |
|
It can specify the conceptual and internal schema, \textbf{and} it can manipulate the data. |
|
44 |
|
|
|
45 |
|
|
|
46 |
|
Exercise +.# |
|
47 |
|
|
|
48 |
|
Name three kind of objects (for lack of a better word) a `CREATE` statement can create. |
|
49 |
|
|
|
50 |
|
|
|
51 |
|
Solution +.# |
|
52 |
|
|
|
53 |
|
Database (schema), table, view, assertion, trigger, etc. |
|
54 |
|
|
|
55 |
|
|
|
56 |
|
Exercise +.# |
|
57 |
|
|
|
58 |
|
Write a `SQL` statement that adds a primary key constraint to an attribute named `Id` in an already existing table named `STAFF`. |
|
59 |
|
|
|
60 |
|
|
|
61 |
|
Solution +.# |
|
62 |
|
|
|
63 |
|
`ALTER TABLE STAFF ADD PRIMARY KEY(Id);` |
|
64 |
|
|
|
65 |
|
|
|
66 |
|
Exercise +.# |
|
67 |
|
|
|
68 |
|
Complete the following table wo different examples when asked for examples: |
|
69 |
|
|
|
70 |
|
\begin{tabular}{m{6cm} | m{6cm} } |
|
71 |
|
\hline |
|
72 |
|
\rowcolor{lgray} |
|
73 |
|
Data Type & Examples \\ |
|
74 |
|
\hline |
|
75 |
|
Int & $4$, $-32$\\ |
|
76 |
|
\hline |
|
77 |
|
Char(4) & %\blank[width=1.9em]{'trai', 'plol'} |
|
78 |
|
\\ |
|
79 |
|
\hline |
|
80 |
|
% \blank[width=1.9em]{VarChar(10)} |
|
81 |
|
& 'Train', 'Michelle'\\ |
|
82 |
|
\hline |
|
83 |
|
Bit(4) & % \blank[width=1.9em]{ B'1010', B'0101'} |
|
84 |
|
\\ |
|
85 |
|
\hline |
|
86 |
|
% \blank[width=1.9em]{Boolean} |
|
87 |
|
& TRUE, UNKNOWN\\ |
|
88 |
|
\hline |
|
89 |
|
\end{tabular} |
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
|
Exercise +.# |
|
94 |
|
|
|
95 |
|
Explain what the following SQL statement does |
|
96 |
|
|
|
97 |
|
```mysql |
|
98 |
|
CREATE SCHEMA Faculty; |
|
99 |
|
``` |
|
100 |
|
|
|
101 |
|
Solution +.# |
|
102 |
|
|
|
103 |
|
It creates a schema, i.e., a database, named "Faculty". |
|
104 |
|
|
|
105 |
|
|
|
106 |
|
Exercise +.# |
|
107 |
|
|
|
108 |
|
If I want to enter January 21, 2016, as a value for an attribute with the `DATE` datatype, what value should I enter? |
|
109 |
|
|
|
110 |
|
|
|
111 |
|
Solution +.# |
|
112 |
|
|
|
113 |
|
`DATE`'2016-01-21' |
|
114 |
|
|
|
115 |
|
|
|
116 |
|
Exercise +.# |
|
117 |
|
|
|
118 |
|
Write a statement that inserts the values "Thomas" and "$4$" into the table `TRAINS`. |
|
119 |
|
|
|
120 |
|
|
|
121 |
|
Solution +.# |
|
122 |
|
|
|
123 |
|
```sql |
|
124 |
|
INSERT INTO TRAINS VALUES('Thomas', 4); |
|
125 |
|
``` |
|
126 |
|
|
|
127 |
|
|
|
128 |
|
Exercise +.# |
|
129 |
|
|
|
130 |
|
If `PkgName` is a primary key, what can you tell about the number of rows returned by the following statement? |
|
131 |
|
```sql |
|
132 |
|
SELECT * FROM MYTABLE WHERE PkgName = 'MySQL'; |
|
133 |
|
``` |
|
134 |
|
|
|
135 |
|
|
|
136 |
|
Solution +.# |
|
137 |
|
|
|
138 |
|
Yes. |
|
139 |
|
|
|
140 |
|
|
|
141 |
|
Exercise +.# |
|
142 |
|
|
|
143 |
|
What is the difference between an implicit, an explicit, and a semantic constraint? |
|
144 |
|
|
|
145 |
|
|
|
146 |
|
Solution +.# |
|
147 |
|
|
|
148 |
|
The textbook reads, pp. 67 - 69: |
|
149 |
|
\begin{enumerate} |
|
150 |
|
\item Constraints that are inherent in the data model. We call these inherent model-based constraints or implicit constraints. |
|
151 |
|
\item Constraints that can be directly expressed in schemas of the data model, typically by specifying them in the DDL (data definition language, see Section 2.3.1). We call these schema-based constraints or explicit constraints. |
|
152 |
|
\item Constraints that cannot be directly expressed in the schemas of the data model, and hence must be expressed and enforced by the application programs. We call these application-based or semantic constraints or business rules. |
|
153 |
|
\end{enumerate} |
|
154 |
|
|
|
155 |
|
|
|
156 |
|
Exercise +.# |
|
157 |
|
|
|
158 |
|
If you want every tuple referencing the tuple you're about to delete to be deleted as well, what mechanism should you use? |
|
159 |
|
|
|
160 |
|
|
|
161 |
|
Solution +.# |
|
162 |
|
|
|
163 |
|
We should use a referential triggered action clause, `ON DELETE CASCADE`. |
|
164 |
|
|
|
165 |
|
|
|
166 |
|
Exercise +.# |
|
167 |
|
|
|
168 |
|
If a database designer is using the `ON UPDATE SET NULL` for a foreign key, what mechanism is he implementing (i.e., describe how the database will react a certain operation)? |
|
169 |
|
|
|
170 |
|
|
|
171 |
|
Solution +.# |
|
172 |
|
|
|
173 |
|
If the referenced tuple is updated, then the attribute of the referencing relation are set to NULL. |
|
174 |
|
|
|
175 |
|
|
|
176 |
|
Exercise +.# |
|
177 |
|
|
|
178 |
|
If the following is part of the design of a table: |
|
179 |
|
|
|
180 |
|
```sqlmysql |
|
181 |
|
FOREIGN KEY (DptNumber) REFERENCES DEPARTMENT(DptNumber) ON DELETE SET DEFAULT ON UPDATE CASCADE |
|
182 |
|
``` |
|
183 |
|
|
|
184 |
|
What happen to the row whose foreign key `DptNumber` is set to $3$ if |
|
185 |
|
\begin{enumerate} |
|
186 |
|
\item the row in the `DEPARTEMENT` table with primary key `DptNumber` set to \(3\) is deleted? |
|
187 |
|
\item the row in the `DEPARTEMENT` table with primary key `DptNumber` set to \(3\) as its value for `DptNumber` updated to \(5\)? |
|
188 |
|
\end{enumerate} |
|
189 |
|
|
|
190 |
|
|
|
191 |
|
|
|
192 |
|
Solution +.# |
|
193 |
|
|
|
194 |
|
The department number is set to the default value. |
|
195 |
|
|
|
196 |
|
The department number is updated accordingly. |
|
197 |
|
|
|
198 |
|
|
|
199 |
|
Exercise +.# |
|
200 |
|
|
|
201 |
|
If the following is part of the design of a `WORKER` table: |
|
202 |
|
|
|
203 |
|
```sqlmysql |
|
204 |
|
FOREIGN KEY WORKER(DptNumber) REFERENCES DEPARTMENT(DptNumber) ON UPDATE CASCADE |
|
205 |
|
``` |
|
206 |
|
|
|
207 |
|
Admitting there is a row in the `WORKER` table whose foreign key `DptNumber` is set to $3$, what would happen if |
|
208 |
|
\begin{enumerate} |
|
209 |
|
\item We tried to delete the row in the `DEPARTEMENT` table with primary key `DptNumber` set to \(3\)? |
|
210 |
|
\item We tried to change the value of `DptNumber` of the row in the `DEPARTEMENT` table with primary key `DptNumber` set to \(3\)? |
|
211 |
|
\end{enumerate} |
|
212 |
|
|
|
213 |
|
|
|
214 |
|
|
|
215 |
|
Exercise +.# |
|
216 |
|
|
|
217 |
|
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. |
|
218 |
|
|
|
219 |
|
|
|
220 |
|
Solution +.# |
|
221 |
|
|
|
222 |
|
```sqlmysql |
|
223 |
|
SELECT Name, Address |
|
224 |
|
FROM TOURIST |
|
225 |
|
WHERE EntryDate > DATE'2012-09-15'; |
|
226 |
|
``` |
|
227 |
|
|
|
228 |
|
|
|
229 |
|
|
|
230 |
|
|
|
231 |
|
Exercise +.# |
|
232 |
|
|
|
233 |
|
What is the fully qualified name of an attribute? Give an example. |
|
234 |
|
|
|
235 |
|
|
|
236 |
|
Solution +.# |
|
237 |
|
|
|
238 |
|
The name of the relation with the name of its schema and a period beforehand. myDB.MYTABLE, EMPLOYEE.name, etc. |
|
239 |
|
|
|
240 |
|
|
|
241 |
|
% Exercise +.# |
|
242 |
|
|
|
243 |
|
If `DEPARTMENT` is a database, what is `DEPARTMENT.*`?} |
|
244 |
|
|
|
245 |
|
|
|
246 |
|
Solution +.# |
|
247 |
|
|
|
248 |
|
All the tables in that database. |
|
249 |
|
|
|
250 |
|
|
|
251 |
|
Exercise +.# |
|
252 |
|
|
|
253 |
|
What is a multi-set? What does it mean to say that SQL treats tables as multisets? |
|
254 |
|
|
|
255 |
|
|
|
256 |
|
Solution +.# |
|
257 |
|
|
|
258 |
|
A set where the same value can occur twice. |
|
259 |
|
The same tuple can occur twice in a table. |
|
260 |
|
|
|
261 |
|
|
|
262 |
|
Exercise +.# |
|
263 |
|
|
|
264 |
|
What is the difference between those two queries? |
|
265 |
|
```mySQL |
|
266 |
|
SELECT ALL * FROM MYTABLE; |
|
267 |
|
``` |
|
268 |
|
and |
|
269 |
|
```mySQL |
|
270 |
|
SELECT DISTINCT * FROM MYTABLE; |
|
271 |
|
``` |
|
272 |
|
How are the results the same? How are they different? |
|
273 |
|
|
|
274 |
|
|
|
275 |
|
Solution +.# |
|
276 |
|
|
|
277 |
|
All will print the duplicate, distinct will eliminate them. |
|
278 |
|
|
|
279 |
|
Problem +.# |
|
280 |
|
|
|
281 |
|
*This exercise assume you successfully completed Problem~2 of Homework 1.* |
|
282 |
|
|
|
283 |
|
1. Connect to your MySQL DBMS as `testuser`: |
|
284 |
|
- **In windows**, open a command prompt (search for "cmd") and type |
|
285 |
|
|
|
286 |
|
```bat |
|
287 |
|
cd "C:\Program Files\MySQL\MySQL Server 5.7\bin" |
|
288 |
|
``` |
|
289 |
|
- **In Linux**, open a shell (as a normal user) |
|
290 |
|
|
|
291 |
|
Then, in both cases, type |
|
292 |
|
|
|
293 |
|
```plain |
|
294 |
|
mysql -u testuser -p |
|
295 |
|
``` |
|
296 |
|
|
|
297 |
|
and enter the password `password`. |
|
298 |
|
If you are prompted with a message |
|
299 |
|
|
|
300 |
|
```bash |
|
301 |
|
ERROR 1045 (28000): Access denied for user 'testuser'@'localhost' (using password: YES) |
|
302 |
|
``` |
|
303 |
|
|
|
304 |
|
then you probably typed the wrong password. |
|
305 |
|
Otherwise, you should see a welcoming message from MySQL / MariaDB and a prompt. |
|
306 |
|
2. Create a new database called `HW_2Q1` |
|
307 |
|
|
|
308 |
|
```sqlmysql |
|
309 |
|
CREATE DATABASE HW_2Q1; |
|
310 |
|
``` |
|
311 |
|
3. List the databases |
|
312 |
|
|
|
313 |
|
```sqlmysql |
|
314 |
|
SHOW DATABASES; |
|
315 |
|
``` Make sure `HW_2Q1` is a part of it. |
|
316 |
|
4. Remove ("drop") the database, using |
|
317 |
|
|
|
318 |
|
```sqlmysql |
|
319 |
|
DROP DATABASE HW_2Q1; |
|
320 |
|
``` |
|
321 |
|
In the future, we will refer to the commands \ref{item-connect}.\ and \ref{item-create}.\ as "log-in as `testuser`" and create a database `HW_2Q1`. |
|
322 |
|
|
|
323 |
|
|
|
324 |
|
|
|
325 |
|
Problem +.# |
|
326 |
|
|
|
327 |
|
The goal of this problem is to learn where to find the documentation of your DBMS, and to have a first look at the syntax of SQL commands. |
|
328 |
|
|
|
329 |
|
You can consult your textbook, Table~5.2, p. 140 (6th edition) or Table~7.2, p. 235 (7th edition), for a very quick summary of the most common commands. |
|
330 |
|
Make sure you are familiar with the Backus–Naur form (BNF) notation commonly used: |
|
331 |
|
\begin{itemize} |
|
332 |
|
\item non-terminal symbols (i.e., variables, parameters) are enclosed in angled brackets, |
|
333 |
|
```plain |
|
334 |
|
<...> |
|
335 |
|
``` |
|
336 |
|
|
|
337 |
|
\item optional parts are shown in square brackets, |
|
338 |
|
```plain |
|
339 |
|
[...] |
|
340 |
|
``` |
|
341 |
|
|
|
342 |
|
\item repetitons are shown in braces |
|
343 |
|
```plain |
|
344 |
|
{...} |
|
345 |
|
``` |
|
346 |
|
|
|
347 |
|
\item alternatives are shown in parenthesis and separated by vertical bars, |
|
348 |
|
|
|
349 |
|
```plain |
|
350 |
|
(...|...|...) |
|
351 |
|
``` |
|
352 |
|
|
|
353 |
|
\end{itemize} |
|
354 |
|
|
|
355 |
|
The most complete lists of commands are probably at |
|
356 |
|
\begin{itemize} |
|
357 |
|
\item \url{https://mariadb.com/kb/en/library/sql-statements/} and |
|
358 |
|
\item \url{https://dev.mysql.com/doc/refman/5.7/en/sql-syntax.html} |
|
359 |
|
\end{itemize} |
|
360 |
|
Those are the commands implemented in the DBMS you are actually using. |
|
361 |
|
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. |
|
362 |
|
|
|
363 |
|
|
|
364 |
|
Problem +.# |
|
365 |
|
|
|
366 |
|
\emph{This exercise, and the following ones, assume you successfully completed Problem~\ref{setup}.} |
|
367 |
|
|
|
368 |
|
Log in as `testuser` and create a database `HW_2Q3`. |
|
369 |
|
|
|
370 |
|
1. We will first set-up our meta-data (i.e., the schema, the structure where our tables will be, and the tables themselves). |
|
371 |
|
|
|
372 |
|
Let us first tell MySQL that we want to use that database |
|
373 |
|
```sqlmysql |
|
374 |
|
USE HW_2Q3; |
|
375 |
|
``` |
|
376 |
|
and ask what it contains |
|
377 |
|
```sqlmysql |
|
378 |
|
SHOW TABLES; |
|
379 |
|
``` |
|
380 |
|
Now, create a first table named `NAME` |
|
381 |
|
|
|
382 |
|
```sqlmysql |
|
383 |
|
CREATE TABLE NAME( |
|
384 |
|
FName VARCHAR(15), |
|
385 |
|
LName VARCHAR(15), |
|
386 |
|
Id INT, |
|
387 |
|
PRIMARY KEY(Id) |
|
388 |
|
); |
|
389 |
|
``` |
|
390 |
|
|
|
391 |
|
|
|
392 |
|
Note that the `SQL` syntax and your DBMS are completely fine with your statement spreading over multiple lines. |
|
393 |
|
Let us create a second table, named `ADDRESS` |
|
394 |
|
|
|
395 |
|
```sqlmysql |
|
396 |
|
CREATE TABLE ADDRESS( |
|
397 |
|
StreetName VARCHAR(15), |
|
398 |
|
Number INT, |
|
399 |
|
Habitants INT, |
|
400 |
|
PRIMARY KEY(StreetName, Number) |
|
401 |
|
); |
|
402 |
|
``` |
|
403 |
|
|
|
404 |
|
|
|
405 |
|
To make sure those two tables were actually created, we can use |
|
406 |
|
|
|
407 |
|
```sqlmysql |
|
408 |
|
SHOW TABLES; |
|
409 |
|
``` |
|
410 |
|
But how to make sure that you entered the attributes correctly? |
|
411 |
|
One way to make sure is to enter the command |
|
412 |
|
|
|
413 |
|
```sqlmysql |
|
414 |
|
show create table name; |
|
415 |
|
``` |
|
416 |
|
|
|
417 |
|
```sqlmysql |
|
418 |
|
DESC ADDRESS; |
|
419 |
|
``` |
|
420 |
|
and to examine carefully the message printed. You should read |
|
421 |
|
|
|
422 |
|
```sqlmysql |
|
423 |
|
+------------+-------------+------+-----+---------+-------+ |
|
424 |
|
| Field | Type| Null | Key | Default | Extra | |
|
425 |
|
+------------+-------------+------+-----+---------+-------+ |
|
426 |
|
| StreetName | varchar(15) | NO | PRI | NULL| | |
|
427 |
|
| Number | int(11) | NO | PRI | NULL| | |
|
428 |
|
| Habitants | int(11) | YES | | NULL| | |
|
429 |
|
+------------+-------------+------+-----+---------+-------+ |
|
430 |
|
``` |
|
431 |
|
|
|
432 |
|
If you believe there is a mistake, you can erase ("drop") the \emph{table} (not the whole database, as we did in Problem~1) using |
|
433 |
|
|
|
434 |
|
```sqlmysql |
|
435 |
|
DROP TABLE ADDRESS; |
|
436 |
|
``` |
|
437 |
|
and then re-create it. Of course, you can do the same for the `NAME` table. |
|
438 |
|
|
|
439 |
|
Now, let us add a foreign key to the `ADDRESS` table: |
|
440 |
|
|
|
441 |
|
```sqlmysql |
|
442 |
|
|
|
443 |
|
ALTER TABLE ADDRESS |
|
444 |
|
ADD FOREIGN KEY (Habitants) |
|
445 |
|
REFERENCES NAME(Id); |
|
446 |
|
``` |
|
447 |
|
|
|
448 |
|
And observe the modification: |
|
449 |
|
```sqlmysql |
|
450 |
|
DESC ADDRESS; |
|
451 |
|
``` |
|
452 |
|
|
|
453 |
|
|
|
454 |
|
|
|
455 |
|
2. This second part is about data, i.e., filling our tables with actual information. |
|
456 |
|
We begin by adding some data in the `NAME` table: |
|
457 |
|
```sqlmysql |
|
458 |
|
INSERT INTO NAME VALUES ('Barbara', 'Liskov', 003); |
|
459 |
|
INSERT INTO NAME VALUES ('Tuong Lu', 'Kim', 004); |
|
460 |
|
INSERT INTO NAME VALUES ('Samantha', NULL, 080); |
|
461 |
|
``` |
|
462 |
|
|
|
463 |
|
|
|
464 |
|
To display the data we just inserted, we can use: |
|
465 |
|
```sqlmysql |
|
466 |
|
SELECT * FROM NAME; |
|
467 |
|
``` |
|
468 |
|
|
|
469 |
|
|
|
470 |
|
Do you notice anything regarding the values we entered for the `Id` attribute? |
|
471 |
|
|
|
472 |
|
We can add some data into the `ADDRESS` table (using a different syntax): |
|
473 |
|
```sqlmysql |
|
474 |
|
INSERT INTO ADDRESS (StreetName, Number, Habitants) |
|
475 |
|
VALUES |
|
476 |
|
('Armstrong Drive', 10019, 003), |
|
477 |
|
('North Broad St.', 23, 004), |
|
478 |
|
('Robert Lane', 120, NULL); |
|
479 |
|
``` |
|
480 |
|
|
|
481 |
|
|
|
482 |
|
And let's use our first update command: |
|
483 |
|
```sqlmysql |
|
484 |
|
UPDATE ADDRESS SET Habitants = 3 WHERE Number = 120; |
|
485 |
|
``` |
|
486 |
|
|
|
487 |
|
|
|
488 |
|
|
|
489 |
|
3. |
|
490 |
|
Now, do the following: |
|
491 |
|
1. Have a look at the formal definition of the |
|
492 |
|
```sqlmysql |
|
493 |
|
ALTER |
|
494 |
|
``` |
|
495 |
|
command, at \url{https://dev.mysql.com/doc/refman/5.7/en/alter-table.html} or \url{https://mariadb.com/kb/en/library/alter-table/}. |
|
496 |
|
2. Draw the relations corresponding to that database, including the domains and primary, as well as foreign, keys. |
|
497 |
|
3. Write a |
|
498 |
|
|
|
499 |
|
```sqlmysql |
|
500 |
|
SELECT |
|
501 |
|
``` |
|
502 |
|
statement that returns the |
|
503 |
|
```sqlmysql |
|
504 |
|
Id |
|
505 |
|
``` |
|
506 |
|
number of the person whose first name is "Samantha". |
|
507 |
|
% SELECT Id FROM NAME WHERE FName = 'Samantha'; |
|
508 |
|
4. Write a statement that violate the entity integrity constraint. What is the error message returned? |
|
509 |
|
% ERROR 1062 (23000): Duplicate entry '80' for key 'PRIMARY' |
|
510 |
|
5. Execute an `UPDATE` statement that violate the referential integrity constraint. What is the error message returned? |
|
511 |
|
% ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`HW_2Q3`.`ADDRESS`, CONSTRAINT `fk_id` FOREIGN KEY (`Habitants`) REFERENCES `name` (`Id`)) |
|
512 |
|
6. Write a statement that violate another kind of constraint. Explain what constraint you are violating, and explain the error message. |
|
513 |
|
% ERROR 1136 (21S01): Column count doesn't match value count at row 1 |
|
514 |
|
|
|
515 |
|
Problem +.# |
|
516 |
|
Log in as `testuser` and create a database `HW_2Q4`. Tell MySQL that you want to use that database, and create a table (I will assume you named it `EXAMPLE` in the following, but you are free to name it the way you want) with at least two attributes that have different data types. Don't declare a primary key yet. Answer the following: |
|
517 |
|
\begin{enumerate} |
|
518 |
|
\item Add a tuple to your table using |
|
519 |
|
```mySQL |
|
520 |
|
INSERT INTO EXAMPLE VALUES(X, Y); |
|
521 |
|
``` |
|
522 |
|
where "X" and "Y" are values with the right type. |
|
523 |
|
Try to add this tuple again. What do you observe? (You can use |
|
524 |
|
```mySQL |
|
525 |
|
SELECT * FROM EXAMPLE; |
|
526 |
|
``` |
|
527 |
|
to observe what is stored in this table.) |
|
528 |
|
\item Alter your table to add a primary key, using |
|
529 |
|
```mySQL |
|
530 |
|
ALTER TABLE EXAMPLE ADD PRIMARY KEY (Attribute); |
|
531 |
|
``` where `Attribute` is the name of the attribute you want to be a primary key. What do you observe? |
|
532 |
|
\item Empty your table using```sqlmysql |
|
533 |
|
DELETE FROM EXAMPLE; |
|
534 |
|
``` and alter your table to add a primary key, using the command we gave at the previous step. What do you observe? |
|
535 |
|
\item Try to add the same tuple twice. What do you observe? |
|
536 |
|
%\item Now, create \emph{another table} (just one attribute and no primary key is fine) and try to create a foreign key pointing to the attribute in `EXAMPLE` that is not the primary key. |
|
537 |
|
% What do you observe? |
|
538 |
|
% => Messy, they get an error message if the attribute is not the primary key or if the datatype isn't the same, or if the command is not properly formatted... The error message is too vague for them to learn anything. |
|
539 |
|
\end{enumerate} |
|
540 |
|
|
|
541 |
|
|
|
542 |
|
Solution +.# |
|
543 |
|
|
|
544 |
|
|
|
545 |
|
```sqlmysql |
|
546 |
|
CREATE TABLE EXAMPLE( |
|
547 |
|
X VARCHAR(15), |
|
548 |
|
Y INT); |
|
549 |
|
``` |
|
550 |
|
|
|
551 |
|
|
|
552 |
|
|
|
553 |
|
```sqlmysql |
|
554 |
|
INSERT INTO EXAMPLE VALUES('Train', 4); |
|
555 |
|
``` |
|
556 |
|
|
|
557 |
|
|
|
558 |
|
OK to insert same tuple twice. |
|
559 |
|
|
|
560 |
|
```sqlmysql |
|
561 |
|
ALTER TABLE EXAMPLE ADD PRIMARY KEY (X); |
|
562 |
|
ERROR 1062 (23000): Duplicate entry 'Train' for key 'PRIMARY' |
|
563 |
|
``` |
|
564 |
|
|
|
565 |
|
|
|
566 |
|
|
|
567 |
|
|
|
568 |
|
%%%%% 03 |
|
569 |
|
|
|
570 |
|
|
|
571 |
|
Exercise +.# |
|
572 |
|
|
|
573 |
|
Describe what the star do in the statement `SELECT ALL * FROM MYTABLE;`. |
|
574 |
|
|
|
575 |
|
|
|
576 |
|
Solution +.# |
|
577 |
|
|
|
578 |
|
It selects all the attributes. |
|
579 |
|
|
|
580 |
|
|
|
581 |
|
|
|
582 |
|
Exercise +.# |
|
583 |
|
|
|
584 |
|
What is wrong with the statement `SELECT * WHERE Name = 'CS' FROM DEPARTMENT;`? |
|
585 |
|
|
|
586 |
|
|
|
587 |
|
Solution +.# |
|
588 |
|
|
|
589 |
|
You can't have the `WHERE` before `FROM`. |
|
590 |
|
|
|
591 |
|
|
|
592 |
|
|
|
593 |
|
Exercise +.# |
|
594 |
|
|
|
595 |
|
When is it useful to use a select-project-join query? |
|
596 |
|
|
|
597 |
|
|
|
598 |
|
Solution +.# |
|
599 |
|
|
|
600 |
|
We use those query that projects on attributes using a selection and join conditions when we need to look for information gathered in multiple tables. |
|
601 |
|
|
|
602 |
|
|
|
603 |
|
Exercise +.# |
|
604 |
|
|
|
605 |
|
When is a tuple variable useful? |
|
606 |
|
|
|
607 |
|
|
|
608 |
|
Solution +.# |
|
609 |
|
|
|
610 |
|
It makes the distinction between two different copies of the same relation, it is useful when we want to select a tuple in a relation that is in a particular relation with a tuple in the same relation. |
|
611 |
|
|
|
612 |
|
cf. \url{https://stackoverflow.com/a/7698796/2657549}: |
|
613 |
|
They are useful for saving typing, but there are other reasons to use them: |
|
614 |
|
\begin{itemize} |
|
615 |
|
\item If you join a table to itself you must give it two different names otherwise referencing the table would be ambiguous. |
|
616 |
|
\item It can be useful to give names to derived tables, and in some database systems it is required... even if you never refer to the name. |
|
617 |
|
\end{itemize} |
|
618 |
|
|
|
619 |
|
|
|
620 |
|
Exercise +.# |
|
621 |
|
|
|
622 |
|
Write a query that changes the name of the professor whose login is 'caubert' to 'Hugo Pernot' in the table `PROF`. |
|
623 |
|
|
|
624 |
|
|
|
625 |
|
Solution +.# |
|
626 |
|
|
|
627 |
|
`UPDATE PROF SET Name = 'Hugo Pernot' WHERE Login = 'caubert';` |
|
628 |
|
|
|
629 |
|
|
|
630 |
|
Exercise +.# |
|
631 |
|
|
|
632 |
|
Can an `UPDATE` statement have a `WHERE` condition using an attribute that isn't the primary key? If no, justify, if yes, tell what could happen. |
|
633 |
|
|
|
634 |
|
|
|
635 |
|
Solution +.# |
|
636 |
|
|
|
637 |
|
Yes, we could update more than one tuple at a time. |
|
638 |
|
|
|
639 |
|
|
|
640 |
|
Exercise +.# |
|
641 |
|
|
|
642 |
|
What is a multi-set? |
|
643 |
|
What does it mean to say that `SQL` treats tables as multisets? |
|
644 |
|
|
|
645 |
|
|
|
646 |
|
Solution +.# |
|
647 |
|
|
|
648 |
|
A set where the same value can occur twice. |
|
649 |
|
The same tuple can occur twice in a table. |
|
650 |
|
|
|
651 |
|
|
|
652 |
|
Exercise +.# |
|
653 |
|
|
|
654 |
|
What is the difference between those two queries? |
|
655 |
|
```mySQL |
|
656 |
|
SELECT ALL * FROM MYTABLE; |
|
657 |
|
``` |
|
658 |
|
and |
|
659 |
|
```mySQL |
|
660 |
|
SELECT DISTINCT * FROM MYTABLE; |
|
661 |
|
``` |
|
662 |
|
How are the results the same? How are they different? |
|
663 |
|
|
|
664 |
|
|
|
665 |
|
Solution +.# |
|
666 |
|
|
|
667 |
|
`ALL` will print the duplicate, `DISTINCT` will eliminate them. |
|
668 |
|
|
|
669 |
|
|
|
670 |
|
Exercise +.# |
|
671 |
|
|
|
672 |
|
What are the possible meanings or interpretations for a `NULL` value? |
|
673 |
|
|
|
674 |
|
|
|
675 |
|
Solution +.# |
|
676 |
|
|
|
677 |
|
Unknown value, unavailable / withheld, N/A. |
|
678 |
|
|
|
679 |
|
|
|
680 |
|
|
|
681 |
|
Exercise +.# |
|
682 |
|
|
|
683 |
|
What are the values of the following expressions (i.e., do they evaluate to `TRUE`, `FALSE`, or `UNKNOWN`)? |
|
684 |
|
|
|
685 |
|
`TRUE AND FALSE`\hspace{2.2em} |
|
686 |
|
`TRUE AND UNKNOWN`\hspace{2.2em} |
|
687 |
|
`NOT UNKNOWN`\hspace{2.2em} |
|
688 |
|
`FALSE OR UNKNOWN` |
|
689 |
|
|
|
690 |
|
|
|
691 |
|
|
|
692 |
|
Solution +.# |
|
693 |
|
|
|
694 |
|
`FALSE`, `UNKNOWN`, `UNKNOWN`, `FALSE` |
|
695 |
|
|
|
696 |
|
|
|
697 |
|
Exercise +.# |
|
698 |
|
|
|
699 |
|
What comparison expression should you use to test if a value is different from `NULL`? |
|
700 |
|
|
|
701 |
|
|
|
702 |
|
Solution +.# |
|
703 |
|
|
|
704 |
|
`IS NOT` |
|
705 |
|
|
|
706 |
|
|
|
707 |
|
Exercise +.# |
|
708 |
|
|
|
709 |
|
Explain this query: |
|
710 |
|
|
|
711 |
|
```sqlmysql |
|
712 |
|
SELECT Login |
|
713 |
|
FROM PROF |
|
714 |
|
WHERE Department IN ( SELECT Major |
|
715 |
|
FROM STUDENT |
|
716 |
|
WHERE Login = 'jrakesh'); |
|
717 |
|
``` |
|
718 |
|
|
|
719 |
|
|
|
720 |
|
Solution +.# |
|
721 |
|
|
|
722 |
|
It list the login of the professors teaching in the department where the student whose login is "jrakesh" is majoring. |
|
723 |
|
|
|
724 |
|
|
|
725 |
|
Exercise +.# |
|
726 |
|
|
|
727 |
|
What is wrong with this query? |
|
728 |
|
|
|
729 |
|
```sqlmysql |
|
730 |
|
SELECT Name |
|
731 |
|
FROM STUDENT |
|
732 |
|
WHERE Login IN ( SELECT Code |
|
733 |
|
FROM Department |
|
734 |
|
WHERE head = 'aturing'); |
|
735 |
|
``` |
|
736 |
|
|
|
737 |
|
|
|
738 |
|
|
|
739 |
|
|
|
740 |
|
|
|
741 |
|
Solution +.# |
|
742 |
|
|
|
743 |
|
It tries to find a `Login` in a `Code`! |
|
744 |
|
|
|
745 |
|
|
|
746 |
|
Exercise +.# |
|
747 |
|
|
|
748 |
|
Write a query that returns the number of row (i.e., of entries, of tuples) in a table named `BOOK`. |
|
749 |
|
|
|
750 |
|
|
|
751 |
|
Solution +.# |
|
752 |
|
|
|
753 |
|
`SELECT COUNT(*) FROM BOOK;` |
|
754 |
|
|
|
755 |
|
|
|
756 |
|
Exercise +.# |
|
757 |
|
|
|
758 |
|
|
|
759 |
|
Consider the following `SQL` code: |
|
760 |
|
|
|
761 |
|
```sqlmysql |
|
762 |
|
CREATE TABLE COMPUTER( |
|
763 |
|
Id VARCHAR(20) PRIMARY KEY, |
|
764 |
|
Model VARCHAR(20) |
|
765 |
|
); |
|
766 |
|
CREATE TABLE PRINTER( |
|
767 |
|
Id VARCHAR(20) PRIMARY KEY, |
|
768 |
|
Model VARCHAR(20) |
|
769 |
|
); |
|
770 |
|
CREATE TABLE CONNEXION( |
|
771 |
|
Computer VARCHAR(20), |
|
772 |
|
Printer VARCHAR(20), |
|
773 |
|
PRIMARY KEY(Computer, Printer), |
|
774 |
|
FOREIGN KEY (Computer) REFERENCES COMPUTER(Id), |
|
775 |
|
FOREIGN KEY (Printer) REFERENCES PRINTER(Id) |
|
776 |
|
); |
|
777 |
|
|
|
778 |
|
INSERT INTO COMPUTER VALUES |
|
779 |
|
('A', 'DELL A'), |
|
780 |
|
('B', 'HP X'), |
|
781 |
|
('C', 'ZEPTO D'), |
|
782 |
|
('D', 'MAC Y'); |
|
783 |
|
INSERT INTO PRINTER VALUES |
|
784 |
|
('12', 'HP-140'), |
|
785 |
|
('13', 'HP-139'), |
|
786 |
|
('14', 'HP-140'), |
|
787 |
|
('15', 'HP-139'); |
|
788 |
|
INSERT INTO CONNEXION VALUES |
|
789 |
|
('A', '12'), |
|
790 |
|
('A', '13'), |
|
791 |
|
('B', '13'), |
|
792 |
|
('C', '14'); |
|
793 |
|
``` |
|
794 |
|
|
|
795 |
|
|
|
796 |
|
Write a query that returns … (in parenthesis, the values returned \emph{in this set-up}, but you have to be general) |
|
797 |
|
|
|
798 |
|
1. … the number of computers connected to the printer whose id is '13' (i.e., \(2\) ). |
|
799 |
|
2. … the number of different models of printers (i.e., \(2\)). |
|
800 |
|
3. … the model(s) of the printer connected to the computer whose id is 'A' (i.e., 'HP-140' and 'HP-139'). |
|
801 |
|
4. … the id of the computer(s) not connected to any printer (i.e., 'D'). |
|
802 |
|
|
|
803 |
|
|
|
804 |
|
|
|
805 |
|
Exercise +.# |
|
806 |
|
Write a query that returns the sum of all the values stored in the `Pages` attribute of a `BOOK` table. |
|
807 |
|
|
|
808 |
|
|
|
809 |
|
Solution +.# |
|
810 |
|
|
|
811 |
|
`SELECT SUM(Pages)FROM BOOK;` |
|
812 |
|
|
|
813 |
|
|
|
814 |
|
|
|
815 |
|
Exercise +.# |
|
816 |
|
Write a query that adds a `Pages` attribute of type `INT` into a (already existing) `BOOK` table. |
|
817 |
|
|
|
818 |
|
|
|
819 |
|
Solution +.# |
|
820 |
|
|
|
821 |
|
`ALTER TABLE BOOK ADD COLUMN Pages INT;` |
|
822 |
|
|
|
823 |
|
|
|
824 |
|
Exercise +.# |
|
825 |
|
|
|
826 |
|
Write a query that removes the default value for a `Pages` attribute in a `BOOK` table. |
|
827 |
|
|
|
828 |
|
|
|
829 |
|
Solution +.# |
|
830 |
|
|
|
831 |
|
`ALTER TABLE BOOK ALTER COLUMN Pages DROP DEFAULT;` |
|
832 |
|
|
|
833 |
|
Problem +.# |
|
834 |
|
|
|
835 |
|
Create the `PROF`, `STUDENT`, `DEPARTMENT` and `GRADE` tables as during the lecture. |
|
836 |
|
Populate them with some data. |
|
837 |
|
|
|
838 |
|
1. Create a `LECTURE` table as follows: |
|
839 |
|
\begin{itemize} |
|
840 |
|
\item It should have four attributes, `Name`, `Instructor`, `Code`, and \texttt{Year}, of type `VARCHAR(25)` for the two first, `YEAR(4)`, and `CHAR(5)`. |
|
841 |
|
\item The \texttt{Year} and `Code` attributes should be the primary key (yes, have \emph{two} attributes be the primary key). |
|
842 |
|
\item The `Instructor` attribute should be a foreign key referencing the `Login` attribute in `PROF`. |
|
843 |
|
\end{itemize} |
|
844 |
|
2. Populate the `LECTURE` table with some made-up data. |
|
845 |
|
3. Add two columns to the `GRADE` table, using |
|
846 |
|
```sqlmysql |
|
847 |
|
ALTER TABLE GRADE |
|
848 |
|
ADD COLUMN LectureCode CHAR(5), |
|
849 |
|
ADD COLUMN LectureYear YEAR(4); |
|
850 |
|
``` |
|
851 |
|
|
|
852 |
|
4. Use `DESCRIBE` and `SELECT` to observe the schema of the `GRADE` table and its rows (i.e., tuples). |
|
853 |
|
Is that what you would have expected? |
|
854 |
|
5. Add a foreign key in `GRADE`, using |
|
855 |
|
|
|
856 |
|
```sqlmysql |
|
857 |
|
ALTER TABLE GRADE |
|
858 |
|
ADD FOREIGN KEY (LectureYear, LectureCode) |
|
859 |
|
REFERENCES LECTURE(Year, Code); |
|
860 |
|
``` |
|
861 |
|
|
|
862 |
|
6. Update the tuples in `GRADE` with some made-up data: now, every row should contain, on top of a login and a grade, a lecture year and a lecture code. |
|
863 |
|
7. Write `SELECT` statements answering the following questions (where XXX and YY should be relevant values considering your data): |
|
864 |
|
1. "I taught class XXX in YYYY, could you give me the logins and grades of the students who took it?" |
|
865 |
|
2. "Could you list the instructors who taught in YYYY?" (and, please, avoid duplicates) |
|
866 |
|
3. "Could your list the name and grade of all the student who took class XXX (no matter the year)?" |
|
867 |
|
4. "Could you tell me which years was the class XXX taught?" |
|
868 |
|
5. "Could you list the classes taught the same year as class XXX?" |
|
869 |
|
6. "Could you print the name of the students who registered after Ava Alyx?" |
|
870 |
|
7. "How many departments' heads are teaching this year?" |
|
871 |
|
|
|
872 |
|
|
|
873 |
|
%Solution +.# |
|
874 |
|
|
|
875 |
|
```sqlmysql |
|
876 |
|
CREATE TABLE LECTURE( |
|
877 |
|
Name VARCHAR(25), |
|
878 |
|
Instructor VARCHAR(25), |
|
879 |
|
Year YEAR(4), |
|
880 |
|
Code CHAR(5), |
|
881 |
|
PRIMARY KEY(Year, Code), |
|
882 |
|
FOREIGN KEY (Instructor) REFERENCES PROF(Login) |
|
883 |
|
); |
|
884 |
|
|
|
885 |
|
INSERT INTO LECTURE VALUES |
|
886 |
|
('Intro to CS', 'caubert', 2017, '1304'), |
|
887 |
|
('Intro to Algebra', 'perdos', 2017, '1405'), |
|
888 |
|
('Intro to Cyber', 'aturing', 2017, '1234'); |
|
889 |
|
|
|
890 |
|
ALTER TABLE GRADE ADD COLUMN LectureCode CHAR(5), |
|
891 |
|
ADD COLUMN LectureYear YEAR(4); |
|
892 |
|
|
|
893 |
|
DESCRIBE GRADE; |
|
894 |
|
|
|
895 |
|
SELECT * FROM GRADE; |
|
896 |
|
|
|
897 |
|
ALTER TABLE GRADE ADD FOREIGN KEY (LectureYear, LectureCode) REFERENCES LECTURE(Year, Code); |
|
898 |
|
|
|
899 |
|
UPDATE GRADE SET LectureCode = '1304', LectureYear = 2017 WHERE Login = 'jrakesh' AND Grade = '2.85'; |
|
900 |
|
UPDATE GRADE SET LectureCode = '1405', LectureYear = 2017 WHERE Login = 'svlatka' OR (Login = 'jrakesh' AND Grade = '3.85'); |
|
901 |
|
UPDATE GRADE SET LectureCode = '1234', LectureYear = 2017 WHERE Login = 'aalyx' OR Login = 'cjoella'; |
|
902 |
|
|
|
903 |
|
SELECT Login, Grade FROM Grade WHERE Lecturecode='1304' and LectureYear = '2017'; |
|
904 |
|
|
|
905 |
|
SELECT DISTINCT Instructor FROM LECTURE WHERE year = 2017; |
|
906 |
|
|
|
907 |
|
SELECT Name, Grade FROM STUDENT, GRADE WHERE GRADE.LectureCode = 1405 AND STUDENT.Login = GRADE.Login; |
|
908 |
|
|
|
909 |
|
SELECT Year FROM Lecture WHERE Code = '1234'; |
|
910 |
|
|
|
911 |
|
SELECT Name FROM LECTURE WHERE Year IN (SELECT Year FROM LECTURE WHERE CODE = '1234'); |
|
912 |
|
|
|
913 |
|
SELECT B.name FROM STUDENT AS A, STUDENT AS B WHERE A.Name = 'Ava Alyx' AND A.Registered > B.Registered; |
|
914 |
|
|
|
915 |
|
SELECT COUNT(DISTINCT PROF.Name) AS 'Head Teaching This Year' FROM LECTURE, DEPARTMENT, PROF WHERE Year = 2017 AND Instructor = Head AND Head = PROF.Login; |
|
916 |
|
``` |
|
917 |
|
|
|
918 |
|
|
|
919 |
|
|
|
920 |
|
|
|
921 |
|
|
|
922 |
|
|
|
923 |
|
|
|
924 |
|
|
|
925 |
|
<!-- |
|
926 |
|
OK TATA UP TO HERE |
|
927 |
|
--> |
|
928 |
|
|
|
929 |
|
|
|
930 |
|
%%%%% 04 |
|
931 |
|
|
|
932 |
|
|
|
933 |
|
Exercise +.# |
|
934 |
|
|
|
935 |
|
What could be the decomposition of an attribute used to store an email address? When could that be useful? |
|
936 |
|
|
|
937 |
|
|
|
938 |
|
Solution +.# |
|
939 |
|
|
|
940 |
|
Name / extension. |
|
941 |
|
To have statistics about the extensions, to sort the username by length, etc. |
|
942 |
|
|
|
943 |
|
|
|
944 |
|
Exercise +.# |
|
945 |
|
|
|
946 |
|
Draw the ER diagram for a "COMPUTER" entity that has one multivalued attribute "Operating\_System", a composite attribute "Devices" (decomposed into "Keyboard" and "Mouse") and an "Id" key attribute. |
|
947 |
|
|
|
948 |
|
|
|
949 |
|
Solution +.# |
|
950 |
|
|
|
951 |
|
\begin{tikzpicture}[node distance=7em] |
|
952 |
|
\node[entity] (computer) {COMPUTER}; |
|
953 |
|
\node[attribute] (pid) [left of=computer] {\key{Id}} edge (computer); |
|
954 |
|
\node[multi attribute] (os) [above of=computer] {Operating\_System} edge (computer); |
|
955 |
|
\node[attribute] (devices) [right of=computer] {Devices} edge (computer); |
|
956 |
|
\node[attribute] (mouse) [above right of=devices] {Mouse} edge (devices); |
|
957 |
|
\node[attribute] (keyboard) [right of=devices] {Keyboard} edge (devices); |
|
958 |
|
\end{tikzpicture} |
|
959 |
|
|
|
960 |
|
|
|
961 |
|
Exercise +.# |
|
962 |
|
|
|
963 |
|
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. |
|
964 |
|
|
|
965 |
|
|
|
966 |
|
Solution +.# |
|
967 |
|
|
|
968 |
|
\begin{tikzpicture}[node distance=7em] |
|
969 |
|
\node[entity] (cellphone) {CELLPHONE}; |
|
970 |
|
\node[attribute] (mid) [left of=cellphone] {\key{MIN}} edge (cellphone); |
|
971 |
|
\node[multi attribute] (app) [above of=cellphone] {App\_Installed} edge (cellphone); |
|
972 |
|
\node[attribute] (plan) [right of=cellphone] {Plan} edge (cellphone); |
|
973 |
|
\node[attribute] (carrier) [above right of=plan] {Carrier} edge (plan); |
|
974 |
|
\node[attribute] (price) [right of=plan] {Price} edge (plan); |
|
975 |
|
\end{tikzpicture} |
|
976 |
|
|
|
977 |
|
|
|
978 |
|
|
|
979 |
|
Exercise +.# |
|
980 |
|
|
|
981 |
|
Name one difference between a primary key in the relational model, and a key attribute in the ER model. |
|
982 |
|
|
|
983 |
|
|
|
984 |
|
Solution +.# |
|
985 |
|
|
|
986 |
|
There can be more than one key in the ER model. |
|
987 |
|
|
|
988 |
|
|
|
989 |
|
Exercise +.# |
|
990 |
|
|
|
991 |
|
What is the difference between an entity type and a weak entity type? |
|
992 |
|
|
|
993 |
|
|
|
994 |
|
Solution +.# |
|
995 |
|
|
|
996 |
|
The weak entity type doesn't have a primary key. |
|
997 |
|
|
|
998 |
|
|
|
999 |
|
Exercise +.# |
|
1000 |
|
|
|
1001 |
|
What is the degree of a relationship type? |
|
1002 |
|
|
|
1003 |
|
|
|
1004 |
|
Solution +.# |
|
1005 |
|
|
|
1006 |
|
The number of participating entity types. |
|
1007 |
|
|
|
1008 |
|
|
|
1009 |
|
Exercise +.# |
|
1010 |
|
|
|
1011 |
|
What is a self-referencing, or recursive, relationship type? Give two examples. |
|
1012 |
|
|
|
1013 |
|
|
|
1014 |
|
Solution +.# |
|
1015 |
|
|
|
1016 |
|
A relationship type where the same entity type participates more than once. |
|
1017 |
|
On rooms, "is to the left of", on persons, "is married to". |
|
1018 |
|
|
|
1019 |
|
|
|
1020 |
|
Exercise +.# |
|
1021 |
|
|
|
1022 |
|
What does it mean for a binary relationship type "Owner" between entity types "Person" and "Computer" to have a cardinality ration $M:N$? |
|
1023 |
|
|
|
1024 |
|
|
|
1025 |
|
Solution +.# |
|
1026 |
|
|
|
1027 |
|
That a person can own multiple computers, and that a computer can have multiple owners. |
|
1028 |
|
|
|
1029 |
|
|
|
1030 |
|
Exercise +.# |
|
1031 |
|
|
|
1032 |
|
What are the two possible structural constraints on a relationship type? |
|
1033 |
|
|
|
1034 |
|
|
|
1035 |
|
Solution +.# |
|
1036 |
|
|
|
1037 |
|
Cardinality ration and participation constraints. |
|
1038 |
|
|
|
1039 |
|
|
|
1040 |
|
Exercise +.# |
|
1041 |
|
|
|
1042 |
|
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? |
|
1043 |
|
|
|
1044 |
|
|
|
1045 |
|
Solution +.# |
|
1046 |
|
|
|
1047 |
|
When the cardinality is $1 : N$, $1 : 1$ or $N : 1$. |
|
1048 |
|
|
|
1049 |
|
|
|
1050 |
|
Exercise +.# |
|
1051 |
|
|
|
1052 |
|
What is a partial key? |
|
1053 |
|
|
|
1054 |
|
|
|
1055 |
|
Solution +.# |
|
1056 |
|
|
|
1057 |
|
For a weak entity attribute, it is the attribute that can uniquely identify weak entites that are related to the same owner entity. |
|
1058 |
|
|
|
1059 |
|
|
|
1060 |
|
Exercise +.# |
|
1061 |
|
|
|
1062 |
|
For the following binary relationships, suggest cardinality ratios based on the common-sense meaning of the entity types. |
|
1063 |
|
|
|
1064 |
|
{\centering |
|
1065 |
|
|
|
1066 |
|
\begin{tabular}{l c l} |
|
1067 |
|
\textbf{Entity 1} & \textbf{Cardinality Ratio} & \textbf{Entity 2}\\ |
|
1068 |
|
STUDENT & & MAJOR\\ % 1 : N |
|
1069 |
|
CAR & & TAG \\ % 1 : 1 |
|
1070 |
|
INSTRUCTOR & & LECTURE \\ % N :1 |
|
1071 |
|
INSTRUCTOR & & OFFICE \\ % 1 : N |
|
1072 |
|
COMPUTER & & OPERATING\_SYSTEM % N : M |
|
1073 |
|
\end{tabular} |
|
1074 |
|
|
|
1075 |
|
} |
|
1076 |
|
|
|
1077 |
|
|
|
1078 |
|
Solution +.# |
|
1079 |
|
|
|
1080 |
|
N:1 , 1 : 1, 1 : N , 1:N, M : N |
|
1081 |
|
|
|
1082 |
|
|
|
1083 |
|
Exercise +.# |
|
1084 |
|
|
|
1085 |
|
Give an example of a binary relationship type of cardinality $1 : N$. |
|
1086 |
|
|
|
1087 |
|
|
|
1088 |
|
Solution +.# |
|
1089 |
|
|
|
1090 |
|
SUPERVISION, a recursive relationship on EMPLOYEE. |
|
1091 |
|
|
|
1092 |
|
|
|
1093 |
|
Exercise +.# |
|
1094 |
|
|
|
1095 |
|
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). |
|
1096 |
|
|
|
1097 |
|
|
|
1098 |
|
Solution +.# |
|
1099 |
|
|
|
1100 |
|
\begin{tikzpicture}[node distance=7em] |
|
1101 |
|
\node[relationship] (contains){BELONGS}; |
|
1102 |
|
\node[entity] (burger) [left = 1cm of contains] {HAND} edge node[above] {$N$} (contains); |
|
1103 |
|
\node[entity] (ingredient) [right = 1cm of contains] {PERSON} edge node[above]{$1$} (contains); |
|
1104 |
|
\end{tikzpicture} |
|
1105 |
|
|
|
1106 |
|
|
|
1107 |
|
|
|
1108 |
|
Exercise +.# |
|
1109 |
|
|
|
1110 |
|
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). |
|
1111 |
|
|
|
1112 |
|
|
|
1113 |
|
Solution +.# |
|
1114 |
|
|
|
1115 |
|
|
|
1116 |
|
\begin{tikzpicture}[node distance=7em] |
|
1117 |
|
\node[entity] (person) {PERSON}; |
|
1118 |
|
\node[attribute] (ssn) [left of=person] {\key{SSN}} edge (person); |
|
1119 |
|
\node[attribute] (dob) [above of=person] {Date\_Of\_Birth} edge (person); |
|
1120 |
|
\node[derived attribute] (age) [right of=person] {Age} edge (person); |
|
1121 |
|
\end{tikzpicture} |
|
1122 |
|
|
|
1123 |
|
|
|
1124 |
|
|
|
1125 |
|
Exercise +.# |
|
1126 |
|
|
|
1127 |
|
Draw an ER diagram expressing the total participation of an entity type "BURGER" in a binary relation "CONTAINS" between "BURGER" and "INGREDIENT". |
|
1128 |
|
What would be the ratio of such a relation? |
|
1129 |
|
|
|
1130 |
|
|
|
1131 |
|
Solution +.# |
|
1132 |
|
|
|
1133 |
|
\begin{tikzpicture}[node distance=7em] |
|
1134 |
|
\node[relationship] (contains){CONTAINS}; |
|
1135 |
|
\node[entity] (burger) [left = 1cm of contains] {BURGER} edge[total] node[above] {$N$} (contains); |
|
1136 |
|
\node[entity] (ingredient) [right = 1cm of contains] {INGREDIENT} edge node[above]{$M$} (contains); |
|
1137 |
|
\end{tikzpicture} |
|
1138 |
|
|
|
1139 |
|
|
|
1140 |
|
Exercise +.# |
|
1141 |
|
|
|
1142 |
|
Convert the following ER diagram into a relational model: |
|
1143 |
|
|
|
1144 |
|
\begin{tikzpicture}[node distance=7em] |
|
1145 |
|
\node[relationship] (contains){STAYS\_AT}; |
|
1146 |
|
\node[entity] (person) [above = 1cm of contains] {PERSON} edge node[right] {$N$} (contains);; |
|
1147 |
|
\node[attribute] (ssn) [left of=person] {\key{SSN}} edge (person); |
|
1148 |
|
\node[attribute] (dob) [above = .5cm of person] {Date\_Of\_Birth} edge (person); |
|
1149 |
|
\node[entity] (burger) [below of= contains] {PLACE} edge node[right] {$1$} (contains); |
|
1150 |
|
\node[attribute] (dob) [below = .5cm of burger] {\key{Address}} edge (burger); |
|
1151 |
|
\node[attribute] (ssn) [left of=burger] {Rooms} edge (burger); |
|
1152 |
|
\end{tikzpicture} |
|
1153 |
|
|
|
1154 |
|
|
|
1155 |
|
Solution +.# |
|
1156 |
|
|
|
1157 |
|
\begin{tikzpicture}[node distance=7em] |
|
1158 |
|
\node[relationship] (contains){CONTAINS}; |
|
1159 |
|
\node[entity] (burger) [left = 1cm of contains] {BURGER} edge[total] node[above] {$N$} (contains); |
|
1160 |
|
\node[entity] (ingredient) [right = 1cm of contains] {INGREDIENT} edge node[above]{$M$} (contains); |
|
1161 |
|
\end{tikzpicture} |
|
1162 |
|
|
|
1163 |
|
|
|
1164 |
|
Exercise +.# |
|
1165 |
|
|
|
1166 |
|
Why do weak entity type have a total participation constraint? |
|
1167 |
|
|
|
1168 |
|
|
|
1169 |
|
Solution +.# |
|
1170 |
|
|
|
1171 |
|
Otherwise, we couldn't identify entities in it without owner entity. |
|
1172 |
|
|
|
1173 |
|
Problem +.# |
|
1174 |
|
|
|
1175 |
|
|
|
1176 |
|
Consider the ER schema for the MOVIES database in Figure~\ref{fig:movies}. |
|
1177 |
|
|
|
1178 |
|
\begin{figure} |
|
1179 |
|
\begin{tikzpicture} |
|
1180 |
|
\node (a) at (0,0) {\includegraphics[page=1,width=1\textwidth]{img/te.pdf}}; |
|
1181 |
|
\fill[white] (-8.3, 6.5) rectangle (-3.5, 4); |
|
1182 |
|
\end{tikzpicture} |
|
1183 |
|
\caption{An ER diagram for a MOVIES database schema.} \label{fig:movies} |
|
1184 |
|
\end{figure} |
|
1185 |
|
|
|
1186 |
|
Assume that MOVIES is a populated database. |
|
1187 |
|
ACTOR is used as a gender-netral term. |
|
1188 |
|
Given the constraints shown in the ER schema, respond to the following statements with \emph{True} or \emph{False}. |
|
1189 |
|
Justify each answer. |
|
1190 |
|
|
|
1191 |
|
\begin{enumerate} |
|
1192 |
|
\item There are no actors in this database that have been in no movies. |
|
1193 |
|
\item There might be actors who have acted in more than ten movies. |
|
1194 |
|
\item Some actors could have done a lead role in multiple movies. |
|
1195 |
|
\item A movie can have only a maximum of two lead actors. |
|
1196 |
|
\item Every director have to have been an actor in some movie. |
|
1197 |
|
\item No producer has ever been an actor. |
|
1198 |
|
\item A producer cannot be an actor in some other movie. |
|
1199 |
|
\item There could be movies with more than a dozen actors. |
|
1200 |
|
\item Producers can be directors as well. |
|
1201 |
|
\item A movie can have one director and one producer. |
|
1202 |
|
\item A movie can have one director and several producers. |
|
1203 |
|
\item There could be some actors who have done a lead role, directed a movie, and produced some movie. |
|
1204 |
|
\item It is impossible for a director to play in the movie (s)he directed. |
|
1205 |
|
\end{enumerate} |
|
1206 |
|
|
|
1207 |
|
|
|
1208 |
|
|
|
1209 |
|
Solution +.# |
|
1210 |
|
|
|
1211 |
|
\begin{enumerate} |
|
1212 |
|
\item true |
|
1213 |
|
\item true |
|
1214 |
|
\item true |
|
1215 |
|
\item true |
|
1216 |
|
\item false |
|
1217 |
|
\item false |
|
1218 |
|
\item false |
|
1219 |
|
\item true |
|
1220 |
|
\item true |
|
1221 |
|
\item true |
|
1222 |
|
\item true |
|
1223 |
|
\item true |
|
1224 |
|
\item false |
|
1225 |
|
\end{enumerate} |
|
1226 |
|
|
|
1227 |
|
|
|
1228 |
|
Problem +.# |
|
1229 |
|
|
|
1230 |
|
Draw the ER diagram for the following situation: |
|
1231 |
|
A car-insurance company wants to have a database of accidents. |
|
1232 |
|
An accident involves cars, drivers, and it has several aspects: the moment and place where it took place, the amount of damages, and a (unique) report number. |
|
1233 |
|
A car has a license, a model, a year, and an owner. |
|
1234 |
|
A driver has an id, an age, a name, and an address. |
|
1235 |
|
|
|
1236 |
|
One of the interesting choice is: should "accident" be an entity type or a relationship type? |
|
1237 |
|
|
|
1238 |
|
Solution +.# |
|
1239 |
|
|
|
1240 |
|
\includegraphics[page=1,width=1\textwidth]{img/p.pdf} |
|
1241 |
|
|
|
1242 |
|
OR |
|
1243 |
|
|
|
1244 |
|
|
|
1245 |
|
|
|
1246 |
|
\begin{tikzpicture}[node distance=7em] |
|
1247 |
|
\node[entity] (person) {Person}; |
|
1248 |
|
\node[attribute] (pid) [left of=person] {\key{id}} edge (person); |
|
1249 |
|
\node[attribute] (name) [above left of=person] {Name} edge (person); |
|
1250 |
|
\node[attribute] (address) [below left of=person] {address} edge (person); |
|
1251 |
|
|
|
1252 |
|
\node[relationship] (owns) [above right of=person] {Owns} edge node[above, sloped]{$M$} (person); |
|
1253 |
|
\node[entity] (car) [above right of=owns] {Car} edge node[above, sloped]{$N$} (owns); |
|
1254 |
|
\node[attribute] (licence) [above left of=car] {\key{licence}} edge (car); |
|
1255 |
|
\node[attribute] (model) [above of =car] {model} edge (car); |
|
1256 |
|
\node[attribute] (year) [above right of =car] {year} edge (car); |
|
1257 |
|
|
|
1258 |
|
\node[relationship] (accident) [right = 5cm of person] {Accident} edge[total] node[above, sloped]{$M$} (person); |
|
1259 |
|
\node[attribute] (report_number) [below of = accident] {\key{Report Number}} edge (accident); |
|
1260 |
|
\node[attribute] (time) [right of = accident] {Time} edge (accident); |
|
1261 |
|
\node[attribute] (place) [below right of = accident] {Place} edge (accident); |
|
1262 |
|
\node[attribute] (damage_amount) [above right of = accident] {Damage\_Amount} edge (accident); |
|
1263 |
|
|
|
1264 |
|
\draw (car) edge[total] node[above, sloped]{$N$} (accident); |
|
1265 |
|
\end{tikzpicture} |
|
1266 |
|
|
|
1267 |
|
|
|
1268 |
|
|
|
1269 |
|
Problem +.# |
|
1270 |
|
|
|
1271 |
|
|
|
1272 |
|
Apply the ER-to-Relation mapping to your ER diagram from the previous problem. |
|
1273 |
|
|
|
1274 |
|
|
|
1275 |
|
%%% 05 |
|
1276 |
|
|
|
1277 |
|
|
|
1278 |
|
Exercise +.# |
|
1279 |
|
|
|
1280 |
|
What is insertion anomaly? Give an example. |
|
1281 |
|
|
|
1282 |
|
|
|
1283 |
|
Solution +.# |
|
1284 |
|
|
|
1285 |
|
|
|
1286 |
|
When you have to invent a primary key or add a lot of `NULL` value to be able to add a tuple. |
|
1287 |
|
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. |
|
1288 |
|
|
|
1289 |
|
|
|
1290 |
|
Exercise +.# |
|
1291 |
|
|
|
1292 |
|
Why should we avoid attributes whose value will often be `NULL`? |
|
1293 |
|
Can the usage of `NULL` be completely avoided? |
|
1294 |
|
|
|
1295 |
|
|
|
1296 |
|
Solution +.# |
|
1297 |
|
|
|
1298 |
|
Because they waste space, and because they are ambiguous (N/A, or unknown, or not communicated?). |
|
1299 |
|
No, it is necessary sometimes. |
|
1300 |
|
|
|
1301 |
|
|
|
1302 |
|
Exercise +.# |
|
1303 |
|
|
|
1304 |
|
Consider the following relation: |
|
1305 |
|
|
|
1306 |
|
\begin{tabular}{p{3em} l} |
|
1307 |
|
& PROF(\underline{SSN}, Name, Department, Bike\_brand) |
|
1308 |
|
\end{tabular} |
|
1309 |
|
|
|
1310 |
|
Why is it a poor design to have a "Bike\_brand" attribute in such a relation? |
|
1311 |
|
How should we store this information? |
|
1312 |
|
|
|
1313 |
|
|
|
1314 |
|
Solution +.# |
|
1315 |
|
|
|
1316 |
|
Because it will be `NULL` most of the time. |
|
1317 |
|
In a separate table. |
|
1318 |
|
|
|
1319 |
|
|
|
1320 |
|
Exercise +.# |
|
1321 |
|
|
|
1322 |
|
Consider the following relation: |
|
1323 |
|
|
|
1324 |
|
\begin{tabular}{p{3em} l} |
|
1325 |
|
& STUDENT(\underline{SSN}, Name, \(\hdots\), Sibling\_On\_Campus) |
|
1326 |
|
\end{tabular} |
|
1327 |
|
|
|
1328 |
|
Why is it a poor design to have a "Sibling\_On\_Campus" attribute in such a relation? |
|
1329 |
|
How should we store this information? |
|
1330 |
|
|
|
1331 |
|
|
|
1332 |
|
Solution +.# |
|
1333 |
|
|
|
1334 |
|
Because it will be `NULL` most of the time, and because students could have more than one sibling on campus. |
|
1335 |
|
In a separate table. |
|
1336 |
|
|
|
1337 |
|
|
|
1338 |
|
Exercise +.# |
|
1339 |
|
|
|
1340 |
|
Consider the following relational database schema: |
|
1341 |
|
|
|
1342 |
|
\begin{tabular}{p{3em} l} |
|
1343 |
|
& STUDENT(\underline{Login}, Name, \(\hdots\), Major, Major\_Head)\\ |
|
1344 |
|
& DEPARTMENT(\underline{Code}, Name, Major\_Head) |
|
1345 |
|
\end{tabular} |
|
1346 |
|
|
|
1347 |
|
Assuming that "Major" is a foreign key referencing "DEPARTMENT.Code", what is the problem with that schema? How could you address it? |
|
1348 |
|
|
|
1349 |
|
|
|
1350 |
|
Solution +.# |
|
1351 |
|
|
|
1352 |
|
Major\_Head will give update anomalies. |
|
1353 |
|
By putting the Head of the department in the DEPARTMENT relation only, i.e., removing it from STUDENT. |
|
1354 |
|
|
|
1355 |
|
|
|
1356 |
|
Exercise +.# |
|
1357 |
|
|
|
1358 |
|
Consider the relation \(R(A , B, C, D, E, F)\) and the following functional dependencies: |
|
1359 |
|
\begin{enumerate} |
|
1360 |
|
\item \(F \to \{D, C\}, D \to \{B, E\}, \{B, E\} \to A\) |
|
1361 |
|
\item \(\{A, B\} \to \{C, D\}, \{B, E\} \to F\) |
|
1362 |
|
\item \(A \to \{C, D\}, E \to F, D \to B\) |
|
1363 |
|
\end{enumerate} |
|
1364 |
|
For each set of functional dependency, give a key for \(R\). We want a key, so it has to be minimal. |
|
1365 |
|
|
|
1366 |
|
|
|
1367 |
|
Solution +.# |
|
1368 |
|
|
|
1369 |
|
\begin{enumerate} |
|
1370 |
|
\item \(F\) |
|
1371 |
|
\item \(\{A, B, E\}\) |
|
1372 |
|
\item \(\{A, E\}\) |
|
1373 |
|
\end{enumerate} |
|
1374 |
|
|
|
1375 |
|
|
|
1376 |
|
\begin{exercise}[tags={none}] |
|
1377 |
|
Consider the relation \(R(A, B, C, D, E, F)\) and the following functional dependencies: |
|
1378 |
|
\[ A \to \{D, E\}, D \to \{B, F\}, \{B, E\} \to A, \{A,C\} \to \{B, D, F\}, A \to F\] |
|
1379 |
|
Answer the following: |
|
1380 |
|
\begin{enumerate} |
|
1381 |
|
\item How many candidate keys is there? List them. |
|
1382 |
|
\item How many transitive dependencies can you find? Give them and justify them. |
|
1383 |
|
\end{enumerate} |
|
1384 |
|
|
|
1385 |
|
|
|
1386 |
|
Solution +.# |
|
1387 |
|
|
|
1388 |
|
\begin{enumerate} |
|
1389 |
|
\item \(\{A, C\}\), |
|
1390 |
|
\item \(A \to F\) by \(A \to D, D\to F\). |
|
1391 |
|
\end{enumerate} |
|
1392 |
|
|
|
1393 |
|
|
|
1394 |
|
Exercise +.# |
|
1395 |
|
|
|
1396 |
|
Consider the relation \(R(A, B, C, D)\) and answer the following: |
|
1397 |
|
\begin{enumerate} |
|
1398 |
|
\item 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. |
|
1399 |
|
\item 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. |
|
1400 |
|
\end{enumerate} |
|
1401 |
|
|
|
1402 |
|
|
|
1403 |
|
Solution +.# |
|
1404 |
|
|
|
1405 |
|
\(1.\) Yes. \(C\) and \(D\) are non prime, and they fully depend on \(\{A,B\}\). |
|
1406 |
|
|
|
1407 |
|
\(2.\) No. \(D\) is the only non prime, and it depends only on \(A\). |
|
1408 |
|
|
|
1409 |
|
|
|
1410 |
|
Exercise +.# |
|
1411 |
|
|
|
1412 |
|
Consider the relation \(R(A, B, C, D, E, F)\) with candidate keys \(\{A, B\}\) and \(C\). Answer the following: |
|
1413 |
|
\begin{enumerate} |
|
1414 |
|
\item What are the prime attributes in \(R\)? |
|
1415 |
|
\item Is \(\{C,D\} \to E\) a fully functional dependency? |
|
1416 |
|
\item Write a set of functional dependencies containing at least one transitive depency, and justify your answer. |
|
1417 |
|
\end{enumerate} |
|
1418 |
|
|
|
1419 |
|
|
|
1420 |
|
Solution +.# |
|
1421 |
|
|
|
1422 |
|
\begin{enumerate} |
|
1423 |
|
\item\(A,B,C\) |
|
1424 |
|
\item No, because we can remove \(D\), |
|
1425 |
|
\item \(A \to D\), \(D \to E\) and \(A \to E\) |
|
1426 |
|
\end{enumerate} |
|
1427 |
|
|
|
1428 |
|
|
|
1429 |
|
Exercise +.# |
|
1430 |
|
|
|
1431 |
|
Consider the relation \(R(A , B, C, D, E)\) and the following functional dependencies: |
|
1432 |
|
\begin{enumerate} |
|
1433 |
|
\item \(C \to D, \{C, B\} \to A, A \to \{B, C, D\}, B \to E\) |
|
1434 |
|
\item \(\ A \to \{C, D\}, C \to B, D \to E, \{E, C\} \to A\) |
|
1435 |
|
%\item \(\{A, B\} \to D, D \to \{B, C\}, E \to C\) |
|
1436 |
|
\end{enumerate} |
|
1437 |
|
For each one, give one candidate key for \(R\).%two candidate keys for \(R\). |
|
1438 |
|
|
|
1439 |
|
|
|
1440 |
|
Solution +.# |
|
1441 |
|
|
|
1442 |
|
\begin{enumerate} |
|
1443 |
|
\item \(\{B, C\}\), \(A\) |
|
1444 |
|
\item \(A\), \(\{C,E\}\), |
|
1445 |
|
\item \(\{A, D, E\}\), \(\{A, B, E\}\) |
|
1446 |
|
\end{enumerate} |
|
1447 |
|
|
|
1448 |
|
|
|
1449 |
|
Exercise +.# |
|
1450 |
|
|
|
1451 |
|
Consider the relation \(R(A, B, C, D, E)\) and answer the following: |
|
1452 |
|
\begin{enumerate}[topsep=0pt,itemsep=10em,partopsep=1ex,parsep=1ex] |
|
1453 |
|
\item If \(\{A, B\}\) is the primary key, is \(B \to E, C \to D\) a 2NF?% List the nonprime attributes and justify. |
|
1454 |
|
\item If \(\{A\}\) is the primary key, is \(B \to C, B \to D\) a 2NF?% List the nonprime attributes and justify. |
|
1455 |
|
\end{enumerate} |
|
1456 |
|
|
|
1457 |
|
|
|
1458 |
|
Solution +.# |
|
1459 |
|
|
|
1460 |
|
\(1.\) No. \(C, D, E\), and \(E\) has a partial relation to \(B\) |
|
1461 |
|
|
|
1462 |
|
\(2.\) Yes. Since the primary key is a singleton, it is obvious. |
|
1463 |
|
|
|
1464 |
|
|
|
1465 |
|
Exercise +.# |
|
1466 |
|
|
|
1467 |
|
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\). |
|
1468 |
|
This relation is not in 3NF, can you tell why?% List the nonprime attributes and justify. |
|
1469 |
|
%\item If \(A\) is the primary key, is \(\{A, B\} \to C, \{A, B, C\} \to E\) a 3NF?% List the nonprime attributes and justify. |
|
1470 |
|
\end{enumerate} |
|
1471 |
|
|
|
1472 |
|
|
|
1473 |
|
Solution +.# |
|
1474 |
|
|
|
1475 |
|
\(1.\) No. \(A, C, E, F\) are non-prime, \(\{B, D\} \to C \to F\) breaks the 3NF. |
|
1476 |
|
|
|
1477 |
|
\(2.\) Yes. \(B, C, E, F\), \(\{A,B\}\) and \(\{A, B, C\}\) are not strict subset of the primary key. |
|
1478 |
|
|
|
1479 |
|
|
|
1480 |
|
Exercise +.# |
|
1481 |
|
|
|
1482 |
|
Consider the relation \(R(A, B, C, D)\) and answer the following: |
|
1483 |
|
\begin{enumerate} |
|
1484 |
|
\item 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. |
|
1485 |
|
\item 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. |
|
1486 |
|
\end{enumerate} |
|
1487 |
|
|
|
1488 |
|
|
|
1489 |
|
Solution +.# |
|
1490 |
|
|
|
1491 |
|
\(1.\) No. \(B\), \(C\) and \(D\) are non prime, \(A \to \{B,C\} \to D\) breaks the 3NF. |
|
1492 |
|
|
|
1493 |
|
\(2.\) No. \(A\), \(B\) and \(D\) are non prime, \(B \to \{A,C\} \to D\) breaks the 3NF. |
|
1494 |
|
|
|
1495 |
|
|
|
1496 |
|
\begin{problem}[tags={none}] |
|
1497 |
|
Consider the following relation, and its functional dependencies: |
|
1498 |
|
|
|
1499 |
|
\begin{tabular}{l} |
|
1500 |
|
CAR\_SALE(\underline{Car\_no}, Date\_sold, \underline{Salesman\_no}, Commission, Discount\_amt) |
|
1501 |
|
\end{tabular} |
|
1502 |
|
|
|
1503 |
|
\begin{tabular}{rcl} |
|
1504 |
|
\{Car\_no, Salesman\_no\} & \(\to\)& \{Date\_sold, Commission, Discount\_amt\}\\ |
|
1505 |
|
Date\_sold & \(\to\)& Discount\_amt\\ |
|
1506 |
|
Salesman\_no & \(\to\)& Commission |
|
1507 |
|
\end{tabular} |
|
1508 |
|
|
|
1509 |
|
Based on the given primary key, is this relation in 1NF, 2NF, or 3NF? Why or why not? Normalize it completely. Then draw an ER diagram for the resulting schema. |
|
1510 |
|
|
|
1511 |
|
|
|
1512 |
|
Solution +.# |
|
1513 |
|
|
|
1514 |
|
This relation satisfies 1NF but not 2NF (Car\_no \(\to\) Date\_sold and Salesman\_no \(\to\) Commission so these two attributes are not fully functional dependent on the primary key) and not 3NF. |
|
1515 |
|
|
|
1516 |
|
To normalize, |
|
1517 |
|
|
|
1518 |
|
2NF: |
|
1519 |
|
\begin{tabular}{l} |
|
1520 |
|
Car\_Sale1(Car\_no, Date\_sold, Discount\_amt)\\ |
|
1521 |
|
Car\_Sale2(Car\_no, Salesman\_no)\\ |
|
1522 |
|
Car\_Sale3(Salesman\_no,Commission) |
|
1523 |
|
\end{tabular} |
|
1524 |
|
3NF: |
|
1525 |
|
\begin{tabular}{c} |
|
1526 |
|
Car\_Sale1-1(Car\_no, Date\_sold)\\ |
|
1527 |
|
Car\_Sale1-2(Date\_sold, Discount\_amt)\\ |
|
1528 |
|
Car\_Sale2(Car\_no, Salesman\_no)\\ |
|
1529 |
|
Car\_Sale3(Salesman\_no,Commission) |
|
1530 |
|
\end{tabular} |
|
1531 |
|
|
|
1532 |
|
|
|
1533 |
|
Problem +.# |
|
1534 |
|
|
|
1535 |
|
This problem asks you to convert business statements into dependencies. |
|
1536 |
|
Consider the following relation: |
|
1537 |
|
\begin{center} |
|
1538 |
|
BIKE(Serial\_no, Manufacturer, Model, Batch, Wheel\_size, Retailer) |
|
1539 |
|
\end{center} |
|
1540 |
|
|
|
1541 |
|
Each tuple in the relation BIKE contains information about a bike with a serial number, made by a manufacturer, with a particular model number, released in a certain batch, which has a certain wheel size, and is sold by a certain retailer. |
|
1542 |
|
\begin{itemize} |
|
1543 |
|
\item Write each of the following dependencies as a functional dependency (I give you the first one as an example): |
|
1544 |
|
|
|
1545 |
|
\begin{enumerate} |
|
1546 |
|
\item A retailer can't have two bikes of the same model from different batches. |
|
1547 |
|
|
|
1548 |
|
\emph{solution:} \{Retailer, Model\} \(\to\) Batch |
|
1549 |
|
\item The manufacturer and serial number uniquely identifies the bike and where it is sold. |
|
1550 |
|
\item A model number is registered by a manufacturer and therefore can't be |
|
1551 |
|
used by another manufacturer. |
|
1552 |
|
\item All bikes in a particular batch are of the same model. |
|
1553 |
|
\item All bikes of a certain model have |
|
1554 |
|
the same wheel size. |
|
1555 |
|
\end{enumerate} |
|
1556 |
|
\item Based on those statements, what could be a key for this relation? |
|
1557 |
|
\item Assuming all those functional dependencies hold, and taking the primary key you identified at the previous step, what is the degree of normality of this relation? Justify your answer. |
|
1558 |
|
\end{itemize} |
|
1559 |
|
|
|
1560 |
|
|
|
1561 |
|
Solution +.# |
|
1562 |
|
|
|
1563 |
|
\begin{itemize} |
|
1564 |
|
\item |
|
1565 |
|
\begin{enumerate} |
|
1566 |
|
\item \{ Manufacturer, Serial\_no \} \(\to\) \{ Model, Batch, Wheel\_size, Retailer\} |
|
1567 |
|
\item Model \(\to\) Manufacturer |
|
1568 |
|
\item Batch \(\to\) Model |
|
1569 |
|
\item \{Model, Manufacturer\} \(\to\) Wheel\_size |
|
1570 |
|
\end{enumerate} |
|
1571 |
|
\item \{Manufacturer, Serial\_no \} |
|
1572 |
|
\item If every attribute is atomic, it is in second nf. |
|
1573 |
|
\{ Manufacturer, Serial\_no \} \(\to\) Batch \(\to\)Model breaks the 3NF. |
|
1574 |
|
|
|
1575 |
|
\end{itemize} |
|
1576 |
|
|
|
1577 |
|
|
|
1578 |
|
|
|
1579 |
|
Problem +.# |
|
1580 |
|
|
|
1581 |
|
Consider the relations \(R\) and \(T\) below, and their functional dependencies (on top of the one induced by the primary keys): |
|
1582 |
|
|
|
1583 |
|
\begin{tabular}{l} |
|
1584 |
|
R(\underline{EventId}, \underline{Email}, Time, Date, Location, Status)\\ |
|
1585 |
|
T(\underline{Invno}, Subtotal, Tax, Total, Email, Lname, Fname, Phone) |
|
1586 |
|
\end{tabular} |
|
1587 |
|
|
|
1588 |
|
\begin{tabular}{r c l} |
|
1589 |
|
\{EventId, Email\} & \(\to\) & Status\\ |
|
1590 |
|
EventId & \(\to\) & \{Time, Date, Location\}\\ |
|
1591 |
|
Invno & \(\to\) & \{Subtotal, Tax, Total, Email\}\\ |
|
1592 |
|
Email & \(\to\) & \{Fname, Lname, Phone\} |
|
1593 |
|
\end{tabular} |
|
1594 |
|
|
|
1595 |
|
Normalize the relations to 2NF and 3NF. Show all relations at each stage (2NF and 3NF) of the normalization process. |
|
1596 |
|
|
|
1597 |
|
|
|
1598 |
|
Solution +.# |
|
1599 |
|
|
|
1600 |
|
TO BE WRITTEN |
|
1601 |
|
|
|
1602 |
|
|
|
1603 |
|
|
|
1604 |
|
Problem +.# |
|
1605 |
|
|
|
1606 |
|
Consider the following relation for published books: |
|
1607 |
|
\begin{center} |
|
1608 |
|
BOOK (Book\_title, Book\_type, Author\_name, List\_price, Author\_affil, Publisher) |
|
1609 |
|
\end{center} |
|
1610 |
|
|
|
1611 |
|
Suppose we have the following dependencies: |
|
1612 |
|
|
|
1613 |
|
\begin{center} |
|
1614 |
|
\begin{tabular}{r c l} |
|
1615 |
|
Book\_title & \(\to\) & \{ Publisher, Book\_type \}\\ |
|
1616 |
|
Book\_type & \(\to\) & List\_price\\ |
|
1617 |
|
Author\_name & \(\to\) & Author\_affil |
|
1618 |
|
\end{tabular} |
|
1619 |
|
\end{center} |
|
1620 |
|
|
|
1621 |
|
\begin{itemize} |
|
1622 |
|
\item What would be a suitable key for this relation? |
|
1623 |
|
\item How could this relation not be in first normal form? Explain your answer. |
|
1624 |
|
\item This relation is not in second normal form: explain why and normalize it. |
|
1625 |
|
\item Is the relations you obtained at the previous step in third normal form? Explain why, and normalize them if needed. |
|
1626 |
|
\end{itemize} |
|
1627 |
|
|
|
1628 |
|
|
|
1629 |
|
|
|
1630 |
|
Solution +.# |
|
1631 |
|
|
|
1632 |
|
\begin{itemize} |
|
1633 |
|
\item \{Book Title, Author Name\} |
|
1634 |
|
\item If an attribute is composite or multi-valued. |
|
1635 |
|
\item Because of \{ Book\_title \}\(\to\) \{ Publisher, Book\_type \}. |
|
1636 |
|
We can normalize it as |
|
1637 |
|
(Book Title, Publisher, Book Type, List Price), (Author Name, Author Affiliation), (Author Name, Book Title). |
|
1638 |
|
\item Because of \{Book title\} \(\to\) \{ Book\_type\} \(\to\) \{ List\_price\}\\ |
|
1639 |
|
(Book Title, Publisher, Book Type) and (Book Type, List Price), (Author Name, Author Affiliation), (Author Name, Book Title). |
|
1640 |
|
|
|
1641 |
|
\end{itemize} |
|
1642 |
|
|
|
1643 |
|
|
|
1644 |
|
%%% 06 |
|
1645 |
|
|
|
1646 |
|
|
|
1647 |
|
Exercise +.# |
|
1648 |
|
|
|
1649 |
|
Consider the relation \(R(A, B, C, D, E)\) and the functional dependencies \( \{A, B\} \to C, B \to D, C \to E\). |
|
1650 |
|
Answer the following: |
|
1651 |
|
|
|
1652 |
|
\begin{enumerate} |
|
1653 |
|
\item \(A\) by itself is not a primary key, but what is the only key that contains \(A\)? |
|
1654 |
|
\item List the non-prime attributes. |
|
1655 |
|
\item This relation is not in 2NF: what transformation can you operate to obtain a 2NF? |
|
1656 |
|
\item One of the relation you obtained at the previous step is likely not to be in 3NF. |
|
1657 |
|
Can you normalize it? |
|
1658 |
|
If yes, how? |
|
1659 |
|
\end{enumerate} |
|
1660 |
|
|
|
1661 |
|
|
|
1662 |
|
Solution +.# |
|
1663 |
|
|
|
1664 |
|
\(\{A, B\}\), |
|
1665 |
|
|
|
1666 |
|
\(C, D, E\), |
|
1667 |
|
|
|
1668 |
|
\(R_1(A, B, C, E)\) and \(R_2(B, D)\) |
|
1669 |
|
|
|
1670 |
|
\(R_1(A, B, C)\), \(R_2(C, E)\) and \(R_3(B, D)\) |
|
1671 |
|
|
|
1672 |
|
|
|
1673 |
|
|
|
1674 |
|
Exercise +.# |
|
1675 |
|
|
|
1676 |
|
What are the two different categories of U.M.L. diagram? |
|
1677 |
|
|
|
1678 |
|
|
|
1679 |
|
Solution +.# |
|
1680 |
|
|
|
1681 |
|
Behaviour and structure |
|
1682 |
|
|
|
1683 |
|
|
|
1684 |
|
Exercise +.# |
|
1685 |
|
|
|
1686 |
|
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? |
|
1687 |
|
Justify your answer. |
|
1688 |
|
|
|
1689 |
|
|
|
1690 |
|
Solution +.# |
|
1691 |
|
|
|
1692 |
|
Yes, U.M.L. diagram is language-independent and platform-independent. |
|
1693 |
|
|
|
1694 |
|
|
|
1695 |
|
Exercise +.# |
|
1696 |
|
|
|
1697 |
|
What kind of diagram should we use if we want to \(\hdots\) |
|
1698 |
|
\begin{enumerate} |
|
1699 |
|
\item describe the functional behavior of the system as seen by the user? |
|
1700 |
|
\item capture the flow of messages in a software? |
|
1701 |
|
\item represent the workflow of actions of an user? |
|
1702 |
|
\end{enumerate} |
|
1703 |
|
|
|
1704 |
|
|
|
1705 |
|
Solution +.# |
|
1706 |
|
|
|
1707 |
|
\begin{enumerate} |
|
1708 |
|
\item Use-case |
|
1709 |
|
\item Sequence diagram |
|
1710 |
|
\item Activity diagram |
|
1711 |
|
\end{enumerate} |
|
1712 |
|
|
|
1713 |
|
|
|
1714 |
|
|
|
1715 |
|
Exercise +.# |
|
1716 |
|
|
|
1717 |
|
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. |
|
1718 |
|
|
|
1719 |
|
|
|
1720 |
|
Solution +.# |
|
1721 |
|
|
|
1722 |
|
To use direction for association, to have a common language with someone less knowledgeable of other diagrammatic notations. |
|
1723 |
|
For the concept of integration. |
|
1724 |
|
|
|
1725 |
|
|
|
1726 |
|
|
|
1727 |
|
|
|
1728 |
|
Exercise +.# |
|
1729 |
|
|
|
1730 |
|
Consider the following diagram: |
|
1731 |
|
|
|
1732 |
|
\begin{tikzpicture}[scale = 0.8] |
|
1733 |
|
\begin{class}[text width=7cm]{Flight}{0,0} |
|
1734 |
|
\attribute{flightNumber : Integer} |
|
1735 |
|
\attribute{departureTime : Date} |
|
1736 |
|
\attribute{flightDuration : Minutes} |
|
1737 |
|
\attribute{departingAirport : String} |
|
1738 |
|
\attribute{arrivingAirport : String} |
|
1739 |
|
\operation{delayFlight(numberOfMinutes : Minutes)} |
|
1740 |
|
\operation{getArrivalTime( ) : Date} |
|
1741 |
|
\end{class} |
|
1742 |
|
\begin{class}{Plane}{11, -0.8} |
|
1743 |
|
\attribute{airPlaneType : String} |
|
1744 |
|
\attribute{maximumSpeed : MPH} |
|
1745 |
|
\attribute{maximumDistance : Miles} |
|
1746 |
|
\attribute{tailID : String} |
|
1747 |
|
\end{class} |
|
1748 |
|
\association{Plane}{}{0..1}{Flight}{0..*}{} |
|
1749 |
|
\end{tikzpicture} |
|
1750 |
|
|
|
1751 |
|
Give the number of attributes for both classes, and suggest two operations for the class that doesn't have any. |
|
1752 |
|
Discuss the multiplicities: why did the designer picked those values? |
|
1753 |
|
|
|
1754 |
|
|
|
1755 |
|
Solution +.# |
|
1756 |
|
|
|
1757 |
|
\(5\) and \(4\). |
|
1758 |
|
|
|
1759 |
|
getLastFlightNumber() : Integer |
|
1760 |
|
|
|
1761 |
|
stMaximumSpeed(MPH) : void |
|
1762 |
|
|
|
1763 |
|
A flight could be assigned to no plane, and a plane could not be assigned to a flight. |
|
1764 |
|
|
|
1765 |
|
A plane can be assigned to multiple (or no) flights, but a flight must have at most one plane (and could have none). |
|
1766 |
|
|
|
1767 |
|
|
|
1768 |
|
Exercise +.# |
|
1769 |
|
|
|
1770 |
|
Briefly explain the difference between an aggregation and a composition association. |
|
1771 |
|
|
|
1772 |
|
|
|
1773 |
|
Solution +.# |
|
1774 |
|
|
|
1775 |
|
Aggregation: associated class can have an existence of its own. |
|
1776 |
|
|
|
1777 |
|
Composition association: class doesn't exist without the association. |
|
1778 |
|
|
|
1779 |
|
|
|
1780 |
|
Exercise +.# |
|
1781 |
|
|
|
1782 |
|
How is generalization (or inheritance) represented in a U.M.L. class diagram? |
|
1783 |
|
Why is such a concept useful? |
|
1784 |
|
|
|
1785 |
|
|
|
1786 |
|
Solution +.# |
|
1787 |
|
|
|
1788 |
|
\tikz{\draw[>=open triangle 60,->] (0,0) to (1,0);} |
|
1789 |
|
Because it avoids redundancy. |
|
1790 |
|
|
|
1791 |
|
|
|
1792 |
|
Exercise +.# |
|
1793 |
|
|
|
1794 |
|
Convert the following E.R. diagram into a U.M.L. class diagram: |
|
1795 |
|
|
|
1796 |
|
\begin{tikzpicture}[node distance=8em] |
|
1797 |
|
\node[entity] (person) {PILOT}; |
|
1798 |
|
\node[attribute] (pid) [left of=person] {\key{ID}} edge (person); |
|
1799 |
|
\node[attribute] (name) [above left of=person] {Name} edge (person); |
|
1800 |
|
\node[attribute] (phone) [above of=person] {Experience} edge (person); |
|
1801 |
|
|
|
1802 |
|
\node[relationship] (drives) [below of=person] {ASSIGNED\_TO} edge node[right, pos=0.1] {$N$} (person); |
|
1803 |
|
\node[entity] (plane) [below of=drives] {PLANE} edge [total] node[right, pos=0.7] {$1$} (drives); |
|
1804 |
|
\node[attribute] (make) [left of=plane] {\key{TailID}} edge (plane); |
|
1805 |
|
\node[attribute] (maxS) [below left of =plane] {MaxSpeed} edge (plane); |
|
1806 |
|
\node[attribute] (airPlaneType) [below of = plane] {AirPlaneType} edge (plane); |
|
1807 |
|
\end{tikzpicture} |
|
1808 |
|
|
|
1809 |
|
Problem +.# |
|
1810 |
|
|
|
1811 |
|
\label{probMYSQLW} |
|
1812 |
|
Consider the following E.R. schema for the CAR\_INFO database: |
|
1813 |
|
|
|
1814 |
|
\begin{tikzpicture}[node distance=8em] |
|
1815 |
|
\node[entity] (person) {PERSON}; |
|
1816 |
|
\node[attribute] (pid) [left of=person] {\key{ID}} edge (person); |
|
1817 |
|
\node[attribute] (name) [above left of=person] {Name} edge (person); |
|
1818 |
|
\node[multi attribute] (phone) [above of=person] {Phone} edge (person); |
|
1819 |
|
\node[attribute] (address) [above right of=person] {Address} edge (person); |
|
1820 |
|
\node[attribute] (street) [above right of=address] {Street} edge (address); |
|
1821 |
|
\node[attribute] (city) [right of=address] {City} edge (address); |
|
1822 |
|
%\node[derived attribute] (age) [right of=person] {Age} edge (person); |
|
1823 |
|
|
|
1824 |
|
\node[relationship] (drives) [below right of=person] {DRIVES} edge node[above, pos=0.1] {$1$} (person); |
|
1825 |
|
\node[entity] (car) [below left of=drives] {CAR} edge node[above, pos=0.7] {$1$} (drives); |
|
1826 |
|
\node[attribute] (make) [left of=car] {Make} edge (car); |
|
1827 |
|
\node[attribute] (year) [below left of =car] {Year} edge (car); |
|
1828 |
|
\node[attribute] (brand) [below of =car] {Brand} edge (car); |
|
1829 |
|
|
|
1830 |
|
\node[relationship] (seats) [below left of=person] {SEATS\_IN} edge node[above, pos=0.1] {$N$} (person); |
|
1831 |
|
\draw (seats) edge node[above, pos=0.3] {$1$} (car); |
|
1832 |
|
\node[attribute] (position) [left of=seats] {Position} edge (seats); |
|
1833 |
|
|
|
1834 |
|
\node[ident relationship] (insured) [right of=car] {INSURED} edge node[above, pos=0.3] {$1$} (car); |
|
1835 |
|
\node[weak entity] (insurance) [right = 1cm of insured] {CAR\_INSURANCE} edge[total] node[above, pos=0.7] {$N$} (insured); |
|
1836 |
|
\node[attribute] (amount) [above of =insurance] {Covered Amount} edge (insurance); |
|
1837 |
|
\node[attribute] (policy) [above right of =insurance] {Policy Number} edge (insurance); |
|
1838 |
|
\node[attribute] (company) [below right of =insurance] {Company Name} edge (insurance); |
|
1839 |
|
\end{tikzpicture} |
|
1840 |
|
|
|
1841 |
|
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"). |
|
1842 |
|
|
|
1843 |
|
\begin{enumerate} |
|
1844 |
|
\item 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. |
|
1845 |
|
\item Convert that E.-R. diagram to a relational database schema. |
|
1846 |
|
\item 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. |
|
1847 |
|
\end{enumerate} |
|
1848 |
|
|
|
1849 |
|
|
|
1850 |
|
|
|
1851 |
|
Solution +.# |
|
1852 |
|
|
|
1853 |
|
For "Car", we need to create an attribute, like "vin". |
|
1854 |
|
For "Car Insurance", "Policy Number" is perfect. |
|
1855 |
|
|
|
1856 |
|
\colorlet{lightgray}{gray!20} |
|
1857 |
|
|
|
1858 |
|
\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} |
|
1859 |
|
|
|
1860 |
|
% RELATIONS |
|
1861 |
|
|
|
1862 |
|
\node (phonetitle) {\textbf{PHONE}}; |
|
1863 |
|
|
|
1864 |
|
\node [relation=2, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of phonetitle.west, anchor=west] (phone) |
|
1865 |
|
{\underline{id}% |
|
1866 |
|
\nodepart{two} \underline{number} |
|
1867 |
|
}; |
|
1868 |
|
|
|
1869 |
|
\node [below=1.3cm of phone.west, anchor=west] (persontitle) {\textbf{PERSON}}; |
|
1870 |
|
|
|
1871 |
|
\node [relation=6, rectangle split horizontal, rectangle split part fill={lightgray!50}, below=0.6cm of persontitle.west, anchor=west] (person) |
|
1872 |
|
{\underline{id}% |
|
1873 |
|
\nodepart{two} Name |
|
1874 |
|
\nodepart{three} Street |
|
1875 |
|
\nodepart{four} City |
|
1876 |
|
\nodepart{five} Seat |
|
1877 |
|
\nodepart{six}{Position} |
|
1878 |
|
}; |
|
1879 |
|
|
|
1880 |
|
\node [below=1.1cm of person.west, anchor=west] (cartitle) {\textbf{CAR}}; |
|
1881 |
|
|
|
1882 |
|
\node [relation=5, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of cartitle.west, anchor=west] (car) |
|
1883 |
|
{\underline{Vin}% |
|
1884 |
|
\nodepart{two} Make |
|
1885 |
|
\nodepart{three} Year |
|
1886 |
|
\nodepart{four} Brand |
|
1887 |
|
\nodepart{five} Driver |
|
1888 |
|
}; |
|
1889 |
|
|
|
1890 |
|
\node [below=1.4cm of car.west, anchor=west] (carinsurancetitle) {\textbf{CAR INSURANCE}}; |
|
1891 |
|
|
|
1892 |
|
\node [relation=4, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of carinsurancetitle.west, anchor=west] (carinsurance) |
|
1893 |
|
{\underline{Policy Number}% |
|
1894 |
|
\nodepart{two} Covered Amount |
|
1895 |
|
\nodepart{three} Company Name |
|
1896 |
|
\nodepart{four} Insured Car |
|
1897 |
|
}; |
|
1898 |
|
|
|
1899 |
|
% FOREIGN KEYS |
|
1900 |
|
|
|
1901 |
|
\draw[-latex] (phone.one south) -- ++(0,-0.2) --++(7,0) --++(0, -2) -| ($(person.one south) + (0.2,0)$); |
|
1902 |
|
%\draw[-latex] (person.five south) -- ++(0,-0.2) --++(3,0) --++(0, -2) -| ($(car.one south) + (-0.2,0)$); |
|
1903 |
|
\draw[-latex] (carinsurance.four south) -- ++(0,-0.2) --++(2,0) --++(0, 1.75) -| ($(car.one south) + (0.1,0)$); |
|
1904 |
|
\draw[-latex] (car.five south) -- ++(0,-0.2) --++(2,0) --++(0, 1.5) -| ($(person.one south) + (-0.1,0)$); |
|
1905 |
|
\end{tikzpicture} |
|
1906 |
|
|
|
1907 |
|
|
|
1908 |
|
\begin{tikzpicture}[scale = 0.8] |
|
1909 |
|
\node (person) at (0,0){\begin{tabular}{| r l |} |
|
1910 |
|
\hline |
|
1911 |
|
\multicolumn{2}{| c |}{\textbf{Person}}\\ |
|
1912 |
|
\hline |
|
1913 |
|
id &: String\\ |
|
1914 |
|
name &: String\\ |
|
1915 |
|
address &: Street\\ |
|
1916 |
|
& City\\ |
|
1917 |
|
\hline |
|
1918 |
|
getAge()&: Int\\ |
|
1919 |
|
\hline |
|
1920 |
|
\end{tabular} |
|
1921 |
|
}; |
|
1922 |
|
\node (phone) at (8, 1){\begin{tabular}{| r l |} |
|
1923 |
|
\hline |
|
1924 |
|
\multicolumn{2}{| c |}{\textbf{Phone}}\\ |
|
1925 |
|
\hline |
|
1926 |
|
number &: String\\ |
|
1927 |
|
\hline |
|
1928 |
|
\end{tabular} |
|
1929 |
|
}; |
|
1930 |
|
\draw[open diamond-] (person) -- node[above, pos=0.1]{\(0..\star\)} node[above, pos=0.9]{\(1..1\)} (phone); |
|
1931 |
|
\node (car) at (8, -3){\begin{tabular}{| r l |} |
|
1932 |
|
\hline |
|
1933 |
|
\multicolumn{2}{| c |}{\textbf{Car}}\\ |
|
1934 |
|
\hline |
|
1935 |
|
vin &: String\\ |
|
1936 |
|
make &: String\\ |
|
1937 |
|
year &: Year\\ |
|
1938 |
|
brand &: String\\ |
|
1939 |
|
\hline |
|
1940 |
|
\end{tabular} |
|
1941 |
|
}; |
|
1942 |
|
\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); |
|
1943 |
|
%\draw[-] (person) -- node[below, pos=0.1]{\(0..1\)} node[below, pos=0.9]{\(0..4\)} ($(car)+(-2.5, 0.5)$); |
|
1944 |
|
\node (seat) at (3.5, -5){\begin{tabular}{| r l |} |
|
1945 |
|
\hline |
|
1946 |
|
\multicolumn{2}{| c |}{\textbf{Seats In}}\\ |
|
1947 |
|
\hline |
|
1948 |
|
position &: String\\ |
|
1949 |
|
\hline |
|
1950 |
|
\end{tabular} |
|
1951 |
|
}; |
|
1952 |
|
\draw[dashed] (seat) -- (3.5, -1.6); |
|
1953 |
|
|
|
1954 |
|
\node (insu) at (14.3, -2){\begin{tabular}{| r l |} |
|
1955 |
|
\hline |
|
1956 |
|
\multicolumn{2}{| c |}{\textbf{Car Insurance}}\\ |
|
1957 |
|
\hline |
|
1958 |
|
policy number &: String\\ |
|
1959 |
|
covered amount &: int\\ |
|
1960 |
|
compagny name &: String\\ |
|
1961 |
|
\hline |
|
1962 |
|
\end{tabular} |
|
1963 |
|
}; |
|
1964 |
|
|
|
1965 |
|
\draw[diamond -] (car) -- node[above, pos=0.1]{\(0..\star\)} node[above, pos=0.9]{\(1\)} (insu); |
|
1966 |
|
\end{tikzpicture} |
|
1967 |
|
|
|
1968 |
|
|
|
1969 |
|
|
|
1970 |
|
Problem +.# |
|
1971 |
|
|
|
1972 |
|
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. |
|
1973 |
|
\begin{enumerate} |
|
1974 |
|
\item Install MySQL Workbench: use your package manager, or download the binaries from \url{https://dev.mysql.com/downloads/workbench/}. |
|
1975 |
|
\item 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". |
|
1976 |
|
\item 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". |
|
1977 |
|
\item 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. |
|
1978 |
|
\item 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". |
|
1979 |
|
\item 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. |
|
1980 |
|
\item 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. |
|
1981 |
|
\item 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. |
|
1982 |
|
\end{enumerate} |
|
1983 |
|
|
|
1984 |
|
|
|
1985 |
|
Problem +.# |
|
1986 |
|
|
|
1987 |
|
\emph{This problem requires you to have successfully completed Problem~\ref{probMYSQLW} and Problem~\ref{probERtoREL}.} |
|
1988 |
|
|
|
1989 |
|
Using the relational database schema you obtained in question~\ref{ERtoREL} of Problem~\ref{probERtoREL}, write the \texttt{SQL} implementation of that database. |
|
1990 |
|
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}. |
|
1991 |
|
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? |
|
1992 |
|
|
|
1993 |
|
|
|
1994 |
|
Solution +.# |
|
1995 |
|
|
|
1996 |
|
\inputminted{sql}{include/sol.sql} |
|
1997 |
|
\includegraphics{include/mysql_workbench_drawing-crop} |
|
1998 |
|
|
|
1999 |
|
|
|
2000 |
|
%%% 07 |
|
2001 |
|
|
|
2002 |
|
|
|
2003 |
|
Exercise +.# |
|
2004 |
|
|
|
2005 |
|
What are the technologies that makes it possible for a Java application to communicate with a DBMS? |
|
2006 |
|
|
|
2007 |
|
|
|
2008 |
|
Solution +.# |
|
2009 |
|
|
|
2010 |
|
API + driver |
|
2011 |
|
|
|
2012 |
|
|
|
2013 |
|
Exercise +.# |
|
2014 |
|
|
|
2015 |
|
What JDBC method do you call to get a connection to a database? |
|
2016 |
|
|
|
2017 |
|
Solution +.# |
|
2018 |
|
|
|
2019 |
|
DriverManager.getConnection() |
|
2020 |
|
|
|
2021 |
|
|
|
2022 |
|
Exercise +.# |
|
2023 |
|
|
|
2024 |
|
Briefly explain what the \texttt{next()} method from the \texttt{ResultSet} class does, and what is its return type. |
|
2025 |
|
|
|
2026 |
|
Solution +.# |
|
2027 |
|
|
|
2028 |
|
It checks if there is data to read, and if move the cursor reads it. |
|
2029 |
|
It returns a Boolean. |
|
2030 |
|
|
|
2031 |
|
|
|
2032 |
|
Exercise +.# |
|
2033 |
|
|
|
2034 |
|
How do you submit a `SELECT` statement to the DBMS? |
|
2035 |
|
|
|
2036 |
|
|
|
2037 |
|
Solution +.# |
|
2038 |
|
|
|
2039 |
|
Using |
|
2040 |
|
|
|
2041 |
|
```java |
|
2042 |
|
.executeQuery(strSelect) |
|
2043 |
|
``` |
|
2044 |
|
|
|
2045 |
|
|
|
2046 |
|
Exercise +.# |
|
2047 |
|
|
|
2048 |
|
Where is a ResultSet object's cursor initially pointing? How do you move the cursor forward in the result set? |
|
2049 |
|
|
|
2050 |
|
|
|
2051 |
|
Solution +.# |
|
2052 |
|
|
|
2053 |
|
Before the first line. |
|
2054 |
|
|
|
2055 |
|
```java |
|
2056 |
|
next() |
|
2057 |
|
``` |
|
2058 |
|
|
|
2059 |
|
method |
|
2060 |
|
|
|
2061 |
|
Exercise +.# |
|
2062 |
|
|
|
2063 |
|
Give three navigation methods provided by `ResultSet`. |
|
2064 |
|
|
|
2065 |
|
|
|
2066 |
|
Solution +.# |
|
2067 |
|
|
|
2068 |
|
`first()` |
|
2069 |
|
`last()` |
|
2070 |
|
`next()` |
|
2071 |
|
`previous()` |
|
2072 |
|
`relative(rows)` |
|
2073 |
|
`absolute(row)` method. |
|
2074 |
|
|
|
2075 |
|
|
|
2076 |
|
Exercise +.# |
|
2077 |
|
|
|
2078 |
|
Explain this JDBC URL format: |
|
2079 |
|
|
|
2080 |
|
```java |
|
2081 |
|
jdbc:mysql://localhost:3306/HW_NewDB?createDatabaseIfNotExist=true&useSSL=true |
|
2082 |
|
``` |
|
2083 |
|
|
|
2084 |
|
|
|
2085 |
|
Solution +.# |
|
2086 |
|
|
|
2087 |
|
Connect to localhost:3306 and create a new database if needed, and use secure connection. |
|
2088 |
|
|
|
2089 |
|
|
|
2090 |
|
Exercise +.# |
|
2091 |
|
|
|
2092 |
|
In what class is the `getColumnName()} method? |
|
2093 |
|
|
|
2094 |
|
|
|
2095 |
|
Solution +.# |
|
2096 |
|
|
|
2097 |
|
ResultSetMetaData |
|
2098 |
|
|
|
2099 |
|
|
|
2100 |
|
Exercise +.# |
|
2101 |
|
|
|
2102 |
|
What is a prepared statement? |
|
2103 |
|
|
|
2104 |
|
|
|
2105 |
|
Solution +.# |
|
2106 |
|
|
|
2107 |
|
A prepared statement is a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency. |
|
2108 |
|
|
|
2109 |
|
|
|
2110 |
|
Exercise +.# |
|
2111 |
|
|
|
2112 |
|
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. |
|
2113 |
|
|
|
2114 |
|
\begin{minted}[linenos, breaklines=true, baselinestretch=1.4]{Java} |
|
2115 |
|
import java.sql.*; |
|
2116 |
|
|
|
2117 |
|
public class MyProg{ |
|
2118 |
|
public static void main(String[] args) { |
|
2119 |
|
try ( |
|
2120 |
|
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" |
|
2121 |
|
+"HW_TestDB?user=testuser&password=password"); |
|
2122 |
|
Statement stmt = conn.createStatement(); |
|
2123 |
|
) { |
|
2124 |
|
|
|
2125 |
|
/* Errors after this point.*/ |
|
2126 |
|
|
|
2127 |
|
String strSelect = "SELECT title WHERE qty > 40 FROM disks;"; |
|
2128 |
|
ResultSet rset = stmt.executeUpdate(strSelect); |
|
2129 |
|
|
|
2130 |
|
System.out.println("The records selected are: (listed last first):"); |
|
2131 |
|
rset.last(); |
|
2132 |
|
|
|
2133 |
|
while(rset.previous()) { |
|
2134 |
|
String title = rset.getDouble("title"); |
|
2135 |
|
System.out.println(title + "\n"); |
|
2136 |
|
} |
|
2137 |
|
|
|
2138 |
|
String sss = "SELECT title FROM disks WHERE Price <= ?"; |
|
2139 |
|
PreparedStatement ps = conn.prepareStatement(sss); |
|
2140 |
|
ResultSet result = ps.executeQuery(); |
|
2141 |
|
|
|
2142 |
|
conn.close(); |
|
2143 |
|
|
|
2144 |
|
/* Errors before this point.*/ |
|
2145 |
|
|
|
2146 |
|
} catch(SQLException ex) { |
|
2147 |
|
ex.printStackTrace(); |
|
2148 |
|
} |
|
2149 |
|
} |
|
2150 |
|
} |
|
2151 |
|
``` |
|
2152 |
|
|
|
2153 |
|
|
|
2154 |
|
|
|
2155 |
|
|
|
2156 |
|
% |
|
2157 |
|
%METADATA |
|
2158 |
|
% |
|
2159 |
|
%ResultSet is array, other? |
|
2160 |
|
% |
|
2161 |
|
%Multiple queries |
|
2162 |
|
% |
|
2163 |
|
%qet datatype |
|
2164 |
|
% |
|
2165 |
|
%prepared statemernt. |
|
2166 |
|
% |
|
2167 |
|
%http://tutorials.jenkov.com/jdbc/resultset.html |
|
2168 |
|
|
|
2169 |
|
|
|
2170 |
|
|
|
2171 |
|
This part will mainly hands on, and ask you to read code. |
|
2172 |
|
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. |
|
2173 |
|
I'll assume that you will have successfully completed those tasks by \hwDateNext, so don't wait and let me know if you had difficulties solving them. |
|
2174 |
|
|
|
2175 |
|
**References:** |
|
2176 |
|
|
|
2177 |
|
\begin{itemize} |
|
2178 |
|
\item This second part takes some inspiration from \url{https://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html}. If you experience troubles, \url{https://www.ntu.edu.sg/home/ehchua/programming/howto/ErrorMessages.html#JDBCErrors} might be a good read. |
|
2179 |
|
\item Section 13.3.2 of your textbook is a condensed, but good read. Many textbook on Java includes a part on Databases, cf. for instance the Chapter 16 of \emph{Starting Out with Java: Early Objects} (5th Edition) by Tony Gaddis. |
|
2180 |
|
\end{itemize} |
|
2181 |
|
|
|
2182 |
|
|
|
2183 |
|
Problem +.# |
|
2184 |
|
|
|
2185 |
|
In the archive, navigate to \texttt{code/sql/}, open and read \texttt{HW\_ebookshop.sql}. |
|
2186 |
|
|
|
2187 |
|
Then, open a terminal (or command-line interpreter), navigate to the folder where you stored that file (using `cd`), and type |
|
2188 |
|
|
|
2189 |
|
\begin{minted}{bash} |
|
2190 |
|
mysql -u testuser -p < HW_ebookshop.sql |
|
2191 |
|
``` |
|
2192 |
|
|
|
2193 |
|
|
|
2194 |
|
|
|
2195 |
|
for linux, or (something like) |
|
2196 |
|
|
|
2197 |
|
\begin{minted}[breaklines=true]{bash} |
|
2198 |
|
"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql.exe" -u testuser -p < HW_ebookshop.sql |
|
2199 |
|
``` |
|
2200 |
|
|
|
2201 |
|
|
|
2202 |
|
for Windows. |
|
2203 |
|
|
|
2204 |
|
You just discovered MySQL's batch mode, that perform \emph{series} of instructions from a file. |
|
2205 |
|
You can easily make sure that the database and the table were indeed created, and the values inserted. |
|
2206 |
|
|
|
2207 |
|
|
|
2208 |
|
Problem +.# |
|
2209 |
|
|
|
2210 |
|
This exercise supposes you successfully completed Problem~\ref{qu:batch}. |
|
2211 |
|
We will compile and execute your first database application, using Java and MySQL. |
|
2212 |
|
\begin{itemize} |
|
2213 |
|
\item I will assume that you have MySQL installed and set-up as indicated in Homeworks \#1 and \#2. |
|
2214 |
|
\item I will assume that you have Java installed. If not, please refer to \url{http://spots.augusta.edu/caubert/teaching/general/java/} for a simple program and the instructions to compile and execute it. |
|
2215 |
|
\item We need to set up the \emph{driver} (or \emph{connector}) to make the java \texttt{sql} API and MySQL communicate. To do so, |
|
2216 |
|
\begin{itemize} |
|
2217 |
|
\item Go to \url{https://dev.mysql.com/downloads/connector/j/} |
|
2218 |
|
\item Click on "Download" in front of "Platform Independent (Architecture Independent), ZIP Archive" |
|
2219 |
|
\item Look for the (somewhat hidden) "No thanks, just start my download." |
|
2220 |
|
\item You will download a file named "mysql-connector-java-***.zip", where \texttt{***} is the version number. |
|
2221 |
|
\item Upon completion of the download, unzip the file, and locate the "mysql-connector-java-***-bin.jar" file. |
|
2222 |
|
\item Copy that file in \texttt{code/java/}. |
|
2223 |
|
\end{itemize} |
|
2224 |
|
\item Open a terminal in that same folder, and compile \texttt{FirstProg.java}, using |
|
2225 |
|
\begin{minted}{bash} |
|
2226 |
|
javac FirstProg.java |
|
2227 |
|
``` |
|
2228 |
|
|
|
2229 |
|
|
|
2230 |
|
(or an equivalent command for windows). |
|
2231 |
|
Normally, nothing will be printed, but a \texttt{FirstProg.class} file will be created. |
|
2232 |
|
\item Now, execute that program, using |
|
2233 |
|
\begin{minted}{bash} |
|
2234 |
|
java -cp .:mysql-connector-java-***-bin.jar FirstProg |
|
2235 |
|
``` |
|
2236 |
|
|
|
2237 |
|
|
|
2238 |
|
in Linux, or |
|
2239 |
|
\begin{minted}{bash} |
|
2240 |
|
java -cp .;mysql-connector-java-***-bin.jar FirstProg |
|
2241 |
|
``` |
|
2242 |
|
|
|
2243 |
|
|
|
2244 |
|
in Windows. |
|
2245 |
|
The \texttt{-cp} option lists the places where java should look for the class used in the program: we are explicitely asking java to use the \texttt{mysql-connector-java-***-bin.jar} executable to execute our \texttt{FirstProg} executable. |
|
2246 |
|
Try to execute \texttt{FirstProg} without that flag, and see what happens. |
|
2247 |
|
\end{itemize} |
|
2248 |
|
|
|
2249 |
|
|
|
2250 |
|
Solution +.# |
|
2251 |
|
|
|
2252 |
|
\begin{minted}[breaklines=true]{bash} |
|
2253 |
|
$ java FirstProg |
|
2254 |
|
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/HW_ebookshop |
|
2255 |
|
at java.sql.DriverManager.getConnection(DriverManager.java:689) |
|
2256 |
|
at java.sql.DriverManager.getConnection(DriverManager.java:247) |
|
2257 |
|
at FirstProg.main(FirstProg.java:9) |
|
2258 |
|
``` |
|
2259 |
|
|
|
2260 |
|
|
|
2261 |
|
|
|
2262 |
|
|
|
2263 |
|
Problem +.# |
|
2264 |
|
|
|
2265 |
|
There are three other programs in that archive: |
|
2266 |
|
\begin{description} |
|
2267 |
|
\item[Variation.java] is the program we looked at during class. |
|
2268 |
|
\item[NullProg.java] is an attempt to answer the question whenever SQL's `NULL` is JAVA's \texttt{NULL} when it comes to strings. |
|
2269 |
|
\item[AdvancedProg.java] is a long and commented program that introduces multiple new tools and ideas. |
|
2270 |
|
\end{description} |
|
2271 |
|
|
|
2272 |
|
Read, execute, break, edit, compile, patch, hack and (most importantly) understand those three programs, with, of course, a focus on the last one. |
|
2273 |
|
|
|
2274 |
|
|
|
2275 |
|
|
|
2276 |
|
%%% 08 -- Exam 1 |
|
2277 |
|
|
|
2278 |
|
|
|
2279 |
|
Problem +.# |
|
2280 |
|
%30 |
|
2281 |
|
A cinema company wants you to design a relational model for the following set-up: |
|
2282 |
|
\begin{itemize} |
|
2283 |
|
\item The company has movie stars. Each star has a name, gender, birthday, and unique id. |
|
2284 |
|
\item The company has information about movies: title, year, length, genre, and studio. Each movie has a unique id, and features multiple stars. |
|
2285 |
|
\item The company owns movie theaters as well. Each theater has a name, address, and a unique id. |
|
2286 |
|
\item Furthermore, each theater has a set of auditoriums. Each auditorium has a unique number, and seating capacity. |
|
2287 |
|
\item Each theater can schedule movies at show-times. |
|
2288 |
|
Each show-time has a unique id, a start time, and is for a specific movie, at a theater auditorium. |
|
2289 |
|
\item The company sells tickets for scheduled show-times. Each ticket has a unique ticket id, and a price. |
|
2290 |
|
\end{itemize} |
|
2291 |
|
%Your relations should be written as follow: relation names in uppercase, coma separated attributes enclosed in parenthesis, primary key underlined, and foreign key(s) detailed on the side (or drawn). That is, for instance, |
|
2292 |
|
%"PROF(\underline{Login", Name, Department) ; where Department references the Code attribute in the DEPARTMENT table.} |
|
2293 |
|
Complete the following relational model. |
|
2294 |
|
If you think there are missing relations, add them, and when the parenthesis isn't closed, close it or add attribute(s). |
|
2295 |
|
Also, specify the primary as well as foreign keys. |
|
2296 |
|
|
|
2297 |
|
{ |
|
2298 |
|
\renewcommand{\arraystretch}{3}% |
|
2299 |
|
\large |
|
2300 |
|
\hspace{3em}\begin{tabular}{l} |
|
2301 |
|
STAR(Name, Gender, Birthday, Id\\ |
|
2302 |
|
MOVIE(Title\\ |
|
2303 |
|
FEATURE\_IN(StarId, MovieId)\\ |
|
2304 |
|
THEATER(Name\\ |
|
2305 |
|
AUDITORIUM(Id, Capacity, Theater\\ |
|
2306 |
|
SHOWTIME( |
|
2307 |
|
\end{tabular} |
|
2308 |
|
} |
|
2309 |
|
|
|
2310 |
|
|
|
2311 |
|
|
|
2312 |
|
\begin{problem}[tags={ex}, points ={10}] |
|
2313 |
|
Your professor designed the following relational model at some point in his career, to help him organizing his exams and the students grades: |
|
2314 |
|
|
|
2315 |
|
\begin{center} |
|
2316 |
|
\begin{tabular}{l | l} |
|
2317 |
|
Table Name and Attributes & Example of Value\\ |
|
2318 |
|
\hline |
|
2319 |
|
EXAM(\underline{Number}, Date, Course) & \(\langle 1, \text{'2018-02-14'}, \text{'CSCI3410'}\rangle\)\\ |
|
2320 |
|
PROBLEM(\underline{Statement}, Points, Length, Exam) & \(\langle \text{'Your professor designed…'}, 10, \text{'00:10:00'}, 1\rangle\) \\ |
|
2321 |
|
STUDENT\_GRADE(\underline{Login}, \underline{Exam}, Grade) & \(\langle \text{'aalyx'}, 1, 83\rangle\) |
|
2322 |
|
\end{tabular} |
|
2323 |
|
\end{center} |
|
2324 |
|
|
|
2325 |
|
The idea was to have |
|
2326 |
|
\begin{itemize} |
|
2327 |
|
\item The EXAM table storing information about exams, |
|
2328 |
|
\item One entry per problem in the PROBLEM table, and to associate every problem to an exam (i.e., to have the Exam attribute referencing the Number attribute in the EXAM table), |
|
2329 |
|
\item The grade of one student for one particular exam stored in the STUDENT\_GRADE table, where Exam is referencing the Number attribute in the EXAM table. |
|
2330 |
|
\end{itemize} |
|
2331 |
|
Unfortunately, this design turned out to be terrible. |
|
2332 |
|
Describe at least one common and interesting situation where this model would fail to fulfill its purpose, and propose a way to correct the particular problem you identified. |
|
2333 |
|
|
|
2334 |
|
|
|
2335 |
|
|
|
2336 |
|
|
|
2337 |
|
|
|
2338 |
|
Problem +.# |
|
2339 |
|
Look at the SQL code below, and then answer the following questions. |
|
2340 |
|
|
|
2341 |
|
\begin{minted}[linenos]{mysql} |
|
2342 |
|
CREATE TABLE TRAIN( |
|
2343 |
|
Id VARCHAR(30), |
|
2344 |
|
Model VARCHAR(30), |
|
2345 |
|
ConstructionYear YEAR(4) |
|
2346 |
|
); |
|
2347 |
|
|
|
2348 |
|
CREATE TABLE CONDUCTOR( |
|
2349 |
|
Id VARCHAR(20), |
|
2350 |
|
Name VARCHAR(20), |
|
2351 |
|
ExperienceLevel VARCHAR(20) |
|
2352 |
|
); |
|
2353 |
|
|
|
2354 |
|
CREATE TABLE ASSIGNED_TO( |
|
2355 |
|
TrainId VARCHAR(20), |
|
2356 |
|
ConductorId VARCHAR(20), |
|
2357 |
|
Day DATE, |
|
2358 |
|
PRIMARY KEY(TrainId, ConductorId) |
|
2359 |
|
); |
|
2360 |
|
``` |
|
2361 |
|
|
|
2362 |
|
|
|
2363 |
|
|
|
2364 |
|
\begin{enumerate}[topsep=0pt,itemsep=10em,partopsep=1ex,parsep=1ex] |
|
2365 |
|
\item Modify the `CREATE` statement that creates the `TRAIN` table (l. 1--5), so that `Id` would be declared as the primary key. |
|
2366 |
|
You can write only the line(s) that need to change. |
|
2367 |
|
\item Write an `ALTER` statement that makes `Id` become the primary key of the `CONDUCTOR` table. |
|
2368 |
|
\item Modify the `CREATE` statement that creates the `ASSIGNED_TO` table (l. 13--18), so that it has two foreign keys: `ConductorId` would be referencing the `Id` attribute in `CONDUCTOR` and `TrainId` would be referencing the `Id` attribute in `TRAIN`. |
|
2369 |
|
You can write only the line(s) that need to change. |
|
2370 |
|
|
|
2371 |
|
\item Write `INSERT` statements that insert one tuple of your invention in each relation. |
|
2372 |
|
Your statements should respect all the constraints (including the ones we added at the previous questions) and result in actual insertions. (Remember that four digits is a valid value for an attribute with the `YEAR(4)` datatype.) |
|
2373 |
|
\item Write a statement that sets the value of the `ExperienceLevel` attribute to "Senior" in all the tuples where the `Id` attribute is "GP1029" in the `CONDUCTOR` relation.\item For each of the following questions, write a `SELECT` statement that would answer it: |
|
2374 |
|
\begin{enumerate}[topsep=0pt,itemsep=6em,partopsep=1ex,parsep=1ex] |
|
2375 |
|
\item "What are the identification numbers of the trains?" |
|
2376 |
|
\item "What are the names of the conductors with a \enquote{Senior" experience level?} |
|
2377 |
|
\item "What are the construction years of the \enquote{Surfliner" and "Regina" models that we have?} |
|
2378 |
|
\item "What is the id of the conductor that was responsible of the train referenced \enquote{K-13" on 2015/12/14?} |
|
2379 |
|
\item "What are the models that were ever conducted by the conductor whose id is \enquote{GP1029"?} |
|
2380 |
|
%\item "What is the name of the conductor that was responsible of the \enquote{Surfliner" model on October, 25th, 2015?} |
|
2381 |
|
\end{enumerate} |
|
2382 |
|
\end{enumerate} |
|
2383 |
|
|
|
2384 |
|
|
|
2385 |
|
|
|
2386 |
|
|
|
2387 |
|
\begin{problem}[tags={ex}, bonus-points = {4}, points={40}] |
|
2388 |
|
Suppose we have the relational model depicted in \autoref{fig:coffee}, p.~\pageref{fig:coffee}, with the indicated data in it. |
|
2389 |
|
|
|
2390 |
|
In the following, we will assume that this model was implemented in a DBMS (MySQL or MariaDB), the primary keys being the underlined attributes, and the foreign keys as follow: |
|
2391 |
|
|
|
2392 |
|
\begin{tabular}{r c l} |
|
2393 |
|
\textbf{FavCoffee} in the \textbf{CUSTOMER} relation & refers to & \textbf{Ref} in the \textbf{COFFEE} relation\\ |
|
2394 |
|
\textbf{Provider} in the \textbf{SUPPLY} & refers to & \textbf{Name} in the \textbf{PROVIDER} relation\\ |
|
2395 |
|
\textbf{Coffee} in the \textbf{SUPPLY} & refers to & \textbf{Ref} in the \textbf{COFFEE} relation |
|
2396 |
|
\end{tabular} |
|
2397 |
|
|
|
2398 |
|
You will be asked to read and write SQL commands. |
|
2399 |
|
You should assume that |
|
2400 |
|
\begin{enumerate} |
|
2401 |
|
\item Datatype doesn't matter: we use only strings and appropriate numerical datatypes. |
|
2402 |
|
\item Every statement respects SQL's syntax (there's no "a semi-colon is missing" trap). |
|
2403 |
|
\item None of those commands are actually executed: the data is always in the state of \autoref{fig:coffee}, you are asked to answer "what if" questions. |
|
2404 |
|
\end{enumerate} |
|
2405 |
|
|
|
2406 |
|
In questions (\ref{ques:upd}) and (\ref{ques:del}), please, use \textbf{COFFEE}.\(1\) to denote the first tuple in \textbf{COFFEE}, and similarly for other relations and tuples (so that, for instance \textbf{SUPPLY}.\(4\) corresponds to "Johns \& Co., \(221\)"). |
|
2407 |
|
|
|
2408 |
|
\begin{enumerate} |
|
2409 |
|
\item Determine if the following insertion statements would violate the |
|
2410 |
|
the \textbf{E}ntity integrity constraint, % ("primary key cannot be \texttt{NULL" and should be unique}) |
|
2411 |
|
the \textbf{R}eferential integrity constraint% ("the foreign key must refer to something that exists"), |
|
2412 |
|
, if there would be some \textbf{O}ther kind of error% (ignoring the plausability / revelance of inserting that tuple) |
|
2413 |
|
, or if it would result in \textbf{S}uccessful insertion. |
|
2414 |
|
|
|
2415 |
|
{ \renewcommand{\arraystretch}{2} |
|
2416 |
|
\begin{tabular}{l | c | c | c | c |} |
|
2417 |
|
& \textbf{E} & \textbf{R} & \textbf{O} & \textbf{S}\\ |
|
2418 |
|
\hline |
|
2419 |
|
`INSERT INTO CUSTOMER VALUES(005, 'Bob Hill', NULL, 001);` & & & & \\ \hline |
|
2420 |
|
`INSERT INTO COFFEE VALUES(002, "Peru", "Decaf", 3.00);` & & & & \\ \hline |
|
2421 |
|
`INSERT INTO PROVIDER VALUES(NULL, "contact@localcof.com");` & & & & \\ \hline |
|
2422 |
|
`INSERT INTO SUPPLY VALUES("Johns & Co.", 121);` & & & & \\ \hline |
|
2423 |
|
`INSERT INTO SUPPLY VALUES("Coffee Unl.", 311, 221);` & & & & \\ \hline |
|
2424 |
|
\end{tabular} |
|
2425 |
|
} |
|
2426 |
|
|
|
2427 |
|
\item Assuming that we cascade update statements, list the tuples modified by the following statements: |
|
2428 |
|
\begin{enumerate}[topsep=0pt,itemsep=4em,partopsep=1ex,parsep=1ex] |
|
2429 |
|
\item `UPDATE CUSTOMER SET FavCoffee = 001 WHERE CardNo = 001;` |
|
2430 |
|
\item `UPDATE COFFEE SET TypeOfRoast = 'Decaf' WHERE Origin = 'Brazil';` |
|
2431 |
|
\item `UPDATE PROVIDER SET Name = 'Coffee Unlimited' WHERE Name = 'Coffee Unl.';` |
|
2432 |
|
\item `UPDATE COFFEE SET PricePerPound = 10.00 WHERE PricePerPound > 10.00;` |
|
2433 |
|
\end{enumerate} |
|
2434 |
|
|
|
2435 |
|
\item Assuming that we cascade delete statements, list the tuples deleted by the following statements: |
|
2436 |
|
\begin{enumerate}[topsep=0pt,itemsep=4em,partopsep=1ex,parsep=1ex] |
|
2437 |
|
\item `DELETE FROM CUSTOMER WHERE Name LIKE '%S%';` |
|
2438 |
|
\item `DELETE FROM COFFEE WHERE Ref = 001;` |
|
2439 |
|
\item `DELETE FROM SUPPLY WHERE Provider = 'Coffee Unl.' AND Coffee = 001;` |
|
2440 |
|
\item `DELETE FROM PROVIDER WHERE Name = 'Johns & Co.';` |
|
2441 |
|
\end{enumerate} |
|
2442 |
|
|
|
2443 |
|
\item %Starting here, assume that there is more data in our table than what was given at the beginning of the problem. |
|
2444 |
|
Write queries that answer the following questions (the last two are bonus): |
|
2445 |
|
\begin{enumerate}[topsep=0pt,itemsep=6em,partopsep=1ex,parsep=1ex] |
|
2446 |
|
\item "What are the origins of your dark coffees?" |
|
2447 |
|
\item "What is the reference of Bob's favorite coffee?" (nota: it doesn't matter if you return the favorite coffee of all the Bobs in the database.) |
|
2448 |
|
\item "What are the names of the providers who didn't give their email?" |
|
2449 |
|
\item "How many coffees does Johns \& co. provide us with?" |
|
2450 |
|
\item "What are the names of the providers of my dark coffees?" |
|
2451 |
|
\end{enumerate} |
|
2452 |
|
|
|
2453 |
|
\end{enumerate} |
|
2454 |
|
|
|
2455 |
|
|
|
2456 |
|
|
|
2457 |
|
|
|
2458 |
|
|
|
2459 |
|
\begin{figure} |
|
2460 |
|
|
|
2461 |
|
\begin{multicols}{2} |
|
2462 |
|
\textbf{COFFEE} |
|
2463 |
|
|
|
2464 |
|
\begin{tabular}{| c | c | c | c | } |
|
2465 |
|
\hline |
|
2466 |
|
\rowcolor{gray!50} \underline{\textbf{Ref}} & \textbf{Origin} & \textbf{TypeOfRoast} & \textbf{PricePerPound}\\ \hline |
|
2467 |
|
\(001\) & Brazil & Light & 8.90\\ |
|
2468 |
|
\(121\) & Bolivia & Dark& 7.50\\ |
|
2469 |
|
\(311\) & Brazil & Medium & 9.00\\ |
|
2470 |
|
\(221\) & Sumatra & Dark & 10.25\\ \hline |
|
2471 |
|
\end{tabular} |
|
2472 |
|
\\[2em] |
|
2473 |
|
\textbf{CUSTOMER} |
|
2474 |
|
|
|
2475 |
|
\begin{tabular}{| c | c | c | c | } |
|
2476 |
|
\hline |
|
2477 |
|
\rowcolor{gray!50} \underline{\textbf{CardNo}} & \textbf{Name} & \textbf{Email} & \textbf{FavCoffee}\\ \hline |
|
2478 |
|
\(001\) & Bob Hill & b.hill@isp.net & 221\\ |
|
2479 |
|
\(002\) & Ana Swamp & swampa@nca.edu & 311\\ |
|
2480 |
|
\(003\) & Mary Sea & brig@gsu.gov & 121\\ |
|
2481 |
|
\(004\) & Pat Mount & pmount@fai.fr & 121\\ \hline |
|
2482 |
|
\end{tabular} |
|
2483 |
|
%\\[2em] |
|
2484 |
|
%\noindent |
|
2485 |
|
|
|
2486 |
|
\hspace{5em} |
|
2487 |
|
\textbf{SUPPLY} |
|
2488 |
|
|
|
2489 |
|
\hspace{5em} |
|
2490 |
|
\begin{tabular}{| c | c | } |
|
2491 |
|
\hline |
|
2492 |
|
\rowcolor{gray!50} \underline{\textbf{Provider}} & \underline{\textbf{Coffee}}\\ \hline |
|
2493 |
|
Coffee Unl. & \(001\) \\ |
|
2494 |
|
Coffee Unl. & \(121\) \\ |
|
2495 |
|
Coffee Exp. & \(311\) \\ |
|
2496 |
|
Johns \& Co. & \(221\) \\ \hline |
|
2497 |
|
\end{tabular} |
|
2498 |
|
\\[2em] |
|
2499 |
|
|
|
2500 |
|
\hspace{5em} |
|
2501 |
|
\textbf{PROVIDER} |
|
2502 |
|
|
|
2503 |
|
\hspace{5em} |
|
2504 |
|
\begin{tabular}{| c | c | } |
|
2505 |
|
\hline |
|
2506 |
|
\rowcolor{gray!50} \underline{\textbf{Name}} & \textbf{Email}\\ \hline |
|
2507 |
|
Coffee Unl. & bob@cofunl.com \\ |
|
2508 |
|
Coffee Exp. & pat@coffeex.dk \\ |
|
2509 |
|
Johns \& Co. & NULL \\ \hline |
|
2510 |
|
\end{tabular} |
|
2511 |
|
\end{multicols} |
|
2512 |
|
\caption{Relational Model and Data for the DB\_COFFEE Database} |
|
2513 |
|
\label{fig:coffee} |
|
2514 |
|
\end{figure} |
|
2515 |
|
|
|
2516 |
|
%% 09 -- Exam 2 |
|
2517 |
|
|
|
2518 |
|
|
|
2519 |
|
Problem +.# |
|
2520 |
|
Consider the relation |
|
2521 |
|
|
|
2522 |
|
\begin{center} |
|
2523 |
|
CONTACT(Phone, Call\_center, Email, Zip, Brand, Website) |
|
2524 |
|
\end{center} |
|
2525 |
|
|
|
2526 |
|
and the following functional dependencies: |
|
2527 |
|
|
|
2528 |
|
\begin{center} |
|
2529 |
|
\begin{tabular}{r c l} |
|
2530 |
|
\{Zip, Brand\} & \(\to\) & \{Phone\}\\ |
|
2531 |
|
\{Brand\} & \(\to\) & \{Email\}\\ |
|
2532 |
|
\{ Brand\} & \(\to\) & \{Website\}\\ |
|
2533 |
|
\{Phone\} & \(\to\) & \{Call\_center\} |
|
2534 |
|
\end{tabular} |
|
2535 |
|
\end{center} |
|
2536 |
|
|
|
2537 |
|
Assume that \{Zip, Brand\} is the primary key. |
|
2538 |
|
Normalize this relation to the second normal form, and \emph{then} to the third normal form. |
|
2539 |
|
Give the relations, their primary keys, and functional dependencies for both steps. |
|
2540 |
|
|
|
2541 |
|
|
|
2542 |
|
|
|
2543 |
|
|
|
2544 |
|
Problem +.# |
|
2545 |
|
|
|
2546 |
|
Consider the relation |
|
2547 |
|
|
|
2548 |
|
\begin{center} |
|
2549 |
|
CONSULTATION(Doctor\_no, Patient\_no, Date, Diagnosis, Treatment, Charge, Insurance) |
|
2550 |
|
\end{center} |
|
2551 |
|
|
|
2552 |
|
with the following functional dependencies: |
|
2553 |
|
|
|
2554 |
|
\begin{center} |
|
2555 |
|
\begin{tabular}{r c l} |
|
2556 |
|
\{Doctor\_no, Patient\_no, Date\} & \(\to\) & \{Diagnosis\}\\ |
|
2557 |
|
\{Doctor\_no, Patient\_no, Date\} & \(\to\) & \{Treatment\}\\ |
|
2558 |
|
\{Treatment, Insurance\} & \(\to\) & \{Charge\}\\ |
|
2559 |
|
\{Patient\_no\} & \(\to\) & \{Insurance\} |
|
2560 |
|
\end{tabular} |
|
2561 |
|
\end{center} |
|
2562 |
|
|
|
2563 |
|
\begin{enumerate}[topsep=0pt,itemsep=10em,partopsep=1ex,parsep=1ex] |
|
2564 |
|
\item The designer decided not to add the functional dependency \{Diagnosis\} $\to$ \{Treatment\}. |
|
2565 |
|
Explain what could be the designer's justification, at the level of the mini-world. |
|
2566 |
|
\item Identify a primary key for this relation. |
|
2567 |
|
\item What is the degree of normalization of this relation? |
|
2568 |
|
Normalize it to the third normal form if necessary. |
|
2569 |
|
\end{enumerate} |
|
2570 |
|
|
|
2571 |
|
|
|
2572 |
|
|
|
2573 |
|
Solution +.# |
|
2574 |
|
|
|
2575 |
|
Answer: |
|
2576 |
|
Because there are no partial dependencies, the given relation is in 2NF already. This however is not 3NF because the Charge is a nonkey attribute that is determined by another nonkey attribute, Treatment. We must decompose further: |
|
2577 |
|
|
|
2578 |
|
R (Doctor\_no, Patient\_no, Date, Diagnosis, Treatment) |
|
2579 |
|
|
|
2580 |
|
R1 (Treatment, Charge) |
|
2581 |
|
|
|
2582 |
|
We could further infer that the treatment for a given diagnosis is functionally dependant, but we should be sure to allow the doctor to have some flexibility when prescribing cures. |
|
2583 |
|
|
|
2584 |
|
|
|
2585 |
|
Problem +.# |
|
2586 |
|
A friend of yours want you to review and improve the code for a role-playing game. |
|
2587 |
|
|
|
2588 |
|
The original idea was that each character should have a name, a class (e.g., Bard, Assassin, Druid), a certain amount of experience, a level, one or more weapons (providing bonuses) and can complete quests. |
|
2589 |
|
A quest have a name, and rewards the characters that completed it with a certain amount of experience, and sometimes (but rarely) with a special item. |
|
2590 |
|
|
|
2591 |
|
Your friend came up with the code presented in \autoref{lst:RPG}, page~\pageref{lst:RPG}, but there are several problems: |
|
2592 |
|
|
|
2593 |
|
\begin{itemize} |
|
2594 |
|
\item As of now, a character can have only one weapon. |
|
2595 |
|
All the attempts to "hack" the `CHARACTER` table to add an arbitrary number of weapons ended up creating horrible messes. |
|
2596 |
|
\item Every time a character completes a quest, a copy of the quest must be created. |
|
2597 |
|
Your friend is not so sure why, but nothing else works. |
|
2598 |
|
Also it seems that a character can complete only one quest, but your friend is not so sure about that. |
|
2599 |
|
\item It would be nice to be able to store features that are tied to the class, and not to the character, like the bonus they provide and the associated element (e.g., all bards use fire, all assassins use wind, etc.). |
|
2600 |
|
But you friend simply can't figure out how to do that. |
|
2601 |
|
\end{itemize} |
|
2602 |
|
|
|
2603 |
|
Can you provide a \emph{relational database schema} (no need to write the `SQL` code, but remember to indicate the primary and foreign keys) that would solve all of your friend's troubles? |
|
2604 |
|
|
|
2605 |
|
|
|
2606 |
|
|
|
2607 |
|
|
|
2608 |
|
|
|
2609 |
|
|
|
2610 |
|
Problem +.# |
|
2611 |
|
This exercise asks you to convert business statements into dependencies. |
|
2612 |
|
Consider the following relation: |
|
2613 |
|
\begin{center} |
|
2614 |
|
KEYBOARD(Manufacturer, Model, Layout, Retail\_Store, Price) |
|
2615 |
|
\end{center} |
|
2616 |
|
|
|
2617 |
|
A tuple in the KEYBOARD relation contains information about a computer keyboard: its manufacturer, its model, its layout (AZERTY, QWERTY, etc.), the place where it is sold, and its price. |
|
2618 |
|
|
|
2619 |
|
\begin{enumerate} |
|
2620 |
|
\item Write each of the following business statement as a functional dependency: |
|
2621 |
|
|
|
2622 |
|
\begin{enumerate}[topsep=0pt,itemsep=7em,partopsep=1ex,parsep=1ex] |
|
2623 |
|
\item A model has a fixed layout. |
|
2624 |
|
\item A retail store can't have two different models produced by the same manufacturer. |
|
2625 |
|
\end{enumerate} |
|
2626 |
|
|
|
2627 |
|
\item Based on those statements, what could be a key for this relation? |
|
2628 |
|
\item Assuming all those functional dependencies hold, and taking the primary key you identified at the previous step, what is the degree of normality of this relation? Justify your answer. |
|
2629 |
|
\end{enumerate} |
|
2630 |
|
|
|
2631 |
|
|
|
2632 |
|
|
|
2633 |
|
|
|
2634 |
|
Problem +.# |
|
2635 |
|
Consider the E.R. schema of Figure~\ref{fig:erCOUNTRY}, p.~\pageref{fig:erCOUNTRY}. |
|
2636 |
|
Map that E.-R. diagram to a relational database schema. |
|
2637 |
|
|
|
2638 |
|
|
|
2639 |
|
|
|
2640 |
|
\begin{figure} |
|
2641 |
|
%\begin{listing} |
|
2642 |
|
\begin{minted}[linenos, breaklines=true]{mysql} |
|
2643 |
|
CREATE TABLE CHARACTER( |
|
2644 |
|
Name VARCHAR(30) PRIMARY KEY, |
|
2645 |
|
Class VARCHAR(30), |
|
2646 |
|
XP INT, |
|
2647 |
|
LVL INT, |
|
2648 |
|
Weapon_Name VARCHAR(30), |
|
2649 |
|
Weapon_Bonus INT, |
|
2650 |
|
Quest_Completed VARCHAR(30) |
|
2651 |
|
); |
|
2652 |
|
|
|
2653 |
|
CREATE TABLE QUEST( |
|
2654 |
|
Id VARCHAR(20) PRIMARY KEY, |
|
2655 |
|
Completed_By VARCHAR(30), |
|
2656 |
|
XP_Gained INT, |
|
2657 |
|
Special_Item VARCHAR(20), |
|
2658 |
|
FOREIGN KEY (Completed_By) REFERENCES CHARACTER(Name) |
|
2659 |
|
); |
|
2660 |
|
|
|
2661 |
|
ALTER TABLE CHARACTER ADD FOREIGN KEY (Quest_Completed) REFERENCES QUEST(Id); |
|
2662 |
|
``` |
|
2663 |
|
|
|
2664 |
|
|
|
2665 |
|
%\end{listing} |
|
2666 |
|
\caption{A `SQL` Code for a Role-Playing Game} |
|
2667 |
|
\label{lst:RPG} |
|
2668 |
|
\end{figure} |
|
2669 |
|
|
|
2670 |
|
\begin{figure} |
|
2671 |
|
\begin{tikzpicture}[node distance=8em] |
|
2672 |
|
\node[entity] (COUNTRY) {COUNTRY}; |
|
2673 |
|
\node[attribute] (name) [left of=COUNTRY] {\key{Name}} edge (COUNTRY); |
|
2674 |
|
\node[attribute] (population) [below left of=COUNTRY] {Population} edge (COUNTRY); |
|
2675 |
|
|
|
2676 |
|
\node[relationship] (SPEAKS) [below right of=COUNTRY] {SPEAKS} edge node[right, pos=0.4] {$M$} (COUNTRY); |
|
2677 |
|
\node[entity] (LANGUAGE) [below right of=SPEAKS] {LANGUAGE} edge node[right, pos=0.5] {$N$} (SPEAKS); |
|
2678 |
|
\node[attribute] (code) [left of = LANGUAGE] {\key{Code}} edge (LANGUAGE); |
|
2679 |
|
\node[attribute] (symbol) [right of = LANGUAGE] {Name} edge (LANGUAGE); |
|
2680 |
|
|
|
2681 |
|
\node[relationship] (BWF) [below of = LANGUAGE] {B\_W\_F}; |
|
2682 |
|
\draw (LANGUAGE) to node[left, pos=0.6] {$N$} (BWF.west); |
|
2683 |
|
\draw (LANGUAGE) to node[right, pos=0.6] {$M$} (BWF.east); |
|
2684 |
|
|
|
2685 |
|
%\node[relationship] (boundary) [above of = COUNTRY] {S\_B\_W}; |
|
2686 |
|
%\draw (COUNTRY) to node[left, pos=0.6] {$N$} (boundary.west); |
|
2687 |
|
%\draw (COUNTRY) to node[right, pos=0.6] {$M$} (boundary.east); |
|
2688 |
|
|
|
2689 |
|
%\node[relationship] (conversion) [below of =LANGUAGE] {CONVERSION}; |
|
2690 |
|
%\draw (LANGUAGE) to node[left, pos=0.6] {$N$} (conversion.west); |
|
2691 |
|
%\draw (LANGUAGE) to node[right, pos=0.6] {$M$} (conversion.east); |
|
2692 |
|
|
|
2693 |
|
%\node[attribute] (timestamp) [below left of=conversion] {\key{Timestamp}} edge (conversion); |
|
2694 |
|
%\node[attribute] (rate) [below right of=conversion] {Exchange\_rate} edge (conversion); |
|
2695 |
|
|
|
2696 |
|
\node[ident relationship] (hasfor) [right of=COUNTRY] {SINGS} edge node[above, pos=0.4] {$1$} (COUNTRY); |
|
2697 |
|
\node[weak entity] (anthem) %[right of=hasfor] |
|
2698 |
|
[right = 1cm of hasfor] {NATIONAL\_ANTHEM} edge[total] node[above, pos=0.6]{\(M\)} (hasfor); |
|
2699 |
|
\node[multi attribute] (color) [right =1cm of anthem] {Creator} edge (anthem); |
|
2700 |
|
\node[attribute] (name) [below right of = anthem] {\pkey{Name}} edge (anthem); |
|
2701 |
|
|
|
2702 |
|
\node[relationship] (WIN) [above right of =LANGUAGE] {W\_IN}; |
|
2703 |
|
\draw (LANGUAGE) to node[left, pos=0.6] {$N$} (WIN); |
|
2704 |
|
\draw (anthem) to node[right, pos=0.6] {$M$} (WIN); |
|
2705 |
|
\end{tikzpicture} |
|
2706 |
|
|
|
2707 |
|
%"S\_B\_W" stands for "SHARES\_A\_BORDER\_WITH". |
|
2708 |
|
"W\_IN" stands for "WRITTEN\_IN", and |
|
2709 |
|
"B\_W\_F" stands for "BORROWS\_WORDS\_FROM". |
|
2710 |
|
For this relationship, on the left-hand side is the language that borrows a word, and on the right-hand side is the language that provides the loanword. |
|
2711 |
|
\caption{E.R. Schema for the COUNTRY\_INFO database} |
|
2712 |
|
\label{fig:erCOUNTRY} |
|
2713 |
|
\end{figure} |
|
2714 |
|
|
|
2715 |
|
%%% 10 Final |
|
2716 |
|
|
|
2717 |
|
|
|
2718 |
|
Problem +.# |
|
2719 |
|
Look at the relational model from Figure~\ref{fig:RelMod}, p.~\pageref{fig:RelMod}, and "reverse-engineer" it to obtain an E.-R. diagram. |
|
2720 |
|
|
|
2721 |
|
|
|
2722 |
|
|
|
2723 |
|
|
|
2724 |
|
Problem +.# |
|
2725 |
|
Answer the following in one or two sentences: |
|
2726 |
|
|
|
2727 |
|
\begin{enumerate}[topsep=0pt,itemsep=12em,partopsep=1ex,parsep=1ex] |
|
2728 |
|
\item What is deletion anomaly? Is it a desirable feature? |
|
2729 |
|
\item What is polyglot persistence? Is it useful? |
|
2730 |
|
\item What does it mean to be "schemaless"? What does it imply? |
|
2731 |
|
\item What is denormalization? |
|
2732 |
|
When could that be useful? |
|
2733 |
|
\item What is the (object-relational) impedance mismatch? Is it an issue that can't be overcome? |
|
2734 |
|
\end{enumerate} |
|
2735 |
|
|
|
2736 |
|
|
|
2737 |
|
Solution +.# |
|
2738 |
|
|
|
2739 |
|
A Delete Anomaly exists when certain attributes are lost because of the deletion of other attributes. |
|
2740 |
|
|
|
2741 |
|
When storing data, it is best to use multiple data storage technologies, chosen based upon the way data is being used by individual applications or components of a single application. |
|
2742 |
|
|
|
2743 |
|
|
|
2744 |
|
|
|
2745 |
|
|
|
2746 |
|
|
|
2747 |
|
Problem +.# |
|
2748 |
|
Consider the following relation: |
|
2749 |
|
\begin{center} |
|
2750 |
|
FLIGHT(From, \qquad To, \qquad Airline, \qquad Flight\#, \qquad DateHour, \qquad HeadQuarter, \qquad Pilot, \qquad TZDifference) |
|
2751 |
|
\end{center} |
|
2752 |
|
|
|
2753 |
|
A tuple in the FLIGHT relation contains information about an airplane flight: the airports of departure and arrival, the airline carrier, the number of the flight, its time of departure, the headquarter of the company chartering the flight, the name of the pilot(s), and the time zone difference between the departure and arrival airports. |
|
2754 |
|
|
|
2755 |
|
The "Pilot" attribute is multi-valued (so that between \(1\) and \(4\) pilot's names can be stored in it). |
|
2756 |
|
Given an airline and a flight number, one can determine the departure and arrival airports, as well as the date and hour and the pilot(s). |
|
2757 |
|
Given the airline carrier, one can determine the headquarter. |
|
2758 |
|
Finally, given the departure and arrival airports, one can determine their time zone difference. |
|
2759 |
|
|
|
2760 |
|
Normalize the "FLIGHT" relation to its third normal form. |
|
2761 |
|
You can indicate your steps, justify your reasoning, and indicate the foreign keys if you want to, but don't have to. |
|
2762 |
|
|
|
2763 |
|
|
|
2764 |
|
|
|
2765 |
|
|
|
2766 |
|
Problem +.# |
|
2767 |
|
Consider the set of requirements in Figure~\ref{fig:reqUNIV}, p.~\pageref{fig:reqUNIV}. |
|
2768 |
|
Draw an E.-R. diagram for that schema. |
|
2769 |
|
Specify key attributes of each entity type and structural constraints on each relationship type. |
|
2770 |
|
Note any unspecified requirements, and make appropriate assumptions to make the specification complete. |
|
2771 |
|
|
|
2772 |
|
|
|
2773 |
|
|
|
2774 |
|
Problem +.# |
|
2775 |
|
Consider the UML diagram in Figure~\ref{fig:UML}, p.~\pageref{fig:UML}, and convert it to the relational model. |
|
2776 |
|
Don't forget to indicate primary and foreign keys. |
|
2777 |
|
|
|
2778 |
|
|
|
2779 |
|
|
|
2780 |
|
\begin{figure} |
|
2781 |
|
\colorlet{lightgray}{gray!20} |
|
2782 |
|
|
|
2783 |
|
\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} |
|
2784 |
|
|
|
2785 |
|
% RELATIONS |
|
2786 |
|
|
|
2787 |
|
\node (actortitle) {\textbf{ACTOR}}; |
|
2788 |
|
|
|
2789 |
|
\node [relation=3, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of actortitle.west, anchor=west] (actor) |
|
2790 |
|
{\underline{Id}% |
|
2791 |
|
\nodepart{two} Name |
|
2792 |
|
\nodepart{three} Birthdate |
|
2793 |
|
}; |
|
2794 |
|
|
|
2795 |
|
\node [below=1.3cm of actor.west, anchor=west] (movietitle) {\textbf{MOVIE}}; |
|
2796 |
|
|
|
2797 |
|
\node [relation=4, rectangle split horizontal, rectangle split part fill={lightgray!50}, below=0.6cm of movietitle.west, anchor=west] (movie) |
|
2798 |
|
{\underline{Title}% |
|
2799 |
|
\nodepart{two} Year |
|
2800 |
|
\nodepart{three} Length |
|
2801 |
|
\nodepart{four} Studio |
|
2802 |
|
}; |
|
2803 |
|
|
|
2804 |
|
|
|
2805 |
|
\node [right=8cm of actortitle.west, anchor=west] (actingtitle) {\textbf{ACTING}}; |
|
2806 |
|
|
|
2807 |
|
\node [relation=2, rectangle split horizontal, rectangle split part fill={lightgray!50}, below=0.6cm of actingtitle.west, anchor=west] (acting) |
|
2808 |
|
{\underline{ActorID}% |
|
2809 |
|
\nodepart{two} \underline{MovieTitle}% |
|
2810 |
|
}; |
|
2811 |
|
|
|
2812 |
|
\node [below=1.3cm of movie.west, anchor=west] (theatretitle) {\textbf{THEATRE}}; |
|
2813 |
|
|
|
2814 |
|
\node [relation=5, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of theatretitle.west, anchor=west] (theatre) |
|
2815 |
|
{\underline{Name}% |
|
2816 |
|
\nodepart{two} Street |
|
2817 |
|
\nodepart{three} City |
|
2818 |
|
\nodepart{four} State |
|
2819 |
|
\nodepart{five} Zip |
|
2820 |
|
}; |
|
2821 |
|
|
|
2822 |
|
\node [below=1.3cm of acting.west, anchor=west] (showingtitle) {\textbf{SHOWING}}; |
|
2823 |
|
|
|
2824 |
|
\node [relation=4, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of showingtitle.west, anchor=west] (showing) |
|
2825 |
|
{\underline{TheaterName}% |
|
2826 |
|
\nodepart{two} \underline{MovieTitle} |
|
2827 |
|
\nodepart{three} \underline{Day} |
|
2828 |
|
\nodepart{four} \underline{Time} |
|
2829 |
|
}; |
|
2830 |
|
|
|
2831 |
|
|
|
2832 |
|
% FOREIGN KEYS |
|
2833 |
|
|
|
2834 |
|
%\draw[-latex] (acting) -- -| (actor.one south);%(actor.one south) -- ++(0,-0.2) --++(7,0) --++(0, -2) -| ($(movie.one south) + (0.2,0)$); |
|
2835 |
|
\draw[-latex] ($(showing.two south) + (-0.3,0)$)-- ++(0,-0.3) -| (movie.one south); |
|
2836 |
|
\draw[-latex] (showing.one south) -- ++ (0,-2.6) -| (theatre.one south); |
|
2837 |
|
\draw[-latex] (acting.one south) -- ++ (0,-0.3) -| (actor.one south); |
|
2838 |
|
\draw[-latex] (acting.two south) -- ++ (0,-0.5) -- ++(-3, 0) -- ++(0, -2.1)-| ($(movie.one south) + (-0.25,0)$); |
|
2839 |
|
%\draw[-latex] (showing.four south) -- ++(0,-0.2) --++(2,0) --++(0, 1.75) -| ($(theatre.one south) + (0.1,0)$); |
|
2840 |
|
%\draw[-latex] (theatre.five south) -- ++(0,-0.2) --++(2,0) --++(0, 1.5) -| ($(movie.one south) + (-0.1,0)$); |
|
2841 |
|
\end{tikzpicture} |
|
2842 |
|
\caption{Relational Model for a MOVIE Database} |
|
2843 |
|
\label{fig:RelMod} |
|
2844 |
|
\end{figure} |
|
2845 |
|
|
|
2846 |
|
\begin{figure} |
|
2847 |
|
Consider the following requirements for a UNIVERSITY database, used to keep track of students' transcripts. |
|
2848 |
|
\begin{enumerate} |
|
2849 |
|
\item The university keeps track of each student's name, student number, class (freshman, sophomore, …, graduate), major department, minor department (if any), and degree program (B.A., B.S., …, Ph.D.). |
|
2850 |
|
Student number has unique values for each student. |
|
2851 |
|
\item Each department is described by a name and has a (unique) department code. |
|
2852 |
|
\item Each course has a course name, a course number, credit hours, and is offered by at least one department. The value of course number is unique for each course. A course has at least one section. |
|
2853 |
|
\item Each section of a course has an instructor, a semester, a year, and a section number. |
|
2854 |
|
The section number distinguishes different sections of the same course that are taught during the same semester/year; its values are 1, 2, 3, …, up to the number of sections taught during each semester. Students can enroll in sections and receive a letter grade, and grade point (0, 1, 2, 3, 4 for F, D, C, B, A, respectively). |
|
2855 |
|
\end{enumerate} |
|
2856 |
|
\caption{Requirements for a UNIVERSITY database} |
|
2857 |
|
\label{fig:reqUNIV} |
|
2858 |
|
\end{figure} |
|
2859 |
|
|
|
2860 |
|
\begin{figure} |
|
2861 |
|
\begin{tikzpicture}[scale = 0.8] |
|
2862 |
|
\node (movie) at (0,0){\begin{tabular}{| r l |} |
|
2863 |
|
\hline |
|
2864 |
|
\multicolumn{2}{| c |}{\textbf{DRIVER}}\\ |
|
2865 |
|
\hline |
|
2866 |
|
id &: String\\ |
|
2867 |
|
dob &: Date\\ |
|
2868 |
|
name &: String\\ |
|
2869 |
|
address &: Street\\ |
|
2870 |
|
& City\\ |
|
2871 |
|
\hline |
|
2872 |
|
getAge()&: Int\\ |
|
2873 |
|
\hline |
|
2874 |
|
\end{tabular} |
|
2875 |
|
}; |
|
2876 |
|
\node (cdriver) at (8, 1){\begin{tabular}{| r l |} |
|
2877 |
|
\hline |
|
2878 |
|
\multicolumn{2}{| c |}{\textbf{COMMERCIAL\_DRIVER}}\\ |
|
2879 |
|
\hline |
|
2880 |
|
Class &: String\\ |
|
2881 |
|
\hline |
|
2882 |
|
\end{tabular} |
|
2883 |
|
}; |
|
2884 |
|
%\draw[open diamond-] (movie) -- node[above, pos=0.1]{\(0..\star\)} node[above, pos=0.9]{\(1..1\)} (cdriver); |
|
2885 |
|
\draw[{Triangle[open]}-] (movie) -- %node[above, pos=0.1]{\(0..\star\)} node[above, pos=0.9]{\(1..1\)} |
|
2886 |
|
(cdriver); |
|
2887 |
|
\node (car) at (9, -2){\begin{tabular}{| r l |} |
|
2888 |
|
\hline |
|
2889 |
|
\multicolumn{2}{| c |}{\textbf{CAR}}\\ |
|
2890 |
|
\hline |
|
2891 |
|
vin &: String\\ |
|
2892 |
|
make &: String\\ |
|
2893 |
|
year &: Year\\ |
|
2894 |
|
brand &: String\\ |
|
2895 |
|
\hline |
|
2896 |
|
\end{tabular} |
|
2897 |
|
}; |
|
2898 |
|
\draw[-] ($(movie)+(2.5,-0.5)$) -- node[above, pos=0.1]{\(0..*\)} node[above=0.5em]{POSSESSES} node[above, pos=0.9]{\(0..*\)} (car); |
|
2899 |
|
%\draw[-] (movie) -- node[below, pos=0.1]{\(0..1\)} node[below, pos=0.9]{\(0..4\)} ($(car)+(-2.5, 0.5)$); |
|
2900 |
|
\node (seat) at (3.5, -5){\begin{tabular}{| r l |} |
|
2901 |
|
\hline |
|
2902 |
|
\multicolumn{2}{| c |}{\textbf{POSSESSION\_HISTORY}}\\ |
|
2903 |
|
\hline |
|
2904 |
|
date of purchase &: Date\\ |
|
2905 |
|
\hline |
|
2906 |
|
\end{tabular} |
|
2907 |
|
}; |
|
2908 |
|
\draw[dashed] (seat) -- (3.5, -1.2); |
|
2909 |
|
|
|
2910 |
|
\node (insu) at (15.5, 2){\begin{tabular}{| r l |} |
|
2911 |
|
\hline |
|
2912 |
|
\multicolumn{2}{| c |}{\textbf{CAR\_INSURANCE}}\\ |
|
2913 |
|
\hline |
|
2914 |
|
policy number &: String\\ |
|
2915 |
|
covered amount &: int\\ |
|
2916 |
|
compagny name &: String\\ |
|
2917 |
|
\hline |
|
2918 |
|
\end{tabular} |
|
2919 |
|
}; |
|
2920 |
|
|
|
2921 |
|
\draw[diamond -] (car) -- node[below, pos=0.1, right=.2]{\(1..1\)} node[below, pos=0.9, right=0.2]{\(1..\star\)} node[right=0.2]{IS\_COVERED\_BY} (insu); |
|
2922 |
|
\end{tikzpicture} |
|
2923 |
|
\caption{UML Diagram for the CAR\_POSSESSION Database} |
|
2924 |
|
\label{fig:UML} |
|
2925 |
|
\end{figure} |
|
2926 |
|
|
|
2927 |
|
\end{document} |
|
2928 |
|
|
|
2929 |
|
|
|
2930 |
|
|
|
2931 |
|
Problem +.# |
|
2932 |
|
|
|
2933 |
|
Consider the relation |
|
2934 |
|
|
|
2935 |
|
\begin{center} |
|
2936 |
|
CONSULTATION(Doctor\_no, Patient\_no, Date, Diagnosis, Treatment, Charge, Insurance) |
|
2937 |
|
\end{center} |
|
2938 |
|
|
|
2939 |
|
with the following functional dependencies: |
|
2940 |
|
|
|
2941 |
|
\begin{center} |
|
2942 |
|
\begin{tabular}{r c l} |
|
2943 |
|
\{Doctor\_no, Patient\_no, Date\} & \(\to\) & \{Diagnosis\}\\ |
|
2944 |
|
\{Doctor\_no, Patient\_no, Date\} & \(\to\) & \{Treatment\}\\ |
|
2945 |
|
\{Treatment, Insurance\} & \(\to\) & \{Charge\}\\ |
|
2946 |
|
\{Patient\_no\} & \(\to\) & \{Insurance\} |
|
2947 |
|
\end{tabular} |
|
2948 |
|
\end{center} |
|
2949 |
|
|
|
2950 |
|
\begin{enumerate}[topsep=0pt,itemsep=10em,partopsep=1ex,parsep=1ex] |
|
2951 |
|
\item The designer decided not to add the functional dependency \{Diagnosis\} $\to$ \{Treatment\}. |
|
2952 |
|
Explain what could be the designer's justification, at the level of the mini-world. |
|
2953 |
|
\item Identify a primary key for this relation. |
|
2954 |
|
\item What is the degree of normalization of this relation? |
|
2955 |
|
Normalize it to the third normal form if necessary. |
|
2956 |
|
\end{enumerate} |
|
2957 |
|
|
|
2958 |
|
|
|
2959 |
|
|
|
2960 |
|
Solution +.# |
|
2961 |
|
|
|
2962 |
|
Answer: |
|
2963 |
|
Because there are no partial dependencies, the given relation is in 2NF already. This however is not 3NF because the Charge is a nonkey attribute that is determined by another nonkey attribute, Treatment. We must decompose further: |
|
2964 |
|
|
|
2965 |
|
R (Doctor\_no, Patient\_no, Date, Diagnosis, Treatment) |
|
2966 |
|
|
|
2967 |
|
R1 (Treatment, Charge) |
|
2968 |
|
|
|
2969 |
|
We could further infer that the treatment for a given diagnosis is functionally dependant, but we should be sure to allow the doctor to have some flexibility when prescribing cures. |
|
2970 |
|
|
|
2971 |
|
|
|
2972 |
|
Problem +.# |
|
2973 |
|
A friend of yours want you to review and improve the code for a role-playing game. |
|
2974 |
|
|
|
2975 |
|
The original idea was that each character should have a name, a class (e.g., Bard, Assassin, Druid), a certain amount of experience, a level, one or more weapons (providing bonuses) and can complete quests. |
|
2976 |
|
A quest have a name, and rewards the characters that completed it with a certain amount of experience, and sometimes (but rarely) with a special item. |
|
2977 |
|
|
|
2978 |
|
Your friend came up with the code presented in \autoref{lst:RPG}, page~\pageref{lst:RPG}, but there are several problems: |
|
2979 |
|
|
|
2980 |
|
\begin{itemize} |
|
2981 |
|
\item As of now, a character can have only one weapon. |
|
2982 |
|
All the attempts to "hack" the `CHARACTER` table to add an arbitrary number of weapons ended up creating horrible messes. |
|
2983 |
|
\item Every time a character completes a quest, a copy of the quest must be created. |
|
2984 |
|
Your friend is not so sure why, but nothing else works. |
|
2985 |
|
Also it seems that a character can complete only one quest, but your friend is not so sure about that. |
|
2986 |
|
\item It would be nice to be able to store features that are tied to the class, and not to the character, like the bonus they provide and the associated element (e.g., all bards use fire, all assassins use wind, etc.). |
|
2987 |
|
But you friend simply can't figure out how to do that. |
|
2988 |
|
\end{itemize} |
|
2989 |
|
|
|
2990 |
|
Can you provide a \emph{relational database schema} (no need to write the `SQL` code, but remember to indicate the primary and foreign keys) that would solve all of your friend's troubles? |
|
2991 |
|
|
|
2992 |
|
|
|
2993 |
|
|
|
2994 |
|
|
|
2995 |
|
|
|
2996 |
|
|
|
2997 |
|
Problem +.# |
|
2998 |
|
This exercise asks you to convert business statements into dependencies. |
|
2999 |
|
Consider the following relation: |
|
3000 |
|
\begin{center} |
|
3001 |
|
KEYBOARD(Manufacturer, Model, Layout, Retail\_Store, Price) |
|
3002 |
|
\end{center} |
|
3003 |
|
|
|
3004 |
|
A tuple in the KEYBOARD relation contains information about a computer keyboard: its manufacturer, its model, its layout (AZERTY, QWERTY, etc.), the place where it is sold, and its price. |
|
3005 |
|
|
|
3006 |
|
\begin{enumerate} |
|
3007 |
|
\item Write each of the following business statement as a functional dependency: |
|
3008 |
|
|
|
3009 |
|
\begin{enumerate}[topsep=0pt,itemsep=7em,partopsep=1ex,parsep=1ex] |
|
3010 |
|
\item A model has a fixed layout. |
|
3011 |
|
\item A retail store can't have two different models produced by the same manufacturer. |
|
3012 |
|
\end{enumerate} |
|
3013 |
|
|
|
3014 |
|
\item Based on those statements, what could be a key for this relation? |
|
3015 |
|
\item Assuming all those functional dependencies hold, and taking the primary key you identified at the previous step, what is the degree of normality of this relation? Justify your answer. |
|
3016 |
|
\end{enumerate} |
|
3017 |
|
|
|
3018 |
|
|
|
3019 |
|
|
|
3020 |
|
|
|
3021 |
|
Problem +.# |
|
3022 |
|
Consider the E.R. schema of Figure~\ref{fig:erCOUNTRY}, p.~\pageref{fig:erCOUNTRY}. |
|
3023 |
|
Map that E.-R. diagram to a relational database schema. |
|
3024 |
|
|
|
3025 |
|
|
|
3026 |
|
|
|
3027 |
|
\begin{figure} |
|
3028 |
|
%\begin{listing} |
|
3029 |
|
\begin{minted}[linenos, breaklines=true]{mysql} |
|
3030 |
|
CREATE TABLE CHARACTER( |
|
3031 |
|
Name VARCHAR(30) PRIMARY KEY, |
|
3032 |
|
Class VARCHAR(30), |
|
3033 |
|
XP INT, |
|
3034 |
|
LVL INT, |
|
3035 |
|
Weapon_Name VARCHAR(30), |
|
3036 |
|
Weapon_Bonus INT, |
|
3037 |
|
Quest_Completed VARCHAR(30) |
|
3038 |
|
); |
|
3039 |
|
|
|
3040 |
|
CREATE TABLE QUEST( |
|
3041 |
|
Id VARCHAR(20) PRIMARY KEY, |
|
3042 |
|
Completed_By VARCHAR(30), |
|
3043 |
|
XP_Gained INT, |
|
3044 |
|
Special_Item VARCHAR(20), |
|
3045 |
|
FOREIGN KEY (Completed_By) REFERENCES CHARACTER(Name) |
|
3046 |
|
); |
|
3047 |
|
|
|
3048 |
|
ALTER TABLE CHARACTER ADD FOREIGN KEY (Quest_Completed) REFERENCES QUEST(Id); |
|
3049 |
|
``` |
|
3050 |
|
|
|
3051 |
|
|
|
3052 |
|
%\end{listing} |
|
3053 |
|
\caption{A `SQL` Code for a Role-Playing Game} |
|
3054 |
|
\label{lst:RPG} |
|
3055 |
|
\end{figure} |
|
3056 |
|
|
|
3057 |
|
\begin{figure} |
|
3058 |
|
\begin{tikzpicture}[node distance=8em] |
|
3059 |
|
\node[entity] (COUNTRY) {COUNTRY}; |
|
3060 |
|
\node[attribute] (name) [left of=COUNTRY] {\key{Name}} edge (COUNTRY); |
|
3061 |
|
\node[attribute] (population) [below left of=COUNTRY] {Population} edge (COUNTRY); |
|
3062 |
|
|
|
3063 |
|
\node[relationship] (SPEAKS) [below right of=COUNTRY] {SPEAKS} edge node[right, pos=0.4] {$M$} (COUNTRY); |
|
3064 |
|
\node[entity] (LANGUAGE) [below right of=SPEAKS] {LANGUAGE} edge node[right, pos=0.5] {$N$} (SPEAKS); |
|
3065 |
|
\node[attribute] (code) [left of = LANGUAGE] {\key{Code}} edge (LANGUAGE); |
|
3066 |
|
\node[attribute] (symbol) [right of = LANGUAGE] {Name} edge (LANGUAGE); |
|
3067 |
|
|
|
3068 |
|
\node[relationship] (BWF) [below of = LANGUAGE] {B\_W\_F}; |
|
3069 |
|
\draw (LANGUAGE) to node[left, pos=0.6] {$N$} (BWF.west); |
|
3070 |
|
\draw (LANGUAGE) to node[right, pos=0.6] {$M$} (BWF.east); |
|
3071 |
|
|
|
3072 |
|
%\node[relationship] (boundary) [above of = COUNTRY] {S\_B\_W}; |
|
3073 |
|
%\draw (COUNTRY) to node[left, pos=0.6] {$N$} (boundary.west); |
|
3074 |
|
%\draw (COUNTRY) to node[right, pos=0.6] {$M$} (boundary.east); |
|
3075 |
|
|
|
3076 |
|
%\node[relationship] (conversion) [below of =LANGUAGE] {CONVERSION}; |
|
3077 |
|
%\draw (LANGUAGE) to node[left, pos=0.6] {$N$} (conversion.west); |
|
3078 |
|
%\draw (LANGUAGE) to node[right, pos=0.6] {$M$} (conversion.east); |
|
3079 |
|
|
|
3080 |
|
%\node[attribute] (timestamp) [below left of=conversion] {\key{Timestamp}} edge (conversion); |
|
3081 |
|
%\node[attribute] (rate) [below right of=conversion] {Exchange\_rate} edge (conversion); |
|
3082 |
|
|
|
3083 |
|
\node[ident relationship] (hasfor) [right of=COUNTRY] {SINGS} edge node[above, pos=0.4] {$1$} (COUNTRY); |
|
3084 |
|
\node[weak entity] (anthem) %[right of=hasfor] |
|
3085 |
|
[right = 1cm of hasfor] {NATIONAL\_ANTHEM} edge[total] node[above, pos=0.6]{\(M\)} (hasfor); |
|
3086 |
|
\node[multi attribute] (color) [right =1cm of anthem] {Creator} edge (anthem); |
|
3087 |
|
\node[attribute] (name) [below right of = anthem] {\pkey{Name}} edge (anthem); |
|
3088 |
|
|
|
3089 |
|
\node[relationship] (WIN) [above right of =LANGUAGE] {W\_IN}; |
|
3090 |
|
\draw (LANGUAGE) to node[left, pos=0.6] {$N$} (WIN); |
|
3091 |
|
\draw (anthem) to node[right, pos=0.6] {$M$} (WIN); |
|
3092 |
|
\end{tikzpicture} |
|
3093 |
|
|
|
3094 |
|
%"S\_B\_W" stands for "SHARES\_A\_BORDER\_WITH". |
|
3095 |
|
"W\_IN" stands for "WRITTEN\_IN", and |
|
3096 |
|
"B\_W\_F" stands for "BORROWS\_WORDS\_FROM". |
|
3097 |
|
For this relationship, on the left-hand side is the language that borrows a word, and on the right-hand side is the language that provides the loanword. |
|
3098 |
|
\caption{E.R. Schema for the COUNTRY\_INFO database} |
|
3099 |
|
\label{fig:erCOUNTRY} |
|
3100 |
|
\end{figure} |
|
3101 |
|
|
File exercises/sum.tex deleted (index ba4254b..0000000) |
1 |
|
|
|
2 |
|
\begin{exercise}[tags={hw,qz}] |
|
3 |
|
What does it mean to say that \sqli{SQL} is at the same time a \enquote{data definition language} and a \enquote{data manipulation language}? |
|
4 |
|
\end{exercise} |
|
5 |
|
|
|
6 |
|
\begin{solution} |
|
7 |
|
It can specify the conceptual and internal schema, \textbf{and} it can manipulate the data. |
|
8 |
|
\end{solution} |
|
9 |
|
|
|
10 |
|
\begin{exercise}[tags={hw}] |
|
11 |
|
Name three kind of objects (for lack of a better word) a \sqli{CREATE} statement can create. |
|
12 |
|
\end{exercise} |
|
13 |
|
|
|
14 |
|
\begin{solution} |
|
15 |
|
Database (schema), table, view, assertion, trigger, etc. |
|
16 |
|
\end{solution} |
|
17 |
|
|
|
18 |
|
\begin{exercise}[tags={qz}] |
|
19 |
|
Write a \sqli{SQL} statement that adds a primary key constraint to an attribute named \sqli{Id} in an already existing table named \sqli{STAFF}. |
|
20 |
|
\end{exercise} |
|
21 |
|
|
|
22 |
|
\begin{solution} |
|
23 |
|
\sqli{ALTER TABLE STAFF ADD PRIMARY KEY(Id);} |
|
24 |
|
\end{solution} |
|
25 |
|
|
|
26 |
|
\begin{exercise}[tags={hw}] |
|
27 |
|
Complete the following table wo different examples when asked for examples: |
|
28 |
|
|
|
29 |
|
{\centering |
|
30 |
|
|
|
31 |
|
\begin{tabular}{m{6cm} | m{6cm} } |
|
32 |
|
\hline |
|
33 |
|
\rowcolor{lgray} |
|
34 |
|
Data Type & Examples \\ |
|
35 |
|
\hline |
|
36 |
|
Int & $4$, $-32$\\ |
|
37 |
|
\hline |
|
38 |
|
Char(4) & \blank[width=1.9em]{'trai', 'plol'}\\ |
|
39 |
|
\hline |
|
40 |
|
\blank[width=1.9em]{VarChar(10)} & 'Train', 'Michelle'\\ |
|
41 |
|
\hline |
|
42 |
|
Bit(4) & \blank[width=1.9em]{ B'1010', B'0101'}\\ |
|
43 |
|
\hline |
|
44 |
|
\blank[width=1.9em]{Boolean} & TRUE, UNKNOWN\\ |
|
45 |
|
\hline |
|
46 |
|
\end{tabular} |
|
47 |
|
|
|
48 |
|
} |
|
49 |
|
\end{exercise} |
|
50 |
|
|
|
51 |
|
\begin{exercise}[tags={hw}] |
|
52 |
|
Explain what the following SQL statement does |
|
53 |
|
\mint{mysql}| CREATE SCHEMA Faculty;| |
|
54 |
|
\end{exercise} |
|
55 |
|
|
|
56 |
|
\begin{solution} |
|
57 |
|
It creates a schema, i.e., a database, named \enquote{Faculty}. |
|
58 |
|
\end{solution} |
|
59 |
|
|
|
60 |
|
\begin{exercise}[tags={hw}] |
|
61 |
|
If I want to enter January 21, 2016, as a value for an attribute with the \sqli{DATE} datatype, what value should I enter? |
|
62 |
|
\end{exercise} |
|
63 |
|
|
|
64 |
|
\begin{solution} |
|
65 |
|
\sqli{DATE}'2016-01-21' |
|
66 |
|
\end{solution} |
|
67 |
|
|
|
68 |
|
\begin{exercise}[tags={hw,qz}] |
|
69 |
|
Write a statement that inserts the values \enquote{Thomas} and \enquote{$4$} into the table \sqli{TRAINS}. |
|
70 |
|
\end{exercise} |
|
71 |
|
|
|
72 |
|
\begin{solution} |
|
73 |
|
\mint{sql}| INSERT INTO TRAINS VALUES('Thomas', 4);| |
|
74 |
|
\end{solution} |
|
75 |
|
|
|
76 |
|
\begin{exercise}[tags={hw}] |
|
77 |
|
If \sqli{PkgName} is a primary key, what can you tell about the number of rows returned by the following statement? |
|
78 |
|
\mint{sql}| SELECT * FROM MYTABLE WHERE PkgName = 'MySQL';| |
|
79 |
|
\end{exercise} |
|
80 |
|
|
|
81 |
|
\begin{solution} |
|
82 |
|
Yes. |
|
83 |
|
\end{solution} |
|
84 |
|
|
|
85 |
|
\begin{exercise}[tags={hw}] |
|
86 |
|
What is the difference between an implicit, an explicit, and a semantic constraint? |
|
87 |
|
\end{exercise} |
|
88 |
|
|
|
89 |
|
\begin{solution} |
|
90 |
|
The textbook reads, pp. 67 - 69: |
|
91 |
|
\begin{enumerate} |
|
92 |
|
\item Constraints that are inherent in the data model. We call these inherent model-based constraints or implicit constraints. |
|
93 |
|
\item Constraints that can be directly expressed in schemas of the data model, typically by specifying them in the DDL (data definition language, see Section 2.3.1). We call these schema-based constraints or explicit constraints. |
|
94 |
|
\item Constraints that cannot be directly expressed in the schemas of the data model, and hence must be expressed and enforced by the application programs. We call these application-based or semantic constraints or business rules. |
|
95 |
|
\end{enumerate} |
|
96 |
|
\end{solution} |
|
97 |
|
|
|
98 |
|
\begin{exercise}[tags={hw}] |
|
99 |
|
If you want every tuple referencing the tuple you're about to delete to be deleted as well, what mechanism should you use? |
|
100 |
|
\end{exercise} |
|
101 |
|
|
|
102 |
|
\begin{solution} |
|
103 |
|
We should use a referential triggered action clause, \sqli{ON DELETE CASCADE}. |
|
104 |
|
\end{solution} |
|
105 |
|
|
|
106 |
|
\begin{exercise}[tags={hw}] |
|
107 |
|
If a database designer is using the \sqli{ON UPDATE SET NULL} for a foreign key, what mechanism is he implementing (i.e., describe how the database will react a certain operation)? |
|
108 |
|
\end{exercise} |
|
109 |
|
|
|
110 |
|
\begin{solution} |
|
111 |
|
If the referenced tuple is updated, then the attribute of the referencing relation are set to NULL. |
|
112 |
|
\end{solution} |
|
113 |
|
|
|
114 |
|
\begin{exercise}[tags={hw}] |
|
115 |
|
If the following is part of the design of a table: |
|
116 |
|
\mint[breaklines, linenos]{mySQL}|FOREIGN KEY (DptNumber) REFERENCES DEPARTMENT(DptNumber) ON DELETE SET DEFAULT ON UPDATE CASCADE| |
|
117 |
|
What happen to the row whose foreign key \sqli{DptNumber} is set to $3$ if |
|
118 |
|
\begin{enumerate} |
|
119 |
|
\item the row in the \sqli{DEPARTEMENT} table with primary key \sqli{DptNumber} set to \(3\) is deleted? |
|
120 |
|
\item the row in the \sqli{DEPARTEMENT} table with primary key \sqli{DptNumber} set to \(3\) as its value for \sqli{DptNumber} updated to \(5\)? |
|
121 |
|
\end{enumerate} |
|
122 |
|
\end{exercise} |
|
123 |
|
|
|
124 |
|
|
|
125 |
|
\begin{solution} |
|
126 |
|
The department number is set to the default value. |
|
127 |
|
|
|
128 |
|
The department number is updated accordingly. |
|
129 |
|
\end{solution} |
|
130 |
|
|
|
131 |
|
\begin{exercise}[tags={qz}] |
|
132 |
|
If the following is part of the design of a \sqli{WORKER} table: |
|
133 |
|
\mint[breaklines, linenos]{mySQL}|FOREIGN KEY WORKER(DptNumber) REFERENCES DEPARTMENT(DptNumber) ON UPDATE CASCADE| |
|
134 |
|
Admitting there is a row in the \sqli{WORKER} table whose foreign key \sqli{DptNumber} is set to $3$, what would happen if |
|
135 |
|
\begin{enumerate} |
|
136 |
|
\item We tried to delete the row in the \sqli{DEPARTEMENT} table with primary key \sqli{DptNumber} set to \(3\)? |
|
137 |
|
\IfTagSetF{hw}{\vspace{4em}} |
|
138 |
|
\item We tried to change the value of \sqli{DptNumber} of the row in the \sqli{DEPARTEMENT} table with primary key \sqli{DptNumber} set to \(3\)? |
|
139 |
|
\end{enumerate} |
|
140 |
|
\end{exercise} |
|
141 |
|
|
|
142 |
|
|
|
143 |
|
\begin{exercise}[tags={hw,qz}] |
|
144 |
|
Given a relation \sqli{TOURIST(Name, EntryDate, Address)}, write a \sqli{SQL} statement printing the name and address of all the tourists who entered the territory after the 15 September, 2012. |
|
145 |
|
\end{exercise} |
|
146 |
|
|
|
147 |
|
\begin{solution} |
|
148 |
|
\begin{minted}{mySQL} |
|
149 |
|
SELECT Name, Address |
|
150 |
|
FROM TOURIST |
|
151 |
|
WHERE EntryDate > DATE'2012-09-15'; |
|
152 |
|
\end{minted} |
|
153 |
|
\end{solution} |
|
154 |
|
|
|
155 |
|
\begin{exercise}[tags={hw}] |
|
156 |
|
What is the fully qualified name of an attribute? Give an example. |
|
157 |
|
\end{exercise} |
|
158 |
|
|
|
159 |
|
\begin{solution} |
|
160 |
|
The name of the relation with the name of its schema and a period beforehand. myDB.MYTABLE, EMPLOYEE.name, etc. |
|
161 |
|
\end{solution} |
|
162 |
|
|
|
163 |
|
% \begin{exercise}[tags={hw}]If \sqli{DEPARTMENT} is a database, what is \sqli{DEPARTMENT.*}?} |
|
164 |
|
% |
|
165 |
|
% \begin{solution} |
|
166 |
|
% All the tables in that database. |
|
167 |
|
% \end{solution} |
|
168 |
|
|
|
169 |
|
\begin{exercise}[tags={hw}] |
|
170 |
|
What is a multi-set? What does it mean to say that SQL treats tables as multisets? |
|
171 |
|
\end{exercise} |
|
172 |
|
|
|
173 |
|
\begin{solution} |
|
174 |
|
A set where the same value can occur twice. |
|
175 |
|
The same tuple can occur twice in a table. |
|
176 |
|
\end{solution} |
|
177 |
|
|
|
178 |
|
\begin{exercise}[tags={hw}] |
|
179 |
|
What is the difference between those two queries? |
|
180 |
|
\mint{mySQL}| SELECT ALL * FROM MYTABLE;| |
|
181 |
|
and |
|
182 |
|
\mint{mySQL}| SELECT DISTINCT * FROM MYTABLE;| |
|
183 |
|
How are the results the same? How are they different? |
|
184 |
|
\end{exercise} |
|
185 |
|
|
|
186 |
|
\begin{solution} |
|
187 |
|
All will print the duplicate, distinct will eliminate them. |
|
188 |
|
\end{solution} |
|
189 |
|
|
|
190 |
|
\IfTagSetF{hw}{ |
|
191 |
|
\end{document} |
|
192 |
|
} |
|
193 |
|
|
|
194 |
|
\section{Part II --- Programming Exercises} |
|
195 |
|
|
|
196 |
|
This part will help you in assessing your level of understanding of this lecture, and help you to become more familiar with \sqli{SQL}. |
|
197 |
|
It constitutes the practical part of this class and shouldn't be overlooked. |
|
198 |
|
This time, all the problems require a computer. |
|
199 |
|
They might look long, but it is because the instructions are detailed: follow them carefully and the four problems shouldn't take you very long. |
|
200 |
|
I'll assume that you will have successfully completed them by the time Homework \#\the\numexpr\hwNum +1\relax{} is released (\hwDateNext), so don't wait and let me know if you had difficulties doing them. |
|
201 |
|
|
|
202 |
|
\begin{problem}[tags={hw}] |
|
203 |
|
\label{setup} |
|
204 |
|
\emph{This exercise assume you successfully completed Problem~2 of Homework 1.} |
|
205 |
|
\begin{enumerate} |
|
206 |
|
\item \label{item-connect} Connect to your MySQL DBMS as \sqli{testuser}: |
|
207 |
|
\begin{itemize} |
|
208 |
|
\item In windows, open a command prompt (search for \enquote{cmd}) and type |
|
209 |
|
\mint{bat}| cd "C:\Program Files\MySQL\MySQL Server 5.7\bin"| |
|
210 |
|
\item In Linux, open a shell (as a normal user) |
|
211 |
|
\end{itemize} |
|
212 |
|
Then, in both cases, type \mint{BNF}| mysql -u testuser -p| and enter the password \sqli{password}. |
|
213 |
|
If you are prompted with a message |
|
214 |
|
\mint[breaklines]{BNF}| ERROR 1045 (28000): Access denied for user 'testuser'@'localhost' (using password: YES)| |
|
215 |
|
then you probably typed the wrong password. |
|
216 |
|
Otherwise, you should see a welcoming message from MySQL / MariaDB and a prompt. |
|
217 |
|
\item \label{item-create} Create a new database called \sqli{HW_2Q1} \mint{mySQL}| CREATE DATABASE HW_2Q1;| |
|
218 |
|
\item List the databases \mint{mySQL}| SHOW DATABASES;| Make sure \sqli{HW_2Q1} is a part of it. |
|
219 |
|
\item Remove (\enquote{drop}) the database, using \mint{mySQL}| DROP DATABASE HW_2Q1;| |
|
220 |
|
\end{enumerate} |
|
221 |
|
In the future, we will refer to the commands \ref{item-connect}.\ and \ref{item-create}.\ as \enquote{log-in as \sqli{testuser} and create a database \sqli{HW_2Q1}}. |
|
222 |
|
|
|
223 |
|
\end{problem} |
|
224 |
|
|
|
225 |
|
\begin{problem}[tags={hw}] |
|
226 |
|
The goal of this problem is to learn where to find the documentation of your DBMS, and to have a first look at the syntax of SQL commands. |
|
227 |
|
|
|
228 |
|
You can consult your textbook, Table~5.2, p. 140 (6th edition) or Table~7.2, p. 235 (7th edition), for a very quick summary of the most common commands. |
|
229 |
|
Make sure you are familiar with the Backus–Naur form (BNF) notation commonly used: |
|
230 |
|
\begin{itemize} |
|
231 |
|
\item non-terminal symbols (i.e., variables, parameters) are enclosed in angled brackets, \mintinline{BNF}|<...>| |
|
232 |
|
\item optional parts are shown in square brackets, \mintinline{BNF}|[...]| |
|
233 |
|
\item repetitons are shown in braces \mintinline{BNF}|{...}| |
|
234 |
|
\item alternatives are shown in parenthesis and separated by vertical bars, \mintinline{BNF}\(...|...|...)\ |
|
235 |
|
\end{itemize} |
|
236 |
|
|
|
237 |
|
The most complete lists of commands are probably at |
|
238 |
|
\begin{itemize} |
|
239 |
|
\item \url{https://mariadb.com/kb/en/library/sql-statements/} and |
|
240 |
|
\item \url{https://dev.mysql.com/doc/refman/5.7/en/sql-syntax.html} |
|
241 |
|
\end{itemize} |
|
242 |
|
Those are the commands implemented in the DBMS you are actually using. |
|
243 |
|
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. |
|
244 |
|
\end{problem} |
|
245 |
|
|
|
246 |
|
\begin{problem}[tags={hw}] |
|
247 |
|
\emph{This exercise, and the following ones, assume you successfully completed Problem~\ref{setup}.} |
|
248 |
|
|
|
249 |
|
Log in as \sqli{testuser} and create a database \sqli{HW_2Q3}. |
|
250 |
|
|
|
251 |
|
\begin{enumerate} |
|
252 |
|
\item We will first set-up our meta-data (i.e., the schema, the structure where our tables will be, and the tables themselves). |
|
253 |
|
|
|
254 |
|
Let us first tell MySQL that we want to use that database |
|
255 |
|
\mint{mySQL}| USE HW_2Q3;| |
|
256 |
|
and ask what it contains |
|
257 |
|
\mint{mySQL}| SHOW TABLES;| |
|
258 |
|
Now, create a first table named \sqli{NAME} |
|
259 |
|
\begin{minted}{mySQL} |
|
260 |
|
CREATE TABLE NAME( |
|
261 |
|
FName VARCHAR(15), |
|
262 |
|
LName VARCHAR(15), |
|
263 |
|
Id INT, |
|
264 |
|
PRIMARY KEY(Id) |
|
265 |
|
); |
|
266 |
|
\end{minted} |
|
267 |
|
Note that the \sqli{SQL} syntax and your DBMS are completely fine with your statement spreading over multiple lines. |
|
268 |
|
Let us create a second table, named \sqli{ADDRESS} |
|
269 |
|
\begin{minted}{mySQL} |
|
270 |
|
CREATE TABLE ADDRESS( |
|
271 |
|
StreetName VARCHAR(15), |
|
272 |
|
Number INT, |
|
273 |
|
Habitants INT, |
|
274 |
|
PRIMARY KEY(StreetName, Number) |
|
275 |
|
); |
|
276 |
|
\end{minted} |
|
277 |
|
To make sure those two tables were actually created, we can use |
|
278 |
|
\mint{mySQL}| SHOW TABLES;| |
|
279 |
|
But how to make sure that you entered the attributes correctly? |
|
280 |
|
One way to make sure is to enter the command |
|
281 |
|
% \mint{mySQL}| show create table name;| |
|
282 |
|
\mint{mySQL}| DESC ADDRESS;| |
|
283 |
|
and to examine carefully the message printed. You should read |
|
284 |
|
\begin{minted}{mySQL} |
|
285 |
|
+------------+-------------+------+-----+---------+-------+ |
|
286 |
|
| Field | Type | Null | Key | Default | Extra | |
|
287 |
|
+------------+-------------+------+-----+---------+-------+ |
|
288 |
|
| StreetName | varchar(15) | NO | PRI | NULL | | |
|
289 |
|
| Number | int(11) | NO | PRI | NULL | | |
|
290 |
|
| Habitants | int(11) | YES | | NULL | | |
|
291 |
|
+------------+-------------+------+-----+---------+-------+ |
|
292 |
|
\end{minted} |
|
293 |
|
If you believe there is a mistake, you can erase (\enquote{drop}) the \emph{table} (not the whole database, as we did in Problem~1) using |
|
294 |
|
\mint{mySQL}| DROP TABLE ADDRESS;| |
|
295 |
|
and then re-create it. Of course, you can do the same for the \sqli{NAME} table. |
|
296 |
|
|
|
297 |
|
Now, let us add a foreign key to the \sqli{ADDRESS} table: |
|
298 |
|
|
|
299 |
|
\begin{minted}{mySQL} |
|
300 |
|
ALTER TABLE ADDRESS |
|
301 |
|
ADD FOREIGN KEY (Habitants) |
|
302 |
|
REFERENCES NAME(Id); |
|
303 |
|
\end{minted} |
|
304 |
|
And observe the modification: |
|
305 |
|
\begin{minted}{mySQL} |
|
306 |
|
DESC ADDRESS; |
|
307 |
|
\end{minted} |
|
308 |
|
|
|
309 |
|
\item This second part is about data, i.e., filling our tables with actual information. |
|
310 |
|
We begin by adding some data in the \sqli{NAME} table: |
|
311 |
|
\begin{minted}{mySQL} |
|
312 |
|
INSERT INTO NAME VALUES ('Barbara', 'Liskov', 003); |
|
313 |
|
INSERT INTO NAME VALUES ('Tuong Lu', 'Kim', 004); |
|
314 |
|
INSERT INTO NAME VALUES ('Samantha', NULL, 080); |
|
315 |
|
\end{minted} |
|
316 |
|
To display the data we just inserted, we can use: |
|
317 |
|
\begin{minted}{mySQL} |
|
318 |
|
SELECT * FROM NAME; |
|
319 |
|
\end{minted} |
|
320 |
|
Do you notice anything regarding the values we entered for the \sqli{Id} attribute? |
|
321 |
|
|
|
322 |
|
We can add some data into the \sqli{ADDRESS} table (using a different syntax): |
|
323 |
|
\begin{minted}{mySQL} |
|
324 |
|
INSERT INTO ADDRESS (StreetName, Number, Habitants) |
|
325 |
|
VALUES |
|
326 |
|
('Armstrong Drive', 10019, 003), |
|
327 |
|
('North Broad St.', 23, 004), |
|
328 |
|
('Robert Lane', 120, NULL); |
|
329 |
|
\end{minted} |
|
330 |
|
And let's use our first update command: |
|
331 |
|
\begin{minted}{mySQL} |
|
332 |
|
UPDATE ADDRESS SET Habitants = 3 WHERE Number = 120; |
|
333 |
|
\end{minted} |
|
334 |
|
|
|
335 |
|
\item |
|
336 |
|
Now, do the following: |
|
337 |
|
\begin{enumerate} |
|
338 |
|
\item Have a look at the formal definition of the \mintinline{SQL}|ALTER| command, at \url{https://dev.mysql.com/doc/refman/5.7/en/alter-table.html} or \url{https://mariadb.com/kb/en/library/alter-table/}. |
|
339 |
|
\item Draw the relations corresponding to that database, including the domains and primary, as well as foreign, keys. |
|
340 |
|
\item Write a \mintinline{SQL}{SELECT} statement that returns the \mintinline{SQL}{Id} number of the person whose first name is \enquote{Samantha}. |
|
341 |
|
% SELECT Id FROM NAME WHERE FName = 'Samantha'; |
|
342 |
|
\item Write a statement that violate the entity integrity constraint. What is the error message returned? |
|
343 |
|
% ERROR 1062 (23000): Duplicate entry '80' for key 'PRIMARY' |
|
344 |
|
\item Execute an \sqli{UPDATE} statement that violate the referential integrity constraint. What is the error message returned? |
|
345 |
|
% ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`HW_2Q3`.`ADDRESS`, CONSTRAINT `fk_id` FOREIGN KEY (`Habitants`) REFERENCES `name` (`Id`)) |
|
346 |
|
\item Write a statement that violate another kind of constraint. Explain what constraint you are violating, and explain the error message. |
|
347 |
|
% ERROR 1136 (21S01): Column count doesn't match value count at row 1 |
|
348 |
|
\end{enumerate} |
|
349 |
|
|
|
350 |
|
\end{enumerate} |
|
351 |
|
\end{problem} |
|
352 |
|
|
|
353 |
|
\begin{problem}[tags={hw}]Log in as \sqli{testuser} and create a database \sqli{HW_2Q4}. Tell MySQL that you want to use that database, and create a table (I will assume you named it \sqli{EXAMPLE} in the following, but you are free to name it the way you want) with at least two attributes that have different data types. Don't declare a primary key yet. Answer the following: |
|
354 |
|
\begin{enumerate} |
|
355 |
|
\item Add a tuple to your table using |
|
356 |
|
\mint{mySQL}| INSERT INTO EXAMPLE VALUES(X, Y);| |
|
357 |
|
where \enquote{X} and \enquote{Y} are values with the right type. |
|
358 |
|
Try to add this tuple again. What do you observe? (You can use \mintinline{mySQL}|SELECT * FROM EXAMPLE;| to observe what is stored in this table.) |
|
359 |
|
\item Alter your table to add a primary key, using |
|
360 |
|
\mint{mySQL}| ALTER TABLE EXAMPLE ADD PRIMARY KEY (Attribute);| where \sqli{Attribute} is the name of the attribute you want to be a primary key. What do you observe? |
|
361 |
|
\item Empty your table using \mint{mySQL}| DELETE FROM EXAMPLE;| and alter your table to add a primary key, using the command we gave at the previous step. What do you observe? |
|
362 |
|
\item Try to add the same tuple twice. What do you observe? |
|
363 |
|
% \item Now, create \emph{another table} (just one attribute and no primary key is fine) and try to create a foreign key pointing to the attribute in \sqli{EXAMPLE} that is not the primary key. |
|
364 |
|
% What do you observe? |
|
365 |
|
% => Messy, they get an error message if the attribute is not the primary key or if the datatype isn't the same, or if the command is not properly formatted... The error message is too vague for them to learn anything. |
|
366 |
|
\end{enumerate} |
|
367 |
|
\end{problem} |
|
368 |
|
|
|
369 |
|
\begin{solution} |
|
370 |
|
\begin{minted}{SQL} |
|
371 |
|
CREATE TABLE EXAMPLE( |
|
372 |
|
X VARCHAR(15), |
|
373 |
|
Y INT); |
|
374 |
|
\end{minted} |
|
375 |
|
\begin{minted}{SQL} |
|
376 |
|
INSERT INTO EXAMPLE VALUES('Train', 4); |
|
377 |
|
\end{minted} |
|
378 |
|
OK to insert same tuple twice. |
|
379 |
|
\begin{minted}{SQL} |
|
380 |
|
ALTER TABLE EXAMPLE ADD PRIMARY KEY (X); |
|
381 |
|
ERROR 1062 (23000): Duplicate entry 'Train' for key 'PRIMARY' |
|
382 |
|
\end{minted} |
|
383 |
|
\end{solution} |
|
384 |
|
|
|
385 |
|
%%%%% 03 |
|
386 |
|
|
|
387 |
|
|
|
388 |
|
\begin{exercise}[tags={hw}] |
|
389 |
|
Describe what the star do in the statement \sqli{SELECT ALL * FROM MYTABLE;}. |
|
390 |
|
\end{exercise} |
|
391 |
|
|
|
392 |
|
\begin{solution} |
|
393 |
|
It selects all the attributes. |
|
394 |
|
\end{solution} |
|
395 |
|
|
|
396 |
|
|
|
397 |
|
\begin{exercise}[tags={hw}] |
|
398 |
|
What is wrong with the statement \sqli{SELECT * WHERE Name = 'CS' FROM DEPARTMENT;}? |
|
399 |
|
\end{exercise} |
|
400 |
|
|
|
401 |
|
\begin{solution} |
|
402 |
|
You can't have the \sqli{WHERE} before \sqli{FROM}. |
|
403 |
|
\end{solution} |
|
404 |
|
|
|
405 |
|
|
|
406 |
|
\begin{exercise}[tags={hw}] |
|
407 |
|
When is it useful to use a select-project-join query? |
|
408 |
|
\end{exercise} |
|
409 |
|
|
|
410 |
|
\begin{solution} |
|
411 |
|
We use those query that projects on attributes using a selection and join conditions when we need to look for information gathered in multiple tables. |
|
412 |
|
\end{solution} |
|
413 |
|
|
|
414 |
|
\begin{exercise}[tags={hw}] |
|
415 |
|
When is a tuple variable useful? |
|
416 |
|
\end{exercise} |
|
417 |
|
|
|
418 |
|
\begin{solution} |
|
419 |
|
It makes the distinction between two different copies of the same relation, it is useful when we want to select a tuple in a relation that is in a particular relation with a tuple in the same relation. |
|
420 |
|
|
|
421 |
|
cf. \url{https://stackoverflow.com/a/7698796/2657549}: |
|
422 |
|
They are useful for saving typing, but there are other reasons to use them: |
|
423 |
|
\begin{itemize} |
|
424 |
|
\item If you join a table to itself you must give it two different names otherwise referencing the table would be ambiguous. |
|
425 |
|
\item It can be useful to give names to derived tables, and in some database systems it is required... even if you never refer to the name. |
|
426 |
|
\end{itemize} |
|
427 |
|
\end{solution} |
|
428 |
|
|
|
429 |
|
\begin{exercise}[tags={hw}] |
|
430 |
|
Write a query that changes the name of the professor whose login is 'caubert' to 'Hugo Pernot' in the table \sqli{PROF}. |
|
431 |
|
\end{exercise} |
|
432 |
|
|
|
433 |
|
\begin{solution} |
|
434 |
|
\sqli{UPDATE PROF SET Name = 'Hugo Pernot' WHERE Login = 'caubert';} |
|
435 |
|
\end{solution} |
|
436 |
|
|
|
437 |
|
\begin{exercise}[tags={hw}] |
|
438 |
|
Can an \sqli{UPDATE} statement have a \sqli{WHERE} condition using an attribute that isn't the primary key? If no, justify, if yes, tell what could happen. |
|
439 |
|
\end{exercise} |
|
440 |
|
|
|
441 |
|
\begin{solution} |
|
442 |
|
Yes, we could update more than one tuple at a time. |
|
443 |
|
\end{solution} |
|
444 |
|
|
|
445 |
|
\begin{exercise}[tags={hw}] |
|
446 |
|
What is a multi-set? |
|
447 |
|
What does it mean to say that \sqli{SQL} treats tables as multisets? |
|
448 |
|
\end{exercise} |
|
449 |
|
|
|
450 |
|
\begin{solution} |
|
451 |
|
A set where the same value can occur twice. |
|
452 |
|
The same tuple can occur twice in a table. |
|
453 |
|
\end{solution} |
|
454 |
|
|
|
455 |
|
\begin{exercise}[tags={hw}] |
|
456 |
|
What is the difference between those two queries? |
|
457 |
|
\mint{mySQL}| SELECT ALL * FROM MYTABLE;| |
|
458 |
|
and |
|
459 |
|
\mint{mySQL}| SELECT DISTINCT * FROM MYTABLE;| |
|
460 |
|
How are the results the same? How are they different? |
|
461 |
|
\end{exercise} |
|
462 |
|
|
|
463 |
|
\begin{solution} |
|
464 |
|
\sqli{ALL} will print the duplicate, \sqli{DISTINCT} will eliminate them. |
|
465 |
|
\end{solution} |
|
466 |
|
|
|
467 |
|
\begin{exercise}[tags={hw}] |
|
468 |
|
What are the possible meanings or interpretations for a \sqli{NULL} value? |
|
469 |
|
\end{exercise} |
|
470 |
|
|
|
471 |
|
\begin{solution} |
|
472 |
|
Unknown value, unavailable / withheld, N/A. |
|
473 |
|
\end{solution} |
|
474 |
|
|
|
475 |
|
|
|
476 |
|
\begin{exercise}[tags={hw}] |
|
477 |
|
What are the values of the following expressions (i.e., do they evaluate to \sqli{TRUE}, \sqli{FALSE}, or \sqli{UNKNOWN})? |
|
478 |
|
|
|
479 |
|
\sqli{TRUE AND FALSE}\hspace{2.2em} |
|
480 |
|
\sqli{TRUE AND UNKNOWN}\hspace{2.2em} |
|
481 |
|
\sqli{NOT UNKNOWN}\hspace{2.2em} |
|
482 |
|
\sqli{FALSE OR UNKNOWN} |
|
483 |
|
|
|
484 |
|
\end{exercise} |
|
485 |
|
|
|
486 |
|
\begin{solution} |
|
487 |
|
\sqli{FALSE}, \sqli{UNKNOWN}, \sqli{UNKNOWN}, \sqli{FALSE} |
|
488 |
|
\end{solution} |
|
489 |
|
|
|
490 |
|
\begin{exercise}[tags={hw,qz}] |
|
491 |
|
What comparison expression should you use to test if a value is different from \sqli{NULL}? |
|
492 |
|
\end{exercise} |
|
493 |
|
|
|
494 |
|
\begin{solution} |
|
495 |
|
\sqli{IS NOT} |
|
496 |
|
\end{solution} |
|
497 |
|
|
|
498 |
|
\begin{exercise}[tags={hw}] |
|
499 |
|
Explain this query: |
|
500 |
|
\begin{minted}{sql} |
|
501 |
|
SELECT Login |
|
502 |
|
FROM PROF |
|
503 |
|
WHERE Department IN ( SELECT Major |
|
504 |
|
FROM STUDENT |
|
505 |
|
WHERE Login = 'jrakesh'); |
|
506 |
|
\end{minted} |
|
507 |
|
\end{exercise} |
|
508 |
|
|
|
509 |
|
\begin{solution} |
|
510 |
|
It list the login of the professors teaching in the department where the student whose login is \enquote{jrakesh} is majoring. |
|
511 |
|
\end{solution} |
|
512 |
|
|
|
513 |
|
\begin{exercise}[tags={hw}] |
|
514 |
|
What is wrong with this query? |
|
515 |
|
|
|
516 |
|
\begin{minted}{sql} |
|
517 |
|
SELECT Name |
|
518 |
|
FROM STUDENT |
|
519 |
|
WHERE Login IN ( SELECT Code |
|
520 |
|
FROM Department |
|
521 |
|
WHERE head = 'aturing'); |
|
522 |
|
\end{minted} |
|
523 |
|
|
|
524 |
|
\end{exercise} |
|
525 |
|
|
|
526 |
|
\begin{solution} |
|
527 |
|
It tries to find a \sqli{Login} in a \sqli{Code}! |
|
528 |
|
\end{solution} |
|
529 |
|
|
|
530 |
|
\begin{exercise}[tags={hw}] |
|
531 |
|
Write a query that returns the number of row (i.e., of entries, of tuples) in a table named \sqli{BOOK}. |
|
532 |
|
\end{exercise} |
|
533 |
|
|
|
534 |
|
\begin{solution} |
|
535 |
|
\sqli{SELECT COUNT(*) FROM BOOK;} |
|
536 |
|
\end{solution} |
|
537 |
|
|
|
538 |
|
\begin{exercise}[tags={qz}] |
|
539 |
|
\vspace{-5em} |
|
540 |
|
Consider the following \sqli{SQL} code: |
|
541 |
|
\begin{multicols}{2} |
|
542 |
|
\begin{minted}[breaklines=true, linenos]{sql} |
|
543 |
|
CREATE TABLE COMPUTER( |
|
544 |
|
Id VARCHAR(20) PRIMARY KEY, |
|
545 |
|
Model VARCHAR(20) |
|
546 |
|
); |
|
547 |
|
CREATE TABLE PRINTER( |
|
548 |
|
Id VARCHAR(20) PRIMARY KEY, |
|
549 |
|
Model VARCHAR(20) |
|
550 |
|
); |
|
551 |
|
CREATE TABLE CONNEXION( |
|
552 |
|
Computer VARCHAR(20), |
|
553 |
|
Printer VARCHAR(20), |
|
554 |
|
PRIMARY KEY(Computer, Printer), |
|
555 |
|
FOREIGN KEY (Computer) REFERENCES COMPUTER(Id), |
|
556 |
|
FOREIGN KEY (Printer) REFERENCES PRINTER(Id) |
|
557 |
|
); |
|
558 |
|
|
|
559 |
|
INSERT INTO COMPUTER VALUES |
|
560 |
|
('A', 'DELL A'), |
|
561 |
|
('B', 'HP X'), |
|
562 |
|
('C', 'ZEPTO D'), |
|
563 |
|
('D', 'MAC Y'); |
|
564 |
|
INSERT INTO PRINTER VALUES |
|
565 |
|
('12', 'HP-140'), |
|
566 |
|
('13', 'HP-139'), |
|
567 |
|
('14', 'HP-140'), |
|
568 |
|
('15', 'HP-139'); |
|
569 |
|
INSERT INTO CONNEXION VALUES |
|
570 |
|
('A', '12'), |
|
571 |
|
('A', '13'), |
|
572 |
|
('B', '13'), |
|
573 |
|
('C', '14'); |
|
574 |
|
|
|
575 |
|
\end{minted} |
|
576 |
|
\end{multicols} |
|
577 |
|
Write a query that returns \dots (in parenthesis, the values returned \emph{in this set-up}, but you have to be general) |
|
578 |
|
\begin{enumerate}[topsep=0pt,itemsep=7em,partopsep=1ex,parsep=1ex] |
|
579 |
|
\item \dots the number of computers connected to the printer whose id is '13' (i.e., \(2\) ). |
|
580 |
|
\item \dots the number of different models of printers (i.e., \(2\)). |
|
581 |
|
\item \dots the model(s) of the printer connected to the computer whose id is 'A' (i.e., 'HP-140' and 'HP-139'). |
|
582 |
|
\item \dots the id of the computer(s) not connected to any printer (i.e., 'D'). |
|
583 |
|
\end{enumerate} |
|
584 |
|
|
|
585 |
|
\end{exercise} |
|
586 |
|
|
|
587 |
|
\begin{exercise}[tags={hw}] |
|
588 |
|
Write a query that returns the sum of all the values stored in the \sqli{Pages} attribute of a \sqli{BOOK} table. |
|
589 |
|
\end{exercise} |
|
590 |
|
|
|
591 |
|
\begin{solution} |
|
592 |
|
\sqli{SELECT SUM(Pages) FROM BOOK;} |
|
593 |
|
\end{solution} |
|
594 |
|
|
|
595 |
|
|
|
596 |
|
\begin{exercise}[tags={hw}] |
|
597 |
|
Write a query that adds a \sqli{Pages} attribute of type \sqli{INT} into a (already existing) \sqli{BOOK} table. |
|
598 |
|
\end{exercise} |
|
599 |
|
|
|
600 |
|
\begin{solution} |
|
601 |
|
\sqli{ALTER TABLE BOOK ADD COLUMN Pages INT;} |
|
602 |
|
\end{solution} |
|
603 |
|
|
|
604 |
|
\begin{exercise}[tags={hw}] |
|
605 |
|
Write a query that removes the default value for a \sqli{Pages} attribute in a \sqli{BOOK} table. |
|
606 |
|
\end{exercise} |
|
607 |
|
|
|
608 |
|
\begin{solution} |
|
609 |
|
\sqli{ALTER TABLE BOOK ALTER COLUMN Pages DROP DEFAULT;} |
|
610 |
|
\end{solution} |
|
611 |
|
|
|
612 |
|
|
|
613 |
|
\IfTagSetF{hw}{ |
|
614 |
|
\end{document} |
|
615 |
|
} |
|
616 |
|
|
|
617 |
|
\section{Part II --- Programming Exercises} |
|
618 |
|
|
|
619 |
|
This time, the (only) problem requires a computer and to have access to the code used during the lecture. |
|
620 |
|
I'll assume that you will have successfully completed it by the time Homework \#\the\numexpr\hwNum +1\relax{} is released (\hwDateNext), so don't wait and let me know if you had difficulties doing it. |
|
621 |
|
|
|
622 |
|
\begin{problem}[tags={hw}] |
|
623 |
|
Create the \sqli{PROF}, \sqli{STUDENT}, \sqli{DEPARTMENT} and \sqli{GRADE} tables as during the lecture. |
|
624 |
|
Populate them with some data. |
|
625 |
|
\begin{enumerate} |
|
626 |
|
\item Create a \sqli{LECTURE} table as follows: |
|
627 |
|
\begin{itemize} |
|
628 |
|
\item It should have four attributes, \sqli{Name}, \sqli{Instructor}, \sqli{Code}, and \texttt{Year}, of type \sqli{VARCHAR(25)} for the two first, \sqli{YEAR(4)}, and \sqli{CHAR(5)}. |
|
629 |
|
\item The \texttt{Year} and \sqli{Code} attributes should be the primary key (yes, have \emph{two} attributes be the primary key). |
|
630 |
|
\item The \sqli{Instructor} attribute should be a foreign key referencing the \sqli{Login} attribute in \sqli{PROF}. |
|
631 |
|
\end{itemize} |
|
632 |
|
\item Populate the \sqli{LECTURE} table with some made-up data. |
|
633 |
|
\item Add two columns to the \sqli{GRADE} table, using |
|
634 |
|
\begin{minted}{sql} |
|
635 |
|
ALTER TABLE GRADE |
|
636 |
|
ADD COLUMN LectureCode CHAR(5), |
|
637 |
|
ADD COLUMN LectureYear YEAR(4); |
|
638 |
|
\end{minted} |
|
639 |
|
\item Use \sqli{DESCRIBE} and \sqli{SELECT} to observe the schema of the \sqli{GRADE} table and its rows (i.e., tuples). |
|
640 |
|
Is that what you would have expected? |
|
641 |
|
\item Add a foreign key in \sqli{GRADE}, using |
|
642 |
|
\begin{minted}{mysql} |
|
643 |
|
ALTER TABLE GRADE |
|
644 |
|
ADD FOREIGN KEY (LectureYear, LectureCode) |
|
645 |
|
REFERENCES LECTURE(Year, Code); |
|
646 |
|
\end{minted} |
|
647 |
|
\item Update the tuples in \sqli{GRADE} with some made-up data: now, every row should contain, on top of a login and a grade, a lecture year and a lecture code. |
|
648 |
|
\item Write \sqli{SELECT} statements answering the following questions (where XXX and YY should be relevant values considering your data): |
|
649 |
|
|
|
650 |
|
\begin{enumerate} |
|
651 |
|
\item \enquote{I taught class XXX in YYYY, could you give me the logins and grades of the students who took it?} |
|
652 |
|
\item \enquote{Could you list the instructors who taught in YYYY?} (and, please, avoid duplicates) |
|
653 |
|
\item \enquote{Could your list the name and grade of all the student who took class XXX (no matter the year)?} |
|
654 |
|
\item \enquote{Could you tell me which years was the class XXX taught?} |
|
655 |
|
\item \enquote{Could you list the classes taught the same year as class XXX?} |
|
656 |
|
\item \enquote{Could you print the name of the students who registered after Ava Alyx?} |
|
657 |
|
\item \enquote{How many departments' heads are teaching this year?} |
|
658 |
|
\end{enumerate} |
|
659 |
|
\end{enumerate} |
|
660 |
|
\end{problem} |
|
661 |
|
|
|
662 |
|
%\begin{solution}[print=true] |
|
663 |
|
% Hey! |
|
664 |
|
\IfSolutionPrintT{ |
|
665 |
|
% Essayer avec print = true? |
|
666 |
|
\begin{minted}[breaklines=true, linenos]{sql} |
|
667 |
|
CREATE TABLE LECTURE( |
|
668 |
|
Name VARCHAR(25), |
|
669 |
|
Instructor VARCHAR(25), |
|
670 |
|
Year YEAR(4), |
|
671 |
|
Code CHAR(5), |
|
672 |
|
PRIMARY KEY(Year, Code), |
|
673 |
|
FOREIGN KEY (Instructor) REFERENCES PROF(Login) |
|
674 |
|
); |
|
675 |
|
|
|
676 |
|
INSERT INTO LECTURE VALUES |
|
677 |
|
('Intro to CS', 'caubert', 2017, '1304'), |
|
678 |
|
('Intro to Algebra', 'perdos', 2017, '1405'), |
|
679 |
|
('Intro to Cyber', 'aturing', 2017, '1234'); |
|
680 |
|
|
|
681 |
|
ALTER TABLE GRADE ADD COLUMN LectureCode CHAR(5), |
|
682 |
|
ADD COLUMN LectureYear YEAR(4); |
|
683 |
|
|
|
684 |
|
DESCRIBE GRADE; |
|
685 |
|
|
|
686 |
|
SELECT * FROM GRADE; |
|
687 |
|
|
|
688 |
|
ALTER TABLE GRADE ADD FOREIGN KEY (LectureYear, LectureCode) REFERENCES LECTURE(Year, Code); |
|
689 |
|
|
|
690 |
|
UPDATE GRADE SET LectureCode = '1304', LectureYear = 2017 WHERE Login = 'jrakesh' AND Grade = '2.85'; |
|
691 |
|
UPDATE GRADE SET LectureCode = '1405', LectureYear = 2017 WHERE Login = 'svlatka' OR (Login = 'jrakesh' AND Grade = '3.85'); |
|
692 |
|
UPDATE GRADE SET LectureCode = '1234', LectureYear = 2017 WHERE Login = 'aalyx' OR Login = 'cjoella'; |
|
693 |
|
|
|
694 |
|
SELECT Login, Grade FROM Grade WHERE Lecturecode='1304' and LectureYear = '2017'; |
|
695 |
|
|
|
696 |
|
SELECT DISTINCT Instructor FROM LECTURE WHERE year = 2017; |
|
697 |
|
|
|
698 |
|
SELECT Name, Grade FROM STUDENT, GRADE WHERE GRADE.LectureCode = 1405 AND STUDENT.Login = GRADE.Login; |
|
699 |
|
|
|
700 |
|
SELECT Year FROM Lecture WHERE Code = '1234'; |
|
701 |
|
|
|
702 |
|
SELECT Name FROM LECTURE WHERE Year IN (SELECT Year FROM LECTURE WHERE CODE = '1234'); |
|
703 |
|
|
|
704 |
|
SELECT B.name FROM STUDENT AS A, STUDENT AS B WHERE A.Name = 'Ava Alyx' AND A.Registered > B.Registered; |
|
705 |
|
|
|
706 |
|
SELECT COUNT(DISTINCT PROF.Name) AS 'Head Teaching This Year' FROM LECTURE, DEPARTMENT, PROF WHERE Year = 2017 AND Instructor = Head AND Head = PROF.Login; |
|
707 |
|
\end{minted} |
|
708 |
|
} |
|
709 |
|
|
|
710 |
|
%%%%% 04 |
|
711 |
|
|
|
712 |
|
|
|
713 |
|
\begin{exercise}[tags={hw}] |
|
714 |
|
What could be the decomposition of an attribute used to store an email address? When could that be useful? |
|
715 |
|
\end{exercise} |
|
716 |
|
|
|
717 |
|
\begin{solution} |
|
718 |
|
Name / extension. |
|
719 |
|
To have statistics about the extensions, to sort the username by length, etc. |
|
720 |
|
\end{solution} |
|
721 |
|
|
|
722 |
|
\begin{exercise}[tags={hw}] |
|
723 |
|
Draw the ER diagram for a \enquote{COMPUTER} entity that has one multivalued attribute \enquote{Operating\_System}, a composite attribute \enquote{Devices} (decomposed into \enquote{Keyboard} and \enquote{Mouse}) and an \enquote{Id} key attribute. |
|
724 |
|
\end{exercise} |
|
725 |
|
|
|
726 |
|
\begin{solution} |
|
727 |
|
\begin{tikzpicture}[node distance=7em] |
|
728 |
|
\node[entity] (computer) {COMPUTER}; |
|
729 |
|
\node[attribute] (pid) [left of=computer] {\key{Id}} edge (computer); |
|
730 |
|
\node[multi attribute] (os) [above of=computer] {Operating\_System} edge (computer); |
|
731 |
|
\node[attribute] (devices) [right of=computer] {Devices} edge (computer); |
|
732 |
|
\node[attribute] (mouse) [above right of=devices] {Mouse} edge (devices); |
|
733 |
|
\node[attribute] (keyboard) [right of=devices] {Keyboard} edge (devices); |
|
734 |
|
\end{tikzpicture} |
|
735 |
|
\end{solution} |
|
736 |
|
|
|
737 |
|
\begin{exercise}[tags={qz}] |
|
738 |
|
Draw the ER diagram for a \enquote{CELLPHONE} entity that has a composite attribute \enquote{Plan} (decomposed into \enquote{Carrier} and \enquote{Price}), a \enquote{Mobile\_identification\_number} key attribute, and a multi-valued \enquote{App\_Installed} attribute. |
|
739 |
|
\end{exercise} |
|
740 |
|
|
|
741 |
|
\begin{solution} |
|
742 |
|
\begin{tikzpicture}[node distance=7em] |
|
743 |
|
\node[entity] (cellphone) {CELLPHONE}; |
|
744 |
|
\node[attribute] (mid) [left of=cellphone] {\key{MIN}} edge (cellphone); |
|
745 |
|
\node[multi attribute] (app) [above of=cellphone] {App\_Installed} edge (cellphone); |
|
746 |
|
\node[attribute] (plan) [right of=cellphone] {Plan} edge (cellphone); |
|
747 |
|
\node[attribute] (carrier) [above right of=plan] {Carrier} edge (plan); |
|
748 |
|
\node[attribute] (price) [right of=plan] {Price} edge (plan); |
|
749 |
|
\end{tikzpicture} |
|
750 |
|
\end{solution} |
|
751 |
|
|
|
752 |
|
|
|
753 |
|
\begin{exercise}[tags={hw,qz}] |
|
754 |
|
Name one difference between a primary key in the relational model, and a key attribute in the ER model. |
|
755 |
|
\end{exercise} |
|
756 |
|
|
|
757 |
|
\begin{solution} |
|
758 |
|
There can be more than one key in the ER model. |
|
759 |
|
\end{solution} |
|
760 |
|
|
|
761 |
|
\begin{exercise}[tags={hw}] |
|
762 |
|
What is the difference between an entity type and a weak entity type? |
|
763 |
|
\end{exercise} |
|
764 |
|
|
|
765 |
|
\begin{solution} |
|
766 |
|
The weak entity type doesn't have a primary key. |
|
767 |
|
\end{solution} |
|
768 |
|
|
|
769 |
|
\begin{exercise}[tags={hw}] |
|
770 |
|
What is the degree of a relationship type? |
|
771 |
|
\end{exercise} |
|
772 |
|
|
|
773 |
|
\begin{solution} |
|
774 |
|
The number of participating entity types. |
|
775 |
|
\end{solution} |
|
776 |
|
|
|
777 |
|
\begin{exercise}[tags={hw}] |
|
778 |
|
What is a self-referencing, or recursive, relationship type? Give two examples. |
|
779 |
|
\end{exercise} |
|
780 |
|
|
|
781 |
|
\begin{solution} |
|
782 |
|
A relationship type where the same entity type participates more than once. |
|
783 |
|
On rooms, \enquote{is to the left of}, on persons, \enquote{is married to}. |
|
784 |
|
\end{solution} |
|
785 |
|
|
|
786 |
|
\begin{exercise}[tags={hw,qz}] |
|
787 |
|
What does it mean for a binary relationship type \enquote{Owner} between entity types \enquote{Person} and \enquote{Computer} to have a cardinality ration $M:N$? |
|
788 |
|
\end{exercise} |
|
789 |
|
|
|
790 |
|
\begin{solution} |
|
791 |
|
That a person can own multiple computers, and that a computer can have multiple owners. |
|
792 |
|
\end{solution} |
|
793 |
|
|
|
794 |
|
\begin{exercise}[tags={hw}] |
|
795 |
|
What are the two possible structural constraints on a relationship type? |
|
796 |
|
\end{exercise} |
|
797 |
|
|
|
798 |
|
\begin{solution} |
|
799 |
|
Cardinality ration and participation constraints. |
|
800 |
|
\end{solution} |
|
801 |
|
|
|
802 |
|
\begin{exercise}[tags={hw}] |
|
803 |
|
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? |
|
804 |
|
\end{exercise} |
|
805 |
|
|
|
806 |
|
\begin{solution} |
|
807 |
|
When the cardinality is $1 : N$, $1 : 1$ or $N : 1$. |
|
808 |
|
\end{solution} |
|
809 |
|
|
|
810 |
|
\begin{exercise}[tags={hw}] |
|
811 |
|
What is a partial key? |
|
812 |
|
\end{exercise} |
|
813 |
|
|
|
814 |
|
\begin{solution} |
|
815 |
|
For a weak entity attribute, it is the attribute that can uniquely identify weak entites that are related to the same owner entity. |
|
816 |
|
\end{solution} |
|
817 |
|
|
|
818 |
|
\begin{exercise}[tags={hw}] |
|
819 |
|
For the following binary relationships, suggest cardinality ratios based on the common-sense meaning of the entity types. |
|
820 |
|
|
|
821 |
|
{\centering |
|
822 |
|
|
|
823 |
|
\begin{tabular}{l c l} |
|
824 |
|
\textbf{Entity 1} & \textbf{Cardinality Ratio} & \textbf{Entity 2}\\ |
|
825 |
|
STUDENT & & MAJOR\\ % 1 : N |
|
826 |
|
CAR & & TAG \\ % 1 : 1 |
|
827 |
|
INSTRUCTOR & & LECTURE \\ % N :1 |
|
828 |
|
INSTRUCTOR & & OFFICE \\ % 1 : N |
|
829 |
|
COMPUTER & & OPERATING\_SYSTEM % N : M |
|
830 |
|
\end{tabular} |
|
831 |
|
|
|
832 |
|
} |
|
833 |
|
\end{exercise} |
|
834 |
|
|
|
835 |
|
\begin{solution} |
|
836 |
|
N:1 , 1 : 1, 1 : N , 1:N, M : N |
|
837 |
|
\end{solution} |
|
838 |
|
|
|
839 |
|
\begin{exercise}[tags={hw}] |
|
840 |
|
Give an example of a binary relationship type of cardinality $1 : N$. |
|
841 |
|
\end{exercise} |
|
842 |
|
|
|
843 |
|
\begin{solution} |
|
844 |
|
SUPERVISION, a recursive relationship on EMPLOYEE. |
|
845 |
|
\end{solution} |
|
846 |
|
|
|
847 |
|
\begin{exercise}[tags={hw}] |
|
848 |
|
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). |
|
849 |
|
\end{exercise} |
|
850 |
|
|
|
851 |
|
\begin{solution} |
|
852 |
|
\begin{tikzpicture}[node distance=7em] |
|
853 |
|
\node[relationship] (contains){BELONGS}; |
|
854 |
|
\node[entity] (burger) [left = 1cm of contains] {HAND} edge node[above] {$N$} (contains); |
|
855 |
|
\node[entity] (ingredient) [right = 1cm of contains] {PERSON} edge node[above]{$1$} (contains); |
|
856 |
|
\end{tikzpicture} |
|
857 |
|
|
|
858 |
|
\end{solution} |
|
859 |
|
|
|
860 |
|
\begin{exercise}[tags={hw}] |
|
861 |
|
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). |
|
862 |
|
\end{exercise} |
|
863 |
|
|
|
864 |
|
\begin{solution} |
|
865 |
|
|
|
866 |
|
\begin{tikzpicture}[node distance=7em] |
|
867 |
|
\node[entity] (person) {PERSON}; |
|
868 |
|
\node[attribute] (ssn) [left of=person] {\key{SSN}} edge (person); |
|
869 |
|
\node[attribute] (dob) [above of=person] {Date\_Of\_Birth} edge (person); |
|
870 |
|
\node[derived attribute] (age) [right of=person] {Age} edge (person); |
|
871 |
|
\end{tikzpicture} |
|
872 |
|
|
|
873 |
|
\end{solution} |
|
874 |
|
|
|
875 |
|
\begin{exercise}[tags={hw}] |
|
876 |
|
Draw an ER diagram expressing the total participation of an entity type \enquote{BURGER} in a binary relation \enquote{CONTAINS} between \enquote{BURGER} and \enquote{INGREDIENT}. |
|
877 |
|
What would be the ratio of such a relation? |
|
878 |
|
\end{exercise} |
|
879 |
|
|
|
880 |
|
\begin{solution} |
|
881 |
|
\begin{tikzpicture}[node distance=7em] |
|
882 |
|
\node[relationship] (contains){CONTAINS}; |
|
883 |
|
\node[entity] (burger) [left = 1cm of contains] {BURGER} edge[total] node[above] {$N$} (contains); |
|
884 |
|
\node[entity] (ingredient) [right = 1cm of contains] {INGREDIENT} edge node[above]{$M$} (contains); |
|
885 |
|
\end{tikzpicture} |
|
886 |
|
\end{solution} |
|
887 |
|
|
|
888 |
|
\begin{exercise}[tags={qz}] |
|
889 |
|
Convert the following ER diagram into a relational model: |
|
890 |
|
|
|
891 |
|
\begin{tikzpicture}[node distance=7em] |
|
892 |
|
\node[relationship] (contains){STAYS\_AT}; |
|
893 |
|
\node[entity] (person) [above = 1cm of contains] {PERSON} edge node[right] {$N$} (contains);; |
|
894 |
|
\node[attribute] (ssn) [left of=person] {\key{SSN}} edge (person); |
|
895 |
|
\node[attribute] (dob) [above = .5cm of person] {Date\_Of\_Birth} edge (person); |
|
896 |
|
\node[entity] (burger) [below of= contains] {PLACE} edge node[right] {$1$} (contains); |
|
897 |
|
\node[attribute] (dob) [below = .5cm of burger] {\key{Address}} edge (burger); |
|
898 |
|
\node[attribute] (ssn) [left of=burger] {Rooms} edge (burger); |
|
899 |
|
\end{tikzpicture} |
|
900 |
|
\end{exercise} |
|
901 |
|
|
|
902 |
|
\begin{solution} |
|
903 |
|
\begin{tikzpicture}[node distance=7em] |
|
904 |
|
\node[relationship] (contains){CONTAINS}; |
|
905 |
|
\node[entity] (burger) [left = 1cm of contains] {BURGER} edge[total] node[above] {$N$} (contains); |
|
906 |
|
\node[entity] (ingredient) [right = 1cm of contains] {INGREDIENT} edge node[above]{$M$} (contains); |
|
907 |
|
\end{tikzpicture} |
|
908 |
|
\end{solution} |
|
909 |
|
|
|
910 |
|
\begin{exercise}[tags={hw}] |
|
911 |
|
Why do weak entity type have a total participation constraint? |
|
912 |
|
\end{exercise} |
|
913 |
|
|
|
914 |
|
\begin{solution} |
|
915 |
|
Otherwise, we couldn't identify entities in it without owner entity. |
|
916 |
|
\end{solution} |
|
917 |
|
|
|
918 |
|
\IfTagSetF{hw}{ |
|
919 |
|
\end{document} |
|
920 |
|
} |
|
921 |
|
|
|
922 |
|
\section{Part II --- Problems} |
|
923 |
|
|
|
924 |
|
The three exercises from this homework requires some time to be completed. |
|
925 |
|
The first one will help you to become better at reading ER diagrams, the second will ask you to design one, and the last one will ask you to apply the algorithm to your ER diagram to obtain a relational model. |
|
926 |
|
These three exercises are similar to what you will be asked to do during the second exam. |
|
927 |
|
|
|
928 |
|
I'll assume that you will have successfully completed them by the time Homework \#\the\numexpr\hwNum +1\relax{} is released (\hwDateNext), so don't wait and let me know if you had difficulties solving them. |
|
929 |
|
|
|
930 |
|
\begin{problem}[tags={hw}] |
|
931 |
|
|
|
932 |
|
Consider the ER schema for the MOVIES database in Figure~\ref{fig:movies}. |
|
933 |
|
|
|
934 |
|
\begin{figure} |
|
935 |
|
\begin{tikzpicture} |
|
936 |
|
\node (a) at (0,0) {\includegraphics[page=1,width=1\textwidth]{img/te.pdf}}; |
|
937 |
|
\fill[white] (-8.3, 6.5) rectangle (-3.5, 4); |
|
938 |
|
\end{tikzpicture} |
|
939 |
|
\caption{An ER diagram for a MOVIES database schema.} \label{fig:movies} |
|
940 |
|
\end{figure} |
|
941 |
|
|
|
942 |
|
Assume that MOVIES is a populated database. |
|
943 |
|
ACTOR is used as a gender-netral term. |
|
944 |
|
Given the constraints shown in the ER schema, respond to the following statements with \emph{True} or \emph{False}. |
|
945 |
|
Justify each answer. |
|
946 |
|
|
|
947 |
|
\begin{enumerate} |
|
948 |
|
\item There are no actors in this database that have been in no movies. |
|
949 |
|
\item There might be actors who have acted in more than ten movies. |
|
950 |
|
\item Some actors could have done a lead role in multiple movies. |
|
951 |
|
\item A movie can have only a maximum of two lead actors. |
|
952 |
|
\item Every director have to have been an actor in some movie. |
|
953 |
|
\item No producer has ever been an actor. |
|
954 |
|
\item A producer cannot be an actor in some other movie. |
|
955 |
|
\item There could be movies with more than a dozen actors. |
|
956 |
|
\item Producers can be directors as well. |
|
957 |
|
\item A movie can have one director and one producer. |
|
958 |
|
\item A movie can have one director and several producers. |
|
959 |
|
\item There could be some actors who have done a lead role, directed a movie, and produced some movie. |
|
960 |
|
\item It is impossible for a director to play in the movie (s)he directed. |
|
961 |
|
\end{enumerate} |
|
962 |
|
|
|
963 |
|
\end{problem} |
|
964 |
|
|
|
965 |
|
\begin{answer}[print = true] |
|
966 |
|
\begin{enumerate} |
|
967 |
|
\item true |
|
968 |
|
\item true |
|
969 |
|
\item true |
|
970 |
|
\item true |
|
971 |
|
\item false |
|
972 |
|
\item false |
|
973 |
|
\item false |
|
974 |
|
\item true |
|
975 |
|
\item true |
|
976 |
|
\item true |
|
977 |
|
\item true |
|
978 |
|
\item true |
|
979 |
|
\item false |
|
980 |
|
\end{enumerate} |
|
981 |
|
\end{answer} |
|
982 |
|
|
|
983 |
|
\begin{problem}[tags={hw}] |
|
984 |
|
Draw the ER diagram for the following situation: |
|
985 |
|
A car-insurance company wants to have a database of accidents. |
|
986 |
|
An accident involves cars, drivers, and it has several aspects: the moment and place where it took place, the amount of damages, and a (unique) report number. |
|
987 |
|
A car has a license, a model, a year, and an owner. |
|
988 |
|
A driver has an id, an age, a name, and an address. |
|
989 |
|
|
|
990 |
|
One of the interesting choice is: should \enquote{accident} be an entity type or a relationship type? |
|
991 |
|
|
|
992 |
|
\begin{answer} |
|
993 |
|
|
|
994 |
|
\includegraphics[page=1,width=1\textwidth]{img/p.pdf} |
|
995 |
|
|
|
996 |
|
OR |
|
997 |
|
|
|
998 |
|
|
|
999 |
|
|
|
1000 |
|
\begin{tikzpicture}[node distance=7em] |
|
1001 |
|
\node[entity] (person) {Person}; |
|
1002 |
|
\node[attribute] (pid) [left of=person] {\key{id}} edge (person); |
|
1003 |
|
\node[attribute] (name) [above left of=person] {Name} edge (person); |
|
1004 |
|
\node[attribute] (address) [below left of=person] {address} edge (person); |
|
1005 |
|
|
|
1006 |
|
\node[relationship] (owns) [above right of=person] {Owns} edge node[above, sloped]{$M$} (person); |
|
1007 |
|
\node[entity] (car) [above right of=owns] {Car} edge node[above, sloped]{$N$} (owns); |
|
1008 |
|
\node[attribute] (licence) [above left of=car] {\key{licence}} edge (car); |
|
1009 |
|
\node[attribute] (model) [above of =car] {model} edge (car); |
|
1010 |
|
\node[attribute] (year) [above right of =car] {year} edge (car); |
|
1011 |
|
|
|
1012 |
|
\node[relationship] (accident) [right = 5cm of person] {Accident} edge[total] node[above, sloped]{$M$} (person); |
|
1013 |
|
\node[attribute] (report_number) [below of = accident] {\key{Report Number}} edge (accident); |
|
1014 |
|
\node[attribute] (time) [right of = accident] {Time} edge (accident); |
|
1015 |
|
\node[attribute] (place) [below right of = accident] {Place} edge (accident); |
|
1016 |
|
\node[attribute] (damage_amount) [above right of = accident] {Damage\_Amount} edge (accident); |
|
1017 |
|
|
|
1018 |
|
\draw (car) edge[total] node[above, sloped]{$N$} (accident); |
|
1019 |
|
\end{tikzpicture} |
|
1020 |
|
\end{answer} |
|
1021 |
|
\end{problem} |
|
1022 |
|
|
|
1023 |
|
\begin{problem}[tags={hw}] |
|
1024 |
|
Apply the ER-to-Relation mapping to your ER diagram from the previous problem. |
|
1025 |
|
\end{problem} |
|
1026 |
|
|
|
1027 |
|
%%% 05 |
|
1028 |
|
|
|
1029 |
|
|
|
1030 |
|
\begin{exercise}[tags={hw}] |
|
1031 |
|
What is insertion anomaly? Give an example. |
|
1032 |
|
\end{exercise} |
|
1033 |
|
|
|
1034 |
|
\begin{solution} |
|
1035 |
|
When you have to invent a primary key or add a lot of \sqli{NULL} value to be able to add a tuple. |
|
1036 |
|
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. |
|
1037 |
|
\end{solution} |
|
1038 |
|
|
|
1039 |
|
\begin{exercise}[tags={hw}] |
|
1040 |
|
Why should we avoid attributes whose value will often be \sqli{NULL}? |
|
1041 |
|
Can the usage of \sqli{NULL} be completely avoided? |
|
1042 |
|
\end{exercise} |
|
1043 |
|
|
|
1044 |
|
\begin{solution} |
|
1045 |
|
Because they waste space, and because they are ambiguous (N/A, or unknown, or not communicated?). |
|
1046 |
|
No, it is necessary sometimes. |
|
1047 |
|
\end{solution} |
|
1048 |
|
|
|
1049 |
|
\begin{exercise}[tags={qz}] |
|
1050 |
|
Consider the following relation: |
|
1051 |
|
|
|
1052 |
|
\begin{tabular}{p{3em} l} |
|
1053 |
|
& PROF(\underline{SSN}, Name, Department, Bike\_brand) |
|
1054 |
|
\end{tabular} |
|
1055 |
|
|
|
1056 |
|
Why is it a poor design to have a \enquote{Bike\_brand} attribute in such a relation? |
|
1057 |
|
How should we store this information? |
|
1058 |
|
\end{exercise} |
|
1059 |
|
|
|
1060 |
|
\begin{solution} |
|
1061 |
|
Because it will be \sqli{NULL} most of the time. |
|
1062 |
|
In a separate table. |
|
1063 |
|
\end{solution} |
|
1064 |
|
|
|
1065 |
|
\begin{exercise}[tags={hw}] |
|
1066 |
|
Consider the following relation: |
|
1067 |
|
|
|
1068 |
|
\begin{tabular}{p{3em} l} |
|
1069 |
|
& STUDENT(\underline{SSN}, Name, \(\hdots\), Sibling\_On\_Campus) |
|
1070 |
|
\end{tabular} |
|
1071 |
|
|
|
1072 |
|
Why is it a poor design to have a \enquote{Sibling\_On\_Campus} attribute in such a relation? |
|
1073 |
|
How should we store this information? |
|
1074 |
|
\end{exercise} |
|
1075 |
|
|
|
1076 |
|
\begin{solution} |
|
1077 |
|
Because it will be \sqli{NULL} most of the time, and because students could have more than one sibling on campus. |
|
1078 |
|
In a separate table. |
|
1079 |
|
\end{solution} |
|
1080 |
|
|
|
1081 |
|
\begin{exercise}[tags={hw}] |
|
1082 |
|
Consider the following relational database schema: |
|
1083 |
|
|
|
1084 |
|
\begin{tabular}{p{3em} l} |
|
1085 |
|
& STUDENT(\underline{Login}, Name, \(\hdots\), Major, Major\_Head)\\ |
|
1086 |
|
& DEPARTMENT(\underline{Code}, Name, Major\_Head) |
|
1087 |
|
\end{tabular} |
|
1088 |
|
|
|
1089 |
|
Assuming that \enquote{Major} is a foreign key referencing \enquote{DEPARTMENT.Code}, what is the problem with that schema? How could you address it? |
|
1090 |
|
\end{exercise} |
|
1091 |
|
|
|
1092 |
|
\begin{solution} |
|
1093 |
|
Major\_Head will give update anomalies. |
|
1094 |
|
By putting the Head of the department in the DEPARTMENT relation only, i.e., removing it from STUDENT. |
|
1095 |
|
\end{solution} |
|
1096 |
|
|
|
1097 |
|
\begin{exercise}[tags={hw}] |
|
1098 |
|
Consider the relation \(R(A , B, C, D, E, F)\) and the following functional dependencies: |
|
1099 |
|
\begin{enumerate} |
|
1100 |
|
\item \(F \to \{D, C\}, D \to \{B, E\}, \{B, E\} \to A\) |
|
1101 |
|
\item \(\{A, B\} \to \{C, D\}, \{B, E\} \to F\) |
|
1102 |
|
\item \(A \to \{C, D\}, E \to F, D \to B\) |
|
1103 |
|
\end{enumerate} |
|
1104 |
|
For each set of functional dependency, give a key for \(R\). We want a key, so it has to be minimal. |
|
1105 |
|
\end{exercise} |
|
1106 |
|
|
|
1107 |
|
\begin{solution} |
|
1108 |
|
\begin{enumerate} |
|
1109 |
|
\item \(F\) |
|
1110 |
|
\item \(\{A, B, E\}\) |
|
1111 |
|
\item \(\{A, E\}\) |
|
1112 |
|
\end{enumerate} |
|
1113 |
|
\end{solution} |
|
1114 |
|
|
|
1115 |
|
\begin{exercise}[tags={none}] |
|
1116 |
|
Consider the relation \(R(A, B, C, D, E, F)\) and the following functional dependencies: |
|
1117 |
|
\[ A \to \{D, E\}, D \to \{B, F\}, \{B, E\} \to A, \{A,C\} \to \{B, D, F\}, A \to F\] |
|
1118 |
|
Answer the following: |
|
1119 |
|
\begin{enumerate} |
|
1120 |
|
\item How many candidate keys is there? List them. |
|
1121 |
|
\item How many transitive dependencies can you find? Give them and justify them. |
|
1122 |
|
\end{enumerate} |
|
1123 |
|
\end{exercise} |
|
1124 |
|
|
|
1125 |
|
\begin{solution} |
|
1126 |
|
\begin{enumerate} |
|
1127 |
|
\item \(\{A, C\}\), |
|
1128 |
|
\item \(A \to F\) by \(A \to D, D\to F\). |
|
1129 |
|
\end{enumerate} |
|
1130 |
|
\end{solution} |
|
1131 |
|
|
|
1132 |
|
\begin{exercise}[tags={hw}] |
|
1133 |
|
Consider the relation \(R(A, B, C, D)\) and answer the following: |
|
1134 |
|
\begin{enumerate} |
|
1135 |
|
\item 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. |
|
1136 |
|
\item 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. |
|
1137 |
|
\end{enumerate} |
|
1138 |
|
\end{exercise} |
|
1139 |
|
|
|
1140 |
|
\begin{solution} |
|
1141 |
|
\(1.\) Yes. \(C\) and \(D\) are non prime, and they fully depend on \(\{A,B\}\). |
|
1142 |
|
|
|
1143 |
|
\(2.\) No. \(D\) is the only non prime, and it depends only on \(A\). |
|
1144 |
|
\end{solution} |
|
1145 |
|
|
|
1146 |
|
\begin{exercise}[tags={hw}] |
|
1147 |
|
Consider the relation \(R(A, B, C, D, E, F)\) with candidate keys \(\{A, B\}\) and \(C\). Answer the following: |
|
1148 |
|
\begin{enumerate} |
|
1149 |
|
\item What are the prime attributes in \(R\)? |
|
1150 |
|
\item Is \(\{C,D\} \to E\) a fully functional dependency? |
|
1151 |
|
\item Write a set of functional dependencies containing at least one transitive depency, and justify your answer. |
|
1152 |
|
\end{enumerate} |
|
1153 |
|
\end{exercise} |
|
1154 |
|
|
|
1155 |
|
\begin{solution} |
|
1156 |
|
\begin{enumerate} |
|
1157 |
|
\item\(A,B,C\) |
|
1158 |
|
\item No, because we can remove \(D\), |
|
1159 |
|
\item \(A \to D\), \(D \to E\) and \(A \to E\) |
|
1160 |
|
\end{enumerate} |
|
1161 |
|
\end{solution} |
|
1162 |
|
|
|
1163 |
|
\begin{exercise}[tags={qz}] |
|
1164 |
|
Consider the relation \(R(A , B, C, D, E)\) and the following functional dependencies: |
|
1165 |
|
\begin{enumerate} |
|
1166 |
|
\item \(C \to D, \{C, B\} \to A, A \to \{B, C, D\}, B \to E\) |
|
1167 |
|
\item \(\ A \to \{C, D\}, C \to B, D \to E, \{E, C\} \to A\) |
|
1168 |
|
% \item \(\{A, B\} \to D, D \to \{B, C\}, E \to C\) |
|
1169 |
|
\end{enumerate} |
|
1170 |
|
For each one, give one candidate key for \(R\).%two candidate keys for \(R\). |
|
1171 |
|
\end{exercise} |
|
1172 |
|
|
|
1173 |
|
\begin{solution} |
|
1174 |
|
\begin{enumerate} |
|
1175 |
|
\item \(\{B, C\}\), \(A\) |
|
1176 |
|
\item \(A\), \(\{C,E\}\), |
|
1177 |
|
\item \(\{A, D, E\}\), \(\{A, B, E\}\) |
|
1178 |
|
\end{enumerate} |
|
1179 |
|
\end{solution} |
|
1180 |
|
|
|
1181 |
|
\begin{exercise}[tags={qz}] |
|
1182 |
|
Consider the relation \(R(A, B, C, D, E)\) and answer the following: |
|
1183 |
|
\begin{enumerate}[topsep=0pt,itemsep=10em,partopsep=1ex,parsep=1ex] |
|
1184 |
|
\item If \(\{A, B\}\) is the primary key, is \(B \to E, C \to D\) a 2NF?% List the nonprime attributes and justify. |
|
1185 |
|
\item If \(\{A\}\) is the primary key, is \(B \to C, B \to D\) a 2NF?% List the nonprime attributes and justify. |
|
1186 |
|
\end{enumerate} |
|
1187 |
|
\end{exercise} |
|
1188 |
|
|
|
1189 |
|
\begin{solution} |
|
1190 |
|
\(1.\) No. \(C, D, E\), and \(E\) has a partial relation to \(B\) |
|
1191 |
|
|
|
1192 |
|
\(2.\) Yes. Since the primary key is a singleton, it is obvious. |
|
1193 |
|
\end{solution} |
|
1194 |
|
|
|
1195 |
|
\begin{exercise}[tags={qz}] |
|
1196 |
|
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\). |
|
1197 |
|
This relation is not in 3NF, can you tell why?% List the nonprime attributes and justify. |
|
1198 |
|
% \item If \(A\) is the primary key, is \(\{A, B\} \to C, \{A, B, C\} \to E\) a 3NF?% List the nonprime attributes and justify. |
|
1199 |
|
\end{enumerate} |
|
1200 |
|
\end{exercise} |
|
1201 |
|
|
|
1202 |
|
\begin{solution} |
|
1203 |
|
\(1.\) No. \(A, C, E, F\) are non-prime, \(\{B, D\} \to C \to F\) breaks the 3NF. |
|
1204 |
|
|
|
1205 |
|
\(2.\) Yes. \(B, C, E, F\), \(\{A,B\}\) and \(\{A, B, C\}\) are not strict subset of the primary key. |
|
1206 |
|
\end{solution} |
|
1207 |
|
|
|
1208 |
|
\begin{exercise}[tags={hw}] |
|
1209 |
|
Consider the relation \(R(A, B, C, D)\) and answer the following: |
|
1210 |
|
\begin{enumerate} |
|
1211 |
|
\item 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. |
|
1212 |
|
\item 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. |
|
1213 |
|
\end{enumerate} |
|
1214 |
|
\end{exercise} |
|
1215 |
|
|
|
1216 |
|
\begin{solution} |
|
1217 |
|
\(1.\) No. \(B\), \(C\) and \(D\) are non prime, \(A \to \{B,C\} \to D\) breaks the 3NF. |
|
1218 |
|
|
|
1219 |
|
\(2.\) No. \(A\), \(B\) and \(D\) are non prime, \(B \to \{A,C\} \to D\) breaks the 3NF. |
|
1220 |
|
\end{solution} |
|
1221 |
|
|
|
1222 |
|
|
|
1223 |
|
\IfTagSetF{hw}{ |
|
1224 |
|
\end{document} |
|
1225 |
|
} |
|
1226 |
|
|
|
1227 |
|
\section{Part II --- Problems} |
|
1228 |
|
|
|
1229 |
|
This part will help you in assessing your level of understanding of this lecture, and give you an idea of the kind of problem you will be asked to solve during the exams. |
|
1230 |
|
I'll assume that you will have successfully completed those two problems by the time Homework \#\the\numexpr\hwNum +1\relax{} is released (\hwDateNext), so don't wait and let me know if you had difficulties solving them. |
|
1231 |
|
|
|
1232 |
|
\begin{problem}[tags={none}] |
|
1233 |
|
Consider the following relation, and its functional dependencies: |
|
1234 |
|
|
|
1235 |
|
\begin{tabular}{l} |
|
1236 |
|
CAR\_SALE(\underline{Car\_no}, Date\_sold, \underline{Salesman\_no}, Commission, Discount\_amt) |
|
1237 |
|
\end{tabular} |
|
1238 |
|
|
|
1239 |
|
\begin{tabular}{rcl} |
|
1240 |
|
\{Car\_no, Salesman\_no\} & \(\to\)& \{Date\_sold, Commission, Discount\_amt\}\\ |
|
1241 |
|
Date\_sold & \(\to\)& Discount\_amt\\ |
|
1242 |
|
Salesman\_no & \(\to\)& Commission |
|
1243 |
|
\end{tabular} |
|
1244 |
|
|
|
1245 |
|
Based on the given primary key, is this relation in 1NF, 2NF, or 3NF? Why or why not? Normalize it completely. Then draw an ER diagram for the resulting schema. |
|
1246 |
|
\end{problem} |
|
1247 |
|
|
|
1248 |
|
\begin{answer} |
|
1249 |
|
This relation satisfies 1NF but not 2NF (Car\_no \(\to\) Date\_sold and Salesman\_no \(\to\) Commission so these two attributes are not fully functional dependent on the primary key) and not 3NF. |
|
1250 |
|
|
|
1251 |
|
To normalize, |
|
1252 |
|
|
|
1253 |
|
2NF: |
|
1254 |
|
\begin{tabular}{l} |
|
1255 |
|
Car\_Sale1(Car\_no, Date\_sold, Discount\_amt)\\ |
|
1256 |
|
Car\_Sale2(Car\_no, Salesman\_no)\\ |
|
1257 |
|
Car\_Sale3(Salesman\_no,Commission) |
|
1258 |
|
\end{tabular} |
|
1259 |
|
3NF: |
|
1260 |
|
\begin{tabular}{c} |
|
1261 |
|
Car\_Sale1-1(Car\_no, Date\_sold)\\ |
|
1262 |
|
Car\_Sale1-2(Date\_sold, Discount\_amt)\\ |
|
1263 |
|
Car\_Sale2(Car\_no, Salesman\_no)\\ |
|
1264 |
|
Car\_Sale3(Salesman\_no,Commission) |
|
1265 |
|
\end{tabular} |
|
1266 |
|
\end{answer} |
|
1267 |
|
|
|
1268 |
|
\begin{problem}[tags={hw}] |
|
1269 |
|
This problem asks you to convert business statements into dependencies. |
|
1270 |
|
Consider the following relation: |
|
1271 |
|
\begin{center} |
|
1272 |
|
BIKE(Serial\_no, Manufacturer, Model, Batch, Wheel\_size, Retailer) |
|
1273 |
|
\end{center} |
|
1274 |
|
|
|
1275 |
|
Each tuple in the relation BIKE contains information about a bike with a serial number, made by a manufacturer, with a particular model number, released in a certain batch, which has a certain wheel size, and is sold by a certain retailer. |
|
1276 |
|
\begin{itemize} |
|
1277 |
|
\item Write each of the following dependencies as a functional dependency (I give you the first one as an example): |
|
1278 |
|
|
|
1279 |
|
\begin{enumerate} |
|
1280 |
|
\item A retailer can't have two bikes of the same model from different batches. |
|
1281 |
|
|
|
1282 |
|
\emph{solution:} \{Retailer, Model\} \(\to\) Batch |
|
1283 |
|
\item The manufacturer and serial number uniquely identifies the bike and where it is sold. |
|
1284 |
|
\item A model number is registered by a manufacturer and therefore can't be |
|
1285 |
|
used by another manufacturer. |
|
1286 |
|
\item All bikes in a particular batch are of the same model. |
|
1287 |
|
\item All bikes of a certain model have |
|
1288 |
|
the same wheel size. |
|
1289 |
|
\end{enumerate} |
|
1290 |
|
\item Based on those statements, what could be a key for this relation? |
|
1291 |
|
\item Assuming all those functional dependencies hold, and taking the primary key you identified at the previous step, what is the degree of normality of this relation? Justify your answer. |
|
1292 |
|
\end{itemize} |
|
1293 |
|
\end{problem} |
|
1294 |
|
|
|
1295 |
|
\begin{answer} |
|
1296 |
|
\begin{itemize} |
|
1297 |
|
\item |
|
1298 |
|
\begin{enumerate} |
|
1299 |
|
\item \{ Manufacturer, Serial\_no \} \(\to\) \{ Model, Batch, Wheel\_size, Retailer\} |
|
1300 |
|
\item Model \(\to\) Manufacturer |
|
1301 |
|
\item Batch \(\to\) Model |
|
1302 |
|
\item \{Model, Manufacturer\} \(\to\) Wheel\_size |
|
1303 |
|
\end{enumerate} |
|
1304 |
|
\item \{Manufacturer, Serial\_no \} |
|
1305 |
|
\item If every attribute is atomic, it is in second nf. |
|
1306 |
|
\{ Manufacturer, Serial\_no \} \(\to\) Batch \(\to\)Model breaks the 3NF. |
|
1307 |
|
|
|
1308 |
|
\end{itemize} |
|
1309 |
|
\end{answer} |
|
1310 |
|
|
|
1311 |
|
|
|
1312 |
|
\begin{problem}[tags={hw}] |
|
1313 |
|
Consider the relations \(R\) and \(T\) below, and their functional dependencies (on top of the one induced by the primary keys): |
|
1314 |
|
|
|
1315 |
|
\begin{tabular}{l} |
|
1316 |
|
R(\underline{EventId}, \underline{Email}, Time, Date, Location, Status)\\ |
|
1317 |
|
T(\underline{Invno}, Subtotal, Tax, Total, Email, Lname, Fname, Phone) |
|
1318 |
|
\end{tabular} |
|
1319 |
|
|
|
1320 |
|
\begin{tabular}{r c l} |
|
1321 |
|
\{EventId, Email\} & \(\to\) & Status\\ |
|
1322 |
|
EventId & \(\to\) & \{Time, Date, Location\}\\ |
|
1323 |
|
Invno & \(\to\) & \{Subtotal, Tax, Total, Email\}\\ |
|
1324 |
|
Email & \(\to\) & \{Fname, Lname, Phone\} |
|
1325 |
|
\end{tabular} |
|
1326 |
|
|
|
1327 |
|
Normalize the relations to 2NF and 3NF. Show all relations at each stage (2NF and 3NF) of the normalization process. |
|
1328 |
|
\end{problem} |
|
1329 |
|
|
|
1330 |
|
\begin{answer} |
|
1331 |
|
TO BE WRITTEN |
|
1332 |
|
\end{answer} |
|
1333 |
|
|
|
1334 |
|
|
|
1335 |
|
\begin{problem}[tags={hw}] |
|
1336 |
|
Consider the following relation for published books: |
|
1337 |
|
\begin{center} |
|
1338 |
|
BOOK (Book\_title, Book\_type, Author\_name, List\_price, Author\_affil, Publisher) |
|
1339 |
|
\end{center} |
|
1340 |
|
|
|
1341 |
|
Suppose we have the following dependencies: |
|
1342 |
|
|
|
1343 |
|
\begin{center} |
|
1344 |
|
\begin{tabular}{r c l} |
|
1345 |
|
Book\_title & \(\to\) & \{ Publisher, Book\_type \}\\ |
|
1346 |
|
Book\_type & \(\to\) & List\_price\\ |
|
1347 |
|
Author\_name & \(\to\) & Author\_affil |
|
1348 |
|
\end{tabular} |
|
1349 |
|
\end{center} |
|
1350 |
|
|
|
1351 |
|
\begin{itemize} |
|
1352 |
|
\item What would be a suitable key for this relation? |
|
1353 |
|
\item How could this relation not be in first normal form? Explain your answer. |
|
1354 |
|
\item This relation is not in second normal form: explain why and normalize it. |
|
1355 |
|
\item Is the relations you obtained at the previous step in third normal form? Explain why, and normalize them if needed. |
|
1356 |
|
\end{itemize} |
|
1357 |
|
\end{problem} |
|
1358 |
|
|
|
1359 |
|
|
|
1360 |
|
\begin{answer} |
|
1361 |
|
\begin{itemize} |
|
1362 |
|
\item \{Book Title, Author Name\} |
|
1363 |
|
\item If an attribute is composite or multi-valued. |
|
1364 |
|
\item Because of \{ Book\_title \}\(\to\) \{ Publisher, Book\_type \}. |
|
1365 |
|
We can normalize it as |
|
1366 |
|
(Book Title, Publisher, Book Type, List Price), (Author Name, Author Affiliation), (Author Name, Book Title). |
|
1367 |
|
\item Because of \{Book title\} \(\to\) \{ Book\_type\} \(\to\) \{ List\_price\}\\ |
|
1368 |
|
(Book Title, Publisher, Book Type) and (Book Type, List Price), (Author Name, Author Affiliation), (Author Name, Book Title). |
|
1369 |
|
|
|
1370 |
|
\end{itemize} |
|
1371 |
|
\end{answer} |
|
1372 |
|
|
|
1373 |
|
%%% 06 |
|
1374 |
|
|
|
1375 |
|
|
|
1376 |
|
\begin{exercise}[tags={hw}] |
|
1377 |
|
Consider the relation \(R(A, B, C, D, E)\) and the functional dependencies \( \{A, B\} \to C, B \to D, C \to E\). |
|
1378 |
|
Answer the following: |
|
1379 |
|
|
|
1380 |
|
\begin{enumerate} |
|
1381 |
|
\item \(A\) by itself is not a primary key, but what is the only key that contains \(A\)? |
|
1382 |
|
\item List the non-prime attributes. |
|
1383 |
|
\item This relation is not in 2NF: what transformation can you operate to obtain a 2NF? |
|
1384 |
|
\item One of the relation you obtained at the previous step is likely not to be in 3NF. |
|
1385 |
|
Can you normalize it? |
|
1386 |
|
If yes, how? |
|
1387 |
|
\end{enumerate} |
|
1388 |
|
\end{exercise} |
|
1389 |
|
|
|
1390 |
|
\begin{solution} |
|
1391 |
|
\(\{A, B\}\), |
|
1392 |
|
|
|
1393 |
|
\(C, D, E\), |
|
1394 |
|
|
|
1395 |
|
\(R_1(A, B, C, E)\) and \(R_2(B, D)\) |
|
1396 |
|
|
|
1397 |
|
\(R_1(A, B, C)\), \(R_2(C, E)\) and \(R_3(B, D)\) |
|
1398 |
|
\end{solution} |
|
1399 |
|
|
|
1400 |
|
|
|
1401 |
|
\begin{exercise}[tags={hw,qz}] |
|
1402 |
|
What are the two different categories of U.M.L. diagram? |
|
1403 |
|
\end{exercise} |
|
1404 |
|
|
|
1405 |
|
\begin{solution} |
|
1406 |
|
Behaviour and structure |
|
1407 |
|
\end{solution} |
|
1408 |
|
|
|
1409 |
|
\begin{exercise}[tags={hw,qz}] |
|
1410 |
|
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? |
|
1411 |
|
Justify your answer. |
|
1412 |
|
\end{exercise} |
|
1413 |
|
|
|
1414 |
|
\begin{solution} |
|
1415 |
|
Yes, U.M.L. diagram is language-independent and platform-independent. |
|
1416 |
|
\end{solution} |
|
1417 |
|
|
|
1418 |
|
\begin{exercise}[tags={hw}] |
|
1419 |
|
What kind of diagram should we use if we want to \(\hdots\) |
|
1420 |
|
\begin{enumerate} |
|
1421 |
|
\item describe the functional behavior of the system as seen by the user? |
|
1422 |
|
\item capture the flow of messages in a software? |
|
1423 |
|
\item represent the workflow of actions of an user? |
|
1424 |
|
\end{enumerate} |
|
1425 |
|
\end{exercise} |
|
1426 |
|
|
|
1427 |
|
\begin{solution} |
|
1428 |
|
\begin{enumerate} |
|
1429 |
|
\item Use-case |
|
1430 |
|
\item Sequence diagram |
|
1431 |
|
\item Activity diagram |
|
1432 |
|
\end{enumerate} |
|
1433 |
|
\end{solution} |
|
1434 |
|
|
|
1435 |
|
|
|
1436 |
|
\begin{exercise}[tags={hw,qz}] |
|
1437 |
|
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. |
|
1438 |
|
\end{exercise} |
|
1439 |
|
|
|
1440 |
|
\begin{solution} |
|
1441 |
|
To use direction for association, to have a common language with someone less knowledgeable of other diagrammatic notations. |
|
1442 |
|
For the concept of integration. |
|
1443 |
|
\end{solution} |
|
1444 |
|
|
|
1445 |
|
|
|
1446 |
|
|
|
1447 |
|
\begin{exercise}[tags={hw}] |
|
1448 |
|
Consider the following diagram: |
|
1449 |
|
|
|
1450 |
|
\begin{tikzpicture}[scale = 0.8] |
|
1451 |
|
\begin{class}[text width=7cm]{Flight}{0,0} |
|
1452 |
|
\attribute{flightNumber : Integer} |
|
1453 |
|
\attribute{departureTime : Date} |
|
1454 |
|
\attribute{flightDuration : Minutes} |
|
1455 |
|
\attribute{departingAirport : String} |
|
1456 |
|
\attribute{arrivingAirport : String} |
|
1457 |
|
\operation{delayFlight(numberOfMinutes : Minutes)} |
|
1458 |
|
\operation{getArrivalTime( ) : Date} |
|
1459 |
|
\end{class} |
|
1460 |
|
\begin{class}{Plane}{11, -0.8} |
|
1461 |
|
\attribute{airPlaneType : String} |
|
1462 |
|
\attribute{maximumSpeed : MPH} |
|
1463 |
|
\attribute{maximumDistance : Miles} |
|
1464 |
|
\attribute{tailID : String} |
|
1465 |
|
\end{class} |
|
1466 |
|
\association{Plane}{}{0..1}{Flight}{0..*}{} |
|
1467 |
|
\end{tikzpicture} |
|
1468 |
|
|
|
1469 |
|
Give the number of attributes for both classes, and suggest two operations for the class that doesn't have any. |
|
1470 |
|
Discuss the multiplicities: why did the designer picked those values? |
|
1471 |
|
\end{exercise} |
|
1472 |
|
|
|
1473 |
|
\begin{solution} |
|
1474 |
|
\(5\) and \(4\). |
|
1475 |
|
|
|
1476 |
|
getLastFlightNumber() : Integer |
|
1477 |
|
|
|
1478 |
|
stMaximumSpeed(MPH) : void |
|
1479 |
|
|
|
1480 |
|
A flight could be assigned to no plane, and a plane could not be assigned to a flight. |
|
1481 |
|
|
|
1482 |
|
A plane can be assigned to multiple (or no) flights, but a flight must have at most one plane (and could have none). |
|
1483 |
|
\end{solution} |
|
1484 |
|
|
|
1485 |
|
\begin{exercise}[tags={hw}] |
|
1486 |
|
Briefly explain the difference between an aggregation and a composition association. |
|
1487 |
|
\end{exercise} |
|
1488 |
|
|
|
1489 |
|
\begin{solution} |
|
1490 |
|
Aggregation: associated class can have an existence of its own. |
|
1491 |
|
|
|
1492 |
|
Composition association: class doesn't exist without the association. |
|
1493 |
|
\end{solution} |
|
1494 |
|
|
|
1495 |
|
\begin{exercise}[tags={hw}] |
|
1496 |
|
How is generalization (or inheritance) represented in a U.M.L. class diagram? |
|
1497 |
|
Why is such a concept useful? |
|
1498 |
|
\end{exercise} |
|
1499 |
|
|
|
1500 |
|
\begin{solution} |
|
1501 |
|
\tikz{\draw[>=open triangle 60,->] (0,0) to (1,0);} |
|
1502 |
|
Because it avoids redundancy. |
|
1503 |
|
\end{solution} |
|
1504 |
|
|
|
1505 |
|
\begin{exercise}[tags={qz}] |
|
1506 |
|
Convert the following E.R. diagram into a U.M.L. class diagram: |
|
1507 |
|
|
|
1508 |
|
\begin{tikzpicture}[node distance=8em] |
|
1509 |
|
\node[entity] (person) {PILOT}; |
|
1510 |
|
\node[attribute] (pid) [left of=person] {\key{ID}} edge (person); |
|
1511 |
|
\node[attribute] (name) [above left of=person] {Name} edge (person); |
|
1512 |
|
\node[attribute] (phone) [above of=person] {Experience} edge (person); |
|
1513 |
|
|
|
1514 |
|
\node[relationship] (drives) [below of=person] {ASSIGNED\_TO} edge node[right, pos=0.1] {$N$} (person); |
|
1515 |
|
\node[entity] (plane) [below of=drives] {PLANE} edge [total] node[right, pos=0.7] {$1$} (drives); |
|
1516 |
|
\node[attribute] (make) [left of=plane] {\key{TailID}} edge (plane); |
|
1517 |
|
\node[attribute] (maxS) [below left of =plane] {MaxSpeed} edge (plane); |
|
1518 |
|
\node[attribute] (airPlaneType) [below of = plane] {AirPlaneType} edge (plane); |
|
1519 |
|
\end{tikzpicture} |
|
1520 |
|
\end{exercise} |
|
1521 |
|
|
|
1522 |
|
\IfTagSetF{hw}{ |
|
1523 |
|
\vspace{-8em} % ugly hack… |
|
1524 |
|
\end{document} |
|
1525 |
|
} |
|
1526 |
|
|
|
1527 |
|
\section{Part II --- Problems} |
|
1528 |
|
|
|
1529 |
|
This part will help you in assessing your level of understanding of this lecture, prepare you for the rest of your cursus, and give you an idea of the kind of problem you will be asked to solve during the exams. |
|
1530 |
|
I'll assume that you will have successfully completed those problems by the time Homework \#\the\numexpr\hwNum +1\relax{} is released (\hwDateNext), so don't wait and let me know if you had difficulties solving them. |
|
1531 |
|
|
|
1532 |
|
\begin{problem}[tags={hw}] |
|
1533 |
|
\label{probMYSQLW} |
|
1534 |
|
Consider the following E.R. schema for the CAR\_INFO database: |
|
1535 |
|
|
|
1536 |
|
\begin{tikzpicture}[node distance=8em] |
|
1537 |
|
\node[entity] (person) {PERSON}; |
|
1538 |
|
\node[attribute] (pid) [left of=person] {\key{ID}} edge (person); |
|
1539 |
|
\node[attribute] (name) [above left of=person] {Name} edge (person); |
|
1540 |
|
\node[multi attribute] (phone) [above of=person] {Phone} edge (person); |
|
1541 |
|
\node[attribute] (address) [above right of=person] {Address} edge (person); |
|
1542 |
|
\node[attribute] (street) [above right of=address] {Street} edge (address); |
|
1543 |
|
\node[attribute] (city) [right of=address] {City} edge (address); |
|
1544 |
|
% \node[derived attribute] (age) [right of=person] {Age} edge (person); |
|
1545 |
|
|
|
1546 |
|
\node[relationship] (drives) [below right of=person] {DRIVES} edge node[above, pos=0.1] {$1$} (person); |
|
1547 |
|
\node[entity] (car) [below left of=drives] {CAR} edge node[above, pos=0.7] {$1$} (drives); |
|
1548 |
|
\node[attribute] (make) [left of=car] {Make} edge (car); |
|
1549 |
|
\node[attribute] (year) [below left of =car] {Year} edge (car); |
|
1550 |
|
\node[attribute] (brand) [below of =car] {Brand} edge (car); |
|
1551 |
|
|
|
1552 |
|
\node[relationship] (seats) [below left of=person] {SEATS\_IN} edge node[above, pos=0.1] {$N$} (person); |
|
1553 |
|
\draw (seats) edge node[above, pos=0.3] {$1$} (car); |
|
1554 |
|
\node[attribute] (position) [left of=seats] {Position} edge (seats); |
|
1555 |
|
|
|
1556 |
|
\node[ident relationship] (insured) [right of=car] {INSURED} edge node[above, pos=0.3] {$1$} (car); |
|
1557 |
|
\node[weak entity] (insurance) [right = 1cm of insured] {CAR\_INSURANCE} edge[total] node[above, pos=0.7] {$N$} (insured); |
|
1558 |
|
\node[attribute] (amount) [above of =insurance] {Covered Amount} edge (insurance); |
|
1559 |
|
\node[attribute] (policy) [above right of =insurance] {Policy Number} edge (insurance); |
|
1560 |
|
\node[attribute] (company) [below right of =insurance] {Company Name} edge (insurance); |
|
1561 |
|
\end{tikzpicture} |
|
1562 |
|
|
|
1563 |
|
Note that a car can have at most one driver, \(N\) passengers, \(N\) insurances, and that car insurances exist only if they are \enquote{tied up} to a car (i.e., they are weak entities, and their identifying relationship is called \enquote{Insured}). |
|
1564 |
|
|
|
1565 |
|
\begin{enumerate} |
|
1566 |
|
\item Find the key attribute for \enquote{Car}, and the partial key for \enquote{Car Insurance}. If you can't think of any, add a dummy attribute and make it be the key. |
|
1567 |
|
\item Convert that E.-R. diagram to a relational database schema. \label{ERtoREL} |
|
1568 |
|
\item 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. \label{ERtoUML} |
|
1569 |
|
\end{enumerate} |
|
1570 |
|
\end{problem} |
|
1571 |
|
|
|
1572 |
|
|
|
1573 |
|
\begin{answer} |
|
1574 |
|
For \enquote{Car}, we need to create an attribute, like \enquote{vin}. |
|
1575 |
|
For \enquote{Car Insurance}, \enquote{Policy Number} is perfect. |
|
1576 |
|
|
|
1577 |
|
\colorlet{lightgray}{gray!20} |
|
1578 |
|
|
|
1579 |
|
\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} |
|
1580 |
|
|
|
1581 |
|
% RELATIONS |
|
1582 |
|
|
|
1583 |
|
\node (phonetitle) {\textbf{PHONE}}; |
|
1584 |
|
|
|
1585 |
|
\node [relation=2, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of phonetitle.west, anchor=west] (phone) |
|
1586 |
|
{\underline{id}% |
|
1587 |
|
\nodepart{two} \underline{number} |
|
1588 |
|
}; |
|
1589 |
|
|
|
1590 |
|
\node [below=1.3cm of phone.west, anchor=west] (persontitle) {\textbf{PERSON}}; |
|
1591 |
|
|
|
1592 |
|
\node [relation=6, rectangle split horizontal, rectangle split part fill={lightgray!50}, below=0.6cm of persontitle.west, anchor=west] (person) |
|
1593 |
|
{\underline{id}% |
|
1594 |
|
\nodepart{two} Name |
|
1595 |
|
\nodepart{three} Street |
|
1596 |
|
\nodepart{four} City |
|
1597 |
|
\nodepart{five} Seat |
|
1598 |
|
\nodepart{six}{Position} |
|
1599 |
|
}; |
|
1600 |
|
|
|
1601 |
|
\node [below=1.1cm of person.west, anchor=west] (cartitle) {\textbf{CAR}}; |
|
1602 |
|
|
|
1603 |
|
\node [relation=5, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of cartitle.west, anchor=west] (car) |
|
1604 |
|
{\underline{Vin}% |
|
1605 |
|
\nodepart{two} Make |
|
1606 |
|
\nodepart{three} Year |
|
1607 |
|
\nodepart{four} Brand |
|
1608 |
|
\nodepart{five} Driver |
|
1609 |
|
}; |
|
1610 |
|
|
|
1611 |
|
\node [below=1.4cm of car.west, anchor=west] (carinsurancetitle) {\textbf{CAR INSURANCE}}; |
|
1612 |
|
|
|
1613 |
|
\node [relation=4, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of carinsurancetitle.west, anchor=west] (carinsurance) |
|
1614 |
|
{\underline{Policy Number}% |
|
1615 |
|
\nodepart{two} Covered Amount |
|
1616 |
|
\nodepart{three} Company Name |
|
1617 |
|
\nodepart{four} Insured Car |
|
1618 |
|
}; |
|
1619 |
|
|
|
1620 |
|
% FOREIGN KEYS |
|
1621 |
|
|
|
1622 |
|
\draw[-latex] (phone.one south) -- ++(0,-0.2) --++(7,0) --++(0, -2) -| ($(person.one south) + (0.2,0)$); |
|
1623 |
|
%\draw[-latex] (person.five south) -- ++(0,-0.2) --++(3,0) --++(0, -2) -| ($(car.one south) + (-0.2,0)$); |
|
1624 |
|
\draw[-latex] (carinsurance.four south) -- ++(0,-0.2) --++(2,0) --++(0, 1.75) -| ($(car.one south) + (0.1,0)$); |
|
1625 |
|
\draw[-latex] (car.five south) -- ++(0,-0.2) --++(2,0) --++(0, 1.5) -| ($(person.one south) + (-0.1,0)$); |
|
1626 |
|
\end{tikzpicture} |
|
1627 |
|
|
|
1628 |
|
|
|
1629 |
|
\begin{tikzpicture}[scale = 0.8] |
|
1630 |
|
\node (person) at (0,0){\begin{tabular}{| r l |} |
|
1631 |
|
\hline |
|
1632 |
|
\multicolumn{2}{| c |}{\textbf{Person}}\\ |
|
1633 |
|
\hline |
|
1634 |
|
id &: String\\ |
|
1635 |
|
name &: String\\ |
|
1636 |
|
address &: Street\\ |
|
1637 |
|
& City\\ |
|
1638 |
|
\hline |
|
1639 |
|
getAge()&: Int\\ |
|
1640 |
|
\hline |
|
1641 |
|
\end{tabular} |
|
1642 |
|
}; |
|
1643 |
|
\node (phone) at (8, 1){\begin{tabular}{| r l |} |
|
1644 |
|
\hline |
|
1645 |
|
\multicolumn{2}{| c |}{\textbf{Phone}}\\ |
|
1646 |
|
\hline |
|
1647 |
|
number &: String\\ |
|
1648 |
|
\hline |
|
1649 |
|
\end{tabular} |
|
1650 |
|
}; |
|
1651 |
|
\draw[open diamond-] (person) -- node[above, pos=0.1]{\(0..\star\)} node[above, pos=0.9]{\(1..1\)} (phone); |
|
1652 |
|
\node (car) at (8, -3){\begin{tabular}{| r l |} |
|
1653 |
|
\hline |
|
1654 |
|
\multicolumn{2}{| c |}{\textbf{Car}}\\ |
|
1655 |
|
\hline |
|
1656 |
|
vin &: String\\ |
|
1657 |
|
make &: String\\ |
|
1658 |
|
year &: Year\\ |
|
1659 |
|
brand &: String\\ |
|
1660 |
|
\hline |
|
1661 |
|
\end{tabular} |
|
1662 |
|
}; |
|
1663 |
|
\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); |
|
1664 |
|
%\draw[-] (person) -- node[below, pos=0.1]{\(0..1\)} node[below, pos=0.9]{\(0..4\)} ($(car)+(-2.5, 0.5)$); |
|
1665 |
|
\node (seat) at (3.5, -5){\begin{tabular}{| r l |} |
|
1666 |
|
\hline |
|
1667 |
|
\multicolumn{2}{| c |}{\textbf{Seats In}}\\ |
|
1668 |
|
\hline |
|
1669 |
|
position &: String\\ |
|
1670 |
|
\hline |
|
1671 |
|
\end{tabular} |
|
1672 |
|
}; |
|
1673 |
|
\draw[dashed] (seat) -- (3.5, -1.6); |
|
1674 |
|
|
|
1675 |
|
\node (insu) at (14.3, -2){\begin{tabular}{| r l |} |
|
1676 |
|
\hline |
|
1677 |
|
\multicolumn{2}{| c |}{\textbf{Car Insurance}}\\ |
|
1678 |
|
\hline |
|
1679 |
|
policy number &: String\\ |
|
1680 |
|
covered amount &: int\\ |
|
1681 |
|
compagny name &: String\\ |
|
1682 |
|
\hline |
|
1683 |
|
\end{tabular} |
|
1684 |
|
}; |
|
1685 |
|
|
|
1686 |
|
\draw[diamond -] (car) -- node[above, pos=0.1]{\(0..\star\)} node[above, pos=0.9]{\(1\)} (insu); |
|
1687 |
|
\end{tikzpicture} |
|
1688 |
|
|
|
1689 |
|
\end{answer} |
|
1690 |
|
|
|
1691 |
|
\begin{problem}[tags={hw}] |
|
1692 |
|
\label{probERtoREL} |
|
1693 |
|
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. |
|
1694 |
|
\begin{enumerate} |
|
1695 |
|
\item Install MySQL Workbench: use your package manager, or download the binaries from \url{https://dev.mysql.com/downloads/workbench/}. |
|
1696 |
|
\item Once installed, execute the software. Under the panel \enquote{MySQL Connections}, you should see your local installation listed as \enquote{Local instance 3306}. Click on the top-right corner of that box, and then on \enquote{Edit Connections}. Alternatively, click on \enquote{Database}, on \enquote{Manage Connections}, and then on \enquote{Local instance 3306}. |
|
1697 |
|
\item Check that all the parameters are correct. Normally, you only have to change the name of the user to \enquote{testuser}, and leave the rest as it is. Click on \enquote{Test the connection}, and enter your password (which should be \enquote{password}) when prompted. If you receive a warning about \enquote{Incompatible/nonstandard server version or connection protocol detected}, click on \enquote{Continue anyway}. |
|
1698 |
|
\item Now, click on the box \enquote{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. |
|
1699 |
|
\item Click on \enquote{Database}, and then on \enquote{Reverse Engineering} (or hit \texttt{ctrl} + \texttt{r}), click on \enquote{next}, enter your password, and click on \enquote{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 \enquote{next}, and then click on \enquote{execute}, \enquote{next}, and \enquote{close}. |
|
1700 |
|
\item You're back on the previous view, but you should now see \enquote{E.E.R. diagram} on the top of the middle panel. Click on \enquote{E.E.R. diagram} twice, scroll down if needed, and you should see the E.E.R. diagram. |
|
1701 |
|
\item 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. |
|
1702 |
|
\item Once you're done, try to \enquote{Forward Engineer} by hitting \enquote{Ctrl} + \enquote{G}. Click on \enquote{next} twice, enter your password, click on lick on \enquote{next} once more, and you should see the \texttt{SQL} code needed to produce the table you just designed using the graphical tool. |
|
1703 |
|
\end{enumerate} |
|
1704 |
|
\end{problem} |
|
1705 |
|
|
|
1706 |
|
\begin{problem}[tags={hw}] |
|
1707 |
|
\emph{This problem requires you to have successfully completed Problem~\ref{probMYSQLW} and Problem~\ref{probERtoREL}.} |
|
1708 |
|
|
|
1709 |
|
Using the relational database schema you obtained in question~\ref{ERtoREL} of Problem~\ref{probERtoREL}, write the \texttt{SQL} implementation of that database. |
|
1710 |
|
Then, using MySQL Workbench, use the \enquote{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}. |
|
1711 |
|
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? |
|
1712 |
|
\end{problem} |
|
1713 |
|
|
|
1714 |
|
\begin{answer} |
|
1715 |
|
\inputminted{sql}{include/sol.sql} |
|
1716 |
|
\includegraphics{include/mysql_workbench_drawing-crop} |
|
1717 |
|
\end{answer} |
|
1718 |
|
|
|
1719 |
|
%%% 07 |
|
1720 |
|
|
|
1721 |
|
|
|
1722 |
|
\begin{exercise}[tags={hw}] |
|
1723 |
|
What are the technologies that makes it possible for a Java application to communicate with a DBMS? |
|
1724 |
|
\end{exercise} |
|
1725 |
|
|
|
1726 |
|
\begin{solution} |
|
1727 |
|
API + driver |
|
1728 |
|
\end{solution} |
|
1729 |
|
|
|
1730 |
|
\begin{exercise}[tags={hw}] |
|
1731 |
|
What JDBC method do you call to get a connection to a database? |
|
1732 |
|
\end{exercise} |
|
1733 |
|
\begin{solution} |
|
1734 |
|
DriverManager.getConnection() |
|
1735 |
|
\end{solution} |
|
1736 |
|
|
|
1737 |
|
\begin{exercise}[tags={hw}] |
|
1738 |
|
Briefly explain what the \texttt{next()} method from the \texttt{ResultSet} class does, and what is its return type. |
|
1739 |
|
\end{exercise} |
|
1740 |
|
\begin{solution} |
|
1741 |
|
It checks if there is data to read, and if move the cursor reads it. |
|
1742 |
|
It returns a Boolean. |
|
1743 |
|
\end{solution} |
|
1744 |
|
|
|
1745 |
|
\begin{exercise}[tags={hw}] |
|
1746 |
|
How do you submit a \mintinline{SQL}{SELECT} statement to the DBMS? |
|
1747 |
|
\end{exercise} |
|
1748 |
|
|
|
1749 |
|
\begin{solution} |
|
1750 |
|
Using \mintinline{java}{.executeQuery(strSelect)} |
|
1751 |
|
\end{solution} |
|
1752 |
|
|
|
1753 |
|
\begin{exercise}[tags={hw}] |
|
1754 |
|
Where is a ResultSet object's cursor initially pointing? How do you move the cursor forward in the result set? |
|
1755 |
|
\end{exercise} |
|
1756 |
|
|
|
1757 |
|
\begin{solution} |
|
1758 |
|
Before the first line. |
|
1759 |
|
Unsing the \mintinline{java}{next()} method. |
|
1760 |
|
\end{solution} |
|
1761 |
|
|
|
1762 |
|
\begin{exercise}[tags={hw}] |
|
1763 |
|
Give three navigation methods provided by \mintinline{Java}{ResultSet}. |
|
1764 |
|
\end{exercise} |
|
1765 |
|
|
|
1766 |
|
\begin{solution} |
|
1767 |
|
\mintinline{java}{first()} |
|
1768 |
|
\mintinline{java}{last()} |
|
1769 |
|
\mintinline{java}{next()} |
|
1770 |
|
\mintinline{java}{previous()} |
|
1771 |
|
\mintinline{java}{relative(rows)} |
|
1772 |
|
\mintinline{java}{absolute(row} method. |
|
1773 |
|
\end{solution} |
|
1774 |
|
|
|
1775 |
|
\begin{exercise}[tags={hw}] |
|
1776 |
|
Explain this JDBC URL format: |
|
1777 |
|
|
|
1778 |
|
\mintinline[breaklines=true]{Java}{jdbc:mysql://localhost:3306/HW_NewDB?createDatabaseIfNotExist=true&useSSL=true} |
|
1779 |
|
\end{exercise} |
|
1780 |
|
|
|
1781 |
|
\begin{solution} |
|
1782 |
|
Connect to localhost:3306 and create a new database if needed, and use secure connection. |
|
1783 |
|
\end{solution} |
|
1784 |
|
|
|
1785 |
|
\begin{exercise}[tags={hw}] |
|
1786 |
|
In what class is the \mintinline{java}{getColumnName()} method? |
|
1787 |
|
\end{exercise} |
|
1788 |
|
|
|
1789 |
|
\begin{solution} |
|
1790 |
|
ResultSetMetaData |
|
1791 |
|
\end{solution} |
|
1792 |
|
|
|
1793 |
|
\begin{exercise}[tags={hw}] |
|
1794 |
|
What is a prepared statement? |
|
1795 |
|
\end{exercise} |
|
1796 |
|
|
|
1797 |
|
\begin{solution} |
|
1798 |
|
A prepared statement is a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency. |
|
1799 |
|
\end{solution} |
|
1800 |
|
|
|
1801 |
|
\begin{exercise}[tags={qz}] |
|
1802 |
|
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. |
|
1803 |
|
|
|
1804 |
|
\begin{minted}[linenos, breaklines=true, baselinestretch=1.4]{Java} |
|
1805 |
|
import java.sql.*; |
|
1806 |
|
|
|
1807 |
|
public class MyProg{ |
|
1808 |
|
public static void main(String[] args) { |
|
1809 |
|
try ( |
|
1810 |
|
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" |
|
1811 |
|
+"HW_TestDB?user=testuser&password=password"); |
|
1812 |
|
Statement stmt = conn.createStatement(); |
|
1813 |
|
) { |
|
1814 |
|
|
|
1815 |
|
/* Errors after this point.*/ |
|
1816 |
|
|
|
1817 |
|
String strSelect = "SELECT title WHERE qty > 40 FROM disks;"; |
|
1818 |
|
ResultSet rset = stmt.executeUpdate(strSelect); |
|
1819 |
|
|
|
1820 |
|
System.out.println("The records selected are: (listed last first):"); |
|
1821 |
|
rset.last(); |
|
1822 |
|
|
|
1823 |
|
while(rset.previous()) { |
|
1824 |
|
String title = rset.getDouble("title"); |
|
1825 |
|
System.out.println(title + "\n"); |
|
1826 |
|
} |
|
1827 |
|
|
|
1828 |
|
String sss = "SELECT title FROM disks WHERE Price <= ?"; |
|
1829 |
|
PreparedStatement ps = conn.prepareStatement(sss); |
|
1830 |
|
ResultSet result = ps.executeQuery(); |
|
1831 |
|
|
|
1832 |
|
conn.close(); |
|
1833 |
|
|
|
1834 |
|
/* Errors before this point.*/ |
|
1835 |
|
|
|
1836 |
|
} catch(SQLException ex) { |
|
1837 |
|
ex.printStackTrace(); |
|
1838 |
|
} |
|
1839 |
|
} |
|
1840 |
|
} |
|
1841 |
|
\end{minted} |
|
1842 |
|
\end{exercise} |
|
1843 |
|
|
|
1844 |
|
% |
|
1845 |
|
%METADATA |
|
1846 |
|
% |
|
1847 |
|
%ResultSet is array, other? |
|
1848 |
|
% |
|
1849 |
|
%Multiple queries |
|
1850 |
|
% |
|
1851 |
|
%qet datatype |
|
1852 |
|
% |
|
1853 |
|
%prepared statemernt. |
|
1854 |
|
% |
|
1855 |
|
%http://tutorials.jenkov.com/jdbc/resultset.html |
|
1856 |
|
|
|
1857 |
|
\IfTagSetF{hw}{ |
|
1858 |
|
\vspace{-8em} % ugly hack… |
|
1859 |
|
\end{document} |
|
1860 |
|
} |
|
1861 |
|
|
|
1862 |
|
\section{Part II --- Problems} |
|
1863 |
|
|
|
1864 |
|
|
|
1865 |
|
This part will mainly hands on, and ask you to read code. |
|
1866 |
|
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. |
|
1867 |
|
I'll assume that you will have successfully completed those tasks by \hwDateNext, so don't wait and let me know if you had difficulties solving them. |
|
1868 |
|
|
|
1869 |
|
\subsection*{References}\label{ref} |
|
1870 |
|
\begin{itemize} |
|
1871 |
|
\item This second part takes some inspiration from \url{https://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html}. If you experience troubles, \url{https://www.ntu.edu.sg/home/ehchua/programming/howto/ErrorMessages.html#JDBCErrors} might be a good read. |
|
1872 |
|
\item Section 13.3.2 of your textbook is a condensed, but good read. Many textbook on Java includes a part on Databases, cf. for instance the Chapter 16 of \emph{Starting Out with Java: Early Objects} (5th Edition) by Tony Gaddis. |
|
1873 |
|
\end{itemize} |
|
1874 |
|
|
|
1875 |
|
|
|
1876 |
|
\begin{problem}[tags={hw}] |
|
1877 |
|
\label{qu:batch} |
|
1878 |
|
In the archive, navigate to \texttt{code/sql/}, open and read \texttt{HW\_ebookshop.sql}. |
|
1879 |
|
|
|
1880 |
|
Then, open a terminal (or command-line interpreter), navigate to the folder where you stored that file (using \mintinline{bash}{cd}), and type |
|
1881 |
|
|
|
1882 |
|
\begin{minted}{bash} |
|
1883 |
|
mysql -u testuser -p < HW_ebookshop.sql |
|
1884 |
|
\end{minted} |
|
1885 |
|
|
|
1886 |
|
for linux, or (something like) |
|
1887 |
|
|
|
1888 |
|
\begin{minted}[breaklines=true]{bash} |
|
1889 |
|
"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql.exe" -u testuser -p < HW_ebookshop.sql |
|
1890 |
|
\end{minted} |
|
1891 |
|
for Windows. |
|
1892 |
|
|
|
1893 |
|
You just discovered MySQL's batch mode, that perform \emph{series} of instructions from a file. |
|
1894 |
|
You can easily make sure that the database and the table were indeed created, and the values inserted. |
|
1895 |
|
\end{problem} |
|
1896 |
|
|
|
1897 |
|
\begin{problem}[tags={hw}] |
|
1898 |
|
This exercise supposes you successfully completed Problem~\ref{qu:batch}. |
|
1899 |
|
We will compile and execute your first database application, using Java and MySQL. |
|
1900 |
|
\begin{itemize} |
|
1901 |
|
\item I will assume that you have MySQL installed and set-up as indicated in Homeworks \#1 and \#2. |
|
1902 |
|
\item I will assume that you have Java installed. If not, please refer to \url{http://spots.augusta.edu/caubert/teaching/general/java/} for a simple program and the instructions to compile and execute it. |
|
1903 |
|
\item We need to set up the \emph{driver} (or \emph{connector}) to make the java \texttt{sql} API and MySQL communicate. To do so, |
|
1904 |
|
\begin{itemize} |
|
1905 |
|
\item Go to \url{https://dev.mysql.com/downloads/connector/j/} |
|
1906 |
|
\item Click on \enquote{Download} in front of \enquote{Platform Independent (Architecture Independent), ZIP Archive} |
|
1907 |
|
\item Look for the (somewhat hidden) \enquote{No thanks, just start my download.} |
|
1908 |
|
\item You will download a file named \enquote{mysql-connector-java-***.zip}, where \texttt{***} is the version number. |
|
1909 |
|
\item Upon completion of the download, unzip the file, and locate the \enquote{mysql-connector-java-***-bin.jar} file. |
|
1910 |
|
\item Copy that file in \texttt{code/java/}. |
|
1911 |
|
\end{itemize} |
|
1912 |
|
\item Open a terminal in that same folder, and compile \texttt{FirstProg.java}, using |
|
1913 |
|
\begin{minted}{bash} |
|
1914 |
|
javac FirstProg.java |
|
1915 |
|
\end{minted} |
|
1916 |
|
(or an equivalent command for windows). |
|
1917 |
|
Normally, nothing will be printed, but a \texttt{FirstProg.class} file will be created. |
|
1918 |
|
\item Now, execute that program, using |
|
1919 |
|
\begin{minted}{bash} |
|
1920 |
|
java -cp .:mysql-connector-java-***-bin.jar FirstProg |
|
1921 |
|
\end{minted} |
|
1922 |
|
in Linux, or |
|
1923 |
|
\begin{minted}{bash} |
|
1924 |
|
java -cp .;mysql-connector-java-***-bin.jar FirstProg |
|
1925 |
|
\end{minted} |
|
1926 |
|
in Windows. |
|
1927 |
|
The \texttt{-cp} option lists the places where java should look for the class used in the program: we are explicitely asking java to use the \texttt{mysql-connector-java-***-bin.jar} executable to execute our \texttt{FirstProg} executable. |
|
1928 |
|
Try to execute \texttt{FirstProg} without that flag, and see what happens. |
|
1929 |
|
\end{itemize} |
|
1930 |
|
\end{problem} |
|
1931 |
|
|
|
1932 |
|
\begin{answer} |
|
1933 |
|
\begin{minted}[breaklines=true]{bash} |
|
1934 |
|
$ java FirstProg |
|
1935 |
|
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/HW_ebookshop |
|
1936 |
|
at java.sql.DriverManager.getConnection(DriverManager.java:689) |
|
1937 |
|
at java.sql.DriverManager.getConnection(DriverManager.java:247) |
|
1938 |
|
at FirstProg.main(FirstProg.java:9) |
|
1939 |
|
\end{minted} |
|
1940 |
|
\end{answer} |
|
1941 |
|
|
|
1942 |
|
\begin{problem}[tags={hw}] |
|
1943 |
|
There are three other programs in that archive: |
|
1944 |
|
\begin{description} |
|
1945 |
|
\item[Variation.java] is the program we looked at during class. |
|
1946 |
|
\item[NullProg.java] is an attempt to answer the question whenever SQL's \sqli{NULL} is JAVA's \texttt{NULL} when it comes to strings. |
|
1947 |
|
\item[AdvancedProg.java] is a long and commented program that introduces multiple new tools and ideas. |
|
1948 |
|
\end{description} |
|
1949 |
|
|
|
1950 |
|
Read, execute, break, edit, compile, patch, hack and (most importantly) understand those three programs, with, of course, a focus on the last one. |
|
1951 |
|
|
|
1952 |
|
\end{problem} |
|
1953 |
|
|
|
1954 |
|
%%% 08 -- Exam 1 |
|
1955 |
|
|
|
1956 |
|
|
|
1957 |
|
\begin{problem}[tags={ex}, points = {20}] |
|
1958 |
|
%30 |
|
1959 |
|
A cinema company wants you to design a relational model for the following set-up: |
|
1960 |
|
\begin{itemize} |
|
1961 |
|
\item The company has movie stars. Each star has a name, gender, birthday, and unique id. |
|
1962 |
|
\item The company has information about movies: title, year, length, genre, and studio. Each movie has a unique id, and features multiple stars. |
|
1963 |
|
\item The company owns movie theaters as well. Each theater has a name, address, and a unique id. |
|
1964 |
|
\item Furthermore, each theater has a set of auditoriums. Each auditorium has a unique number, and seating capacity. |
|
1965 |
|
\item Each theater can schedule movies at show-times. |
|
1966 |
|
Each show-time has a unique id, a start time, and is for a specific movie, at a theater auditorium. |
|
1967 |
|
\item The company sells tickets for scheduled show-times. Each ticket has a unique ticket id, and a price. |
|
1968 |
|
\end{itemize} |
|
1969 |
|
%Your relations should be written as follow: relation names in uppercase, coma separated attributes enclosed in parenthesis, primary key underlined, and foreign key(s) detailed on the side (or drawn). That is, for instance, |
|
1970 |
|
%\enquote{PROF(\underline{Login}, Name, Department) ; where Department references the Code attribute in the DEPARTMENT table.} |
|
1971 |
|
Complete the following relational model. |
|
1972 |
|
If you think there are missing relations, add them, and when the parenthesis isn't closed, close it or add attribute(s). |
|
1973 |
|
Also, specify the primary as well as foreign keys. |
|
1974 |
|
|
|
1975 |
|
{ |
|
1976 |
|
\renewcommand{\arraystretch}{3}% |
|
1977 |
|
\large |
|
1978 |
|
\hspace{3em}\begin{tabular}{l} |
|
1979 |
|
STAR(Name, Gender, Birthday, Id\\ |
|
1980 |
|
MOVIE(Title\\ |
|
1981 |
|
FEATURE\_IN(StarId, MovieId)\\ |
|
1982 |
|
THEATER(Name\\ |
|
1983 |
|
AUDITORIUM(Id, Capacity, Theater\\ |
|
1984 |
|
SHOWTIME( |
|
1985 |
|
\end{tabular} |
|
1986 |
|
} |
|
1987 |
|
\end{problem} |
|
1988 |
|
\clearpage |
|
1989 |
|
|
|
1990 |
|
\begin{problem}[tags={ex}, points ={10}] |
|
1991 |
|
Your professor designed the following relational model at some point in his career, to help him organizing his exams and the students grades: |
|
1992 |
|
|
|
1993 |
|
\begin{center} |
|
1994 |
|
\begin{tabular}{l | l} |
|
1995 |
|
Table Name and Attributes & Example of Value\\ |
|
1996 |
|
\hline |
|
1997 |
|
EXAM(\underline{Number}, Date, Course) & \(\langle 1, \text{'2018-02-14'}, \text{'CSCI3410'}\rangle\)\\ |
|
1998 |
|
PROBLEM(\underline{Statement}, Points, Length, Exam) & \(\langle \text{'Your professor designed\dots'}, 10, \text{'00:10:00'}, 1\rangle\) \\ |
|
1999 |
|
STUDENT\_GRADE(\underline{Login}, \underline{Exam}, Grade) & \(\langle \text{'aalyx'}, 1, 83\rangle\) |
|
2000 |
|
\end{tabular} |
|
2001 |
|
\end{center} |
|
2002 |
|
|
|
2003 |
|
The idea was to have |
|
2004 |
|
\begin{itemize} |
|
2005 |
|
\item The EXAM table storing information about exams, |
|
2006 |
|
\item One entry per problem in the PROBLEM table, and to associate every problem to an exam (i.e., to have the Exam attribute referencing the Number attribute in the EXAM table), |
|
2007 |
|
\item The grade of one student for one particular exam stored in the STUDENT\_GRADE table, where Exam is referencing the Number attribute in the EXAM table. |
|
2008 |
|
\end{itemize} |
|
2009 |
|
Unfortunately, this design turned out to be terrible. |
|
2010 |
|
Describe at least one common and interesting situation where this model would fail to fulfill its purpose, and propose a way to correct the particular problem you identified. |
|
2011 |
|
\end{problem} |
|
2012 |
|
|
|
2013 |
|
\clearpage |
|
2014 |
|
|
|
2015 |
|
|
|
2016 |
|
\begin{problem}[tags={ex}, points = {30}] |
|
2017 |
|
Look at the SQL code below, and then answer the following questions. |
|
2018 |
|
|
|
2019 |
|
\begin{minted}[linenos]{mysql} |
|
2020 |
|
CREATE TABLE TRAIN( |
|
2021 |
|
Id VARCHAR(30), |
|
2022 |
|
Model VARCHAR(30), |
|
2023 |
|
ConstructionYear YEAR(4) |
|
2024 |
|
); |
|
2025 |
|
|
|
2026 |
|
CREATE TABLE CONDUCTOR( |
|
2027 |
|
Id VARCHAR(20), |
|
2028 |
|
Name VARCHAR(20), |
|
2029 |
|
ExperienceLevel VARCHAR(20) |
|
2030 |
|
); |
|
2031 |
|
|
|
2032 |
|
CREATE TABLE ASSIGNED_TO( |
|
2033 |
|
TrainId VARCHAR(20), |
|
2034 |
|
ConductorId VARCHAR(20), |
|
2035 |
|
Day DATE, |
|
2036 |
|
PRIMARY KEY(TrainId, ConductorId) |
|
2037 |
|
); |
|
2038 |
|
\end{minted} |
|
2039 |
|
|
|
2040 |
|
\begin{enumerate}[topsep=0pt,itemsep=10em,partopsep=1ex,parsep=1ex] |
|
2041 |
|
\item Modify the \sqli{CREATE} statement that creates the \sqli{TRAIN} table (l. 1--5), so that \sqli{Id} would be declared as the primary key. |
|
2042 |
|
You can write only the line(s) that need to change. |
|
2043 |
|
\item Write an \sqli{ALTER} statement that makes \sqli{Id} become the primary key of the \sqli{CONDUCTOR} table. |
|
2044 |
|
\item Modify the \sqli{CREATE} statement that creates the \sqli{ASSIGNED_TO} table (l. 13--18), so that it has two foreign keys: \sqli{ConductorId} would be referencing the \sqli{Id} attribute in \sqli{CONDUCTOR} and \sqli{TrainId} would be referencing the \sqli{Id} attribute in \sqli{TRAIN}. |
|
2045 |
|
You can write only the line(s) that need to change. |
|
2046 |
|
\clearpage |
|
2047 |
|
\item Write \sqli{INSERT} statements that insert one tuple of your invention in each relation. |
|
2048 |
|
Your statements should respect all the constraints (including the ones we added at the previous questions) and result in actual insertions. (Remember that four digits is a valid value for an attribute with the \sqli{YEAR(4)} datatype.) |
|
2049 |
|
\vspace{2em} |
|
2050 |
|
|
|
2051 |
|
\item Write a statement that sets the value of the \sqli{ExperienceLevel} attribute to \enquote{Senior} in all the tuples where the \sqli{Id} attribute is \enquote{GP1029} in the \sqli{CONDUCTOR} relation. |
|
2052 |
|
\vspace{-2em} |
|
2053 |
|
\item For each of the following questions, write a \sqli{SELECT} statement that would answer it: |
|
2054 |
|
\begin{enumerate}[topsep=0pt,itemsep=6em,partopsep=1ex,parsep=1ex] |
|
2055 |
|
\item \enquote{What are the identification numbers of the trains?} |
|
2056 |
|
\item \enquote{What are the names of the conductors with a \enquote{Senior} experience level?} |
|
2057 |
|
\item \enquote{What are the construction years of the \enquote{Surfliner} and \enquote{Regina} models that we have?} |
|
2058 |
|
\item \enquote{What is the id of the conductor that was responsible of the train referenced \enquote{K-13} on 2015/12/14?} |
|
2059 |
|
\item \enquote{What are the models that were ever conducted by the conductor whose id is \enquote{GP1029}?} |
|
2060 |
|
% \item \enquote{What is the name of the conductor that was responsible of the \enquote{Surfliner} model on October, 25th, 2015?} |
|
2061 |
|
\end{enumerate} |
|
2062 |
|
\end{enumerate} |
|
2063 |
|
\end{problem} |
|
2064 |
|
|
|
2065 |
|
\clearpage |
|
2066 |
|
|
|
2067 |
|
\begin{problem}[tags={ex}, bonus-points = {4}, points={40}] |
|
2068 |
|
Suppose we have the relational model depicted in \autoref{fig:coffee}, p.~\pageref{fig:coffee}, with the indicated data in it. |
|
2069 |
|
|
|
2070 |
|
In the following, we will assume that this model was implemented in a DBMS (MySQL or MariaDB), the primary keys being the underlined attributes, and the foreign keys as follow: |
|
2071 |
|
|
|
2072 |
|
\begin{tabular}{r c l} |
|
2073 |
|
\textbf{FavCoffee} in the \textbf{CUSTOMER} relation & refers to & \textbf{Ref} in the \textbf{COFFEE} relation\\ |
|
2074 |
|
\textbf{Provider} in the \textbf{SUPPLY} & refers to & \textbf{Name} in the \textbf{PROVIDER} relation\\ |
|
2075 |
|
\textbf{Coffee} in the \textbf{SUPPLY} & refers to & \textbf{Ref} in the \textbf{COFFEE} relation |
|
2076 |
|
\end{tabular} |
|
2077 |
|
|
|
2078 |
|
You will be asked to read and write SQL commands. |
|
2079 |
|
You should assume that |
|
2080 |
|
\begin{enumerate} |
|
2081 |
|
\item Datatype doesn't matter: we use only strings and appropriate numerical datatypes. |
|
2082 |
|
\item Every statement respects SQL's syntax (there's no \enquote{a semi-colon is missing} trap). |
|
2083 |
|
\item None of those commands are actually executed: the data is always in the state of \autoref{fig:coffee}, you are asked to answer \enquote{what if} questions. |
|
2084 |
|
\end{enumerate} |
|
2085 |
|
|
|
2086 |
|
In questions (\ref{ques:upd}) and (\ref{ques:del}), please, use \textbf{COFFEE}.\(1\) to denote the first tuple in \textbf{COFFEE}, and similarly for other relations and tuples (so that, for instance \textbf{SUPPLY}.\(4\) corresponds to \enquote{Johns \& Co., \(221\)}). |
|
2087 |
|
|
|
2088 |
|
\begin{enumerate} |
|
2089 |
|
\item Determine if the following insertion statements would violate the |
|
2090 |
|
the \textbf{E}ntity integrity constraint, % (\enquote{primary key cannot be \texttt{NULL} and should be unique}) |
|
2091 |
|
the \textbf{R}eferential integrity constraint% (\enquote{the foreign key must refer to something that exists}), |
|
2092 |
|
, if there would be some \textbf{O}ther kind of error% (ignoring the plausability / revelance of inserting that tuple) |
|
2093 |
|
, or if it would result in \textbf{S}uccessful insertion. |
|
2094 |
|
|
|
2095 |
|
{ \renewcommand{\arraystretch}{2} |
|
2096 |
|
\begin{tabular}{l | c | c | c | c |} |
|
2097 |
|
& \textbf{E} & \textbf{R} & \textbf{O} & \textbf{S}\\ |
|
2098 |
|
\hline |
|
2099 |
|
\sqli{INSERT INTO CUSTOMER VALUES(005, 'Bob Hill', NULL, 001);} & & & & \\ \hline |
|
2100 |
|
\sqli{INSERT INTO COFFEE VALUES(002, "Peru", "Decaf", 3.00);} & & & & \\ \hline |
|
2101 |
|
\sqli{INSERT INTO PROVIDER VALUES(NULL, "contact@localcof.com");} & & & & \\ \hline |
|
2102 |
|
\sqli{INSERT INTO SUPPLY VALUES("Johns & Co.", 121);} & & & & \\ \hline |
|
2103 |
|
\sqli{INSERT INTO SUPPLY VALUES("Coffee Unl.", 311, 221);} & & & & \\ \hline |
|
2104 |
|
\end{tabular} |
|
2105 |
|
} |
|
2106 |
|
|
|
2107 |
|
\item Assuming that we cascade update statements, list the tuples modified by the following statements: \label{ques:upd} |
|
2108 |
|
\begin{enumerate}[topsep=0pt,itemsep=4em,partopsep=1ex,parsep=1ex] |
|
2109 |
|
\item \sqli{UPDATE CUSTOMER SET FavCoffee = 001 WHERE CardNo = 001;} |
|
2110 |
|
\item \sqli{UPDATE COFFEE SET TypeOfRoast = 'Decaf' WHERE Origin = 'Brazil';} |
|
2111 |
|
\item \sqli{UPDATE PROVIDER SET Name = 'Coffee Unlimited' WHERE Name = 'Coffee Unl.';} |
|
2112 |
|
\item \sqli{UPDATE COFFEE SET PricePerPound = 10.00 WHERE PricePerPound > 10.00;} |
|
2113 |
|
\end{enumerate} |
|
2114 |
|
\clearpage |
|
2115 |
|
\item Assuming that we cascade delete statements, list the tuples deleted by the following statements: \label{ques:del} |
|
2116 |
|
\begin{enumerate}[topsep=0pt,itemsep=4em,partopsep=1ex,parsep=1ex] |
|
2117 |
|
\item \mintinline{MYSQL}{DELETE FROM CUSTOMER WHERE Name LIKE '%S%';} |
|
2118 |
|
\item \sqli{DELETE FROM COFFEE WHERE Ref = 001;} |
|
2119 |
|
\item \sqli{DELETE FROM SUPPLY WHERE Provider = 'Coffee Unl.' AND Coffee = 001;} |
|
2120 |
|
\item \sqli{DELETE FROM PROVIDER WHERE Name = 'Johns & Co.';} |
|
2121 |
|
\vspace{4em} |
|
2122 |
|
\end{enumerate} |
|
2123 |
|
|
|
2124 |
|
\item %Starting here, assume that there is more data in our table than what was given at the beginning of the problem. |
|
2125 |
|
Write queries that answer the following questions (the last two are bonus): |
|
2126 |
|
\begin{enumerate}[topsep=0pt,itemsep=6em,partopsep=1ex,parsep=1ex] |
|
2127 |
|
\item \enquote{What are the origins of your dark coffees?} |
|
2128 |
|
\item \enquote{What is the reference of Bob's favorite coffee?} (nota: it doesn't matter if you return the favorite coffee of all the Bobs in the database.) |
|
2129 |
|
\item \enquote{What are the names of the providers who didn't give their email?} |
|
2130 |
|
\item \enquote{How many coffees does Johns \& co. provide us with?} |
|
2131 |
|
\item \enquote{What are the names of the providers of my dark coffees?} |
|
2132 |
|
\end{enumerate} |
|
2133 |
|
|
|
2134 |
|
\end{enumerate} |
|
2135 |
|
|
|
2136 |
|
\end{problem} |
|
2137 |
|
|
|
2138 |
|
|
|
2139 |
|
|
|
2140 |
|
\begin{figure} |
|
2141 |
|
|
|
2142 |
|
\begin{multicols}{2} |
|
2143 |
|
\textbf{COFFEE} |
|
2144 |
|
|
|
2145 |
|
\begin{tabular}{| c | c | c | c | } |
|
2146 |
|
\hline |
|
2147 |
|
\rowcolor{gray!50} \underline{\textbf{Ref}} & \textbf{Origin} & \textbf{TypeOfRoast} & \textbf{PricePerPound}\\ \hline |
|
2148 |
|
\(001\) & Brazil & Light & 8.90\\ |
|
2149 |
|
\(121\) & Bolivia & Dark& 7.50\\ |
|
2150 |
|
\(311\) & Brazil & Medium & 9.00\\ |
|
2151 |
|
\(221\) & Sumatra & Dark & 10.25\\ \hline |
|
2152 |
|
\end{tabular} |
|
2153 |
|
\\[2em] |
|
2154 |
|
\textbf{CUSTOMER} |
|
2155 |
|
|
|
2156 |
|
\begin{tabular}{| c | c | c | c | } |
|
2157 |
|
\hline |
|
2158 |
|
\rowcolor{gray!50} \underline{\textbf{CardNo}} & \textbf{Name} & \textbf{Email} & \textbf{FavCoffee}\\ \hline |
|
2159 |
|
\(001\) & Bob Hill & b.hill@isp.net & 221\\ |
|
2160 |
|
\(002\) & Ana Swamp & swampa@nca.edu & 311\\ |
|
2161 |
|
\(003\) & Mary Sea & brig@gsu.gov & 121\\ |
|
2162 |
|
\(004\) & Pat Mount & pmount@fai.fr & 121\\ \hline |
|
2163 |
|
\end{tabular} |
|
2164 |
|
%\\[2em] |
|
2165 |
|
%\noindent |
|
2166 |
|
|
|
2167 |
|
\hspace{5em} |
|
2168 |
|
\textbf{SUPPLY} |
|
2169 |
|
|
|
2170 |
|
\hspace{5em} |
|
2171 |
|
\begin{tabular}{| c | c | } |
|
2172 |
|
\hline |
|
2173 |
|
\rowcolor{gray!50} \underline{\textbf{Provider}} & \underline{\textbf{Coffee}}\\ \hline |
|
2174 |
|
Coffee Unl. & \(001\) \\ |
|
2175 |
|
Coffee Unl. & \(121\) \\ |
|
2176 |
|
Coffee Exp. & \(311\) \\ |
|
2177 |
|
Johns \& Co. & \(221\) \\ \hline |
|
2178 |
|
\end{tabular} |
|
2179 |
|
\\[2em] |
|
2180 |
|
|
|
2181 |
|
\hspace{5em} |
|
2182 |
|
\textbf{PROVIDER} |
|
2183 |
|
|
|
2184 |
|
\hspace{5em} |
|
2185 |
|
\begin{tabular}{| c | c | } |
|
2186 |
|
\hline |
|
2187 |
|
\rowcolor{gray!50} \underline{\textbf{Name}} & \textbf{Email}\\ \hline |
|
2188 |
|
Coffee Unl. & bob@cofunl.com \\ |
|
2189 |
|
Coffee Exp. & pat@coffeex.dk \\ |
|
2190 |
|
Johns \& Co. & NULL \\ \hline |
|
2191 |
|
\end{tabular} |
|
2192 |
|
\end{multicols} |
|
2193 |
|
\caption{Relational Model and Data for the DB\_COFFEE Database} |
|
2194 |
|
\label{fig:coffee} |
|
2195 |
|
\end{figure} |
|
2196 |
|
|
|
2197 |
|
%% 09 -- Exam 2 |
|
2198 |
|
|
|
2199 |
|
|
|
2200 |
|
\begin{problem}[tags={ex}, points = {15}] |
|
2201 |
|
Consider the relation |
|
2202 |
|
|
|
2203 |
|
\begin{center} |
|
2204 |
|
CONTACT(Phone, Call\_center, Email, Zip, Brand, Website) |
|
2205 |
|
\end{center} |
|
2206 |
|
|
|
2207 |
|
and the following functional dependencies: |
|
2208 |
|
|
|
2209 |
|
\begin{center} |
|
2210 |
|
\begin{tabular}{r c l} |
|
2211 |
|
\{Zip, Brand\} & \(\to\) & \{Phone\}\\ |
|
2212 |
|
\{Brand\} & \(\to\) & \{Email\}\\ |
|
2213 |
|
\{ Brand\} & \(\to\) & \{Website\}\\ |
|
2214 |
|
\{Phone\} & \(\to\) & \{Call\_center\} |
|
2215 |
|
\end{tabular} |
|
2216 |
|
\end{center} |
|
2217 |
|
|
|
2218 |
|
Assume that \{Zip, Brand\} is the primary key. |
|
2219 |
|
Normalize this relation to the second normal form, and \emph{then} to the third normal form. |
|
2220 |
|
Give the relations, their primary keys, and functional dependencies for both steps. |
|
2221 |
|
\end{problem} |
|
2222 |
|
|
|
2223 |
|
\clearpage |
|
2224 |
|
|
|
2225 |
|
\begin{problem}[tags={ex}, points = {15}] |
|
2226 |
|
\label{nf} |
|
2227 |
|
|
|
2228 |
|
Consider the relation |
|
2229 |
|
|
|
2230 |
|
\begin{center} |
|
2231 |
|
CONSULTATION(Doctor\_no, Patient\_no, Date, Diagnosis, Treatment, Charge, Insurance) |
|
2232 |
|
\end{center} |
|
2233 |
|
|
|
2234 |
|
with the following functional dependencies: |
|
2235 |
|
|
|
2236 |
|
\begin{center} |
|
2237 |
|
\begin{tabular}{r c l} |
|
2238 |
|
\{Doctor\_no, Patient\_no, Date\} & \(\to\) & \{Diagnosis\}\\ |
|
2239 |
|
\{Doctor\_no, Patient\_no, Date\} & \(\to\) & \{Treatment\}\\ |
|
2240 |
|
\{Treatment, Insurance\} & \(\to\) & \{Charge\}\\ |
|
2241 |
|
\{Patient\_no\} & \(\to\) & \{Insurance\} |
|
2242 |
|
\end{tabular} |
|
2243 |
|
\end{center} |
|
2244 |
|
|
|
2245 |
|
\begin{enumerate}[topsep=0pt,itemsep=10em,partopsep=1ex,parsep=1ex] |
|
2246 |
|
\item The designer decided not to add the functional dependency \{Diagnosis\} $\to$ \{Treatment\}. |
|
2247 |
|
Explain what could be the designer's justification, at the level of the mini-world. |
|
2248 |
|
\item Identify a primary key for this relation. |
|
2249 |
|
\item What is the degree of normalization of this relation? |
|
2250 |
|
Normalize it to the third normal form if necessary. |
|
2251 |
|
\end{enumerate} |
|
2252 |
|
\end{problem} |
|
2253 |
|
\clearpage |
|
2254 |
|
|
|
2255 |
|
\begin{solution} |
|
2256 |
|
Answer: |
|
2257 |
|
Because there are no partial dependencies, the given relation is in 2NF already. This however is not 3NF because the Charge is a nonkey attribute that is determined by another nonkey attribute, Treatment. We must decompose further: |
|
2258 |
|
|
|
2259 |
|
R (Doctor\_no, Patient\_no, Date, Diagnosis, Treatment) |
|
2260 |
|
|
|
2261 |
|
R1 (Treatment, Charge) |
|
2262 |
|
|
|
2263 |
|
We could further infer that the treatment for a given diagnosis is functionally dependant, but we should be sure to allow the doctor to have some flexibility when prescribing cures. |
|
2264 |
|
\end{solution} |
|
2265 |
|
|
|
2266 |
|
\begin{problem}[tags={ex}, points = {20}] |
|
2267 |
|
A friend of yours want you to review and improve the code for a role-playing game. |
|
2268 |
|
|
|
2269 |
|
The original idea was that each character should have a name, a class (e.g., Bard, Assassin, Druid), a certain amount of experience, a level, one or more weapons (providing bonuses) and can complete quests. |
|
2270 |
|
A quest have a name, and rewards the characters that completed it with a certain amount of experience, and sometimes (but rarely) with a special item. |
|
2271 |
|
|
|
2272 |
|
Your friend came up with the code presented in \autoref{lst:RPG}, page~\pageref{lst:RPG}, but there are several problems: |
|
2273 |
|
|
|
2274 |
|
\begin{itemize} |
|
2275 |
|
\item As of now, a character can have only one weapon. |
|
2276 |
|
All the attempts to \enquote{hack} the \sqli{CHARACTER} table to add an arbitrary number of weapons ended up creating horrible messes. |
|
2277 |
|
\item Every time a character completes a quest, a copy of the quest must be created. |
|
2278 |
|
Your friend is not so sure why, but nothing else works. |
|
2279 |
|
Also it seems that a character can complete only one quest, but your friend is not so sure about that. |
|
2280 |
|
\item It would be nice to be able to store features that are tied to the class, and not to the character, like the bonus they provide and the associated element (e.g., all bards use fire, all assassins use wind, etc.). |
|
2281 |
|
But you friend simply can't figure out how to do that. |
|
2282 |
|
\end{itemize} |
|
2283 |
|
|
|
2284 |
|
Can you provide a \emph{relational database schema} (no need to write the \sqli{SQL} code, but remember to indicate the primary and foreign keys) that would solve all of your friend's troubles? |
|
2285 |
|
|
|
2286 |
|
\end{problem} |
|
2287 |
|
|
|
2288 |
|
\clearpage |
|
2289 |
|
|
|
2290 |
|
|
|
2291 |
|
\begin{problem}[tags={ex}, points = {25}] |
|
2292 |
|
This exercise asks you to convert business statements into dependencies. |
|
2293 |
|
Consider the following relation: |
|
2294 |
|
\begin{center} |
|
2295 |
|
KEYBOARD(Manufacturer, Model, Layout, Retail\_Store, Price) |
|
2296 |
|
\end{center} |
|
2297 |
|
|
|
2298 |
|
A tuple in the KEYBOARD relation contains information about a computer keyboard: its manufacturer, its model, its layout (AZERTY, QWERTY, etc.), the place where it is sold, and its price. |
|
2299 |
|
|
|
2300 |
|
\begin{enumerate} |
|
2301 |
|
\item Write each of the following business statement as a functional dependency: |
|
2302 |
|
|
|
2303 |
|
\begin{enumerate}[topsep=0pt,itemsep=7em,partopsep=1ex,parsep=1ex] |
|
2304 |
|
\item A model has a fixed layout. |
|
2305 |
|
\item A retail store can't have two different models produced by the same manufacturer. |
|
2306 |
|
\item The price of one particular model can vary from one store to another, but all the keyboards of a particular model are sold at the same price at one particular store.\vspace{7em} |
|
2307 |
|
\end{enumerate} |
|
2308 |
|
|
|
2309 |
|
\item Based on those statements, what could be a key for this relation? |
|
2310 |
|
\vspace{6em} |
|
2311 |
|
\item Assuming all those functional dependencies hold, and taking the primary key you identified at the previous step, what is the degree of normality of this relation? Justify your answer. |
|
2312 |
|
\end{enumerate} |
|
2313 |
|
\end{problem} |
|
2314 |
|
|
|
2315 |
|
\clearpage |
|
2316 |
|
|
|
2317 |
|
\begin{problem}[tags={ex}, points={25}] |
|
2318 |
|
Consider the E.R. schema of Figure~\ref{fig:erCOUNTRY}, p.~\pageref{fig:erCOUNTRY}. |
|
2319 |
|
Map that E.-R. diagram to a relational database schema. |
|
2320 |
|
\end{problem} |
|
2321 |
|
\clearpage |
|
2322 |
|
|
|
2323 |
|
\begin{figure} |
|
2324 |
|
% \begin{listing} |
|
2325 |
|
\begin{minted}[linenos, breaklines=true]{mysql} |
|
2326 |
|
CREATE TABLE CHARACTER( |
|
2327 |
|
Name VARCHAR(30) PRIMARY KEY, |
|
2328 |
|
Class VARCHAR(30), |
|
2329 |
|
XP INT, |
|
2330 |
|
LVL INT, |
|
2331 |
|
Weapon_Name VARCHAR(30), |
|
2332 |
|
Weapon_Bonus INT, |
|
2333 |
|
Quest_Completed VARCHAR(30) |
|
2334 |
|
); |
|
2335 |
|
|
|
2336 |
|
CREATE TABLE QUEST( |
|
2337 |
|
Id VARCHAR(20) PRIMARY KEY, |
|
2338 |
|
Completed_By VARCHAR(30), |
|
2339 |
|
XP_Gained INT, |
|
2340 |
|
Special_Item VARCHAR(20), |
|
2341 |
|
FOREIGN KEY (Completed_By) REFERENCES CHARACTER(Name) |
|
2342 |
|
); |
|
2343 |
|
|
|
2344 |
|
ALTER TABLE CHARACTER ADD FOREIGN KEY (Quest_Completed) REFERENCES QUEST(Id); |
|
2345 |
|
\end{minted} |
|
2346 |
|
% \end{listing} |
|
2347 |
|
\caption{A \sqli{SQL} Code for a Role-Playing Game} |
|
2348 |
|
\label{lst:RPG} |
|
2349 |
|
\end{figure} |
|
2350 |
|
|
|
2351 |
|
\begin{figure} |
|
2352 |
|
\begin{tikzpicture}[node distance=8em] |
|
2353 |
|
\node[entity] (COUNTRY) {COUNTRY}; |
|
2354 |
|
\node[attribute] (name) [left of=COUNTRY] {\key{Name}} edge (COUNTRY); |
|
2355 |
|
\node[attribute] (population) [below left of=COUNTRY] {Population} edge (COUNTRY); |
|
2356 |
|
|
|
2357 |
|
\node[relationship] (SPEAKS) [below right of=COUNTRY] {SPEAKS} edge node[right, pos=0.4] {$M$} (COUNTRY); |
|
2358 |
|
\node[entity] (LANGUAGE) [below right of=SPEAKS] {LANGUAGE} edge node[right, pos=0.5] {$N$} (SPEAKS); |
|
2359 |
|
\node[attribute] (code) [left of = LANGUAGE] {\key{Code}} edge (LANGUAGE); |
|
2360 |
|
\node[attribute] (symbol) [right of = LANGUAGE] {Name} edge (LANGUAGE); |
|
2361 |
|
|
|
2362 |
|
\node[relationship] (BWF) [below of = LANGUAGE] {B\_W\_F}; |
|
2363 |
|
\draw (LANGUAGE) to node[left, pos=0.6] {$N$} (BWF.west); |
|
2364 |
|
\draw (LANGUAGE) to node[right, pos=0.6] {$M$} (BWF.east); |
|
2365 |
|
|
|
2366 |
|
%\node[relationship] (boundary) [above of = COUNTRY] {S\_B\_W}; |
|
2367 |
|
%\draw (COUNTRY) to node[left, pos=0.6] {$N$} (boundary.west); |
|
2368 |
|
%\draw (COUNTRY) to node[right, pos=0.6] {$M$} (boundary.east); |
|
2369 |
|
|
|
2370 |
|
%\node[relationship] (conversion) [below of =LANGUAGE] {CONVERSION}; |
|
2371 |
|
%\draw (LANGUAGE) to node[left, pos=0.6] {$N$} (conversion.west); |
|
2372 |
|
%\draw (LANGUAGE) to node[right, pos=0.6] {$M$} (conversion.east); |
|
2373 |
|
|
|
2374 |
|
%\node[attribute] (timestamp) [below left of=conversion] {\key{Timestamp}} edge (conversion); |
|
2375 |
|
%\node[attribute] (rate) [below right of=conversion] {Exchange\_rate} edge (conversion); |
|
2376 |
|
|
|
2377 |
|
\node[ident relationship] (hasfor) [right of=COUNTRY] {SINGS} edge node[above, pos=0.4] {$1$} (COUNTRY); |
|
2378 |
|
\node[weak entity] (anthem) %[right of=hasfor] |
|
2379 |
|
[right = 1cm of hasfor] {NATIONAL\_ANTHEM} edge[total] node[above, pos=0.6]{\(M\)} (hasfor); |
|
2380 |
|
\node[multi attribute] (color) [right =1cm of anthem] {Creator} edge (anthem); |
|
2381 |
|
\node[attribute] (name) [below right of = anthem] {\pkey{Name}} edge (anthem); |
|
2382 |
|
|
|
2383 |
|
\node[relationship] (WIN) [above right of =LANGUAGE] {W\_IN}; |
|
2384 |
|
\draw (LANGUAGE) to node[left, pos=0.6] {$N$} (WIN); |
|
2385 |
|
\draw (anthem) to node[right, pos=0.6] {$M$} (WIN); |
|
2386 |
|
\end{tikzpicture} |
|
2387 |
|
|
|
2388 |
|
%\enquote{S\_B\_W} stands for \enquote{SHARES\_A\_BORDER\_WITH}. |
|
2389 |
|
\enquote{W\_IN} stands for \enquote{WRITTEN\_IN}, and |
|
2390 |
|
\enquote{B\_W\_F} stands for \enquote{BORROWS\_WORDS\_FROM}. |
|
2391 |
|
For this relationship, on the left-hand side is the language that borrows a word, and on the right-hand side is the language that provides the loanword. |
|
2392 |
|
\caption{E.R. Schema for the COUNTRY\_INFO database} |
|
2393 |
|
\label{fig:erCOUNTRY} |
|
2394 |
|
\end{figure} |
|
2395 |
|
|
|
2396 |
|
%%% 10 Final |
|
2397 |
|
|
|
2398 |
|
|
|
2399 |
|
\begin{problem}[tags={ex}, points = {40}] |
|
2400 |
|
Look at the relational model from Figure~\ref{fig:RelMod}, p.~\pageref{fig:RelMod}, and \enquote{reverse-engineer} it to obtain an E.-R. diagram. |
|
2401 |
|
\end{problem} |
|
2402 |
|
|
|
2403 |
|
\clearpage |
|
2404 |
|
|
|
2405 |
|
\begin{problem}[tags={ex}, points = {30}] |
|
2406 |
|
Answer the following in one or two sentences: |
|
2407 |
|
|
|
2408 |
|
\begin{enumerate}[topsep=0pt,itemsep=12em,partopsep=1ex,parsep=1ex] |
|
2409 |
|
\item What is deletion anomaly? Is it a desirable feature? |
|
2410 |
|
\item What is polyglot persistence? Is it useful? |
|
2411 |
|
\item What does it mean to be \enquote{schemaless}? What does it imply? |
|
2412 |
|
\item What is denormalization? |
|
2413 |
|
When could that be useful? |
|
2414 |
|
\item What is the (object-relational) impedance mismatch? Is it an issue that can't be overcome? |
|
2415 |
|
\end{enumerate} |
|
2416 |
|
\end{problem} |
|
2417 |
|
|
|
2418 |
|
\begin{solution} |
|
2419 |
|
A Delete Anomaly exists when certain attributes are lost because of the deletion of other attributes. |
|
2420 |
|
|
|
2421 |
|
When storing data, it is best to use multiple data storage technologies, chosen based upon the way data is being used by individual applications or components of a single application. |
|
2422 |
|
\end{solution} |
|
2423 |
|
|
|
2424 |
|
\clearpage |
|
2425 |
|
|
|
2426 |
|
|
|
2427 |
|
\begin{problem}[tags={ex}, points = {35}] |
|
2428 |
|
Consider the following relation: |
|
2429 |
|
\vspace{1.5em} |
|
2430 |
|
\begin{center} |
|
2431 |
|
FLIGHT(From, \qquad To, \qquad Airline, \qquad Flight\#, \qquad DateHour, \qquad HeadQuarter, \qquad Pilot, \qquad TZDifference) |
|
2432 |
|
\end{center} |
|
2433 |
|
\vspace{1.5em} |
|
2434 |
|
|
|
2435 |
|
A tuple in the FLIGHT relation contains information about an airplane flight: the airports of departure and arrival, the airline carrier, the number of the flight, its time of departure, the headquarter of the company chartering the flight, the name of the pilot(s), and the time zone difference between the departure and arrival airports. |
|
2436 |
|
|
|
2437 |
|
The \enquote{Pilot} attribute is multi-valued (so that between \(1\) and \(4\) pilot's names can be stored in it). |
|
2438 |
|
Given an airline and a flight number, one can determine the departure and arrival airports, as well as the date and hour and the pilot(s). |
|
2439 |
|
Given the airline carrier, one can determine the headquarter. |
|
2440 |
|
Finally, given the departure and arrival airports, one can determine their time zone difference. |
|
2441 |
|
|
|
2442 |
|
Normalize the \enquote{FLIGHT} relation to its third normal form. |
|
2443 |
|
You can indicate your steps, justify your reasoning, and indicate the foreign keys if you want to, but don't have to. |
|
2444 |
|
\end{problem} |
|
2445 |
|
|
|
2446 |
|
\clearpage |
|
2447 |
|
|
|
2448 |
|
\begin{problem}[tags={ex}, points = {60}] |
|
2449 |
|
Consider the set of requirements in Figure~\ref{fig:reqUNIV}, p.~\pageref{fig:reqUNIV}. |
|
2450 |
|
Draw an E.-R. diagram for that schema. |
|
2451 |
|
Specify key attributes of each entity type and structural constraints on each relationship type. |
|
2452 |
|
Note any unspecified requirements, and make appropriate assumptions to make the specification complete. |
|
2453 |
|
\end{problem} |
|
2454 |
|
\clearpage |
|
2455 |
|
|
|
2456 |
|
\begin{problem}[tags={ex}, points = {35}] |
|
2457 |
|
Consider the UML diagram in Figure~\ref{fig:UML}, p.~\pageref{fig:UML}, and convert it to the relational model. |
|
2458 |
|
Don't forget to indicate primary and foreign keys. |
|
2459 |
|
\clearpage |
|
2460 |
|
\end{problem} |
|
2461 |
|
|
|
2462 |
|
\begin{figure} |
|
2463 |
|
\colorlet{lightgray}{gray!20} |
|
2464 |
|
|
|
2465 |
|
\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} |
|
2466 |
|
|
|
2467 |
|
% RELATIONS |
|
2468 |
|
|
|
2469 |
|
\node (actortitle) {\textbf{ACTOR}}; |
|
2470 |
|
|
|
2471 |
|
\node [relation=3, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of actortitle.west, anchor=west] (actor) |
|
2472 |
|
{\underline{Id}% |
|
2473 |
|
\nodepart{two} Name |
|
2474 |
|
\nodepart{three} Birthdate |
|
2475 |
|
}; |
|
2476 |
|
|
|
2477 |
|
\node [below=1.3cm of actor.west, anchor=west] (movietitle) {\textbf{MOVIE}}; |
|
2478 |
|
|
|
2479 |
|
\node [relation=4, rectangle split horizontal, rectangle split part fill={lightgray!50}, below=0.6cm of movietitle.west, anchor=west] (movie) |
|
2480 |
|
{\underline{Title}% |
|
2481 |
|
\nodepart{two} Year |
|
2482 |
|
\nodepart{three} Length |
|
2483 |
|
\nodepart{four} Studio |
|
2484 |
|
}; |
|
2485 |
|
|
|
2486 |
|
|
|
2487 |
|
\node [right=8cm of actortitle.west, anchor=west] (actingtitle) {\textbf{ACTING}}; |
|
2488 |
|
|
|
2489 |
|
\node [relation=2, rectangle split horizontal, rectangle split part fill={lightgray!50}, below=0.6cm of actingtitle.west, anchor=west] (acting) |
|
2490 |
|
{\underline{ActorID}% |
|
2491 |
|
\nodepart{two} \underline{MovieTitle}% |
|
2492 |
|
}; |
|
2493 |
|
|
|
2494 |
|
\node [below=1.3cm of movie.west, anchor=west] (theatretitle) {\textbf{THEATRE}}; |
|
2495 |
|
|
|
2496 |
|
\node [relation=5, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of theatretitle.west, anchor=west] (theatre) |
|
2497 |
|
{\underline{Name}% |
|
2498 |
|
\nodepart{two} Street |
|
2499 |
|
\nodepart{three} City |
|
2500 |
|
\nodepart{four} State |
|
2501 |
|
\nodepart{five} Zip |
|
2502 |
|
}; |
|
2503 |
|
|
|
2504 |
|
\node [below=1.3cm of acting.west, anchor=west] (showingtitle) {\textbf{SHOWING}}; |
|
2505 |
|
|
|
2506 |
|
\node [relation=4, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of showingtitle.west, anchor=west] (showing) |
|
2507 |
|
{\underline{TheaterName}% |
|
2508 |
|
\nodepart{two} \underline{MovieTitle} |
|
2509 |
|
\nodepart{three} \underline{Day} |
|
2510 |
|
\nodepart{four} \underline{Time} |
|
2511 |
|
}; |
|
2512 |
|
|
|
2513 |
|
|
|
2514 |
|
% FOREIGN KEYS |
|
2515 |
|
|
|
2516 |
|
%\draw[-latex] (acting) -- -| (actor.one south);%(actor.one south) -- ++(0,-0.2) --++(7,0) --++(0, -2) -| ($(movie.one south) + (0.2,0)$); |
|
2517 |
|
\draw[-latex] ($(showing.two south) + (-0.3,0)$)-- ++(0,-0.3) -| (movie.one south); |
|
2518 |
|
\draw[-latex] (showing.one south) -- ++ (0,-2.6) -| (theatre.one south); |
|
2519 |
|
\draw[-latex] (acting.one south) -- ++ (0,-0.3) -| (actor.one south); |
|
2520 |
|
\draw[-latex] (acting.two south) -- ++ (0,-0.5) -- ++(-3, 0) -- ++(0, -2.1)-| ($(movie.one south) + (-0.25,0)$); |
|
2521 |
|
%\draw[-latex] (showing.four south) -- ++(0,-0.2) --++(2,0) --++(0, 1.75) -| ($(theatre.one south) + (0.1,0)$); |
|
2522 |
|
%\draw[-latex] (theatre.five south) -- ++(0,-0.2) --++(2,0) --++(0, 1.5) -| ($(movie.one south) + (-0.1,0)$); |
|
2523 |
|
\end{tikzpicture} |
|
2524 |
|
\caption{Relational Model for a MOVIE Database} |
|
2525 |
|
\label{fig:RelMod} |
|
2526 |
|
\end{figure} |
|
2527 |
|
|
|
2528 |
|
\begin{figure} |
|
2529 |
|
Consider the following requirements for a UNIVERSITY database, used to keep track of students' transcripts. |
|
2530 |
|
\begin{enumerate} |
|
2531 |
|
\item The university keeps track of each student's name, student number, class (freshman, sophomore, \dots, graduate), major department, minor department (if any), and degree program (B.A., B.S., \dots, Ph.D.). |
|
2532 |
|
Student number has unique values for each student. |
|
2533 |
|
\item Each department is described by a name and has a (unique) department code. |
|
2534 |
|
\item Each course has a course name, a course number, credit hours, and is offered by at least one department. The value of course number is unique for each course. A course has at least one section. |
|
2535 |
|
\item Each section of a course has an instructor, a semester, a year, and a section number. |
|
2536 |
|
The section number distinguishes different sections of the same course that are taught during the same semester/year; its values are 1, 2, 3, \dots, up to the number of sections taught during each semester. Students can enroll in sections and receive a letter grade, and grade point (0, 1, 2, 3, 4 for F, D, C, B, A, respectively). |
|
2537 |
|
\end{enumerate} |
|
2538 |
|
\caption{Requirements for a UNIVERSITY database} |
|
2539 |
|
\label{fig:reqUNIV} |
|
2540 |
|
\end{figure} |
|
2541 |
|
|
|
2542 |
|
\begin{figure} |
|
2543 |
|
\begin{tikzpicture}[scale = 0.8] |
|
2544 |
|
\node (movie) at (0,0){\begin{tabular}{| r l |} |
|
2545 |
|
\hline |
|
2546 |
|
\multicolumn{2}{| c |}{\textbf{DRIVER}}\\ |
|
2547 |
|
\hline |
|
2548 |
|
id &: String\\ |
|
2549 |
|
dob &: Date\\ |
|
2550 |
|
name &: String\\ |
|
2551 |
|
address &: Street\\ |
|
2552 |
|
& City\\ |
|
2553 |
|
\hline |
|
2554 |
|
getAge()&: Int\\ |
|
2555 |
|
\hline |
|
2556 |
|
\end{tabular} |
|
2557 |
|
}; |
|
2558 |
|
\node (cdriver) at (8, 1){\begin{tabular}{| r l |} |
|
2559 |
|
\hline |
|
2560 |
|
\multicolumn{2}{| c |}{\textbf{COMMERCIAL\_DRIVER}}\\ |
|
2561 |
|
\hline |
|
2562 |
|
Class &: String\\ |
|
2563 |
|
\hline |
|
2564 |
|
\end{tabular} |
|
2565 |
|
}; |
|
2566 |
|
%\draw[open diamond-] (movie) -- node[above, pos=0.1]{\(0..\star\)} node[above, pos=0.9]{\(1..1\)} (cdriver); |
|
2567 |
|
\draw[{Triangle[open]}-] (movie) -- %node[above, pos=0.1]{\(0..\star\)} node[above, pos=0.9]{\(1..1\)} |
|
2568 |
|
(cdriver); |
|
2569 |
|
\node (car) at (9, -2){\begin{tabular}{| r l |} |
|
2570 |
|
\hline |
|
2571 |
|
\multicolumn{2}{| c |}{\textbf{CAR}}\\ |
|
2572 |
|
\hline |
|
2573 |
|
vin &: String\\ |
|
2574 |
|
make &: String\\ |
|
2575 |
|
year &: Year\\ |
|
2576 |
|
brand &: String\\ |
|
2577 |
|
\hline |
|
2578 |
|
\end{tabular} |
|
2579 |
|
}; |
|
2580 |
|
\draw[-] ($(movie)+(2.5,-0.5)$) -- node[above, pos=0.1]{\(0..*\)} node[above=0.5em]{POSSESSES} node[above, pos=0.9]{\(0..*\)} (car); |
|
2581 |
|
%\draw[-] (movie) -- node[below, pos=0.1]{\(0..1\)} node[below, pos=0.9]{\(0..4\)} ($(car)+(-2.5, 0.5)$); |
|
2582 |
|
\node (seat) at (3.5, -5){\begin{tabular}{| r l |} |
|
2583 |
|
\hline |
|
2584 |
|
\multicolumn{2}{| c |}{\textbf{POSSESSION\_HISTORY}}\\ |
|
2585 |
|
\hline |
|
2586 |
|
date of purchase &: Date\\ |
|
2587 |
|
\hline |
|
2588 |
|
\end{tabular} |
|
2589 |
|
}; |
|
2590 |
|
\draw[dashed] (seat) -- (3.5, -1.2); |
|
2591 |
|
|
|
2592 |
|
\node (insu) at (15.5, 2){\begin{tabular}{| r l |} |
|
2593 |
|
\hline |
|
2594 |
|
\multicolumn{2}{| c |}{\textbf{CAR\_INSURANCE}}\\ |
|
2595 |
|
\hline |
|
2596 |
|
policy number &: String\\ |
|
2597 |
|
covered amount &: int\\ |
|
2598 |
|
compagny name &: String\\ |
|
2599 |
|
\hline |
|
2600 |
|
\end{tabular} |
|
2601 |
|
}; |
|
2602 |
|
|
|
2603 |
|
\draw[diamond -] (car) -- node[below, pos=0.1, right=.2]{\(1..1\)} node[below, pos=0.9, right=0.2]{\(1..\star\)} node[right=0.2]{IS\_COVERED\_BY} (insu); |
|
2604 |
|
\end{tikzpicture} |
|
2605 |
|
\caption{UML Diagram for the CAR\_POSSESSION Database} |
|
2606 |
|
\label{fig:UML} |
|
2607 |
|
\end{figure} |
|
2608 |
|
|
|
2609 |
|
\end{document} |
|
2610 |
|
|
|
2611 |
|
|
|
2612 |
|
|
|
2613 |
|
\begin{problem}[tags={ex}, points = {40}] |
|
2614 |
|
\label{nf} |
|
2615 |
|
|
|
2616 |
|
Consider the relation |
|
2617 |
|
|
|
2618 |
|
\begin{center} |
|
2619 |
|
CONSULTATION(Doctor\_no, Patient\_no, Date, Diagnosis, Treatment, Charge, Insurance) |
|
2620 |
|
\end{center} |
|
2621 |
|
|
|
2622 |
|
with the following functional dependencies: |
|
2623 |
|
|
|
2624 |
|
\begin{center} |
|
2625 |
|
\begin{tabular}{r c l} |
|
2626 |
|
\{Doctor\_no, Patient\_no, Date\} & \(\to\) & \{Diagnosis\}\\ |
|
2627 |
|
\{Doctor\_no, Patient\_no, Date\} & \(\to\) & \{Treatment\}\\ |
|
2628 |
|
\{Treatment, Insurance\} & \(\to\) & \{Charge\}\\ |
|
2629 |
|
\{Patient\_no\} & \(\to\) & \{Insurance\} |
|
2630 |
|
\end{tabular} |
|
2631 |
|
\end{center} |
|
2632 |
|
|
|
2633 |
|
\begin{enumerate}[topsep=0pt,itemsep=10em,partopsep=1ex,parsep=1ex] |
|
2634 |
|
\item The designer decided not to add the functional dependency \{Diagnosis\} $\to$ \{Treatment\}. |
|
2635 |
|
Explain what could be the designer's justification, at the level of the mini-world. |
|
2636 |
|
\item Identify a primary key for this relation. |
|
2637 |
|
\item What is the degree of normalization of this relation? |
|
2638 |
|
Normalize it to the third normal form if necessary. |
|
2639 |
|
\end{enumerate} |
|
2640 |
|
\end{problem} |
|
2641 |
|
\clearpage |
|
2642 |
|
|
|
2643 |
|
\begin{solution} |
|
2644 |
|
Answer: |
|
2645 |
|
Because there are no partial dependencies, the given relation is in 2NF already. This however is not 3NF because the Charge is a nonkey attribute that is determined by another nonkey attribute, Treatment. We must decompose further: |
|
2646 |
|
|
|
2647 |
|
R (Doctor\_no, Patient\_no, Date, Diagnosis, Treatment) |
|
2648 |
|
|
|
2649 |
|
R1 (Treatment, Charge) |
|
2650 |
|
|
|
2651 |
|
We could further infer that the treatment for a given diagnosis is functionally dependant, but we should be sure to allow the doctor to have some flexibility when prescribing cures. |
|
2652 |
|
\end{solution} |
|
2653 |
|
|
|
2654 |
|
\begin{problem}[tags={ex}, points = {40}] |
|
2655 |
|
A friend of yours want you to review and improve the code for a role-playing game. |
|
2656 |
|
|
|
2657 |
|
The original idea was that each character should have a name, a class (e.g., Bard, Assassin, Druid), a certain amount of experience, a level, one or more weapons (providing bonuses) and can complete quests. |
|
2658 |
|
A quest have a name, and rewards the characters that completed it with a certain amount of experience, and sometimes (but rarely) with a special item. |
|
2659 |
|
|
|
2660 |
|
Your friend came up with the code presented in \autoref{lst:RPG}, page~\pageref{lst:RPG}, but there are several problems: |
|
2661 |
|
|
|
2662 |
|
\begin{itemize} |
|
2663 |
|
\item As of now, a character can have only one weapon. |
|
2664 |
|
All the attempts to \enquote{hack} the \sqli{CHARACTER} table to add an arbitrary number of weapons ended up creating horrible messes. |
|
2665 |
|
\item Every time a character completes a quest, a copy of the quest must be created. |
|
2666 |
|
Your friend is not so sure why, but nothing else works. |
|
2667 |
|
Also it seems that a character can complete only one quest, but your friend is not so sure about that. |
|
2668 |
|
\item It would be nice to be able to store features that are tied to the class, and not to the character, like the bonus they provide and the associated element (e.g., all bards use fire, all assassins use wind, etc.). |
|
2669 |
|
But you friend simply can't figure out how to do that. |
|
2670 |
|
\end{itemize} |
|
2671 |
|
|
|
2672 |
|
Can you provide a \emph{relational database schema} (no need to write the \sqli{SQL} code, but remember to indicate the primary and foreign keys) that would solve all of your friend's troubles? |
|
2673 |
|
|
|
2674 |
|
\end{problem} |
|
2675 |
|
|
|
2676 |
|
\clearpage |
|
2677 |
|
|
|
2678 |
|
|
|
2679 |
|
\begin{problem}[tags={ex}, points = {40}] |
|
2680 |
|
This exercise asks you to convert business statements into dependencies. |
|
2681 |
|
Consider the following relation: |
|
2682 |
|
\begin{center} |
|
2683 |
|
KEYBOARD(Manufacturer, Model, Layout, Retail\_Store, Price) |
|
2684 |
|
\end{center} |
|
2685 |
|
|
|
2686 |
|
A tuple in the KEYBOARD relation contains information about a computer keyboard: its manufacturer, its model, its layout (AZERTY, QWERTY, etc.), the place where it is sold, and its price. |
|
2687 |
|
|
|
2688 |
|
\begin{enumerate} |
|
2689 |
|
\item Write each of the following business statement as a functional dependency: |
|
2690 |
|
|
|
2691 |
|
\begin{enumerate}[topsep=0pt,itemsep=7em,partopsep=1ex,parsep=1ex] |
|
2692 |
|
\item A model has a fixed layout. |
|
2693 |
|
\item A retail store can't have two different models produced by the same manufacturer. |
|
2694 |
|
\item The price of one particular model can vary from one store to another, but all the keyboards of a particular model are sold at the same price at one particular store.\vspace{7em} |
|
2695 |
|
\end{enumerate} |
|
2696 |
|
|
|
2697 |
|
\item Based on those statements, what could be a key for this relation? |
|
2698 |
|
\vspace{6em} |
|
2699 |
|
\item Assuming all those functional dependencies hold, and taking the primary key you identified at the previous step, what is the degree of normality of this relation? Justify your answer. |
|
2700 |
|
\end{enumerate} |
|
2701 |
|
\end{problem} |
|
2702 |
|
|
|
2703 |
|
\clearpage |
|
2704 |
|
|
|
2705 |
|
\begin{problem}[tags={ex}, points={40}] |
|
2706 |
|
Consider the E.R. schema of Figure~\ref{fig:erCOUNTRY}, p.~\pageref{fig:erCOUNTRY}. |
|
2707 |
|
Map that E.-R. diagram to a relational database schema. |
|
2708 |
|
\end{problem} |
|
2709 |
|
\clearpage |
|
2710 |
|
|
|
2711 |
|
\begin{figure} |
|
2712 |
|
% \begin{listing} |
|
2713 |
|
\begin{minted}[linenos, breaklines=true]{mysql} |
|
2714 |
|
CREATE TABLE CHARACTER( |
|
2715 |
|
Name VARCHAR(30) PRIMARY KEY, |
|
2716 |
|
Class VARCHAR(30), |
|
2717 |
|
XP INT, |
|
2718 |
|
LVL INT, |
|
2719 |
|
Weapon_Name VARCHAR(30), |
|
2720 |
|
Weapon_Bonus INT, |
|
2721 |
|
Quest_Completed VARCHAR(30) |
|
2722 |
|
); |
|
2723 |
|
|
|
2724 |
|
CREATE TABLE QUEST( |
|
2725 |
|
Id VARCHAR(20) PRIMARY KEY, |
|
2726 |
|
Completed_By VARCHAR(30), |
|
2727 |
|
XP_Gained INT, |
|
2728 |
|
Special_Item VARCHAR(20), |
|
2729 |
|
FOREIGN KEY (Completed_By) REFERENCES CHARACTER(Name) |
|
2730 |
|
); |
|
2731 |
|
|
|
2732 |
|
ALTER TABLE CHARACTER ADD FOREIGN KEY (Quest_Completed) REFERENCES QUEST(Id); |
|
2733 |
|
\end{minted} |
|
2734 |
|
% \end{listing} |
|
2735 |
|
\caption{A \sqli{SQL} Code for a Role-Playing Game} |
|
2736 |
|
\label{lst:RPG} |
|
2737 |
|
\end{figure} |
|
2738 |
|
|
|
2739 |
|
\begin{figure} |
|
2740 |
|
\begin{tikzpicture}[node distance=8em] |
|
2741 |
|
\node[entity] (COUNTRY) {COUNTRY}; |
|
2742 |
|
\node[attribute] (name) [left of=COUNTRY] {\key{Name}} edge (COUNTRY); |
|
2743 |
|
\node[attribute] (population) [below left of=COUNTRY] {Population} edge (COUNTRY); |
|
2744 |
|
|
|
2745 |
|
\node[relationship] (SPEAKS) [below right of=COUNTRY] {SPEAKS} edge node[right, pos=0.4] {$M$} (COUNTRY); |
|
2746 |
|
\node[entity] (LANGUAGE) [below right of=SPEAKS] {LANGUAGE} edge node[right, pos=0.5] {$N$} (SPEAKS); |
|
2747 |
|
\node[attribute] (code) [left of = LANGUAGE] {\key{Code}} edge (LANGUAGE); |
|
2748 |
|
\node[attribute] (symbol) [right of = LANGUAGE] {Name} edge (LANGUAGE); |
|
2749 |
|
|
|
2750 |
|
\node[relationship] (BWF) [below of = LANGUAGE] {B\_W\_F}; |
|
2751 |
|
\draw (LANGUAGE) to node[left, pos=0.6] {$N$} (BWF.west); |
|
2752 |
|
\draw (LANGUAGE) to node[right, pos=0.6] {$M$} (BWF.east); |
|
2753 |
|
|
|
2754 |
|
%\node[relationship] (boundary) [above of = COUNTRY] {S\_B\_W}; |
|
2755 |
|
%\draw (COUNTRY) to node[left, pos=0.6] {$N$} (boundary.west); |
|
2756 |
|
%\draw (COUNTRY) to node[right, pos=0.6] {$M$} (boundary.east); |
|
2757 |
|
|
|
2758 |
|
%\node[relationship] (conversion) [below of =LANGUAGE] {CONVERSION}; |
|
2759 |
|
%\draw (LANGUAGE) to node[left, pos=0.6] {$N$} (conversion.west); |
|
2760 |
|
%\draw (LANGUAGE) to node[right, pos=0.6] {$M$} (conversion.east); |
|
2761 |
|
|
|
2762 |
|
%\node[attribute] (timestamp) [below left of=conversion] {\key{Timestamp}} edge (conversion); |
|
2763 |
|
%\node[attribute] (rate) [below right of=conversion] {Exchange\_rate} edge (conversion); |
|
2764 |
|
|
|
2765 |
|
\node[ident relationship] (hasfor) [right of=COUNTRY] {SINGS} edge node[above, pos=0.4] {$1$} (COUNTRY); |
|
2766 |
|
\node[weak entity] (anthem) %[right of=hasfor] |
|
2767 |
|
[right = 1cm of hasfor] {NATIONAL\_ANTHEM} edge[total] node[above, pos=0.6]{\(M\)} (hasfor); |
|
2768 |
|
\node[multi attribute] (color) [right =1cm of anthem] {Creator} edge (anthem); |
|
2769 |
|
\node[attribute] (name) [below right of = anthem] {\pkey{Name}} edge (anthem); |
|
2770 |
|
|
|
2771 |
|
\node[relationship] (WIN) [above right of =LANGUAGE] {W\_IN}; |
|
2772 |
|
\draw (LANGUAGE) to node[left, pos=0.6] {$N$} (WIN); |
|
2773 |
|
\draw (anthem) to node[right, pos=0.6] {$M$} (WIN); |
|
2774 |
|
\end{tikzpicture} |
|
2775 |
|
|
|
2776 |
|
%\enquote{S\_B\_W} stands for \enquote{SHARES\_A\_BORDER\_WITH}. |
|
2777 |
|
\enquote{W\_IN} stands for \enquote{WRITTEN\_IN}, and |
|
2778 |
|
\enquote{B\_W\_F} stands for \enquote{BORROWS\_WORDS\_FROM}. |
|
2779 |
|
For this relationship, on the left-hand side is the language that borrows a word, and on the right-hand side is the language that provides the loanword. |
|
2780 |
|
\caption{E.R. Schema for the COUNTRY\_INFO database} |
|
2781 |
|
\label{fig:erCOUNTRY} |
|
2782 |
|
\end{figure} |
|
2783 |
|
|
|