File notes/code/sql/WORK_SOL.sql added (mode: 100644) (index 0000000..ed5df5b) |
|
1 |
|
/* code/sql/WORK_SOL.sql */ |
|
2 |
|
|
|
3 |
|
-- For this code to work, you need to execute |
|
4 |
|
-- the code in |
|
5 |
|
-- code/sql/WORK.sql |
|
6 |
|
-- first. |
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
/* |
|
11 |
|
* |
|
12 |
|
* Determine if the following insertion statements would violate the the Entity integrity constraint, the Referential integrity constraint, if there would be some Other kind of error, or if it would result in uccessful insertion. |
|
13 |
|
* |
|
14 |
|
*/ |
|
15 |
|
|
|
16 |
|
START TRANSACTION; -- We don't want to perform the actual insertions. |
|
17 |
|
|
|
18 |
|
INSERT INTO EBOOK VALUES(0, NULL, 20180101, 0); |
|
19 |
|
-- Query OK, 1 row affected (0.003 sec) |
|
20 |
|
-- So, "Successful insertion". |
|
21 |
|
|
|
22 |
|
INSERT INTO AUTHOR VALUES("Mary B.", "mb@fai.fr", NULL); |
|
23 |
|
-- ERROR 1136 (21S01): Column count doesn't match value count at row 1 |
|
24 |
|
-- So, "Other kind of error". |
|
25 |
|
|
|
26 |
|
INSERT INTO WORK VALUES("My Life", "Claude A."); |
|
27 |
|
-- ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`HW_EXAM_1`.`WORK`, CONSTRAINT `WORK_ibfk_1` FOREIGN KEY (`Author`) REFERENCES `AUTHOR` (`Name`) ON DELETE CASCADE ON UPDATE CASCADE) |
|
28 |
|
-- So, "Referential integrity constraint" |
|
29 |
|
|
|
30 |
|
INSERT INTO BOOK VALUES(00000000, NULL, DATE'20001225', 90.9); |
|
31 |
|
-- Query OK, 1 row affected (0.000 sec) |
|
32 |
|
-- So, "Successful insertion". |
|
33 |
|
|
|
34 |
|
INSERT INTO AUTHOR VALUES("Virginia W.", "alt@isp.net"); |
|
35 |
|
-- ERROR 1062 (23000): Duplicate entry 'Virginia W.' for key 'PRIMARY' |
|
36 |
|
-- So, "Entity integrity constraint". |
|
37 |
|
|
|
38 |
|
ROLLBACK; -- We go back to the previous state. |
|
39 |
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
|
/* |
|
44 |
|
* |
|
45 |
|
* List the rows (i.e., A.2, W.1, etc.) modified by the following statements (be careful about the conditions on foreign keys!): |
|
46 |
|
* |
|
47 |
|
*/ |
|
48 |
|
|
|
49 |
|
|
|
50 |
|
START TRANSACTION; -- We don't want to perform the following operations. |
|
51 |
|
|
|
52 |
|
UPDATE AUTHOR SET Email = 'Deprecated' WHERE Email LIKE '%isp.net'; |
|
53 |
|
-- Query OK, 2 rows affected (0.010 sec) |
|
54 |
|
-- Rows matched: 2 Changed: 2 Warnings: 0 |
|
55 |
|
-- This changed A.1 and A.2 |
|
56 |
|
|
|
57 |
|
UPDATE WORK SET Title = "How to eat" WHERE Title = "What to eat"; |
|
58 |
|
-- Rows matched: 1 Changed: 1 Warnings: 0 |
|
59 |
|
-- SQL returns only the number of row changed in the WORK table, |
|
60 |
|
-- but other rows have been changed as well. |
|
61 |
|
-- This changed W.1, B.1, E.1. |
|
62 |
|
|
|
63 |
|
DELETE FROM WORK; |
|
64 |
|
-- ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`HW_EXAM_1`.`BOOK`, CONSTRAINT `BOOK_ibfk_1` FOREIGN KEY (`Work`) REFERENCES `WORK` (`Title`) ON UPDATE CASCADE) |
|
65 |
|
-- Does not change any row. |
|
66 |
|
|
|
67 |
|
DELETE FROM AUTHOR WHERE Name = "Virginia W."; |
|
68 |
|
-- ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`HW_EXAM_1`.`BOOK`, CONSTRAINT `BOOK_ibfk_1` FOREIGN KEY (`Work`) REFERENCES `WORK` (`Title`) ON UPDATE CASCADE) |
|
69 |
|
-- Does not change any row. |
|
70 |
|
|
|
71 |
|
ROLLBACK; -- We go back to the previous state. |
|
72 |
|
|
|
73 |
|
/* |
|
74 |
|
* |
|
75 |
|
* You can now assume that there is more data than what we inserted, if that helps you. Write a command that selects … |
|
76 |
|
* |
|
77 |
|
*/ |
|
78 |
|
|
|
79 |
|
-- We insert some dummy values for this next part. |
|
80 |
|
INSERT INTO WORK VALUES("My Life", "Paul B."), ("What to eat, 2", "Virginia W."); |
|
81 |
|
INSERT INTO BOOK VALUES(15355627, "My Life", DATE'20180219', 15.00), (12912912, "What to eat, 2", DATE'20200101', 13); |
|
82 |
|
INSERT INTO EBOOK VALUES(15150628, "My Life", DATE'20190215', 10.89), (42912912, "What to eat, 2", DATE'20200115', 12); |
|
83 |
|
|
|
84 |
|
-- … the price of all the ebooks. |
|
85 |
|
SELECT Price FROM EBOOK; |
|
86 |
|
|
|
87 |
|
-- … the (distinct) names of the authors who have authored a piece of work. |
|
88 |
|
SELECT DISTINCT Author FROM WORK; |
|
89 |
|
|
|
90 |
|
-- … the name of the authors using fai.fr for their email. |
|
91 |
|
SELECT Name FROM AUTHOR WHERE Email LIKE '%fai.fr'; |
|
92 |
|
|
|
93 |
|
-- … the price of the ebooks published after 2018. |
|
94 |
|
SELECT Price FROM BOOK WHERE Published >= 20180101; |
|
95 |
|
-- Note that |
|
96 |
|
-- SELECT Price FROM BOOK WHERE Published > 2018; |
|
97 |
|
-- would return all the prices, along with a warning: |
|
98 |
|
-- Incorrect datetime value: '2018' |
|
99 |
|
|
|
100 |
|
-- … the price of the most expensive book. |
|
101 |
|
SELECT MAX(Price) FROM BOOK; |
|
102 |
|
|
|
103 |
|
-- … the number of pieces of work written by the author whose name is “Virginia W.”. |
|
104 |
|
SELECT COUNT(*) FROM WORK WHERE WORK.Author = "Virginia W."; |
|
105 |
|
|
|
106 |
|
-- … the email of the author who wrote the piece of work called “My Life”. |
|
107 |
|
SELECT Email FROM AUTHOR, WORK WHERE WORK.Title = "My Life" AND WORK.Author = AUTHOR.Name; |
|
108 |
|
|
|
109 |
|
-- the isbn(s) of the book containing a work written by the author whose email is "vw@isp.net". |
|
110 |
|
SELECT ISBN FROM BOOK, WORK, AUTHOR WHERE AUTHOR.Email = "vw@isp.net" AND WORK.Author = AUTHOR.Name AND BOOK.Work = WORK.Title; |
|
111 |
|
|
|
112 |
|
/* |
|
113 |
|
* |
|
114 |
|
* Write a command that updates the title of all the pieces of work written by the author whose name is “Virginia W. to”BANNED". Is there any reason for this command to be rejected by the system? If yes, explain which one. |
|
115 |
|
* |
|
116 |
|
*/ |
|
117 |
|
|
|
118 |
|
UPDATE WORK SET Title = "BANNED" WHERE Author = "Virginia W."; |
|
119 |
|
-- Does not give an error with the that we currently have. |
|
120 |
|
-- However, since "Title" is the primary key in the WORK table, if Virginia W. had authored two pieces of work or more, then this command would give an error. |
|
121 |
|
|
|
122 |
|
/* |
|
123 |
|
* |
|
124 |
|
* Write one or multiple commands that would delete the work whose title is “My Life”, as well as all of the books and ebooks versions of it. |
|
125 |
|
* |
|
126 |
|
*/ |
|
127 |
|
|
|
128 |
|
DELETE FROM WORK WHERE Title = "My Life"; |
|
129 |
|
-- Fails |
|
130 |
|
-- ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`HW_EXAM_1`.`BOOK`, CONSTRAINT `BOOK_ibfk_1` FOREIGN KEY (`Work`) REFERENCES `WORK` (`Title`) ON UPDATE CASCADE) |
|
131 |
|
-- We have to first delete the corresponding publications: |
|
132 |
|
|
|
133 |
|
DELETE FROM BOOK WHERE Work = "My Life"; |
|
134 |
|
DELETE FROM EBOOK WHERE Work = "My Life"; |
|
135 |
|
-- And then we can delete the work: |
|
136 |
|
DELETE FROM WORK WHERE Title = "My Life"; |
|
137 |
|
-- And, no, we cannot delete "simply" from multiple tables in one command. |
|
138 |
|
-- Some workaround exists, cf. https://stackoverflow.com/q/1233451/ . |
File notes/lectures_notes.md changed (mode: 100644) (index 533eb31..bf8d878) |
... |
... |
marks the (usual) separation between two lectures. |
87 |
87 |
|
|
88 |
88 |
To give you a sense of what you will be asked to do during the exams, please find below a description of the exams given previous semesters. |
To give you a sense of what you will be asked to do during the exams, please find below a description of the exams given previous semesters. |
89 |
89 |
|
|
90 |
|
### Fall 2017 {-} |
|
|
90 |
|
### Spring 2018 {-} |
91 |
91 |
|
|
92 |
92 |
- Exam #1:^[This exam was probably a bit too long, but students managed it pretty well.] |
- Exam #1:^[This exam was probably a bit too long, but students managed it pretty well.] |
93 |
93 |
- Six small exercises (@exercise:programdataindependence, @exercise:schemastate, @exercise:entityintegrity, @exercise:sqldatatype, @exercise:explainsql and @exercise:explainfk) |
- Six small exercises (@exercise:programdataindependence, @exercise:schemastate, @exercise:entityintegrity, @exercise:sqldatatype, @exercise:explainsql and @exercise:explainfk) |
|
... |
... |
To give you a sense of what you will be asked to do during the exams, please fin |
108 |
108 |
- [%D %n (%T)](#problem:book) |
- [%D %n (%T)](#problem:book) |
109 |
109 |
- [%D %n (%T)](#problem:consultation) |
- [%D %n (%T)](#problem:consultation) |
110 |
110 |
|
|
111 |
|
### Spring 2018 {-} |
|
|
111 |
|
### Fall 2018 {-} |
112 |
112 |
|
|
113 |
113 |
- Exam #1: |
- Exam #1: |
114 |
114 |
- [%D %n (%T)](#problem:cinema), except that I gave some of the relations and attributes, to help getting started with the problem. |
- [%D %n (%T)](#problem:cinema), except that I gave some of the relations and attributes, to help getting started with the problem. |
|
... |
... |
To give you a sense of what you will be asked to do during the exams, please fin |
128 |
128 |
- A variation on [%D %n (%T)](#problem:BusinessToEr) |
- A variation on [%D %n (%T)](#problem:BusinessToEr) |
129 |
129 |
- A variation on [%D %n (%T)](#problem:UMLtoRELDriver): students were asked to draw the ER diagram for that schema. |
- A variation on [%D %n (%T)](#problem:UMLtoRELDriver): students were asked to draw the ER diagram for that schema. |
130 |
130 |
|
|
131 |
|
### Fall 2019 {-} |
|
|
131 |
|
### Spring 2019 {-} |
132 |
132 |
|
|
133 |
133 |
- Exam #1: |
- Exam #1: |
134 |
134 |
- [%D %n (%T)](#problem:sqlBooks) |
- [%D %n (%T)](#problem:sqlBooks) |
|
... |
... |
To give you a sense of what you will be asked to do during the exams, please fin |
147 |
147 |
- [%D %n (%T)](#problem:library_network) |
- [%D %n (%T)](#problem:library_network) |
148 |
148 |
- Five exercises from the [Database Application](#databases-applications) chapter. |
- Five exercises from the [Database Application](#databases-applications) chapter. |
149 |
149 |
|
|
150 |
|
|
|
|
150 |
|
|
|
151 |
|
### Fall 2019 {-} |
|
152 |
|
|
|
153 |
|
- Exam #1: |
|
154 |
|
- [%D %n (%T)](#problem:sqlWorks) |
|
155 |
|
- Three exercises (@exercise:defaultForeign, @exercise:truthTableAnd but for the `OR` operator, and @exercise:NullMeaning) |
|
156 |
|
- [%D %n (%T)](#problem:rel_model_universities) |
|
157 |
|
|
|
158 |
|
|
151 |
159 |
## Typesetting and Acknowledgments {-} |
## Typesetting and Acknowledgments {-} |
152 |
160 |
|
|
153 |
161 |
The source code for those notes is hosted at [rocketgit](https://rocketgit.com/user/caubert/CSCI_3410), typeset in markdown, and then compiled using [pandoc](http://pandoc.org/) and multiple filters ([pandoc-numbering](https://github.com/chdemko/pandoc-numbering), [pandoc-citeproc](https://github.com/jgm/pandoc-citeproc), [pandoc-include-code](https://github.com/owickstrom/pandoc-include-code)). |
The source code for those notes is hosted at [rocketgit](https://rocketgit.com/user/caubert/CSCI_3410), typeset in markdown, and then compiled using [pandoc](http://pandoc.org/) and multiple filters ([pandoc-numbering](https://github.com/chdemko/pandoc-numbering), [pandoc-citeproc](https://github.com/jgm/pandoc-citeproc), [pandoc-include-code](https://github.com/owickstrom/pandoc-include-code)). |
|
... |
... |
Problem (Relational model for a cinema company) +.#cinema |
1083 |
1091 |
- Furthermore, each theater has a set of auditoriums. Each auditorium has a unique number, and seating capacity. |
- Furthermore, each theater has a set of auditoriums. Each auditorium has a unique number, and seating capacity. |
1084 |
1092 |
- Each theater can schedule movies at show-times. Each show-time has a unique id, a start time, and is for a specific movie, at a theater auditorium. |
- Each theater can schedule movies at show-times. Each show-time has a unique id, a start time, and is for a specific movie, at a theater auditorium. |
1085 |
1093 |
- The company sells tickets for scheduled show-times. Each ticket has a unique ticket id, and a price. |
- The company sells tickets for scheduled show-times. Each ticket has a unique ticket id, and a price. |
1086 |
|
|
|
1087 |
1094 |
--- |
--- |
1088 |
1095 |
|
|
1089 |
1096 |
Problem (Relational model for bills) +.#rel_model_bills |
Problem (Relational model for bills) +.#rel_model_bills |
|
... |
... |
Problem (Relational model for bills) +.#rel_model_bills |
1095 |
1102 |
- You also want to record the name of the Speaker, Majority Leader, Minority Leader, Majority Whip, and Minority Whip, which are all members of the house. |
- You also want to record the name of the Speaker, Majority Leader, Minority Leader, Majority Whip, and Minority Whip, which are all members of the house. |
1096 |
1103 |
- Finally, for each bill, you want to record the vote of every member of the house. |
- Finally, for each bill, you want to record the vote of every member of the house. |
1097 |
1104 |
|
|
|
1105 |
|
--- |
|
1106 |
|
|
|
1107 |
|
Problem (Relational model for universities) +.#rel_model_universities |
|
1108 |
|
~ |
|
1109 |
|
|
|
1110 |
|
Propose a relational model for the following situation: |
|
1111 |
|
|
|
1112 |
|
- You want to store information about multiple universities. A university has multiple departments, a name and a website. |
|
1113 |
|
- Each department offers multiple courses. A course has a name, one (or multiple, when it is cross-listed) code, a number of credit hours. |
|
1114 |
|
- A campus has a name, an address, and belong to one university. |
|
1115 |
|
- A department has a contact address, a date of creation and a (unique) code. |
|
1116 |
|
|
1098 |
1117 |
## Solution to Selected Problems {-} |
## Solution to Selected Problems {-} |
1099 |
1118 |
|
|
1100 |
1119 |
Solution to @problem:cinema |
Solution to @problem:cinema |
|
... |
... |
Solution to @problem:rel_model_bills |
1128 |
1147 |
|
|
1129 |
1148 |
For simplicity, we added an `Id` to our `MEMBER` and `BILL` relations. Note that having a "role" in the `MEMBER` relation to store the information about speaker, etc., would be extremely inefficient, since we would add an attribute to the ~435 members that would be `NULL` in ~430 of them. |
For simplicity, we added an `Id` to our `MEMBER` and `BILL` relations. Note that having a "role" in the `MEMBER` relation to store the information about speaker, etc., would be extremely inefficient, since we would add an attribute to the ~435 members that would be `NULL` in ~430 of them. |
1130 |
1149 |
|
|
|
1150 |
|
--- |
|
1151 |
|
|
|
1152 |
|
Solution to @problem:rel_model_universities |
|
1153 |
|
|
|
1154 |
|
~ |
|
1155 |
|
|
|
1156 |
|
A possible solution follows. |
|
1157 |
|
The part that is the hardest to accomodate is the fact that a course can have multiple codes. |
|
1158 |
|
We are reading here "cross-listed" as "a course that is offered under more than one departmental heading and can receive different codes (e.g., CSCI XXXX and AIST YYYY)". |
|
1159 |
|
|
|
1160 |
|
![ |
|
1161 |
|
UNIVERSITY (Name (PK), Website) |
|
1162 |
|
CAMPUS (Address (PK), University (FK to UNIVERSITY.Name)) |
|
1163 |
|
DEPARTMENT (Code (PK), Contact, CreationDate, University (FK to UNIVERSITY.Name)) |
|
1164 |
|
COURSE (Name (PK), CreditHours) |
|
1165 |
|
OFFERING (Department (PK, FK to DEPARTMENT.Name), Course (PK, FK to COURSE.Name), Code) |
|
1166 |
|
](fig/rel_mod/UNIVERSITIES) |
|
1167 |
|
\ |
|
1168 |
|
|
|
1169 |
|
|
1131 |
1170 |
# The SQL Programming Language |
# The SQL Programming Language |
1132 |
1171 |
|
|
1133 |
1172 |
## Resources {-} |
## Resources {-} |
|
... |
... |
Exercise +.# |
2471 |
2510 |
|
|
2472 |
2511 |
: If you want that every time a referenced row is delted, all the refering rows are deleted as well, what mechanism should you use? |
: If you want that every time a referenced row is delted, all the refering rows are deleted as well, what mechanism should you use? |
2473 |
2512 |
|
|
|
2513 |
|
Exercise +.#defaultForeign |
|
2514 |
|
|
|
2515 |
|
: By default, does the foreign key restrict, cascade, or set null on update? Can you justify this choice? |
|
2516 |
|
|
2474 |
2517 |
Exercise +.# |
Exercise +.# |
2475 |
2518 |
|
|
2476 |
2519 |
: If a database designer is using the `ON UPDATE SET NULL` for a foreign key, what mechanism is (s)he implementing (i.e., describe how the database will react a certain operation)? |
: If a database designer is using the `ON UPDATE SET NULL` for a foreign key, what mechanism is (s)he implementing (i.e., describe how the database will react a certain operation)? |
|
... |
... |
Exercise +.# |
2576 |
2619 |
|
|
2577 |
2620 |
: Can an `UPDATE` statement have a `WHERE` condition using an attribute that isn't the primary key? If no, justify, if yes, tell what could happen. |
: Can an `UPDATE` statement have a `WHERE` condition using an attribute that isn't the primary key? If no, justify, if yes, tell what could happen. |
2578 |
2621 |
|
|
2579 |
|
Exercise +.# |
|
|
2622 |
|
Exercise +.#NullMeaning |
2580 |
2623 |
|
|
2581 |
|
: What are the possible meanings or interpretations for a `NULL` value? |
|
|
2624 |
|
: Give the three possible meaning of the `NULL` value, and an example for each of them. |
2582 |
2625 |
|
|
2583 |
2626 |
Exercise +.# |
Exercise +.# |
2584 |
2627 |
~ |
~ |
|
... |
... |
Solution +.# |
2720 |
2763 |
|
|
2721 |
2764 |
Solution +.# |
Solution +.# |
2722 |
2765 |
|
|
|
2766 |
|
: By default, the foreign key restricts updates. This prevents unwanted update of information: if an update needs to be propagated, then it needs to be "acknowledged" and done explicitely. |
|
2767 |
|
|
|
2768 |
|
Solution +.# |
|
2769 |
|
|
2723 |
2770 |
: If the referenced row is updated, then the attribute of the referencing rows are set to `NULL`{.sqlmysql}. |
: If the referenced row is updated, then the attribute of the referencing rows are set to `NULL`{.sqlmysql}. |
2724 |
2771 |
|
|
2725 |
2772 |
Solution +.# |
Solution +.# |
|
... |
... |
Solution +.# |
2800 |
2847 |
|
|
2801 |
2848 |
Solution +.# |
Solution +.# |
2802 |
2849 |
|
|
2803 |
|
: Unknown value, unavailable / withheld, N/A. |
|
|
2850 |
|
: Unknown value ("Will it rain tomorrow?"), unavailable / withheld ("What is the phone number of Harry Belafonte?"), N/A ("What is the email address of Abraham Lincoln?"). |
2804 |
2851 |
|
|
2805 |
2852 |
Solution +.# |
Solution +.# |
2806 |
2853 |
~ |
~ |
|
... |
... |
Solution +.# |
2822 |
2869 |
- `FALSE AND UNKNOWN`{.sqlmysql} → `FALSE`{.sqlmysql} |
- `FALSE AND UNKNOWN`{.sqlmysql} → `FALSE`{.sqlmysql} |
2823 |
2870 |
- The other cases can be deduced by symmetry. |
- The other cases can be deduced by symmetry. |
2824 |
2871 |
|
|
2825 |
|
For a more compact presentation, refer to [the three-valued truth table"](#truth-tables). |
|
|
2872 |
|
For a more compact presentation, refer to [the three-valued truth table](#truth-tables). |
2826 |
2873 |
|
|
2827 |
2874 |
Solution +.# |
Solution +.# |
2828 |
2875 |
|
|
|
... |
... |
Problem (A simple database for books) +.#sqlBooks |
3413 |
3460 |
#. Draw the relational model of the database you created (i.e., including all the relations given in the code and the one you added). |
#. Draw the relational model of the database you created (i.e., including all the relations given in the code and the one you added). |
3414 |
3461 |
#. Discuss two limitations of the model and how to improve it. |
#. Discuss two limitations of the model and how to improve it. |
3415 |
3462 |
|
|
|
3463 |
|
|
|
3464 |
|
--- |
|
3465 |
|
|
|
3466 |
|
Problem (A simple database for published pieces of work) +.#sqlWorks |
|
3467 |
|
~ |
|
3468 |
|
|
|
3469 |
|
Consider the following code: |
|
3470 |
|
|
|
3471 |
|
```{.sqlmysql .numberLines include=code/sql/WORK.sql} |
|
3472 |
|
``` |
|
3473 |
|
|
|
3474 |
|
You should assume that |
|
3475 |
|
|
|
3476 |
|
#. Every statement respects SQL's syntax (there's no "a semi-colon is missing" trap). |
|
3477 |
|
#. None of the commands in the rest of this problem are actually executed, you are asked to answer "what if" questions. |
|
3478 |
|
|
|
3479 |
|
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.). |
|
3480 |
|
|
|
3481 |
|
- Draw the relational model corresponding to this series of commands. |
|
3482 |
|
- Determine if the following insertion statements would violate the the Entity integrity constraint, the Referential integrity constraint, if there would be some Other kind of error, or if it would result in \textbf{S}uccessful insertion. |
|
3483 |
|
|
|
3484 |
|
~~~{.sqlmysql .numberLines} |
|
3485 |
|
INSERT INTO EBOOK VALUES (0, NULL, 20180101, 0); |
|
3486 |
|
INSERT INTO AUTHOR VALUES("Mary B.", "mb@fai.fr", NULL); |
|
3487 |
|
INSERT INTO WORK VALUES("My Life", "Claude A."); |
|
3488 |
|
INSERT INTO BOOK VALUES(00000000, NULL, DATE'20001225', 90.9); |
|
3489 |
|
INSERT INTO AUTHOR VALUES("Virginia W.", "alt@isp.net"); |
|
3490 |
|
~~~ |
|
3491 |
|
|
|
3492 |
|
- List the rows (i.e., A.2, W.1, etc.) modified by the following statements (be careful about the conditions on foreign keys!): |
|
3493 |
|
|
|
3494 |
|
~~~{.sqlmysql .numberLines} |
|
3495 |
|
UPDATE AUTHOR SET Email = 'Deprecated' WHERE Email LIKE '%isp.net'; |
|
3496 |
|
UPDATE WORK SET Title = "How to eat" WHERE Title = "What to eat"; |
|
3497 |
|
DELETE FROM WORK; |
|
3498 |
|
DELETE FROM AUTHOR WHERE Name = "Virginia W."; |
|
3499 |
|
~~~ |
|
3500 |
|
|
|
3501 |
|
- You can now assume that there is more data than what we inserted, if that helps you. Write a command that selects … |
|
3502 |
|
- … the price of all the ebooks. |
|
3503 |
|
- … the (distinct) names of the authors who have authored a piece of work. |
|
3504 |
|
- … the name of the authors using fai.fr for their email. |
|
3505 |
|
- … the price of the ebooks published after 2018. |
|
3506 |
|
- … the price of the most expensive book. |
|
3507 |
|
- … the number of pieces of work written by the author whose name is "Virginia W.". |
|
3508 |
|
- … the email of the author who wrote the piece of work called "My Life". |
|
3509 |
|
- … the isbn(s) of the book containing a work written by the author whose email is "vw@isp.net". |
|
3510 |
|
|
|
3511 |
|
- Write a command that updates the title of all the pieces of work written by the author whose name is "Virginia W. to "BANNED". |
|
3512 |
|
Is there any reason for this command to be rejected by the system? |
|
3513 |
|
If yes, explain which one. |
|
3514 |
|
- Write one or multiple commands that would delete the work whose title is "My Life", as well as all of the books and ebooks versions of it. |
|
3515 |
|
- Discuss two limitations of the model and how to improve it. |
|
3516 |
|
|
3416 |
3517 |
## Solution to Selected Problems {-} |
## Solution to Selected Problems {-} |
3417 |
3518 |
|
|
3418 |
3519 |
|
|
|
... |
... |
Solution to [%D %n (%T)](#problem:sqlBooks) |
3861 |
3962 |
- The choice of the primary key for the `BOOK` relation: two books with the same title cannot be published on the same day, and that is a serious limitation. Using a primary key like ISBN would be much more appropriate. |
- The choice of the primary key for the `BOOK` relation: two books with the same title cannot be published on the same day, and that is a serious limitation. Using a primary key like ISBN would be much more appropriate. |
3862 |
3963 |
- The impossibility to deal with books written by multiple authors or published by multiple publishers. We could address this by having two separate tables, `IS_THE_AUTHOR_OF` and `PUBLISHED_BY`, that "maps" book's ISBN with author's or editor's primary key. |
- The impossibility to deal with books written by multiple authors or published by multiple publishers. We could address this by having two separate tables, `IS_THE_AUTHOR_OF` and `PUBLISHED_BY`, that "maps" book's ISBN with author's or editor's primary key. |
3863 |
3964 |
|
|
|
3965 |
|
--- |
|
3966 |
|
|
|
3967 |
|
Solution to [%D %n (%T)](#problem:sqlWorks) |
|
3968 |
|
~ The relational model for this code is: |
|
3969 |
|
|
|
3970 |
|
![ |
|
3971 |
|
WORK(Title (PK), Author (FK to AUTHOR.Name)) |
|
3972 |
|
AUTHOR(Name (PK), Email) |
|
3973 |
|
BOOK(ISBN (PK), Work (FK to WORK.Title), Published, Price) |
|
3974 |
|
EBOOK(ISBN (PK), Work (FK to WORK.Title), Published, Price) |
|
3975 |
|
](fig/rel_mod/WORK) |
|
3976 |
|
\ |
|
3977 |
|
|
|
3978 |
|
The solution to the next questions can be read from the following code: |
|
3979 |
|
|
|
3980 |
|
|
|
3981 |
|
```{.sqlmysql .numberLines include=code/sql/WORK_SOL.sql} |
|
3982 |
|
``` |
|
3983 |
|
|
|
3984 |
|
Finally, to answer the last question, we could list, among the possible limitations: |
|
3985 |
|
|
|
3986 |
|
- 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! |
|
3987 |
|
- 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 e.g. PUBLICATION, with a boolean to indicate whenever the publication is numeric or on paper. |
|
3988 |
|
- 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 the same update policy on both tables. |
|
3989 |
|
|
|
3990 |
|
|
3864 |
3991 |
# Designing a Good Database |
# Designing a Good Database |
3865 |
3992 |
|
|
3866 |
3993 |
## Resources {-} |
## Resources {-} |