File notes/code/sql/HW_AdvancedFK.sql changed (mode: 100644) (index 8a12e14..ec81722) |
... |
... |
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 |
|
35 |
|
-- "My_pk_to_T1") |
|
36 |
|
-- would be displayed in the error message: |
|
37 |
|
-- Cannot add or update a child row: a foreign key |
|
38 |
|
-- constraint fails (`db_9_9837c1`.`t2`, CONSTRAINT |
|
39 |
|
-- `My_pk_to_T1` FOREIGN KEY (`B2`) REFERENCES `t2` |
|
40 |
|
-- (`B1`)) |
|
41 |
|
-- end snippet advancedFK |
File notes/code/sql/HW_CapstoneSol.sql changed (mode: 100644) (index 90524f2..0139319) |
... |
... |
VALUES ( |
79 |
79 |
-- |
-- |
80 |
80 |
-- |
-- |
81 |
81 |
-- |
-- |
|
82 |
|
-- |
82 |
83 |
-- https://en.wikipedia.org/wiki/Comparison_of_open-source_pr |
-- https://en.wikipedia.org/wiki/Comparison_of_open-source_pr |
|
84 |
|
-- ogramming_language_licensing |
83 |
85 |
INSERT INTO PROJECT |
INSERT INTO PROJECT |
84 |
86 |
VALUES ( |
VALUES ( |
85 |
87 |
"Brick Break", |
"Brick Break", |
|
... |
... |
VALUES ( |
117 |
118 |
/* |
/* |
118 |
119 |
* You can start editing starting here. |
* You can start editing starting here. |
119 |
120 |
*/ |
*/ |
120 |
|
|
|
121 |
121 |
-- start snippet solution |
-- start snippet solution |
122 |
122 |
/* |
/* |
123 |
123 |
code/sql/HW_CapstoneSol.sql |
code/sql/HW_CapstoneSol.sql |
124 |
124 |
*/ |
*/ |
125 |
|
|
|
126 |
125 |
/* |
/* |
127 |
126 |
|
|
128 |
127 |
I. Short Questions (6 pts) |
I. Short Questions (6 pts) |
|
... |
... |
Answer the following short questions based on the model implemented above. |
131 |
130 |
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). |
132 |
131 |
*/ |
*/ |
133 |
132 |
-- 1. Can a project uses multiple programming languages? |
-- 1. Can a project uses multiple programming languages? |
|
133 |
|
-- Yes. |
|
134 |
|
-- 2. Can a student be the leader of multiple |
|
135 |
|
-- projects? |
|
136 |
|
-- Yes. |
|
137 |
|
-- 3. Can multiple projects have the same code name? |
|
138 |
|
-- Yes. |
|
139 |
|
-- 4. Could Claude simply enter NULL for the value of |
|
140 |
|
-- his |
|
141 |
|
-- project's code name, since he's undecided? |
|
142 |
|
-- No. |
|
143 |
|
-- 5. Can a project be created without project leader? |
|
144 |
|
-- No. |
|
145 |
|
-- 6. Can we know who is working on a project without |
|
146 |
|
-- being |
|
147 |
|
-- its leader? |
|
148 |
|
-- No. |
134 |
149 |
/* |
/* |
135 |
150 |
|
|
136 |
151 |
II. Relational Model (6 pts.) |
II. Relational Model (6 pts.) |
|
... |
... |
Please, leave them uncommented, unless you can't write them correctly, in which |
162 |
162 |
The first question is answered as an example. |
The first question is answered as an example. |
163 |
163 |
*/ |
*/ |
164 |
164 |
-- 0. Write a command that list all the names of the |
-- 0. Write a command that list all the names of the |
|
165 |
|
-- programming languages. |
165 |
166 |
SELECT Name |
SELECT Name |
166 |
167 |
FROM PROGRAMMING_LANGUAGE; |
FROM PROGRAMMING_LANGUAGE; |
167 |
168 |
|
|
168 |
169 |
-- 1. Write a command that insert a new student in the |
-- 1. Write a command that insert a new student in the |
|
170 |
|
-- STUDENT table. |
|
171 |
|
-- (You should invent the values). |
169 |
172 |
INSERT INTO STUDENT |
INSERT INTO STUDENT |
170 |
173 |
VALUES ( |
VALUES ( |
171 |
174 |
"Bob", |
"Bob", |
|
... |
... |
VALUES ( |
177 |
177 |
NULL); |
NULL); |
178 |
178 |
|
|
179 |
179 |
-- 2. Write a command that updates the code name of the |
-- 2. Write a command that updates the code name of the |
|
180 |
|
-- project ("Undecided", "9999999999999") to "VR in |
|
181 |
|
-- ER". |
180 |
182 |
UPDATE |
UPDATE |
181 |
183 |
PROJECT |
PROJECT |
182 |
184 |
SET CodeName = "VR in ER" |
SET CodeName = "VR in ER" |
|
... |
... |
WHERE CodeName = "Undecided" |
185 |
186 |
AND Leader = "9999999999999"; |
AND Leader = "9999999999999"; |
186 |
187 |
|
|
187 |
188 |
-- 3. Write a command that updates the graduation year of the |
-- 3. Write a command that updates the graduation year of the |
|
189 |
|
-- student whose id is "0987654321098" to 2024, and the |
|
190 |
|
-- semester to "Fall". |
188 |
191 |
UPDATE |
UPDATE |
189 |
192 |
STUDENT |
STUDENT |
190 |
193 |
SET GraduationYear = 2024, |
SET GraduationYear = 2024, |
|
... |
... |
SET GraduationYear = 2024, |
194 |
195 |
WHERE id = "0987654321098"; |
WHERE id = "0987654321098"; |
195 |
196 |
|
|
196 |
197 |
-- 4. Write a command that changes the STUDENT table to make |
-- 4. Write a command that changes the STUDENT table to make |
|
198 |
|
-- it impossible to enter NULL for the first name of a |
|
199 |
|
-- student, without changing the primary key. |
197 |
200 |
ALTER TABLE STUDENT MODIFY FName VARCHAR(50) NOT NULL; |
ALTER TABLE STUDENT MODIFY FName VARCHAR(50) NOT NULL; |
198 |
201 |
|
|
199 |
202 |
-- 5. Write a command that changes the datatype of |
-- 5. Write a command that changes the datatype of |
|
203 |
|
-- GraduationYear to SMALLINT. |
200 |
204 |
ALTER TABLE STUDENT MODIFY GraduationYear SMALLINT; |
ALTER TABLE STUDENT MODIFY GraduationYear SMALLINT; |
201 |
205 |
|
|
202 |
206 |
-- 6. Write a command that adds an attribute "ReleaseDate" to |
-- 6. Write a command that adds an attribute "ReleaseDate" to |
|
207 |
|
-- the PROJECT table. |
203 |
208 |
ALTER TABLE PROJECT |
ALTER TABLE PROJECT |
204 |
209 |
ADD COLUMN ReleaseDate DATE; |
ADD COLUMN ReleaseDate DATE; |
205 |
210 |
|
|
206 |
211 |
-- 6.bis If you managed to write the previous command |
-- 6.bis If you managed to write the previous command |
|
212 |
|
-- correctly, write a command that sets the release |
|
213 |
|
-- date |
|
214 |
|
-- of |
|
215 |
|
-- the project ("Brick Break", "0123456789100") to the |
|
216 |
|
-- 26th |
|
217 |
|
-- of November 2022. |
207 |
218 |
UPDATE |
UPDATE |
208 |
219 |
PROJECT |
PROJECT |
209 |
220 |
SET ReleaseDate = DATE "20221126" |
SET ReleaseDate = DATE "20221126" |
|
... |
... |
WHERE CodeName = "Brick Break" |
220 |
222 |
AND Leader = "0123456789100"; |
AND Leader = "0123456789100"; |
221 |
223 |
|
|
222 |
224 |
-- 7. Write a command that makes it impossible for a student |
-- 7. Write a command that makes it impossible for a student |
|
225 |
|
-- to be the leader in more than one project |
|
226 |
|
-- (This command should return an error) |
|
227 |
|
-- ALTER TABLE PROJECT ADD UNIQUE (Leader); |
File notes/code/sql/HW_Certificate.sql changed (mode: 100644) (index 2cd79b9..03e33ff) |
... |
... |
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 |
|
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 |
140 |
140 |
AND CA.CN = "Let's encrypt"; |
AND CA.CN = "Let's encrypt"; |
141 |
141 |
|
|
142 |
142 |
-- (2) |
-- (2) |
143 |
|
-- A table listing the CN of the organizations |
|
144 |
|
-- the CN of their certificates. |
|
|
143 |
|
-- A table listing the CN of the organizations |
|
144 |
|
-- along |
|
145 |
|
-- with |
|
146 |
|
-- the CN of their certificates. |
145 |
147 |
SELECT ORGANIZATION.CN AS Organization, |
SELECT ORGANIZATION.CN AS Organization, |
146 |
148 |
CERTIFICATE.CN AS Certificate |
CERTIFICATE.CN AS Certificate |
147 |
149 |
FROM ORGANIZATION, |
FROM ORGANIZATION, |
|
... |
... |
FROM ORGANIZATION, |
151 |
151 |
WHERE CERTIFICATE.Org = ORGANIZATION.SN; |
WHERE CERTIFICATE.Org = ORGANIZATION.SN; |
152 |
152 |
|
|
153 |
153 |
-- ( Wikimedia Foundation, *.wikimedia.org | Free Software |
-- ( Wikimedia Foundation, *.wikimedia.org | Free Software |
154 |
|
-- Foundation, *.fsf.org | Free Software Foundation |
|
155 |
|
-- *.shadytest.org | Wikimedia Foundation , |
|
156 |
|
-- ) |
|
|
154 |
|
-- Foundation, *.fsf.org | Free Software Foundation |
|
155 |
|
-- , |
|
156 |
|
-- *.shadytest.org | Wikimedia Foundation , |
|
157 |
|
-- *.wikipedia.org |
|
158 |
|
-- ) |
157 |
159 |
/* |
/* |
158 |
160 |
DELETE FROM CA WHERE SN = 'A'; |
DELETE FROM CA WHERE SN = 'A'; |
159 |
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 2c94f53..c31ab37) |
... |
... |
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_ScientificResearch.sql changed (mode: 100644) (index 408fa6e..9c4d322) |
1 |
|
/* code/sql/HW_SCIENTIFIC_RESEARCH.sql */ |
|
2 |
|
|
|
3 |
|
|
|
4 |
1 |
DROP SCHEMA IF EXISTS HW_SCIENTIFIC_RESEARCH; |
DROP SCHEMA IF EXISTS HW_SCIENTIFIC_RESEARCH; |
|
2 |
|
|
5 |
3 |
CREATE SCHEMA HW_SCIENTIFIC_RESEARCH; |
CREATE SCHEMA HW_SCIENTIFIC_RESEARCH; |
6 |
|
USE HW_SCIENTIFIC_RESEARCH; |
|
7 |
4 |
|
|
|
5 |
|
USE HW_SCIENTIFIC_RESEARCH; |
8 |
6 |
|
|
|
7 |
|
-- start snippet statement |
|
8 |
|
/* code/sql/HW_ScientificResearch.sql */ |
9 |
9 |
CREATE TABLE SCIENTIST ( |
CREATE TABLE SCIENTIST ( |
10 |
10 |
SSN INT PRIMARY KEY, |
SSN INT PRIMARY KEY, |
11 |
11 |
Name VARCHAR(30) NOT NULL |
Name VARCHAR(30) NOT NULL |
|
... |
... |
CREATE TABLE PROJECT ( |
15 |
15 |
Code CHAR(4) PRIMARY KEY, |
Code CHAR(4) PRIMARY KEY, |
16 |
16 |
Name VARCHAR(150) NOT NULL |
Name VARCHAR(150) NOT NULL |
17 |
17 |
); |
); |
18 |
|
|
|
|
18 |
|
|
19 |
19 |
CREATE TABLE CONTRIBUTESTO ( |
CREATE TABLE CONTRIBUTESTO ( |
20 |
20 |
Scientist INT, |
Scientist INT, |
21 |
21 |
Project CHAR(4), |
Project CHAR(4), |
22 |
|
Hours INT NOT NULL CHECK (Hours > 0), |
|
|
22 |
|
Hours INT, |
23 |
23 |
PRIMARY KEY (Scientist, Project), |
PRIMARY KEY (Scientist, Project), |
24 |
|
FOREIGN KEY (Scientist) REFERENCES SCIENTIST(SSN), |
|
25 |
|
FOREIGN KEY (Project) REFERENCES PROJECT(Code) |
|
26 |
|
ON DELETE CASCADE |
|
27 |
|
ON UPDATE CASCADE |
|
|
24 |
|
FOREIGN KEY (Scientist) REFERENCES SCIENTIST (SSN), |
|
25 |
|
FOREIGN KEY (Project) REFERENCES PROJECT (Code) ON DELETE |
|
26 |
|
CASCADE ON UPDATE CASCADE |
28 |
27 |
); |
); |
29 |
28 |
|
|
30 |
29 |
CREATE TABLE FUNDINGAGENCY ( |
CREATE TABLE FUNDINGAGENCY ( |
31 |
|
Name VARCHAR(150) PRIMARY KEY, |
|
32 |
|
Type ENUM("State", "Federal", "Foundation"), |
|
33 |
|
Creation YEAR |
|
|
30 |
|
Name VARCHAR(150) PRIMARY KEY, |
|
31 |
|
TYPE ENUM ("State", "Federal", "Foundation"), |
|
32 |
|
Creation YEAR |
34 |
33 |
); |
); |
35 |
34 |
|
|
36 |
35 |
CREATE TABLE FUNDS ( |
CREATE TABLE FUNDS ( |
37 |
|
Agency VARCHAR(150), |
|
38 |
|
Project CHAR(4), |
|
39 |
|
Amount DECIMAL(12, 2), |
|
40 |
|
FOREIGN KEY (Agency) REFERENCES FUNDINGAGENCY(Name) |
|
41 |
|
ON UPDATE CASCADE |
|
42 |
|
ON DELETE RESTRICT, |
|
43 |
|
FOREIGN KEY (Project) REFERENCES PROJECT(Code) |
|
|
36 |
|
Agency VARCHAR(150), |
|
37 |
|
Project CHAR(4), |
|
38 |
|
Amount DECIMAL(12, 2), |
|
39 |
|
FOREIGN KEY (Agency) REFERENCES FUNDINGAGENCY (NAME) ON |
|
40 |
|
UPDATE CASCADE ON DELETE RESTRICT, |
|
41 |
|
FOREIGN KEY (Project) REFERENCES PROJECT (Code) |
44 |
42 |
); |
); |
45 |
43 |
|
|
46 |
|
|
|
47 |
|
INSERT INTO SCIENTIST VALUES |
|
48 |
|
("000000000", "Mike"), -- S.1 |
|
49 |
|
("000000001", "Sabine"), -- S.2 |
|
50 |
|
("000000002", "James"), -- S.3 |
|
51 |
|
("000000003", "Emily"), -- S.4 |
|
52 |
|
("000000004", "Claire"); -- S.5 |
|
53 |
|
|
|
54 |
|
INSERT INTO PROJECT VALUES |
|
55 |
|
("AA", "Advancing Airplanes"), -- P.1 |
|
56 |
|
("BA", "Better Airplanes"), -- P.2 |
|
57 |
|
("BB", "Better Buildings"), -- P.3 |
|
58 |
|
("CC", "Creative Creation"); -- P.4 |
|
59 |
|
|
|
60 |
|
INSERT INTO CONTRIBUTESTO VALUES |
|
61 |
|
("000000001", "AA", 12), -- C.1 |
|
62 |
|
("000000001", "BB", 10), -- C.2 |
|
63 |
|
("000000002", "AA", 5), -- C.3 |
|
64 |
|
("000000003", "BA", 3), -- C.4 |
|
65 |
|
("000000000", "BB", 1), -- C.5 |
|
66 |
|
("000000000", "AA", 1); -- C.6 |
|
67 |
|
|
|
68 |
|
INSERT INTO FUNDINGAGENCY VALUES |
|
69 |
|
("National Science Foundation", "Federal", 1950), -- FA.1 |
|
70 |
|
("French-American Cultural Exchange", "Foundation", 2017); -- FA.2 |
|
71 |
|
|
|
72 |
|
INSERT INTO FUNDS VALUES |
|
73 |
|
("National Science Foundation", "AA", 100000), -- F.1 |
|
74 |
|
("French-American Cultural Exchange", "CC", 10000); -- F.2 |
|
75 |
|
|
|
76 |
|
SELECT Name FROM FUNDINGAGENCY WHERE Creation >= 2000; |
|
77 |
|
|
|
78 |
|
SELECT CODE FROM PROJECT WHERE Name LIKE ("%Airplanes%"); |
|
79 |
|
|
|
80 |
|
SELECT SUM(Hours) FROM CONTRIBUTESTO WHERE Project = "AA"; |
|
81 |
|
|
|
82 |
|
SELECT Project FROM CONTRIBUTESTO, SCIENTIST WHERE SCIENTIST.Name = "Sabine" AND SCIENTIST.SSN = CONTRIBUTESTO.Scientist; |
|
83 |
|
|
|
84 |
|
SELECT PROJECT.Name FROM PROJECT, FUNDS, FUNDINGAGENCY WHERE FUNDINGAGENCY.Type = "Federal" AND FUNDINGAGENCY.Name = FUNDS.Agency AND FUNDS.Project = PROJECT.Code; |
|
85 |
|
|
|
86 |
|
SELECT DISTINCT(Fellow.Name) AS "Mike's fellow" FROM SCIENTIST AS Mike, SCIENTIST AS Fellow, CONTRIBUTESTO AS A, CONTRIBUTESTO AS B WHERE Mike.Name = "Mike" AND Mike.SSN = A.Scientist AND A.Project = B.Project AND B.Scientist = Fellow.SSN AND NOT Fellow.Name = "Mike"; |
|
87 |
|
|
|
88 |
|
SELECT DISTINCT(PROJECT.Name) FROM PROJECT, FUNDS WHERE NOT PROJECT.Code IN (SELECT FUNDS.Project FROM FUNDS); |
|
89 |
|
|
|
90 |
|
SELECT SCIENTIST.Name FROM SCIENTIST, CONTRIBUTESTO WHERE CONTRIBUTESTO.Hours >= (SELECT MAX(Hours) FROM CONTRIBUTESTO, PROJECT WHERE PROJECT.Name = "Advancing Airplanes" AND PROJECT.Code = CONTRIBUTESTO.Project) AND CONTRIBUTESTO.Scientist = SCIENTIST.SSN; |
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
|
START TRANSACTION; |
|
96 |
|
UPDATE SCIENTIST SET SSN = "000000001" WHERE Name = "Claire"; |
|
97 |
|
ROLLBACK; |
|
98 |
|
|
|
99 |
|
|
|
100 |
|
START TRANSACTION; |
|
101 |
|
UPDATE FUNDINGAGENCY SET Name = "NSF" WHERE Name = "National Science Foundation"; |
|
102 |
|
SELECT * FROM FUNDINGAGENCY; -- FA. 1 |
|
103 |
|
SELECT * FROM FUNDS; -- F.1 |
|
104 |
|
ROLLBACK; |
|
105 |
|
|
|
106 |
|
|
|
107 |
|
START TRANSACTION; |
|
108 |
|
DELETE FROM FUNDINGAGENCY WHERE Name = "French-American Cultural Exchange"; |
|
109 |
|
ROLLBACK; |
|
110 |
|
|
|
111 |
|
|
|
|
44 |
|
INSERT INTO SCIENTIST |
|
45 |
|
VALUES ( |
|
46 |
|
"000000000", |
|
47 |
|
"Mike"), -- S.1 |
|
48 |
|
( |
|
49 |
|
"000000001", "Sabine"), -- S.2 |
|
50 |
|
( |
|
51 |
|
"000000002", "James"), -- S.3 |
|
52 |
|
( |
|
53 |
|
"000000003", "Emily"), -- S.4 |
|
54 |
|
( |
|
55 |
|
"000000004", "Claire"); |
|
56 |
|
|
|
57 |
|
-- S.5 |
|
58 |
|
INSERT INTO PROJECT |
|
59 |
|
VALUES ( |
|
60 |
|
"AA", |
|
61 |
|
"Advancing Airplanes"), -- P.1 |
|
62 |
|
( |
|
63 |
|
"BA", "Better Airplanes"), -- P.2 |
|
64 |
|
( |
|
65 |
|
"BB", "Better Buildings"), -- P.3 |
|
66 |
|
( |
|
67 |
|
"CC", "Creative Creation"); |
|
68 |
|
|
|
69 |
|
-- P.4 |
|
70 |
|
INSERT INTO CONTRIBUTESTO |
|
71 |
|
VALUES ( |
|
72 |
|
"000000001", |
|
73 |
|
"AA", |
|
74 |
|
12), -- C.1 |
|
75 |
|
( |
|
76 |
|
"000000001", "BB", 10), -- C.2 |
|
77 |
|
( |
|
78 |
|
"000000002", "AA", 5), -- C.3 |
|
79 |
|
( |
|
80 |
|
"000000003", "BA", 3), -- C.4 |
|
81 |
|
( |
|
82 |
|
"000000000", "BB", 1), -- C.5 |
|
83 |
|
( |
|
84 |
|
"000000000", "AA", 1); |
|
85 |
|
|
|
86 |
|
-- C.6 |
|
87 |
|
INSERT INTO FUNDINGAGENCY |
|
88 |
|
VALUES ( |
|
89 |
|
"National Science Foundation", |
|
90 |
|
"Federal", |
|
91 |
|
1950), -- FA.1 |
|
92 |
|
( |
|
93 |
|
"French-American Cultural Exchange", "Foundation", 2017); |
|
94 |
|
|
|
95 |
|
-- FA.2 |
|
96 |
|
INSERT INTO FUNDS |
|
97 |
|
VALUES ( |
|
98 |
|
"National Science Foundation", |
|
99 |
|
"AA", |
|
100 |
|
100000), -- F.1 |
|
101 |
|
( |
|
102 |
|
"French-American Cultural Exchange", "CC", 10000); |
|
103 |
|
|
|
104 |
|
-- F.2 |
|
105 |
|
-- end snippet statement |
File notes/code/sql/HW_ScientificResearchSol.sql added (mode: 100644) (index 0000000..bed42a7) |
|
1 |
|
DROP SCHEMA IF EXISTS HW_SCIENTIFIC_RESEARCH; |
|
2 |
|
|
|
3 |
|
CREATE SCHEMA HW_SCIENTIFIC_RESEARCH; |
|
4 |
|
|
|
5 |
|
USE HW_SCIENTIFIC_RESEARCH; |
|
6 |
|
|
|
7 |
|
CREATE TABLE SCIENTIST ( |
|
8 |
|
SSN INT PRIMARY KEY, |
|
9 |
|
Name VARCHAR(30) NOT NULL |
|
10 |
|
); |
|
11 |
|
|
|
12 |
|
CREATE TABLE PROJECT ( |
|
13 |
|
Code CHAR(4) PRIMARY KEY, |
|
14 |
|
Name VARCHAR(150) NOT NULL |
|
15 |
|
); |
|
16 |
|
|
|
17 |
|
CREATE TABLE CONTRIBUTESTO ( |
|
18 |
|
Scientist INT, |
|
19 |
|
Project CHAR(4), |
|
20 |
|
Hours INT NOT NULL CHECK (Hours > 0), |
|
21 |
|
PRIMARY KEY (Scientist, Project), |
|
22 |
|
FOREIGN KEY (Scientist) REFERENCES SCIENTIST (SSN), |
|
23 |
|
FOREIGN KEY (Project) REFERENCES PROJECT (Code) ON DELETE |
|
24 |
|
CASCADE ON UPDATE CASCADE |
|
25 |
|
); |
|
26 |
|
|
|
27 |
|
CREATE TABLE FUNDINGAGENCY ( |
|
28 |
|
Name VARCHAR(150) PRIMARY KEY, |
|
29 |
|
TYPE ENUM ("State", "Federal", "Foundation"), |
|
30 |
|
Creation YEAR |
|
31 |
|
); |
|
32 |
|
|
|
33 |
|
CREATE TABLE FUNDS ( |
|
34 |
|
Agency VARCHAR(150), |
|
35 |
|
Project CHAR(4), |
|
36 |
|
Amount DECIMAL(12, 2), |
|
37 |
|
FOREIGN KEY (Agency) REFERENCES FUNDINGAGENCY (NAME) ON |
|
38 |
|
UPDATE CASCADE ON DELETE RESTRICT, |
|
39 |
|
FOREIGN KEY (Project) REFERENCES PROJECT (Code) |
|
40 |
|
); |
|
41 |
|
|
|
42 |
|
INSERT INTO SCIENTIST |
|
43 |
|
VALUES ( |
|
44 |
|
"000000000", |
|
45 |
|
"Mike"), -- S.1 |
|
46 |
|
( |
|
47 |
|
"000000001", "Sabine"), -- S.2 |
|
48 |
|
( |
|
49 |
|
"000000002", "James"), -- S.3 |
|
50 |
|
( |
|
51 |
|
"000000003", "Emily"), -- S.4 |
|
52 |
|
( |
|
53 |
|
"000000004", "Claire"); |
|
54 |
|
|
|
55 |
|
-- S.5 |
|
56 |
|
INSERT INTO PROJECT |
|
57 |
|
VALUES ( |
|
58 |
|
"AA", |
|
59 |
|
"Advancing Airplanes"), -- P.1 |
|
60 |
|
( |
|
61 |
|
"BA", "Better Airplanes"), -- P.2 |
|
62 |
|
( |
|
63 |
|
"BB", "Better Buildings"), -- P.3 |
|
64 |
|
( |
|
65 |
|
"CC", "Creative Creation"); |
|
66 |
|
|
|
67 |
|
-- P.4 |
|
68 |
|
INSERT INTO CONTRIBUTESTO |
|
69 |
|
VALUES ( |
|
70 |
|
"000000001", |
|
71 |
|
"AA", |
|
72 |
|
12), -- C.1 |
|
73 |
|
( |
|
74 |
|
"000000001", "BB", 10), -- C.2 |
|
75 |
|
( |
|
76 |
|
"000000002", "AA", 5), -- C.3 |
|
77 |
|
( |
|
78 |
|
"000000003", "BA", 3), -- C.4 |
|
79 |
|
( |
|
80 |
|
"000000000", "BB", 1), -- C.5 |
|
81 |
|
( |
|
82 |
|
"000000000", "AA", 1); |
|
83 |
|
|
|
84 |
|
-- C.6 |
|
85 |
|
INSERT INTO FUNDINGAGENCY |
|
86 |
|
VALUES ( |
|
87 |
|
"National Science Foundation", |
|
88 |
|
"Federal", |
|
89 |
|
1950), -- FA.1 |
|
90 |
|
( |
|
91 |
|
"French-American Cultural Exchange", "Foundation", 2017); |
|
92 |
|
|
|
93 |
|
-- FA.2 |
|
94 |
|
INSERT INTO FUNDS |
|
95 |
|
VALUES ( |
|
96 |
|
"National Science Foundation", |
|
97 |
|
"AA", |
|
98 |
|
100000), -- F.1 |
|
99 |
|
( |
|
100 |
|
"French-American Cultural Exchange", "CC", 10000); |
|
101 |
|
|
|
102 |
|
-- F.2 |
|
103 |
|
-- start snippet solution |
|
104 |
|
/* code/sql/HW_ScientificResearchSol.sql */ |
|
105 |
|
-- List the rows affected (updated or deleted) by the |
|
106 |
|
-- following commands. |
|
107 |
|
-- If no rows are affected because the command would would |
|
108 |
|
-- violate the entity integrity constraint, the referential |
|
109 |
|
-- integrity constraint, or if there would be some other kind |
|
110 |
|
-- of error, please indicate it. |
|
111 |
|
START TRANSACTION; |
|
112 |
|
/* |
|
113 |
|
UPDATE |
|
114 |
|
SCIENTIST |
|
115 |
|
SET SSN = "000000001" |
|
116 |
|
WHERE Name = "Claire"; |
|
117 |
|
*/ |
|
118 |
|
-- ERROR 1062 (23000) at line 106: Duplicate entry '1' for |
|
119 |
|
-- key 'PRIMARY' |
|
120 |
|
ROLLBACK; |
|
121 |
|
|
|
122 |
|
START TRANSACTION; |
|
123 |
|
|
|
124 |
|
UPDATE |
|
125 |
|
FUNDINGAGENCY |
|
126 |
|
SET Name = "NSF" |
|
127 |
|
WHERE Name = "National Science Foundation"; |
|
128 |
|
|
|
129 |
|
SELECT * |
|
130 |
|
FROM FUNDINGAGENCY; |
|
131 |
|
|
|
132 |
|
-- FA. 1 |
|
133 |
|
SELECT * |
|
134 |
|
FROM FUNDS; |
|
135 |
|
|
|
136 |
|
-- F.1 |
|
137 |
|
ROLLBACK; |
|
138 |
|
|
|
139 |
|
START TRANSACTION; |
|
140 |
|
/* |
|
141 |
|
DELETE FROM FUNDINGAGENCY |
|
142 |
|
WHERE Name = "French-American Cultural Exchange"; |
|
143 |
|
*/ |
|
144 |
|
-- ERROR 1451 (23000): Cannot delete or update a parent row: |
|
145 |
|
-- a foreign key constraint fails |
|
146 |
|
-- (`HW_SCIENTIFIC_RESEARCH`.`FUNDS`, CONSTRAINT |
|
147 |
|
-- `FUNDS_ibfk_1` FOREIGN KEY (`Agency`) REFERENCES |
|
148 |
|
-- `FUNDINGAGENCY` (`Name`) ON UPDATE CASCADE) |
|
149 |
|
ROLLBACK; |
|
150 |
|
|
|
151 |
|
-- List the name of the funding agencies created after 2000 |
|
152 |
|
-- ("French-American Cultural Exchange") |
|
153 |
|
SELECT Name |
|
154 |
|
FROM FUNDINGAGENCY |
|
155 |
|
WHERE Creation >= 2000; |
|
156 |
|
|
|
157 |
|
-- List the code of the projects that contains the word |
|
158 |
|
-- "Airplanes" ("AA", "BA") |
|
159 |
|
SELECT CODE |
|
160 |
|
FROM PROJECT |
|
161 |
|
WHERE Name LIKE ("%Airplanes%"); |
|
162 |
|
|
|
163 |
|
-- List the number of hours scientists contributed to the |
|
164 |
|
-- project "AA" (18) |
|
165 |
|
SELECT SUM(Hours) |
|
166 |
|
FROM CONTRIBUTESTO |
|
167 |
|
WHERE Project = "AA"; |
|
168 |
|
|
|
169 |
|
-- List the code of the projects to which the scientist named |
|
170 |
|
-- Sabine contributed ("AA", "BB") |
|
171 |
|
SELECT Project |
|
172 |
|
FROM CONTRIBUTESTO, |
|
173 |
|
SCIENTIST |
|
174 |
|
WHERE SCIENTIST.Name = "Sabine" |
|
175 |
|
AND SCIENTIST.SSN = CONTRIBUTESTO.Scientist; |
|
176 |
|
|
|
177 |
|
-- Give the name of the projects who benefited from federal |
|
178 |
|
-- funds ("Advancing Airplanes") |
|
179 |
|
SELECT PROJECT.Name |
|
180 |
|
FROM PROJECT, |
|
181 |
|
FUNDS, |
|
182 |
|
FUNDINGAGENCY |
|
183 |
|
WHERE FUNDINGAGENCY.Type = "Federal" |
|
184 |
|
AND FUNDINGAGENCY.Name = FUNDS.Agency |
|
185 |
|
AND FUNDS.Project = PROJECT.Code; |
|
186 |
|
|
|
187 |
|
-- Give the name of the scientist who contributed to the same |
|
188 |
|
-- project as Mike ("Sabine", "James") |
|
189 |
|
SELECT DISTINCT (Fellow.Name) AS "Mike's fellow" |
|
190 |
|
FROM SCIENTIST AS Mike, |
|
191 |
|
SCIENTIST AS Fellow, |
|
192 |
|
CONTRIBUTESTO AS A, |
|
193 |
|
CONTRIBUTESTO AS B |
|
194 |
|
WHERE Mike.Name = "Mike" |
|
195 |
|
AND Mike.SSN = A.Scientist |
|
196 |
|
AND A.Project = B.Project |
|
197 |
|
AND B.Scientist = Fellow.SSN |
|
198 |
|
AND NOT Fellow.Name = "Mike"; |
|
199 |
|
|
|
200 |
|
-- List the name of the projects that are not funded by an |
|
201 |
|
-- agency ("Better Airplanes", "Better Buildings") |
|
202 |
|
SELECT DISTINCT (PROJECT.Name) |
|
203 |
|
FROM PROJECT, |
|
204 |
|
FUNDS |
|
205 |
|
WHERE NOT PROJECT.Code IN ( |
|
206 |
|
SELECT FUNDS.Project |
|
207 |
|
FROM FUNDS); |
|
208 |
|
|
|
209 |
|
-- Give the name of the scientist who contributed the most |
|
210 |
|
-- (in terms of hours) to the project named "Advancing |
|
211 |
|
-- Airplanes" (Sabine) |
|
212 |
|
SELECT SCIENTIST.Name |
|
213 |
|
FROM SCIENTIST, |
|
214 |
|
CONTRIBUTESTO |
|
215 |
|
WHERE CONTRIBUTESTO.Hours >= ( |
|
216 |
|
SELECT MAX(Hours) |
|
217 |
|
FROM CONTRIBUTESTO, |
|
218 |
|
PROJECT |
|
219 |
|
WHERE PROJECT.Name = "Advancing Airplanes" |
|
220 |
|
AND PROJECT.Code = CONTRIBUTESTO.Project) |
|
221 |
|
AND CONTRIBUTESTO.Scientist = SCIENTIST.SSN; |
|
222 |
|
|
|
223 |
|
-- end snippet solution |
File notes/code/sql/HW_SocialMedia.sql changed (mode: 100644) (index 610f009..3ded51f) |
... |
... |
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 9541231..866b642) |
... |
... |
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 |
|
-- "2017-17-08"); |
|
51 |
|
-- ERROR 1525 (HY000) at line 40: Incorrect DATE |
|
52 |
|
-- "2017-17-08" |
|
53 |
|
-- 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", |
|
51 |
|
-- and |
|
52 |
|
-- since |
|
53 |
|
-- the date is incorrect, nothing gets inserted. |
|
54 |
|
-- INSERT INTO STORM |
|
55 |
|
-- VALUES ("Dummy2", "Hurricane", 120, DATE |
|
56 |
|
-- "2017-17-08"); |
|
57 |
|
-- ERROR 1525 (HY000) at line 40: Incorrect DATE |
|
58 |
|
-- value: |
|
59 |
|
-- "2017-17-08" |
|
60 |
|
-- The next one sets NULL for DATE. |
54 |
61 |
INSERT INTO STORM |
INSERT INTO STORM |
55 |
62 |
VALUES ( |
VALUES ( |
56 |
63 |
"Irma", |
"Irma", |
|
... |
... |
VALUES ( |
85 |
86 |
NULL); |
NULL); |
86 |
87 |
|
|
87 |
88 |
-- This instruction is not using the primary key, is that a |
-- This instruction is not using the primary key, is that a |
88 |
|
-- problem? |
|
|
89 |
|
-- problem? |
89 |
90 |
UPDATE |
UPDATE |
90 |
91 |
STATE |
STATE |
91 |
92 |
SET Affected_by = "Harvey" |
SET Affected_by = "Harvey" |
File notes/code/sql/HW_TriggerExample.sql changed (mode: 100644) (index 964cec6..f4cd7bb) |
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 |
|
116 |
|
-- average! |
|
|
115 |
|
-- Note also that the student "C" does not have an |
|
116 |
|
-- average! |
File notes/code/sql/HW_Work.sql changed (mode: 100644) (index 0096c9b..e750737) |
... |
... |
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 |
|
-- selects |
|
191 |
|
-- … |
|
192 |
|
-- 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 |
|
192 |
|
-- that |
|
193 |
|
-- selects |
|
194 |
|
-- … |
|
195 |
|
-- We insert some dummy values for this next part. |
193 |
196 |
INSERT INTO WORK |
INSERT INTO WORK |
194 |
197 |
VALUES ( |
VALUES ( |
195 |
198 |
"My Life", |
"My Life", |
|
... |
... |
SELECT Price |
229 |
230 |
FROM EBOOK; |
FROM EBOOK; |
230 |
231 |
|
|
231 |
232 |
-- … the (distinct) names of the authors who have authored |
-- … the (distinct) names of the authors who have authored |
232 |
|
-- a piece of work. |
|
|
233 |
|
-- a piece of work. |
233 |
234 |
SELECT DISTINCT Author |
SELECT DISTINCT Author |
234 |
235 |
FROM WORK; |
FROM WORK; |
235 |
236 |
|
|
|
... |
... |
SELECT MAX(Price) |
255 |
256 |
FROM BOOK; |
FROM BOOK; |
256 |
257 |
|
|
257 |
258 |
-- … 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.”. |
|
|
259 |
|
-- whose name is “Virginia W.”. |
259 |
260 |
SELECT COUNT(*) |
SELECT COUNT(*) |
260 |
261 |
FROM WORK |
FROM WORK |
261 |
262 |
WHERE WORK.Author = "Virginia W."; |
WHERE WORK.Author = "Virginia W."; |
262 |
263 |
|
|
263 |
264 |
-- … 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”. |
|
|
265 |
|
-- called “My Life”. |
265 |
266 |
SELECT Email |
SELECT Email |
266 |
267 |
FROM AUTHOR, |
FROM AUTHOR, |
267 |
268 |
WORK |
WORK |
|
... |
... |
WHERE WORK.Title = "My Life" |
269 |
270 |
AND WORK.Author = AUTHOR.Name; |
AND WORK.Author = AUTHOR.Name; |
270 |
271 |
|
|
271 |
272 |
-- 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". |
|
|
273 |
|
-- author whose email is "vw@isp.net". |
273 |
274 |
SELECT ISBN |
SELECT ISBN |
274 |
275 |
FROM BOOK, |
FROM BOOK, |
275 |
276 |
WORK, |
WORK, |
|
... |
... |
WHERE |
297 |
298 |
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 |
299 |
*/ |
*/ |
299 |
300 |
-- 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 |
|
-- books |
|
302 |
|
-- and ebooks versions of it. |
|
303 |
|
-- The following statement raises an error. |
|
304 |
|
-- DELETE FROM WORK |
|
305 |
|
-- WHERE Title = "My Life"; |
|
|
301 |
|
-- whose title is “My Life”, as well as all of |
|
302 |
|
-- the |
|
303 |
|
-- books |
|
304 |
|
-- and ebooks versions of it. |
|
305 |
|
-- The following statement raises an error. |
|
306 |
|
-- DELETE FROM WORK |
|
307 |
|
-- WHERE Title = "My Life"; |
306 |
308 |
/* |
/* |
307 |
309 |
Fails |
Fails |
308 |
310 |
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 94b7beb..79bc984) |
... |
... |
Problem (A simple database for authors of textbooks) +.#project1 |
4119 |
4119 |
|
|
4120 |
4120 |
--- |
--- |
4121 |
4121 |
|
|
4122 |
|
--- |
|
4123 |
|
|
|
4124 |
4122 |
Problem (A simple database for capstone projects) +.#project1bis |
Problem (A simple database for capstone projects) +.#project1bis |
4125 |
4123 |
~ |
~ |
4126 |
4124 |
|
|
|
... |
... |
Problem (A database for residencies) +.#residency |
4226 |
4224 |
@problem:residency -- Question -.# |
@problem:residency -- Question -.# |
4227 |
4225 |
: Discuss why the primary key identified from the previous question for the `RESIDENCY` table is a good choice. |
: Discuss why the primary key identified from the previous question for the `RESIDENCY` table is a good choice. |
4228 |
4226 |
|
|
|
4227 |
|
|
|
4228 |
|
--- |
|
4229 |
|
|
|
4230 |
|
Problem (A database for research fundings) +.#research-funding |
|
4231 |
|
~ |
|
4232 |
|
|
|
4233 |
|
Consider the following code: |
|
4234 |
|
|
|
4235 |
|
|
|
4236 |
|
```{.sqlmysql .numberLines include=code/sql/HW_ScientificResearch.sql snippet=statement} |
|
4237 |
|
``` |
|
4238 |
|
Note that each row inserted in the tables is given a name and noted as afterwards as a comment (`"S.1, S.2, P.1, C.1, FA.1"`, etc.). |
|
4239 |
|
|
|
4240 |
|
Answer the following questions and problems, assuming that none of the commands in the rest of the problem are actually executed. |
|
4241 |
|
|
|
4242 |
|
@problem:research-funding -- Question -.# |
|
4243 |
|
|
|
4244 |
|
: Draw the relational model corresponding to this series of commands (it is not necessary to include the state). |
|
4245 |
|
|
|
4246 |
|
@problem:research-funding -- Question -.# |
|
4247 |
|
|
|
4248 |
|
: Draw the relational model corresponding to this series of commands (no need to include the state). |
|
4249 |
|
|
|
4250 |
|
@problem:research-funding -- Question -.# |
|
4251 |
|
|
|
4252 |
|
: How could you edit line 12 so that negative values and `NULL`{.sql} would not be admitted as values for `Hours`{.sql}? |
|
4253 |
|
|
|
4254 |
|
@problem:research-funding -- Question -.# |
|
4255 |
|
|
|
4256 |
|
: Write a command that would violate the referential integrity constraint. |
|
4257 |
|
|
|
4258 |
|
@problem:research-funding -- Question -.# |
|
4259 |
|
|
|
4260 |
|
: List the rows affected (updated or deleted) by the following commands. If no rows are affected because the command would would violate the entity integrity constraint, the referential integrity constraint, or if there would be some other kind of error, please indicate it. |
|
4261 |
|
|
|
4262 |
|
#. `UPDATE SCIENTIST SET SSN = "000000001" WHERE Name = "Claire";`{.sql} |
|
4263 |
|
#. `UPDATE FUNDINGAGENCY SET Name = "NSF" WHERE Name = "National Science Foundation";`{.sql} |
|
4264 |
|
#. `DELETE FROM FUNDINGAGENCY WHERE Name = "French-American Cultural Exchange";`{.sql} |
|
4265 |
|
|
|
4266 |
|
@problem:research-funding -- Question -.# |
|
4267 |
|
|
|
4268 |
|
: Write a query that selects …(In parenthesis, the values returned *in this set-up*, but you have to be general.) |
|
4269 |
|
|
|
4270 |
|
#. …the name of the funding agencies created after 2000 (\"French-American Cultural Exchange\") |
|
4271 |
|
|
|
4272 |
|
#. …the code of the projects that contains the word \"Airplanes\" (\"AA\", \"BA\") |
|
4273 |
|
|
|
4274 |
|
#. …the number of hours scientists contributed to the project \"AA\" (18) |
|
4275 |
|
|
|
4276 |
|
#. …the code of the projects to which the scientist named Sabine contributed (\"AA\", \"BB\") |
|
4277 |
|
|
|
4278 |
|
#. …the name of the projects who benefited from federal funds (\"Advancing Airplanes\") |
|
4279 |
|
|
|
4280 |
|
#. …the name of the scientist who contributed to the same project as Mike (\"Sabine\", \"James\") |
|
4281 |
|
|
|
4282 |
|
#. …the name of the projects that are not funded by an agency (\"Better Airplanes\", \"Better Buildings\") |
|
4283 |
|
|
|
4284 |
|
#. …the name of the scientist who contributed the most (in terms of hours) to the project named \"Advancing Airplanes\" (Sabine). |
|
4285 |
|
|
|
4286 |
|
@problem:research-funding -- Question -.# |
|
4287 |
|
|
|
4288 |
|
: Identify and discuss two limitations of this model, and offer a way to remedy at least one of them. |
|
4289 |
|
|
|
4290 |
|
|
4229 |
4291 |
## Solutions to Selected Problems {-} |
## Solutions to Selected Problems {-} |
4230 |
4292 |
|
|
4231 |
4293 |
|
|
|
... |
... |
Solution to [%D %n (%T)](#problem:residency) |
4954 |
5016 |
The second key could make sense, since it would refrain a person from declaring the same address twice as their residency. |
The second key could make sense, since it would refrain a person from declaring the same address twice as their residency. |
4955 |
5017 |
The only case that could be hard to work around is if a person was trying to own multiple units at the same address; however, this is more an issue with the primary key of `HOUSE` than an issue with the primary key we suggested for `RESIDENCY`. |
The only case that could be hard to work around is if a person was trying to own multiple units at the same address; however, this is more an issue with the primary key of `HOUSE` than an issue with the primary key we suggested for `RESIDENCY`. |
4956 |
5018 |
|
|
|
5019 |
|
|
|
5020 |
|
--- |
|
5021 |
|
|
|
5022 |
|
Solution to [%D %n (%T)](#problem:research-funding) |
|
5023 |
|
~ |
|
5024 |
|
(Some of) the answers can be found in the following snippet: |
|
5025 |
|
|
|
5026 |
|
```{.sqlmysql .numberLines include=code/sql/HW_ScientificResearchSol.sql snippet=solution} |
|
5027 |
|
``` |
|
5028 |
|
|
|
5029 |
|
|
4957 |
5030 |
# Designing a Good Database |
# Designing a Good Database |
4958 |
5031 |
|
|
4959 |
5032 |
## Resources {-} |
## Resources {-} |