-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate-db-template.sql
More file actions
201 lines (172 loc) · 6.84 KB
/
Copy pathcreate-db-template.sql
File metadata and controls
201 lines (172 loc) · 6.84 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
-- Template for creating the database given by `@{database}`.
-- Create the database if it does not exist, and check.
CREATE DATABASE IF NOT EXISTS @{database};
SHOW DATABASES;
-- Enter the database
USE @{database};
-- Procedure to add all tables so far in a given database to
-- "Tables_here"
DROP PROCEDURE IF EXISTS update_Tables_here;
CREATE PROCEDURE update_Tables_here(IN database_name CHAR(64))
BEGIN
INSERT IGNORE INTO Tables_here (TABLE_NAME) (
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA=database_name
);
-- show number of rows inserted
SELECT ROW_COUNT();
END; -- PROCEDURE update_Tables_here(database_name)
-- Create a table of tables
-- tid gives creation order
CREATE TABLE IF NOT EXISTS Tables_here (
tid INT NOT NULL AUTO_INCREMENT,
TABLE_NAME VARCHAR(255) NOT NULL,
KEY tid (tid),
CONSTRAINT name_is_primary_key PRIMARY KEY (TABLE_NAME)
);
CALL update_Tables_here(database());
-- Create the Profile entity table
CREATE TABLE IF NOT EXISTS Profile (
pfid INT NOT NULL AUTO_INCREMENT,
UserName VARCHAR(20) NOT NULL,
UserEmail VARCHAR(40) NOT NULL,
UserType ENUM('educator', 'student')
NOT NULL,
UserImage VARCHAR(2048) NOT NULL,
StyleSheet ENUM('lightmode', 'darkmode')
NOT NULL,
-- dash separated ISO language (3), country (3), script (4) codes
PreferredLanguage VARCHAR(12) NOT NULL,
pfLevel INT NOT NULL DEFAULT 1,
score INT NOT NULL DEFAULT 0,
KEY pfid (pfid),
CONSTRAINT name_is_primary_key PRIMARY KEY (UserName)
);
CALL update_Tables_here(database());
-- Create the Canvas-linked Accounts entity table
CREATE TABLE IF NOT EXISTS CanvasAccount (
cacid INT NOT NULL AUTO_INCREMENT,
pfid INT NOT NULL,
pat BINARY(128) NOT NULL,
-- allow VARCHAR(4x) for salt, in case of escaped characters
patSalt VARCHAR(512) NOT NULL,
CONSTRAINT id_is_primary_key PRIMARY KEY (cacid),
CONSTRAINT cac_Profile_id_references FOREIGN KEY (pfid) REFERENCES Profile(pfid)
);
CALL update_Tables_here(database());
-- Create the Game Accounts entity table
CREATE TABLE IF NOT EXISTS GameAccount (
pfid INT NOT NULL,
password BINARY(128) NOT NULL,
-- allow VARCHAR(4x) for salt, in case of escaped characters
passwordSalt VARCHAR(512) NOT NULL,
CONSTRAINT pfid_is_primary_key PRIMARY KEY (pfid),
CONSTRAINT gac_Profile_id_references FOREIGN KEY (pfid) REFERENCES Profile(pfid)
);
CALL update_Tables_here(database());
-- Create the Group entity table
CREATE TABLE IF NOT EXISTS UserGroup (
grid CHAR(12) NOT NULL,
groupName VARCHAR(50) NOT NULL,
CONSTRAINT id_is_primary_key PRIMARY KEY (grid)
);
CALL update_Tables_here(database());
-- Create the "is in Group" relation table
CREATE TABLE IF NOT EXISTS inGroup (
inGrid CHAR(24) NOT NULL,
pfid INT NOT NULL,
grid CHAR(12) NOT NULL,
CONSTRAINT id_is_primary_key PRIMARY KEY (grid),
CONSTRAINT inGr_Profile_id_references FOREIGN KEY (pfid) REFERENCES Profile(pfid),
CONSTRAINT Group_id_references FOREIGN KEY (grid) REFERENCES UserGroup(grid)
);
CALL update_Tables_here(database());
-- Create the Lesson entity table
CREATE TABLE IF NOT EXISTS Lesson (
lsid CHAR(18) NOT NULL,
pfid INT NOT NULL,
lsLevel INT NOT NULL,
CONSTRAINT id_is_primary_key PRIMARY KEY (lsid),
CONSTRAINT ls_Profile_id_references FOREIGN KEY (pfid) REFERENCES Profile(pfid)
);
CALL update_Tables_here(database());
-- Create the Item entity table
-- Note: for itSource, 2048 is the maximum URL length in IExplorer
CREATE TABLE IF NOT EXISTS Item (
itid CHAR(21) NOT NULL,
itName VARCHAR(255) NOT NULL,
itSource VARCHAR(2048) NOT NULL,
CONSTRAINT id_is_primary_key PRIMARY KEY (itid)
);
CALL update_Tables_here(database());
-- Create the Question entity table
CREATE TABLE IF NOT EXISTS Question (
qsid CHAR(20) NOT NULL,
lsid CHAR(18) NOT NULL,
qsItid CHAR(21) NOT NULL,
CONSTRAINT id_is_primary_key PRIMARY KEY (qsid),
CONSTRAINT Lesson_id_references FOREIGN KEY (lsid) REFERENCES Lesson(lsid),
CONSTRAINT qsItem_id_references FOREIGN KEY (qsItid) REFERENCES Item(itid)
);
CALL update_Tables_here(database());
-- Create the answers relation table
CREATE TABLE IF NOT EXISTS answers (
anid CHAR(21) NOT NULL,
qsid CHAR(20) NOT NULL,
itid CHAR(21) NOT NULL,
isCorrect BOOLEAN NOT NULL,
CONSTRAINT id_is_primary_key PRIMARY KEY (anid),
CONSTRAINT Question_id_references FOREIGN KEY (qsid) REFERENCES Question(qsid),
CONSTRAINT Item_id_references FOREIGN KEY (itid) REFERENCES Item(itid)
);
CALL update_Tables_here(database());
-- Create the Room entity table
CREATE TABLE IF NOT EXISTS Room (
room_id CHAR(12) NOT NULL,
room_name VARCHAR(200) NOT NULL,
room_type VARCHAR(20) NOT NULL,
CONSTRAINT id_is_primary_key PRIMARY KEY (room_id)
);
CALL update_Tables_here(database());
-- Create the Message entity table
CREATE TABLE IF NOT EXISTS Message (
message_id CHAR(12) NOT NULL,
room_id CHAR(12) NOT NULL,
from_user_id INT NOT NULL,
to_user_id INT NOT NULL,
content VARCHAR(200) NOT NULL,
CONSTRAINT id_is_primary_key PRIMARY KEY (message_id),
CONSTRAINT Room_id_references FOREIGN KEY (room_id) REFERENCES Room(room_id)
);
CALL update_Tables_here(database());
-- Show all tables created
SHOW TABLES;
-- Show table of tables in creation order
SELECT tid, TABLE_NAME FROM Tables_here;
-- Count the tables stored
SET @N_TABLES_HERE := (SELECT COUNT(*) FROM Tables_here);
SELECT @N_TABLES_HERE;
-- Describe every table
DROP PROCEDURE IF EXISTS describe_tables_here;
CREATE PROCEDURE describe_tables_here(IN n_tables INT)
BEGIN
-- index of the tables
DECLARE k INT DEFAULT 0;
-- for each table
WHILE (k < n_tables) DO
-- Concatenate the describe statement
SET @expression := (
SELECT CONCAT('DESCRIBE ', TABLE_NAME, ';')
FROM Tables_here ORDER BY tid LIMIT k, 1
);
SELECT @expression;
-- Create the statement
PREPARE stmt FROM @expression;
-- Perform the statement
EXECUTE stmt;
-- increment the counter
SET k := k + 1;
END WHILE; -- WHILE (k <= n_tables)
END; -- PROCEDURE describe_tables_here()
CALL describe_tables_here((@N_TABLES_HERE));
DROP PROCEDURE describe_tables_here;