List of commits:
Subject Hash Author Date (UTC)
Added example for MAX and NULL values. b82a496a5ffbcecaf2c5851f18d1b08ce8732623 aubert@math.cnrs.fr 2020-09-10 13:14:13
Changed SQL code formatting. 6c3cad5a2545f46ab113f7df7a83457857d82ed8 aubert@math.cnrs.fr 2020-09-09 17:04:55
Cleaned code. 5bdb4faed3a83b81257734f1e1aced2890783f04 aubert@math.cnrs.fr 2020-09-03 21:35:41
Added the first project. 564a02887933f2395bc40d7d8a10833f657659fd aubert@math.cnrs.fr 2020-08-28 22:34:08
Week 2 edits, added quiz #1, couple of fixes, replaced single quote with double quotes. 3c9942731678900122088356db3a2cbabd99b9be aubert@math.cnrs.fr 2020-08-27 19:00:13
Added ressource for makefile. 7696c44bca707646530a7dbb71bf2e05badaa306 aubert@math.cnrs.fr 2020-08-03 16:00:23
Crystal's final edits. 714e3030423a836c4ba07890f9aa5e45f58ad15a aubert@math.cnrs.fr 2020-05-21 17:43:26
Converted an image into a figure (Movie example). c55e61ed5d11631e908d99b14ef10a0a0247bda0 aubert@math.cnrs.fr 2020-05-20 20:58:41
Re-formatted SQL code. 915442a1ba4d8baa120343f98de5ee39d4ac45f6 aubert@math.cnrs.fr 2020-05-18 15:52:06
Fixed Known_bugs 5900c572928ec3b8c98c82fe4e95ebbe9aeee6c3 aubert@math.cnrs.fr 2020-05-15 18:19:36
Fixed contrib and enriched example. 04864c0ee2d4fa77b4e681ebf8049c4642bf1e67 aubert@math.cnrs.fr 2020-05-15 18:17:22
Fixed formatting mistake. 948a87c75b5d9aa8317feb5a0859d4efc23e95d6 aubert@math.cnrs.fr 2020-05-15 17:58:40
Cleaned SQL code. 4d39ebc5c1a3566ef4d3fa7afc8b2868f827c108 aubert@math.cnrs.fr 2020-05-15 17:41:00
Fixing few mistakes in code. b7eb7a0e476f8e0c3c6d3e651fd80827a03dd127 aubert@math.cnrs.fr 2020-05-15 17:38:32
Fixing few mistakes in code. 2bc77d7ee4e82e6961ce123fb7c3e1c68cba59b5 aubert@math.cnrs.fr 2020-05-15 17:30:02
Testing and indenting SQL code. a2b3bb4e242dd4980b94b25d11d6001459e2f0a0 aubert@math.cnrs.fr 2020-05-15 17:26:27
Clarified an example. e68bac453ab427c132b55249e21a08166b112f31 aubert@math.cnrs.fr 2020-05-15 15:06:54
Edits in style. 47578b081f74e9ec706772fa70a3079957129542 aubert@math.cnrs.fr 2020-05-15 14:38:16
Added activity diagram. 11c9acfa88c398f7463d6e54f45ea48c8793caf1 aubert@math.cnrs.fr 2020-05-15 14:34:31
Added activity diagram. 995cf4f64c43601716f77bb46d1535025ea14d10 aubert@math.cnrs.fr 2020-05-15 14:33:30
Commit b82a496a5ffbcecaf2c5851f18d1b08ce8732623 - Added example for MAX and NULL values.
Author: aubert@math.cnrs.fr
Author date (UTC): 2020-09-10 13:14
Committer name: aubert@math.cnrs.fr
Committer date (UTC): 2020-09-10 13:14
Parent(s): 6c3cad5a2545f46ab113f7df7a83457857d82ed8
Signer:
Signing key:
Signing status: N
Tree: 92b17cc27d66784e5f43b29b16e6e745bfeca305
File Lines added Lines deleted
notes/code/sql/HW_AdvancedFK.sql 13 13
notes/code/sql/HW_Captone.sql 53 52
notes/code/sql/HW_Certificate.sql 27 24
notes/code/sql/HW_ConstraintsPart1.sql 1 1
notes/code/sql/HW_DBCoffee.sql 12 12
notes/code/sql/HW_Department.sql 1 1
notes/code/sql/HW_Faculty.sql 1 1
notes/code/sql/HW_Max.sql 25 0
notes/code/sql/HW_ProcedureExamples.sql 3 3
notes/code/sql/HW_ProfExample.sql 2 2
notes/code/sql/HW_ResidencySol.sql 25 25
notes/code/sql/HW_SocialMedia.sql 9 9
notes/code/sql/HW_Storm.sql 22 22
notes/code/sql/HW_TriggerExample.sql 10 10
notes/code/sql/HW_Work.sql 26 26
notes/lectures_notes.md 8 14
File notes/code/sql/HW_AdvancedFK.sql changed (mode: 100644) (index 62cc113..8a12e14)
... ... 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 name
26 -- it,
27 -- as follows:
25 -- key of the table we are currently creating, and name
26 -- it,
27 -- as follows:
28 28 CONSTRAINT My_pk_to_T1 FOREIGN KEY (B2) REFERENCES T2 (B1) CONSTRAINT My_pk_to_T1 FOREIGN KEY (B2) REFERENCES T2 (B1)
29 29 ); );
30 30
31 31 -- The benefit of naming our fk constraint is that, if we -- The benefit of naming our fk constraint is that, if we
32 -- violate it, for instance with
33 -- INSERT INTO T2 VALUES (1, 1, 1, 3);
34 -- then the name of the constraint (here "My_pk_to_T1")
35 -- would be displayed in the error message:
36 -- Cannot add or update a child row: a foreign key
37 -- constraint fails (`db_9_9837c1`.`t2`, CONSTRAINT
38 -- `My_pk_to_T1` FOREIGN KEY (`B2`) REFERENCES `t2`
39 -- (`B1`))
40 -- end snippet advancedFK
File notes/code/sql/HW_Captone.sql changed (mode: 100644) (index c2afd87..84121f6)
... ... VALUES (
92 92 -- --
93 93 -- --
94 94 -- --
95 --
95 96 -- https://en.wikipedia.org/wiki/Comparison_of_open-source_pr -- https://en.wikipedia.org/wiki/Comparison_of_open-source_pr
97 -- ogramming_language_licensing
96 98 INSERT INTO PROJECT INSERT INTO PROJECT
97 99 VALUES ( VALUES (
98 100 "Brick Break", "Brick Break",
 
... ... Answer the following short questions based on the model implemented above.
138 139 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).
139 140 */ */
140 141 -- 1. Can a project uses multiple programming languages? -- 1. Can a project uses multiple programming languages?
142 -- FILL HERE
143 -- 2. Can a student be the leader of multiple projects?
144 -- FILL HERE
145 -- 3. Can multiple projects have the same code name?
146 -- FILL HERE
147 -- 4. Could Claude simply enter NULL for the value of
148 -- his
149 -- project's code name, since he's undecided?
150 -- FILL HERE
151 -- 5. Can a project be created without project leader?
152 -- FILL HERE
153 -- 6. Can we know who is working on a project without
154 -- being
155 -- its leader?
156 -- FILL HERE
141 157 /* /*
142 158
143 159 II. Relational Model (6 pts.) II. Relational Model (6 pts.)
 
... ... Please, leave them uncommented, unless you can't write them correctly, in which
169 170 The first question is answered as an example. The first question is answered as an example.
170 171 */ */
171 172 -- 0. Write a command that list all the names of the -- 0. Write a command that list all the names of the
173 -- programming languages.
172 174 SELECT Name SELECT Name
173 175 FROM PROGRAMMING_LANGUAGE; FROM PROGRAMMING_LANGUAGE;
174 176
175 177 -- 1. Write a command that insert a new student in the -- 1. Write a command that insert a new student in the
178 -- STUDENT table.
179 -- (You should invent the values).
180 -- FILL HERE
181 -- 2. Write a command that updates the code name of the
182 -- project ("Undecided", "9999999999999") to "VR in ER".
183 -- FILL HERE
184 -- 3. Write a command that updates the graduation year
176 185 -- of -- of
186 -- the student whose id is "0987654321098" to 2024, and
187 -- the
188 -- semester to "Fall".
189 -- FILL HERE
190 -- 4. Write a command that changes the STUDENT table to
191 -- make
192 -- it impossible to enter NULL for the first name of a
193 -- student, without changing the primary key.
194 -- FILL HERE
195 -- 5. Write a command that changes the datatype of
196 -- GraduationYear to SMALLINT.
197 -- FILL HERE
198 -- 6. Write a command that adds an attribute
199 -- "ReleaseDate"
200 -- to the PROJECT table.
201 -- FILL HERE
202 -- 6.bis If you managed to write the previous command
203 -- correctly, write a command that sets the release date
204 -- of
205 -- the project ("Brick Break", "0123456789100") to the
206 -- 26th
207 -- of November 2022.
208 -- FILL HERE
209 -- 7. Write a command that makes it impossible for a
210 -- student
211 -- to be the leader in more than one project
212 -- (This command should return an error)
213 -- FILL HERE
File notes/code/sql/HW_Certificate.sql changed (mode: 100644) (index f4b82a9..2cd79b9)
... ... 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 contains
96 -- "Foundation"
94 -- *.wikipedia.org)
95 -- The SN of the organizations whose CN contains
96 -- "Foundation"
97 97 SELECT SN SELECT SN
98 98 FROM ORGANIZATION FROM ORGANIZATION
99 99 WHERE CN LIKE "%Foundation%"; WHERE CN LIKE "%Foundation%";
100 100
101 101 -- (01 | 02) -- (01 | 02)
102 -- The CN and expiration date of all the
103 -- expired (assuming we are the 6th of December
102 -- The CN and expiration date of all the
103 -- certificates
104 -- that
105 -- expired (assuming we are the 6th of December
106 -- 2019).
104 107 SELECT CN, SELECT CN,
105 108 Valid_Until Valid_Until
106 109 FROM CERTIFICATE FROM CERTIFICATE
107 110 WHERE Valid_Until < DATE '20191206'; WHERE Valid_Until < DATE '20191206';
108 111
109 112 -- (*.fsf.org, 2019-10-10) -- (*.fsf.org, 2019-10-10)
110 -- The CN of the CA that are not trusted.
113 -- The CN of the CA that are not trusted.
111 114 SELECT CN SELECT CN
112 115 FROM CA FROM CA
113 116 WHERE Trusted IS NOT TRUE; WHERE Trusted IS NOT TRUE;
114 117
115 118 -- (Shady Corp. | NewComer Ltd.) -- (Shady Corp. | NewComer Ltd.)
116 -- The CN of the certificates that are signed by a
117 -- is not trusted.
119 -- The CN of the certificates that are signed by a
120 -- CA
121 -- that
122 -- is not trusted.
118 123 SELECT CERTIFICATE.CN SELECT CERTIFICATE.CN
119 124 FROM CERTIFICATE, FROM CERTIFICATE,
120 125 CA CA
 
... ... WHERE Trusted IS NOT TRUE
127 127 AND CA.SN = CERTIFICATE.Issuer; AND CA.SN = CERTIFICATE.Issuer;
128 128
129 129 -- (Shady Corp. | NewComer Ltd.) -- (Shady Corp. | NewComer Ltd.)
130 -- The number of certificates signed by the CA whose
131 -- "Let's encrypt".
130 -- The number of certificates signed by the CA
131 -- whose
132 -- CN
133 -- is
134 -- "Let's encrypt".
132 135 SELECT COUNT(CERTIFICATE.SN) AS "Number of certificates signed SELECT COUNT(CERTIFICATE.SN) AS "Number of certificates signed
133 136 by Let's encrypt" by Let's encrypt"
134 137 FROM CERTIFICATE, FROM CERTIFICATE,
 
... ... WHERE CERTIFICATE.Issuer = CA.SN
139 140 AND CA.CN = "Let's encrypt"; AND CA.CN = "Let's encrypt";
140 141
141 142 -- (2) -- (2)
142 -- A table listing the CN of the organizations along
143 -- the CN of their certificates.
143 -- A table listing the CN of the organizations
144 -- along
145 -- with
146 -- the CN of their certificates.
144 147 SELECT ORGANIZATION.CN AS Organization, SELECT ORGANIZATION.CN AS Organization,
145 148 CERTIFICATE.CN AS Certificate CERTIFICATE.CN AS Certificate
146 149 FROM ORGANIZATION, FROM ORGANIZATION,
 
... ... FROM ORGANIZATION,
149 151 WHERE CERTIFICATE.Org = ORGANIZATION.SN; WHERE CERTIFICATE.Org = ORGANIZATION.SN;
150 152
151 153 -- ( Wikimedia Foundation, *.wikimedia.org | Free Software -- ( Wikimedia Foundation, *.wikimedia.org | Free Software
152 -- Foundation, *.fsf.org | Free Software Foundation ,
153 -- *.shadytest.org | Wikimedia Foundation ,
154 -- )
154 -- Foundation, *.fsf.org | Free Software Foundation
155 -- ,
156 -- *.shadytest.org | Wikimedia Foundation ,
157 -- *.wikipedia.org
158 -- )
155 159 /* /*
156 160 DELETE FROM CA WHERE SN = 'A'; DELETE FROM CA WHERE SN = 'A';
157 161 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 fb320ad..68cea1d)
... ... 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 6970c69..147d39a)
... ... 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 -- null
128 -- INSERT INTO PROVIDER
129 -- VALUES (NULL, "contact@localcof.com");
130 -- ERROR 1048 (23000) at line 68: Column 'Name'
131 -- cannot
132 -- be
133 -- null
132 134 INSERT INTO SUPPLY INSERT INTO SUPPLY
133 135 VALUES ( VALUES (
134 136 "Johns & Co.", "Johns & Co.",
135 137 121); 121);
136 138
137 139 -- The following statement raises an error. -- The following statement raises an error.
138 -- -INSERT INTO SUPPLY
139 -- VALUES ("Coffee Unl.", 311, 221);
140 -- ERROR 1136 (21S01): Column count doesn't match
141 -- count at row 1
142 -- Rest the changes:
140 -- -INSERT INTO SUPPLY
141 -- VALUES ("Coffee Unl.", 311, 221);
142 -- ERROR 1136 (21S01): Column count doesn't match
143 -- value
144 -- count at row 1
145 -- Rest the changes:
143 146 ROLLBACK; ROLLBACK;
144 147
145 148 -- Question 3: -- Question 3:
File notes/code/sql/HW_Department.sql changed (mode: 100644) (index 43c7756..d004b66)
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 a5266e2..8fd04eb)
... ... 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_Max.sql added (mode: 100644) (index 0000000..0983727)
1 DROP SCHEMA IF EXISTS HW_Max;
2
3 CREATE SCHEMA HW_Max;
4
5 USE HW_Max;
6
7 -- start snippet show-max
8 /* code/sql/HW_Max.sql */
9 CREATE TABLE TEST (
10 A DATE
11 );
12
13 INSERT INTO TEST
14 VALUES (
15 DATE "2020-01-01"),
16 (
17 DATE "2019-01-01"),
18 (
19 NULL);
20
21 SELECT MAX(A)
22 FROM TEST;
23
24 -- Returns 2020-01-01
25 -- end snippet show-max
File notes/code/sql/HW_ProcedureExamples.sql changed (mode: 100644) (index 740d6fa..95c4c9e)
... ... 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 our
73 -- procedure.
72 -- We can pass quite naturally an argument to our
73 -- procedure.
74 74 CALL STUDENTLOGIN ("Test A"); CALL STUDENTLOGIN ("Test A");
75 75
76 76 -- end snippet procedure-3 -- end snippet procedure-3
File notes/code/sql/HW_ProfExample.sql changed (mode: 100644) (index 64afa6c..b9c0af7)
... ... 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 8896711..2c94f53)
... ... 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 -- integrity constraint.
113 -- ERROR 1451 (23000): Cannot delete or update a
114 -- row:
115 -- a foreign key constraint fails
116 -- (`HW_RESIDENCY_SOL`.`RESIDENCY`, CONSTRAINT
117 -- `RESIDENCY_ibfk_1` FOREIGN KEY (`Person`)
118 -- `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 update a
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`))
119 122 ROLLBACK; ROLLBACK;
120 123
121 124 -- end snippet solution4 -- end snippet solution4
 
... ... SELECT Address
131 131 FROM HOUSE; FROM HOUSE;
132 132
133 133 -- … the SSN of the persons whose first name was not -- … the SSN of the persons whose first name was not
134 -- entered in the system (000-00-0000).
134 -- entered in the system (000-00-0000).
135 135 SELECT SSN SELECT SSN
136 136 FROM PERSON FROM PERSON
137 137 WHERE FName IS NULL; WHERE FName IS NULL;
 
... ... SELECT DISTINCT COLOR
141 141 FROM HOUSE; FROM HOUSE;
142 142
143 143 -- … the address of the residency of James Baldwin (123 -- … the address of the residency of James Baldwin (123
144 -- Main St.).
144 -- Main St.).
145 145 SELECT House SELECT House
146 146 FROM RESIDENCY, FROM RESIDENCY,
147 147 PERSON PERSON
 
... ... WHERE PERSON.Fname = "James"
150 150 AND PERSON.SSN = RESIDENCY.Person; AND PERSON.SSN = RESIDENCY.Person;
151 151
152 152 -- … the first name of the oldest person in the database -- … the first name of the oldest person in the database
153 -- (James).
153 -- (James).
154 154 SELECT FName SELECT FName
155 155 FROM PERSON FROM PERSON
156 156 WHERE Birthdate = ( WHERE Birthdate = (
 
... ... WHERE Birthdate = (
159 159 WHERE Birthdate IS NOT NULL); WHERE Birthdate IS NOT NULL);
160 160
161 161 -- … Michael Keal’s principal residency address (123 Main -- … Michael Keal’s principal residency address (123 Main
162 -- St.).
162 -- St.).
163 163 SELECT RESIDENCY.House SELECT RESIDENCY.House
164 164 FROM RESIDENCY, FROM RESIDENCY,
165 165 PERSON PERSON
 
... ... WHERE PERSON.FName = "Michael"
169 169 AND RESIDENCY.PrincipalResidence = TRUE; AND RESIDENCY.PrincipalResidence = TRUE;
170 170
171 171 -- … the (distinct) first and last names of the homeowners -- … the (distinct) first and last names of the homeowners
172 -- (Michael Keal, Mridula Warrier).
172 -- (Michael Keal, Mridula Warrier).
173 173 SELECT DISTINCT (PERSON.FName), SELECT DISTINCT (PERSON.FName),
174 174 PERSON.LName PERSON.LName
175 175 FROM PERSON, FROM PERSON,
 
... ... WHERE SSN IN ( SELECT DISTINCT (RESIDENCY.Person)
186 186 WHERE RESIDENCY.Status = "own"); WHERE RESIDENCY.Status = "own");
187 187
188 188 -- … the SSN of the persons that have the same principal -- … the SSN of the persons that have the same principal
189 -- residency as James Baldwin (000-00-0001).
189 -- residency as James Baldwin (000-00-0001).
190 190 SELECT RoomMate.Person SELECT RoomMate.Person
191 191 FROM RESIDENCY AS James, FROM RESIDENCY AS James,
192 192 RESIDENCY AS RoomMate, RESIDENCY AS RoomMate,
 
... ... WHERE PERSON.FName = "James"
202 202 START TRANSACTION; START TRANSACTION;
203 203
204 204 -- start snippet homonyms -- start snippet homonyms
205 -- If we have homonymns in our database, e.g.
205 -- If we have homonymns in our database, e.g.
206 206 INSERT INTO PERSON INSERT INTO PERSON
207 207 VALUES ( VALUES (
208 208 "A", "A",
 
... ... VALUES (
234 234 "own"); "own");
235 235
236 236 -- Then the query below fails, in the sense that it reports -- Then the query below fails, in the sense that it reports
237 -- the name "A, B" only once.
237 -- the name "A, B" only once.
238 238 SELECT DISTINCT (PERSON.FName), SELECT DISTINCT (PERSON.FName),
239 239 PERSON.LName PERSON.LName
240 240 FROM PERSON, FROM PERSON,
 
... ... WHERE RESIDENCY.Status = "own"
243 243 AND RESIDENCY.Person = PERSON.SSN; AND RESIDENCY.Person = PERSON.SSN;
244 244
245 245 -- A better (and not much more complicated) solution would -- A better (and not much more complicated) solution would
246 -- have been
246 -- have been
247 247 SELECT PERSON.FName, SELECT PERSON.FName,
248 248 PERSON.LName PERSON.LName
249 249 FROM PERSON FROM PERSON
File notes/code/sql/HW_SocialMedia.sql changed (mode: 100644) (index fabebfe..610f009)
... ... 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 d1dc78f..9541231)
... ... 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 date
44 -- "2017-17-08" for column
45 -- row 1
46 -- In the following, we explicitely use "DATE", and
47 -- the date is incorrect, nothing gets inserted.
48 -- INSERT INTO STORM
49 -- VALUES ("Dummy2", "Hurricane", 120, DATE
50 -- ERROR 1525 (HY000) at line 40: Incorrect DATE
51 -- "2017-17-08"
52 -- 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 date
45 -- value:
46 -- "2017-17-08" for column
47 -- `HW_STORM`.`STORM`.`Creation`
48 -- at
49 -- row 1
50 -- In the following, we explicitely use "DATE", and
51 -- since
52 -- the date is incorrect, nothing gets inserted.
53 -- INSERT INTO STORM
54 -- VALUES ("Dummy2", "Hurricane", 120, DATE
55 -- "2017-17-08");
56 -- ERROR 1525 (HY000) at line 40: Incorrect DATE
57 -- value:
58 -- "2017-17-08"
59 -- The next one sets NULL for DATE.
53 60 INSERT INTO STORM INSERT INTO STORM
54 61 VALUES ( VALUES (
55 62 "Irma", "Irma",
 
... ... VALUES (
85 85 NULL); NULL);
86 86
87 87 -- This instruction is not using the primary key, is that a -- This instruction is not using the primary key, is that a
88 -- problem?
88 -- problem?
89 89 UPDATE UPDATE
90 90 STATE STATE
91 91 SET Affected_by = "Harvey" SET Affected_by = "Harvey"
File notes/code/sql/HW_TriggerExample.sql changed (mode: 100644) (index 6b66ce5..964cec6)
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 statement
87 -- triggering
88 -- the trigger.
89 -- end snippet trigger-3
86 -- that is being inserted by the INSERT statement
87 -- triggering
88 -- the trigger.
89 -- end snippet trigger-3
90 90 INSERT INTO GRADE INSERT INTO GRADE
91 91 VALUES ( VALUES (
92 92 "A", "A",
 
... ... SELECT *
112 112 FROM STUDENT; FROM STUDENT;
113 113
114 114 -- Tada, all the averages have been computed! -- Tada, all the averages have been computed!
115 -- Note also that the student "C" does not have an
115 -- Note also that the student "C" does not have an
116 -- average!
File notes/code/sql/HW_Work.sql changed (mode: 100644) (index 62c7283..0096c9b)
... ... 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 than
189 -- inserted, if that helps you. Write a command that
190 -- …
191 -- We insert some dummy values for this next part.
188 -- You can now assume that there is more data than
189 -- what
190 -- we
191 -- inserted, if that helps you. Write a command that
192 -- selects
193 -- …
194 -- We insert some dummy values for this next part.
192 195 INSERT INTO WORK INSERT INTO WORK
193 196 VALUES ( VALUES (
194 197 "My Life", "My Life",
 
... ... SELECT Price
229 229 FROM EBOOK; FROM EBOOK;
230 230
231 231 -- … the (distinct) names of the authors who have authored -- … the (distinct) names of the authors who have authored
232 -- a piece of work.
232 -- a piece of work.
233 233 SELECT DISTINCT Author SELECT DISTINCT Author
234 234 FROM WORK; FROM WORK;
235 235
 
... ... SELECT MAX(Price)
255 255 FROM BOOK; FROM BOOK;
256 256
257 257 -- … the number of pieces of work written by the author -- … the number of pieces of work written by the author
258 -- whose name is “Virginia W.”.
258 -- whose name is “Virginia W.”.
259 259 SELECT COUNT(*) SELECT COUNT(*)
260 260 FROM WORK FROM WORK
261 261 WHERE WORK.Author = "Virginia W."; WHERE WORK.Author = "Virginia W.";
262 262
263 263 -- … the email of the author who wrote the piece of work -- … the email of the author who wrote the piece of work
264 -- called “My Life”.
264 -- called “My Life”.
265 265 SELECT Email SELECT Email
266 266 FROM AUTHOR, FROM AUTHOR,
267 267 WORK WORK
 
... ... WHERE WORK.Title = "My Life"
269 269 AND WORK.Author = AUTHOR.Name; AND WORK.Author = AUTHOR.Name;
270 270
271 271 -- the isbn(s) of the book containing a work written by the -- the isbn(s) of the book containing a work written by the
272 -- author whose email is "vw@isp.net".
272 -- author whose email is "vw@isp.net".
273 273 SELECT ISBN SELECT ISBN
274 274 FROM BOOK, FROM BOOK,
275 275 WORK, WORK,
 
... ... WHERE
297 297 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.
298 298 */ */
299 299 -- Write one or multiple commands that would delete the work -- Write one or multiple commands that would delete the work
300 -- whose title is “My Life”, as well as all of
301 -- and ebooks versions of it.
302 -- The following statement raises an error.
303 -- DELETE FROM WORK
304 -- WHERE Title = "My Life";
300 -- whose title is “My Life”, as well as all of
301 -- the
302 -- books
303 -- and ebooks versions of it.
304 -- The following statement raises an error.
305 -- DELETE FROM WORK
306 -- WHERE Title = "My Life";
305 307 /* /*
306 308 Fails Fails
307 309 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 3ef58dd..1602067)
... ... On top of the notes, you will find in this document:
74 74 - and for each chapter, - and for each chapter,
75 75 - A list of additional resources, - A list of additional resources,
76 76 - A list of short exercises, - A list of short exercises,
77 - Solution to those exercises,
78 - A list of problem,
79 - Sometimes, solution to some of those problems.
80
81 Any feedback is greatly appreciated.
82 Please refer to <http://spots.augusta.edu/caubert/db/ln/README.html#contributing> for how to contribute to those notes.
83
84 The syllabus is at <http://spots.augusta.edu/caubert/db/>, and the webpage for this notes is at <http://spots.augusta.edu/caubert/db/ln/>.
85 Please, refer to those notes using this entry [@AubertCSCI3410-DatabaseSystems]:
86
87 ```{.bibtex include=bib/entry.bib}
88 ```
89
77 - Solution to those exercises, - A list of problem, - Sometimes, solution to some of those problems. Any feedback is greatly appreciated. Please refer to <http://spots.augusta.edu/caubert/db/ln/README.html#contributing> for how to contribute to those notes. The syllabus is at <http://spots.augusta.edu/caubert/db/>, and the webpage for this notes is at <http://spots.augusta.edu/caubert/db/ln/>. Please, refer to those notes using this entry [@AubertCSCI3410-DatabaseSystems]: ```{.bibtex include=bib/entry.bib} ```
90 78 ## Planned Schedule {-} ## Planned Schedule {-}
91 79
92 80 A typical (meeting twice a week, ±17 weeks, ±30 classes) semester is divided as follows: A typical (meeting twice a week, ±17 weeks, ±30 classes) semester is divided as follows:
 
... ... You can have `ORDER BY` specifications:
2180 2168
2181 2169 ### Aggregate Functions ### Aggregate Functions
2182 2170
2183 You can use `MAX`, `SUM`, `MIN`, `AVG`, `COUNT`:
2171 You can use `MAX`, `SUM`, `MIN`, `AVG`, `COUNT` to peform simple operations.
2184 2172
2185 2173 ``` ```
2186 2174 SELECT MAX(Registered) FROM STUDENT; SELECT MAX(Registered) FROM STUDENT;
 
... ... Note that `AVG` returns the average of all **non-`NULL`** values, as we can see
2205 2193 ```{.sqlmysql .numberLines include=code/sql/HW_Avg.sql snippet=show-avg} ```{.sqlmysql .numberLines include=code/sql/HW_Avg.sql snippet=show-avg}
2206 2194 ``` ```
2207 2195
2196 The same goes for e.g. MAX:
2197
2198 ```{.sqlmysql .numberLines include=code/sql/HW_Max.sql snippet=show-max}
2199 ```
2200
2201
2208 2202 ### Aliases for Columns ### Aliases for Columns
2209 2203
2210 2204 We can use aliases for the columns. We can use aliases for the columns.
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