File notes/code/sql/HW_Faculty.sql changed (mode: 100644) (index 1f0b7fe..1edea44) |
... |
... |
CREATE SCHEMA HW_Faculty; |
10 |
10 |
Or we could have use the syntax: |
Or we could have use the syntax: |
11 |
11 |
|
|
12 |
12 |
CREATE DATABASE HW_FACUTLY; |
CREATE DATABASE HW_FACUTLY; |
13 |
|
|
|
14 |
13 |
*/ |
*/ |
15 |
14 |
|
|
16 |
15 |
-- Now, let us create a table in it: |
-- Now, let us create a table in it: |
17 |
16 |
CREATE TABLE HW_Faculty.PROF( |
CREATE TABLE HW_Faculty.PROF( |
18 |
|
Fname VARCHAR(15), |
|
19 |
|
-- No String! |
|
20 |
|
-- The value "15" vas picked randomly, any value below 255 would |
|
21 |
|
-- more or less do the same. Note that declaring extremely large |
|
22 |
|
-- values without using them can impact the performance of |
|
23 |
|
-- your database, cf. for instance https://dba.stackexchange.com/a/162117/ |
|
24 |
|
Room INT, |
|
25 |
|
-- shorthad for INTEGER, are also available: SMALLINT, FLOAT, REAL, DEC |
|
26 |
|
-- The "REAL" datatype is like the "DOUBLE" datatype of C# (they are actually synonyms in SQL): |
|
27 |
|
-- more precise than the "FLOAT" datatype, but not as exact as the "NUMERIC" datatype. |
|
28 |
|
-- cf. https://dev.mysql.com/doc/refman/8.0/en/numeric-types.html |
|
29 |
|
Title CHAR(3), |
|
30 |
|
-- fixed-length string, padded with blanks if needed |
|
31 |
|
Tenured BIT(1), |
|
32 |
|
Nice BOOLEAN, |
|
33 |
|
-- True / False (= 0) / Unknown |
|
34 |
|
Hiring DATE, |
|
35 |
|
-- The DATE is always supposed to be entered in a YEAR/MONTH/DAY variation. |
|
36 |
|
-- To tune the way it will be displayed, you can use the "DATE_FORMAT" function |
|
37 |
|
-- (cf. https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format), |
|
38 |
|
-- but you can enter those values only using the "standard" literals |
|
39 |
|
-- (cf. https://dev.mysql.com/doc/refman/8.0/en/date-and-time-literals.html ) |
|
40 |
|
Last_seen TIME, |
|
41 |
|
FavoriteFruit ENUM('apple', 'orange', 'pear'), |
|
42 |
|
PRIMARY KEY(Fname, Hiring) |
|
|
17 |
|
Fname VARCHAR(15), |
|
18 |
|
/* |
|
19 |
|
No String! |
|
20 |
|
The value "15" vas picked randomly, any value below 255 would |
|
21 |
|
more or less do the same. Note that declaring extremely large |
|
22 |
|
values without using them can impact the performance of |
|
23 |
|
your database, cf. for instance https://dba.stackexchange.com/a/162117/ |
|
24 |
|
*/ |
|
25 |
|
Room INT, |
|
26 |
|
/* |
|
27 |
|
shorthand for INTEGER, are also available: SMALLINT, FLOAT, REAL, DEC |
|
28 |
|
The "REAL" datatype is like the "DOUBLE" datatype of C# (they are actually synonyms in SQL): |
|
29 |
|
more precise than the "FLOAT" datatype, but not as exact as the "NUMERIC" datatype. |
|
30 |
|
cf. https://dev.mysql.com/doc/refman/8.0/en/numeric-types.html |
|
31 |
|
*/ |
|
32 |
|
Title CHAR(3), |
|
33 |
|
-- fixed-length string, padded with blanks if needed |
|
34 |
|
Tenured BIT(1), |
|
35 |
|
Nice BOOLEAN, |
|
36 |
|
-- True / False (= 0) / Unknown |
|
37 |
|
Hiring DATE, |
|
38 |
|
/* |
|
39 |
|
The DATE is always supposed to be entered in a YEAR/MONTH/DAY variation. |
|
40 |
|
To tune the way it will be displayed, you can use the "DATE_FORMAT" function |
|
41 |
|
(cf. https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format), |
|
42 |
|
but you can enter those values only using the "standard" literals |
|
43 |
|
(cf. https://dev.mysql.com/doc/refman/8.0/en/date-and-time-literals.html ) |
|
44 |
|
*/ |
|
45 |
|
Last_seen TIME, |
|
46 |
|
FavoriteFruit ENUM('apple', 'orange', 'pear'), |
|
47 |
|
PRIMARY KEY(Fname, Hiring) |
43 |
48 |
); |
); |
44 |
49 |
|
|
45 |
|
/* |
|
46 |
|
Or, instead of using the fully qualified name HW_Faculty.PROF, |
|
47 |
|
we could have done: |
|
48 |
50 |
|
|
49 |
|
USE HW_Faculty; |
|
50 |
|
CREATE TABLE PROF(…) |
|
|
51 |
|
/* |
|
52 |
|
Or, instead of using the fully qualified name HW_Faculty.PROF, |
|
53 |
|
we could have done: |
51 |
54 |
|
|
|
55 |
|
USE HW_Faculty; |
|
56 |
|
CREATE TABLE PROF(…) |
52 |
57 |
*/ |
*/ |
53 |
58 |
|
|
54 |
59 |
-- Let us use this schema, from now on. |
-- Let us use this schema, from now on. |
|
... |
... |
USE HW_Faculty; |
56 |
61 |
|
|
57 |
62 |
-- Let us insert some "Dummy" value in our table: |
-- Let us insert some "Dummy" value in our table: |
58 |
63 |
INSERT INTO PROF VALUES |
INSERT INTO PROF VALUES |
59 |
|
( |
|
60 |
|
"Clément", -- Or 'Clément'. |
|
61 |
|
290, |
|
62 |
|
'PhD', |
|
63 |
|
0, |
|
64 |
|
NULL, |
|
65 |
|
'19940101', -- Or '940101', '1994-01-01', '94/01/01' |
|
66 |
|
'090500', -- Or '09:05:00', '9:05:0', '9:5:0', '090500' |
|
67 |
|
-- Note also the existence of DATETIME, with 'YYYY-MM-DD HH:MM:SS' |
|
68 |
|
'Apple' -- This is not case-sensitive, oddly enough. |
|
69 |
|
); |
|
|
64 |
|
( |
|
65 |
|
"Clément", -- Or 'Clément'. |
|
66 |
|
290, |
|
67 |
|
'PhD', |
|
68 |
|
0, |
|
69 |
|
NULL, |
|
70 |
|
'19940101', -- Or '940101', '1994-01-01', '94/01/01' |
|
71 |
|
'090500', -- Or '09:05:00', '9:05:0', '9:5:0', '090500' |
|
72 |
|
-- Note also the existence of DATETIME, with 'YYYY-MM-DD HH:MM:SS' |
|
73 |
|
'Apple' -- This is not case-sensitive, oddly enough. |
|
74 |
|
); |
File notes/code/sql/HW_ProcedureExamples.sql changed (mode: 100644) (index a21c07e..f5bfafa) |
... |
... |
CREATE SCHEMA HW_ProcedureExamples; |
5 |
5 |
USE HW_ProcedureExamples; |
USE HW_ProcedureExamples; |
6 |
6 |
|
|
7 |
7 |
/* |
/* |
8 |
|
A "procedure" is a serie of statements stored in a schema, |
|
9 |
|
that can easily be executed repeatedly. |
|
10 |
|
https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html |
|
11 |
|
https://mariadb.com/kb/en/library/create-procedure/ |
|
12 |
|
*/ |
|
|
8 |
|
A "procedure" is a serie of statements stored in a schema, |
|
9 |
|
that can easily be executed repeatedly. |
|
10 |
|
https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html |
|
11 |
|
https://mariadb.com/kb/en/library/create-procedure/ |
|
12 |
|
*/ |
13 |
13 |
|
|
14 |
14 |
|
|
15 |
15 |
CREATE TABLE STUDENT( |
CREATE TABLE STUDENT( |
|
... |
... |
INSERT INTO STUDENT VALUES (123, "Test A", "CS", "a@a.edu"), |
24 |
24 |
(125, "Test C", "CYBR", "c@a.edu"); |
(125, "Test C", "CYBR", "c@a.edu"); |
25 |
25 |
|
|
26 |
26 |
DELIMITER // -- This tells mysql not to mistake the ; below for the end of the procedure definition. |
DELIMITER // -- This tells mysql not to mistake the ; below for the end of the procedure definition. |
|
27 |
|
/* |
|
28 |
|
We temporarily alter the language, and make the delimiter being //. |
|
29 |
|
$$ is often used too, and the documentation, at https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html, reads: |
|
30 |
|
" You can redefine the delimiter to a string other than //, |
|
31 |
|
" and the delimiter can consist of a single character or multiple characters. |
|
32 |
|
" You should avoid the use of the backslash (\) character because that is the escape character for MySQL. |
|
33 |
|
I am assuming that using the minus sign twice is also a poor choice. |
|
34 |
|
*/ |
27 |
35 |
CREATE PROCEDURE STUDENTLIST() |
CREATE PROCEDURE STUDENTLIST() |
28 |
36 |
BEGIN |
BEGIN |
29 |
37 |
SELECT * FROM STUDENT; -- This ";" is not the end of the procedure definition! |
SELECT * FROM STUDENT; -- This ";" is not the end of the procedure definition! |
|
... |
... |
DELIMITER ; -- Now, we want ";" to be the "natural" delimiter again. |
39 |
41 |
|
|
40 |
42 |
CALL STUDENTLIST(); |
CALL STUDENTLIST(); |
41 |
43 |
|
|
42 |
|
/* |
|
43 |
|
As the "()" suggests, a procedure can take |
|
44 |
|
argument(s). |
|
45 |
|
*/ |
|
46 |
|
|
|
|
44 |
|
-- As the "()" suggests, a procedure can take argument(s). |
47 |
45 |
|
|
48 |
46 |
DELIMITER // |
DELIMITER // |
49 |
47 |
CREATE PROCEDURE STUDENTLOGIN(NameP VARCHAR(30)) |
CREATE PROCEDURE STUDENTLOGIN(NameP VARCHAR(30)) |
|
... |
... |
END; |
54 |
52 |
// |
// |
55 |
53 |
DELIMITER ; |
DELIMITER ; |
56 |
54 |
|
|
57 |
|
SHOW CREATE PROCEDURE STUDENTLOGIN; -- You can ask the system to give you information |
|
|
55 |
|
SHOW CREATE PROCEDURE STUDENTLOGIN; |
|
56 |
|
-- You can ask the system to give you information about the procedure you just created. |
58 |
57 |
|
|
59 |
58 |
CALL STUDENTLOGIN("Test A"); -- We can pass quite naturally an argument to our procedure. |
CALL STUDENTLOGIN("Test A"); -- We can pass quite naturally an argument to our procedure. |
File notes/code/sql/HW_ResidencySol.sql changed (mode: 100644) (index ccbbf26..e31a43f) |
... |
... |
CREATE SCHEMA HW_RESIDENCY_SOL; |
5 |
5 |
USE HW_RESIDENCY_SOL; |
USE HW_RESIDENCY_SOL; |
6 |
6 |
|
|
7 |
7 |
CREATE TABLE PERSON( |
CREATE TABLE PERSON( |
8 |
|
FName VARCHAR(40), |
|
|
8 |
|
FName VARCHAR(40), |
9 |
9 |
LName VARCHAR(40), |
LName VARCHAR(40), |
10 |
10 |
SSN VARCHAR(11) PRIMARY KEY, |
SSN VARCHAR(11) PRIMARY KEY, |
11 |
11 |
Birthdate DATE |
Birthdate DATE |
|
... |
... |
INSERT INTO RESIDENCY VALUES |
43 |
43 |
("000-00-0003", "456 Second St.", TRUE, "own"); -- R.4 |
("000-00-0003", "456 Second St.", TRUE, "own"); -- R.4 |
44 |
44 |
|
|
45 |
45 |
-- start snippet solution4 |
-- start snippet solution4 |
46 |
|
|
|
47 |
46 |
/* |
/* |
48 |
47 |
In the following we use transactions |
In the following we use transactions |
49 |
48 |
to be able to simulate the "what if" |
to be able to simulate the "what if" |
|
... |
... |
INSERT INTO RESIDENCY VALUES |
52 |
51 |
and roll back on them before moving to |
and roll back on them before moving to |
53 |
52 |
the next question. |
the next question. |
54 |
53 |
*/ |
*/ |
55 |
|
|
|
56 |
54 |
-- Exercise 4 |
-- Exercise 4 |
57 |
55 |
-- List the rows (i.e., P.2, H.1, or even “none”) modified by the following statements: |
-- List the rows (i.e., P.2, H.1, or even “none”) modified by the following statements: |
58 |
56 |
|
|
|
... |
... |
ROLLBACK; |
73 |
71 |
|
|
74 |
72 |
START TRANSACTION; |
START TRANSACTION; |
75 |
73 |
DELETE FROM PERSON WHERE Birthdate=DATE"1990-02-11"; |
DELETE FROM PERSON WHERE Birthdate=DATE"1990-02-11"; |
76 |
|
-- None, because of the foreign key and the referential integrity constraint. |
|
|
74 |
|
-- None, because of the foreign key and the referential integrity constraint. |
77 |
75 |
-- ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`HW_RESIDENCY_SOL`.`RESIDENCY`, CONSTRAINT `RESIDENCY_ibfk_1` FOREIGN KEY (`Person`) REFERENCES `PERSON` (`SSN`)) |
-- ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`HW_RESIDENCY_SOL`.`RESIDENCY`, CONSTRAINT `RESIDENCY_ibfk_1` FOREIGN KEY (`Person`) REFERENCES `PERSON` (`SSN`)) |
78 |
76 |
ROLLBACK; |
ROLLBACK; |
79 |
|
|
|
80 |
77 |
-- end snippet solution4 |
-- end snippet solution4 |
81 |
78 |
|
|
|
79 |
|
|
82 |
80 |
-- start snippet solution5 |
-- start snippet solution5 |
83 |
81 |
-- Exercise 5 |
-- Exercise 5 |
84 |
|
|
|
|
82 |
|
/* Write a query that selects … |
|
83 |
|
… the addresses of the houses in the system (11 Third St., 123 Main St., 456 Second St.). |
|
84 |
|
*/ |
85 |
85 |
SELECT Address FROM HOUSE; |
SELECT Address FROM HOUSE; |
86 |
86 |
|
|
87 |
87 |
-- … the SSN of the persons whose first name was not entered in the system (000-00-0000). |
-- … the SSN of the persons whose first name was not entered in the system (000-00-0000). |
|
... |
... |
SELECT DISTINCT COLOR FROM HOUSE; |
96 |
94 |
SELECT House |
SELECT House |
97 |
95 |
FROM RESIDENCY, PERSON |
FROM RESIDENCY, PERSON |
98 |
96 |
WHERE PERSON.Fname = "James" |
WHERE PERSON.Fname = "James" |
99 |
|
AND PERSON.LName = "Baldwin" |
|
100 |
|
AND PERSON.SSN = RESIDENCY.Person; |
|
|
97 |
|
AND PERSON.LName = "Baldwin" |
|
98 |
|
AND PERSON.SSN = RESIDENCY.Person; |
101 |
99 |
|
|
102 |
100 |
-- … the first name of the oldest person in the database (James). |
-- … the first name of the oldest person in the database (James). |
103 |
101 |
SELECT FName |
SELECT FName |
|
... |
... |
WHERE Birthdate = ( |
110 |
108 |
SELECT RESIDENCY.House |
SELECT RESIDENCY.House |
111 |
109 |
FROM RESIDENCY, PERSON |
FROM RESIDENCY, PERSON |
112 |
110 |
WHERE PERSON.FName = "Michael" |
WHERE PERSON.FName = "Michael" |
113 |
|
AND PERSON.LName = "Keal" |
|
114 |
|
AND PERSON.SSN = RESIDENCY.Person |
|
115 |
|
AND RESIDENCY.PrincipalResidence = TRUE; |
|
|
111 |
|
AND PERSON.LName = "Keal" |
|
112 |
|
AND PERSON.SSN = RESIDENCY.Person |
|
113 |
|
AND RESIDENCY.PrincipalResidence = TRUE; |
116 |
114 |
|
|
117 |
115 |
-- … the (distinct) first and last names of the homeowners (Michael Keal, Mridula Warrier). |
-- … the (distinct) first and last names of the homeowners (Michael Keal, Mridula Warrier). |
118 |
116 |
SELECT DISTINCT (PERSON.FName), PERSON.LName |
SELECT DISTINCT (PERSON.FName), PERSON.LName |
119 |
117 |
FROM PERSON, RESIDENCY |
FROM PERSON, RESIDENCY |
120 |
118 |
WHERE RESIDENCY.Status = "own" |
WHERE RESIDENCY.Status = "own" |
121 |
|
AND RESIDENCY.Person = PERSON.SSN; |
|
|
119 |
|
AND RESIDENCY.Person = PERSON.SSN; |
122 |
120 |
-- cf comment at snippet homonyms |
-- cf comment at snippet homonyms |
123 |
121 |
|
|
124 |
122 |
SELECT PERSON.FName, PERSON.LName |
SELECT PERSON.FName, PERSON.LName |
|
... |
... |
WHERE SSN IN (SELECT DISTINCT(RESIDENCY.Person) FROM RESIDENCY WHERE RESIDENCY.S |
130 |
128 |
SELECT RoomMate.Person |
SELECT RoomMate.Person |
131 |
129 |
FROM RESIDENCY AS James, RESIDENCY AS RoomMate, PERSON |
FROM RESIDENCY AS James, RESIDENCY AS RoomMate, PERSON |
132 |
130 |
WHERE PERSON.FName = "James" |
WHERE PERSON.FName = "James" |
133 |
|
AND PERSON.LName = "Baldwin" |
|
134 |
|
AND PERSON.SSN = James.Person |
|
135 |
|
AND James.House = RoomMate.House |
|
136 |
|
AND NOT James.Person = RoomMate.Person |
|
137 |
|
AND RoomMate.PrincipalResidence = TRUE; |
|
138 |
|
|
|
|
131 |
|
AND PERSON.LName = "Baldwin" |
|
132 |
|
AND PERSON.SSN = James.Person |
|
133 |
|
AND James.House = RoomMate.House |
|
134 |
|
AND NOT James.Person = RoomMate.Person |
|
135 |
|
AND RoomMate.PrincipalResidence = TRUE; |
139 |
136 |
-- end snippet solution5 |
-- end snippet solution5 |
140 |
137 |
|
|
141 |
138 |
START TRANSACTION; |
START TRANSACTION; |
|
... |
... |
START TRANSACTION; |
143 |
140 |
-- If we have homonymns in our database, e.g. |
-- If we have homonymns in our database, e.g. |
144 |
141 |
|
|
145 |
142 |
INSERT INTO PERSON VALUES |
INSERT INTO PERSON VALUES |
146 |
|
("A", "B", "000-00-0010", NULL), |
|
|
143 |
|
("A", "B", "000-00-0010", NULL), |
147 |
144 |
("A", "B", "000-00-0011", NULL); |
("A", "B", "000-00-0011", NULL); |
148 |
145 |
|
|
149 |
146 |
INSERT INTO HOUSE VALUES |
INSERT INTO HOUSE VALUES |
|
... |
... |
INSERT INTO RESIDENCY VALUES |
153 |
150 |
("000-00-0010", "H", TRUE, "own"), |
("000-00-0010", "H", TRUE, "own"), |
154 |
151 |
("000-00-0011", "H", TRUE, "own"); |
("000-00-0011", "H", TRUE, "own"); |
155 |
152 |
|
|
|
153 |
|
-- Then the query below fails, in the sense that it reports the name "A, B" only once. |
156 |
154 |
|
|
157 |
155 |
SELECT DISTINCT (PERSON.FName), PERSON.LName |
SELECT DISTINCT (PERSON.FName), PERSON.LName |
158 |
156 |
FROM PERSON, RESIDENCY |
FROM PERSON, RESIDENCY |
159 |
157 |
WHERE RESIDENCY.Status = "own" |
WHERE RESIDENCY.Status = "own" |
160 |
|
AND RESIDENCY.Person = PERSON.SSN; |
|
|
158 |
|
AND RESIDENCY.Person = PERSON.SSN; |
161 |
159 |
|
|
|
160 |
|
-- A better (and not much more complicated) solution would have been |
162 |
161 |
|
|
163 |
162 |
SELECT PERSON.FName, PERSON.LName |
SELECT PERSON.FName, PERSON.LName |
164 |
163 |
FROM PERSON |
FROM PERSON |
File notes/code/sql/HW_TextbookAuthoredSol.sql changed (mode: 100644) (index 2ac0e7c..0061e0c) |
1 |
1 |
/* code/sql/HW_TextbookAuthoredSol.sql */ |
/* code/sql/HW_TextbookAuthoredSol.sql */ |
2 |
2 |
|
|
3 |
|
/* |
|
4 |
|
DO **NOT** EDIT THE CODE BELOW |
|
5 |
|
*/ |
|
|
3 |
|
-- DO **NOT** EDIT THE CODE BELOW |
6 |
4 |
|
|
7 |
5 |
DROP SCHEMA IF EXISTS HW_PROJECT1; |
DROP SCHEMA IF EXISTS HW_PROJECT1; |
8 |
6 |
CREATE SCHEMA HW_PROJECT1; |
CREATE SCHEMA HW_PROJECT1; |
9 |
7 |
USE HW_PROJECT1; |
USE HW_PROJECT1; |
10 |
8 |
|
|
11 |
9 |
CREATE TABLE TEXTBOOK( |
CREATE TABLE TEXTBOOK( |
12 |
|
Title VARCHAR(50), |
|
|
10 |
|
Title VARCHAR(50), |
13 |
11 |
ISBN CHAR(13) PRIMARY KEY, |
ISBN CHAR(13) PRIMARY KEY, |
14 |
12 |
Price DECIMAL(10,2) |
Price DECIMAL(10,2) |
15 |
13 |
); |
); |
|
... |
... |
CREATE TABLE AUTHOR( |
21 |
19 |
PRIMARY KEY(Lname, Fname) |
PRIMARY KEY(Lname, Fname) |
22 |
20 |
); |
); |
23 |
21 |
|
|
|
22 |
|
/* |
|
23 |
|
The meaning of this table is that a tuple <I, L, F> means that the |
|
24 |
|
author whose last name is L and whose first name is F wrote the textbook |
|
25 |
|
whose ISBN is I. |
|
26 |
|
*/ |
24 |
27 |
|
|
25 |
28 |
CREATE TABLE AUTHORED( |
CREATE TABLE AUTHORED( |
26 |
|
Book CHAR(13), |
|
|
29 |
|
Book CHAR(13), |
27 |
30 |
FOREIGN KEY (Book) |
FOREIGN KEY (Book) |
28 |
31 |
REFERENCES TEXTBOOK(ISBN), |
REFERENCES TEXTBOOK(ISBN), |
29 |
32 |
AuthorLName VARCHAR(30), |
AuthorLName VARCHAR(30), |
|
... |
... |
CREATE TABLE AUTHORED( |
38 |
36 |
); |
); |
39 |
37 |
|
|
40 |
38 |
INSERT INTO TEXTBOOK VALUES |
INSERT INTO TEXTBOOK VALUES |
41 |
|
('Starting Out with Java: Early Objects', |
|
42 |
|
9780133776744, |
|
43 |
|
30.00), |
|
44 |
|
('NoSQL for Mere Mortals', |
|
45 |
|
9780134023212, |
|
46 |
|
47.99); |
|
|
39 |
|
('Starting Out with Java: Early Objects', |
|
40 |
|
9780133776744, |
|
41 |
|
30.00), |
|
42 |
|
('NoSQL for Mere Mortals', |
|
43 |
|
9780134023212, |
|
44 |
|
47.99); |
47 |
45 |
|
|
48 |
46 |
INSERT INTO AUTHOR VALUES |
INSERT INTO AUTHOR VALUES |
49 |
|
('Sullivan', 'Dan', NULL), |
|
50 |
|
('Gaddis', 'Tony', NULL); |
|
|
47 |
|
('Sullivan', 'Dan', NULL), |
|
48 |
|
('Gaddis', 'Tony', NULL); |
51 |
49 |
|
|
52 |
50 |
INSERT INTO AUTHORED VALUES |
INSERT INTO AUTHORED VALUES |
53 |
|
(9780134023212, 'Sullivan', 'Dan'), |
|
54 |
|
(9780133776744, 'Gaddis', 'Tony'); |
|
|
51 |
|
(9780134023212, 'Sullivan', 'Dan'), |
|
52 |
|
(9780133776744, 'Gaddis', 'Tony'); |
55 |
53 |
|
|
56 |
54 |
/* |
/* |
57 |
|
START EDITING |
|
|
55 |
|
START EDITING |
|
56 |
|
start snippet solution |
|
57 |
|
code/sql/HW_TEXTBOOK_AUTHORED_SOL.sql |
58 |
58 |
*/ |
*/ |
59 |
59 |
|
|
60 |
|
|
|
61 |
|
/* code/sql/HW_TEXTBOOK_AUTHORED_SOL.sql */ |
|
62 |
|
|
|
|
60 |
|
/* |
|
61 |
|
EXERCISE 1 |
63 |
62 |
|
|
|
63 |
|
Write a command that updates the email address of 'Gaddis', 'Tony' to "tgaddis@pearson.com" |
|
64 |
|
*/ |
64 |
65 |
|
|
65 |
66 |
UPDATE AUTHOR SET Email = "tgaddis@pearson.com" |
UPDATE AUTHOR SET Email = "tgaddis@pearson.com" |
66 |
|
WHERE LName = 'Gaddis' AND FName = 'Tony'; |
|
|
67 |
|
WHERE LName = 'Gaddis' AND FName = 'Tony'; |
67 |
68 |
|
|
|
69 |
|
/* |
|
70 |
|
You can use |
|
71 |
|
SELECT * FROM AUTHOR; |
|
72 |
|
to check that the modification took place. |
|
73 |
|
*/ |
68 |
74 |
|
|
69 |
75 |
|
|
|
76 |
|
/* |
|
77 |
|
EXERCISE 2 |
70 |
78 |
|
|
|
79 |
|
Write a command that inserts the textbook of your choice in the |
|
80 |
|
TEXTBOOK table. No value should be NULL, but you can invent |
|
81 |
|
the values. |
|
82 |
|
*/ |
71 |
83 |
|
|
72 |
84 |
INSERT INTO TEXTBOOK VALUES ('Fundamentals of Database Systems', 9780133970777, 165.89); |
INSERT INTO TEXTBOOK VALUES ('Fundamentals of Database Systems', 9780133970777, 165.89); |
73 |
85 |
|
|
74 |
|
|
|
|
86 |
|
/* |
|
87 |
|
You can use |
|
88 |
|
SELECT * FROM TEXTBOOK; |
|
89 |
|
to check that the insertion was correctly made. |
|
90 |
|
*/ |
75 |
91 |
|
|
|
92 |
|
/* |
|
93 |
|
EXERCISE 3 |
76 |
94 |
|
|
|
95 |
|
Write a command that makes 'Gaddis', 'Tony' the author of the |
|
96 |
|
textbook you just added to our database. |
|
97 |
|
*/ |
77 |
98 |
|
|
78 |
99 |
INSERT INTO AUTHORED VALUES(9780133970777, 'Gaddis', 'Tony'); |
INSERT INTO AUTHORED VALUES(9780133970777, 'Gaddis', 'Tony'); |
79 |
100 |
|
|
|
101 |
|
/* |
|
102 |
|
You can use |
|
103 |
|
SELECT * FROM AUTHORED; |
|
104 |
|
to check that the insertion was correctly made. |
80 |
105 |
|
|
81 |
106 |
|
|
|
107 |
|
EXERCISE 4 |
82 |
108 |
|
|
|
109 |
|
Write a command that makes "0.01" becomes the |
|
110 |
|
default value for the Price attribute of the |
|
111 |
|
TEXTBOOK relation. |
|
112 |
|
*/ |
83 |
113 |
|
|
84 |
114 |
ALTER TABLE TEXTBOOK ALTER COLUMN Price SET DEFAULT 0.01; |
ALTER TABLE TEXTBOOK ALTER COLUMN Price SET DEFAULT 0.01; |
85 |
115 |
|
|
|
116 |
|
/* |
|
117 |
|
You can use |
|
118 |
|
DESCRIBE TEXTBOOK; |
|
119 |
|
to check that the Price attribute now has a default |
|
120 |
|
value. |
86 |
121 |
|
|
87 |
122 |
|
|
|
123 |
|
EXERCISE 5 |
88 |
124 |
|
|
|
125 |
|
Write a command that insert a textbook of |
|
126 |
|
your choice in the TEXTBOOK table, with the |
|
127 |
|
price set to the default value. |
|
128 |
|
*/ |
89 |
129 |
|
|
90 |
130 |
INSERT INTO TEXTBOOK VALUES('Proof Theory', 9780486490731, DEFAULT); |
INSERT INTO TEXTBOOK VALUES('Proof Theory', 9780486490731, DEFAULT); |
91 |
131 |
|
|
|
132 |
|
/* |
|
133 |
|
You can use |
|
134 |
|
SELECT * FROM TEXTBOOK; |
|
135 |
|
to check that the insertion was correctly made. |
92 |
136 |
|
|
93 |
137 |
|
|
|
138 |
|
EXERCISE 6 |
94 |
139 |
|
|
|
140 |
|
Write a command that creates a table called EDITOR |
|
141 |
|
with 3 attributes, "Name", "Address" and "Website". |
|
142 |
|
The "Name" attribute should be the primary key. |
|
143 |
|
Then, insert two tuples in the EDITOR table, one |
|
144 |
|
should have the "Name" attribute set to "Pearson". |
|
145 |
|
*/ |
95 |
146 |
|
|
96 |
147 |
CREATE TABLE EDITOR( |
CREATE TABLE EDITOR( |
97 |
|
Name VARCHAR(30) |
|
|
148 |
|
Name VARCHAR(30) |
98 |
149 |
PRIMARY KEY, |
PRIMARY KEY, |
99 |
150 |
Address VARCHAR(255), |
Address VARCHAR(255), |
100 |
151 |
Website VARCHAR(100) |
Website VARCHAR(100) |
|
... |
... |
INSERT INTO EDITOR VALUES |
145 |
155 |
('Pearson', NULL, 'http://pearsoned.com/'), |
('Pearson', NULL, 'http://pearsoned.com/'), |
146 |
156 |
('Dover', NULL, 'https://store.doverpublications.com/'); |
('Dover', NULL, 'https://store.doverpublications.com/'); |
147 |
157 |
|
|
|
158 |
|
/* |
|
159 |
|
You can use |
|
160 |
|
DESCRIBE EDITOR; |
|
161 |
|
to check that the table was actually created, and |
|
162 |
|
SELECT * FROM EDITOR; |
|
163 |
|
to check that the values were inserted. |
148 |
164 |
|
|
149 |
165 |
|
|
|
166 |
|
EXERCISE 7 |
150 |
167 |
|
|
|
168 |
|
Write a command that creates a table called PUBLISHED |
|
169 |
|
with 2 attributes, "Editor", and "Textbook". |
|
170 |
|
The "Editor" attribute should references the EDITOR |
|
171 |
|
table, and the "Textbook" attribute should reference |
|
172 |
|
the TEXTBOOK table. |
|
173 |
|
*/ |
151 |
174 |
|
|
152 |
175 |
CREATE TABLE PUBLISHED( |
CREATE TABLE PUBLISHED( |
153 |
176 |
Editor VARCHAR(30), |
Editor VARCHAR(30), |
|
... |
... |
CREATE TABLE PUBLISHED( |
169 |
181 |
REFERENCES TEXTBOOK(ISBN) |
REFERENCES TEXTBOOK(ISBN) |
170 |
182 |
); |
); |
171 |
183 |
|
|
172 |
|
|
|
|
184 |
|
/* |
|
185 |
|
You can use |
|
186 |
|
DESCRIBE PUBLISHED; |
|
187 |
|
to check that the table was actually created. |
173 |
188 |
|
|
|
189 |
|
EXERCISE 8 |
174 |
190 |
|
|
|
191 |
|
Write a command that makes "Pearson" the editor of |
|
192 |
|
the textbook whose ISBN is 9780133776744. |
|
193 |
|
*/ |
175 |
194 |
|
|
176 |
195 |
INSERT INTO PUBLISHED VALUES ("Pearson", 9780133776744); |
INSERT INTO PUBLISHED VALUES ("Pearson", 9780133776744); |
177 |
196 |
|
|
|
197 |
|
/* |
|
198 |
|
You can use |
|
199 |
|
SELECT * FROM PUBLISHED; |
|
200 |
|
to check that the table was actually created. |
178 |
201 |
|
|
179 |
202 |
|
|
|
203 |
|
EXERCISE 9 |
180 |
204 |
|
|
|
205 |
|
Answer the following short questions. In our model, as it is, … |
181 |
206 |
|
|
|
207 |
|
Can an author have authored more than one textbook? |
|
208 |
|
Yes. |
182 |
209 |
|
|
|
210 |
|
Can a textbook have more than one author? |
|
211 |
|
Yes. |
183 |
212 |
|
|
|
213 |
|
Can a textbook without ISBN be inserted in the TEXTBOOK relation? |
|
214 |
|
No, unless you create a "dummy" (fake) value for it, |
|
215 |
|
like 0000000000000, but this value can be used only |
|
216 |
|
once, since ISBN is the primary key. |
184 |
217 |
|
|
|
218 |
|
Can the price of a textbook be negative? |
|
219 |
|
Yes. We can actually test it: |
|
220 |
|
INSERT INTO TEXTBOOK VALUES ("Test", 0000000000000, -1); |
185 |
221 |
|
|
|
222 |
|
Can two author have the same first and last name? |
|
223 |
|
No. The query: |
|
224 |
|
INSERT INTO AUTHOR VALUES ('Smith', 'Bob', NULL), ('Smith', 'Bob', NULL); |
|
225 |
|
returns |
|
226 |
|
ERROR 1062 (23000): Duplicate entry 'Smith-Bob' for key 'PRIMARY' |
186 |
227 |
|
|
|
228 |
|
Can two textbooks have the same title? |
|
229 |
|
Yes, as long as they have different ISBN. The command |
|
230 |
|
INSERT INTO TEXTBOOK VALUES ("Test", 0000000000001, NULL), ("Test", 0000000000002, NULL); |
|
231 |
|
is processed just fine. |
187 |
232 |
|
|
|
233 |
|
Can two editiors have the same address? |
|
234 |
|
Yes. The command: |
|
235 |
|
INSERT INTO EDITOR VALUES ("Test 1", "123 Main St.", NULL), ("Test 2", "123 Main St.", NULL); |
|
236 |
|
is processed just fine. |
188 |
237 |
|
|
|
238 |
|
end snippet solution |
|
239 |
|
*/ |
File notes/code/sql/HW_Train.sql changed (mode: 100644) (index 8040e94..ab8d78e) |
1 |
1 |
/* code/sql/HW_Train.sql */ |
/* code/sql/HW_Train.sql */ |
2 |
2 |
|
|
3 |
3 |
-- Question 1: |
-- Question 1: |
4 |
|
|
|
5 |
4 |
CREATE TABLE HW_Train( |
CREATE TABLE HW_Train( |
6 |
5 |
Id VARCHAR(30) PRIMARY KEY, -- This line was changed. |
Id VARCHAR(30) PRIMARY KEY, -- This line was changed. |
7 |
6 |
Model VARCHAR(30), |
Model VARCHAR(30), |
|
... |
... |
CREATE TABLE HW_Train( |
9 |
8 |
); |
); |
10 |
9 |
|
|
11 |
10 |
-- Question 2 : |
-- Question 2 : |
12 |
|
|
|
13 |
11 |
CREATE TABLE CONDUCTOR( |
CREATE TABLE CONDUCTOR( |
14 |
12 |
Id VARCHAR(20), |
Id VARCHAR(20), |
15 |
13 |
Name VARCHAR(20), |
Name VARCHAR(20), |
|
... |
... |
ALTER TABLE CONDUCTOR |
20 |
18 |
ADD PRIMARY KEY (Id); |
ADD PRIMARY KEY (Id); |
21 |
19 |
|
|
22 |
20 |
-- Question 3 |
-- Question 3 |
23 |
|
|
|
24 |
21 |
CREATE TABLE ASSIGNED_TO( |
CREATE TABLE ASSIGNED_TO( |
25 |
22 |
TrainId VARCHAR(20), |
TrainId VARCHAR(20), |
26 |
23 |
ConductorId VARCHAR(20), |
ConductorId VARCHAR(20), |
|
... |
... |
CREATE TABLE ASSIGNED_TO( |
31 |
28 |
); |
); |
32 |
29 |
|
|
33 |
30 |
-- Question 4: |
-- Question 4: |
34 |
|
|
|
35 |
31 |
/* |
/* |
36 |
|
* We insert more than one tuple, to make |
|
37 |
|
* the SELECT statements that follow easier |
|
38 |
|
* to test and debug. |
|
39 |
|
*/ |
|
40 |
|
|
|
|
32 |
|
We insert more than one tuple, to make the SELECT statements that follow easier |
|
33 |
|
to test and debug. |
|
34 |
|
*/ |
41 |
35 |
INSERT INTO TRAIN VALUES ('K-13', 'SurfLiner', 2019), ('K-12', 'Regina', 2015); |
INSERT INTO TRAIN VALUES ('K-13', 'SurfLiner', 2019), ('K-12', 'Regina', 2015); |
42 |
36 |
INSERT INTO CONDUCTOR VALUES ('GP1029', 'Bill', 'Junior'), ('GP1030', 'Sandrine', 'Junior'); |
INSERT INTO CONDUCTOR VALUES ('GP1029', 'Bill', 'Junior'), ('GP1030', 'Sandrine', 'Junior'); |
43 |
37 |
INSERT INTO ASSIGNED_TO VALUES ('K-13', 'GP1029', DATE'2015/12/14'), ('K-12', 'GP1030', '20120909'); |
INSERT INTO ASSIGNED_TO VALUES ('K-13', 'GP1029', DATE'2015/12/14'), ('K-12', 'GP1030', '20120909'); |
44 |
38 |
|
|
45 |
39 |
-- Question 5: |
-- Question 5: |
46 |
|
|
|
47 |
40 |
UPDATE CONDUCTOR SET ExperienceLevel = 'Senior' WHERE Id = 'GP1029'; |
UPDATE CONDUCTOR SET ExperienceLevel = 'Senior' WHERE Id = 'GP1029'; |
48 |
41 |
|
|
49 |
42 |
-- Question 6: |
-- Question 6: |
|
... |
... |
SELECT Name FROM CONDUCTOR WHERE ExperienceLevel = 'Senior'; |
56 |
49 |
-- 3. |
-- 3. |
57 |
50 |
SELECT ConstructionYear FROM TRAIN WHERE Model='SurfLiner' OR Model='Regina'; |
SELECT ConstructionYear FROM TRAIN WHERE Model='SurfLiner' OR Model='Regina'; |
58 |
51 |
|
|
59 |
|
--4. |
|
|
52 |
|
-- 4. |
60 |
53 |
SELECT ConductorId FROM ASSIGNED_TO WHERE TrainId = 'K-13' AND Day='2015/12/14'; |
SELECT ConductorId FROM ASSIGNED_TO WHERE TrainId = 'K-13' AND Day='2015/12/14'; |
61 |
54 |
|
|
62 |
|
--5. |
|
|
55 |
|
-- 5. |
63 |
56 |
SELECT Model FROM TRAIN, ASSIGNED_TO WHERE ConductorID = 'GP1029' AND TrainId = TRAIN.ID; |
SELECT Model FROM TRAIN, ASSIGNED_TO WHERE ConductorID = 'GP1029' AND TrainId = TRAIN.ID; |
File notes/code/sql/HW_TriggerExample.sql changed (mode: 100644) (index 9cea482..72a8ab5) |
... |
... |
CREATE SCHEMA HW_TriggerExample; |
5 |
5 |
USE HW_TriggerExample; |
USE HW_TriggerExample; |
6 |
6 |
|
|
7 |
7 |
CREATE TABLE STUDENT( |
CREATE TABLE STUDENT( |
8 |
|
Login VARCHAR(30) PRIMARY KEY, |
|
|
8 |
|
Login VARCHAR(30) PRIMARY KEY, |
9 |
9 |
Average Float |
Average Float |
10 |
10 |
); |
); |
11 |
11 |
|
|
|
... |
... |
CREATE TABLE STUDENT( |
13 |
13 |
SET @number_of_student = 0; |
SET @number_of_student = 0; |
14 |
14 |
|
|
15 |
15 |
/* |
/* |
16 |
|
SQL supports some primitive form of variables. |
|
17 |
|
cf. |
|
18 |
|
https://dev.mysql.com/doc/refman/8.0/en/user-variables.html |
|
19 |
|
https://mariadb.com/kb/en/library/user-defined-variables/ |
|
20 |
|
There is no "clear" form of type |
|
21 |
|
https://dev.mysql.com/doc/refman/8.0/en/user-variables.html |
|
22 |
|
reads: |
|
23 |
|
" In addition, the default result type of a variable is based on |
|
24 |
|
" its type at the beginning of the statement. This may have unintended |
|
25 |
|
" effects if a variable holds a value of one type at the beginning of a |
|
26 |
|
" statement in which it is also assigned a new value of a different type. |
|
27 |
|
|
|
28 |
|
" To avoid problems with this behavior, either do not assign a value to |
|
29 |
|
" and read the value of the same variable within a single statement, or else |
|
30 |
|
" set the variable to 0, 0.0, or '' to define its type before you use it. |
|
|
16 |
|
SQL supports some primitive form of variables. |
|
17 |
|
cf. |
|
18 |
|
https://dev.mysql.com/doc/refman/8.0/en/user-variables.html |
|
19 |
|
https://mariadb.com/kb/en/library/user-defined-variables/ |
|
20 |
|
There is no "clear" form of type |
|
21 |
|
https://dev.mysql.com/doc/refman/8.0/en/user-variables.html |
|
22 |
|
reads: |
|
23 |
|
" In addition, the default result type of a variable is based on |
|
24 |
|
" its type at the beginning of the statement. This may have unintended |
|
25 |
|
" effects if a variable holds a value of one type at the beginning of a |
|
26 |
|
" statement in which it is also assigned a new value of a different type. |
|
27 |
|
|
|
28 |
|
" To avoid problems with this behavior, either do not assign a value to |
|
29 |
|
" and read the value of the same variable within a single statement, or else |
|
30 |
|
" set the variable to 0, 0.0, or '' to define its type before you use it. |
31 |
31 |
|
|
32 |
|
In other words, mysql just "guess" the type of your value and go with it. |
|
|
32 |
|
In other words, mysql just "guess" the type of your value and go with it. |
33 |
33 |
*/ |
*/ |
34 |
34 |
|
|
35 |
35 |
/* |
/* |
36 |
|
We can create a trigger to count the number |
|
37 |
|
of times something was inserted in our STUDENT |
|
38 |
|
table. |
|
|
36 |
|
We can create a trigger to count the number |
|
37 |
|
of times something was inserted in our STUDENT |
|
38 |
|
table. |
39 |
39 |
*/ |
*/ |
40 |
40 |
|
|
41 |
41 |
CREATE TRIGGER NUMBER_OF_STUDENT |
CREATE TRIGGER NUMBER_OF_STUDENT |
|
... |
... |
SELECT COUNT(*) FROM STUDENT; -- We now have four value inserted in the table. |
49 |
49 |
SELECT @number_of_student AS 'Total number of student'; -- And the counter knows it. |
SELECT @number_of_student AS 'Total number of student'; -- And the counter knows it. |
50 |
50 |
|
|
51 |
51 |
/* |
/* |
52 |
|
We should not forget to update our counter |
|
53 |
|
when a student is removed from our table! |
|
54 |
|
*/ |
|
|
52 |
|
We should not forget to update our counter |
|
53 |
|
when a student is removed from our table! |
|
54 |
|
*/ |
55 |
55 |
|
|
56 |
56 |
CREATE TRIGGER NUMBER_OF_STUDENT |
CREATE TRIGGER NUMBER_OF_STUDENT |
57 |
57 |
AFTER DELETE ON STUDENT |
AFTER DELETE ON STUDENT |
|
... |
... |
SELECT COUNT(*) FROM STUDENT; -- Note that our previous query deleted only one s |
63 |
63 |
SELECT @number_of_student AS 'Total number of student'; -- And the counter knows it. |
SELECT @number_of_student AS 'Total number of student'; -- And the counter knows it. |
64 |
64 |
|
|
65 |
65 |
/* |
/* |
66 |
|
Let us now create a table for each individal grade, |
|
67 |
|
and a trigger to calculate the average for us. |
|
68 |
|
Note that the trigger will need to manipulate two tables |
|
69 |
|
at the same time. |
|
70 |
|
*/ |
|
|
66 |
|
Let us now create a table for each individal grade, |
|
67 |
|
and a trigger to calculate the average for us. |
|
68 |
|
Note that the trigger will need to manipulate two tables |
|
69 |
|
at the same time. |
|
70 |
|
*/ |
71 |
71 |
|
|
72 |
72 |
CREATE TABLE GRADE( |
CREATE TABLE GRADE( |
73 |
|
Student VARCHAR(30), |
|
|
73 |
|
Student VARCHAR(30), |
74 |
74 |
Exam VARCHAR(30), |
Exam VARCHAR(30), |
75 |
75 |
Grade INT, |
Grade INT, |
76 |
76 |
PRIMARY KEY(Student, Exam), |
PRIMARY KEY(Student, Exam), |
|
... |
... |
AFTER INSERT ON GRADE |
82 |
82 |
FOR EACH ROW -- Woh, a whole query inside our trigger! |
FOR EACH ROW -- Woh, a whole query inside our trigger! |
83 |
83 |
UPDATE STUDENT |
UPDATE STUDENT |
84 |
84 |
SET STUDENT.Average = |
SET STUDENT.Average = |
85 |
|
(SELECT AVG(Grade) FROM GRADE WHERE GRADE.Student = STUDENT.Login) |
|
|
85 |
|
(SELECT AVG(Grade) FROM GRADE WHERE GRADE.Student = STUDENT.Login) |
86 |
86 |
WHERE STUDENT.Login = NEW.Student; -- The "NEW" keyword here refers to the "new" entry |
WHERE STUDENT.Login = NEW.Student; -- The "NEW" keyword here refers to the "new" entry |
87 |
87 |
-- that is being inserted by the INSERT statement triggering the trigger. |
-- that is being inserted by the INSERT statement triggering the trigger. |
88 |
88 |
|
|
File notes/code/sql/HW_WorkSol.sql changed (mode: 100644) (index 7f202fd..b7e7d9f) |
1 |
1 |
/* code/sql/HW_WorkSol.sql */ |
/* code/sql/HW_WorkSol.sql */ |
2 |
2 |
|
|
3 |
3 |
/* |
/* |
4 |
|
For this code to work, you need to execute |
|
5 |
|
the code in |
|
6 |
|
code/sql/HW_WORK.sql |
|
7 |
|
first. |
|
|
4 |
|
For this code to work, you need to execute the code in |
|
5 |
|
code/sql/HW_WORK.sql |
|
6 |
|
first. |
8 |
7 |
*/ |
*/ |
9 |
8 |
|
|
10 |
9 |
/* |
/* |
11 |
|
* |
|
12 |
|
* Determine if the following insertion statements would violate the the Entity integrity constraint, the Referential integrity constraint, if there would be some Other kind of error, or if it would result in uccessful insertion. |
|
13 |
|
* |
|
14 |
|
*/ |
|
|
10 |
|
Determine if the following insertion statements would violate the the Entity integrity constraint, |
|
11 |
|
the Referential integrity constraint, if there would be some Other kind of error, or if it would |
|
12 |
|
result in uccessful insertion. |
|
13 |
|
*/ |
15 |
14 |
|
|
16 |
15 |
START TRANSACTION; -- We don't want to perform the actual insertions. |
START TRANSACTION; -- We don't want to perform the actual insertions. |
17 |
16 |
|
|
18 |
17 |
INSERT INTO EBOOK VALUES(0, NULL, 20180101, 0); |
INSERT INTO EBOOK VALUES(0, NULL, 20180101, 0); |
|
18 |
|
/* |
|
19 |
|
Query OK, 1 row affected (0.003 sec) |
|
20 |
|
So, "Successful insertion". |
|
21 |
|
*/ |
19 |
22 |
|
|
20 |
23 |
INSERT INTO AUTHOR VALUES("Mary B.", "mb@fai.fr", NULL); |
INSERT INTO AUTHOR VALUES("Mary B.", "mb@fai.fr", NULL); |
|
24 |
|
/* |
|
25 |
|
ERROR 1136 (21S01): Column count doesn't match value count at row 1 |
|
26 |
|
So, "Other kind of error". |
|
27 |
|
*/ |
21 |
28 |
|
|
22 |
29 |
INSERT INTO WORK VALUES("My Life", "Claude A."); |
INSERT INTO WORK VALUES("My Life", "Claude A."); |
|
30 |
|
/* |
|
31 |
|
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails |
|
32 |
|
(`HW_EXAM_1`.`WORK`, CONSTRAINT `WORK_ibfk_1` FOREIGN KEY (`Author`) REFERENCES `AUTHOR` (`Name`) |
|
33 |
|
ON DELETE CASCADE ON UPDATE CASCADE) |
|
34 |
|
So, "Referential integrity constraint" |
|
35 |
|
*/ |
23 |
36 |
|
|
24 |
37 |
INSERT INTO BOOK VALUES(00000000, NULL, DATE'20001225', 90.9); |
INSERT INTO BOOK VALUES(00000000, NULL, DATE'20001225', 90.9); |
|
38 |
|
/* |
|
39 |
|
Query OK, 1 row affected (0.000 sec) |
|
40 |
|
So, "Successful insertion". |
|
41 |
|
*/ |
25 |
42 |
|
|
26 |
43 |
INSERT INTO AUTHOR VALUES("Virginia W.", "alt@isp.net"); |
INSERT INTO AUTHOR VALUES("Virginia W.", "alt@isp.net"); |
|
44 |
|
/* |
|
45 |
|
ERROR 1062 (23000): Duplicate entry 'Virginia W.' for key 'PRIMARY' |
|
46 |
|
So, "Entity integrity constraint". |
|
47 |
|
*/ |
27 |
48 |
|
|
28 |
49 |
ROLLBACK; -- We go back to the previous state. |
ROLLBACK; -- We go back to the previous state. |
29 |
50 |
|
|
30 |
51 |
|
|
31 |
|
|
|
32 |
|
|
|
33 |
52 |
/* |
/* |
34 |
|
* |
|
35 |
|
* List the rows (i.e., A.2, W.1, etc.) modified by the following statements (be careful about the conditions on foreign keys!): |
|
36 |
|
* |
|
37 |
|
*/ |
|
|
53 |
|
List the rows (i.e., A.2, W.1, etc.) modified by the following statements |
|
54 |
|
(be careful about the conditions on foreign keys!): |
|
55 |
|
*/ |
38 |
56 |
|
|
39 |
57 |
|
|
40 |
58 |
START TRANSACTION; -- We don't want to perform the following operations. |
START TRANSACTION; -- We don't want to perform the following operations. |
41 |
59 |
|
|
42 |
60 |
UPDATE AUTHOR SET Email = 'Deprecated' WHERE Email LIKE '%isp.net'; |
UPDATE AUTHOR SET Email = 'Deprecated' WHERE Email LIKE '%isp.net'; |
|
61 |
|
/* |
|
62 |
|
Query OK, 2 rows affected (0.010 sec) |
|
63 |
|
Rows matched: 2 Changed: 2 Warnings: 0 |
|
64 |
|
This changed A.1 and A.2 |
|
65 |
|
*/ |
43 |
66 |
|
|
44 |
67 |
UPDATE WORK SET Title = "How to eat" WHERE Title = "What to eat"; |
UPDATE WORK SET Title = "How to eat" WHERE Title = "What to eat"; |
|
68 |
|
/* |
|
69 |
|
Rows matched: 1 Changed: 1 Warnings: 0 |
|
70 |
|
SQL returns only the number of row changed in the WORK table, |
|
71 |
|
but other rows have been changed as well. |
|
72 |
|
This changed W.1, B.1, E.1. |
|
73 |
|
*/ |
45 |
74 |
|
|
46 |
75 |
DELETE FROM WORK; |
DELETE FROM WORK; |
|
76 |
|
/* |
|
77 |
|
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails |
|
78 |
|
(`HW_EXAM_1`.`BOOK`, CONSTRAINT `BOOK_ibfk_1` FOREIGN KEY (`Work`) REFERENCES `WORK` (`Title`) ON UPDATE CASCADE) |
|
79 |
|
Does not change any row. |
|
80 |
|
*/ |
47 |
81 |
|
|
48 |
82 |
DELETE FROM AUTHOR WHERE Name = "Virginia W."; |
DELETE FROM AUTHOR WHERE Name = "Virginia W."; |
|
83 |
|
/* |
|
84 |
|
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails |
|
85 |
|
(`HW_EXAM_1`.`BOOK`, CONSTRAINT `BOOK_ibfk_1` FOREIGN KEY (`Work`) REFERENCES `WORK` (`Title`) ON UPDATE CASCADE) |
|
86 |
|
Does not change any row. |
|
87 |
|
*/ |
49 |
88 |
|
|
50 |
89 |
ROLLBACK; -- We go back to the previous state. |
ROLLBACK; -- We go back to the previous state. |
51 |
90 |
|
|
52 |
|
/* |
|
53 |
|
* |
|
54 |
|
* You can now assume that there is more data than what we inserted, if that helps you. Write a command that selects … |
|
55 |
|
* |
|
56 |
|
*/ |
|
|
91 |
|
-- You can now assume that there is more data than what we inserted, if that helps you. Write a command that selects … |
57 |
92 |
|
|
58 |
93 |
-- We insert some dummy values for this next part. |
-- We insert some dummy values for this next part. |
59 |
94 |
INSERT INTO WORK VALUES("My Life", "Paul B."), ("What to eat, 2", "Virginia W."); |
INSERT INTO WORK VALUES("My Life", "Paul B."), ("What to eat, 2", "Virginia W."); |
|
... |
... |
SELECT Name FROM AUTHOR WHERE Email LIKE '%fai.fr'; |
92 |
106 |
|
|
93 |
107 |
-- … the price of the ebooks published after 2018. |
-- … the price of the ebooks published after 2018. |
94 |
108 |
SELECT Price FROM BOOK WHERE Published >= 20180101; |
SELECT Price FROM BOOK WHERE Published >= 20180101; |
|
109 |
|
/* |
|
110 |
|
Note that |
|
111 |
|
SELECT Price FROM BOOK WHERE Published > 2018; |
|
112 |
|
would return all the prices, along with a warning: |
|
113 |
|
Incorrect datetime value: '2018' |
|
114 |
|
*/ |
95 |
115 |
|
|
96 |
116 |
-- … the price of the most expensive book. |
-- … the price of the most expensive book. |
97 |
117 |
SELECT MAX(Price) FROM BOOK; |
SELECT MAX(Price) FROM BOOK; |
|
... |
... |
SELECT Email FROM AUTHOR, WORK WHERE WORK.Title = "My Life" AND WORK.Author = AU |
110 |
126 |
SELECT ISBN FROM BOOK, WORK, AUTHOR WHERE AUTHOR.Email = "vw@isp.net" AND WORK.Author = AUTHOR.Name AND BOOK.Work = WORK.Title; |
SELECT ISBN FROM BOOK, WORK, AUTHOR WHERE AUTHOR.Email = "vw@isp.net" AND WORK.Author = AUTHOR.Name AND BOOK.Work = WORK.Title; |
111 |
127 |
|
|
112 |
128 |
/* |
/* |
113 |
|
* |
|
114 |
|
* Write a command that updates the title of all the pieces of work written by the author whose name is “Virginia W. to”BANNED". Is there any reason for this command to be rejected by the system? If yes, explain which one. |
|
115 |
|
* |
|
116 |
|
*/ |
|
|
129 |
|
Write a command that updates the title of all the pieces of work written by the author whose name is “Virginia W. to”BANNED". |
|
130 |
|
Is there any reason for this command to be rejected by the system? If yes, explain which one. |
|
131 |
|
*/ |
117 |
132 |
|
|
118 |
133 |
UPDATE WORK SET Title = "BANNED" WHERE Author = "Virginia W."; |
UPDATE WORK SET Title = "BANNED" WHERE Author = "Virginia W."; |
119 |
|
|
|
120 |
134 |
/* |
/* |
121 |
|
* |
|
122 |
|
* Write one or multiple commands that would delete the work whose title is “My Life”, as well as all of the books and ebooks versions of it. |
|
123 |
|
* |
|
124 |
|
*/ |
|
|
135 |
|
Does not give an error with the that we currently have. |
|
136 |
|
However, since "Title" is the primary key in the WORK table, if Virginia W. had authored two pieces of work or more, |
|
137 |
|
then this command would give an error. |
|
138 |
|
*/ |
|
139 |
|
|
|
140 |
|
-- Write one or multiple commands that would delete the work whose title is “My Life”, as well as all of the books and ebooks versions of it. |
125 |
141 |
|
|
126 |
142 |
DELETE FROM WORK WHERE Title = "My Life"; |
DELETE FROM WORK WHERE Title = "My Life"; |
|
143 |
|
/* |
|
144 |
|
Fails |
|
145 |
|
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails |
|
146 |
|
(`HW_EXAM_1`.`BOOK`, CONSTRAINT `BOOK_ibfk_1` FOREIGN KEY (`Work`) REFERENCES `WORK` (`Title`) ON UPDATE CASCADE) |
|
147 |
|
*/ |
127 |
148 |
|
|
|
149 |
|
-- We have to first delete the corresponding publications: |
128 |
150 |
DELETE FROM BOOK WHERE Work = "My Life"; |
DELETE FROM BOOK WHERE Work = "My Life"; |
129 |
151 |
DELETE FROM EBOOK WHERE Work = "My Life"; |
DELETE FROM EBOOK WHERE Work = "My Life"; |
130 |
152 |
-- And then we can delete the work: |
-- And then we can delete the work: |
131 |
153 |
DELETE FROM WORK WHERE Title = "My Life"; |
DELETE FROM WORK WHERE Title = "My Life"; |
|
154 |
|
/* |
|
155 |
|
And, no, we cannot delete "simply" from multiple tables in one command. |
|
156 |
|
Some workaround exists, cf. https://stackoverflow.com/q/1233451/ . |
|
157 |
|
*/ |
File notes/code/xml/NSFAward.xml changed (mode: 100644) (index 424830b..5398c77) |
1 |
1 |
<?xml version="1.0" encoding="UTF-8"?> |
<?xml version="1.0" encoding="UTF-8"?> |
2 |
2 |
<!-- code/xml/NSFAward.xml --> |
<!-- code/xml/NSFAward.xml --> |
3 |
|
|
|
4 |
3 |
<rootTag> |
<rootTag> |
5 |
|
<Award> |
|
6 |
|
<AwardTitle>CAREER: Advances in Graph Learning and Inference</AwardTitle> |
|
7 |
|
<AwardEffectiveDate>11/01/2019</AwardEffectiveDate> |
|
8 |
|
<AwardExpirationDate>01/31/2023</AwardExpirationDate> |
|
9 |
|
<AwardAmount>105091</AwardAmount> |
|
10 |
|
<Organization> |
|
11 |
|
<Code>05010000</Code> |
|
12 |
|
<Directorate> |
|
13 |
|
<Abbreviation>CSE</Abbreviation> |
|
14 |
|
<LongName>Direct For Computer & Info Scie & Enginr</LongName> |
|
15 |
|
</Directorate> |
|
16 |
|
<Division> |
|
17 |
|
<Abbreviation>CCF</Abbreviation> |
|
18 |
|
<LongName>Division of Computing and Communication Foundations</LongName> |
|
19 |
|
</Division> |
|
20 |
|
</Organization> |
|
21 |
|
<ProgramOfficer> |
|
22 |
|
<SignBlockName>Phillip Regalia</SignBlockName> |
|
23 |
|
</ProgramOfficer> |
|
24 |
|
<AwardID>2005804</AwardID> |
|
25 |
|
<Investigator> |
|
26 |
|
<FirstName>Patrick</FirstName> |
|
27 |
|
<LastName>Hopkins</LastName> |
|
28 |
|
<EmailAddress>phopkins@virginia.edu</EmailAddress> |
|
29 |
|
<StartDate>11/22/2019</StartDate> |
|
30 |
|
<EndDate /> |
|
31 |
|
<RoleCode>Co-Principal Investigator</RoleCode> |
|
32 |
|
</Investigator> |
|
33 |
|
<Investigator> |
|
34 |
|
<FirstName>Jon</FirstName> |
|
35 |
|
<LastName>Ihlefeld</LastName> |
|
36 |
|
<EmailAddress>jfi4n@virginia.edu</EmailAddress> |
|
37 |
|
<StartDate>11/22/2019</StartDate> |
|
38 |
|
<EndDate /> |
|
39 |
|
<RoleCode>Principal Investigator</RoleCode> |
|
40 |
|
</Investigator> |
|
41 |
|
<Institution> |
|
42 |
|
<Name>University of Virginia Main Campus</Name> |
|
43 |
|
<CityName>CHARLOTTESVILLE</CityName> |
|
44 |
|
<ZipCode>229044195</ZipCode> |
|
45 |
|
<PhoneNumber>4349244270</PhoneNumber> |
|
46 |
|
<StreetAddress>P.O. BOX 400195</StreetAddress> |
|
47 |
|
<CountryName>United States</CountryName> |
|
48 |
|
<StateName>Virginia</StateName> |
|
49 |
|
<StateCode>VA</StateCode> |
|
50 |
|
</Institution> |
|
51 |
|
</Award> |
|
|
4 |
|
<Award> |
|
5 |
|
<AwardTitle>CAREER: Advances in Graph Learning and Inference</AwardTitle> |
|
6 |
|
<AwardEffectiveDate>11/01/2019</AwardEffectiveDate> |
|
7 |
|
<AwardExpirationDate>01/31/2023</AwardExpirationDate> |
|
8 |
|
<AwardAmount>105091</AwardAmount> |
|
9 |
|
<Organization> |
|
10 |
|
<Code>05010000</Code> |
|
11 |
|
<Directorate> |
|
12 |
|
<Abbreviation>CSE</Abbreviation> |
|
13 |
|
<LongName>Direct For Computer & Info Scie & Enginr</LongName> |
|
14 |
|
</Directorate> |
|
15 |
|
<Division> |
|
16 |
|
<Abbreviation>CCF</Abbreviation> |
|
17 |
|
<LongName>Division of Computing and Communication Foundations</LongName> |
|
18 |
|
</Division> |
|
19 |
|
</Organization> |
|
20 |
|
<ProgramOfficer> |
|
21 |
|
<SignBlockName>Phillip Regalia</SignBlockName> |
|
22 |
|
</ProgramOfficer> |
|
23 |
|
<AwardID>2005804</AwardID> |
|
24 |
|
<Investigator> |
|
25 |
|
<FirstName>Patrick</FirstName> |
|
26 |
|
<LastName>Hopkins</LastName> |
|
27 |
|
<EmailAddress>phopkins@virginia.edu</EmailAddress> |
|
28 |
|
<StartDate>11/22/2019</StartDate> |
|
29 |
|
<EndDate /> |
|
30 |
|
<RoleCode>Co-Principal Investigator</RoleCode> |
|
31 |
|
</Investigator> |
|
32 |
|
<Investigator> |
|
33 |
|
<FirstName>Jon</FirstName> |
|
34 |
|
<LastName>Ihlefeld</LastName> |
|
35 |
|
<EmailAddress>jfi4n@virginia.edu</EmailAddress> |
|
36 |
|
<StartDate>11/22/2019</StartDate> |
|
37 |
|
<EndDate /> |
|
38 |
|
<RoleCode>Principal Investigator</RoleCode> |
|
39 |
|
</Investigator> |
|
40 |
|
<Institution> |
|
41 |
|
<Name>University of Virginia Main Campus</Name> |
|
42 |
|
<CityName>CHARLOTTESVILLE</CityName> |
|
43 |
|
<ZipCode>229044195</ZipCode> |
|
44 |
|
<PhoneNumber>4349244270</PhoneNumber> |
|
45 |
|
<StreetAddress>P.O. BOX 400195</StreetAddress> |
|
46 |
|
<CountryName>United States</CountryName> |
|
47 |
|
<StateName>Virginia</StateName> |
|
48 |
|
<StateCode>VA</StateCode> |
|
49 |
|
</Institution> |
|
50 |
|
</Award> |
52 |
51 |
</rootTag> |
</rootTag> |