List of commits:
Subject Hash Author Date (UTC)
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
Edits in intro, converted an image to a figure for example of class diagram. 1ff4ef2f7f44ece81972a6c77e9f6654c144fcdc aubert@math.cnrs.fr 2020-05-14 22:12:38
Adding example file, to test installation. 5ed35e64a4e4dc60c888358bde54594999aab34d aubert@math.cnrs.fr 2020-05-13 19:03:41
Minor adjustments to the syllabus. dca6f12d9d3d561755991029f2d53bf9823a8cea aubert@math.cnrs.fr 2020-05-13 19:02:41
Worked on reverse-engineering section. 34aeba178fe553c08b4fb3738523be293704e09d aubert@math.cnrs.fr 2020-05-13 18:52:09
Commit 3c9942731678900122088356db3a2cbabd99b9be - Week 2 edits, added quiz #1, couple of fixes, replaced single quote with double quotes.
Author: aubert@math.cnrs.fr
Author date (UTC): 2020-08-27 19:00
Committer name: aubert@math.cnrs.fr
Committer date (UTC): 2020-08-27 19:00
Parent(s): 7696c44bca707646530a7dbb71bf2e05badaa306
Signer:
Signing key:
Signing status: N
Tree: 672f251123878633a91e5af5142a2cc534ac5f90
File Lines added Lines deleted
KNOWN_BUGS.md 1 0
notes/code/sql/HW_Certificate.sql 19 15
notes/code/sql/HW_ConstraintsPart1.sql 1 1
notes/code/sql/HW_DBCoffee.sql 9 9
notes/code/sql/HW_Department.sql 1 1
notes/code/sql/HW_Faculty.sql 1 1
notes/code/sql/HW_ProcedureExamples.sql 3 3
notes/code/sql/HW_ProfExample.sql 27 27
notes/code/sql/HW_ResidencySol.sql 21 21
notes/code/sql/HW_SocialMedia.sql 9 9
notes/code/sql/HW_Storm.sql 17 16
notes/code/sql/HW_TriggerExample.sql 10 10
notes/code/sql/HW_Work.sql 24 23
notes/lectures_notes.md 17 7
File KNOWN_BUGS.md changed (mode: 100644) (index 83b20eb..32b15de)
... ... CONTENT TO ADD
9 9 - Add content on Security and "Recognize professional responsibilities and make informed judgments in computing practice based on legal and ethical principles." - Add content on Security and "Recognize professional responsibilities and make informed judgments in computing practice based on legal and ethical principles."
10 10 - Mention / discuss propagate options, and UNIQUE / NOT NULL, etc. options on particular attributes, during the discussion on how to map E.R. diagram to the relational model. - Mention / discuss propagate options, and UNIQUE / NOT NULL, etc. options on particular attributes, during the discussion on how to map E.R. diagram to the relational model.
11 11 - Explain what "try-with-ressource" is in Java (cf. <https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html>, and remove the call to the close() after explaining that Statement and Connection are auto-closeable. (<https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html>, <https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html>). - Explain what "try-with-ressource" is in Java (cf. <https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html>, and remove the call to the close() after explaining that Statement and Connection are auto-closeable. (<https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html>, <https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html>).
12 - Add content on hashing techniques, data storage mechanisms.
12 13
13 14 TO ANSWER TO ANSWER
14 15 ========= =========
File notes/code/sql/HW_Certificate.sql changed (mode: 100644) (index 817947b..a11be3d)
... ... SELECT CN
47 47 FROM CERTIFICATE; FROM CERTIFICATE;
48 48
49 49 -- (*.wikimedia.org | *.fsf.org | *.shadytest.org | -- (*.wikimedia.org | *.fsf.org | *.shadytest.org |
50 -- *.wikipedia.org)
51 -- The SN of the organizations whose CN contains
52 -- "Foundation"
50 53 SELECT SN SELECT SN
51 54 FROM ORGANIZATION FROM ORGANIZATION
52 55 WHERE CN LIKE "%Foundation%"; WHERE CN LIKE "%Foundation%";
53 56
54 57 -- (01 | 02) -- (01 | 02)
58 -- The CN and expiration date of all the certificates
59 -- that
60 -- expired (assuming we are the 6th of December 2019).
55 61 SELECT CN, Valid_Until SELECT CN, Valid_Until
56 62 FROM CERTIFICATE FROM CERTIFICATE
57 63 WHERE Valid_Until < DATE '20191206'; WHERE Valid_Until < DATE '20191206';
58 64
59 65 -- (*.fsf.org, 2019-10-10) -- (*.fsf.org, 2019-10-10)
66 -- The CN of the CA that are not trusted.
60 67 SELECT CN SELECT CN
61 68 FROM CA FROM CA
62 69 WHERE Trusted IS NOT TRUE; WHERE Trusted IS NOT TRUE;
63 70
64 71 -- (Shady Corp. | NewComer Ltd.) -- (Shady Corp. | NewComer Ltd.)
72 -- The CN of the certificates that are signed by a CA
73 -- that
74 -- is not trusted.
65 75 SELECT CERTIFICATE.CN SELECT CERTIFICATE.CN
66 76 FROM CERTIFICATE, CA FROM CERTIFICATE, CA
67 77 WHERE Trusted IS NOT TRUE WHERE Trusted IS NOT TRUE
68 78 AND CA.SN = CERTIFICATE.Issuer; AND CA.SN = CERTIFICATE.Issuer;
69 79
70 80 -- (Shady Corp. | NewComer Ltd.) -- (Shady Corp. | NewComer Ltd.)
81 -- The number of certificates signed by the CA whose CN
82 -- is
83 -- "Let's encrypt".
71 84 SELECT COUNT(CERTIFICATE.SN) AS "Number of certificates signed SELECT COUNT(CERTIFICATE.SN) AS "Number of certificates signed
72 85 by Let's encrypt" by Let's encrypt"
73 86 FROM CERTIFICATE, CA FROM CERTIFICATE, CA
 
... ... WHERE CERTIFICATE.Issuer = CA.SN
85 88 AND CA.CN = "Let's encrypt"; AND CA.CN = "Let's encrypt";
86 89
87 90 -- (2) -- (2)
91 -- A table listing the CN of the organizations along with
92 -- the CN of their certificates.
88 93 SELECT ORGANIZATION.CN AS Organization, CERTIFICATE.CN AS SELECT ORGANIZATION.CN AS Organization, CERTIFICATE.CN AS
89 94 Certificate Certificate
90 95 FROM ORGANIZATION, CERTIFICATE FROM ORGANIZATION, CERTIFICATE
91 96 WHERE CERTIFICATE.Org = ORGANIZATION.SN; WHERE CERTIFICATE.Org = ORGANIZATION.SN;
92 97
93 98 -- ( Wikimedia Foundation, *.wikimedia.org | Free Software -- ( Wikimedia Foundation, *.wikimedia.org | Free Software
99 -- Foundation, *.fsf.org | Free Software Foundation ,
100 -- *.shadytest.org | Wikimedia Foundation ,
101 -- *.wikipedia.org
102 -- )
94 103 /* /*
95 104 DELETE FROM CA WHERE SN = 'A'; DELETE FROM CA WHERE SN = 'A';
96 105 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 489a33b..c8a2dff)
... ... CREATE TABLE HURRICANE (
9 9 NAME VARCHAR(25) PRIMARY KEY, WindSpeed INT DEFAULT 76 NAME VARCHAR(25) PRIMARY KEY, WindSpeed INT DEFAULT 76
10 10 CHECK (WindSpeed > 74 AND WindSpeed < 500), CHECK (WindSpeed > 74 AND WindSpeed < 500),
11 11 -- 75mph is the minimum to be considered as a hurricane -- 75mph is the minimum to be considered as a hurricane
12 -- cf. https://www.hwn.org/resources/bws.html
12 -- cf. https://www.hwn.org/resources/bws.html
13 13 Above VARCHAR(25) Above VARCHAR(25)
14 14 ); );
15 15
File notes/code/sql/HW_DBCoffee.sql changed (mode: 100644) (index 230099a..35c177b)
... ... INSERT INTO COFFEE
61 61 VALUES (002, "Peru", "Decaf", 3.00); VALUES (002, "Peru", "Decaf", 3.00);
62 62
63 63 -- The following statement raises an error. -- The following statement raises an error.
64 -- INSERT INTO PROVIDER
65 -- VALUES (NULL, "contact@localcof.com");
66 -- ERROR 1048 (23000) at line 68: Column 'Name' cannot be
67 -- null
64 68 INSERT INTO SUPPLY INSERT INTO SUPPLY
65 69 VALUES ("Johns & Co.", 121); VALUES ("Johns & Co.", 121);
66 70
67 71 -- The following statement raises an error. -- The following statement raises an error.
72 -- -INSERT INTO SUPPLY
73 -- VALUES ("Coffee Unl.", 311, 221);
74 -- ERROR 1136 (21S01): Column count doesn't match value
75 -- count at row 1
76 -- Rest the changes:
68 77 ROLLBACK; ROLLBACK;
69 78
70 79 -- Question 3: -- Question 3:
File notes/code/sql/HW_Department.sql changed (mode: 100644) (index 96e0329..aa23331)
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 9 CREATE SCHEMA HW_Department; CREATE SCHEMA HW_Department;
9 10
10 11 -- And then re-creating it. -- And then re-creating it.
File notes/code/sql/HW_Faculty.sql changed (mode: 100644) (index e53f345..1d6be25)
... ... INSERT INTO PROF
62 62 290, 'PhD', 0, NULL, '19940101', -- Or '940101', '1994-01-01', '94/01/01' 290, 'PhD', 0, NULL, '19940101', -- Or '940101', '1994-01-01', '94/01/01'
63 63 '090500', -- Or '09:05:00', '9:05:0', '9:5:0', '090500' '090500', -- Or '09:05:00', '9:05:0', '9:5:0', '090500'
64 64 -- Note also the existence of DATETIME, with 'YYYY-MM-DD -- Note also the existence of DATETIME, with 'YYYY-MM-DD
65 -- HH:MM:SS'
65 -- HH:MM:SS'
66 66 'Apple' -- This is not case-sensitive, oddly enough. 'Apple' -- This is not case-sensitive, oddly enough.
67 67 ); );
File notes/code/sql/HW_ProcedureExamples.sql changed (mode: 100644) (index a1638a9..5ee7e4d)
... ... BEGIN
27 27 END; END;
28 28 $$ $$
29 29 -- This is the delimiter that marks the end of the procedure -- This is the delimiter that marks the end of the procedure
30 -- definition.
30 31 DELIMITER ; DELIMITER ;
31 32
32 33 -- Now, we want ";" to be the "natural" delimiter again. -- Now, we want ";" to be the "natural" delimiter again.
 
... ... DELIMITER ;
54 54 SHOW CREATE PROCEDURE STUDENTLOGIN; SHOW CREATE PROCEDURE STUDENTLOGIN;
55 55
56 56 -- This display information about the procedure just created. -- This display information about the procedure just created.
57 -- We can pass quite naturally an argument to our
58 -- procedure.
57 59 CALL STUDENTLOGIN ("Test A"); CALL STUDENTLOGIN ("Test A");
58 60
59 61 -- end snippet procedure-3 -- end snippet procedure-3
File notes/code/sql/HW_ProfExample.sql changed (mode: 100644) (index d56b982..34d394d)
... ... CREATE TABLE GRADE (
37 37 -- end snippet tables-2 -- end snippet tables-2
38 38 -- start snippet insert-1 -- start snippet insert-1
39 39 INSERT INTO DEPARTMENT INSERT INTO DEPARTMENT
40 VALUES ('MATH', 'Mathematics', NULL), ('CS', 'Computer
41 Science', NULL);
40 VALUES ("MATH", "Mathematics", NULL), ("CS", "Computer
41 Science", NULL);
42 42
43 43 -- end snippet insert-1 -- end snippet insert-1
44 44 -- start snippet insert-2 -- start snippet insert-2
45 45 INSERT INTO DEPARTMENT (Code, Name) INSERT INTO DEPARTMENT (Code, Name)
46 VALUES ('CYBR', 'Cyber Secturity');
46 VALUES ("CYBR", "Cyber Secturity");
47 47
48 48 -- end snippet insert-2 -- end snippet insert-2
49 49 -- start snippet insert-3 -- start snippet insert-3
50 50 INSERT INTO PROF (LOGIN, Department, Name) INSERT INTO PROF (LOGIN, Department, Name)
51 VALUES ('caubert', 'CS', 'Clément Aubert');
51 VALUES ("caubert", "CS", "Clément Aubert");
52 52
53 53 INSERT INTO PROF (LOGIN, Name, Department) INSERT INTO PROF (LOGIN, Name, Department)
54 VALUES ('aturing', 'Alan Turing', 'CS'), ('perdos', 'Paul
55 Erdős', 'MATH'), ('bgates', 'Bill Gates', 'CYBR');
54 VALUES ("aturing", "Alan Turing", "CS"), ("perdos", "Paul
55 Erdős", "MATH"), ("bgates", "Bill Gates", "CYBR");
56 56
57 57 INSERT INTO STUDENT (LOGIN, Name, Registered, Major) INSERT INTO STUDENT (LOGIN, Name, Registered, Major)
58 VALUES ('jrakesh', 'Jalal Rakesh', DATE '2017-12-01',
59 'CS'), ('svlatka', 'Sacnite Vlatka', '2015-03-12',
60 'MATH'), ('cjoella', 'Candice Joella', '20120212',
61 'CYBR'), ('aalyx', 'Ava Alyx', 20121011, 'CYBR'),
62 ('caubert', 'Clément Aubert', NULL, 'CYBR');
58 VALUES ("jrakesh", "Jalal Rakesh", DATE "2017-12-01",
59 "CS"), ("svlatka", "Sacnite Vlatka", "2015-03-12",
60 "MATH"), ("cjoella", "Candice Joella", "20120212",
61 "CYBR"), ("aalyx", "Ava Alyx", 20121011, "CYBR"),
62 ("caubert", "Clément Aubert", NULL, "CYBR");
63 63
64 64 INSERT INTO GRADE INSERT INTO GRADE
65 VALUES ('jrakesh', 3.8), ('svlatka', 2.5);
65 VALUES ("jrakesh", 3.8), ("svlatka", 2.5);
66 66
67 67 -- end snippet insert-3 -- end snippet insert-3
68 68 -- start snippet select-update -- start snippet select-update
 
... ... FROM STUDENT;
71 71
72 72 UPDATE UPDATE
73 73 DEPARTMENT DEPARTMENT
74 SET Head = 'aturing'
75 WHERE Code = 'MATH';
74 SET Head = "aturing"
75 WHERE Code = "MATH";
76 76
77 77 UPDATE UPDATE
78 78 DEPARTMENT DEPARTMENT
79 SET Head = 'bgates'
80 WHERE Code = 'CS'
81 OR Code = 'CYBR';
79 SET Head = "bgates"
80 WHERE Code = "CS"
81 OR Code = "CYBR";
82 82
83 83 SELECT LOGIN SELECT LOGIN
84 84 FROM STUDENT FROM STUDENT
85 WHERE NOT Major = 'CYBR';
85 WHERE NOT Major = "CYBR";
86 86
87 87 SELECT LOGIN, Name SELECT LOGIN, Name
88 88 FROM PROF FROM PROF
89 WHERE Department = 'CS';
89 WHERE Department = "CS";
90 90
91 91 SELECT LOGIN SELECT LOGIN
92 92 FROM STUDENT FROM STUDENT
93 WHERE Major = 'CYBR'
94 AND Registered > DATE '20121001';
93 WHERE Major = "CYBR"
94 AND Registered > DATE "20121001";
95 95
96 96 SELECT LOGIN SELECT LOGIN
97 97 FROM STUDENT FROM STUDENT
98 WHERE Name LIKE 'Ava%';
98 WHERE Name LIKE "Ava%";
99 99
100 100 SELECT Name SELECT Name
101 101 FROM PROF FROM PROF
102 WHERE LOGIN LIKE '_aubert';
102 WHERE LOGIN LIKE "_aubert";
103 103
104 104 -- end snippet select-update -- end snippet select-update
105 105 -- start snippet null -- start snippet null
106 106 INSERT INTO DEPARTMENT INSERT INTO DEPARTMENT
107 VALUES ('Hist', 'History', NULL);
107 VALUES ("Hist", "History", NULL);
108 108
109 109 SELECT * SELECT *
110 110 FROM DEPARTMENT FROM DEPARTMENT
 
... ... WHERE DEPARTMENT.Name = "Mathematics"
142 142 AND Department = Code; AND Department = Code;
143 143
144 144 -- end snippet select-project-join-1 -- end snippet select-project-join-1
145 -- start snippet select-project-join-2
145 146 SELECT Name SELECT Name
146 147 FROM STUDENT, GRADE FROM STUDENT, GRADE
147 148 WHERE Grade > 3.0 WHERE Grade > 3.0
148 149 AND STUDENT.Login = GRADE.Login; AND STUDENT.Login = GRADE.Login;
149 150
150 151 -- end snippet select-project-join-2 -- end snippet select-project-join-2
152 -- start snippet select-project-join-3
151 153 SELECT PROF.Name SELECT PROF.Name
152 154 FROM PROF, DEPARTMENT, STUDENT FROM PROF, DEPARTMENT, STUDENT
153 155 WHERE STUDENT.Name = "Ava Alyx" WHERE STUDENT.Name = "Ava Alyx"
 
... ... FROM PROF
226 226 WHERE DEPARTMENT IN ( WHERE DEPARTMENT IN (
227 227 SELECT Major SELECT Major
228 228 FROM STUDENT FROM STUDENT
229 WHERE LOGIN LIKE '%a');
229 WHERE LOGIN LIKE "%a");
230 230
231 231 -- end snippet whodunit -- end snippet whodunit
232 232 -- start snippet transf-1a -- start snippet transf-1a
File notes/code/sql/HW_ResidencySol.sql changed (mode: 100644) (index 4c2f3a2..29ba036)
... ... INSERT INTO RESIDENCY
54 54 the next question. the next question.
55 55 */ */
56 56 -- Exercise 4 -- Exercise 4
57 -- List the rows (i.e., P.2, H.1, or even “none”)
58 -- modified by the following statements:
57 59 START TRANSACTION; START TRANSACTION;
58 60
59 61 UPDATE UPDATE
 
... ... ROLLBACK;
84 84 START TRANSACTION; START TRANSACTION;
85 85
86 86 -- Commented, because it causes an error. -- Commented, because it causes an error.
87 -- DELETE FROM PERSON
88 -- WHERE Birthdate = DATE "1990-02-11";
89 -- None, because of the foreign key and the referential
90 -- integrity constraint.
91 -- ERROR 1451 (23000): Cannot delete or update a parent
92 -- row:
93 -- a foreign key constraint fails
94 -- (`HW_RESIDENCY_SOL`.`RESIDENCY`, CONSTRAINT
95 -- `RESIDENCY_ibfk_1` FOREIGN KEY (`Person`) REFERENCES
96 -- `PERSON` (`SSN`))
87 97 ROLLBACK; ROLLBACK;
88 98
89 99 -- end snippet solution4 -- end snippet solution4
 
... ... SELECT Address
106 106 FROM HOUSE; FROM HOUSE;
107 107
108 108 -- … the SSN of the persons whose first name was not -- … the SSN of the persons whose first name was not
109 -- entered in the system (000-00-0000).
109 110 SELECT SSN SELECT SSN
110 111 FROM PERSON FROM PERSON
111 112 WHERE FName IS NULL; WHERE FName IS NULL;
 
... ... SELECT DISTINCT COLOR
116 116 FROM HOUSE; FROM HOUSE;
117 117
118 118 -- … the address of the residency of James Baldwin (123 -- … the address of the residency of James Baldwin (123
119 -- Main St.).
119 120 SELECT House SELECT House
120 121 FROM RESIDENCY, PERSON FROM RESIDENCY, PERSON
121 122 WHERE PERSON.Fname = "James" WHERE PERSON.Fname = "James"
 
... ... WHERE PERSON.Fname = "James"
124 124 AND PERSON.SSN = RESIDENCY.Person; AND PERSON.SSN = RESIDENCY.Person;
125 125
126 126 -- … the first name of the oldest person in the database -- … the first name of the oldest person in the database
127 -- (James).
127 128 SELECT FName SELECT FName
128 129 FROM PERSON FROM PERSON
129 130 WHERE Birthdate = ( WHERE Birthdate = (
 
... ... WHERE Birthdate = (
133 133 WHERE Birthdate IS NOT NULL); WHERE Birthdate IS NOT NULL);
134 134
135 135 -- … Michael Keal’s principal residency address (123 Main -- … Michael Keal’s principal residency address (123 Main
136 -- St.).
136 137 SELECT RESIDENCY.House SELECT RESIDENCY.House
137 138 FROM RESIDENCY, PERSON FROM RESIDENCY, PERSON
138 139 WHERE PERSON.FName = "Michael" WHERE PERSON.FName = "Michael"
 
... ... WHERE PERSON.FName = "Michael"
142 142 AND RESIDENCY.PrincipalResidence = TRUE; AND RESIDENCY.PrincipalResidence = TRUE;
143 143
144 144 -- … the (distinct) first and last names of the homeowners -- … the (distinct) first and last names of the homeowners
145 -- (Michael Keal, Mridula Warrier).
145 146 SELECT DISTINCT (PERSON.FName), PERSON.LName SELECT DISTINCT (PERSON.FName), PERSON.LName
146 147 FROM PERSON, RESIDENCY FROM PERSON, RESIDENCY
147 148 WHERE RESIDENCY.Status = "own" WHERE RESIDENCY.Status = "own"
 
... ... WHERE SSN IN ( SELECT DISTINCT (RESIDENCY.Person)
156 156 WHERE RESIDENCY.Status = "own"); WHERE RESIDENCY.Status = "own");
157 157
158 158 -- … the SSN of the persons that have the same principal -- … the SSN of the persons that have the same principal
159 -- residency as James Baldwin (000-00-0001).
159 160 SELECT RoomMate.Person SELECT RoomMate.Person
160 161 FROM RESIDENCY AS James, RESIDENCY AS RoomMate, PERSON FROM RESIDENCY AS James, RESIDENCY AS RoomMate, PERSON
161 162 WHERE PERSON.FName = "James" WHERE PERSON.FName = "James"
 
... ... WHERE PERSON.FName = "James"
170 170 START TRANSACTION; START TRANSACTION;
171 171
172 172 -- start snippet homonyms -- start snippet homonyms
173 -- If we have homonymns in our database, e.g.
173 174 INSERT INTO PERSON INSERT INTO PERSON
174 175 VALUES ("A", "B", "000-00-0010", NULL), ("A", "B", VALUES ("A", "B", "000-00-0010", NULL), ("A", "B",
175 176 "000-00-0011", NULL); "000-00-0011", NULL);
 
... ... INSERT INTO RESIDENCY
184 184 "H", TRUE, "own"); "H", TRUE, "own");
185 185
186 186 -- Then the query below fails, in the sense that it reports -- Then the query below fails, in the sense that it reports
187 -- the name "A, B" only once.
187 188 SELECT DISTINCT (PERSON.FName), PERSON.LName SELECT DISTINCT (PERSON.FName), PERSON.LName
188 189 FROM PERSON, RESIDENCY FROM PERSON, RESIDENCY
189 190 WHERE RESIDENCY.Status = "own" WHERE RESIDENCY.Status = "own"
190 191 AND RESIDENCY.Person = PERSON.SSN; AND RESIDENCY.Person = PERSON.SSN;
191 192
192 193 -- A better (and not much more complicated) solution would -- A better (and not much more complicated) solution would
194 -- have been
193 195 SELECT PERSON.FName, PERSON.LName SELECT PERSON.FName, PERSON.LName
194 196 FROM PERSON FROM PERSON
195 197 WHERE SSN IN ( SELECT DISTINCT (RESIDENCY.Person) WHERE SSN IN ( SELECT DISTINCT (RESIDENCY.Person)
File notes/code/sql/HW_SocialMedia.sql changed (mode: 100644) (index 58d8848..965e270)
... ... INSERT INTO SUBSCRIBE
39 39 "2019-03-03"), (1, 2, DATE "2019-03-03"); "2019-03-03"), (1, 2, DATE "2019-03-03");
40 40
41 41 -- The first entry means that 2 subscribed to 1, not the -- The first entry means that 2 subscribed to 1, not the
42 -- other way around.
43 -- And similarly for the other entries.
42 44 INSERT INTO VIDEO INSERT INTO VIDEO
43 45 VALUES (10, "My first video!", DATE "2020-02-02", 1), VALUES (10, "My first video!", DATE "2020-02-02", 1),
44 46 (20, "My second video!", DATE "2020-02-03", 1), (30, (20, "My second video!", DATE "2020-02-03", 1), (30,
 
... ... INSERT INTO THUMBS_UP
63 63 -- start snippet solution -- start snippet solution
64 64 /* code/sql/HW_SocialMedia.sql */ /* code/sql/HW_SocialMedia.sql */
65 65 -- … the title of all the videos ("My first video!", "My -- … the title of all the videos ("My first video!", "My
66 -- second video!", "My vacations").
66 67 SELECT TITLE SELECT TITLE
67 68 FROM VIDEO; FROM VIDEO;
68 69
69 70 -- … the release date of the video whose title is "My first -- … the release date of the video whose title is "My first
71 -- video!" ("2020-02-02").
70 72 SELECT Released SELECT Released
71 73 FROM VIDEO FROM VIDEO
72 74 WHERE Title = "My first video!"; WHERE Title = "My first video!";
73 75
74 76 -- … the ID of the account(s) where the "Name" attribute -- … the ID of the account(s) where the "Name" attribute
77 -- was not given ("2").
75 78 SELECT ID SELECT ID
76 79 FROM ACCOUNT FROM ACCOUNT
77 80 WHERE Name IS NULL; WHERE Name IS NULL;
78 81
79 82 -- … the ID of the videos whose title contains the word -- … the ID of the videos whose title contains the word
83 -- "video" ("10", "20").
80 84 SELECT ID SELECT ID
81 85 FROM VIDEO FROM VIDEO
82 86 WHERE TITLE LIKE "%video%"; WHERE TITLE LIKE "%video%";
 
... ... FROM VIDEO
91 91 WHERE Title REGEXP 'video'; WHERE Title REGEXP 'video';
92 92
93 93 -- … the number of thumbs up for the video with title "My -- … the number of thumbs up for the video with title "My
94 -- vacations" ("1").
94 95 SELECT COUNT(*) SELECT COUNT(*)
95 96 FROM THUMBS_UP, VIDEO FROM THUMBS_UP, VIDEO
96 97 WHERE VIDEO.Title = "My vacations" WHERE VIDEO.Title = "My vacations"
 
... ... ORDER BY Released ASC
118 118 LIMIT 1; LIMIT 1;
119 119
120 120 -- … the names of the accounts who gave a thumbs up to the -- … the names of the accounts who gave a thumbs up to the
121 -- video with id 30 ("Bob Ross").
121 122 SELECT Name SELECT Name
122 123 FROM ACCOUNT, THUMBS_UP FROM ACCOUNT, THUMBS_UP
123 124 WHERE THUMBS_UP.Video = 30 WHERE THUMBS_UP.Video = 30
124 125 AND THUMBS_UP.Account = ACCOUNT.ID; AND THUMBS_UP.Account = ACCOUNT.ID;
125 126
126 127 -- … the ID of the account with the greatest number of -- … the ID of the account with the greatest number of
128 -- subscribers ("2").
127 129 SELECT Subscribed SELECT Subscribed
128 130 FROM SUBSCRIBE FROM SUBSCRIBE
129 131 GROUP BY Subscribed GROUP BY Subscribed
File notes/code/sql/HW_Storm.sql changed (mode: 100644) (index 590aab2..ba8c866)
... ... INSERT INTO STORM
29 29 VALUES ('Harvey', 'Hurricane', 130, '2017-08-17'); VALUES ('Harvey', 'Hurricane', 130, '2017-08-17');
30 30
31 31 -- In the following, the entry gets created, but date is -- In the following, the entry gets created, but date is
32 -- "corrected" to '2017-17-08'!
33 -- INSERT INTO STORM
34 -- VALUES ('Dummy', 'Hurricane', 120, '2017-17-08');
35 -- The error message returned is
36 -- ERROR 1292 (22007) at line 34: Incorrect date value:
37 -- '2017-17-08' for column `HW_STORM`.`STORM`.`Creation`
38 -- at
39 -- row 1
40 -- In the following, we explicitely use 'DATE', and since
41 -- the date is incorrect, nothing gets inserted.
42 -- INSERT INTO STORM
43 -- VALUES ('Dummy2', 'Hurricane', 120, DATE
44 -- '2017-17-08');
45 -- ERROR 1525 (HY000) at line 40: Incorrect DATE value:
46 -- '2017-17-08'
47 -- The next one sets NULL for DATE.
32 48 INSERT INTO STORM INSERT INTO STORM
33 49 VALUES ('Irma', 'Tropical Storm', 102, DEFAULT); VALUES ('Irma', 'Tropical Storm', 102, DEFAULT);
34 50
 
... ... INSERT INTO STATE
59 60 VALUES ('Florida', 'FL', NULL); VALUES ('Florida', 'FL', NULL);
60 61
61 62 -- This instruction is not using the primary key, is that a -- This instruction is not using the primary key, is that a
63 -- problem?
62 64 UPDATE UPDATE
63 65 STATE STATE
64 66 SET Affected_by = 'Harvey' SET Affected_by = 'Harvey'
File notes/code/sql/HW_TriggerExample.sql changed (mode: 100644) (index e3cc065..c5ddeb3)
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 9 CREATE SCHEMA HW_TriggerExample; CREATE SCHEMA HW_TriggerExample;
6 10
7 11 USE HW_TriggerExample; USE HW_TriggerExample;
 
... ... CREATE TRIGGER STUDENT_AVERAGE
72 72 WHERE STUDENT.Login = NEW.Student; WHERE STUDENT.Login = NEW.Student;
73 73
74 74 -- The "NEW" keyword here refers to the "new" entry -- The "NEW" keyword here refers to the "new" entry
75 -- that is being inserted by the INSERT statement
76 -- triggering
77 -- the trigger.
78 -- end snippet trigger-3
75 79 INSERT INTO GRADE INSERT INTO GRADE
76 80 VALUES ("A", "Exam 1", 50), ("A", "Exam 2", 40), ("B", VALUES ("A", "Exam 1", 50), ("A", "Exam 2", 40), ("B",
77 81 "Exam 1", 80), ("B", "Exam 2", 100); "Exam 1", 80), ("B", "Exam 2", 100);
 
... ... SELECT *
87 87 FROM STUDENT; FROM STUDENT;
88 88
89 89 -- Tada, all the averages have been computed! -- Tada, all the averages have been computed!
90 -- Note also that the student "C" does not have an
91 -- average!
File notes/code/sql/HW_Work.sql changed (mode: 100644) (index cd7d88c..543f0bf)
... ... INSERT INTO EBOOK
78 78 So, "Successful insertion". So, "Successful insertion".
79 79 */ */
80 80 -- The following statement raises an error. -- The following statement raises an error.
81 -- INSERT INTO AUTHOR
82 -- VALUES ("Mary B.", "mb@fai.fr", NULL);
81 83 /* /*
82 84 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
83 85 So, "Other kind of error". So, "Other kind of error".
84 86 */ */
85 87 -- The following statement raises an error. -- The following statement raises an error.
88 -- INSERT INTO WORK
89 -- VALUES ("My Life", "Claude A.");
86 90 /* /*
87 91 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
88 92 (`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`)
 
... ... INSERT INTO BOOK
102 102 So, "Successful insertion". So, "Successful insertion".
103 103 */ */
104 104 -- The following statement raises an error. -- The following statement raises an error.
105 -- INSERT INTO AUTHOR
106 -- VALUES ("Virginia W.", "alt@isp.net");
105 107 /* /*
106 108 ERROR 1062 (23000): Duplicate entry 'Virginia W.' for key 'PRIMARY' ERROR 1062 (23000): Duplicate entry 'Virginia W.' for key 'PRIMARY'
107 109 So, "Entity integrity constraint". So, "Entity integrity constraint".
 
... ... WHERE Title = "What to eat";
149 149 Does not change any row. Does not change any row.
150 150 */ */
151 151 -- The following statement raises an error. -- The following statement raises an error.
152 -- DELETE FROM AUTHOR
153 -- WHERE Name = "Virginia W.";
152 154 /* /*
153 155 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
154 156 (`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";
159 159 ROLLBACK; ROLLBACK;
160 160
161 161 -- We go back to the previous state. -- We go back to the previous state.
162 -- You can now assume that there is more data than what
163 -- we
164 -- inserted, if that helps you. Write a command that
165 -- selects
166 -- …
167 -- We insert some dummy values for this next part.
162 168 INSERT INTO WORK INSERT INTO WORK
163 169 VALUES ("My Life", "Paul B."), ("What to eat, 2", "Virginia W."); VALUES ("My Life", "Paul B."), ("What to eat, 2", "Virginia W.");
164 170
 
... ... SELECT Price
180 181 FROM EBOOK; FROM EBOOK;
181 182
182 183 -- … the (distinct) names of the authors who have authored -- … the (distinct) names of the authors who have authored
184 -- a piece of work.
183 185 SELECT DISTINCT Author SELECT DISTINCT Author
184 186 FROM WORK; FROM WORK;
185 187
 
... ... SELECT MAX(Price)
206 207 FROM BOOK; FROM BOOK;
207 208
208 209 -- … the number of pieces of work written by the author -- … the number of pieces of work written by the author
210 -- whose name is “Virginia W.”.
209 211 SELECT COUNT(*) SELECT COUNT(*)
210 212 FROM WORK FROM WORK
211 213 WHERE WORK.Author = "Virginia W."; WHERE WORK.Author = "Virginia W.";
212 214
213 215 -- … the email of the author who wrote the piece of work -- … the email of the author who wrote the piece of work
216 -- called “My Life”.
214 217 SELECT Email SELECT Email
215 218 FROM AUTHOR, WORK FROM AUTHOR, WORK
216 219 WHERE WORK.Title = "My Life" WHERE WORK.Title = "My Life"
217 220 AND WORK.Author = AUTHOR.Name; AND WORK.Author = AUTHOR.Name;
218 221
219 222 -- the isbn(s) of the book containing a work written by the -- the isbn(s) of the book containing a work written by the
223 -- author whose email is "vw@isp.net".
220 224 SELECT ISBN SELECT ISBN
221 225 FROM BOOK, WORK, AUTHOR FROM BOOK, WORK, AUTHOR
222 226 WHERE AUTHOR.Email = "vw@isp.net" WHERE AUTHOR.Email = "vw@isp.net"
 
... ... WHERE
245 246 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.
246 247 */ */
247 248 -- Write one or multiple commands that would delete the work -- Write one or multiple commands that would delete the work
249 -- whose title is “My Life”, as well as all of the
250 -- books
251 -- and ebooks versions of it.
252 -- The following statement raises an error.
253 -- DELETE FROM WORK
254 -- WHERE Title = "My Life";
248 255 /* /*
249 256 Fails Fails
250 257 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 da5788c..6d87395)
... ... Go back to the CAR example and populate it with some data to "see" how those opt
941 941
942 942 ## Exercises {-} ## Exercises {-}
943 943
944 Exercise .+#
945
946 : What are the meta-data and the data called in the relational model?
947
944 948 Exercise +.# Exercise +.#
945 949 ~ ~
946 950
 
... ... Exercise +.#
1115 1119
1116 1120 Solution +.# Solution +.#
1117 1121
1122 : The meta-data is called the _schema_, and the data is called the _relation state_. You can refer to the [diagram we studied at the beginnig of the Chapter](#concepts-1) for a reminder.
1123
1124 Solution +.#
1125
1118 1126 : Row is Tuple, Column header is Attribute, Table is Relation. : Row is Tuple, Column header is Attribute, Table is Relation.
1119 1127
1120 1128 Solution +.# Solution +.#
 
... ... Solution +.#
1209 1217 - $\{A\}$ is not a key, and not a superkey: multiple tuples have the value $1$. - $\{A\}$ is not a key, and not a superkey: multiple tuples have the value $1$.
1210 1218
1211 1219 Solution +.# Solution +.#
1212 ~ Possible superkeys are $\{A, B, C, D\}$, $\{A, B, C\}$, $\{A, C, D\}$, $\{B, C, D\}$, $\{A, B\}$, $\{B, C\}$, . The possible keys are $\{A, B\}$ $\{A, C\}$, and $\{B, C\}$.
1220 ~ For this relation, $\{A, B, C, D\}$, $\{A, B, C\}$, and $\{D\}$ are superkey. Only the latter, $\{D\}$, is a key (for $\{A, B, C\}$, removing either $A$ or $C$ still gives a superkey).
1213 1221
1214 1222 Solution +.# Solution +.#
1215 ~ For this relation, $\{A, B, C, D\}$, $\{A, B, C\}$, and $\{D\}$ are superkey. Only the latter, $\{D\}$, is a key (for $\{A, B, C\}$, removing either $A$ or $C$ still gives a superkey).
1223 ~ Possible superkeys are $\{A, B, C, D\}$, $\{A, B, C\}$, $\{A, C, D\}$, $\{B, C, D\}$, $\{A, B\}$, $\{B, C\}$ . The possible keys are $\{A, B\}$ $\{A, C\}$, and $\{B, C\}$.
1216 1224
1217 1225 Solution +.# Solution +.#
1218 1226 ~ ~
 
... ... OFFERING (Department (PK, FK to DEPARTMENT.Name), Course (PK, FK to COURSE.Name)
1378 1386 ## Resources {-} ## Resources {-}
1379 1387
1380 1388 - [@Textbook6, ch. 4--5], [@Textbook7, ch. 6--7] describes `SQL`, but none of its implementation. - [@Textbook6, ch. 4--5], [@Textbook7, ch. 6--7] describes `SQL`, but none of its implementation.
1381 - To compare DBMS, you can look at their features, at <https://en.wikipedia.org/wiki/Comparison_of_relational_database_management_systems>, and at their popularity at <https://db-engines.com/en/ranking> and <https://insights.stackoverflow.com/survey/2019/#technology-_-databases> (if you sum up MySQL and MariaDB, they are first in both).
1389 - To compare DBMS, you can look at their features, at <https://en.wikipedia.org/wiki/Comparison_of_relational_database_management_systems>, and at their popularity at <https://db-engines.com/en/ranking> and <https://insights.stackoverflow.com/survey/2020/#technology-databases> (if you sum up MySQL and MariaDB, they are first in both).
1382 1390 - MySQL and MariaDB have some differences, you can look them up at < https://mariadb.com/kb/en/mariadb-vs-mysql-features/> and <https://mariadb.com/kb/en/mariadb-vs-mysql-compatibility/>. - MySQL and MariaDB have some differences, you can look them up at < https://mariadb.com/kb/en/mariadb-vs-mysql-features/> and <https://mariadb.com/kb/en/mariadb-vs-mysql-compatibility/>.
1383 1391
1384 1392 This chapter will be "code-driven": the code will illustrate and help you understand some concepts. This chapter will be "code-driven": the code will illustrate and help you understand some concepts.
1385 You may want to have a look at the ["Setting Up Your Work Environment"](#sec:setup) Section, as early as possible in this lecture.
1393 You may want to have a look at the ["Setting Up Your Work Environment"](#sec:setup) Section as early as possible in this lecture.
1386 1394 On top of being a step-by-step guide to install and configure a relational database managment system, it contains a list of useful links. On top of being a step-by-step guide to install and configure a relational database managment system, it contains a list of useful links.
1387 1395
1388 1396 ## Actors ## Actors
 
... ... They will be studied separately, in the [Presentation of NoSQL](#presentation-of
1407 1415 - **D**ata **C**ontrol **L**anguage (authorizations, users), - **D**ata **C**ontrol **L**anguage (authorizations, users),
1408 1416 - **D**ata **M**anipulation **L**anguage (insert, update and delete). - **D**ata **M**anipulation **L**anguage (insert, update and delete).
1409 1417
1410 The three last sublanguages being dubbed "**D**ata **M**anipulation **L**anguage".
1418 <!--
1419 The three last sublanguages being dubbed "**D**ata **M**anipulation **L**anguage".
1420 -->
1411 1421
1412 1422 ### SQL ### SQL
1413 1423
 
... ... Type and domains are two different things in some implementations, cf. for insta
1441 1451 `SQL` _is_ a programming language: it has a strict syntax, sometimes cryptic error messages, it evolves, etc. `SQL` _is_ a programming language: it has a strict syntax, sometimes cryptic error messages, it evolves, etc.
1442 1452 Some of its salient aspects are: Some of its salient aspects are:
1443 1453
1444 - `SQL` is "kind of" case-insensitive^[The `SQL` keywords are case-insensitive, but the table and schema names are sometimes case-sensitive, it depends of the actual implementation. For instance, MySQL is completely case-insensitive (reserved words, tables, attributes), MariaDB is not (the case for table names matter).], does not care about spaces and new lines
1454 - `SQL` is "kind of" case-insensitive^[The `SQL` keywords are case-insensitive, but the table and schema names are sometimes case-sensitive, it depends of the actual implementation. For instance, MySQL is completely case-insensitive (reserved words, tables, attributes), MariaDB is not (the case for table names matter).], does not care about spaces and new lines.
1445 1455 - In-line comments are what is after `--`, multi-line comments uses `/* …*/`. - In-line comments are what is after `--`, multi-line comments uses `/* …*/`.
1446 1456 - Every statement ends with a `;`. - Every statement ends with a `;`.
1447 1457 - The exact syntax is left as an exercise in @problem:sqldoc. - The exact syntax is left as an exercise in @problem:sqldoc.
 
... ... For aggregate functions, cf. [@Textbook6, 5.1.7] or [@Textbook7, 7.1.7].
2089 2099 ### `AUTO_INCREMENT` ### `AUTO_INCREMENT`
2090 2100
2091 2101 Something that is not exactly a constraint, but that can be used to "qualify" domains, is the `AUTO_INCREMENT` feature of MySQL. Something that is not exactly a constraint, but that can be used to "qualify" domains, is the `AUTO_INCREMENT` feature of MySQL.
2092 Cf. <https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html>, you can have MySQL increment a particular attribute (most probably intended to be your primary key) for you.
2102 Cf. <https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html>, you can have MySQL increment a particular attribute (most probably intended to be your primary key, or some form of counter) for you.
2093 2103
2094 2104 ### Transactions ### Transactions
2095 2105
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