-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_queries.sql
102 lines (90 loc) · 4.61 KB
/
database_queries.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
DROP TABLE IF EXISTS StudentClassEnrollments;
DROP TABLE IF EXISTS Students;
DROP TABLE IF EXISTS Classes;
DROP TABLE IF EXISTS Instructors;
DROP TABLE IF EXISTS Houses;
CREATE TABLE Houses
(
houseID INT(11) UNIQUE AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
founder VARCHAR(255) NOT NULL,
animal VARCHAR(255) NOT NULL,
colors VARCHAR(255) NOT NULL,
points INT(11) NOT NULL DEFAULT 0
);
CREATE TABLE Students
(
studentID INT(11) UNIQUE AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(250) NOT NULL,
year INT(4) NOT NULL,
patronus VARCHAR(250),
wandType VARCHAR(250),
prefect BOOLEAN not NULL DEFAULT FALSE,
houseID INT NOT NULL DEFAULT 0,
FOREIGN KEY (houseID) REFERENCES Houses (houseID)
);
CREATE TABLE Instructors
(
instructorID INT(11) UNIQUE AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(250) NOT NULL,
patronus VARCHAR(250),
wandType VARCHAR(250),
houseID INT,
FOREIGN KEY (houseID) REFERENCES Houses (houseID)
);
CREATE TABLE Classes
(
classID INT(11) UNIQUE AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
maxSize INT(11) NOT NULL DEFAULT 0,
description VARCHAR(255),
instructorID INT NOT NULL,
FOREIGN KEY (instructorID) REFERENCES Instructors (instructorID)
);
CREATE TABLE StudentClassEnrollments
(
enrollmentID INT(11) AUTO_INCREMENT NOT NULL,
studentID INT(11) NOT NULL,
classID INT(11) NOT NULL,
finished BOOLEAN NOT NULL,
rating INT(11),
year INT(11) NOT NULL,
term INT(11),
PRIMARY KEY (enrollmentID),
FOREIGN KEY (studentID) REFERENCES Students (studentID) ON DELETE CASCADE,
FOREIGN KEY (classID) REFERENCES Classes (classID)
);
-- House Data
INSERT INTO Houses (name, founder, animal, colors)
VALUES ('Gryffindor', 'Godric Gryffindor', 'Lion', 'Red and Gold'),
('Ravenclaw', 'Rowena Ravenclaw', 'Eagle', 'Blue and Bronze'),
('Slytherin', 'Salazar Slytherin', 'Snake', 'Green and Silver'),
('Hufflepuff', 'Helga Hufflepuff', 'Badger', 'Yellow and Black');
INSERT INTO Students (name, year, patronus, wandType, prefect, houseID)
VALUES ('Harry Potter', 1, 'Stag', 'Elder Wood', FALSE, (SELECT houseID FROM Houses WHERE name = 'Gryffindor')),
('Hermione Granger', 1, 'Otter', 'Vine Wood', FALSE, (SELECT houseID FROM Houses WHERE name = 'Gryffindor')),
('Draco Malfoy', 1, NULL, 'Hawthorn Wood', FALSE, (SELECT houseID FROM Houses WHERE name = 'Slytherin')),
('Percy Weasley', 4, NULL, 'Wood', TRUE, (SELECT houseID FROM Houses WHERE name = 'Gryffindor')),
('Luna Lovegood', 1, 'Hare', 'Wood', FALSE, (SELECT houseID FROM Houses WHERE name = 'Hufflepuff'));
INSERT INTO Instructors (name, patronus, wandType, houseID)
VALUES ('Minerva McGonagall', 'Cat', 'Fir', (SELECT houseID FROM Houses WHERE name = 'Gryffindor')),
('Severus Snape', 'Doe', 'Wood', (SELECT houseID FROM Houses WHERE name = 'Slytherin')),
('Filius Flitwick', NULL, 'Wood', (SELECT houseID FROM Houses WHERE name = 'Ravenclaw'));
INSERT INTO Classes (name, maxSize, description, instructorID)
VALUES ('Transfiguration', 40, 'Alteration of the form or appearance of an object',
(SELECT instructorID FROM Instructors WHERE name = 'Minerva McGonagall')),
('Potions', 20, 'The correct way to brew potions',
(SELECT instructorID FROM Instructors WHERE name = 'Severus Snape')),
('Charms', 30, 'The science of charm-work',
(SELECT instructorID FROM Instructors WHERE name = 'Filius Flitwick'));
INSERT INTO StudentClassEnrollments (studentID, classID, finished, rating, year, term)
VALUES ((SELECT studentID FROM Students WHERE name = 'Harry Potter'),
(SELECT classID FROM Classes WHERE name = 'Transfiguration'), FALSE, NULL, 1, 1),
((SELECT studentID FROM Students WHERE name = 'Percy Weasley'),
(SELECT classID FROM Classes WHERE name = 'Transfiguration'), TRUE, 8, 1, 1),
((SELECT studentID FROM Students WHERE name = 'Hermione Granger'),
(SELECT classID FROM Classes WHERE name = 'Potions'), FALSE, NULL, 1, 1),
((SELECT studentID FROM Students WHERE name = 'Luna Lovegood'),
(SELECT classID FROM Classes WHERE name = 'Charms'), FALSE, NULL, 1, 1);
select *
from Students;