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_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 |
|
|