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