File notes/lectures_notes.md changed (mode: 100644) (index 327de2e..fed0c2c) |
... |
... |
Those are part of the definition of the relational model and are independent of |
848 |
848 |
Those constraints are parts of the schema. |
Those constraints are parts of the schema. |
849 |
849 |
|
|
850 |
850 |
- The value must match its domain ("Domain constraint"), knowing that a domain can have additional constraints (`NOT NULL`, `UNIQUE`). |
- The value must match its domain ("Domain constraint"), knowing that a domain can have additional constraints (`NOT NULL`, `UNIQUE`). |
851 |
|
- The entity integrity constraint: no primary key value can be `NULL`. |
|
|
851 |
|
- The entity integrity constraint: no primary key value can be `NULL`^[This is also the way this is implemented in MySQL: no part of the primary key can have for value `NULL`. Cf. the ["Declaring Constraints"](#declaring-constraints-1) Section.]. |
852 |
852 |
- The referential integrity constraint: referred values must exist. |
- The referential integrity constraint: referred values must exist. |
853 |
853 |
|
|
854 |
854 |
Those last two constraints will be studied in the next section. |
Those last two constraints will be studied in the next section. |
|
... |
... |
and the following sets of attributes: |
885 |
885 |
| | \{A, B, C, D\} | \{A\} | \{B, C\} | \{D\} | |
| | \{A, B, C, D\} | \{A\} | \{B, C\} | \{D\} | |
886 |
886 |
| ---: | :---: | :---: | :---: | :---: | |
| ---: | :---: | :---: | :---: | :---: | |
887 |
887 |
| Superkey ? | ✔ | ✘ | ✔ | ✔ | |
| Superkey ? | ✔ | ✘ | ✔ | ✔ | |
888 |
|
| Key ?| ✘ | ✘ | ✔ | ✔ | |
|
|
888 |
|
| Key ?| ✘ | ✘ | ✘ | ✔ | |
889 |
889 |
|
|
890 |
890 |
Note that here we "retro-fit" those definitions, in database design, they come first (i.e., you define what attributes should always distinguish between tuples before populating your database). |
Note that here we "retro-fit" those definitions, in database design, they come first (i.e., you define what attributes should always distinguish between tuples before populating your database). |
891 |
891 |
We are making the assumption that the data pre-exist to the specification to make the concept clearer. |
We are making the assumption that the data pre-exist to the specification to make the concept clearer. |
|
... |
... |
If both elements were common, that would mean that we are registering a driver t |
929 |
929 |
|
|
930 |
930 |
The operations you can perform on your data are of two kinds: retrievals and updates. |
The operations you can perform on your data are of two kinds: retrievals and updates. |
931 |
931 |
|
|
932 |
|
- Retrievals leave the relation state as it is and output a result relation. That is, retrieval: relation state → result relation |
|
933 |
|
- Updates change the relation state. That is, update: relation state → relation state |
|
|
932 |
|
- Retrievals leave the relation(s) state as it is and output a result relation. That is, retrieval: relation(s) state → result relation |
|
933 |
|
- Updates change the relation(s) state. That is, update: relation(s) state → relation(s) state |
934 |
934 |
|
|
935 |
935 |
They are two constraints for updates: |
They are two constraints for updates: |
936 |
936 |
|
|
937 |
|
#. The new relation state must be "valid" (i.e., comply with the state constraints). |
|
|
937 |
|
#. The new relation state must be "valid" (i.e., comply with the constraints). |
938 |
938 |
#. There might be transition constraints (your balance cannot become negative, for instance). |
#. There might be transition constraints (your balance cannot become negative, for instance). |
939 |
939 |
|
|
940 |
940 |
A transaction is a series of retrievals and updates performed by an application program, that leaves the database in a consistent state. |
A transaction is a series of retrievals and updates performed by an application program, that leaves the database in a consistent state. |
|
... |
... |
Go back to the CAR example and populate it with some data to "see" how those opt |
989 |
989 |
|
|
990 |
990 |
## Exercises {-} |
## Exercises {-} |
991 |
991 |
|
|
|
992 |
|
|
992 |
993 |
Exercise .+# |
Exercise .+# |
993 |
994 |
|
|
994 |
995 |
: What are the meta-data and the data called in the relational model? |
: What are the meta-data and the data called in the relational model? |
|
... |
... |
Exercise +.# |
1068 |
1069 |
|
|
1069 |
1070 |
: Define what is the domain constraint. |
: Define what is the domain constraint. |
1070 |
1071 |
|
|
|
1072 |
|
Exercise +.# |
|
1073 |
|
|
|
1074 |
|
: Circle the _correct_ statements: |
|
1075 |
|
|
|
1076 |
|
- Every key is a superkey. |
|
1077 |
|
- Every superkey is a singleton. |
|
1078 |
|
- Every singleton is either a superkey, or a key. |
|
1079 |
|
- Every primary key is a key. |
|
1080 |
|
- Every superkey with one element is a key. |
|
1081 |
|
|
1071 |
1082 |
Exercise +.# |
Exercise +.# |
1072 |
1083 |
~ |
~ |
1073 |
1084 |
|
|
|
... |
... |
Solution +.# |
1233 |
1244 |
|
|
1234 |
1245 |
: The requirement that each tuple must have for an attribute $A$ an atomic value from the domain dom($A$), or `NULL`. |
: The requirement that each tuple must have for an attribute $A$ an atomic value from the domain dom($A$), or `NULL`. |
1235 |
1246 |
|
|
|
1247 |
|
Solution +.# |
|
1248 |
|
|
|
1249 |
|
: "Every key is a superkey.", "Every primary key is a key." and "Every superkey with one element is a key." are correct statements. |
|
1250 |
|
|
1236 |
1251 |
Solution +.# |
Solution +.# |
1237 |
1252 |
~ |
~ |
1238 |
1253 |
|
|
|
... |
... |
TOY (Id (PK), Location, Description, Name, BestSuited (FK to TYPE.Name)) |
1487 |
1502 |
|
|
1488 |
1503 |
- [@Textbook6, ch. 4--5], [@Textbook7, ch. 6--7] describes `SQL`, but none of its implementation. |
- [@Textbook6, ch. 4--5], [@Textbook7, ch. 6--7] describes `SQL`, but none of its implementation. |
1489 |
1504 |
- To compare DBMS, you can look at their features, at <https://en.wikipedia.org/wiki/Comparison_of_relational_database_management_systems>, and at their popularity at <https://db-engines.com/en/ranking> and <https://insights.stackoverflow.com/survey/2020/#technology-databases> (if you sum up MySQL and MariaDB, they are first in both). |
- To compare DBMS, you can look at their features, at <https://en.wikipedia.org/wiki/Comparison_of_relational_database_management_systems>, and at their popularity at <https://db-engines.com/en/ranking> and <https://insights.stackoverflow.com/survey/2020/#technology-databases> (if you sum up MySQL and MariaDB, they are first in both). |
1490 |
|
- MySQL and MariaDB have some differences, you can look them up at < https://mariadb.com/kb/en/mariadb-vs-mysql-features/> and <https://mariadb.com/kb/en/mariadb-vs-mysql-compatibility/>. |
|
|
1505 |
|
- MySQL and MariaDB have some differences, you can look them up at <https://mariadb.com/kb/en/mariadb-vs-mysql-features/> and <https://mariadb.com/kb/en/mariadb-vs-mysql-compatibility/>. |
1491 |
1506 |
|
|
1492 |
1507 |
This chapter will be "code-driven": the code will illustrate and help you understand some concepts. |
This chapter will be "code-driven": the code will illustrate and help you understand some concepts. |
1493 |
1508 |
You may want to have a look at the ["Setting Up Your Work Environment"](#sec:setup) Section as early as possible in this lecture. |
You may want to have a look at the ["Setting Up Your Work Environment"](#sec:setup) Section as early as possible in this lecture. |
|
... |
... |
The following is an adaptation of [w3resource.com](https://www.w3resource.com/my |
1567 |
1582 |
- For monetary amounts, [it is recommended](https://stackoverflow.com/a/4397416) to use `DECIMAL(10, 2)` (or its synonym in MySQL `NUMERIC`). |
- For monetary amounts, [it is recommended](https://stackoverflow.com/a/4397416) to use `DECIMAL(10, 2)` (or its synonym in MySQL `NUMERIC`). |
1568 |
1583 |
- Characters can be stored using `CHAR` and `VARCHAR`: the length (resp. maximal length) of the `CHAR` (resp. `VARCHAR`) has to be declared, and `CHAR` are right-padded with spaces to the specified length. |
- Characters can be stored using `CHAR` and `VARCHAR`: the length (resp. maximal length) of the `CHAR` (resp. `VARCHAR`) has to be declared, and `CHAR` are right-padded with spaces to the specified length. |
1569 |
1584 |
Historically, [255](https://stackoverflow.com/q/1217466) was the size used, because it is the largest number of characters that can be counted with an 8-bit number, but, whenever possible, the "[right size](https://dba.stackexchange.com/a/183277)" should be used. |
Historically, [255](https://stackoverflow.com/q/1217466) was the size used, because it is the largest number of characters that can be counted with an 8-bit number, but, whenever possible, the "[right size](https://dba.stackexchange.com/a/183277)" should be used. |
1570 |
|
- You can store a single bit using `BIT(1)`, and a boolean using `BOOLEAN`. |
|
1571 |
|
- For date and time types, you can use `DATE`, `TIME`, `DATETIME` and `TIMESTAMP`. |
|
|
1585 |
|
- You can store a single bit using `BIT(1)`, and a boolean using `BOOLEAN` (or `BOOL`, both actually being aliases for `TINYINT(1)`). |
|
1586 |
|
- For date and time types, you can use `DATE`, `TIME`, `DATETIME` and `TIMESTAMP` (which convert the current day / time to from the current time zone to UTC). |
1572 |
1587 |
|
|
1573 |
1588 |
There are many other datatypes, but they really depends on the particular implementation, so we will not consider them too much. |
There are many other datatypes, but they really depends on the particular implementation, so we will not consider them too much. |
1574 |
1589 |
|
|
|
... |
... |
Note that |
2450 |
2465 |
- The part `IS NOT NULL` is needed: otherwise, if one of the grade is `NULL`, then the comparison would yelds "unknown", and no grade would be greater than all of the others. |
- The part `IS NOT NULL` is needed: otherwise, if one of the grade is `NULL`, then the comparison would yelds "unknown", and no grade would be greater than all of the others. |
2451 |
2466 |
- This query could be simplified, using `MAX`: |
- This query could be simplified, using `MAX`: |
2452 |
2467 |
|
|
2453 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ProfExample.sql snippet=max} |
|
2454 |
|
``` |
|
|
2468 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
2469 |
|
|
|
2470 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ProfExample.sql snippet=max} |
|
2471 |
|
``` |
2455 |
2472 |
|
|
2456 |
2473 |
Answering the question |
Answering the question |
2457 |
2474 |
|
|
|
... |
... |
Bye |
2751 |
2768 |
|
|
2752 |
2769 |
We now log in as the normal user called "testuser". |
We now log in as the normal user called "testuser". |
2753 |
2770 |
|
|
2754 |
|
Linux users should type *as a normal user, i.e., not as root*, in their terminal the following, and Windows users should type in their command prompt the following^[Provided the working directory is still `C:\Program Files\MySQL\MySQL Server 8.0\bin` or similar. Cf. <https://dev.mysql.com/doc/mysql-windows-excerpt/8.0/en/mysql-installation-windows-path.html> to add the MySQL bin directory to your Windows system `PATH` environment variable.]: |
|
|
2771 |
|
Linux users should type *as a normal user, i.e., not as root*, in their terminal the following, and Windows users should type in their command prompt the following^[Provided the working directory is still `C:\Program Files\MySQL\MySQL Server 8.0\bin` or similar. Cf. <https://dev.mysql.com/doc/mysql-windows-excerpt/8.0/en/mysql-installation-windows-path.html> to add the MySQL bin directory to your Windows system `PATH` environment variable. For MacOS user, something like `sudo sh -c 'echo /usr/local/mysql/bin > /etc/paths.d/mysql'` should do.]: |
2755 |
2772 |
|
|
2756 |
2773 |
```{.bash} |
```{.bash} |
2757 |
2774 |
mysql -u testuser -p |
mysql -u testuser -p |
|
... |
... |
Solution +.# |
3205 |
3222 |
~ |
~ |
3206 |
3223 |
A simple and compact code could be: |
A simple and compact code could be: |
3207 |
3224 |
|
|
3208 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Short.sql snippet=solution} |
|
3209 |
|
``` |
|
|
3225 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
3226 |
|
|
|
3227 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Short.sql snippet=solution} |
|
3228 |
|
``` |
3210 |
3229 |
|
|
3211 |
3230 |
Solution +.# |
Solution +.# |
3212 |
3231 |
|
|
|
... |
... |
Problem (Constraints on foreign keys) +.#fk |
3580 |
3599 |
|
|
3581 |
3600 |
But, the situation is slightly more complex. |
But, the situation is slightly more complex. |
3582 |
3601 |
Test for yourself by editing the following code as indicated: |
Test for yourself by editing the following code as indicated: |
|
3602 |
|
|
|
3603 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
3583 |
3604 |
|
|
3584 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_FKtest.sql} |
|
3585 |
|
``` |
|
|
3605 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_FKtest.sql} |
|
3606 |
|
``` |
3586 |
3607 |
|
|
3587 |
|
#. Remove the `PRIMARY KEY` constraint. |
|
3588 |
|
#. Replace `PRIMARY KEY` with `UNIQUE`. |
|
3589 |
|
#. Replace one of the `VARCHAR(25)` with `CHAR(25)`. |
|
3590 |
|
#. Replace one of the `VARCHAR(25)` with `INT`. |
|
3591 |
|
#. Replace one of the `VARCHAR(25)` with `VARCHAR(15)` |
|
3592 |
|
#. Once you have edited and run the program in all of its modified versions, adjust the remarks above to better reflect the reality of the implementation we are using. |
|
|
3608 |
|
#. Remove the `PRIMARY KEY` constraint. |
|
3609 |
|
#. Replace `PRIMARY KEY` with `UNIQUE`. |
|
3610 |
|
#. Replace one of the `VARCHAR(25)` with `CHAR(25)`. |
|
3611 |
|
#. Replace one of the `VARCHAR(25)` with `INT`. |
|
3612 |
|
#. Replace one of the `VARCHAR(25)` with `VARCHAR(15)` |
|
3613 |
|
#. Once you have edited and run the program in all of its modified versions, adjust the remarks above to better reflect the reality of the implementation we are using. |
3593 |
3614 |
|
|
3594 |
3615 |
|
|
3595 |
3616 |
--- |
--- |
|
... |
... |
Problem (Revisiting the PROF table) +.#profrevisited |
3611 |
3632 |
|
|
3612 |
3633 |
The code we studied during the lecture is more or less the following. |
The code we studied during the lecture is more or less the following. |
3613 |
3634 |
|
|
3614 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ProfExampleRevisitedRevisited.sql snippet=recreate} |
|
3615 |
|
``` |
|
3616 |
|
|
|
3617 |
|
We will resume working on this model, and enhance it. |
|
|
3635 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
3618 |
3636 |
|
|
3619 |
|
@problem:profrevisited -- Question -.# |
|
3620 |
|
|
|
3621 |
|
: Draw the *complete* relational model for this database (i.e., for the `PROF`, `DEPARTMENT`, `STUDENT` and `GRADE` relations). |
|
|
3637 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ProfExampleRevisitedRevisited.sql snippet=recreate} |
|
3638 |
|
``` |
3622 |
3639 |
|
|
3623 |
|
@problem:profrevisited -- Question -.# |
|
3624 |
|
~ |
|
3625 |
|
|
|
3626 |
|
Create and populate a `LECTURE` table as follows: |
|
|
3640 |
|
We will resume working on this model, and enhance it. |
3627 |
3641 |
|
|
3628 |
|
- It should have four attributes: `Name`, `Instructor`, `Code`, and `Year`, of types `VARCHAR(25)` for the first two, `CHAR(5)` for `Code`, and `YEAR(4)` for `Year`. |
|
3629 |
|
- The `Year` and `Code` attributes should be the primary key (yes, have *two* attributes be the primary key). |
|
3630 |
|
- The `Instructor` attribute should be a foreign key referencing the `Login` attribute in `PROF`. |
|
3631 |
|
- Populate the `LECTURE` table with some made-up data. |
|
|
3642 |
|
@problem:profrevisited -- Question -.# |
3632 |
3643 |
|
|
3633 |
|
Try to think about some of the weakenesses of this representation. For instance, can it accomodate two instructors for the same class? Write down two possibles scenarios in which this schema would not be appropriate. |
|
|
3644 |
|
: Draw the *complete* relational model for this database (i.e., for the `PROF`, `DEPARTMENT`, `STUDENT` and `GRADE` relations). |
3634 |
3645 |
|
|
3635 |
|
@problem:profrevisited -- Question -.# |
|
3636 |
|
~ |
|
3637 |
|
The `GRADE` table had some limitations too. For example, every student could have only one grade. Add two columns to the `GRADE` table using: |
|
|
3646 |
|
@problem:profrevisited -- Question -.# |
|
3647 |
|
~ |
3638 |
3648 |
|
|
3639 |
|
``` |
|
3640 |
|
ALTER TABLE GRADE |
|
3641 |
|
ADD COLUMN LectureCode CHAR(5), |
|
3642 |
|
ADD COLUMN LectureYear YEAR(4); |
|
3643 |
|
``` |
|
3644 |
|
|
|
3645 |
|
Add a foreign key: |
|
3646 |
|
|
|
3647 |
|
``` |
|
3648 |
|
ALTER TABLE GRADE |
|
3649 |
|
ADD FOREIGN KEY (LectureYear, LectureCode) |
|
3650 |
|
REFERENCES LECTURE(Year, Code); |
|
3651 |
|
``` |
|
3652 |
|
|
|
3653 |
|
Use `DESCRIBE` and `SELECT` to observe the schema of the `GRADE` table and its rows. Is it what you would have expected? |
|
|
3649 |
|
Create and populate a `LECTURE` table as follows: |
3654 |
3650 |
|
|
3655 |
|
@problem:profrevisited -- Question -.# |
|
3656 |
|
: Update the tuples in `GRADE` with some made-up data. Now every row should contain, in addition to a login and a grade, a lecture year and a lecture code. |
|
|
3651 |
|
- It should have four attributes: `Name`, `Instructor`, `Code`, and `Year`, of types `VARCHAR(25)` for the first two, `CHAR(5)` for `Code`, and `YEAR(4)` for `Year`. |
|
3652 |
|
- The `Year` and `Code` attributes should be the primary key (yes, have *two* attributes be the primary key). |
|
3653 |
|
- The `Instructor` attribute should be a foreign key referencing the `Login` attribute in `PROF`. |
|
3654 |
|
- Populate the `LECTURE` table with some made-up data. |
3657 |
3655 |
|
|
3658 |
|
@problem:profrevisited -- Question -.# |
|
3659 |
|
: Update the relational model you previously drew to reflect the new situation of your tables. |
|
|
3656 |
|
Try to think about some of the weakenesses of this representation. For instance, can it accomodate two instructors for the same class? Write down two possibles scenarios in which this schema would not be appropriate. |
3660 |
3657 |
|
|
3661 |
|
@problem:profrevisited -- Question -.# |
|
3662 |
|
~ |
|
3663 |
|
|
|
3664 |
|
Write `SELECT` statements answering the following questions (where `PROF.Name`, `LECTURE.Name`, `YYYY`, `LECTURE.Code` and `STUDENT.Login` should be relevant values considering your data): |
|
3665 |
|
|
|
3666 |
|
#. "Could you give me the logins and grades of the students who took `LECTURE.Name` in `YYYY`?" |
|
3667 |
|
#. "Could you list the instructors who taught in year `YYYY` without any duplicates?" |
|
3668 |
|
#. "Could you list the name and grade of all the student who ever took the class `LECTURE.Code`?" |
|
3669 |
|
#. "Could you tell me which years was the class `LECTURE.Code` taught?" |
|
3670 |
|
#. "Could you list the other classes taught the same year as the class `LECTURE.Code`?" |
|
3671 |
|
#. "Could you print the names of the students who registered after `STUDENT.Login`?" |
|
3672 |
|
#. "Could you tell me how many departments' heads are teaching this year?" |
|
|
3658 |
|
@problem:profrevisited -- Question -.# |
|
3659 |
|
~ |
|
3660 |
|
The `GRADE` table had some limitations too. For example, every student could have only one grade. Add two columns to the `GRADE` table using: |
|
3661 |
|
|
|
3662 |
|
``` |
|
3663 |
|
ALTER TABLE GRADE |
|
3664 |
|
ADD COLUMN LectureCode CHAR(5), |
|
3665 |
|
ADD COLUMN LectureYear YEAR(4); |
|
3666 |
|
``` |
|
3667 |
|
|
|
3668 |
|
Add a foreign key: |
|
3669 |
|
|
|
3670 |
|
``` |
|
3671 |
|
ALTER TABLE GRADE |
|
3672 |
|
ADD FOREIGN KEY (LectureYear, LectureCode) |
|
3673 |
|
REFERENCES LECTURE(Year, Code); |
|
3674 |
|
``` |
|
3675 |
|
|
|
3676 |
|
Use `DESCRIBE` and `SELECT` to observe the schema of the `GRADE` table and its rows. Is it what you would have expected? |
|
3677 |
|
|
|
3678 |
|
@problem:profrevisited -- Question -.# |
|
3679 |
|
: Update the tuples in `GRADE` with some made-up data. Now every row should contain, in addition to a login and a grade, a lecture year and a lecture code. |
|
3680 |
|
|
|
3681 |
|
@problem:profrevisited -- Question -.# |
|
3682 |
|
: Update the relational model you previously drew to reflect the new situation of your tables. |
|
3683 |
|
|
|
3684 |
|
@problem:profrevisited -- Question -.# |
|
3685 |
|
~ |
|
3686 |
|
|
|
3687 |
|
Write `SELECT` statements answering the following questions (where `PROF.Name`, `LECTURE.Name`, `YYYY`, `LECTURE.Code` and `STUDENT.Login` should be relevant values considering your data): |
|
3688 |
|
|
|
3689 |
|
#. "Could you give me the logins and grades of the students who took `LECTURE.Name` in `YYYY`?" |
|
3690 |
|
#. "Could you list the instructors who taught in year `YYYY` without any duplicates?" |
|
3691 |
|
#. "Could you list the name and grade of all the student who ever took the class `LECTURE.Code`?" |
|
3692 |
|
#. "Could you tell me which years was the class `LECTURE.Code` taught?" |
|
3693 |
|
#. "Could you list the other classes taught the same year as the class `LECTURE.Code`?" |
|
3694 |
|
#. "Could you print the names of the students who registered after `STUDENT.Login`?" |
|
3695 |
|
#. "Could you tell me how many departments' heads are teaching this year?" |
3673 |
3696 |
|
|
3674 |
3697 |
--- |
--- |
3675 |
3698 |
|
|
|
... |
... |
Problem (TRAIN table and more advanced `SQL` coding) +.#train |
3678 |
3701 |
|
|
3679 |
3702 |
Look at the `SQL` code below and then answer the following questions. |
Look at the `SQL` code below and then answer the following questions. |
3680 |
3703 |
|
|
|
3704 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
3705 |
|
|
3681 |
3706 |
|
|
3682 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Train.sql snippet=set-up} |
|
3683 |
|
``` |
|
|
3707 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Train.sql snippet=set-up} |
|
3708 |
|
``` |
3684 |
3709 |
|
|
3685 |
|
@problem:train -- Question -.# |
|
|
3710 |
|
@problem:train -- Question -.# |
3686 |
3711 |
|
|
3687 |
|
: Modify the `CREATE` statement that creates the `TRAIN` table (lines 1--5), so that `ID` would be declared as the primary key. It is sufficient to only write the line(s) that need to change. |
|
|
3712 |
|
: Modify the `CREATE` statement that creates the `TRAIN` table (lines 1--5), so that `ID` would be declared as the primary key. It is sufficient to only write the line(s) that need to change. |
3688 |
3713 |
|
|
3689 |
|
@problem:train -- Question -.# |
|
|
3714 |
|
@problem:train -- Question -.# |
3690 |
3715 |
|
|
3691 |
|
: Write an `ALTER` statement that makes `ID` become the primary key of the `CONDUCTOR` table. |
|
|
3716 |
|
: Write an `ALTER` statement that makes `ID` become the primary key of the `CONDUCTOR` table. |
3692 |
3717 |
|
|
3693 |
|
@problem:train -- Question -.# |
|
|
3718 |
|
@problem:train -- Question -.# |
3694 |
3719 |
|
|
3695 |
|
: Modify the `CREATE` statement that creates the `ASSIGNED_TO` table (lines 13--18), so that it has two foreign keys: `ConductorId` references the `ID` attribute in `CONDUCTOR` and `TrainId` references the `ID` attribute in `TRAIN`. It is sufficient to only write the line(s) that need to change. |
|
|
3720 |
|
: Modify the `CREATE` statement that creates the `ASSIGNED_TO` table (lines 13--18), so that it has two foreign keys: `ConductorId` references the `ID` attribute in `CONDUCTOR` and `TrainId` references the `ID` attribute in `TRAIN`. It is sufficient to only write the line(s) that need to change. |
3696 |
3721 |
|
|
3697 |
|
@problem:train -- Question -.# |
|
|
3722 |
|
@problem:train -- Question -.# |
3698 |
3723 |
|
|
3699 |
|
: Write `INSERT` statements that insert one tuple of your choosing in each relation (no `NULL` values). |
|
3700 |
|
These statements should respect all the constraints (including the ones we added in the previous questions) and result in actual insertions. (Remember that four digits is a valid value for an attribute with the `YEAR(4)` datatype.) |
|
|
3724 |
|
: Write `INSERT` statements that insert one tuple of your choosing in each relation (no `NULL` values). |
|
3725 |
|
These statements should respect all the constraints (including the ones we added in the previous questions) and result in actual insertions. (Remember that four digits is a valid value for an attribute with the `YEAR(4)` datatype.) |
3701 |
3726 |
|
|
3702 |
|
@problem:train -- Question -.# |
|
|
3727 |
|
@problem:train -- Question -.# |
|
3728 |
|
|
|
3729 |
|
: 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. |
|
3730 |
|
|
|
3731 |
|
@problem:train -- Question -.# |
|
3732 |
|
~ |
|
3733 |
|
Write a `SELECT` statement that answers each of the following questions: |
3703 |
3734 |
|
|
3704 |
|
: 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. |
|
|
3735 |
|
#. "What are the identification numbers of the trains?" |
|
3736 |
|
#. "What are the names of the conductors with a "Senior" experience level?" |
|
3737 |
|
#. "What are the construction years of the "Surfliner" and "Regina" models that we have?" |
|
3738 |
|
#. "What is the ID of the conductor that was responsible of the train referenced "K-13" on 2015/12/14?" |
|
3739 |
|
#. "What are the models that were ever conducted by the conductor whose ID is "GP1029"?" |
3705 |
3740 |
|
|
3706 |
|
@problem:train -- Question -.# |
|
3707 |
|
~ |
|
3708 |
|
Write a `SELECT` statement that answers each of the following questions: |
|
3709 |
|
|
|
3710 |
|
#. "What are the identification numbers of the trains?" |
|
3711 |
|
#. "What are the names of the conductors with a "Senior" experience level?" |
|
3712 |
|
#. "What are the construction years of the "Surfliner" and "Regina" models that we have?" |
|
3713 |
|
#. "What is the ID of the conductor that was responsible of the train referenced "K-13" on 2015/12/14?" |
|
3714 |
|
#. "What are the models that were ever conducted by the conductor whose ID is "GP1029"?" |
|
3715 |
|
|
|
3716 |
3741 |
--- |
--- |
3717 |
3742 |
|
|
3718 |
3743 |
Problem (Read, correct, and write `SQL` statements for the COFFEE database) +.#coffee |
Problem (Read, correct, and write `SQL` statements for the COFFEE database) +.#coffee |
|
... |
... |
Problem (Write select queries for the DEPARTMENT table) +.#DepartmentSelect |
3843 |
3868 |
~ |
~ |
3844 |
3869 |
|
|
3845 |
3870 |
Consider the following `SQL` code: |
Consider the following `SQL` code: |
3846 |
|
|
|
3847 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Department.sql snippet=statement} |
|
3848 |
|
``` |
|
3849 |
|
|
|
3850 |
|
Write queries that return the following information. The values returned *in this set-up* will be in parenthesis, but keep the queries general. |
|
3851 |
3871 |
|
|
3852 |
|
#. The name of the employees working in the `Storage` department (`"Bob"`, `"Samantha"`, `"Karen"` and `"Jocelyn"`), |
|
3853 |
|
#. The name of the employee that has been hired for the longest period of time (`"Mark"`), |
|
3854 |
|
#. The name(s) of the employee(s) from the `Storage` department who has(have) been hired for the longest period of time. Phrased differently, the oldest employees of the `Storage` department (`"Bob"` and `"Jocelyn"`). |
|
|
3872 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
3873 |
|
|
|
3874 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Department.sql snippet=statement} |
|
3875 |
|
``` |
|
3876 |
|
|
|
3877 |
|
Write queries that return the following information. The values returned *in this set-up* will be in parenthesis, but keep the queries general. |
|
3878 |
|
|
|
3879 |
|
#. The name of the employees working in the `Storage` department (`"Bob"`, `"Samantha"`, `"Karen"` and `"Jocelyn"`), |
|
3880 |
|
#. The name of the employee that has been hired for the longest period of time (`"Mark"`), |
|
3881 |
|
#. The name(s) of the employee(s) from the `Storage` department who has(have) been hired for the longest period of time. Phrased differently, the oldest employees of the `Storage` department (`"Bob"` and `"Jocelyn"`). |
3855 |
3882 |
|
|
3856 |
3883 |
--- |
--- |
3857 |
3884 |
|
|
|
... |
... |
Problem (Write select queries for the COMPUTER table) +.#ComputerSelect |
3861 |
3888 |
|
|
3862 |
3889 |
Consider the following `SQL` code: |
Consider the following `SQL` code: |
3863 |
3890 |
|
|
3864 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Computer.sql} |
|
3865 |
|
``` |
|
3866 |
|
|
|
3867 |
|
Write queries that return the following information. The values returned *in this set-up* will be in parenthesis, but keep the queries general. |
|
|
3891 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
3892 |
|
|
|
3893 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Computer.sql} |
|
3894 |
|
``` |
3868 |
3895 |
|
|
3869 |
|
#. The number of computers connected to the printer whose `ID` is `'13'` (`2`). |
|
3870 |
|
#. The number of different models of printers (`2`). |
|
3871 |
|
#. The model(s) of the printer(s) connected to the computer whose `ID` is `'A'` (`'HP-140'` and `'HP-139'`). |
|
3872 |
|
#. The ID('s) of the computer(s) not connected to any printer (`'D'`). |
|
|
3896 |
|
Write queries that return the following information. The values returned *in this set-up* will be in parenthesis, but keep the queries general. |
|
3897 |
|
|
|
3898 |
|
#. The number of computers connected to the printer whose `ID` is `'13'` (`2`). |
|
3899 |
|
#. The number of different models of printers (`2`). |
|
3900 |
|
#. The model(s) of the printer(s) connected to the computer whose `ID` is `'A'` (`'HP-140'` and `'HP-139'`). |
|
3901 |
|
#. The ID('s) of the computer(s) not connected to any printer (`'D'`). |
3873 |
3902 |
|
|
3874 |
3903 |
--- |
--- |
3875 |
3904 |
|
|
|
... |
... |
Problem (Write select queries for the SocialMedia schema) +.#SocialMedia |
3877 |
3906 |
~ |
~ |
3878 |
3907 |
|
|
3879 |
3908 |
Consider the following `SQL` code: |
Consider the following `SQL` code: |
3880 |
|
|
|
3881 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_SocialMedia.sql snippet=set-up} |
|
3882 |
|
``` |
|
|
3909 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
3883 |
3910 |
|
|
3884 |
|
Write queries that return the following information. The values returned *in this set-up* will be in parenthesis, but keep the queries general. |
|
3885 |
3911 |
|
|
3886 |
|
#. The title of all the videos (`"My first video!"`, `"My second video!"`, `"My vacations"`). |
|
3887 |
|
#. The release date of the video whose title is `"My first video!"` (`"2020-02-02"`). |
|
3888 |
|
#. The ID of the account(s) where the "Name" attribute was not given (`"2"`). |
|
3889 |
|
#. The ID of the videos whose title contains the word `"video"` (`"10"`, `"20"`). |
|
3890 |
|
#. The number of thumbs up for the video with title `"My vacations"` (`"1"`). |
|
3891 |
|
#. The title of the oldest video (`"My first video!"`). |
|
3892 |
|
#. The names of the accounts who gave a thumbs up to the video with ID 30 (`"Bob Ross"`). |
|
3893 |
|
#. The ID of the account with the greatest number of subscribers (`"2"`). |
|
|
3912 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_SocialMedia.sql snippet=set-up} |
|
3913 |
|
``` |
|
3914 |
|
|
|
3915 |
|
Write queries that return the following information. The values returned *in this set-up* will be in parenthesis, but keep the queries general. |
|
3916 |
|
|
|
3917 |
|
#. The title of all the videos (`"My first video!"`, `"My second video!"`, `"My vacations"`). |
|
3918 |
|
#. The release date of the video whose title is `"My first video!"` (`"2020-02-02"`). |
|
3919 |
|
#. The ID of the account(s) where the "Name" attribute was not given (`"2"`). |
|
3920 |
|
#. The ID of the videos whose title contains the word `"video"` (`"10"`, `"20"`). |
|
3921 |
|
#. The number of thumbs up for the video with title `"My vacations"` (`"1"`). |
|
3922 |
|
#. The title of the oldest video (`"My first video!"`). |
|
3923 |
|
#. The names of the accounts who gave a thumbs up to the video with ID 30 (`"Bob Ross"`). |
|
3924 |
|
#. The ID of the account with the greatest number of subscribers (`"2"`). |
3894 |
3925 |
|
|
3895 |
3926 |
|
|
3896 |
3927 |
--- |
--- |
|
... |
... |
Problem (Write select queries for a variation of the COMPUTER table) +.#Computer |
3901 |
3932 |
|
|
3902 |
3933 |
Consider the following `SQL` code: |
Consider the following `SQL` code: |
3903 |
3934 |
|
|
3904 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ComputerVariation.sql snippet=set-up} |
|
3905 |
|
``` |
|
3906 |
|
|
|
3907 |
|
Write queries that return the following information. The values returned *in this set-up* will be in parenthesis, but keep the queries general. |
|
|
3935 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
3936 |
|
|
|
3937 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ComputerVariation.sql snippet=set-up} |
|
3938 |
|
``` |
3908 |
3939 |
|
|
3909 |
|
#. The model of the computer whose `ID` is `'A'` (`'Apple IIc Plus'`). |
|
3910 |
|
#. The type of the peripheral whose `ID` is `'14'` (`printer`). |
|
3911 |
|
#. The model of the printers (Trendcom Model, TP-10 Thermal Matrix). |
|
3912 |
|
#. The model of the peripherals whose `NAME` starts with `'IBM'` (`'IBM Selectric'`). |
|
3913 |
|
#. The model of the peripherals connected to the computer whose `ID` is `'A'` (`Trendcom Model, IBM Selectric`). |
|
3914 |
|
#. The number of peripheral connected to the computer whose model is Apple IIc Plus (`2`). |
|
|
3940 |
|
Write queries that return the following information. The values returned *in this set-up* will be in parenthesis, but keep the queries general. |
|
3941 |
|
|
|
3942 |
|
#. The model of the computer whose `ID` is `'A'` (`'Apple IIc Plus'`). |
|
3943 |
|
#. The type of the peripheral whose `ID` is `'14'` (`printer`). |
|
3944 |
|
#. The model of the printers (Trendcom Model, TP-10 Thermal Matrix). |
|
3945 |
|
#. The model of the peripherals whose `NAME` starts with `'IBM'` (`'IBM Selectric'`). |
|
3946 |
|
#. The model of the peripherals connected to the computer whose `ID` is `'A'` (`Trendcom Model, IBM Selectric`). |
|
3947 |
|
#. The number of peripheral connected to the computer whose model is Apple IIc Plus (`2`). |
3915 |
3948 |
|
|
3916 |
3949 |
--- |
--- |
3917 |
3950 |
|
|
|
... |
... |
Problem (A simple database for books) +.#sqlBooks |
3964 |
3997 |
|
|
3965 |
3998 |
Consider the following code: |
Consider the following code: |
3966 |
3999 |
|
|
3967 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_SimpleBook.sql} |
|
3968 |
|
``` |
|
|
4000 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
3969 |
4001 |
|
|
3970 |
|
The values inserted in the database is just to provide some examples; you should assume there is more data in it than what we have inserted. |
|
3971 |
|
In this long problem, you will be asked to write commands to select, update, delete, insert data, and to improve upon the relational model. |
|
3972 |
4002 |
|
|
3973 |
|
@problem:sqlBooks -- Question -.# |
|
3974 |
|
~ |
|
3975 |
|
|
|
3976 |
|
Write a command that selects: |
|
|
4003 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_SimpleBook.sql} |
|
4004 |
|
``` |
3977 |
4005 |
|
|
3978 |
|
#. The `Title` of all the books. |
|
3979 |
|
#. The *distinct* `Name` of the publishers. |
|
3980 |
|
#. The `Titles` and `Published` dates of the books published since January 31, 2012. |
|
3981 |
|
#. The first and last names of the authors published by `"Gallimard"` (from any city). |
|
3982 |
|
#. The first and last names of the authors who were not published by an editor in `"New-York"`. |
|
3983 |
|
#. The `ID` of the authors who published a book whose name starts with `"Where"`. |
|
3984 |
|
#. The total number of pages in the database. |
|
3985 |
|
#. The number of pages in the longest book written by the author whose last name is `"Wolve"`. |
|
3986 |
|
#. The titles of the books published in the 19th century. |
|
|
4006 |
|
The values inserted in the database is just to provide some examples; you should assume there is more data in it than what we have inserted. |
|
4007 |
|
In this long problem, you will be asked to write commands to select, update, delete, insert data, and to improve upon the relational model. |
3987 |
4008 |
|
|
3988 |
|
@problem:sqlBooks -- Question -.# |
|
3989 |
|
: Write a command that updates the title of all the books written by the author whose `ID` is `3` to `"BANNED"`. Is there any reason for this command to be rejected by the system? If yes, explain the reason. |
|
|
4009 |
|
@problem:sqlBooks -- Question -.# |
|
4010 |
|
~ |
|
4011 |
|
|
|
4012 |
|
Write a command that selects: |
|
4013 |
|
|
|
4014 |
|
#. The `Title` of all the books. |
|
4015 |
|
#. The *distinct* `Name` of the publishers. |
|
4016 |
|
#. The `Titles` and `Published` dates of the books published since January 31, 2012. |
|
4017 |
|
#. The first and last names of the authors published by `"Gallimard"` (from any city). |
|
4018 |
|
#. The first and last names of the authors who were not published by an editor in `"New-York"`. |
|
4019 |
|
#. The `ID` of the authors who published a book whose name starts with `"Where"`. |
|
4020 |
|
#. The total number of pages in the database. |
|
4021 |
|
#. The number of pages in the longest book written by the author whose last name is `"Wolve"`. |
|
4022 |
|
#. The titles of the books published in the 19th century. |
3990 |
4023 |
|
|
3991 |
|
@problem:sqlBooks -- Question -.# |
|
3992 |
|
: Write one or multiple commands that would delete the author whose `ID` is `3` and all the books written by that author. Make sure you do not violate any foreign key constraints. |
|
|
4024 |
|
@problem:sqlBooks -- Question -.# |
|
4025 |
|
: Write a command that updates the title of all the books written by the author whose `ID` is `3` to `"BANNED"`. Is there any reason for this command to be rejected by the system? If yes, explain the reason. |
3993 |
4026 |
|
|
3994 |
|
@problem:sqlBooks -- Question -.# |
|
3995 |
|
: Write a command that would create a table used to record the awards granted to authors for particular books. Assume that each award has its own name, is awarded every year, and that it is awarded to an author for a particular book. Pick appropriate attributes, datatypes^[You can use the `DATE` datatype to store a year.], primary and foreign keys, and, as always, avoid redundancy. |
|
|
4027 |
|
@problem:sqlBooks -- Question -.# |
|
4028 |
|
: Write one or multiple commands that would delete the author whose `ID` is `3` and all the books written by that author. Make sure you do not violate any foreign key constraints. |
3996 |
4029 |
|
|
3997 |
|
@problem:sqlBooks -- Question -.# |
|
3998 |
|
: Draw the relational model of the database you created (including all the relations given in the code and the ones you added). |
|
|
4030 |
|
@problem:sqlBooks -- Question -.# |
|
4031 |
|
: Write a command that would create a table used to record the awards granted to authors for particular books. Assume that each award has its own name, is awarded every year, and that it is awarded to an author for a particular book. Pick appropriate attributes, datatypes^[You can use the `DATE` datatype to store a year.], primary and foreign keys, and, as always, avoid redundancy. |
3999 |
4032 |
|
|
4000 |
|
@problem:sqlBooks -- Question -.# |
|
4001 |
|
: Discuss two limitations of the model and how to improve it. |
|
|
4033 |
|
@problem:sqlBooks -- Question -.# |
|
4034 |
|
: Draw the relational model of the database you created (including all the relations given in the code and the ones you added). |
|
4035 |
|
|
|
4036 |
|
@problem:sqlBooks -- Question -.# |
|
4037 |
|
: Discuss two limitations of the model and how to improve it. |
4002 |
4038 |
|
|
4003 |
4039 |
--- |
--- |
4004 |
4040 |
|
|
|
... |
... |
Problem (A simple database for published pieces of work) +.#sqlWorks |
4076 |
4112 |
|
|
4077 |
4113 |
Consider the following code: |
Consider the following code: |
4078 |
4114 |
|
|
4079 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Work.sql snippet=set-up} |
|
4080 |
|
``` |
|
4081 |
|
|
|
4082 |
|
Assume the following: |
|
4083 |
|
|
|
4084 |
|
#. Every statement respects `SQL`'s syntax (there's no "a semi-colon is missing" trap). |
|
4085 |
|
#. None of the commands in the rest of this problem are actually executed; they are for hypothetical "what if" questions. |
|
4086 |
|
|
|
4087 |
|
Also, note that each row inserted between line 39 and 50 is given a name in comment (`"A.1, A.2, A.3, W.1"`, etc.). |
|
|
4115 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
4088 |
4116 |
|
|
4089 |
|
@problem:sqlWorks -- Question -.# |
|
4090 |
|
: Draw the relational model corresponding to this series of commands. |
|
4091 |
4117 |
|
|
4092 |
|
@problem:sqlWorks -- Question -.# |
|
4093 |
|
~ |
|
|
4118 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Work.sql snippet=set-up} |
|
4119 |
|
``` |
|
4120 |
|
|
|
4121 |
|
Assume the following: |
4094 |
4122 |
|
|
4095 |
|
Determine if the following insertion statements would violate the entity integrity constraint, the referential integrity constraint, if there would be some other kind of error, or if it would result in successful insertion. |
|
|
4123 |
|
#. Every statement respects `SQL`'s syntax (there's no "a semi-colon is missing" trap). |
|
4124 |
|
#. None of the commands in the rest of this problem are actually executed; they are for hypothetical "what if" questions. |
4096 |
4125 |
|
|
4097 |
|
``` |
|
4098 |
|
INSERT INTO EBOOK VALUES (0, NULL, 20180101, 0); |
|
4099 |
|
INSERT INTO AUTHOR VALUES("Mary B.", "mb@fai.fr", NULL); |
|
4100 |
|
INSERT INTO WORK VALUES("My Life", "Claude A."); |
|
4101 |
|
INSERT INTO BOOK VALUES(00000000, NULL, DATE'20001225', 90.9); |
|
4102 |
|
INSERT INTO AUTHOR VALUES("Virginia W.", "alt@isp.net"); |
|
4103 |
|
``` |
|
|
4126 |
|
Also, note that each row inserted between line 39 and 50 is given a name in comment (`"A.1, A.2, A.3, W.1"`, etc.). |
|
4127 |
|
|
|
4128 |
|
@problem:sqlWorks -- Question -.# |
|
4129 |
|
: Draw the relational model corresponding to this series of commands. |
|
4130 |
|
|
|
4131 |
|
@problem:sqlWorks -- Question -.# |
|
4132 |
|
~ |
|
4133 |
|
|
|
4134 |
|
Determine if the following insertion statements would violate the entity integrity constraint, the referential integrity constraint, if there would be some other kind of error, or if it would result in successful insertion. |
|
4135 |
|
|
|
4136 |
|
``` |
|
4137 |
|
INSERT INTO EBOOK VALUES (0, NULL, 20180101, 0); |
|
4138 |
|
INSERT INTO AUTHOR VALUES("Mary B.", "mb@fai.fr", NULL); |
|
4139 |
|
INSERT INTO WORK VALUES("My Life", "Claude A."); |
|
4140 |
|
INSERT INTO BOOK VALUES(00000000, NULL, DATE'20001225', 90.9); |
|
4141 |
|
INSERT INTO AUTHOR VALUES("Virginia W.", "alt@isp.net"); |
|
4142 |
|
``` |
|
4143 |
|
|
|
4144 |
|
@problem:sqlWorks -- Question -.# |
|
4145 |
|
~ |
|
4146 |
|
|
|
4147 |
|
List the rows (A.2, W.1, etc.) modified by the following statements. Be careful about the conditions on foreign keys! |
4104 |
4148 |
|
|
4105 |
|
@problem:sqlWorks -- Question -.# |
|
4106 |
|
~ |
|
|
4149 |
|
``` |
|
4150 |
|
UPDATE AUTHOR SET Email = 'Deprecated' WHERE Email LIKE '%isp.net'; |
|
4151 |
|
UPDATE WORK SET Title = "How to eat" WHERE Title = "What to eat"; |
|
4152 |
|
DELETE FROM WORK; |
|
4153 |
|
DELETE FROM AUTHOR WHERE Name = "Virginia W."; |
|
4154 |
|
``` |
4107 |
4155 |
|
|
4108 |
|
List the rows (A.2, W.1, etc.) modified by the following statements. Be careful about the conditions on foreign keys! |
|
4109 |
|
|
|
4110 |
|
``` |
|
4111 |
|
UPDATE AUTHOR SET Email = 'Deprecated' WHERE Email LIKE '%isp.net'; |
|
4112 |
|
UPDATE WORK SET Title = "How to eat" WHERE Title = "What to eat"; |
|
4113 |
|
DELETE FROM WORK; |
|
4114 |
|
DELETE FROM AUTHOR WHERE Name = "Virginia W."; |
|
4115 |
|
``` |
|
4116 |
|
|
|
4117 |
|
@problem:sqlWorks -- Question -.# |
|
4118 |
|
~ |
|
4119 |
|
Assume that there is more data than what we inserted. Write a command that selects: |
|
4120 |
|
|
|
4121 |
|
- The prices of all the ebooks. |
|
4122 |
|
- The *distinct* names of the authors who have authored a piece of work. |
|
4123 |
|
- The names of the authors using fai.fr for their email domain. |
|
4124 |
|
- The prices of the ebooks published after 2018. |
|
4125 |
|
- The price of the most expensive book. |
|
4126 |
|
- The number of the pieces of work written by the author whose name is "Virginia W.." |
|
4127 |
|
- The email of the author who wrote the piece called "My Life." |
|
4128 |
|
- The ISBN's of the books containing a work written by the author whose email is "vw@isp.net." |
|
4129 |
|
|
|
4130 |
|
@problem:sqlWorks -- Question -.# |
|
4131 |
|
~ |
|
4132 |
|
Write a command that updates the title of all the pieces of work written by the author whose name is "Virginia W." to "BANNED." |
|
4133 |
|
Is there any reason for this command to be rejected by the system? |
|
4134 |
|
If yes, explain the reason. |
|
|
4156 |
|
@problem:sqlWorks -- Question -.# |
|
4157 |
|
~ |
|
4158 |
|
Assume that there is more data than what we inserted. Write a command that selects: |
|
4159 |
|
|
|
4160 |
|
- The prices of all the ebooks. |
|
4161 |
|
- The *distinct* names of the authors who have authored a piece of work. |
|
4162 |
|
- The names of the authors using fai.fr for their email domain. |
|
4163 |
|
- The prices of the ebooks published after 2018. |
|
4164 |
|
- The price of the most expensive book. |
|
4165 |
|
- The number of the pieces of work written by the author whose name is "Virginia W.." |
|
4166 |
|
- The email of the author who wrote the piece called "My Life." |
|
4167 |
|
- The ISBN's of the books containing a work written by the author whose email is "vw@isp.net." |
4135 |
4168 |
|
|
4136 |
|
@problem:sqlWorks -- Question -.# |
|
4137 |
|
: Write one or multiple commands that would delete the work whose title is "My Life", as well as all of the book and ebook versions of it. |
|
|
4169 |
|
@problem:sqlWorks -- Question -.# |
|
4170 |
|
~ |
|
4171 |
|
Write a command that updates the title of all the pieces of work written by the author whose name is "Virginia W." to "BANNED." |
|
4172 |
|
Is there any reason for this command to be rejected by the system? |
|
4173 |
|
If yes, explain the reason. |
|
4174 |
|
|
|
4175 |
|
@problem:sqlWorks -- Question -.# |
|
4176 |
|
: Write one or multiple commands that would delete the work whose title is "My Life", as well as all of the book and ebook versions of it. |
|
4177 |
|
|
|
4178 |
|
@problem:sqlWorks -- Question -.# |
|
4179 |
|
: Discuss two limitations of the model and how to improve it. |
4138 |
4180 |
|
|
4139 |
|
@problem:sqlWorks -- Question -.# |
|
4140 |
|
: Discuss two limitations of the model and how to improve it. |
|
4141 |
4181 |
|
|
4142 |
|
|
|
4143 |
4182 |
--- |
--- |
4144 |
4183 |
|
|
4145 |
4184 |
Problem (A simple database for authors of textbooks) +.#project1 |
Problem (A simple database for authors of textbooks) +.#project1 |
4146 |
4185 |
~ |
~ |
4147 |
4186 |
|
|
4148 |
4187 |
Consider the following code: |
Consider the following code: |
4149 |
|
|
|
4150 |
|
|
|
4151 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_TextbookAuthored.sql} |
|
4152 |
|
``` |
|
4153 |
|
The meaning of the `AUTHORED` table is that a tuple $<I, L, F>$ represents that the author whose last name is L and whose first name is F wrote the textbook whose ISBN is I. |
|
4154 |
|
|
|
4155 |
|
Answer the following: |
|
4156 |
|
|
|
4157 |
|
#. Write a command that updates the email address of 'Gaddis', 'Tony.' |
|
4158 |
|
#. Write a command that inserts a textbook of your choice into the `TEXTBOOK` table. No value should be `NULL`. |
|
4159 |
|
#. Write a command that makes 'Gaddis', 'Tony' the author of the textbook you just added to our database. |
|
4160 |
|
#. Write a command that makes "0.01" the default value for the `Price` attribute of the `TEXTBOOK` relation. |
|
4161 |
|
#. Write a command that inserts a textbook of your choice in the `TEXTBOOK` table and have the price set to the default value. |
|
4162 |
|
#. Write a command that creates a table called `EDITOR` with three attributes: `Name`, `Address`, and `Website`. The `Name` attribute should be the primary key. Insert two tuples in the `EDITOR` table, making sure that one should has the `Name` attribute set to "Pearson." |
|
4163 |
|
#. Write a command that creates a table called `PUBLISHED` with two attributes: `Editor` and `Textbook`. The `Editor` attribute should reference the `EDITOR` table and the `Textbook` attribute should reference the `TEXTBOOK` table. |
|
4164 |
|
#. Write a command that makes "Pearson" the editor of the textbook whose ISBN is 9780133776744. |
|
4165 |
|
|
|
4166 |
|
Answer the following short questions based on what is in our model so far: |
|
4167 |
|
|
|
4168 |
|
#. Can an author have authored more than one textbook? |
|
4169 |
|
#. Can a textbook have more than one author? |
|
4170 |
|
#. Can a textbook without an `ISBN` be inserted in the `TEXTBOOK` relation? |
|
4171 |
|
#. Can the price of a textbook be negative? |
|
4172 |
|
#. Can two authors have the same first and last names? |
|
4173 |
|
#. Can two textbooks have the same title? |
|
4174 |
|
#. Can two editors have the same address? |
|
|
4188 |
|
|
|
4189 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
4190 |
|
|
|
4191 |
|
|
|
4192 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_TextbookAuthored.sql} |
|
4193 |
|
``` |
|
4194 |
|
The meaning of the `AUTHORED` table is that a tuple $<I, L, F>$ represents that the author whose last name is L and whose first name is F wrote the textbook whose ISBN is I. |
|
4195 |
|
|
|
4196 |
|
Answer the following: |
|
4197 |
|
|
|
4198 |
|
#. Write a command that updates the email address of 'Gaddis', 'Tony.' |
|
4199 |
|
#. Write a command that inserts a textbook of your choice into the `TEXTBOOK` table. No value should be `NULL`. |
|
4200 |
|
#. Write a command that makes 'Gaddis', 'Tony' the author of the textbook you just added to our database. |
|
4201 |
|
#. Write a command that makes "0.01" the default value for the `Price` attribute of the `TEXTBOOK` relation. |
|
4202 |
|
#. Write a command that inserts a textbook of your choice in the `TEXTBOOK` table and have the price set to the default value. |
|
4203 |
|
#. Write a command that creates a table called `EDITOR` with three attributes: `Name`, `Address`, and `Website`. The `Name` attribute should be the primary key. Insert two tuples in the `EDITOR` table, making sure that one should has the `Name` attribute set to "Pearson." |
|
4204 |
|
#. Write a command that creates a table called `PUBLISHED` with two attributes: `Editor` and `Textbook`. The `Editor` attribute should reference the `EDITOR` table and the `Textbook` attribute should reference the `TEXTBOOK` table. |
|
4205 |
|
#. Write a command that makes "Pearson" the editor of the textbook whose ISBN is 9780133776744. |
|
4206 |
|
|
|
4207 |
|
Answer the following short questions based on what is in our model so far: |
|
4208 |
|
|
|
4209 |
|
#. Can an author have authored more than one textbook? |
|
4210 |
|
#. Can a textbook have more than one author? |
|
4211 |
|
#. Can a textbook without an `ISBN` be inserted in the `TEXTBOOK` relation? |
|
4212 |
|
#. Can the price of a textbook be negative? |
|
4213 |
|
#. Can two authors have the same first and last names? |
|
4214 |
|
#. Can two textbooks have the same title? |
|
4215 |
|
#. Can two editors have the same address? |
4175 |
4216 |
|
|
4176 |
4217 |
--- |
--- |
4177 |
4218 |
|
|
|
... |
... |
Problem (A simple database for capstone projects) +.#project1bis |
4180 |
4221 |
|
|
4181 |
4222 |
Consider the following code: |
Consider the following code: |
4182 |
4223 |
|
|
4183 |
|
|
|
4184 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Capstone.sql} |
|
4185 |
|
``` |
|
4186 |
|
The meaning of the USED_LANGUAGE table is that a tuple $< N, L, U>$ represents the fact that the project whose code name is $N$ and whose leader is $L$ uses the programming language $U$. |
|
|
4224 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
4187 |
4225 |
|
|
4188 |
|
@problem:project1bis -- Question -.# |
|
4189 |
4226 |
|
|
4190 |
|
: Answer the following short questions based on the model implemented above. |
|
4191 |
|
You can simply answer "True" or "False", or justify your reasoning (e.g. with code). |
|
4192 |
4227 |
|
|
4193 |
|
#. Can a project uses multiple programming languages? |
|
4194 |
|
#. Can a student be the leader of multiple projects? |
|
4195 |
|
#. Can multiple projects have the same code name? |
|
4196 |
|
#. Could Claude simply enter NULL for the value of his project's code name, since he's undecided? |
|
4197 |
|
#. Can a project be created without project leader? |
|
4198 |
|
#. Can we know who is working on a project without being its leader? |
|
|
4228 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Capstone.sql} |
|
4229 |
|
``` |
|
4230 |
|
The meaning of the USED_LANGUAGE table is that a tuple $< N, L, U>$ represents the fact that the project whose code name is $N$ and whose leader is $L$ uses the programming language $U$. |
4199 |
4231 |
|
|
4200 |
|
@problem:project1bis -- Question -.# |
|
4201 |
|
|
|
4202 |
|
~ Draw the relational model corresponding to this code. |
|
|
4232 |
|
@problem:project1bis -- Question -.# |
4203 |
4233 |
|
|
4204 |
|
@problem:project1bis -- Question -.# |
|
4205 |
|
|
|
4206 |
|
: Write the following commands. |
|
4207 |
|
|
|
4208 |
|
#. Write a command that insert a new student in the STUDENT table. |
|
4209 |
|
#. Write a command that updates the code name of the project ("Undecided", "9999999999999") to "VR in ER". |
|
4210 |
|
#. Write a command that updates the graduation year of the student whose id is "0987654321098" to 2024, and the semester to "Fall". |
|
4211 |
|
#. Write a command that changes the STUDENT table to make it impossible to enter NULL for the first name of a student, without changing the primary key. |
|
4212 |
|
#. Write a command that changes the datatype of GraduationYear to SMALLINT. |
|
4213 |
|
#. Write a command that adds an attribute "ReleaseDate" to the PROJECT table. |
|
4214 |
|
#. If you managed to write the previous command correctly, write a command that sets the release date of the project ("Brick Break", "0123456789100") to the 26th of November 2022. |
|
4215 |
|
#. Write a command that makes it impossible for a student to be the leader in more than one project |
|
|
4234 |
|
: Answer the following short questions based on the model implemented above. |
|
4235 |
|
You can simply answer "True" or "False", or justify your reasoning (e.g. with code). |
|
4236 |
|
|
|
4237 |
|
#. Can a project uses multiple programming languages? |
|
4238 |
|
#. Can a student be the leader of multiple projects? |
|
4239 |
|
#. Can multiple projects have the same code name? |
|
4240 |
|
#. Could Claude simply enter NULL for the value of his project's code name, since he's undecided? |
|
4241 |
|
#. Can a project be created without project leader? |
|
4242 |
|
#. Can we know who is working on a project without being its leader? |
|
4243 |
|
|
|
4244 |
|
@problem:project1bis -- Question -.# |
|
4245 |
|
|
|
4246 |
|
~ Draw the relational model corresponding to this code. |
|
4247 |
|
|
|
4248 |
|
@problem:project1bis -- Question -.# |
|
4249 |
|
|
|
4250 |
|
: Write the following commands. |
|
4251 |
|
|
|
4252 |
|
#. Write a command that insert a new student in the STUDENT table. |
|
4253 |
|
#. Write a command that updates the code name of the project ("Undecided", "9999999999999") to "VR in ER". |
|
4254 |
|
#. Write a command that updates the graduation year of the student whose id is "0987654321098" to 2024, and the semester to "Fall". |
|
4255 |
|
#. Write a command that changes the STUDENT table to make it impossible to enter NULL for the first name of a student, without changing the primary key. |
|
4256 |
|
#. Write a command that changes the datatype of GraduationYear to SMALLINT. |
|
4257 |
|
#. Write a command that adds an attribute "ReleaseDate" to the PROJECT table. |
|
4258 |
|
#. If you managed to write the previous command correctly, write a command that sets the release date of the project ("Brick Break", "0123456789100") to the 26th of November 2022. |
|
4259 |
|
#. Write a command that makes it impossible for a student to be the leader in more than one project |
4216 |
4260 |
|
|
4217 |
4261 |
--- |
--- |
4218 |
4262 |
|
|
|
... |
... |
Problem (A database for residencies) +.#residency |
4221 |
4265 |
|
|
4222 |
4266 |
Consider the following code: |
Consider the following code: |
4223 |
4267 |
|
|
4224 |
|
|
|
4225 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Residency.sql} |
|
4226 |
|
``` |
|
4227 |
|
Note that each row inserted in the `PERSON`, `HOUSE` and `RESIDENCY` tables is given the name and noted as afterwards as a comment (`"P.1, P.2, P.3, P.4, H.1"`, etc.). |
|
|
4268 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
4228 |
4269 |
|
|
4229 |
|
Answer the following questions and problems, assuming that none of the commands in the rest of the problem are actually executed. |
|
4230 |
4270 |
|
|
4231 |
|
@problem:residency -- Question -.# |
|
4232 |
4271 |
|
|
4233 |
|
: Draw the relational model corresponding to this series of commands (it is not necessary to include the state). |
|
4234 |
|
|
|
4235 |
|
@problem:residency -- Question -.# |
|
4236 |
|
: Write a command that violates the entity integrity constraint. |
|
|
4272 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Residency.sql} |
|
4273 |
|
``` |
|
4274 |
|
Note that each row inserted in the `PERSON`, `HOUSE` and `RESIDENCY` tables is given the name and noted as afterwards as a comment (`"P.1, P.2, P.3, P.4, H.1"`, etc.). |
4237 |
4275 |
|
|
4238 |
|
@problem:residency -- Question -.# |
|
4239 |
|
: Write a command that violates the referential integrity constraint. |
|
|
4276 |
|
Answer the following questions and problems, assuming that none of the commands in the rest of the problem are actually executed. |
4240 |
4277 |
|
|
4241 |
|
@problem:residency -- Question -.# |
|
4242 |
|
: List the rows (e.g. "P.2", "H.1", or "none") modified by the following statements: |
|
|
4278 |
|
@problem:residency -- Question -.# |
4243 |
4279 |
|
|
4244 |
|
#. `UPDATE HOUSE SET COLOR = "green";` |
|
4245 |
|
#. `DELETE FROM RESIDENCY WHERE House LIKE "1%";` |
|
4246 |
|
#. `DELETE FROM HOUSE WHERE Address = "456 Second St.";` |
|
4247 |
|
#. `DELETE FROM PERSON WHERE Birthdate=DATE"1990-02-11";` |
|
|
4280 |
|
: Draw the relational model corresponding to this series of commands (it is not necessary to include the state). |
4248 |
4281 |
|
|
4249 |
|
@problem:residency -- Question -.# |
|
4250 |
|
~ |
|
4251 |
|
|
|
4252 |
|
Write queries that return the following information. The values returned *in this set-up* will be in parenthesis, but keep the queries general. |
|
4253 |
|
|
|
4254 |
|
#. The `Addresses' of the houses in the system (`"11 Third St., 123 Main St., 456 Second St."`). |
|
4255 |
|
#. The `SSN`'s of the people whose first name was not entered in the system (`"000-00-0000"`). |
|
4256 |
|
#. All the different colors of houses (`"white, blue"`). |
|
4257 |
|
#. The `Address` of the residency of `"James Baldwin"` (`"123 Main St."`). |
|
4258 |
|
#. The first name of the oldest person in the database (`"James"`). |
|
4259 |
|
#. `"Michael Keal"`'s principal residency address (`"123 Main St."`). |
|
4260 |
|
#. The *distinct* first and last names of the homeowners (`"Michael Keal, Mridula Warrier"`). |
|
4261 |
|
#. The `SSN`'s of the people that have the same principal residency as `"James Baldwin"` (`"000-00-0001"`). |
|
4262 |
|
|
|
4263 |
|
@problem:residency -- Question -.# |
|
4264 |
|
: Write a command that updates the `SSN` of `"James Baldwin"` to `"000-00-0010"`. Is there any reason for this command to be rejected by the system? If yes, explain the reason. |
|
4265 |
|
|
|
4266 |
|
@problem:residency -- Question -.# |
|
4267 |
|
~ |
|
4268 |
|
Answer the following short questions from the data in our model, as it is currently: |
|
|
4282 |
|
@problem:residency -- Question -.# |
|
4283 |
|
: Write a command that violates the entity integrity constraint. |
|
4284 |
|
|
|
4285 |
|
@problem:residency -- Question -.# |
|
4286 |
|
: Write a command that violates the referential integrity constraint. |
|
4287 |
|
|
|
4288 |
|
@problem:residency -- Question -.# |
|
4289 |
|
: List the rows (e.g. "P.2", "H.1", or "none") modified by the following statements: |
|
4290 |
|
|
|
4291 |
|
#. `UPDATE HOUSE SET COLOR = "green";` |
|
4292 |
|
#. `DELETE FROM RESIDENCY WHERE House LIKE "1%";` |
|
4293 |
|
#. `DELETE FROM HOUSE WHERE Address = "456 Second St.";` |
|
4294 |
|
#. `DELETE FROM PERSON WHERE Birthdate=DATE"1990-02-11";` |
|
4295 |
|
|
|
4296 |
|
@problem:residency -- Question -.# |
|
4297 |
|
~ |
4269 |
4298 |
|
|
4270 |
|
#. Is it possible for two people to have the same last name? |
|
4271 |
|
#. Is it possible for a person to have multiple principal residencies? |
|
4272 |
|
#. Is it possible for a house to not be yellow? |
|
4273 |
|
#. Is it possible for the `SSN` to be any series of 11 characters? |
|
4274 |
|
#. Is it possible for a person to own any number of houses? |
|
4275 |
|
#. Is it possible for a person to rent at most one house? |
|
|
4299 |
|
Write queries that return the following information. The values returned *in this set-up* will be in parenthesis, but keep the queries general. |
|
4300 |
|
|
|
4301 |
|
#. The `Addresses' of the houses in the system (`"11 Third St., 123 Main St., 456 Second St."`). |
|
4302 |
|
#. The `SSN`'s of the people whose first name was not entered in the system (`"000-00-0000"`). |
|
4303 |
|
#. All the different colors of houses (`"white, blue"`). |
|
4304 |
|
#. The `Address` of the residency of `"James Baldwin"` (`"123 Main St."`). |
|
4305 |
|
#. The first name of the oldest person in the database (`"James"`). |
|
4306 |
|
#. `"Michael Keal"`'s principal residency address (`"123 Main St."`). |
|
4307 |
|
#. The *distinct* first and last names of the homeowners (`"Michael Keal, Mridula Warrier"`). |
|
4308 |
|
#. The `SSN`'s of the people that have the same principal residency as `"James Baldwin"` (`"000-00-0001"`). |
|
4309 |
|
|
|
4310 |
|
@problem:residency -- Question -.# |
|
4311 |
|
: Write a command that updates the `SSN` of `"James Baldwin"` to `"000-00-0010"`. Is there any reason for this command to be rejected by the system? If yes, explain the reason. |
|
4312 |
|
|
|
4313 |
|
@problem:residency -- Question -.# |
|
4314 |
|
~ |
|
4315 |
|
Answer the following short questions from the data in our model, as it is currently: |
4276 |
4316 |
|
|
4277 |
|
@problem:residency -- Question -.# |
|
4278 |
|
: Consider the data currently in the `RESIDENCY` table and give a possible primary key. |
|
|
4317 |
|
#. Is it possible for two people to have the same last name? |
|
4318 |
|
#. Is it possible for a person to have multiple principal residencies? |
|
4319 |
|
#. Is it possible for a house to not be yellow? |
|
4320 |
|
#. Is it possible for the `SSN` to be any series of 11 characters? |
|
4321 |
|
#. Is it possible for a person to own any number of houses? |
|
4322 |
|
#. Is it possible for a person to rent at most one house? |
4279 |
4323 |
|
|
4280 |
|
@problem:residency -- Question -.# |
|
4281 |
|
: Discuss why the primary key identified from the previous question for the `RESIDENCY` table is a good choice. |
|
|
4324 |
|
@problem:residency -- Question -.# |
|
4325 |
|
: Consider the data currently in the `RESIDENCY` table and give a possible primary key. |
|
4326 |
|
|
|
4327 |
|
@problem:residency -- Question -.# |
|
4328 |
|
: Discuss why the primary key identified from the previous question for the `RESIDENCY` table is a good choice. |
4282 |
4329 |
|
|
4283 |
4330 |
|
|
4284 |
4331 |
--- |
--- |
|
... |
... |
Problem (A database for research fundings) +.#research-funding |
4287 |
4334 |
~ |
~ |
4288 |
4335 |
|
|
4289 |
4336 |
Consider the following code: |
Consider the following code: |
4290 |
|
|
|
4291 |
|
|
|
4292 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ScientificResearch.sql snippet=statement} |
|
4293 |
|
``` |
|
4294 |
|
Note that each row inserted in the tables is given a name and noted as afterwards as a comment (`"S.1, S.2, P.1, C.1, FA.1"`, etc.). |
|
4295 |
4337 |
|
|
4296 |
|
Answer the following questions and problems, assuming that none of the commands in the rest of the problem are actually executed. |
|
|
4338 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
4297 |
4339 |
|
|
4298 |
|
@problem:research-funding -- Question -.# |
|
4299 |
4340 |
|
|
4300 |
|
: Draw the relational model corresponding to this series of commands (it is not necessary to include the state). |
|
4301 |
|
|
|
4302 |
|
@problem:research-funding -- Question -.# |
|
4303 |
4341 |
|
|
4304 |
|
: Draw the relational model corresponding to this series of commands (no need to include the state). |
|
|
4342 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ScientificResearch.sql snippet=statement} |
|
4343 |
|
``` |
|
4344 |
|
Note that each row inserted in the tables is given a name and noted as afterwards as a comment (`"S.1, S.2, P.1, C.1, FA.1"`, etc.). |
|
4345 |
|
|
|
4346 |
|
Answer the following questions and problems, assuming that none of the commands in the rest of the problem are actually executed. |
|
4347 |
|
|
|
4348 |
|
@problem:research-funding -- Question -.# |
|
4349 |
|
|
|
4350 |
|
: Draw the relational model corresponding to this series of commands (it is not necessary to include the state). |
|
4351 |
|
|
|
4352 |
|
@problem:research-funding -- Question -.# |
4305 |
4353 |
|
|
4306 |
|
@problem:research-funding -- Question -.# |
|
|
4354 |
|
: Draw the relational model corresponding to this series of commands (no need to include the state). |
4307 |
4355 |
|
|
4308 |
|
: How could you edit line 12 so that negative values and `NULL`{.sql} would not be admitted as values for `Hours`{.sql}? |
|
|
4356 |
|
@problem:research-funding -- Question -.# |
4309 |
4357 |
|
|
4310 |
|
@problem:research-funding -- Question -.# |
|
|
4358 |
|
: How could you edit line 12 so that negative values and `NULL`{.sql} would not be admitted as values for `Hours`{.sql}? |
4311 |
4359 |
|
|
4312 |
|
: Write a command that would violate the referential integrity constraint. |
|
|
4360 |
|
@problem:research-funding -- Question -.# |
4313 |
4361 |
|
|
4314 |
|
@problem:research-funding -- Question -.# |
|
|
4362 |
|
: Write a command that would violate the referential integrity constraint. |
4315 |
4363 |
|
|
4316 |
|
: List the rows affected (updated or deleted) by the following commands. If no rows are affected because the command would would violate the entity integrity constraint, the referential integrity constraint, or if there would be some other kind of error, please indicate it. |
|
|
4364 |
|
@problem:research-funding -- Question -.# |
4317 |
4365 |
|
|
4318 |
|
#. `UPDATE SCIENTIST SET SSN = "000000001" WHERE Name = "Claire";`{.sql} |
|
4319 |
|
#. `UPDATE FUNDINGAGENCY SET Name = "NSF" WHERE Name = "National Science Foundation";`{.sql} |
|
4320 |
|
#. `DELETE FROM FUNDINGAGENCY WHERE Name = "French-American Cultural Exchange";`{.sql} |
|
|
4366 |
|
: List the rows affected (updated or deleted) by the following commands. If no rows are affected because the command would would violate the entity integrity constraint, the referential integrity constraint, or if there would be some other kind of error, please indicate it. |
4321 |
4367 |
|
|
4322 |
|
@problem:research-funding -- Question -.# |
|
|
4368 |
|
#. `UPDATE SCIENTIST SET SSN = "000000001" WHERE Name = "Claire";`{.sql} |
|
4369 |
|
#. `UPDATE FUNDINGAGENCY SET Name = "NSF" WHERE Name = "National Science Foundation";`{.sql} |
|
4370 |
|
#. `DELETE FROM FUNDINGAGENCY WHERE Name = "French-American Cultural Exchange";`{.sql} |
4323 |
4371 |
|
|
4324 |
|
: Write a query that selects …(In parenthesis, the values returned *in this set-up*, but you have to be general.) |
|
|
4372 |
|
@problem:research-funding -- Question -.# |
4325 |
4373 |
|
|
4326 |
|
#. …the name of the funding agencies created after 2000 (\"French-American Cultural Exchange\") |
|
|
4374 |
|
: Write a query that selects …(In parenthesis, the values returned *in this set-up*, but you have to be general.) |
4327 |
4375 |
|
|
4328 |
|
#. …the code of the projects that contains the word \"Airplanes\" (\"AA\", \"BA\") |
|
|
4376 |
|
#. …the name of the funding agencies created after 2000 (\"French-American Cultural Exchange\") |
4329 |
4377 |
|
|
4330 |
|
#. …the number of hours scientists contributed to the project \"AA\" (18) |
|
|
4378 |
|
#. …the code of the projects that contains the word \"Airplanes\" (\"AA\", \"BA\") |
4331 |
4379 |
|
|
4332 |
|
#. …the code of the projects to which the scientist named Sabine contributed (\"AA\", \"BB\") |
|
|
4380 |
|
#. …the number of hours scientists contributed to the project \"AA\" (18) |
4333 |
4381 |
|
|
4334 |
|
#. …the name of the projects who benefited from federal funds (\"Advancing Airplanes\") |
|
|
4382 |
|
#. …the code of the projects to which the scientist named Sabine contributed (\"AA\", \"BB\") |
4335 |
4383 |
|
|
4336 |
|
#. …the name of the scientist who contributed to the same project as Mike (\"Sabine\", \"James\") |
|
|
4384 |
|
#. …the name of the projects who benefited from federal funds (\"Advancing Airplanes\") |
4337 |
4385 |
|
|
4338 |
|
#. …the name of the projects that are not funded by an agency (\"Better Airplanes\", \"Better Buildings\") |
|
|
4386 |
|
#. …the name of the scientist who contributed to the same project as Mike (\"Sabine\", \"James\") |
4339 |
4387 |
|
|
4340 |
|
#. …the name of the scientist who contributed the most (in terms of hours) to the project named \"Advancing Airplanes\" (Sabine). |
|
|
4388 |
|
#. …the name of the projects that are not funded by an agency (\"Better Airplanes\", \"Better Buildings\") |
4341 |
4389 |
|
|
4342 |
|
@problem:research-funding -- Question -.# |
|
|
4390 |
|
#. …the name of the scientist who contributed the most (in terms of hours) to the project named \"Advancing Airplanes\" (Sabine). |
4343 |
4391 |
|
|
4344 |
|
: Identify and discuss two limitations of this model, and offer a way to remedy at least one of them. |
|
|
4392 |
|
@problem:research-funding -- Question -.# |
|
4393 |
|
|
|
4394 |
|
: Identify and discuss two limitations of this model, and offer a way to remedy at least one of them. |
4345 |
4395 |
|
|
4346 |
4396 |
|
|
4347 |
4397 |
## Solutions to Selected Problems {-} |
## Solutions to Selected Problems {-} |
|
... |
... |
Solution to [%D %n (%T)](#problem:profrevisited) |
4644 |
4694 |
~ |
~ |
4645 |
4695 |
|
|
4646 |
4696 |
The code is straightforward: |
The code is straightforward: |
4647 |
|
|
|
4648 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ProfExampleRevisitedRevisited.sql snippet=lecture} |
|
4649 |
|
``` |
|
4650 |
|
However, this representation can not handle the following situations: |
|
4651 |
|
|
|
4652 |
|
- If multiple instructors teach the same class, |
|
4653 |
|
- If the lecture is taught more than once a year (either because it is taught in the Fall, Spring and Summer, or if multiple sections are offered at the same time), |
|
4654 |
|
- If a Lecture is cross-listed, then some duplication of information will be needed. |
|
4655 |
|
|
|
4656 |
|
We come back to those short-coming in the ["Reverse-Engineering"](#reverse-engineering-1) section, using more abstract tools (such as Entity Diagrams) that have not been introduced yet. |
|
4657 |
|
|
|
4658 |
|
@problem:profrevisited-- Solution to Q. -.# |
|
4659 |
|
~ |
|
|
4697 |
|
|
|
4698 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
4660 |
4699 |
|
|
4661 |
|
The statements are immediate: |
|
4662 |
|
|
|
4663 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ProfExampleRevisitedRevisited.sql snippet=grade} |
|
4664 |
|
``` |
|
4665 |
|
|
|
4666 |
|
What may be surprising is that the values for `LectureCode` and `LectureYear` are set to `NULL` in all the tuples. |
|
4667 |
4700 |
|
|
4668 |
|
@problem:profrevisited-- Solution to Q. -.# |
|
4669 |
|
~ |
|
|
4701 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ProfExampleRevisitedRevisited.sql snippet=lecture} |
|
4702 |
|
``` |
|
4703 |
|
However, this representation can not handle the following situations: |
4670 |
4704 |
|
|
4671 |
|
We use `UPDATE` statements: |
|
4672 |
|
|
|
4673 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ProfExampleRevisitedRevisited.sql snippet=update} |
|
4674 |
|
``` |
|
4675 |
|
|
|
4676 |
|
@problem:profrevisited-- Solution to Q. -.# |
|
4677 |
|
~ |
|
4678 |
|
We refer back to the solution to Q. 1. |
|
4679 |
|
|
|
4680 |
|
@problem:profrevisited-- Solution to Q. -.# |
|
4681 |
|
~ |
|
|
4705 |
|
- If multiple instructors teach the same class, |
|
4706 |
|
- If the lecture is taught more than once a year (either because it is taught in the Fall, Spring and Summer, or if multiple sections are offered at the same time), |
|
4707 |
|
- If a Lecture is cross-listed, then some duplication of information will be needed. |
4682 |
4708 |
|
|
4683 |
|
We use `SELECT` statements: |
|
4684 |
|
|
|
4685 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ProfExampleRevisitedRevisited.sql snippet=select} |
|
4686 |
|
``` |
|
|
4709 |
|
We come back to those short-coming in the ["Reverse-Engineering"](#reverse-engineering-1) section, using more abstract tools (such as Entity Diagrams) that have not been introduced yet. |
|
4710 |
|
|
|
4711 |
|
@problem:profrevisited-- Solution to Q. -.# |
|
4712 |
|
~ |
|
4713 |
|
|
|
4714 |
|
The statements are immediate: |
|
4715 |
|
|
|
4716 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ProfExampleRevisitedRevisited.sql snippet=grade} |
|
4717 |
|
``` |
|
4718 |
|
|
|
4719 |
|
What may be surprising is that the values for `LectureCode` and `LectureYear` are set to `NULL` in all the tuples. |
|
4720 |
|
|
|
4721 |
|
@problem:profrevisited-- Solution to Q. -.# |
|
4722 |
|
~ |
|
4723 |
|
|
|
4724 |
|
We use `UPDATE` statements: |
|
4725 |
|
|
|
4726 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ProfExampleRevisitedRevisited.sql snippet=update} |
|
4727 |
|
``` |
|
4728 |
|
|
|
4729 |
|
@problem:profrevisited-- Solution to Q. -.# |
|
4730 |
|
~ |
|
4731 |
|
We refer back to the solution to Q. 1. |
|
4732 |
|
|
|
4733 |
|
@problem:profrevisited-- Solution to Q. -.# |
|
4734 |
|
~ |
|
4735 |
|
|
|
4736 |
|
We use `SELECT` statements: |
|
4737 |
|
|
|
4738 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ProfExampleRevisitedRevisited.sql snippet=select} |
|
4739 |
|
``` |
4687 |
4740 |
--- |
--- |
4688 |
4741 |
|
|
4689 |
4742 |
Solution to [%D %n (%T)](#problem:train) |
Solution to [%D %n (%T)](#problem:train) |
|
... |
... |
Solution to [%D %n (%T)](#problem:train) |
4691 |
4744 |
|
|
4692 |
4745 |
The code below includes the answers to all of the questions for this problem: |
The code below includes the answers to all of the questions for this problem: |
4693 |
4746 |
|
|
4694 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Train.sql snippet=solution} |
|
4695 |
|
``` |
|
|
4747 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
4748 |
|
|
|
4749 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Train.sql snippet=solution} |
|
4750 |
|
``` |
4696 |
4751 |
|
|
4697 |
4752 |
--- |
--- |
4698 |
4753 |
|
|
|
... |
... |
Solution to [%D %n (%T)](#problem:coffee) |
4711 |
4766 |
|
|
4712 |
4767 |
|
|
4713 |
4768 |
The answers to the rest of the questions are in the following code: |
The answers to the rest of the questions are in the following code: |
4714 |
|
|
|
4715 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_DBCoffee.sql snippet=solution} |
|
4716 |
|
``` |
|
|
4769 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
4770 |
|
|
|
4771 |
|
|
|
4772 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_DBCoffee.sql snippet=solution} |
|
4773 |
|
``` |
4717 |
4774 |
|
|
4718 |
4775 |
--- |
--- |
4719 |
4776 |
|
|
4720 |
4777 |
Solution to [%D %n (%T)](#problem:DepartmentSelect) |
Solution to [%D %n (%T)](#problem:DepartmentSelect) |
4721 |
4778 |
|
|
4722 |
4779 |
~ |
~ |
|
4780 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
4781 |
|
<!-- Plus, for this case, restore the list. --> |
|
4782 |
|
|
4723 |
4783 |
|
|
4724 |
|
#. ```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Department.sql snippet=solution1} |
|
4725 |
|
``` |
|
4726 |
|
|
|
4727 |
|
#. ```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Department.sql snippet=solution2} |
|
4728 |
|
``` |
|
4729 |
|
|
|
4730 |
|
#. ```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Department.sql snippet=solution3} |
|
4731 |
|
``` |
|
|
4784 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Department.sql snippet=solution1} |
|
4785 |
|
``` |
|
4786 |
|
|
|
4787 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Department.sql snippet=solution2} |
|
4788 |
|
``` |
|
4789 |
|
|
|
4790 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Department.sql snippet=solution3} |
|
4791 |
|
``` |
4732 |
4792 |
--- |
--- |
4733 |
4793 |
|
|
4734 |
4794 |
Solution to [%D %n (%T)](#problem:ComputerSelectBis) |
Solution to [%D %n (%T)](#problem:ComputerSelectBis) |
4735 |
4795 |
|
|
4736 |
4796 |
~ |
~ |
4737 |
4797 |
|
|
4738 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ComputerVariation.sql snippet=solution} |
|
4739 |
|
``` |
|
|
4798 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
4799 |
|
|
|
4800 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ComputerVariation.sql snippet=solution} |
|
4801 |
|
``` |
4740 |
4802 |
|
|
4741 |
4803 |
--- |
--- |
4742 |
4804 |
|
|
4743 |
4805 |
Solution to [%D %n (%T)](#problem:SocialMedia) |
Solution to [%D %n (%T)](#problem:SocialMedia) |
4744 |
4806 |
~ |
~ |
4745 |
4807 |
|
|
4746 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_SocialMedia.sql snippet=solution} |
|
4747 |
|
``` |
|
|
4808 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
4809 |
|
|
|
4810 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_SocialMedia.sql snippet=solution} |
|
4811 |
|
``` |
4748 |
4812 |
|
|
4749 |
4813 |
|
|
4750 |
4814 |
--- |
--- |
|
... |
... |
Solution to [%D %n (%T)](#problem:certificate) |
4898 |
4962 |
~ |
~ |
4899 |
4963 |
|
|
4900 |
4964 |
The solution can be read from the following code: |
The solution can be read from the following code: |
4901 |
|
|
|
4902 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Certificate.sql} |
|
4903 |
|
``` |
|
|
4965 |
|
|
|
4966 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
4967 |
|
|
|
4968 |
|
|
|
4969 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Certificate.sql} |
|
4970 |
|
``` |
4904 |
4971 |
|
|
4905 |
4972 |
|
|
4906 |
4973 |
--- |
--- |
|
... |
... |
Solution to [%D %n (%T)](#problem:sqlWorks) |
4926 |
4993 |
|
|
4927 |
4994 |
The solution to the next questions can be read from the following code: |
The solution to the next questions can be read from the following code: |
4928 |
4995 |
|
|
4929 |
|
|
|
4930 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Work.sql snippet=solution} |
|
4931 |
|
``` |
|
4932 |
|
|
|
4933 |
|
@problem:sqlWorks -- Solution to Q. -.# |
|
4934 |
|
~ |
|
4935 |
|
|
|
4936 |
|
Finally, to answer the last question, here is a list of the possible limitations: |
|
4937 |
|
|
|
4938 |
|
#. Having the name or the title as a primary key (in the `AUTHOR` and `WORK` tables) is not a good idea: we cannot have two authors with the same name or two pieces of work with the same title! |
|
4939 |
|
#. If all the attributes in the `BOOK` and the `EBOOK` tables are going to be the same, then we should probably have only one table called `PUBLICATION` with a boolean to indicate whenever the publication is digital or on paper. |
|
4940 |
|
#. Having a mix of `ON DELETE CASCADE` and `ON DELETE RESTRICT` is not really justified and makes the tables harder to use. We should have used the same update policy on both tables. |
|
4941 |
|
|
|
|
4996 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
4997 |
|
|
|
4998 |
|
|
|
4999 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Work.sql snippet=solution} |
|
5000 |
|
``` |
|
5001 |
|
|
|
5002 |
|
@problem:sqlWorks -- Solution to Q. -.# |
|
5003 |
|
~ |
|
5004 |
|
|
|
5005 |
|
Finally, to answer the last question, here is a list of the possible limitations: |
|
5006 |
|
|
|
5007 |
|
#. Having the name or the title as a primary key (in the `AUTHOR` and `WORK` tables) is not a good idea: we cannot have two authors with the same name or two pieces of work with the same title! |
|
5008 |
|
#. If all the attributes in the `BOOK` and the `EBOOK` tables are going to be the same, then we should probably have only one table called `PUBLICATION` with a boolean to indicate whenever the publication is digital or on paper. |
|
5009 |
|
#. Having a mix of `ON DELETE CASCADE` and `ON DELETE RESTRICT` is not really justified and makes the tables harder to use. We should have used the same update policy on both tables. |
|
5010 |
|
|
4942 |
5011 |
--- |
--- |
4943 |
5012 |
|
|
4944 |
5013 |
Solution to [%D %n (%T)](#problem:project1) |
Solution to [%D %n (%T)](#problem:project1) |
4945 |
5014 |
~ |
~ |
4946 |
5015 |
The answers can be found in the following snippet: |
The answers can be found in the following snippet: |
4947 |
5016 |
|
|
4948 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_TextbookAuthoredSol.sql snippet=solution} |
|
4949 |
|
``` |
|
|
5017 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
5018 |
|
|
|
5019 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_TextbookAuthoredSol.sql snippet=solution} |
|
5020 |
|
``` |
4950 |
5021 |
|
|
4951 |
5022 |
--- |
--- |
4952 |
5023 |
|
|
4953 |
5024 |
Solution to [%D %n (%T)](#problem:project1bis) |
Solution to [%D %n (%T)](#problem:project1bis) |
4954 |
5025 |
~ |
~ |
4955 |
5026 |
The answers can be found in the following snippet: |
The answers can be found in the following snippet: |
4956 |
|
|
|
4957 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_CapstoneSol.sql snippet=solution} |
|
4958 |
|
``` |
|
|
5027 |
|
|
|
5028 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
5029 |
|
|
|
5030 |
|
|
|
5031 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_CapstoneSol.sql snippet=solution} |
|
5032 |
|
``` |
4959 |
5033 |
|
|
4960 |
5034 |
--- |
--- |
4961 |
5035 |
|
|
|
... |
... |
Solution to [%D %n (%T)](#problem:residency) |
5012 |
5086 |
@problem:residency -- Solution to Q. -.# |
@problem:residency -- Solution to Q. -.# |
5013 |
5087 |
~ |
~ |
5014 |
5088 |
|
|
5015 |
|
The answers can be found in the following snippet: |
|
5016 |
|
|
|
5017 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ResidencySol.sql snippet=solution4} |
|
5018 |
|
``` |
|
5019 |
|
|
|
5020 |
|
@problem:residency -- Solution to Q. -.# |
|
5021 |
|
~ |
|
5022 |
|
|
|
5023 |
5089 |
The answers can be found in the following snippet: |
The answers can be found in the following snippet: |
5024 |
5090 |
|
|
5025 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ResidencySol.sql snippet=solution5} |
|
5026 |
|
``` |
|
|
5091 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
5027 |
5092 |
|
|
5028 |
|
Note that the query that returns the name of the homeowners can be improved. |
|
5029 |
|
|
|
5030 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ResidencySol.sql snippet=homonyms} |
|
5031 |
|
``` |
|
|
5093 |
|
|
|
5094 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ResidencySol.sql snippet=solution4} |
|
5095 |
|
``` |
5032 |
5096 |
|
|
5033 |
|
@problem:residency -- Solution to Q. -.# |
|
5034 |
|
~ |
|
5035 |
|
|
|
5036 |
|
To update the `SSN` of `"James Baldwin"` to `"000-00-0010"`, we could use: |
|
5037 |
|
|
|
5038 |
|
``` |
|
5039 |
|
UPDATE PERSON SET SSN = "000-00-0010" WHERE FName = "James" AND LName = "Baldwin"; |
|
5040 |
|
``` |
|
|
5097 |
|
@problem:residency -- Solution to Q. -.# |
|
5098 |
|
~ |
5041 |
5099 |
|
|
5042 |
|
However, this command would be rejected because of the foreign key constraint. On `UPDATE`, the foreign key from `RESIDENCY.Person` to `PERSON.SSN` restricts by default. |
|
5043 |
|
The error would be: |
|
5044 |
|
``` |
|
5045 |
|
ERROR 1451 (23000) at line 75: Cannot delete or update a parent row: a foreign key constraint fails (`HW_Residency_SOL`.`RESIDENCY`, CONSTRAINT `RESIDENCY_ibfk_1` FOREIGN KEY (`Person`) REFERENCES `PERSON` (`SSN`)) |
|
5046 |
|
``` |
|
|
5100 |
|
The answers can be found in the following snippet: |
5047 |
5101 |
|
|
5048 |
|
@problem:residency -- Solution to Q. -.# |
|
5049 |
|
~ |
|
5050 |
|
|
|
5051 |
|
In our model, as it is currently, |
|
5052 |
|
|
|
5053 |
|
#. It is **possible** for two people to have the same last name. |
|
5054 |
|
#. It is **possible** for a person to have multiple principal residencies. |
|
5055 |
|
#. It is **not possible** for a house to not be yellow. |
|
5056 |
|
#. It is **possible** for the SSN to be any series of 11 characters. |
|
5057 |
|
#. It is **possible** for a person to own any number of houses. |
|
5058 |
|
#. It is **possible** for a person to rent any number of houses. |
|
|
5102 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ResidencySol.sql snippet=solution5} |
|
5103 |
|
``` |
5059 |
5104 |
|
|
5060 |
|
@problem:residency -- Solution to Q. -.# |
|
5061 |
|
~ |
|
5062 |
|
|
|
5063 |
|
Considering the given state for the `RESIDENCY` table, the following two are possible primary keys: |
|
5064 |
|
|
|
5065 |
|
#. `Person` and `PrincipalResidence` |
|
5066 |
|
#. `Person` and `House` |
|
|
5105 |
|
Note that the query that returns the name of the homeowners can be improved. |
5067 |
5106 |
|
|
5068 |
|
@problem:residency -- Solution to Q. -.# |
|
5069 |
|
~ |
|
|
5107 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ResidencySol.sql snippet=homonyms} |
|
5108 |
|
``` |
|
5109 |
|
|
|
5110 |
|
@problem:residency -- Solution to Q. -.# |
|
5111 |
|
~ |
|
5112 |
|
|
|
5113 |
|
To update the `SSN` of `"James Baldwin"` to `"000-00-0010"`, we could use: |
|
5114 |
|
|
|
5115 |
|
``` |
|
5116 |
|
UPDATE PERSON SET SSN = "000-00-0010" WHERE FName = "James" AND LName = "Baldwin"; |
|
5117 |
|
``` |
|
5118 |
|
|
|
5119 |
|
However, this command would be rejected because of the foreign key constraint. On `UPDATE`, the foreign key from `RESIDENCY.Person` to `PERSON.SSN` restricts by default. |
|
5120 |
|
The error would be: |
|
5121 |
|
``` |
|
5122 |
|
ERROR 1451 (23000) at line 75: Cannot delete or update a parent row: a foreign key constraint fails (`HW_Residency_SOL`.`RESIDENCY`, CONSTRAINT `RESIDENCY_ibfk_1` FOREIGN KEY (`Person`) REFERENCES `PERSON` (`SSN`)) |
|
5123 |
|
``` |
|
5124 |
|
|
|
5125 |
|
@problem:residency -- Solution to Q. -.# |
|
5126 |
|
~ |
|
5127 |
|
|
|
5128 |
|
In our model, as it is currently, |
|
5129 |
|
|
|
5130 |
|
#. It is **possible** for two people to have the same last name. |
|
5131 |
|
#. It is **possible** for a person to have multiple principal residencies. |
|
5132 |
|
#. It is **not possible** for a house to not be yellow. |
|
5133 |
|
#. It is **possible** for the SSN to be any series of 11 characters. |
|
5134 |
|
#. It is **possible** for a person to own any number of houses. |
|
5135 |
|
#. It is **possible** for a person to rent any number of houses. |
|
5136 |
|
|
|
5137 |
|
@problem:residency -- Solution to Q. -.# |
|
5138 |
|
~ |
|
5139 |
|
|
|
5140 |
|
Considering the given state for the `RESIDENCY` table, the following two are possible primary keys: |
5070 |
5141 |
|
|
5071 |
|
The first key would not accomodate a person with multiple secondary residencies, which is not a good thing. |
|
5072 |
|
The second key could make sense, since it would refrain a person from declaring the same address twice as their residency. |
|
5073 |
|
The only case that could be hard to work around is if a person was trying to own multiple units at the same address; however, this is more an issue with the primary key of `HOUSE` than an issue with the primary key we suggested for `RESIDENCY`. |
|
|
5142 |
|
#. `Person` and `PrincipalResidence` |
|
5143 |
|
#. `Person` and `House` |
|
5144 |
|
|
|
5145 |
|
@problem:residency -- Solution to Q. -.# |
|
5146 |
|
~ |
|
5147 |
|
|
|
5148 |
|
The first key would not accomodate a person with multiple secondary residencies, which is not a good thing. |
|
5149 |
|
The second key could make sense, since it would refrain a person from declaring the same address twice as their residency. |
|
5150 |
|
The only case that could be hard to work around is if a person was trying to own multiple units at the same address; however, this is more an issue with the primary key of `HOUSE` than an issue with the primary key we suggested for `RESIDENCY`. |
5074 |
5151 |
|
|
5075 |
5152 |
|
|
5076 |
5153 |
--- |
--- |
|
... |
... |
Solution to [%D %n (%T)](#problem:residency) |
5078 |
5155 |
Solution to [%D %n (%T)](#problem:research-funding) |
Solution to [%D %n (%T)](#problem:research-funding) |
5079 |
5156 |
~ |
~ |
5080 |
5157 |
(Some of) the answers can be found in the following snippet: |
(Some of) the answers can be found in the following snippet: |
|
5158 |
|
<!-- Bug, this code should be indented to be in the current environment. --> |
|
5159 |
|
|
5081 |
5160 |
|
|
5082 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ScientificResearchSol.sql snippet=solution} |
|
5083 |
|
``` |
|
|
5161 |
|
```{.sqlmysql .numberLines .includeLink include=code/sql/HW_ScientificResearchSol.sql snippet=solution} |
|
5162 |
|
``` |
5084 |
5163 |
|
|
5085 |
5164 |
|
|
5086 |
5165 |
# Designing a Good Database |
# Designing a Good Database |