-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
352 lines (270 loc) · 8.74 KB
/
schema.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
DROP TABLE IF EXISTS homeworksInClass;
DROP TABLE IF EXISTS class;
DROP TABLE IF EXISTS learnerLanguages;
DROP TABLE IF EXISTS targetLanguages;
DROP TABLE IF EXISTS teacherLanguages;
DROP TABLE IF EXISTS Learner ;
DROP TABLE IF EXISTS Teacher;
DROP TABLE IF EXISTS inbox_participants;
DROP TABLE IF EXISTS inbox;
DROP TABLE IF EXISTS messages;
DROP TABLE IF EXISTS Language;
DROP TABLE IF EXISTS Level;
DROP TABLE IF EXISTS Gender;
CREATE TABLE IF NOT EXISTS Gender(
genderName VARCHAR(50) PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS Language(
languageName VARCHAR(50) PRIMARY KEY
);
Create TABLE IF NOT EXISTS Level(
level VARCHAR(50) PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS Learner(
learnerId VARCHAR(20) primary key ,
username VARCHAR(50) UNIQUE ,
learnerName VARCHAR(50),
surname VARCHAR(50),
gender VARCHAR(50),
email VARCHAR(345) UNIQUE,
"password" VARCHAR(50),
FOREIGN KEY (gender) references Gender(genderName)
);
CREATE TABLE IF NOT EXISTS Teacher(
teacherId varchar(20) PRIMARY KEY,
username VARCHAR(50) UNIQUE ,
teacherName varchar(50),
surname VARCHAR(50),
gender VARCHAR(50),
email VARCHAR(345) UNIQUE ,
"password" VARCHAR(50),
FOREIGN KEY (gender) references Gender(genderName)
);
CREATE TABLE IF NOT EXISTS learnerLanguages(
languageName varchar(50),
learnerId varchar(50),
level varchar(50),
FOREIGN KEY (languageName) references Language(languageName),
FOREIGN KEY (learnerId) references Learner(learnerId),
FOREIGN KEY (level) references Level(level)
);
/* teacher languages table*/
CREATE TABLE IF NOT EXISTS teacherLanguages(
languageName varchar(50),
teacherId varchar(50),
level varchar(50),
FOREIGN KEY (languageName) references Language(languageName),
FOREIGN KEY (teacherId) references Teacher(teacherId),
FOREIGN KEY (level) references Level(level)
);
CREATE TABLE IF NOT EXISTS targetLanguages(
languageName varchar(50),
learnerId varchar(50),
targetLevel varchar(50),
FOREIGN KEY (languageName) references Language(languageName),
FOREIGN KEY (learnerId) references Learner(learnerId),
FOREIGN KEY (targetLevel) references Level(level)
);
CREATE TABLE IF NOT EXISTS class(
classId SERIAL PRIMARY KEY, /* can be redundant*/
languageName varchar(50),
learnerId varchar(50),
teacherId varchar(50),
classLevel varchar(50),
creationTime date default NOW(),
classDate date,
classStatus VARCHAR(50) default 'waiting',
FOREIGN KEY (languageName) references Language(languageName),
FOREIGN KEY (learnerId) references Learner(learnerId),
FOREIGN KEY (teacherId) references Teacher(teacherId)
);
CREATE TABLE IF NOT EXISTS homeworksInClass(
classId INT,
teacherId varchar(50),
learnerId varchar(50),
homeworkDescription varchar(1000),
point FLOAT,
FOREIGN KEY (learnerId) references Learner(learnerId),
FOREIGN KEY (teacherId) references Teacher(teacherId),
FOREIGN KEY (classId) references class(classId)
);
CREATE VIEW waitingClasses AS (
Select * FROM class WHERE class.classStatus = 'waiting'
);
CREATE VIEW activeClasses AS (
Select * FROM class WHERE class.classStatus = 'active'
);
CREATE VIEW rejectedClasses AS(
Select * FROM class WHERE class.classStatus = 'rejected'
);
CREATE VIEW finishedClasses AS(
Select * FROM class WHERE class.classStatus = 'finished'
);
CREATE TABLE messages (
inbox_id varchar(8),
user_id varchar(20),
"message" varchar(500),
created_at date default now()
);
CREATE TABLE inbox(
inbox_id varchar(8),
last_message varchar(500) default NULL,
last_sent_user_id varchar(20) default NULL
);
CREATE TABLE inbox_participants(
user_id varchar(20),
inbox_id varchar(8),
PRIMARY KEY (user_id, inbox_id)
);
CREATE TABLE IF NOT EXISTS teacherLanguages(
languageName varchar(50),
teacherid varchar(50),
level varchar(50),
FOREIGN KEY (languageName) references Language(languageName),
FOREIGN KEY (teacherid) references teacher(teacherid),
FOREIGN KEY (level) references Level(level)
);
ALTER TABLE Level ADD rank INT;
ALTER TABLE homeworksInClass ADD point FLOAT;
CREATE VIEW teacher_name_surname AS
SELECT teacherid, teacherName, surname FROM teacher;
INSERT INTO Language VALUES
('English'),
('French'),
('Turkish');
INSERT INTO Level Values
('A1'),
('A2'),
('B1'),
('B2'),
('C1'),
('C2'),
('Native');
INSERT INTO Gender VALUES
('Male'),
('Female'),
('Other');
/*
INSERT INTO learnerLanguages VALUES
('French', 'a18fbf9acca53f39a929', 'A1' ),
('English', 'a18fbf9acca53f39a929', 'B2');
SELECT teacherLanguages.teacherid ,teacherName, surname FROM teacher_name_surname ,teacherLanguages, Level a, Level b, Level c
WHERE teacherLanguages.languageName = 'English'
AND teacherLanguages.level = a.level
AND b.level = 'A1' AND c.level = 'C2'
AND a.rank >= b.rank AND a.rank <= c.rank
AND teacher_name_surname.teacherId = teacherLanguages.teacherid;
CREATE SEQUENCE class_seq START 1;
*/
/*class last updated trigger*/
ALTER TABLE class ADD COLUMN last_updated TIMESTAMP DEFAULT NOW();
ALTER TABLE homeworksInClass ADD COLUMN submission varchar(500) DEFAULT null;
-- Add a serial column named 'serial_column' as the primary key to the existing table 'your_table'
ALTER TABLE homeworksInClass
ADD COLUMN homework_id SERIAL PRIMARY KEY;
CREATE VIEW waitingHomework AS(
Select * FROM homeworksInClass WHERE homeworksInClass.submission is null
);
CREATE VIEW finishedhomework AS(
Select * FROM homeworksInClass WHERE homeworksInClass.submission is not null
);
CREATE OR REPLACE FUNCTION update_last_updated_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.last_updated = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_class_last_updated
BEFORE UPDATE ON class
FOR EACH ROW
EXECUTE FUNCTION update_last_updated_column();
/*contraint to ensure class is in the future*/
ALTER TABLE class ADD CONSTRAINT class_date_future CHECK (classDate > NOW());
/*only one assign*/
ALTER TABLE learnerLanguages ADD CONSTRAINT unique_language UNIQUE (learnerId, languageName);
/*using range*/
SELECT * FROM class
WHERE classDate BETWEEN '2023-01-01' AND '2023-12-31';
/*using like to search users, might delete*/
SELECT * FROM teacher
WHERE teacherName LIKE '%eyl%';
/*sorting users in the chat screen, complex query*/
SELECT
u.userName,
u.userType,
ul.languageName,
ul.level
FROM (
SELECT
learnerName AS userName,
'Learner' AS userType,
learnerId
FROM
Learner
UNION
SELECT
teacherName AS userName,
'Teacher' AS userType,
teacherId
FROM
Teacher
) AS u
JOIN (
SELECT
languageName,
learnerId AS userId,
level
FROM
learnerLanguages
UNION
SELECT
languageName,
teacherid AS userId,
level
FROM
teacherLanguages
) AS ul ON u.learnerId = ul.userId
WHERE
ul.languageName = 'English'
AND ul.level BETWEEN 'A1' AND 'C1'
ORDER BY
ul.level DESC,
u.userName;
/*
WITH CombinedUser AS (
SELECT Teacher.*, 'teacher' as user_type FROM Teacher
UNION
SELECT * , 'learner ' FROM Learner
) ,CombinedLanguagase AS (
SELECT * FROM teacherlanguages
UNION
SELECT * FROM learnerlanguages
)
SELECT CombinedLanguagase.teacherid , CombinedUser.teacherName, CombinedUser.surname FROM CombinedUser , CombinedLanguagase, Level a, Level b, Level c
WHERE CombinedLanguagase.languageName = ($1)
AND CombinedLanguagase.level = a.level
AND b.level = ($2) AND c.level = ($3)
AND a.rank >= b.rank AND a.rank <= c.rank
AND CombinedUser.teacherId = CombinedLanguagase.teacherid;
*/
-- Create the trigger function
CREATE OR REPLACE FUNCTION update_inbox_trigger()
RETURNS TRIGGER AS $$
BEGIN
-- Update the inbox table with the last message and user_id
UPDATE inbox
SET
last_message = NEW.message,
last_sent_user_id = NEW.user_id
WHERE inbox_id = NEW.inbox_id;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Create the trigger
CREATE TRIGGER after_insert_messages
AFTER INSERT
ON messages
FOR EACH ROW
EXECUTE FUNCTION update_inbox_trigger();
Select COUNT(*) as languageCount FROM learnerlanguages WHERE languagename = 'English'