List of commits:
Subject Hash Author Date (UTC)
Added project 1 and its solution. b7a31871ebccbf4caff81d9d57ed6847f4dc5543 caubert 2021-02-06 21:45:24
Added simple examples for SQL. 4ea62f6930ff19bcff8d406d95c9cd84c0f0216d caubert 2021-02-02 22:24:41
Added first project. e851058cc58a9d47ada9970f8b914e798c7d2ce7 caubert 2021-01-27 19:57:12
Few typos. 45ba15cd03fab4388439b617c656f52b2ec96319 caubert 2021-01-26 20:08:24
Small fix in code. 7860e391d0bbe82336b8f05585f846e829d181b1 caubert 2021-01-26 18:55:17
Fixed various bugs. fa76c1e469a4263d7bb7da79532abd1eedcc49be caubert 2021-01-22 19:27:01
First quizz, and fix bug with code displayed. bc9b6bb097e30186805b02dc1cdc5de2d5feabe4 caubert 2021-01-22 19:21:54
Edited the preamble. 364e709107602e163ea9365f25ccf1f5a6bb1914 caubert 2021-01-11 16:00:07
Started to edit notes and add final exam from fall 2020. bb747e27ce08ce17e91913e947614e3057580995 caubert 2021-01-06 22:26:07
Integrated the include-link feature of pandoc-include-code. 3a8f9ededdaa1f05d526742f5d447aaa017e1d1b caubert 2021-01-04 22:52:18
Fixed citeproc. 0d90c7e60e4a4474fd7ded03bb6526ea63a57253 aubert@math.cnrs.fr 2020-12-27 21:33:48
(Finally) updated pandoc and pandoc-numbering. 127c1964a0732b49e14b46fdb0d3f8446e3892d0 aubert@math.cnrs.fr 2020-12-27 21:14:31
Fixed url in comments, avoid them being wrapped. 09a6a78479f24b5749605a4cb7136f8cfbf57d30 aubert@math.cnrs.fr 2020-12-18 18:55:50
Added comments on procedures from code f3e9c2abac41d5de1c7473867c4f688051dec928 aubert@math.cnrs.fr 2020-12-01 15:46:53
Updated mysql connector 228d86db5498aae94a4d162272fa03a84ae9c532 aubert@math.cnrs.fr 2020-12-01 15:09:46
Updated java beautifier 30f4a79f2d911cd0b6a24c31f968f0d23620965f aubert@math.cnrs.fr 2020-12-01 15:07:50
Worked on procedure example from java 998ea14119a5a3f88efc4b0a126177e85151e43d aubert@math.cnrs.fr 2020-12-01 15:06:47
Brief example of calling a procedure from a program. 1054c9d5c83fb956bdd0b8d884d5ce2c8a9a640e aubert@math.cnrs.fr 2020-11-30 19:00:45
Added solution to second exam d8f62ded96991885a96d561db587988d453a28c8 aubert@math.cnrs.fr 2020-11-05 20:35:50
Quick fix on testing semicolons. 7f48d88d2ed69213d803a7736df3d50330cfecd1 aubert@math.cnrs.fr 2020-11-03 14:14:06
Commit b7a31871ebccbf4caff81d9d57ed6847f4dc5543 - Added project 1 and its solution.
Author: caubert
Author date (UTC): 2021-02-06 21:45
Committer name: caubert
Committer date (UTC): 2021-02-06 21:45
Parent(s): 4ea62f6930ff19bcff8d406d95c9cd84c0f0216d
Signer:
Signing key:
Signing status: N
Tree: b0ac756b2638767edd9ece82c2233f6aa28b8beb
File Lines added Lines deleted
notes/code/sql/HW_AdvancedFK.sql 18 15
notes/code/sql/HW_AutoIncrement.sql 16 6
notes/code/sql/HW_CapstoneSol.sql 33 33
notes/code/sql/HW_Certificate.sql 34 31
notes/code/sql/HW_ConstraintsPart1.sql 1 1
notes/code/sql/HW_DBCoffee.sql 14 13
notes/code/sql/HW_DefaultTest.sql 29 15
notes/code/sql/HW_Department.sql 1 1
notes/code/sql/HW_Faculty.sql 1 1
notes/code/sql/HW_ProcedureExamples.sql 4 4
notes/code/sql/HW_ProfExample.sql 2 2
notes/code/sql/HW_ResidencySol.sql 27 26
notes/code/sql/HW_ScientificResearchSol.sql 22 22
notes/code/sql/HW_SocialMedia.sql 9 9
notes/code/sql/HW_Storm.sql 27 27
notes/code/sql/HW_TriggerExample.sql 13 13
notes/code/sql/HW_Vaccine.sql 28 36
notes/code/sql/HW_VaccineSol.sql 225 0
notes/code/sql/HW_Work.sql 32 30
notes/lectures_notes.md 75 0
File notes/code/sql/HW_AdvancedFK.sql changed (mode: 100644) (index 365746b..19f6547)
... ... CREATE TABLE T2 (
19 19 B1 INT PRIMARY KEY, B1 INT PRIMARY KEY,
20 20 B2 INT, B2 INT,
21 21 -- We can create a "pair" of foreign key in one line, as -- We can create a "pair" of foreign key in one line, as
22 -- follows:
22 -- follows:
23 23 FOREIGN KEY (A1, A2) REFERENCES T1 (A1, A2), FOREIGN KEY (A1, A2) REFERENCES T1 (A1, A2),
24 24 -- We can create a foreign key that references the primary -- We can create a foreign key that references the primary
25 -- key of the table we are currently creating, and
26 -- name
27 -- it,
28 -- as follows:
25 -- key of the table we are currently creating, and
26 -- name
27 -- it,
28 -- as follows:
29 29 CONSTRAINT My_pk_to_T1 FOREIGN KEY (B2) REFERENCES T2 (B1) CONSTRAINT My_pk_to_T1 FOREIGN KEY (B2) REFERENCES T2 (B1)
30 30 ); );
31 31
32 32 -- The benefit of naming our fk constraint is that, if we -- The benefit of naming our fk constraint is that, if we
33 -- violate it, for instance with
34 -- INSERT INTO T2 VALUES (1, 1, 1, 3);
35 -- then the name of the constraint (here
36 -- would be displayed in the error message:
37 -- Cannot add or update a child row: a foreign key
38 -- constraint fails (`db_9_9837c1`.`t2`, CONSTRAINT
39 -- `My_pk_to_T1` FOREIGN KEY (`B2`) REFERENCES `t2`
40 -- (`B1`))
41 -- end snippet advancedFK
33 -- violate it, for instance with
34 -- INSERT INTO T2 VALUES (1, 1, 1, 3);
35 -- then the name of the constraint (here
36 -- "My_pk_to_T1")
37 -- would be displayed in the error message:
38 -- Cannot add or update a child row: a foreign
39 -- key
40 -- constraint fails (`db_9_9837c1`.`t2`,
41 -- CONSTRAINT
42 -- `My_pk_to_T1` FOREIGN KEY (`B2`) REFERENCES
43 -- `t2`
44 -- (`B1`))
45 -- end snippet advancedFK
File notes/code/sql/HW_AutoIncrement.sql changed (mode: 100644) (index c02e7ad..2003036)
1 1 DROP SCHEMA IF EXISTS HW_AutoIncrement; DROP SCHEMA IF EXISTS HW_AutoIncrement;
2
2 3 CREATE SCHEMA HW_AutoIncrement; CREATE SCHEMA HW_AutoIncrement;
4
3 5 USE HW_AutoIncrement; USE HW_AutoIncrement;
4 6
5 7 -- start snippet demo1 -- start snippet demo1
6 8 /* code/sql/HW_AutoIncrement.sql */ /* code/sql/HW_AutoIncrement.sql */
7 9 CREATE TABLE PERSON ( CREATE TABLE PERSON (
8 PersonID INT AUTO_INCREMENT,
9 Name VARCHAR(255),
10 PRIMARY KEY (PersonID)
10 PersonID INT AUTO_INCREMENT,
11 Name VARCHAR(255),
12 PRIMARY KEY (PersonID)
11 13 ); );
12 14
13 INSERT INTO PERSON (Name)
14 VALUES ('Lars'), ('Kristina'), ('Sophie');
15 INSERT INTO PERSON (
16 Name)
17 VALUES (
18 'Lars'),
19 (
20 'Kristina'),
21 (
22 'Sophie');
23
24 SELECT *
25 FROM PERSON;
15 26
16 SELECT * FROM PERSON;
17 27 -- end snippet demo1 -- end snippet demo1
File notes/code/sql/HW_CapstoneSol.sql changed (mode: 100644) (index ddb9c00..cd68dd2)
... ... Answer the following short questions based on the model implemented above.
123 123 You can simply answer "True" or "False", or justify your reasoning (e.g. with code). You can simply answer "True" or "False", or justify your reasoning (e.g. with code).
124 124 */ */
125 125 -- 1. Can a project uses multiple programming languages? -- 1. Can a project uses multiple programming languages?
126 -- Yes.
127 -- Yes.
128 -- Yes.
126 -- Yes.
127 -- 2. Can a student be the leader of multiple
128 -- projects?
129 -- Yes.
130 -- 3. Can multiple projects have the same code name?
131 -- Yes.
132 -- 4. Could Claude simply enter NULL for the value
133 -- of his pproject's code name, since he's undecided?
134 -- No.
135 -- 5. Can a project be created without project
136 -- leader?
137 -- No.
138 -- 6. Can we know who is working on a project
139 -- without being its leader?
140 -- No.
129 141 /* /*
130 142
131 143 II. Relational Model (6 pts.) II. Relational Model (6 pts.)
 
... ... Please, leave them uncommented, unless you can't write them correctly, in which
154 154 The first question is answered as an example. The first question is answered as an example.
155 155 */ */
156 156 -- 0. Write a command that list all the names of the -- 0. Write a command that list all the names of the
157 -- programming languages.
157 -- programming languages.
158 158 SELECT Name SELECT Name
159 159 FROM PROGRAMMING_LANGUAGE; FROM PROGRAMMING_LANGUAGE;
160 160
161 161 -- 1. Write a command that insert a new student in the -- 1. Write a command that insert a new student in the
162 -- STUDENT table.
163 -- (You should invent the values).
162 -- STUDENT table.
163 -- (You should invent the values).
164 164 INSERT INTO STUDENT INSERT INTO STUDENT
165 165 VALUES ( VALUES (
166 166 "Bob", "Bob",
 
... ... VALUES (
169 169 NULL); NULL);
170 170
171 171 -- 2. Write a command that updates the code name of the -- 2. Write a command that updates the code name of the
172 -- project ("Undecided", "9999999999999") to "VR in
173 -- ER".
172 174 UPDATE UPDATE
173 175 PROJECT PROJECT
174 176 SET CodeName = "VR in ER" SET CodeName = "VR in ER"
 
... ... WHERE CodeName = "Undecided"
178 178 AND Leader = "9999999999999"; AND Leader = "9999999999999";
179 179
180 180 -- 3. Write a command that updates the graduation year of the -- 3. Write a command that updates the graduation year of the
181 -- student whose id is "0987654321098" to 2024, and
182 -- the semester to "Fall".
181 183 UPDATE UPDATE
182 184 STUDENT STUDENT
183 185 SET GraduationYear = 2024, SET GraduationYear = 2024,
 
... ... SET GraduationYear = 2024,
187 187 WHERE id = "0987654321098"; WHERE id = "0987654321098";
188 188
189 189 -- 4. Write a command that changes the STUDENT table to make -- 4. Write a command that changes the STUDENT table to make
190 -- it impossible to enter NULL for the first name of
191 -- a student, without changing the primary key.
190 192 ALTER TABLE STUDENT MODIFY FName VARCHAR(50) NOT NULL; ALTER TABLE STUDENT MODIFY FName VARCHAR(50) NOT NULL;
191 193
192 194 -- 5. Write a command that changes the datatype of -- 5. Write a command that changes the datatype of
195 -- GraduationYear to SMALLINT.
193 196 ALTER TABLE STUDENT MODIFY GraduationYear SMALLINT; ALTER TABLE STUDENT MODIFY GraduationYear SMALLINT;
194 197
195 198 -- 6. Write a command that adds an attribute "ReleaseDate" to -- 6. Write a command that adds an attribute "ReleaseDate" to
199 -- the PROJECT table.
196 200 ALTER TABLE PROJECT ALTER TABLE PROJECT
197 201 ADD COLUMN ReleaseDate DATE; ADD COLUMN ReleaseDate DATE;
198 202
199 203 -- 6.bis If you managed to write the previous command -- 6.bis If you managed to write the previous command
204 -- correctly, write a command that sets the release
205 -- date of the project ("Brick Break", "0123456789100")
206 -- to
207 -- the 26th of November 2022.
200 208 UPDATE UPDATE
201 209 PROJECT PROJECT
202 210 SET ReleaseDate = DATE "20221126" SET ReleaseDate = DATE "20221126"
 
... ... WHERE CodeName = "Brick Break"
212 212 AND Leader = "0123456789100"; AND Leader = "0123456789100";
213 213
214 214 -- 7. Write a command that makes it impossible for a student -- 7. Write a command that makes it impossible for a student
215 -- to be the leader in more than one project
216 -- (This command should return an error)
217 -- ALTER TABLE PROJECT ADD UNIQUE (Leader);
File notes/code/sql/HW_Certificate.sql changed (mode: 100644) (index 86ff75c..5252980)
... ... SELECT CN
91 91 FROM CERTIFICATE; FROM CERTIFICATE;
92 92
93 93 -- (*.wikimedia.org | *.fsf.org | *.shadytest.org | -- (*.wikimedia.org | *.fsf.org | *.shadytest.org |
94 -- *.wikipedia.org)
95 -- The SN of the organizations whose CN
96 -- "Foundation"
94 -- *.wikipedia.org)
95 -- The SN of the organizations whose CN
96 -- contains
97 -- "Foundation"
97 98 SELECT SN SELECT SN
98 99 FROM ORGANIZATION FROM ORGANIZATION
99 100 WHERE CN LIKE "%Foundation%"; WHERE CN LIKE "%Foundation%";
100 101
101 102 -- (01 | 02) -- (01 | 02)
102 -- The CN and expiration date of all the
103 -- that
104 -- expired (assuming we are the 6th of December
103 -- The CN and expiration date of all the
104 -- certificates
105 -- that
106 -- expired (assuming we are the 6th of
107 -- December
108 -- 2019).
105 109 SELECT CN, SELECT CN,
106 110 Valid_Until Valid_Until
107 111 FROM CERTIFICATE FROM CERTIFICATE
108 112 WHERE Valid_Until < DATE '20191206'; WHERE Valid_Until < DATE '20191206';
109 113
110 114 -- (*.fsf.org, 2019-10-10) -- (*.fsf.org, 2019-10-10)
111 -- The CN of the CA that are not trusted.
115 -- The CN of the CA that are not trusted.
112 116 SELECT CN SELECT CN
113 117 FROM CA FROM CA
114 118 WHERE Trusted IS NOT TRUE; WHERE Trusted IS NOT TRUE;
115 119
116 120 -- (Shady Corp. | NewComer Ltd.) -- (Shady Corp. | NewComer Ltd.)
117 -- The CN of the certificates that are signed
118 -- that
119 -- is not trusted.
121 -- The CN of the certificates that are signed
122 -- by
123 -- a
124 -- CA
125 -- that
126 -- is not trusted.
120 127 SELECT CERTIFICATE.CN SELECT CERTIFICATE.CN
121 128 FROM CERTIFICATE, FROM CERTIFICATE,
122 129 CA CA
 
... ... WHERE Trusted IS NOT TRUE
130 131 AND CA.SN = CERTIFICATE.Issuer; AND CA.SN = CERTIFICATE.Issuer;
131 132
132 133 -- (Shady Corp. | NewComer Ltd.) -- (Shady Corp. | NewComer Ltd.)
133 -- The number of certificates signed by the CA
134 -- CN
135 -- is
136 -- "Let's encrypt".
134 -- The number of certificates signed by the
135 -- CA
136 -- whose
137 -- CN
138 -- is
139 -- "Let's encrypt".
137 140 SELECT COUNT(CERTIFICATE.SN) AS "Number of certificates signed SELECT COUNT(CERTIFICATE.SN) AS "Number of certificates signed
138 141 by Let's encrypt" by Let's encrypt"
139 142 FROM CERTIFICATE, FROM CERTIFICATE,
 
... ... WHERE CERTIFICATE.Issuer = CA.SN
143 145 AND CA.CN = "Let's encrypt"; AND CA.CN = "Let's encrypt";
144 146
145 147 -- (2) -- (2)
146 -- A table listing the CN of the organizations
147 -- with
148 -- the CN of their certificates.
148 -- A table listing the CN of the
149 -- organizations
150 -- along
151 -- with
152 -- the CN of their certificates.
149 153 SELECT ORGANIZATION.CN AS Organization, SELECT ORGANIZATION.CN AS Organization,
150 154 CERTIFICATE.CN AS Certificate CERTIFICATE.CN AS Certificate
151 155 FROM ORGANIZATION, FROM ORGANIZATION,
 
... ... FROM ORGANIZATION,
154 157 WHERE CERTIFICATE.Org = ORGANIZATION.SN; WHERE CERTIFICATE.Org = ORGANIZATION.SN;
155 158
156 159 -- ( Wikimedia Foundation, *.wikimedia.org | Free Software -- ( Wikimedia Foundation, *.wikimedia.org | Free Software
157 -- Foundation, *.fsf.org | Free Software
158 -- *.shadytest.org | Wikimedia Foundation ,
159 -- *.wikipedia.org
160 -- )
160 -- Foundation, *.fsf.org | Free Software
161 -- Foundation
162 -- ,
163 -- *.shadytest.org | Wikimedia Foundation ,
164 -- *.wikipedia.org
165 -- )
161 166 /* /*
162 167 DELETE FROM CA WHERE SN = 'A'; DELETE FROM CA WHERE SN = 'A';
163 168 ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`HW_Certificate`.`CERTIFICATE`, CONSTRAINT `CERTIFICATE_ibfk_2` FOREIGN KEY (`Issuer`) REFERENCES `CA` (`SN`)) ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`HW_Certificate`.`CERTIFICATE`, CONSTRAINT `CERTIFICATE_ibfk_2` FOREIGN KEY (`Issuer`) REFERENCES `CA` (`SN`))
File notes/code/sql/HW_ConstraintsPart1.sql changed (mode: 100644) (index 61bee27..0d9dc35)
... ... CREATE TABLE HURRICANE (
10 10 WindSpeed INT DEFAULT 76 CHECK (WindSpeed > 74 AND WindSpeed INT DEFAULT 76 CHECK (WindSpeed > 74 AND
11 11 WindSpeed < 500), WindSpeed < 500),
12 12 -- 75mph is the minimum to be considered as a hurricane -- 75mph is the minimum to be considered as a hurricane
13 -- cf. https://www.hwn.org/resources/bws.html
13 -- cf. https://www.hwn.org/resources/bws.html
14 14 Above VARCHAR(25) Above VARCHAR(25)
15 15 ); );
16 16
File notes/code/sql/HW_DBCoffee.sql changed (mode: 100644) (index 0e4e696..c4426e1)
... ... VALUES (
125 125 3.00); 3.00);
126 126
127 127 -- The following statement raises an error. -- The following statement raises an error.
128 -- INSERT INTO PROVIDER
129 -- VALUES (NULL, "contact@localcof.com");
130 -- ERROR 1048 (23000) at line 68: Column 'Name'
131 -- be
132 -- null
128 -- INSERT INTO PROVIDER
129 -- VALUES (NULL, "contact@localcof.com");
130 -- ERROR 1048 (23000) at line 68: Column
131 -- 'Name'
132 -- cannot
133 -- be
134 -- null
133 135 INSERT INTO SUPPLY INSERT INTO SUPPLY
134 136 VALUES ( VALUES (
135 137 "Johns & Co.", "Johns & Co.",
136 138 121); 121);
137 139
138 140 -- The following statement raises an error. -- The following statement raises an error.
139 -- -INSERT INTO SUPPLY
140 -- VALUES ("Coffee Unl.", 311, 221);
141 -- ERROR 1136 (21S01): Column count doesn't
142 -- value
143 -- count at row 1
144 -- Rest the changes:
141 -- -INSERT INTO SUPPLY
142 -- VALUES ("Coffee Unl.", 311, 221);
143 -- ERROR 1136 (21S01): Column count doesn't
144 -- match
145 -- value
146 -- count at row 1
147 -- Rest the changes:
145 148 ROLLBACK; ROLLBACK;
146 149
147 150 -- Question 3: -- Question 3:
File notes/code/sql/HW_DefaultTest.sql changed (mode: 100644) (index 2f92d0b..9f652a7)
1 1 DROP SCHEMA IF EXISTS HW_DEFAULT_test; DROP SCHEMA IF EXISTS HW_DEFAULT_test;
2
2 3 CREATE SCHEMA HW_DEFAULT_test; CREATE SCHEMA HW_DEFAULT_test;
4
3 5 USE HW_DEFAULT_test; USE HW_DEFAULT_test;
4 6
5 7 -- start snippet demo1 -- start snippet demo1
6 8 /* code/sql/HW_DefaultTest.sql */ /* code/sql/HW_DefaultTest.sql */
7
8 9 CREATE TABLE TEST ( CREATE TABLE TEST (
9 10 TestA VARCHAR(15), TestA VARCHAR(15),
10 11 TestB INT, TestB INT,
 
... ... VALUES (
25 26
26 27 SELECT * SELECT *
27 28 FROM TEST; FROM TEST;
28 29
30 -- end snippet demo1
29 31 -- start snippet modif1 -- start snippet modif1
30 32 /* code/sql/HW_DefaultTest.sql */ /* code/sql/HW_DefaultTest.sql */
31 ALTER TABLE TEST ALTER COLUMN TestA SET DEFAULT "A";
33 ALTER TABLE TEST
34 ALTER COLUMN TestA SET DEFAULT "A";
32 35
33 36 SELECT * SELECT *
34 37 FROM TEST; FROM TEST;
35 --end snippet modif1
36 38
39 --end snippet modif1
37 40 -- start snippet modif2 -- start snippet modif2
38 41 /* code/sql/HW_DefaultTest.sql */ /* code/sql/HW_DefaultTest.sql */
39 INSERT INTO TEST (TestB)
40 VALUES (1);
42 INSERT INTO TEST (
43 TestB)
44 VALUES (
45 1);
41 46
42 47 SELECT * SELECT *
43 48 FROM TEST; FROM TEST;
44 49
50 -- The value of TestA is set to "A",
51 -- all the other values are set to NULL.
52 -- end snippet modif2
53 -- start snippet demo2
45 54 /* code/sql/HW_DefaultTest.sql */ /* code/sql/HW_DefaultTest.sql */
46 SELECT * FROM TEST WHERE TestA = "A";
47 SELECT * FROM TEST WHERE TestA <> "A";
55 SELECT *
56 FROM TEST
57 WHERE TestA = "A";
48 58
59 SELECT *
60 FROM TEST
61 WHERE TestA <> "A";
49 62
63 -- end snippet demo2
50 64 -- start snippet demo3 -- start snippet demo3
51 65 /* code/sql/HW_DefaultTest.sql */ /* code/sql/HW_DefaultTest.sql */
52 SELECT * FROM TEST WHERE TestA IS NULL;
53 SELECT * FROM TEST WHERE TestA IS NOT NULL;
66 SELECT *
67 FROM TEST
68 WHERE TestA IS NULL;
69
70 SELECT *
71 FROM TEST
72 WHERE TestA IS NOT NULL;
73
54 74 -- end snippet demo3 -- end snippet demo3
File notes/code/sql/HW_Department.sql changed (mode: 100644) (index e7dbf47..dc2afec)
5 5 DROP SCHEMA IF EXISTS HW_Department; DROP SCHEMA IF EXISTS HW_Department;
6 6
7 7 -- Carefull, we are dropping the schema HW_Department if it -- Carefull, we are dropping the schema HW_Department if it
8 -- exists already, and all the data in it.
8 -- exists already, and all the data in it.
9 9 CREATE SCHEMA HW_Department; CREATE SCHEMA HW_Department;
10 10
11 11 -- And then re-creating it. -- And then re-creating it.
File notes/code/sql/HW_Faculty.sql changed (mode: 100644) (index 5d2d505..ad46c93)
... ... VALUES (
68 68 '19940101', -- Or '940101', '1994-01-01', '94/01/01' '19940101', -- Or '940101', '1994-01-01', '94/01/01'
69 69 '090500', -- Or '09:05:00', '9:05:0', '9:5:0', '090500' '090500', -- Or '09:05:00', '9:05:0', '9:5:0', '090500'
70 70 -- Note also the existence of DATETIME, with 'YYYY-MM-DD -- Note also the existence of DATETIME, with 'YYYY-MM-DD
71 -- HH:MM:SS'
71 -- HH:MM:SS'
72 72 'Apple' -- This is not case-sensitive, oddly enough. 'Apple' -- This is not case-sensitive, oddly enough.
73 73 ); );
File notes/code/sql/HW_ProcedureExamples.sql changed (mode: 100644) (index ca7ec92..97c360f)
... ... BEGIN
42 42 END; END;
43 43 $$ $$
44 44 -- This is the delimiter that marks the end of the procedure -- This is the delimiter that marks the end of the procedure
45 -- definition.
45 -- definition.
46 46 DELIMITER ; DELIMITER ;
47 47
48 48 -- Now, we want ";" to be the "natural" delimiter again. -- Now, we want ";" to be the "natural" delimiter again.
 
... ... DELIMITER ;
69 69 SHOW CREATE PROCEDURE STUDENTLOGIN; SHOW CREATE PROCEDURE STUDENTLOGIN;
70 70
71 71 -- This display information about the procedure just created. -- This display information about the procedure just created.
72 -- We can pass quite naturally an argument to
73 -- procedure.
72 -- We can pass quite naturally an argument to
73 -- our
74 -- procedure.
74 75 CALL STUDENTLOGIN ("Test A"); CALL STUDENTLOGIN ("Test A");
75 76
76 77 -- end snippet procedure-3 -- end snippet procedure-3
File notes/code/sql/HW_ProfExample.sql changed (mode: 100644) (index 9b1ef59..1e5636d)
... ... WHERE DEPARTMENT.Name = "Mathematics"
215 215 AND Department = Code; AND Department = Code;
216 216
217 217 -- end snippet select-project-join-1 -- end snippet select-project-join-1
218 -- start snippet select-project-join-2
218 -- start snippet select-project-join-2
219 219 SELECT Name SELECT Name
220 220 FROM STUDENT, FROM STUDENT,
221 221 GRADE GRADE
 
... ... WHERE Grade > 3.0
223 223 AND STUDENT.Login = GRADE.Login; AND STUDENT.Login = GRADE.Login;
224 224
225 225 -- end snippet select-project-join-2 -- end snippet select-project-join-2
226 -- start snippet select-project-join-3
226 -- start snippet select-project-join-3
227 227 SELECT PROF.Name SELECT PROF.Name
228 228 FROM PROF, FROM PROF,
229 229 DEPARTMENT, DEPARTMENT,
File notes/code/sql/HW_ResidencySol.sql changed (mode: 100644) (index ff1d077..ded2951)
... ... VALUES (
75 75 the next question. the next question.
76 76 */ */
77 77 -- Exercise 4 -- Exercise 4
78 -- List the rows (i.e., P.2, H.1, or even
79 -- modified by the following statements:
78 -- List the rows (i.e., P.2, H.1, or even
79 -- “none”)
80 -- modified by the following statements:
80 81 START TRANSACTION; START TRANSACTION;
81 82
82 83 UPDATE UPDATE
 
... ... ROLLBACK;
106 106 START TRANSACTION; START TRANSACTION;
107 107
108 108 -- Commented, because it causes an error. -- Commented, because it causes an error.
109 -- DELETE FROM PERSON
110 -- WHERE Birthdate = DATE "1990-02-11";
111 -- None, because of the foreign key and the
112 -- referential
113 -- integrity constraint.
114 -- ERROR 1451 (23000): Cannot delete or update
115 -- parent
116 -- row:
117 -- a foreign key constraint fails
118 -- (`HW_RESIDENCY_SOL`.`RESIDENCY`, CONSTRAINT
119 -- `RESIDENCY_ibfk_1` FOREIGN KEY (`Person`)
120 -- REFERENCES
121 -- `PERSON` (`SSN`))
109 -- DELETE FROM PERSON
110 -- WHERE Birthdate = DATE "1990-02-11";
111 -- None, because of the foreign key and the
112 -- referential
113 -- integrity constraint.
114 -- ERROR 1451 (23000): Cannot delete or
115 -- update
116 -- a
117 -- parent
118 -- row:
119 -- a foreign key constraint fails
120 -- (`HW_RESIDENCY_SOL`.`RESIDENCY`, CONSTRAINT
121 -- `RESIDENCY_ibfk_1` FOREIGN KEY (`Person`)
122 -- REFERENCES
123 -- `PERSON` (`SSN`))
122 124 ROLLBACK; ROLLBACK;
123 125
124 126 -- end snippet solution4 -- end snippet solution4
 
... ... SELECT Address
132 133 FROM HOUSE; FROM HOUSE;
133 134
134 135 -- … the SSN of the persons whose first name was not -- … the SSN of the persons whose first name was not
135 -- entered in the system (000-00-0000).
136 -- entered in the system (000-00-0000).
136 137 SELECT SSN SELECT SSN
137 138 FROM PERSON FROM PERSON
138 139 WHERE FName IS NULL; WHERE FName IS NULL;
 
... ... SELECT DISTINCT COLOR
142 143 FROM HOUSE; FROM HOUSE;
143 144
144 145 -- … the address of the residency of James Baldwin (123 -- … the address of the residency of James Baldwin (123
145 -- Main St.).
146 -- Main St.).
146 147 SELECT House SELECT House
147 148 FROM RESIDENCY, FROM RESIDENCY,
148 149 PERSON PERSON
 
... ... WHERE PERSON.Fname = "James"
151 152 AND PERSON.SSN = RESIDENCY.Person; AND PERSON.SSN = RESIDENCY.Person;
152 153
153 154 -- … the first name of the oldest person in the database -- … the first name of the oldest person in the database
154 -- (James).
155 -- (James).
155 156 SELECT FName SELECT FName
156 157 FROM PERSON FROM PERSON
157 158 WHERE Birthdate = ( WHERE Birthdate = (
 
... ... WHERE Birthdate = (
160 161 WHERE Birthdate IS NOT NULL); WHERE Birthdate IS NOT NULL);
161 162
162 163 -- … Michael Keal’s principal residency address (123 Main -- … Michael Keal’s principal residency address (123 Main
163 -- St.).
164 -- St.).
164 165 SELECT RESIDENCY.House SELECT RESIDENCY.House
165 166 FROM RESIDENCY, FROM RESIDENCY,
166 167 PERSON PERSON
 
... ... WHERE PERSON.FName = "Michael"
170 171 AND RESIDENCY.PrincipalResidence = TRUE; AND RESIDENCY.PrincipalResidence = TRUE;
171 172
172 173 -- … the (distinct) first and last names of the homeowners -- … the (distinct) first and last names of the homeowners
173 -- (Michael Keal, Mridula Warrier).
174 -- (Michael Keal, Mridula Warrier).
174 175 SELECT DISTINCT (PERSON.FName), SELECT DISTINCT (PERSON.FName),
175 176 PERSON.LName PERSON.LName
176 177 FROM PERSON, FROM PERSON,
 
... ... WHERE SSN IN ( SELECT DISTINCT (RESIDENCY.Person)
187 188 WHERE RESIDENCY.Status = "own"); WHERE RESIDENCY.Status = "own");
188 189
189 190 -- … the SSN of the persons that have the same principal -- … the SSN of the persons that have the same principal
190 -- residency as James Baldwin (000-00-0001).
191 -- residency as James Baldwin (000-00-0001).
191 192 SELECT RoomMate.Person SELECT RoomMate.Person
192 193 FROM RESIDENCY AS James, FROM RESIDENCY AS James,
193 194 RESIDENCY AS RoomMate, RESIDENCY AS RoomMate,
 
... ... WHERE PERSON.FName = "James"
203 204 START TRANSACTION; START TRANSACTION;
204 205
205 206 -- start snippet homonyms -- start snippet homonyms
206 -- If we have homonymns in our database, e.g.
207 -- If we have homonymns in our database, e.g.
207 208 INSERT INTO PERSON INSERT INTO PERSON
208 209 VALUES ( VALUES (
209 210 "A", "A",
 
... ... VALUES (
235 236 "own"); "own");
236 237
237 238 -- Then the query below fails, in the sense that it reports -- Then the query below fails, in the sense that it reports
238 -- the name "A, B" only once.
239 -- the name "A, B" only once.
239 240 SELECT DISTINCT (PERSON.FName), SELECT DISTINCT (PERSON.FName),
240 241 PERSON.LName PERSON.LName
241 242 FROM PERSON, FROM PERSON,
 
... ... WHERE RESIDENCY.Status = "own"
244 245 AND RESIDENCY.Person = PERSON.SSN; AND RESIDENCY.Person = PERSON.SSN;
245 246
246 247 -- A better (and not much more complicated) solution would -- A better (and not much more complicated) solution would
247 -- have been
248 -- have been
248 249 SELECT PERSON.FName, SELECT PERSON.FName,
249 250 PERSON.LName PERSON.LName
250 251 FROM PERSON FROM PERSON
File notes/code/sql/HW_ScientificResearchSol.sql changed (mode: 100644) (index 321817a..5619311)
... ... VALUES (
103 103 -- start snippet solution -- start snippet solution
104 104 /* code/sql/HW_ScientificResearchSol.sql */ /* code/sql/HW_ScientificResearchSol.sql */
105 105 -- List the rows affected (updated or deleted) by the -- List the rows affected (updated or deleted) by the
106 -- following commands.
107 -- If no rows are affected because the command would
108 -- would
109 -- violate the entity integrity constraint, the
110 -- referential
111 -- integrity constraint, or if there would be some other
112 -- kind
113 -- of error, please indicate it.
106 114 START TRANSACTION; START TRANSACTION;
107 115
108 116
 
... ... SET SSN = "000000001"
121 121 WHERE Name = "Claire"; WHERE Name = "Claire";
122 122 */ */
123 123 -- ERROR 1062 (23000) at line 106: Duplicate entry '1' for -- ERROR 1062 (23000) at line 106: Duplicate entry '1' for
124 -- key 'PRIMARY'
124 125 ROLLBACK; ROLLBACK;
125 126
126 127 START TRANSACTION; START TRANSACTION;
 
... ... DELETE FROM FUNDINGAGENCY
149 149 WHERE Name = "French-American Cultural Exchange"; WHERE Name = "French-American Cultural Exchange";
150 150 */ */
151 151 -- ERROR 1451 (23000): Cannot delete or update a parent row: -- ERROR 1451 (23000): Cannot delete or update a parent row:
152 -- a foreign key constraint fails
153 -- (`HW_SCIENTIFIC_RESEARCH`.`FUNDS`, CONSTRAINT
154 -- `FUNDS_ibfk_1` FOREIGN KEY (`Agency`) REFERENCES
155 -- `FUNDINGAGENCY` (`Name`) ON UPDATE CASCADE)
152 156 ROLLBACK; ROLLBACK;
153 157
154 158 -- List the name of the funding agencies created after 2000 -- List the name of the funding agencies created after 2000
159 -- ("French-American Cultural Exchange")
155 160 SELECT Name SELECT Name
156 161 FROM FUNDINGAGENCY FROM FUNDINGAGENCY
157 162 WHERE Creation >= 2000; WHERE Creation >= 2000;
158 163
159 164 -- List the code of the projects that contains the word -- List the code of the projects that contains the word
165 -- "Airplanes" ("AA", "BA")
160 166 SELECT CODE SELECT CODE
161 167 FROM PROJECT FROM PROJECT
162 168 WHERE Name LIKE ("%Airplanes%"); WHERE Name LIKE ("%Airplanes%");
163 169
164 170 -- List the number of hours scientists contributed to the -- List the number of hours scientists contributed to the
171 -- project "AA" (18)
165 172 SELECT SUM(Hours) SELECT SUM(Hours)
166 173 FROM CONTRIBUTESTO FROM CONTRIBUTESTO
167 174 WHERE Project = "AA"; WHERE Project = "AA";
168 175
169 176 -- List the code of the projects to which the scientist named -- List the code of the projects to which the scientist named
177 -- Sabine contributed ("AA", "BB")
170 178 SELECT Project SELECT Project
171 179 FROM CONTRIBUTESTO, FROM CONTRIBUTESTO,
172 180 SCIENTIST SCIENTIST
 
... ... WHERE SCIENTIST.Name = "Sabine"
182 182 AND SCIENTIST.SSN = CONTRIBUTESTO.Scientist; AND SCIENTIST.SSN = CONTRIBUTESTO.Scientist;
183 183
184 184 -- Give the name of the projects who benefited from federal -- Give the name of the projects who benefited from federal
185 -- funds ("Advancing Airplanes")
185 186 SELECT PROJECT.Name SELECT PROJECT.Name
186 187 FROM PROJECT, FROM PROJECT,
187 188 FUNDS, FUNDS,
 
... ... WHERE FUNDINGAGENCY.Type = "Federal"
192 192 AND FUNDS.Project = PROJECT.Code; AND FUNDS.Project = PROJECT.Code;
193 193
194 194 -- Give the name of the scientist who contributed to the same -- Give the name of the scientist who contributed to the same
195 -- project as Mike ("Sabine", "James")
195 196 SELECT DISTINCT (Fellow.Name) AS "Mike's fellow" SELECT DISTINCT (Fellow.Name) AS "Mike's fellow"
196 197 FROM SCIENTIST AS Mike, FROM SCIENTIST AS Mike,
197 198 SCIENTIST AS Fellow, SCIENTIST AS Fellow,
 
... ... WHERE Mike.Name = "Mike"
205 205 AND NOT Fellow.Name = "Mike"; AND NOT Fellow.Name = "Mike";
206 206
207 207 -- List the name of the projects that are not funded by an -- List the name of the projects that are not funded by an
208 -- agency ("Better Airplanes", "Better Buildings")
208 209 SELECT DISTINCT (PROJECT.Name) SELECT DISTINCT (PROJECT.Name)
209 210 FROM PROJECT, FROM PROJECT,
210 211 FUNDS FUNDS
 
... ... WHERE NOT PROJECT.Code IN (
214 214 FROM FUNDS); FROM FUNDS);
215 215
216 216 -- Give the name of the scientist who contributed the most -- Give the name of the scientist who contributed the most
217 -- (in terms of hours) to the project named "Advancing
218 -- Airplanes" (Sabine)
217 219 SELECT SCIENTIST.Name SELECT SCIENTIST.Name
218 220 FROM SCIENTIST, FROM SCIENTIST,
219 221 CONTRIBUTESTO CONTRIBUTESTO
File notes/code/sql/HW_SocialMedia.sql changed (mode: 100644) (index 64bca2e..2ca5244)
... ... VALUES (
75 75 DATE "2019-03-03"); DATE "2019-03-03");
76 76
77 77 -- The first entry means that 2 subscribed to 1, not the -- The first entry means that 2 subscribed to 1, not the
78 -- other way around.
79 -- And similarly for the other entries.
78 -- other way around.
79 -- And similarly for the other entries.
80 80 INSERT INTO VIDEO INSERT INTO VIDEO
81 81 VALUES ( VALUES (
82 82 10, 10,
 
... ... VALUES (
124 124 -- start snippet solution -- start snippet solution
125 125 /* code/sql/HW_SocialMedia.sql */ /* code/sql/HW_SocialMedia.sql */
126 126 -- … the title of all the videos ("My first video!", "My -- … the title of all the videos ("My first video!", "My
127 -- second video!", "My vacations").
127 -- second video!", "My vacations").
128 128 SELECT TITLE SELECT TITLE
129 129 FROM VIDEO; FROM VIDEO;
130 130
131 131 -- … the release date of the video whose title is "My first -- … the release date of the video whose title is "My first
132 -- video!" ("2020-02-02").
132 -- video!" ("2020-02-02").
133 133 SELECT Released SELECT Released
134 134 FROM VIDEO FROM VIDEO
135 135 WHERE Title = "My first video!"; WHERE Title = "My first video!";
136 136
137 137 -- … the ID of the account(s) where the "Name" attribute -- … the ID of the account(s) where the "Name" attribute
138 -- was not given ("2").
138 -- was not given ("2").
139 139 SELECT ID SELECT ID
140 140 FROM ACCOUNT FROM ACCOUNT
141 141 WHERE Name IS NULL; WHERE Name IS NULL;
142 142
143 143 -- … the ID of the videos whose title contains the word -- … the ID of the videos whose title contains the word
144 -- "video" ("10", "20").
144 -- "video" ("10", "20").
145 145 SELECT ID SELECT ID
146 146 FROM VIDEO FROM VIDEO
147 147 WHERE TITLE LIKE "%video%"; WHERE TITLE LIKE "%video%";
 
... ... FROM VIDEO
152 152 WHERE Title REGEXP 'video'; WHERE Title REGEXP 'video';
153 153
154 154 -- … the number of thumbs up for the video with title "My -- … the number of thumbs up for the video with title "My
155 -- vacations" ("1").
155 -- vacations" ("1").
156 156 SELECT COUNT(*) SELECT COUNT(*)
157 157 FROM THUMBS_UP, FROM THUMBS_UP,
158 158 VIDEO VIDEO
 
... ... ORDER BY Released ASC
180 180 LIMIT 1; LIMIT 1;
181 181
182 182 -- … the names of the accounts who gave a thumbs up to the -- … the names of the accounts who gave a thumbs up to the
183 -- video with id 30 ("Bob Ross").
183 -- video with id 30 ("Bob Ross").
184 184 SELECT Name SELECT Name
185 185 FROM ACCOUNT, FROM ACCOUNT,
186 186 THUMBS_UP THUMBS_UP
 
... ... WHERE THUMBS_UP.Video = 30
188 188 AND THUMBS_UP.Account = ACCOUNT.ID; AND THUMBS_UP.Account = ACCOUNT.ID;
189 189
190 190 -- … the ID of the account with the greatest number of -- … the ID of the account with the greatest number of
191 -- subscribers ("2").
191 -- subscribers ("2").
192 192 SELECT Subscribed SELECT Subscribed
193 193 FROM SUBSCRIBE FROM SUBSCRIBE
194 194 GROUP BY Subscribed GROUP BY Subscribed
File notes/code/sql/HW_Storm.sql changed (mode: 100644) (index dbb9929..513298c)
... ... VALUES (
36 36 "2017-08-17"); "2017-08-17");
37 37
38 38 -- In the following, the entry gets created, but date is -- In the following, the entry gets created, but date is
39 -- "corrected" to "2017-17-08"!
40 -- INSERT INTO STORM
41 -- VALUES ("Dummy", "Hurricane", 120,
42 -- The error message returned is
43 -- ERROR 1292 (22007) at line 34: Incorrect
44 -- value:
45 -- "2017-17-08" for column
46 -- `HW_STORM`.`STORM`.`Creation`
47 -- at
48 -- row 1
49 -- In the following, we explicitely use
50 -- since
51 -- the date is incorrect, nothing gets
52 -- INSERT INTO STORM
53 -- VALUES ("Dummy2", "Hurricane", 120, DATE
54 -- "2017-17-08");
55 -- ERROR 1525 (HY000) at line 40: Incorrect
56 -- value:
57 -- "2017-17-08"
58 -- The next one sets NULL for DATE.
39 -- "corrected" to "2017-17-08"!
40 -- INSERT INTO STORM
41 -- VALUES ("Dummy", "Hurricane", 120,
42 -- "2017-17-08");
43 -- The error message returned is
44 -- ERROR 1292 (22007) at line 34: Incorrect
45 -- date
46 -- value:
47 -- "2017-17-08" for column
48 -- `HW_STORM`.`STORM`.`Creation`
49 -- at
50 -- row 1
51 -- In the following, we explicitely use
52 -- "DATE",
53 -- and
54 -- since
55 -- the date is incorrect, nothing gets
56 -- inserted.
57 -- INSERT INTO STORM
58 -- VALUES ("Dummy2", "Hurricane", 120, DATE
59 -- "2017-17-08");
60 -- ERROR 1525 (HY000) at line 40: Incorrect
61 -- DATE
62 -- value:
63 -- "2017-17-08"
64 -- The next one sets NULL for DATE.
59 65 INSERT INTO STORM INSERT INTO STORM
60 66 VALUES ( VALUES (
61 67 "Irma", "Irma",
 
... ... VALUES (
90 90 NULL); NULL);
91 91
92 92 -- This instruction is not using the primary key, is that a -- This instruction is not using the primary key, is that a
93 -- problem?
93 -- problem?
94 94 UPDATE UPDATE
95 95 STATE STATE
96 96 SET Affected_by = "Harvey" SET Affected_by = "Harvey"
File notes/code/sql/HW_TriggerExample.sql changed (mode: 100644) (index 4e8d8fb..1c6321f)
2 2 DROP SCHEMA IF EXISTS HW_TriggerExample; DROP SCHEMA IF EXISTS HW_TriggerExample;
3 3
4 4 -- To drop only a trigger, you can use -- To drop only a trigger, you can use
5 -- DROP TRIGGER IF EXISTS
6 -- HW_TriggerExample.NUMBER_OF_STUDENT_INC;
7 -- DROP TRIGGER IF EXISTS
8 -- HW_TriggerExample.NUMBER_OF_STUDENT_DEC;
5 -- DROP TRIGGER IF EXISTS
6 -- HW_TriggerExample.NUMBER_OF_STUDENT_INC;
7 -- DROP TRIGGER IF EXISTS
8 -- HW_TriggerExample.NUMBER_OF_STUDENT_DEC;
9 9 CREATE SCHEMA HW_TriggerExample; CREATE SCHEMA HW_TriggerExample;
10 10
11 11 USE HW_TriggerExample; USE HW_TriggerExample;
 
... ... CREATE TRIGGER STUDENT_AVERAGE
83 83 WHERE STUDENT.Login = NEW.Student; WHERE STUDENT.Login = NEW.Student;
84 84
85 85 -- The "NEW" keyword here refers to the "new" entry -- The "NEW" keyword here refers to the "new" entry
86 -- that is being inserted by the INSERT
87 -- triggering
88 -- the trigger.
89 -- end snippet trigger-3
86 -- that is being inserted by the INSERT
87 -- statement
88 -- triggering
89 -- the trigger.
90 -- end snippet trigger-3
90 91 INSERT INTO GRADE INSERT INTO GRADE
91 92 VALUES ( VALUES (
92 93 "A", "A",
 
... ... SELECT *
113 113 FROM STUDENT; FROM STUDENT;
114 114
115 115 -- Tada, all the averages have been computed! -- Tada, all the averages have been computed!
116 -- Note also that the student "C" does not
117 -- average!
116 -- Note also that the student "C" does not
117 -- have
118 -- an
119 -- average!
File notes/code/sql/HW_Vaccine.sql changed (mode: 100644) (index 91eb7cb..888325e)
... ... CREATE SCHEMA HW_Vaccine;
12 12
13 13 USE HW_Vaccine; USE HW_Vaccine;
14 14
15 -- start snippet setup
16 /* code/sql/HW_Vaccine.sql */
15 17 CREATE TABLE COMPANY ( CREATE TABLE COMPANY (
16 18 Name VARCHAR(50) PRIMARY KEY, Name VARCHAR(50) PRIMARY KEY,
17 19 Website VARCHAR(255) CHECK (Website LIKE "https://%") Website VARCHAR(255) CHECK (Website LIKE "https://%")
 
... ... CREATE TABLE DISEASE (
21 23 Name VARCHAR(50) PRIMARY KEY, Name VARCHAR(50) PRIMARY KEY,
22 24 Communicable BOOL, Communicable BOOL,
23 25 -- Whether the disease can be transmitted from a human to -- Whether the disease can be transmitted from a human to
24 -- another.
26 -- another.
25 27 TYPE ENUM ("infectious", "deficiency", "hereditary") TYPE ENUM ("infectious", "deficiency", "hereditary")
26 28 ); );
27 29
 
... ... VALUES (
63 65 "mRNA-1273", "mRNA-1273",
64 66 94.1); 94.1);
65 67
66
68 -- end snippet setup
67 69 /* /*
68 70 START EDITING START EDITING
69 71 */ */
 
... ... Please, leave them uncommented, unless
128 130 The first question is answered as an example. The first question is answered as an example.
129 131 */ */
130 132 -- 0. Write a command that list the names of -- 0. Write a command that list the names of
133 -- all the diseases.
131 134 SELECT Name SELECT Name
132 135 FROM DISEASE; FROM DISEASE;
133 136
134 137 -- 1. Write a command that insert "Pfizer" in the -- 1. Write a command that insert "Pfizer" in the
135
136
137
138
139
140
141
142
143
144
145
146
147
138 -- COMPANY table (you can make up the website or look
139 -- it)
140 -- 2. Write a command that insert the "Pfizer-BioNTech
141 -- COVID-19 Vaccine" in the VACCINE table, and a command
142 -- that store the efficacy of that vaccine against
143 -- the "Coronavirus disease 2019" disease
144 -- (you can make up the values or look them up).
145 -- 3. Write a command that updates the name of the
146 -- company "Moderna" to "Moderna, Inc." everywhere.
147 -- 4. Write a command that lists the name of all the
148 -- companies.
149 -- 5. Write a command that deletes the "Coronavirus disease
150 -- 2019" entry from the DISEASE table (if only!).
151 -- This command should return an error. Explain it and
152 -- leave
153 -- the command commented.
154 -- 6. Write two commands: one that adds "physiological" to
155 -- the possible types of diseases, and one that inserts
156 -- a physiological disease in the DISEASE table.
157 -- 7 (difficult). Write a command that return the list of
158 -- all the companies that manufacture a
159 -- vaccine against "Coronavirus disease
160 -- 2019".
File notes/code/sql/HW_VaccineSol.sql added (mode: 100644) (index 0000000..bae78b7)
1 DROP SCHEMA IF EXISTS HW_Vaccine;
2
3 CREATE SCHEMA HW_Vaccine;
4
5 USE HW_Vaccine;
6
7 CREATE TABLE COMPANY (
8 Name VARCHAR(50) PRIMARY KEY,
9 Website VARCHAR(255) CHECK (Website LIKE "https://%")
10 );
11
12 CREATE TABLE DISEASE (
13 Name VARCHAR(50) PRIMARY KEY,
14 Communicable BOOL,
15 -- Whether the disease can be transmitted from a human to
16 -- another.
17 TYPE ENUM ("infectious", "deficiency", "hereditary")
18 );
19
20 CREATE TABLE VACCINE (
21 Name VARCHAR(50) PRIMARY KEY,
22 Manufacturer VARCHAR(50) NOT NULL,
23 FOREIGN KEY (Manufacturer) REFERENCES COMPANY (NAME) ON
24 UPDATE CASCADE
25 );
26
27 CREATE TABLE EFFICACY (
28 DiseaseName VARCHAR(50),
29 VaccineName VARCHAR(50),
30 Efficacy DECIMAl(5, 2),
31 PRIMARY KEY (DiseaseName, VaccineName),
32 FOREIGN KEY (DiseaseName) REFERENCES DISEASE (NAME),
33 FOREIGN KEY (VaccineName) REFERENCES VACCINE (NAME)
34 );
35
36 INSERT INTO COMPANY
37 VALUES (
38 "Moderna",
39 "https://www.modernatx.com/");
40
41 INSERT INTO DISEASE
42 VALUES (
43 "Coronavirus disease 2019",
44 TRUE,
45 "infectious");
46
47 INSERT INTO VACCINE
48 VALUES (
49 "mRNA-1273",
50 "Moderna");
51
52 INSERT INTO EFFICACY
53 VALUES (
54 "Coronavirus disease 2019",
55 "mRNA-1273",
56 94.1);
57
58
59 /*
60 START EDITING
61 */
62 -- start snippet solution
63 /* code/sql/HW_VaccineSol.sql */
64 /*
65
66 I. Short Questions (3 pts.)
67
68 Answer the following short questions. In our implementation…
69
70 1. … can two companies have exactly the same name?
71
72 No, as COMPANY.Name is the only attribute in the primary key of COMPANY.
73
74 2. … can two companies have the same website?
75
76 Yes, nothing prevents it.
77
78 3. … can a company not have a website?
79
80 Yes, the domain of COMPANY.Website is "VARCHAR(255)", without a constraint preventing it from being "NULL".
81
82 4. … can the same vaccine be manufactured by multiple companies?
83
84 No, as VACCINE.Manufacturer is an attribute in VACCINE that accepts only one value.
85
86 5. … can a vaccine not have a manufacturer?
87
88 No, as VACCINE.Manufacturer bears the "NOT NULL" constraint.
89
90 6. … can a disease being neither communicable nor not communicable?
91
92 Yes, as DISEASE.Communicable is of type "BOOL", it accepts the "NULL" value.
93
94 7. … can the same vaccine have different efficacies for different diseases?
95
96 Yes, the EFFICACY table has for primary key VaccineName and DiseaseName, which implies that the same vaccine can occur repeatedly as long as it is associated with different diseases.
97 */
98 /*
99
100 II. Longer Questions (6 pts.)
101
102 Answer the following questions:
103
104 1. What does `CHECK (Website LIKE "https://*")` do?
105
106 It refrains any value not starting with "https://" to be inserted as a value for the COMPANY.Website attribute.
107 Note that in particular it forbids a website from not being secured (that is, http:// is not a valid protocol).
108
109 2. Why did we picked the `DECIMAl(5,2)` datatype?
110
111 It is the appropriate datatype to represent percentage values represented as ranging from 100.00 to 0.00.
112 The discussion at https://stackoverflow.com/a/2762376/ also highlights that percent can be represented as decimal(5,4) with a check to insure that the value will range between 1.0000 and 0.0000.
113
114 3. What is the benefit / are the benefits of having a separate EFFICACY table over having something like
115
116 CREATE TABLE VACCINE(
117 Name VARCHAR(50) PRIMARY KEY,
118 Manufacturer VARCHAR(50),
119 Disease VARCHAR(50),
120 Efficacy DECIMAl(5,2),
121 FOREIGN KEY (Manufacturer) REFERENCES COMPANY (Name)
122 );
123
124 ?
125
126 This implementation does not allow to record that the same vaccine can have different efficacies for different diseases.
127 Stated differently, it forbids to represent vaccines efficient against multiple diseases faitfully.
128 */
129 /*
130
131 III. Relational Model (6 pts.)
132
133 Draw the relational model corresponding to this code.
134 You can hand-draw it and join a scan or a picture, or simply hand me back a sheet.
135 */
136 /*
137
138 IV. Simple Commands (5 pts.)
139
140 Below, you are asked to write commands that perform various actions.
141 Please, leave them uncommented, unless
142 - you can not write them correctly, but want to share your attempt,
143 - it is specified that it should return an error.
144
145 The first question is answered as an example.
146 */
147 -- 0. Write a command that list the names of
148 -- all the diseases.
149 SELECT Name
150 FROM DISEASE;
151
152 -- 1. Write a command that insert "Pfizer" in the
153 -- COMPANY table (you can make up the website or look
154 -- it)
155 INSERT INTO COMPANY
156 VALUES (
157 "Pfizer",
158 "https://www.pfizer.com/");
159
160 -- 2. Write a command that insert the "Pfizer-BioNTech
161 -- COVID-19 Vaccine" in the VACCINE table, and a
162 -- command
163 -- that store the efficacy of that vaccine against
164 -- the "Coronavirus disease 2019" disease
165 -- ( you can make up the values or look them up).
166 INSERT INTO VACCINE
167 VALUES (
168 "Pfizer-BioNTech COVID-19 Vaccine",
169 "Pfizer");
170
171 INSERT INTO EFFICACY
172 VALUES (
173 "Coronavirus disease 2019",
174 "Pfizer-BioNTech COVID-19 Vaccine",
175 89);
176
177 -- 3. Write a command that updates the name of the
178 -- company "Moderna" to "Moderna, Inc." everywhere.
179 UPDATE
180 COMPANY
181 SET Name = "Moderna, Inc."
182 WHERE Name = "Moderna";
183
184 -- 4. Write a command that lists the name of all the
185 -- companies.
186 SELECT Name
187 FROM COMPANY;
188
189 -- 5. Write a command that deletes the "Coronavirus disease
190 -- 2019" entry from the DISEASE table (if only!).
191 /*
192 DELETE FROM DISEASE
193 WHERE Name = "Coronavirus disease 2019";
194 */
195 -- This command should return an error. Explain it and leave
196 -- the command commented.
197 -- The "Coronavirus disease 2019" value in DISEASE.Name is
198 -- refereed to by two entries in the EFFICACY table.
199 -- As the foreign key from EFFICACY.DiseaseName to
200 -- DISEASE.Name does not specify its policy "ON DELETE", its
201 -- default behavior is to restrict deletion, causing the
202 -- error.
203 -- 6. Write two commands: one that adds "physiological" to
204 -- the possible types of diseases, and one that inserts
205 -- a physiological disease in the DISEASE table.
206 ALTER TABLE DISEASE MODIFY TYPE ENUM ("infectious",
207 "deficiency", "hereditary", "physiological");
208
209 INSERT INTO DISEASE
210 VALUES (
211 "Asthma",
212 FALSE,
213 "physiological");
214
215 -- 7 (difficult). Write a command that return the list of
216 -- all the companies that manufacture a
217 -- vaccine against "Coronavirus disease
218 -- 2019".
219 SELECT VACCINE.Manufacturer
220 FROM VACCINE,
221 EFFICACY
222 WHERE VACCINE.Name = EFFICACY.VaccineName
223 AND EFFICACY.DiseaseName = "Coronavirus disease 2019";
224
225 -- end snippet solution
File notes/code/sql/HW_Work.sql changed (mode: 100644) (index ac59c97..bc3d022)
... ... VALUES (
100 100 So, "Successful insertion". So, "Successful insertion".
101 101 */ */
102 102 -- The following statement raises an error. -- The following statement raises an error.
103 -- INSERT INTO AUTHOR
104 -- VALUES ("Mary B.", "mb@fai.fr", NULL);
103 -- INSERT INTO AUTHOR
104 -- VALUES ("Mary B.", "mb@fai.fr", NULL);
105 105 /* /*
106 106 ERROR 1136 (21S01): Column count doesn't match value count at row 1 ERROR 1136 (21S01): Column count doesn't match value count at row 1
107 107 So, "Other kind of error". So, "Other kind of error".
108 108 */ */
109 109 -- The following statement raises an error. -- The following statement raises an error.
110 -- INSERT INTO WORK
111 -- VALUES ("My Life", "Claude A.");
110 -- INSERT INTO WORK
111 -- VALUES ("My Life", "Claude A.");
112 112 /* /*
113 113 ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
114 114 (`HW_EXAM_1`.`WORK`, CONSTRAINT `WORK_ibfk_1` FOREIGN KEY (`Author`) REFERENCES `AUTHOR` (`Name`) (`HW_EXAM_1`.`WORK`, CONSTRAINT `WORK_ibfk_1` FOREIGN KEY (`Author`) REFERENCES `AUTHOR` (`Name`)
 
... ... VALUES (
128 128 So, "Successful insertion". So, "Successful insertion".
129 129 */ */
130 130 -- The following statement raises an error. -- The following statement raises an error.
131 -- INSERT INTO AUTHOR
132 -- VALUES ("Virginia W.", "alt@isp.net");
131 -- INSERT INTO AUTHOR
132 -- VALUES ("Virginia W.", "alt@isp.net");
133 133 /* /*
134 134 ERROR 1062 (23000): Duplicate entry 'Virginia W.' for key 'PRIMARY' ERROR 1062 (23000): Duplicate entry 'Virginia W.' for key 'PRIMARY'
135 135 So, "Entity integrity constraint". So, "Entity integrity constraint".
 
... ... WHERE Title = "What to eat";
175 175 Does not change any row. Does not change any row.
176 176 */ */
177 177 -- The following statement raises an error. -- The following statement raises an error.
178 -- DELETE FROM AUTHOR
179 -- WHERE Name = "Virginia W.";
178 -- DELETE FROM AUTHOR
179 -- WHERE Name = "Virginia W.";
180 180 /* /*
181 181 ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
182 182 (`HW_EXAM_1`.`BOOK`, CONSTRAINT `BOOK_ibfk_1` FOREIGN KEY (`Work`) REFERENCES `WORK` (`Title`) ON UPDATE CASCADE) (`HW_EXAM_1`.`BOOK`, CONSTRAINT `BOOK_ibfk_1` FOREIGN KEY (`Work`) REFERENCES `WORK` (`Title`) ON UPDATE CASCADE)
 
... ... WHERE Title = "What to eat";
185 185 ROLLBACK; ROLLBACK;
186 186
187 187 -- We go back to the previous state. -- We go back to the previous state.
188 -- You can now assume that there is more data
189 -- what
190 -- we
191 -- inserted, if that helps you. Write a command
192 -- selects
193 -- …
194 -- We insert some dummy values for this next
188 -- You can now assume that there is more data
189 -- than
190 -- what
191 -- we
192 -- inserted, if that helps you. Write a
193 -- command
194 -- that
195 -- selects
196 -- …
197 -- We insert some dummy values for this next
198 -- part.
195 199 INSERT INTO WORK INSERT INTO WORK
196 200 VALUES ( VALUES (
197 201 "My Life", "My Life",
 
... ... SELECT Price
232 233 FROM EBOOK; FROM EBOOK;
233 234
234 235 -- … the (distinct) names of the authors who have authored -- … the (distinct) names of the authors who have authored
235 -- a piece of work.
236 -- a piece of work.
236 237 SELECT DISTINCT Author SELECT DISTINCT Author
237 238 FROM WORK; FROM WORK;
238 239
 
... ... SELECT MAX(Price)
258 259 FROM BOOK; FROM BOOK;
259 260
260 261 -- … the number of pieces of work written by the author -- … the number of pieces of work written by the author
261 -- whose name is “Virginia W.”.
262 -- whose name is “Virginia W.”.
262 263 SELECT COUNT(*) SELECT COUNT(*)
263 264 FROM WORK FROM WORK
264 265 WHERE WORK.Author = "Virginia W."; WHERE WORK.Author = "Virginia W.";
265 266
266 267 -- … the email of the author who wrote the piece of work -- … the email of the author who wrote the piece of work
267 -- called “My Life”.
268 -- called “My Life”.
268 269 SELECT Email SELECT Email
269 270 FROM AUTHOR, FROM AUTHOR,
270 271 WORK WORK
 
... ... WHERE WORK.Title = "My Life"
272 273 AND WORK.Author = AUTHOR.Name; AND WORK.Author = AUTHOR.Name;
273 274
274 275 -- the isbn(s) of the book containing a work written by the -- the isbn(s) of the book containing a work written by the
275 -- author whose email is "vw@isp.net".
276 -- author whose email is "vw@isp.net".
276 277 SELECT ISBN SELECT ISBN
277 278 FROM BOOK, FROM BOOK,
278 279 WORK, WORK,
 
... ... WHERE
300 301 they are both given the title "BANNED", which violates the unicity of value in primary keys. they are both given the title "BANNED", which violates the unicity of value in primary keys.
301 302 */ */
302 303 -- Write one or multiple commands that would delete the work -- Write one or multiple commands that would delete the work
303 -- whose title is “My Life”, as well as all
304 -- books
305 -- and ebooks versions of it.
306 -- The following statement raises an error.
307 -- DELETE FROM WORK
308 -- WHERE Title = "My Life";
304 -- whose title is “My Life”, as well as
305 -- all
306 -- of
307 -- the
308 -- books
309 -- and ebooks versions of it.
310 -- The following statement raises an error.
311 -- DELETE FROM WORK
312 -- WHERE Title = "My Life";
309 313 /* /*
310 314 Fails Fails
311 315 ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
File notes/lectures_notes.md changed (mode: 100644) (index 979bd9e..3450976)
... ... You can simply answer "True" or "False", or justify your reasoning (e.g. with co
4303 4303
4304 4304 --- ---
4305 4305
4306
4307 Problem (A simple database for vaccines) +.#project1ter
4308 ~
4309
4310 Consider the following code:
4311
4312 <!-- Bug, this code should be indented to be in the current environment. -->
4313
4314
4315 ```{.sqlmysql .numberLines .includeLink include=code/sql/HW_Vaccine.sql snippet=setup}
4316 ```
4317
4318 @problem:project1ter -- Question -.#
4319
4320 : Answer the following short questions. In our implementation…
4321
4322 #. … can two companies have exactly the same name?
4323 #. … can two companies have the same website?
4324 #. … can a company not have a website?
4325 #. … can the same vaccine be manufactured by multiple companies?
4326 #. … can a vaccine not have a manufacturer?
4327 #. … can a disease being neither communicable nor not communicable?
4328 #. … can the same vaccine have different efficacies for different diseases?
4329
4330 @problem:project1ter -- Question -.#
4331 ~
4332 Answer the following questions:
4333
4334 #. What does `CHECK (Website LIKE "https://*")` do?
4335 #. Why did we picked the `DECIMAl(5,2)` datatype?
4336 #. What is the benefit / are the benefits of having a separate EFFICACY table over having something like
4337
4338 ```
4339 CREATE TABLE VACCINE(
4340 Name VARCHAR(50) PRIMARY KEY,
4341 Manufacturer VARCHAR(50),
4342 Disease VARCHAR(50),
4343 Efficacy DECIMAl(5,2),
4344 FOREIGN KEY (Manufacturer) REFERENCES COMPANY (Name)
4345 );
4346 ```
4347
4348 ?
4349
4350 @problem:project1ter -- Question -.#
4351
4352 ~ Draw the relational model corresponding to this code.
4353
4354 @problem:project1bis -- Question -.#
4355
4356 : Write the following commands.
4357
4358 #. Write a command that insert "Pfizer" in the COMPANY table (you can make up the website or look it)
4359 #. Write a command that insert the "Pfizer-BioNTech COVID-19 Vaccine" in the VACCINE table, and a command that store the efficacy of that vaccine against the "Coronavirus disease 2019" disease (you can make up the values or look them up).
4360 #. Write a command that updates the name of the company "Moderna" to "Moderna, Inc." everywhere.
4361 #. Write a command that lists the name of all the companies.
4362 #. Write a command that deletes the "Coronavirus disease 2019" entry from the DISEASE table (if only!). This command should return an error. Explain it and leave the command commented.
4363 #. Write two commands: one that adds "physiological" to the possible types of diseases, and one that inserts a physiological disease in the DISEASE table.
4364 #. Write a command that return the list of all the companies that manufacture a vaccine against "Coronavirus disease 2019".
4365
4366 ---
4367
4306 4368 Problem (A database for residencies) +.#residency Problem (A database for residencies) +.#residency
4307 4369 ~ ~
4308 4370
 
... ... Solution to [%D %n (%T)](#problem:project1bis)
5076 5138
5077 5139 --- ---
5078 5140
5141 Solution to [%D %n (%T)](#problem:project1ter)
5142 ~
5143 The answers can be found in the following snippet:
5144
5145 <!-- Bug, this code should be indented to be in the current environment. -->
5146
5147
5148 ```{.sqlmysql .numberLines .includeLink include=code/sql/HW_VaccineSol.sql snippet=solution}
5149 ```
5150
5151 ---
5152
5153
5079 5154 Solution to [%D %n (%T)](#problem:residency) Solution to [%D %n (%T)](#problem:residency)
5080 5155 ~ ~
5081 5156 The file [code/sql/HW_ResidencySol.sql](https://rocketgit.com/user/caubert/CSCI_3410/source/tree/branch/master/blob/notes/code/sql/HW_ResidencySol.sql) contains the solution to the code part of this problem. The file [code/sql/HW_ResidencySol.sql](https://rocketgit.com/user/caubert/CSCI_3410/source/tree/branch/master/blob/notes/code/sql/HW_ResidencySol.sql) contains the solution to the code part of this problem.
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/caubert/CSCI_3410

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/caubert/CSCI_3410

Clone this repository using git:
git clone git://git.rocketgit.com/user/caubert/CSCI_3410

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main