File notes/code/sql/HW_Captone.sql changed (mode: 100644) (index 87cd119..717e745) |
1 |
1 |
/* code/sql/HW_CaptoneSol.sql */ |
/* code/sql/HW_CaptoneSol.sql */ |
2 |
|
|
|
3 |
|
|
|
4 |
2 |
/* |
/* |
5 |
3 |
* |
* |
6 |
4 |
* Look for "FILL HERE" |
* Look for "FILL HERE" |
7 |
5 |
* |
* |
8 |
6 |
*/ |
*/ |
9 |
|
|
|
10 |
7 |
-- NAME -- FILL HERE |
-- NAME -- FILL HERE |
11 |
8 |
-- DATE -- FILL HERE |
-- DATE -- FILL HERE |
12 |
9 |
-- CSCI 3410 - Project 1 |
-- CSCI 3410 - Project 1 |
13 |
|
|
|
14 |
10 |
/* |
/* |
15 |
11 |
* |
* |
16 |
12 |
* DO **NOT** EDIT THE CODE BELOW |
* DO **NOT** EDIT THE CODE BELOW |
17 |
13 |
* |
* |
18 |
14 |
*/ |
*/ |
19 |
|
|
|
20 |
15 |
DROP SCHEMA IF EXISTS HW_CAPSTONE; |
DROP SCHEMA IF EXISTS HW_CAPSTONE; |
21 |
16 |
|
|
22 |
17 |
CREATE SCHEMA HW_CAPSTONE; |
CREATE SCHEMA HW_CAPSTONE; |
|
... |
... |
CREATE SCHEMA HW_CAPSTONE; |
24 |
19 |
USE HW_CAPSTONE; |
USE HW_CAPSTONE; |
25 |
20 |
|
|
26 |
21 |
CREATE TABLE STUDENT ( |
CREATE TABLE STUDENT ( |
27 |
|
FName VARCHAR(50), |
|
28 |
|
Id CHAR(13) PRIMARY KEY, |
|
29 |
|
GraduationYear INT, |
|
30 |
|
GraduationSemester ENUM ("Fall", "Spring", "Summer") |
|
|
22 |
|
FName VARCHAR(50), Id CHAR(13) PRIMARY KEY, |
|
23 |
|
GraduationYear INT, GraduationSemester ENUM ("Fall", |
|
24 |
|
"Spring", "Summer") |
31 |
25 |
); |
); |
32 |
26 |
|
|
33 |
|
CREATE TABLE PROGRAMMING_LANGUAGE( |
|
34 |
|
Name VARCHAR(50) PRIMARY KEY, |
|
35 |
|
Licence VARCHAR(50) |
|
36 |
|
); |
|
|
27 |
|
CREATE TABLE PROGRAMMING_LANGUAGE ( |
|
28 |
|
NAME VARCHAR(50) PRIMARY KEY, Licence VARCHAR(50) |
|
29 |
|
); |
37 |
30 |
|
|
38 |
31 |
CREATE TABLE PROJECT ( |
CREATE TABLE PROJECT ( |
39 |
|
CodeName VARCHAR(50), |
|
40 |
|
Leader CHAR(13), |
|
41 |
|
PRIMARY KEY (CodeName, Leader), |
|
42 |
|
FOREIGN KEY (Leader) REFERENCES STUDENT(Id) |
|
|
32 |
|
CodeName VARCHAR(50), Leader CHAR(13), PRIMARY KEY |
|
33 |
|
(CodeName, Leader), |
|
34 |
|
FOREIGN KEY (Leader) REFERENCES STUDENT (Id) |
43 |
35 |
); |
); |
44 |
36 |
|
|
45 |
|
CREATE TABLE USED_LANGUAGE( |
|
46 |
|
ProjectCodeName VARCHAR(50), |
|
47 |
|
ProjectLeader CHAR(13), |
|
48 |
|
UsedLanguage VARCHAR(50), |
|
49 |
|
PRIMARY KEY (ProjectCodeName, ProjectLeader, UsedLanguage), |
|
50 |
|
FOREIGN KEY (ProjectCodeName) REFERENCES PROJECT(CodeName), |
|
51 |
|
FOREIGN KEY (ProjectLeader) REFERENCES PROJECT(Leader), |
|
52 |
|
FOREIGN KEY (UsedLanguage) REFERENCES PROGRAMMING_LANGUAGE(Name) |
|
|
37 |
|
CREATE TABLE USED_LANGUAGE ( |
|
38 |
|
ProjectCodeName VARCHAR(50), ProjectLeader CHAR(13), |
|
39 |
|
UsedLanguage VARCHAR(50), PRIMARY KEY (ProjectCodeName, |
|
40 |
|
ProjectLeader, UsedLanguage), FOREIGN KEY |
|
41 |
|
(ProjectCodeName) REFERENCES PROJECT (CodeName), |
|
42 |
|
FOREIGN KEY (ProjectLeader) REFERENCES PROJECT |
|
43 |
|
(Leader), |
|
44 |
|
FOREIGN KEY (UsedLanguage) REFERENCES PROGRAMMING_LANGUAGE (NAME) |
53 |
45 |
); |
); |
54 |
46 |
|
|
|
47 |
|
|
55 |
48 |
/* |
/* |
56 |
49 |
The meaning of the USED_LANGUAGE table is that a tuple < N, L, U> represents the fact that the project whose code name is N and whose leader is L uses the programming language U. |
The meaning of the USED_LANGUAGE table is that a tuple < N, L, U> represents the fact that the project whose code name is N and whose leader is L uses the programming language U. |
57 |
50 |
*/ |
*/ |
58 |
|
|
|
59 |
|
|
|
60 |
51 |
INSERT INTO STUDENT |
INSERT INTO STUDENT |
61 |
|
VALUES |
|
62 |
|
("Mary", "0123456789100", 2025, "Summer"), |
|
63 |
|
("Steve", "0000000000000", 2025, "Fall" ), |
|
64 |
|
("Claude", "9999999999999", 2024, "Fall" ), |
|
65 |
|
("Meghan", "0987654321098", 2023, "Spring"); |
|
|
52 |
|
VALUES ("Mary", "0123456789100", 2025, "Summer"), |
|
53 |
|
("Steve", "0000000000000", 2025, "Fall"), ("Claude", |
|
54 |
|
"9999999999999", 2024, "Fall"), ("Meghan", |
|
55 |
|
"0987654321098", 2023, "Spring"); |
66 |
56 |
|
|
67 |
57 |
INSERT INTO PROGRAMMING_LANGUAGE |
INSERT INTO PROGRAMMING_LANGUAGE |
68 |
|
VALUES |
|
69 |
|
("Rust", "MIT" ), |
|
70 |
|
(".NET Core", "MIT" ), |
|
71 |
|
("Racket", "LGPL"), |
|
72 |
|
("Python", "PSF" ); |
|
73 |
|
-- Taken from https://en.wikipedia.org/wiki/Comparison_of_open-source_programming_language_licensing |
|
74 |
|
|
|
75 |
|
INSERT INTO PROJECT |
|
76 |
|
VALUES |
|
77 |
|
("Brick Break", "0123456789100"), |
|
78 |
|
("Brick Break", "0000000000000"), |
|
79 |
|
("Grade Calculator", "0123456789100"), |
|
80 |
|
("Undecided", "9999999999999") |
|
81 |
|
; |
|
82 |
|
|
|
83 |
|
INSERT INTO USED_LANGUAGE |
|
84 |
|
VALUES |
|
85 |
|
("Brick Break", "0123456789100", "Rust" ), |
|
86 |
|
("Brick Break", "0000000000000", ".NET Core"), |
|
87 |
|
("Brick Break", "0000000000000", "Python" ), |
|
88 |
|
("Grade Calculator", "0123456789100", "Racket" ) |
|
89 |
|
; |
|
|
58 |
|
VALUES ("Rust", "MIT"), (".NET Core", "MIT"), ("Racket", |
|
59 |
|
"LGPL"), ("Python", "PSF"); |
|
60 |
|
|
|
61 |
|
-- Taken from |
|
62 |
|
-- https://en.wikipedia.org/wiki/Comparison_of_open-source_pr |
|
63 |
|
-- ogramming_language_licensing |
|
64 |
|
INSERT INTO PROJECT |
|
65 |
|
VALUES ("Brick Break", "0123456789100"), ("Brick Break", |
|
66 |
|
"0000000000000"), ("Grade Calculator", |
|
67 |
|
"0123456789100"), ("Undecided", "9999999999999"); |
|
68 |
|
|
|
69 |
|
INSERT INTO USED_LANGUAGE |
|
70 |
|
VALUES ("Brick Break", "0123456789100", "Rust"), ("Brick |
|
71 |
|
Break", "0000000000000", ".NET Core"), ("Brick Break", |
|
72 |
|
"0000000000000", "Python"), ("Grade Calculator", |
|
73 |
|
"0123456789100", "Racket"); |
|
74 |
|
|
90 |
75 |
|
|
91 |
76 |
/* |
/* |
92 |
77 |
* You can start editing starting here. |
* You can start editing starting here. |
93 |
78 |
*/ |
*/ |
94 |
|
|
|
95 |
79 |
/* |
/* |
96 |
80 |
|
|
97 |
81 |
I. Short Questions (6 pts) |
I. Short Questions (6 pts) |
98 |
82 |
|
|
99 |
83 |
Answer the following short questions based on the model implemented above. |
Answer the following short questions based on the model implemented above. |
100 |
84 |
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). |
101 |
|
|
|
102 |
|
*/ |
|
103 |
|
|
|
|
85 |
|
*/ |
104 |
86 |
-- 1. Can a project uses multiple programming languages? |
-- 1. Can a project uses multiple programming languages? |
105 |
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
|
|
|
|
87 |
|
-- FILL HERE |
|
88 |
|
-- 2. Can a student be the leader of multiple projects? |
|
89 |
|
-- FILL HERE |
|
90 |
|
-- 3. Can multiple projects have the same code name? |
|
91 |
|
-- FILL HERE |
|
92 |
|
-- 4. Could Claude simply enter NULL for the value of his |
|
93 |
|
-- project's code name, since he's undecided? |
|
94 |
|
-- FILL HERE |
|
95 |
|
-- 5. Can a project be created without project leader? |
|
96 |
|
-- FILL HERE |
|
97 |
|
-- 6. Can we know who is working on a project without being |
|
98 |
|
-- its leader? |
|
99 |
|
-- FILL HERE |
118 |
100 |
/* |
/* |
119 |
101 |
|
|
120 |
102 |
II. Relational Model (6 pts.) |
II. Relational Model (6 pts.) |
121 |
103 |
|
|
122 |
104 |
Draw the relational model corresponding to this code. |
Draw the relational model corresponding to this code. |
123 |
105 |
You can hand-draw it and join a scan or a picture, or simply hand me back the sheet where you drew it. |
You can hand-draw it and join a scan or a picture, or simply hand me back the sheet where you drew it. |
124 |
|
|
|
125 |
|
*/ |
|
126 |
|
|
|
127 |
|
|
|
|
106 |
|
*/ |
128 |
107 |
/* |
/* |
129 |
108 |
|
|
130 |
109 |
III. Simple Commands (8 pts.) |
III. Simple Commands (8 pts.) |
|
... |
... |
III. Simple Commands (8 pts.) |
143 |
111 |
Below, you are asked to write commands that perform various actions. |
Below, you are asked to write commands that perform various actions. |
144 |
112 |
Please, leave them uncommented, unless you can't write them correctly, in which case it's ok to leave them commented. |
Please, leave them uncommented, unless you can't write them correctly, in which case it's ok to leave them commented. |
145 |
113 |
The first question is answered as an example. |
The first question is answered as an example. |
146 |
|
|
|
147 |
|
*/ |
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
|
|
114 |
|
*/ |
|
115 |
|
-- 0. Write a command that list all the names of the |
|
116 |
|
-- programming languages. |
|
117 |
|
-- FILL HERE |
|
118 |
|
-- 1. Write a command that insert a new student in the |
|
119 |
|
-- STUDENT table. |
|
120 |
|
-- (You should invent the values). |
|
121 |
|
-- FILL HERE |
|
122 |
|
-- 2. Write a command that updates the code name of the |
|
123 |
|
-- project ("Undecided", "9999999999999") to "VR in ER". |
|
124 |
|
-- FILL HERE |
|
125 |
|
-- 3. Write a command that updates the graduation year of |
|
126 |
|
-- the student whose id is "0987654321098" to 2024, and the |
|
127 |
|
-- semester to "Fall". |
|
128 |
|
-- FILL HERE |
|
129 |
|
-- 4. Write a command that changes the STUDENT table to make |
|
130 |
|
-- it impossible to enter NULL for the first name of a |
|
131 |
|
-- student, without changing the primary key. |
|
132 |
|
-- FILL HERE |
|
133 |
|
-- 5. Write a command that changes the datatype of |
|
134 |
|
-- GraduationYear to SMALLINT. |
|
135 |
|
-- FILL HERE |
|
136 |
|
-- 6. Write a command that adds an attribute "ReleaseDate" |
|
137 |
|
-- to the PROJECT table. |
|
138 |
|
-- FILL HERE |
|
139 |
|
-- 6.bis If you managed to write the previous command |
|
140 |
|
-- correctly, write a command that sets the release date of |
|
141 |
|
-- the project ("Brick Break", "0123456789100") to the 26th |
|
142 |
|
-- of November 2022. |
|
143 |
|
-- FILL HERE |
|
144 |
|
-- 7. Write a command that makes it impossible for a student |
|
145 |
|
-- to be the leader in more than one project |
|
146 |
|
-- (This command should return an error) |
|
147 |
|
-- FILL HERE |
File notes/code/sql/HW_Certificate.sql changed (mode: 100644) (index a11be3d..166369b) |
... |
... |
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 |
88 |
88 |
AND CA.CN = "Let's encrypt"; |
AND CA.CN = "Let's encrypt"; |
89 |
89 |
|
|
90 |
90 |
-- (2) |
-- (2) |
|
91 |
|
-- A table listing the CN of the organizations along |
|
92 |
|
-- with |
|
93 |
|
-- the CN of their certificates. |
91 |
94 |
SELECT ORGANIZATION.CN AS Organization, CERTIFICATE.CN AS |
SELECT ORGANIZATION.CN AS Organization, CERTIFICATE.CN AS |
92 |
95 |
Certificate |
Certificate |
93 |
96 |
FROM ORGANIZATION, CERTIFICATE |
FROM ORGANIZATION, CERTIFICATE |
94 |
97 |
WHERE CERTIFICATE.Org = ORGANIZATION.SN; |
WHERE CERTIFICATE.Org = ORGANIZATION.SN; |
95 |
98 |
|
|
96 |
99 |
-- ( Wikimedia Foundation, *.wikimedia.org | Free Software |
-- ( Wikimedia Foundation, *.wikimedia.org | Free Software |
|
100 |
|
-- Foundation, *.fsf.org | Free Software Foundation , |
|
101 |
|
-- *.shadytest.org | Wikimedia Foundation , |
|
102 |
|
-- *.wikipedia.org |
|
103 |
|
-- ) |
97 |
104 |
/* |
/* |
98 |
105 |
DELETE FROM CA WHERE SN = 'A'; |
DELETE FROM CA WHERE SN = 'A'; |
99 |
106 |
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 29ba036..32b09f4) |
... |
... |
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 965e270..acdb22b) |
... |
... |
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 e33b79e..4e65b8b) |
... |
... |
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 |
|
41 |
|
-- since |
|
42 |
|
-- the date is incorrect, nothing gets inserted. |
|
43 |
|
-- INSERT INTO STORM |
|
44 |
|
-- VALUES ("Dummy2", "Hurricane", 120, DATE |
|
45 |
|
-- "2017-17-08"); |
|
46 |
|
-- ERROR 1525 (HY000) at line 40: Incorrect DATE value: |
|
47 |
|
-- "2017-17-08" |
|
48 |
|
-- The next one sets NULL for DATE. |
32 |
49 |
INSERT INTO STORM |
INSERT INTO STORM |
33 |
50 |
VALUES ("Irma", "Tropical Storm", 102, DEFAULT); |
VALUES ("Irma", "Tropical Storm", 102, DEFAULT); |
34 |
51 |
|
|
|
... |
... |
INSERT INTO STATE |
60 |
61 |
VALUES ("Florida", "FL", NULL); |
VALUES ("Florida", "FL", NULL); |
61 |
62 |
|
|
62 |
63 |
-- This instruction is not using the primary key, is that a |
-- This instruction is not using the primary key, is that a |
|
64 |
|
-- problem? |
63 |
65 |
UPDATE |
UPDATE |
64 |
66 |
STATE |
STATE |
65 |
67 |
SET Affected_by = "Harvey" |
SET Affected_by = "Harvey" |
File notes/code/sql/HW_Work.sql changed (mode: 100644) (index 543f0bf..a596b78) |
... |
... |
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 |
181 |
181 |
FROM EBOOK; |
FROM EBOOK; |
182 |
182 |
|
|
183 |
183 |
-- … the (distinct) names of the authors who have authored |
-- … the (distinct) names of the authors who have authored |
|
184 |
|
-- a piece of work. |
184 |
185 |
SELECT DISTINCT Author |
SELECT DISTINCT Author |
185 |
186 |
FROM WORK; |
FROM WORK; |
186 |
187 |
|
|
|
... |
... |
SELECT MAX(Price) |
207 |
207 |
FROM BOOK; |
FROM BOOK; |
208 |
208 |
|
|
209 |
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.”. |
210 |
211 |
SELECT COUNT(*) |
SELECT COUNT(*) |
211 |
212 |
FROM WORK |
FROM WORK |
212 |
213 |
WHERE WORK.Author = "Virginia W."; |
WHERE WORK.Author = "Virginia W."; |
213 |
214 |
|
|
214 |
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”. |
215 |
217 |
SELECT Email |
SELECT Email |
216 |
218 |
FROM AUTHOR, WORK |
FROM AUTHOR, WORK |
217 |
219 |
WHERE WORK.Title = "My Life" |
WHERE WORK.Title = "My Life" |
218 |
220 |
AND WORK.Author = AUTHOR.Name; |
AND WORK.Author = AUTHOR.Name; |
219 |
221 |
|
|
220 |
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". |
221 |
224 |
SELECT ISBN |
SELECT ISBN |
222 |
225 |
FROM BOOK, WORK, AUTHOR |
FROM BOOK, WORK, AUTHOR |
223 |
226 |
WHERE AUTHOR.Email = "vw@isp.net" |
WHERE AUTHOR.Email = "vw@isp.net" |
|
... |
... |
WHERE |
246 |
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. |
247 |
247 |
*/ |
*/ |
248 |
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"; |
249 |
255 |
/* |
/* |
250 |
256 |
Fails |
Fails |
251 |
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 |