File notes/code/sql/HW_Lecture.sql deleted (index 79368f9..0000000) |
1 |
|
/* code/sql/HW_Lecture.sql */ |
|
2 |
|
|
|
3 |
|
CREATE TABLE HW_Lecture( |
|
4 |
|
Name VARCHAR(25), |
|
5 |
|
Instructor VARCHAR(25), |
|
6 |
|
Year YEAR(4), |
|
7 |
|
Code CHAR(5), |
|
8 |
|
PRIMARY KEY(Year, Code), |
|
9 |
|
FOREIGN KEY (Instructor) REFERENCES PROF(Login) |
|
10 |
|
); |
|
11 |
|
|
|
12 |
|
INSERT INTO HW_Lecture VALUES |
|
13 |
|
('Intro to CS', 'caubert', 2017, '1304'), |
|
14 |
|
('Intro to Algebra', 'perdos', 2017, '1405'), |
|
15 |
|
('Intro to Cyber', 'aturing', 2017, '1234'); |
|
16 |
|
|
|
17 |
|
/* |
|
18 |
|
This representation can not handle the following situations: |
|
19 |
|
- If multiple instructors teach the same class, |
|
20 |
|
- If the HW_Lecture is taught more than once a year (either because it is taught in the Fall, Spring and Summer, or if multiple sections are offered at the same time), |
|
21 |
|
- If a HW_Lecture is cross-listed, then some duplication of information will be needed. |
|
22 |
|
*/ |
|
23 |
|
|
|
24 |
|
ALTER TABLE GRADE |
|
25 |
|
ADD COLUMN LectureCode CHAR(5), |
|
26 |
|
ADD COLUMN LectureYear YEAR(4); |
|
27 |
|
|
|
28 |
|
DESCRIBE GRADE; |
|
29 |
|
|
|
30 |
|
SELECT * FROM GRADE; |
|
31 |
|
|
|
32 |
|
ALTER TABLE GRADE |
|
33 |
|
ADD FOREIGN KEY (LectureYear, LectureCode) |
|
34 |
|
REFERENCES HW_Lecture(Year, Code); |
|
35 |
|
|
|
36 |
|
|
|
37 |
|
UPDATE GRADE SET LectureCode = '1304', LectureYear = 2017 |
|
38 |
|
WHERE Login = 'jrakesh' |
|
39 |
|
AND Grade = '2.85'; |
|
40 |
|
|
|
41 |
|
UPDATE GRADE SET LectureCode = '1405', LectureYear = 2017 |
|
42 |
|
WHERE Login = 'svlatka' |
|
43 |
|
OR (Login = 'jrakesh' AND Grade = '3.85'); |
|
44 |
|
|
|
45 |
|
UPDATE GRADE SET LectureCode = '1234', LectureYear = 2017 |
|
46 |
|
WHERE Login = 'aalyx' |
|
47 |
|
OR Login = 'cjoella'; |
|
48 |
|
|
|
49 |
|
SELECT Login, Grade |
|
50 |
|
FROM GRADE |
|
51 |
|
WHERE Lecturecode='1304' |
|
52 |
|
AND LectureYear = '2017'; |
|
53 |
|
|
|
54 |
|
SELECT DISTINCT Instructor |
|
55 |
|
FROM HW_Lecture |
|
56 |
|
WHERE Year = 2017; |
|
57 |
|
|
|
58 |
|
SELECT Name, Grade |
|
59 |
|
FROM STUDENT, GRADE |
|
60 |
|
WHERE GRADE.LectureCode = 1405 |
|
61 |
|
AND STUDENT.Login = GRADE.Login; |
|
62 |
|
|
|
63 |
|
SELECT Year |
|
64 |
|
FROM HW_Lecture |
|
65 |
|
WHERE Code = '1234'; |
|
66 |
|
|
|
67 |
|
SELECT Name |
|
68 |
|
FROM HW_Lecture |
|
69 |
|
WHERE Year IN |
|
70 |
|
(SELECT Year |
|
71 |
|
FROM HW_Lecture |
|
72 |
|
WHERE CODE = '1234'); |
|
73 |
|
|
|
74 |
|
SELECT B.name |
|
75 |
|
FROM STUDENT AS A, STUDENT AS B |
|
76 |
|
WHERE A.Name = 'Ava Alyx' |
|
77 |
|
AND A.Registered > B.Registered; |
|
78 |
|
|
|
79 |
|
SELECT COUNT(DISTINCT PROF.Name) AS 'Head Teaching This Year' |
|
80 |
|
FROM HW_Lecture, DEPARTMENT, PROF |
|
81 |
|
WHERE Year = 2017 |
|
82 |
|
AND Instructor = Head |
|
83 |
|
AND Head = PROF.Login; |
|
File notes/code/sql/HW_ProfExampleRevisitedRevisited.sql added (mode: 100644) (index 0000000..884f9cd) |
|
1 |
|
-- start snippet recreate |
|
2 |
|
/* code/sql/HW_ProfExampleRevisitedRevisited.sql */ |
|
3 |
|
|
|
4 |
|
DROP SCHEMA IF EXISTS HW_ProfExampleRevisited; |
|
5 |
|
CREATE SCHEMA HW_ProfExampleRevisited; |
|
6 |
|
USE HW_ProfExampleRevisited; |
|
7 |
|
|
|
8 |
|
CREATE TABLE PROF( |
|
9 |
|
Login VARCHAR(25) PRIMARY KEY, |
|
10 |
|
Name VARCHAR(25), |
|
11 |
|
Department CHAR(5) |
|
12 |
|
); |
|
13 |
|
|
|
14 |
|
CREATE TABLE DEPARTMENT( |
|
15 |
|
Code CHAR(5) PRIMARY KEY, |
|
16 |
|
Name VARCHAR(25), |
|
17 |
|
Head VARCHAR(25), |
|
18 |
|
FOREIGN KEY (Head) REFERENCES PROF(Login) |
|
19 |
|
ON UPDATE CASCADE |
|
20 |
|
); |
|
21 |
|
|
|
22 |
|
ALTER TABLE PROF ADD FOREIGN KEY (Department) |
|
23 |
|
REFERENCES DEPARTMENT(Code); |
|
24 |
|
|
|
25 |
|
CREATE TABLE STUDENT( |
|
26 |
|
Login VARCHAR(25) PRIMARY KEY, |
|
27 |
|
Name VARCHAR(25), |
|
28 |
|
Registered DATE, |
|
29 |
|
Major CHAR(5), |
|
30 |
|
FOREIGN KEY (Major) REFERENCES DEPARTMENT(Code) |
|
31 |
|
); |
|
32 |
|
|
|
33 |
|
CREATE TABLE GRADE( |
|
34 |
|
Login VARCHAR(25), |
|
35 |
|
Grade INT, |
|
36 |
|
PRIMARY KEY(Login, Grade), |
|
37 |
|
FOREIGN KEY (Login) REFERENCES STUDENT(Login) |
|
38 |
|
); |
|
39 |
|
|
|
40 |
|
INSERT INTO DEPARTMENT VALUES |
|
41 |
|
('MATH', 'Mathematics', NULL), |
|
42 |
|
('CS', 'Computer Science', NULL); |
|
43 |
|
|
|
44 |
|
INSERT INTO DEPARTMENT (Code, Name) VALUES |
|
45 |
|
('CYBR', 'Cyber Secturity'); |
|
46 |
|
|
|
47 |
|
INSERT INTO PROF (Login, Department, Name) VALUES |
|
48 |
|
('caubert', 'CS', 'Clément Aubert'); |
|
49 |
|
|
|
50 |
|
INSERT INTO PROF (Login, Name, Department) VALUES |
|
51 |
|
('aturing', 'Alan Turing', 'CS'), |
|
52 |
|
('perdos', 'Paul Erdős', 'MATH'), |
|
53 |
|
('bgates', 'Bill Gates', 'CYBR'); |
|
54 |
|
|
|
55 |
|
INSERT INTO STUDENT (Login, Name, Registered, Major) VALUES |
|
56 |
|
('jrakesh', 'Jalal Rakesh', DATE'2017-12-01', 'CS'), |
|
57 |
|
('svlatka', 'Sacnite Vlatka', '2015-03-12', 'MATH'), |
|
58 |
|
('cjoella', 'Candice Joella', '20120212', 'CYBR'), |
|
59 |
|
('aalyx', 'Ava Alyx', 20121011, 'CYBR'), |
|
60 |
|
('caubert', 'Clément Aubert', NULL, 'CYBR'); |
|
61 |
|
|
|
62 |
|
INSERT INTO GRADE VALUES |
|
63 |
|
('jrakesh', 3.8), |
|
64 |
|
('svlatka', 2.5); |
|
65 |
|
-- end snippet recreate |
|
66 |
|
|
|
67 |
|
-- start snippet lecture |
|
68 |
|
CREATE TABLE HW_Lecture( |
|
69 |
|
Name VARCHAR(25), |
|
70 |
|
Instructor VARCHAR(25), |
|
71 |
|
Year YEAR(4), |
|
72 |
|
Code CHAR(5), |
|
73 |
|
PRIMARY KEY(Year, Code), |
|
74 |
|
FOREIGN KEY (Instructor) REFERENCES PROF(Login) |
|
75 |
|
); |
|
76 |
|
|
|
77 |
|
INSERT INTO HW_Lecture VALUES |
|
78 |
|
('Intro to CS', 'caubert', 2017, '1304'), |
|
79 |
|
('Intro to Algebra', 'perdos', 2017, '1405'), |
|
80 |
|
('Intro to Cyber', 'aturing', 2017, '1234'); |
|
81 |
|
-- end snippet lecture |
|
82 |
|
|
|
83 |
|
ALTER TABLE GRADE |
|
84 |
|
ADD COLUMN LectureCode CHAR(5), |
|
85 |
|
ADD COLUMN LectureYear YEAR(4); |
|
86 |
|
|
|
87 |
|
ALTER TABLE GRADE |
|
88 |
|
ADD FOREIGN KEY (LectureYear, LectureCode) |
|
89 |
|
REFERENCES HW_Lecture(Year, Code); |
|
90 |
|
|
|
91 |
|
-- start snippet grade |
|
92 |
|
DESCRIBE GRADE; |
|
93 |
|
|
|
94 |
|
SELECT * FROM GRADE; |
|
95 |
|
-- end snippet grade |
|
96 |
|
|
|
97 |
|
-- start snippet update |
|
98 |
|
UPDATE GRADE SET LectureCode = '1304', LectureYear = 2017 |
|
99 |
|
WHERE Login = 'jrakesh' |
|
100 |
|
AND Grade = '2.85'; |
|
101 |
|
|
|
102 |
|
UPDATE GRADE SET LectureCode = '1405', LectureYear = 2017 |
|
103 |
|
WHERE Login = 'svlatka' |
|
104 |
|
OR (Login = 'jrakesh' AND Grade = '3.85'); |
|
105 |
|
|
|
106 |
|
UPDATE GRADE SET LectureCode = '1234', LectureYear = 2017 |
|
107 |
|
WHERE Login = 'aalyx' |
|
108 |
|
OR Login = 'cjoella'; |
|
109 |
|
-- end snippet update |
|
110 |
|
|
|
111 |
|
-- start snippet select |
|
112 |
|
SELECT Login, Grade |
|
113 |
|
FROM GRADE |
|
114 |
|
WHERE Lecturecode='1304' |
|
115 |
|
AND LectureYear = '2017'; |
|
116 |
|
|
|
117 |
|
SELECT DISTINCT Instructor |
|
118 |
|
FROM HW_Lecture |
|
119 |
|
WHERE Year = 2017; |
|
120 |
|
|
|
121 |
|
SELECT Name, Grade |
|
122 |
|
FROM STUDENT, GRADE |
|
123 |
|
WHERE GRADE.LectureCode = 1405 |
|
124 |
|
AND STUDENT.Login = GRADE.Login; |
|
125 |
|
|
|
126 |
|
SELECT Year |
|
127 |
|
FROM HW_Lecture |
|
128 |
|
WHERE Code = '1234'; |
|
129 |
|
|
|
130 |
|
SELECT Name |
|
131 |
|
FROM HW_Lecture |
|
132 |
|
WHERE Year IN |
|
133 |
|
(SELECT Year |
|
134 |
|
FROM HW_Lecture |
|
135 |
|
WHERE CODE = '1234'); |
|
136 |
|
|
|
137 |
|
SELECT B.name |
|
138 |
|
FROM STUDENT AS A, STUDENT AS B |
|
139 |
|
WHERE A.Name = 'Ava Alyx' |
|
140 |
|
AND A.Registered > B.Registered; |
|
141 |
|
|
|
142 |
|
SELECT COUNT(DISTINCT PROF.Name) AS 'Head Teaching This Year' |
|
143 |
|
FROM HW_Lecture, DEPARTMENT, PROF |
|
144 |
|
WHERE Year = 2017 |
|
145 |
|
AND Instructor = Head |
|
146 |
|
AND Head = PROF.Login; |
|
147 |
|
-- end snippet select |
File notes/lectures_notes.md changed (mode: 100644) (index e67ec79..5b1eb9c) |
... |
... |
Finally, the comments `-- start snippet something` and `-- end snippet something |
66 |
66 |
<!--: the colors and the ↪ `↪` ↪ ${\tiny\hookrightarrow}$, \tiny\ensuremath{\hookrightarrow} symbols are here to ease the reading, but you can ignore them safely.--> |
<!--: the colors and the ↪ `↪` ↪ ${\tiny\hookrightarrow}$, \tiny\ensuremath{\hookrightarrow} symbols are here to ease the reading, but you can ignore them safely.--> |
67 |
67 |
|
|
68 |
68 |
To clone this source of those notes and have a local copy of it, please refer to the instructions at <http://spots.augusta.edu/caubert/db/ln/README.html>. |
To clone this source of those notes and have a local copy of it, please refer to the instructions at <http://spots.augusta.edu/caubert/db/ln/README.html>. |
69 |
|
Instructions on how to compile those notes are available at the same place. |
|
|
69 |
|
Instructions on how to compile those notes and how to contribute are linked from this document. |
70 |
70 |
|
|
71 |
71 |
<!-- |
<!-- |
72 |
72 |
To clone and compile this document, refer to the `README.md` file, at <https://rocketgit.com/user/caubert/CSCI_3410/source/tree/branch/master/blob/README.md> or <http://spots.augusta.edu/caubert/db/ln/README.html>. |
To clone and compile this document, refer to the `README.md` file, at <https://rocketgit.com/user/caubert/CSCI_3410/source/tree/branch/master/blob/README.md> or <http://spots.augusta.edu/caubert/db/ln/README.html>. |
|
... |
... |
Problem (Revisiting the PROF table) +.#profrevisited |
3519 |
3519 |
|
|
3520 |
3520 |
Create the `PROF`, `DEPARTMENT`, `STUDENT` and `GRADE` tables as in the ["Constructing and populating a new example"](#sec:profexample) section. |
Create the `PROF`, `DEPARTMENT`, `STUDENT` and `GRADE` tables as in the ["Constructing and populating a new example"](#sec:profexample) section. |
3521 |
3521 |
Populate them with some data (copy it from the notes or come up with your own data). |
Populate them with some data (copy it from the notes or come up with your own data). |
|
3522 |
|
|
|
3523 |
|
To obtain exactly the same schema as the one we developped and edited, you can use mysqldump to "dump" this table, with a command like |
|
3524 |
|
|
|
3525 |
|
```{.bash} |
|
3526 |
|
mysqldump -u testuser -ppassword\ |
|
3527 |
|
-h localhost --add-drop-database\ |
|
3528 |
|
--skip-comments --compact\ |
|
3529 |
|
HW_ProfExample > dump.sql |
|
3530 |
|
``` |
|
3531 |
|
|
|
3532 |
|
The code we studied during the lecture is more or less the following. |
|
3533 |
|
|
|
3534 |
|
```{.sqlmysql .numberLines include=code/sql/HW_ProfExampleRevisitedRevisited.sql snippet=recreate} |
|
3535 |
|
``` |
|
3536 |
|
|
|
3537 |
|
We will resume working on this model, and enhance it. |
3522 |
3538 |
|
|
3523 |
3539 |
@problem:profrevisited -- Question -.# |
@problem:profrevisited -- Question -.# |
3524 |
3540 |
|
|
|
... |
... |
Solution to [%D %n (%T)](#problem:profrevisited) |
4437 |
4453 |
@problem:profrevisited-- Solution to Q. -.# |
@problem:profrevisited-- Solution to Q. -.# |
4438 |
4454 |
~ |
~ |
4439 |
4455 |
|
|
|
4456 |
|
The code is straightforward: |
|
4457 |
|
|
|
4458 |
|
```{.sqlmysql .numberLines include=code/sql/HW_ProfExampleRevisitedRevisited.sql snippet=lecture} |
|
4459 |
|
``` |
|
4460 |
|
However, this representation can not handle the following situations: |
|
4461 |
|
|
|
4462 |
|
- If multiple instructors teach the same class, |
|
4463 |
|
- If the HW_Lecture is taught more than once a year (either because it is taught in the Fall, Spring and Summer, or if multiple sections are offered at the same time), |
|
4464 |
|
- If a Lecture is cross-listed, then some duplication of information will be needed. |
|
4465 |
|
|
|
4466 |
|
@problem:profrevisited-- Solution to Q. -.# |
|
4467 |
|
~ |
|
4468 |
|
|
|
4469 |
|
The statements are immediate: |
4440 |
4470 |
|
|
4441 |
|
For the other questions, refer to this code. |
|
|
4471 |
|
```{.sqlmysql .numberLines include=code/sql/HW_ProfExampleRevisitedRevisited.sql snippet=grade} |
|
4472 |
|
``` |
|
4473 |
|
|
|
4474 |
|
What may be surprising is that the values for `LectureCode` and `LectureYear` are set to `NULL` in all the tuples. |
|
4475 |
|
|
|
4476 |
|
@problem:profrevisited-- Solution to Q. -.# |
|
4477 |
|
~ |
|
4478 |
|
|
|
4479 |
|
We use `UPDATE` statements: |
|
4480 |
|
|
|
4481 |
|
```{.sqlmysql .numberLines include=code/sql/HW_ProfExampleRevisitedRevisited.sql snippet=update} |
|
4482 |
|
``` |
4442 |
4483 |
|
|
4443 |
|
```{.sqlmysql .numberLines include=code/sql/HW_Lecture.sql} |
|
4444 |
|
``` |
|
|
4484 |
|
@problem:profrevisited-- Solution to Q. -.# |
|
4485 |
|
~ |
|
4486 |
|
We refer back to the solution to Q. 1. |
|
4487 |
|
|
|
4488 |
|
@problem:profrevisited-- Solution to Q. -.# |
|
4489 |
|
~ |
4445 |
4490 |
|
|
|
4491 |
|
We use `SELECT` statements: |
|
4492 |
|
|
|
4493 |
|
```{.sqlmysql .numberLines include=code/sql/HW_ProfExampleRevisitedRevisited.sql snippet=select} |
|
4494 |
|
``` |
4446 |
4495 |
--- |
--- |
4447 |
4496 |
|
|
4448 |
4497 |
Solution to [%D %n (%T)](#problem:train) |
Solution to [%D %n (%T)](#problem:train) |
|
... |
... |
Problem (Using MySQL Workbench's reverse engineering) +.#reverseeng |
7048 |
7097 |
Using the relational database schema you obtained in @problem:UMLtoRELDriver, write the `SQL` implementation of that database. |
Using the relational database schema you obtained in @problem:UMLtoRELDriver, write the `SQL` implementation of that database. |
7049 |
7098 |
Then, using MySQL Workbench, use the "Reverse Engineering" function to obtain an EER diagram of your database and compare it with the UML diagram from @problem:UMLtoRELDriver. |
Then, using MySQL Workbench, use the "Reverse Engineering" function to obtain an EER diagram of your database and compare it with the UML diagram from @problem:UMLtoRELDriver. |
7050 |
7099 |
Apart from the difference inherent to the nature of the diagram (i.e., UML vs EER), how else are they different? |
Apart from the difference inherent to the nature of the diagram (i.e., UML vs EER), how else are they different? |
7051 |
|
How do they the same? |
|
|
7100 |
|
How are they the same? |
7052 |
7101 |
Is the automated tool as efficient and accurate as you are? |
Is the automated tool as efficient and accurate as you are? |
7053 |
7102 |
|
|
7054 |
7103 |
--- |
--- |
|
... |
... |
Solution to [%D %n (%T)](#problem:reverseeng) |
7441 |
7490 |
- <http://spots.augusta.edu/caubert/teaching/general/java/> |
- <http://spots.augusta.edu/caubert/teaching/general/java/> |
7442 |
7491 |
- If you experience any trouble, <https://www.ntu.edu.sg/home/ehchua/programming/howto/ErrorMessages.html#JDBCErrors> might be a good read. |
- If you experience any trouble, <https://www.ntu.edu.sg/home/ehchua/programming/howto/ErrorMessages.html#JDBCErrors> might be a good read. |
7443 |
7492 |
- [@Textbook6, 13.3.2] or [@Textbook7, Chapter 10] is a condensed, but good, read. |
- [@Textbook6, 13.3.2] or [@Textbook7, Chapter 10] is a condensed, but good, read. |
7444 |
|
- Many textbooks on Java include a part on Databases, just like this one: [F@Gaddis2014, Chapter 16]. |
|
|
7493 |
|
- Many textbooks on Java include a part on Databases, just like this one: [@Gaddis2014, Chapter 16]. |
7445 |
7494 |
|
|
7446 |
7495 |
## Overview |
## Overview |
7447 |
7496 |
|
|
|
... |
... |
Solution to [%D %n (%T)](#problem:xmltoeraward) |
9081 |
9130 |
Therefore, it seems more reasonnable to make the award an entity. |
Therefore, it seems more reasonnable to make the award an entity. |
9082 |
9131 |
|
|
9083 |
9132 |
How should we connect the AWARD entity with the RESEARCHER and INSTITUTION entities? |
How should we connect the AWARD entity with the RESEARCHER and INSTITUTION entities? |
9084 |
|
A trinary relation has some drawbacks, since it would require some duplication when multiple investigators are working on the same award. |
|
|
9133 |
|
A ternary relation has some drawbacks, since it would require some duplication when multiple investigators are working on the same award. |
9085 |
9134 |
Instead, having one binary relationship between the award and the institution, and one binary relationship between the award and the researcher (that specifies further the role of the researcher for that particular award), seems like a safer choice. |
Instead, having one binary relationship between the award and the institution, and one binary relationship between the award and the researcher (that specifies further the role of the researcher for that particular award), seems like a safer choice. |
9086 |
9135 |
An award must be awarded to at least one researcher and one institution, but we do not know if there is a maximum number of institutions that can obtain the same award, so it is better not to restrict this arity. |
An award must be awarded to at least one researcher and one institution, but we do not know if there is a maximum number of institutions that can obtain the same award, so it is better not to restrict this arity. |
9087 |
9136 |
Whether there should be a relationship between the researcher and the institution is up in the air; we do not know if a researcher has to work for an institution to get a grant, nor if getting a grant for an institution means that you work for it, so it is probably better to refrain from adding such a relationship. |
Whether there should be a relationship between the researcher and the institution is up in the air; we do not know if a researcher has to work for an institution to get a grant, nor if getting a grant for an institution means that you work for it, so it is probably better to refrain from adding such a relationship. |