File notes/lectures_notes.md changed (mode: 100644) (index 3a71369..75fdc6d) |
... |
... |
Refer to the ["The cycle of design" figure](#fig:cycle). |
281 |
281 |
| 2910 | 1301 | |
| 2910 | 1301 | |
282 |
282 |
| 1302 | 1301 | |
| 1302 | 1301 | |
283 |
283 |
|
|
284 |
|
|
|
|
284 |
|
Note that the structure of this database could be described as: |
|
285 |
|
|
|
286 |
|
**RELATIONS** |
|
287 |
|
|
|
288 |
|
| Relation Name | Number of Columns| |
|
289 |
|
| :--- | :--- | |
|
290 |
|
| STUDENT | 4 | |
|
291 |
|
| COURSE | 4 | |
|
292 |
|
| SECCTION | 5 | |
|
293 |
|
| GRADE_REPORT | 3 | |
|
294 |
|
| PREREQUISITE | 2 | |
|
295 |
|
|
|
296 |
|
**COLUMNS** |
|
297 |
|
|
|
298 |
|
| Column Name | Datatype | Belongs to relation| |
|
299 |
|
| :---: | :--- | :--- | |
|
300 |
|
| Name | String | STUDENT | |
|
301 |
|
| Student_number | Integer | STUDENT | |
|
302 |
|
| Class | String | STUDENT | |
|
303 |
|
| Major | String | STUDENT | |
|
304 |
|
| Course_name | String | COURSE | |
|
305 |
|
| Course_number | Integer | COURSE | |
|
306 |
|
| Credit_hours | Integer | COURSE | |
|
307 |
|
| Department | String | COURSE | |
|
308 |
|
| … | … | … | |
|
309 |
|
| Prerequisite_number | Integer | PREREQUISITE | |
|
310 |
|
|
|
311 |
|
|
285 |
312 |
### Structure |
### Structure |
286 |
313 |
|
|
287 |
314 |
- Database structure and records, 5 files (=collection of records), each containing data records of the same type. **Persistent storage** |
- Database structure and records, 5 files (=collection of records), each containing data records of the same type. **Persistent storage** |
|
... |
... |
It's easier to move things around early in the conception. |
327 |
354 |
|
|
328 |
355 |
## Exercises {-} |
## Exercises {-} |
329 |
356 |
|
|
|
357 |
|
|
330 |
358 |
Exercise +.# |
Exercise +.# |
331 |
359 |
|
|
332 |
|
: Is a pile of trash a database? Why, or why not? |
|
|
360 |
|
: What is the difference between a database and the meta-data of the database? |
333 |
361 |
|
|
334 |
362 |
Exercise +.# |
Exercise +.# |
335 |
363 |
|
|
336 |
|
: Which one comes first, the physical design, the conceptual design, or the logical design? |
|
|
364 |
|
: Is a pile of trash a database? Why, or why not? |
337 |
365 |
|
|
338 |
366 |
Exercise +.# |
Exercise +.# |
339 |
367 |
|
|
340 |
|
: What is the difference between a database and the meta-data of the database? |
|
|
368 |
|
: Define the word "miniworld". |
341 |
369 |
|
|
342 |
370 |
Exercise +.# |
Exercise +.# |
343 |
371 |
|
|
344 |
|
: Do I have to change my DBMS if I want to change the structure of my data? |
|
|
372 |
|
: Assign each of the following task to one of the "character" (administrator, client, etc.) we introduced: |
|
373 |
|
|
|
374 |
|
<!-- Bug with table --> |
|
375 |
|
|
|
376 |
|
| Task | Assigned to| |
|
377 |
|
| :--- | :--- | |
|
378 |
|
| Install a DBMS on a server. | | |
|
379 |
|
| Sketch the schema so that the data will not be redundant. | | |
|
380 |
|
| Write client-side application that uses the DBMS API.| | |
|
381 |
|
| Establish the purpose of the database. | | |
345 |
382 |
|
|
346 |
383 |
Exercise +.# |
Exercise +.# |
347 |
384 |
|
|
348 |
|
: What is a virtual data? How can I access it? |
|
|
385 |
|
: Expand the acronym "DBMS". |
349 |
386 |
|
|
350 |
387 |
Exercise +.#nameDBMS |
Exercise +.#nameDBMS |
351 |
388 |
|
|
|
... |
... |
Exercise +.# |
355 |
392 |
|
|
356 |
393 |
: Why do DBMS include concurrency control? |
: Why do DBMS include concurrency control? |
357 |
394 |
|
|
|
395 |
|
Exercise +.# |
|
396 |
|
|
|
397 |
|
: Do I have to change my DBMS if I want to change the structure of my data? |
|
398 |
|
|
358 |
399 |
Exercise +.#programdataindependence |
Exercise +.#programdataindependence |
359 |
400 |
|
|
360 |
401 |
: What is independence between program and data? Why does it matter? |
: What is independence between program and data? Why does it matter? |
361 |
402 |
|
|
362 |
403 |
Exercise +.# |
Exercise +.# |
363 |
404 |
|
|
364 |
|
: Define the word "miniworld". |
|
|
405 |
|
: Name the four different kinds of action that can be performed on data. |
365 |
406 |
|
|
366 |
407 |
Exercise +.# |
Exercise +.# |
367 |
408 |
|
|
368 |
|
: Expand the acronym "DBMS". |
|
|
409 |
|
: Assume that I have a file where one record corresponds to one student. Should the information about the classes a student is taking (e.g. room, instructor, code, etc.) being stored in the same file? Why, or why not? |
369 |
410 |
|
|
370 |
411 |
Exercise +.# |
Exercise +.# |
371 |
412 |
|
|
372 |
|
: Name the four different kinds of action that can be performed on data. |
|
|
413 |
|
: Which one comes first, the physical design, the conceptual design, or the logical design? |
373 |
414 |
|
|
374 |
415 |
Exercise +.# |
Exercise +.# |
375 |
416 |
|
|
376 |
|
: Assume that I have a file where one record corresponds to one student. Should the information about the classes a student is taking (e.g. room, instructor, code, etc.) being stored in the same file? Why, or why not? |
|
|
417 |
|
: What is a virtual data? How can I access it? |
377 |
418 |
|
|
378 |
419 |
|
|
379 |
420 |
## Solution to Exercises {-} |
## Solution to Exercises {-} |
380 |
421 |
|
|
381 |
422 |
Solution +.# |
Solution +.# |
382 |
423 |
|
|
383 |
|
: No, because it lacks a logical structure. |
|
|
424 |
|
: The data is the information we want to store, the meta-data is its organization, how we are going to store it. Meta-data is information about the data, but of no use on its own. |
384 |
425 |
|
|
385 |
426 |
Solution +.# |
Solution +.# |
386 |
427 |
|
|
387 |
|
: The conceptual design. |
|
|
428 |
|
: No, because it lacks a logical structure. |
388 |
429 |
|
|
389 |
430 |
Solution +.# |
Solution +.# |
390 |
431 |
|
|
391 |
|
: The data is the information we want to store, the meta-data is its organization, how we are going to store it. Meta-data is information about the data, but of no use on its own. |
|
|
432 |
|
: The mini-world is the part of the universe we want to represent in the database. It is supposed to be meaningful and will serve a purpose. |
392 |
433 |
|
|
393 |
434 |
Solution +.# |
Solution +.# |
394 |
435 |
|
|
395 |
|
: Normally no, data and programs are indepent. But actually, this is true only if the model doesn't change: shifting to a "less structured model", e.g., one of the NoSQL models, can require to change the DBMS. |
|
|
436 |
|
: We can have something like: |
396 |
437 |
|
|
397 |
|
Solution +.# |
|
|
438 |
|
<!-- Bug with table --> |
398 |
439 |
|
|
399 |
|
: It is a set of information that is derived from the database but not directly stored in it. It is accessed through queries. |
|
|
440 |
|
| Task | Assigned to| |
|
441 |
|
| :--- | :--- | |
|
442 |
|
| Install a DBMS on a server. | Administrator, IT service | |
|
443 |
|
| Sketch the schema so that the data will not be redundant. | Designer | |
|
444 |
|
| Write client-side application that uses the DBMS API.| Programmer, Developer | |
|
445 |
|
| Establish the purpose of the database. | Client, business owner | |
400 |
446 |
|
|
401 |
447 |
Solution +.# |
Solution +.# |
402 |
448 |
|
|
403 |
|
: Oracle RDBMS, IBM DB2, Microsoft SQL Server, MySQL, PostgreSQL, Microsoft Access, etc., are valid answers. Are not valid "SQL", "NoSQL", "Relational Model", or such: we are asking for the names of actual softwares! |
|
|
449 |
|
: Database Management System |
404 |
450 |
|
|
405 |
451 |
Solution +.# |
Solution +.# |
406 |
452 |
|
|
407 |
|
: To ensure that several users trying to update the same data will do so in a controlled manner. To avoid inconsistency. |
|
|
453 |
|
: Oracle RDBMS, IBM DB2, Microsoft SQL Server, MySQL, PostgreSQL, Microsoft Access, etc., are valid answers. Are not valid "SQL", "NoSQL", "Relational Model", or such: we are asking for the names of actual softwares! |
408 |
454 |
|
|
409 |
455 |
Solution +.# |
Solution +.# |
410 |
456 |
|
|
411 |
|
: The application should not be sensible to the "internals" of the definition and organization of the data. It matters because having this independance means that changing the data will not require to change the programs. |
|
|
457 |
|
: Normally no, data and programs are independent. But actually, this is true only if the model doesn't change: shifting to a "less structured model", e.g., one of the NoSQL models, can require to change the DBMS. |
412 |
458 |
|
|
413 |
459 |
Solution +.# |
Solution +.# |
414 |
460 |
|
|
415 |
|
: The mini-world is the part of the universe we want to represent in the database. It is supposed to be meaningfull and will serve a purpose. |
|
|
461 |
|
: DBMS have concurrency control to ensure that several users trying to update the same data will do so in a controlled manner. It is to avoid inconsistency to appear in the data. |
416 |
462 |
|
|
417 |
463 |
Solution +.# |
Solution +.# |
418 |
464 |
|
|
419 |
|
: Database Management System |
|
|
465 |
|
: The application should not be sensible to the "internals" of the definition and organization of the data. It matters because having this independence means that changing the data will not require to change the programs. |
420 |
466 |
|
|
421 |
467 |
Solution +.# |
Solution +.# |
422 |
468 |
|
|
|
... |
... |
Solution +.# |
431 |
477 |
|
|
432 |
478 |
: If we were to store all the information about the classes in the student records, then we would have to store it as many time as its number of students! It is better to store it in a different file, and then to "link" the two files, to avoid redundancy. |
: If we were to store all the information about the classes in the student records, then we would have to store it as many time as its number of students! It is better to store it in a different file, and then to "link" the two files, to avoid redundancy. |
433 |
479 |
|
|
|
480 |
|
Solution +.# |
|
481 |
|
|
|
482 |
|
: The conceptual design. |
|
483 |
|
|
|
484 |
|
Solution +.# |
|
485 |
|
|
|
486 |
|
: It is a set of information that is derived from the database but not directly stored in it. It is accessed through queries. For instance, we can infer the age of a person if their date of birth is in the database, but strictly speaking the age is not an information stored in the database. |
|
487 |
|
|
|
488 |
|
|
|
489 |
|
|
|
490 |
|
|
|
491 |
|
|
|
492 |
|
|
434 |
493 |
## Problems {-} |
## Problems {-} |
435 |
494 |
|
|
436 |
495 |
Problem (A database catalog for a campus) +.#campus |
Problem (A database catalog for a campus) +.#campus |
|
... |
... |
Solution to @problem:campus |
489 |
548 |
**COLUMNS** |
**COLUMNS** |
490 |
549 |
|
|
491 |
550 |
| Column\_name | Data\_type | Belongs\_to\_relation | |
| Column\_name | Data\_type | Belongs\_to\_relation | |
492 |
|
| --- | --- | --- |
|
493 |
|
Building\_Name | Character(30) | `Building` |
|
494 |
|
GPSLat | Decimal(9,6) | `Building` |
|
495 |
|
GPSLon | Decimal(9,6) | `Building` |
|
496 |
|
Building\_Name | Character(30) | `ROOM` |
|
497 |
|
Room\_Number | Integer(1) | `ROOM` |
|
498 |
|
Floor | Integer (1) | `ROOM` |
|
499 |
|
Prof\_Name | Character (30) | `PROF` |
|
500 |
|
Phone | Integer (10) | `PROF` |
|
501 |
|
Email | Character (30) | `PROF` |
|
502 |
|
Room\_Number | Integer (1) | `PROF` |
|
|
551 |
|
| --- | --- | --- | |
|
552 |
|
Building\_Name | Character(30) | `Building` | |
|
553 |
|
GPSLat | Decimal(9,6) | `Building`| |
|
554 |
|
GPSLon | Decimal(9,6) | `Building` | |
|
555 |
|
Building\_Name | Character(30) | `ROOM`| |
|
556 |
|
Room\_Number | Integer(1) | `ROOM`| |
|
557 |
|
Floor | Integer (1) | `ROOM` | |
|
558 |
|
Prof\_Name | Character (30) | `PROF` | |
|
559 |
|
Phone | Integer (10) | `PROF` | |
|
560 |
|
Email | Character (30) | `PROF`| |
|
561 |
|
Room\_Number | Integer (1) | `PROF`| |
503 |
562 |
|
|
504 |
563 |
@problem:campus -- Solution to Q. -.# |
@problem:campus -- Solution to Q. -.# |
505 |
564 |
~ |
~ |
|
... |
... |
Room\_Number | Integer (1) | `PROF` |
532 |
591 |
#. No, we could not get the office hours of a professor. |
#. No, we could not get the office hours of a professor. |
533 |
592 |
#. Yes, we could list all the professors whose offices are in the same building. |
#. Yes, we could list all the professors whose offices are in the same building. |
534 |
593 |
#. Yes, we could list all the rooms. |
#. Yes, we could list all the rooms. |
535 |
|
#. If a new professor arrives, and has to share his office with another professor, we would not have to revise our database catalog (it is fine for two professor to have the same room number, in our model). |
|
|
594 |
|
#. If a new professor arrives, and has to share his office with another professor, we would not have to revise our database catalog (it is fine for two professor to have the same room number, in our model). |
536 |
595 |
#. Yes, we could list which professors are at the same floor. |
#. Yes, we could list which professors are at the same floor. |
537 |
596 |
#. No, we could not tell which professor has the highest evaluations. |
#. No, we could not tell which professor has the highest evaluations. |
538 |
597 |
|
|
|
... |
... |
Exercise +.# |
850 |
909 |
|
|
851 |
910 |
Assuming that this is all the data we will ever have, discuss whenever $\{A, B, C, D\}$, $\{A, B\}$ and $\{B\}$ are superkeys and/or keys. |
Assuming that this is all the data we will ever have, discuss whenever $\{A, B, C, D\}$, $\{A, B\}$ and $\{B\}$ are superkeys and/or keys. |
852 |
911 |
|
|
|
912 |
|
Exercise +.# |
|
913 |
|
~ Consider the following relation and possible state. Assuming that this is all the data we will ever have, give two superkeys, and one key, for this relation. |
|
914 |
|
|
|
915 |
|
<!-- Bug with table --> |
|
916 |
|
**A** | **B** | **C** | **D** | |
|
917 |
|
--- | --- | --- | --- | |
|
918 |
|
1 | A | Austin | `true`| |
|
919 |
|
2 | B | Paris | `true` | |
|
920 |
|
1 | C | Pisa | `false`| |
|
921 |
|
2 | C | Augusta | `true`| |
|
922 |
|
1 | B | Augusta | `true`| |
|
923 |
|
|
|
924 |
|
Exercise +.# |
|
925 |
|
~ Consider the following two relations: |
|
926 |
|
|
|
927 |
|
![ |
|
928 |
|
BUILDING(Name (PK), Address) |
|
929 |
|
ROOM(Code (PK), Building (FK to BUILDING.Name)) |
|
930 |
|
](fig/rel_mod/BUILDING_ROOM) |
|
931 |
|
\ |
|
932 |
|
|
|
933 |
|
#. Give two possible tuples for the BUILDING relation, and two possible tuples for the ROOM relation. |
|
934 |
|
#. Based on the data you gave previously, write (in pseudo-code) one `INSERT` and one `UPDATE` instruction that would violate the integrity of your database. |
|
935 |
|
|
|
936 |
|
|
853 |
937 |
## Solution to Exercises {-} |
## Solution to Exercises {-} |
854 |
938 |
|
|
855 |
939 |
Solution +.# |
Solution +.# |
|
... |
... |
Solution +.# |
892 |
976 |
|
|
893 |
977 |
Solution +.# |
Solution +.# |
894 |
978 |
|
|
895 |
|
: Then we know that $A_2$ is the primary key of $R_2$. |
|
|
979 |
|
: Then we know that $A_2$ is the primary key of $R_2$, and that $A_1$ and $A_2$ have the same domain. |
896 |
980 |
|
|
897 |
981 |
Solution +.# |
Solution +.# |
898 |
982 |
|
|
899 |
983 |
: Reading from the database, performing `UPDATE`{.sqlmysql} or `DELETE`{.sqlmysql} operations. |
: Reading from the database, performing `UPDATE`{.sqlmysql} or `DELETE`{.sqlmysql} operations. |
900 |
984 |
|
|
|
985 |
|
Solution +.# |
|
986 |
|
|
|
987 |
|
: An operation is an "atomic action" that can be performed on the database (adding an element, updating a value, removing an element, etc.). A transaction is a series of such operations, and the assumption is that, even if it can be made of operations that, taken individually, could violate a constraint, the overall transaction will leave the database in a consistent state. |
|
988 |
|
|
901 |
989 |
Solution +.# |
Solution +.# |
902 |
990 |
~ |
~ |
903 |
991 |
|
|
904 |
992 |
a) COMPUTER has for arity $4$, and OS has for arity $3$. |
a) COMPUTER has for arity $4$, and OS has for arity $3$. |
905 |
993 |
b) ("Linda McFather", 32, 2017, "Purism"), and ("Debian", "Stable", "amd64") |
b) ("Linda McFather", 32, 2017, "Purism"), and ("Debian", "Stable", "amd64") |
906 |
994 |
|
|
907 |
|
|
|
908 |
|
Solution +.# |
|
909 |
|
|
|
910 |
|
: An operation is an "atomic action" that can be performed on the database (adding an element, updating a value, removing an element, etc.). A transaction is a series of such operations, and the assumption is that, even if it can be made of operations that, taken individually, could violate a constraint, an operation will leave the database in a consistent state. |
|
911 |
|
|
|
912 |
995 |
Solution +.# |
Solution +.# |
913 |
996 |
|
|
914 |
997 |
: An operation whose execution in isolation would result in the violation of a constraint can either a) be "restricted" (i.e., not executed), b) result in a propagation (i.e., the tuples that would violate a constraint are updated or deleted accordingly), or c) result in some values in tuples that would violate a constraint to be set to a default value, or the `NULL` value (this last option works only if the constraint violated is the referential entity constraint). |
: An operation whose execution in isolation would result in the violation of a constraint can either a) be "restricted" (i.e., not executed), b) result in a propagation (i.e., the tuples that would violate a constraint are updated or deleted accordingly), or c) result in some values in tuples that would violate a constraint to be set to a default value, or the `NULL` value (this last option works only if the constraint violated is the referential entity constraint). |
|
... |
... |
Solution +.# |
948 |
1031 |
- $\{A, B\}$ is a superkey **and** a key, as neither $\{A\}$ nor $\{B\}$ are keys. |
- $\{A, B\}$ is a superkey **and** a key, as neither $\{A\}$ nor $\{B\}$ are keys. |
949 |
1032 |
- $\{A\}$ is not a key, and not a superkey: multiple tuples have the value $1$. |
- $\{A\}$ is not a key, and not a superkey: multiple tuples have the value $1$. |
950 |
1033 |
|
|
|
1034 |
|
Solution +.# |
|
1035 |
|
~ Possible superkeys are $\{A, B, C, D\}$, $\{A, B, C\}$, $\{A, C, D\}$, $\{B, C, D\}$, $\{A, B\}$, $\{B, C\}$, . The possible keys are $\{A, B\}$ $\{A, C\}$, and $\{B, C\}$. |
|
1036 |
|
|
|
1037 |
|
Solution +.# |
|
1038 |
|
~ |
|
1039 |
|
|
|
1040 |
|
#. For the BUILDING relation: <"A.H", "123 Main St.">, <"U.H.", "123 Main St.">. For the ROOM relation: <12, "A.H.">, <15, "A.H.">. |
|
1041 |
|
#. `INSERT <"A.H.", NULL>` would violate the requirement not to have two tuples with the same value for the primary key in the BUILDING relation. `UPDATE ROOM with CODE = 12 to Building = "G.C.C."` would create an entry referencing a name in the BUILDING relation that does not exist. |
|
1042 |
|
|
951 |
1043 |
## Problems {-} |
## Problems {-} |
952 |
1044 |
|
|
953 |
1045 |
Problem (Finding candidate key in a CLASS relation) +.#candidatekeyinclass |
Problem (Finding candidate key in a CLASS relation) +.#candidatekeyinclass |
|
... |
... |
Problem (Relational model for bills) +.#rel_model_bills |
1007 |
1099 |
|
|
1008 |
1100 |
Solution to @problem:cinema |
Solution to @problem:cinema |
1009 |
1101 |
|
|
1010 |
|
~ |
|
|
1102 |
|
~ A possible solution is: |
1011 |
1103 |
|
|
1012 |
1104 |
![STAR(Id (PK), Name, BirthDate) |
![STAR(Id (PK), Name, BirthDate) |
1013 |
1105 |
MOVIE(Id (PK), Title, Year, Length, Genre) |
MOVIE(Id (PK), Title, Year, Length, Genre) |