-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.sql
More file actions
703 lines (555 loc) · 14.5 KB
/
Copy pathScript.sql
File metadata and controls
703 lines (555 loc) · 14.5 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
USE MASTER;
DROP DATABASE IF EXISTS UniversityDB;
CREATE DATABASE UniversityDB;
USE UniversityDB;
/*
Catelogo donde se guardarán las diferentes estados
que puede tener una suscripcion a un curso.
Ejemplo:
- Matriculado
- Aprobado
- Reprobado
*/
CREATE TABLE cat_suscriptions_status (
id CHAR(5) NOT NULL,
title VARCHAR(50) NOT NULL
)
ALTER TABLE cat_suscriptions_status
ADD CONSTRAINT PK_cat_suscriptions_status
PRIMARY KEY (id);
/*
Catalogo donde se guardarán las modalidades
en las que se podrán dar las clases
Ejemplo:
- Virtual
- Presencial
*/
CREATE TABLE cat_modality(
id CHAR(5) NOT NULL,
[name] VARCHAR(20),
[description] VARCHAR(200)
);
ALTER TABLE cat_modality
ADD CONSTRAINT PK_cat_modality
PRIMARY KEY (id);
/*
Catalogo de categorías academicas, donde se guardaran las diferentes
categorías academicas.
Ejemplo:
- Bachillerato
- Licenciatura
- Postgrados
- Cursos Libres
- Diplomas
*/
CREATE TABLE cat_academic_category (
id CHAR(5) NOT NULL,
[name] VARCHAR(50),
[description] VARCHAR(200)
);
ALTER TABLE cat_academic_category
ADD CONSTRAINT PK_cat_academic_category
PRIMARY KEY(id);
/*
Catalogo donde se guardaran los diferentes periodos
en los que se organizar los cursos.
Ejemplo:
- Cuatrimestre
- Trimestre
*/
CREATE TABLE cat_academic_periods(
id CHAR(5) NOT NULL,
[name] VARCHAR(20),
[description] VARCHAR(200)
)
ALTER TABLE cat_academic_periods
ADD CONSTRAINT PK_cat_academic_periods
PRIMARY KEY(id);
/*
Catalogo de cursos, donde se guardarán datos
tales como las "clases" que se pueden llegar a dar
en el curso.
Ejemplo:
- Matematicas I
- Ingles I
- Ingles II
*/
CREATE TABLE cat_courses(
[code] CHAR(10) NOT NULL,
[name] VARCHAR(100) NOT NULL,
[description] VARCHAR(200),
[status] INT NOT NULL DEFAULT 0 -- 1 activo, 0 inactivo
-- Department VARCHAR(100) NOT NULL,
-- Credits INT NOT NULL,
-- Instructor VARCHAR(100) NOT NULL,
-- Prerequisites TEXT,
-- SemesterOffered VARCHAR(50),
--YearOffered INT
);
ALTER TABLE cat_courses
ADD CONSTRAINT PK_cat_courses
PRIMARY KEY (code);
/*
Catalogo de carreras, donde se guardarán todas
las carreras ofrecidas por la universidad.
Ejemplo:
- Ing. Informatica
- Contbilidad
- Administracion de empresas
*/
-- All careers of university
CREATE TABLE cat_career(
[code] CHAR(10) NOT NULL,
[name] VARCHAR(100) NOT NULL,
[description] VARCHAR(200),
[status] INT NOT NULL DEFAULT 0
);
ALTER TABLE cat_career
ADD CONSTRAINT PK_cat_career
PRIMARY KEY (code);
/*
Catalogo de cursos por carrera, donde se guardara una relacion
entre la carrera y el curso.
Ejemplo:
- Ing. Informatica --> Matematicas I
- Adm. Empresas --> Matematicas I
- Ing. Informatica --> Programación I
- Ing. Informatica --> Ingles I
- Adm. Empresas --> Ingles I
*/
CREATE TABLE cat_courses_x_career(
id INT NOT NULL UNIQUE,
[code_career] CHAR(10) NOT NULL,
[code_course] CHAR(10) NOT NULL
);
ALTER TABLE cat_courses_x_career
ADD CONSTRAINT PK_cat_courses_x_career
PRIMARY KEY ([code_career],[code_course] ),
--
CONSTRAINT FK01_cat_career__cat_courses
FOREIGN KEY ([code_career])
REFERENCES cat_career([code]),
--
CONSTRAINT FK02_cat_career__cat_courses
FOREIGN KEY ([code_course])
REFERENCES cat_courses([code]);
/*
Tabla donde se guardarán todos los roles disponibles
Ejemplo:
- Administrador
- Profesor
- Estudiante
*/
-- All roles of DB
CREATE TABLE roles (
id CHAR(5) NOT NULL,
[name] VARCHAR(20),
[description] VARCHAR(200)
);
ALTER TABLE roles
ADD CONSTRAINT PK_roles
PRIMARY KEY(id);
/*
Tabla donde se guardaran todos los usuarios del sistema
*/
-- All users
CREATE TABLE users (
id INT NOT NULL,
last_login DATETIME NOT NULL,
[name] VARCHAR(200),
[last_name] VARCHAR(200),
[gender] CHAR(1),
[birts_date] DATE,
create_at DATETIME,
modified_at DATETIME
);
ALTER TABLE users
ADD CONSTRAINT PK_users
PRIMARY KEY (id);
/*
Tabla de roles por usuario, donde se guardara los roles
con los que cuenta cada usuario.
Ejemplo:
- User1 --> Administrador
- User1 --> Profesor
- User2 --> Estudiante
*/
-- Role X User
CREATE TABLE roles_user (
id_user INT NOT NULL,
id_role CHAR(5) NOT NULL
);
ALTER TABLE roles_user
ADD CONSTRAINT PK_roles_user
PRIMARY KEY (id_user,id_role ),
--
CONSTRAINT FK01_roles_user__users
FOREIGN KEY (id_user)
REFERENCES users(id),
--
CONSTRAINT FK01_roles_user__roles
FOREIGN KEY (id_role)
REFERENCES roles(id);
/*
Tabla donde se almacenaran los correos de los usuarios.
*/
-- Emails of user
CREATE TABLE user_emails (
id_user INT NOT NULL,
[email] NCHAR(255) NOT NULL,
[default] INT NOT NULL DEFAULT 0,
[status] INT NOT NULL DEFAULT 0
);
ALTER TABLE user_emails
ADD CONSTRAINT PK_user_emails
PRIMARY KEY (id_user,[email]),
--
CONSTRAINT FK_user_emails__users
FOREIGN KEY (id_user)
REFERENCES users(id);
/*
Tabla donde se guardara la información de login de los usuarios.
*/
-- Tabla para login
CREATE TABLE login_user (
id_user INT NOT NULL,
[user_access] NCHAR(50) NOT NULL,
[password] NCHAR(255) NOT NULL,
[status] INT NOT NULL DEFAULT 0,
create_at DATETIME,
modified_at DATETIME
)
ALTER TABLE login_user
ADD CONSTRAINT PK_login_user
PRIMARY KEY (id_user, [user_access]),
--
CONSTRAINT FK_login_user__users
FOREIGN KEY (id_user)
REFERENCES users(id);
/*
Tabla donde se alamacenara los planes de estudio disponibles.
*/
CREATE TABLE study_plan(
id INT NOT NULL,
id_academic_category CHAR(5) NOT NULL,
title VARCHAR(30) NOT NULL,
[description] VARCHAR(400) NOT NULL,
create_at DATETIME NOT NULL
)
ALTER TABLE study_plan
ADD CONSTRAINT PK_study_plan
PRIMARY KEY(id),
CONSTRAINT FK01_study_plan__cat_academic_category
FOREIGN KEY (id_academic_category)
REFERENCES cat_academic_category(id);
/*
Tabla donde se almacenará la relacion entre
el plan de estudio y los cursos que se llevaran.
*/
CREATE TABLE study_plan_courses(
id_study_plan INT NOT NULL,
course_code INT NOT NULL,
[block] INT NOT NULL,
[order] INT NOT NULL,
create_at DATETIME NOT NULL,
credits INT NOT NULL DEFAULT 0
);
ALTER TABLE study_plan_courses
ADD CONSTRAINT PK_study_plan_courses
PRIMARY KEY (id_study_plan, course_code),
--
CONSTRAINT FK01_study_plan_courses__study_plan
FOREIGN KEY (id_study_plan)
REFERENCES study_plan(id),
--
CONSTRAINT FK02_study_plan_courses__cat_courses
FOREIGN KEY (course_code)
REFERENCES cat_courses_x_career(id)
;
/*
Tabla donde se almacenarán los datos sobre los periodos de estudio
Ejemplo:
1 - Cuatrimestre - 10/09/2023 - 15/12/2023 - Matricula Abierta
2 - Cuatrimestre - 15/04/2023 - 10/09/2023 - Concluido
*/
CREATE TABLE [periods](
id INT NOT NULL,
id_cat_period CHAR(5) NOT NULL,
[start_date] DATETIME NOT NULL,
[end_date] DATETIME NOT NULL,
[description] VARCHAR(200),
[status] INT NOT NULL DEFAULT 0
)
ALTER TABLE [periods]
ADD CONSTRAINT PK_periods
PRIMARY KEY(id),
--
CONSTRAINT FK01_periods__cat_academic_periods
FOREIGN KEY (id_cat_period)
REFERENCES cat_academic_periods(id);
/*
Tabla donde se almacenara el detalle de los cursos
que se llevarán acabo en ese periodo de estudio.
*/
CREATE TABLE periods_courses (
id INT NOT NULL UNIQUE,
id_periods INT NOT NULL ,
id_teacher INT NOT NULL,
[code_course] INT NOT NULL,
day_week CHAR(1) NOT NULL, -- L M K J V S D
start_hour CHAR(4) NOT NULL, -- En formato militar la hora 6pm --> 1800
end_hour CHAR(4) NOT NULL,
modality CHAR(5) NOT NULL,
class_room_id CHAR(4),
modified_at DATETIME NOT NULL,
create_at DATETIME NOT NULL
-- falta dia de semna y hora
);
ALTER TABLE periods_courses
ADD CONSTRAINT PK_periods_courses
PRIMARY KEY (id_periods,id_teacher,[code_course], day_week, start_hour),
--
CONSTRAINT FK01_periods_courses__periods
FOREIGN KEY (id_periods)
REFERENCES [periods](id),
CONSTRAINT FK02_periods_courses__users
FOREIGN KEY (id_teacher)
REFERENCES users(id),
--
CONSTRAINT FK03_periods_courses__cat_courses
FOREIGN KEY ([code_course])
REFERENCES cat_courses_x_career(id),
--
CONSTRAINT FK04_periods_courses__cat_modality
FOREIGN KEY (modality)
REFERENCES cat_modality(id);
/*
Tabla de las suscripciones que se llevarón acabo en los
cursos correspondientes al periodo de estudio.
En esta tabla se almacenarán datos relevantes tales como el estudiante
relacionado este con la nota que obtuvo y el status(matriculado,aprobado,etc)
*/
CREATE TABLE periods_courses_suscriptions(
id INT NOT NULL UNIQUE, -- SACAR PARA LAS NOTAS
id_periods_courses INT NOT NULL,
student_id INT NOT NULL,
[status] CHAR(5) NOT NULL,
score decimal(18,5) NOT NULL DEFAULT 0,
create_at DATETIME NOT NULL,
modified_at DATETIME NOT NULL
);
ALTER TABLE periods_courses_suscriptions
ADD CONSTRAINT PK_periods_courses_suscriptions
PRIMARY KEY(id_periods_courses,student_id),
--
CONSTRAINT FK01_periods_courses_suscriptions__users
FOREIGN KEY (student_id)
REFERENCES users(id),
--
CONSTRAINT FK02_periods_courses_suscriptions__periods_courses
FOREIGN KEY (id_periods_courses)
REFERENCES periods_courses(id),
--
CONSTRAINT FK03_periods_courses_suscriptions__cat_suscriptions_status
FOREIGN KEY ([status])
REFERENCES cat_suscriptions_status(id)
;
CREATE TABLE study_plan_suscriptions(
id_study_plan INT NOT NULL,
id_student INT NOT NULL,
[status] INT not null, -- 0 inactiva, 1 activa, 2 completada
create_at DATETIME NOT NULL
);
ALTER TABLE study_plan_suscriptions
ADD CONSTRAINT PK_study_plan_suscriptions
PRIMARY KEY (id_study_plan, id_student),
CONSTRAINT FK01_study_plan_suscriptions__S
FOREIGN KEY (id_study_plan)
REFERENCES study_plan(id),
CONSTRAINT FK02_study_plan_suscriptions__users
FOREIGN KEY (id_student)
REFERENCES users(id);
CREATE TABLE performance_evaluation(
id INT NOT NULL,
id_user INT NOT NULL,
[start_date] DATETIME ,
[end_date] DATETIME ,
create_at DATETIME NOT NULL,
modified_at DATETIME NOT NULL,
reason VARCHAR(2000)
);
ALTER TABLE performance_evaluation
ADD CONSTRAINT PK_performance_evaluation
PRIMARY KEY(id),
CONSTRAINT FK01_performance_evaluation__users
FOREIGN KEY (id_user)
REFERENCES users(id);
CREATE TABLE performance_evaluation_details(
id_performance_evaluation INT NOT NULL,
[sequence] INT NOT NULL,
id_user INT NOT NULL,
comment VARCHAR(2000),
score decimal(18,5) NOT NULL DEFAULT 0
);
ALTER TABLE performance_evaluation_details
ADD CONSTRAINT PK_performance_evaluation_details
PRIMARY KEY(id_performance_evaluation, [sequence]),
CONSTRAINT FK01_performance_evaluation__details
FOREIGN KEY (id_performance_evaluation)
REFERENCES performance_evaluation(id),
CONSTRAINT FK02_performance_evaluation__users
FOREIGN KEY (id_user)
REFERENCES users(id);
/*
Hacer tabla donde se almacene una historia de cada entidad
*/
/*
Recupera el id del profesor por el id del Periodo-Curso
*/
CREATE OR ALTER FUNCTION F_GetIdTeacherByPeriodsCourses (@LnPeriodsCourses INT)
RETURNS INT
AS
BEGIN
DECLARE @Ln_id_teacher INT;
SELECT @Ln_id_teacher = id_teacher
From periods_courses
Where id = @LnPeriodsCourses;
RETURN @Ln_id_teacher;
END;
/*
Recupera el codigo del curso por el id del Periodo-Curso
*/
CREATE OR ALTER FUNCTION F_GetCodeCourseByPeriodsCourses (@LnPeriodsCourses INT)
RETURNS INT
AS
BEGIN
DECLARE @Ln_code_course INT;
SELECT @Ln_code_course = [code_course]
From periods_courses
Where id = @LnPeriodsCourses;
RETURN @Ln_code_course;
END;
CREATE OR ALTER FUNCTION F_UserHasRole( @Ln_id_user INT, @Lv_id_role CHAR(5) )
RETURNS INT
AS
BEGIN
DECLARE @Ln_has_role INT = 0;
Select @Ln_has_role = 1
From roles_user
Where id_user = @Ln_id_user
And id_role = @Lv_id_role;
Return @Ln_has_role;
END;
CREATE VIEW V_RandomTeacher AS
SELECT TOP 1 id
FROM users
WHERE [dbo].F_UserHasRole(ID, 'TEACHE') = 1
ORDER BY NEWID();
Select id from V_RandomTeacher;
CREATE VIEW V_RandomModality AS
SELECT TOP 1 id
FROM cat_modality
ORDER BY NEWID();
Select id from V_RandomModality;
CREATE VIEW V_days AS
Select 'L' as ID
Union All
Select 'M'
Union All
Select 'K'
Union All
Select 'J'
Union All
Select 'V'
Union All
Select 'S';
CREATE VIEW V_RandomDay AS
SELECT TOP 1 id
FROM V_days
ORDER BY NEWID();
Select id from V_RandomDay;
CREATE VIEW V_RandomSuscriptionStatus AS
SELECT TOP 1 id
FROM cat_suscriptions_status
ORDER BY NEWID();
SELECT id from V_RandomSuscriptionStatus;
-- recupera el numero id del periodo apartir del id de la suscripcion
CREATE OR ALTER FUNCTION F_GetIdPeriodOfSuscription( @Ln_id_suscription INT)
RETURNS INT
AS
BEGIN
DECLARE @Ln_id_periods INT;
Select @Ln_id_periods = P.ID
From periods_courses_suscriptions PCS
Left Join periods_courses PC
On PC.ID = PCS.id_periods_courses
Left Join [periods] P
On P.id = PC.id_periods
Where PCS.id = @Ln_id_suscription
Group By P.ID;
return @Ln_id_periods;
END;
SELECT [dbo].F_GetIdPeriodOfSuscription(2584);
-- Apartir del id del periodo y el id del estudiante se retorna el total de cursos registrados
CREATE OR ALTER FUNCTION F_TotalSuscriptionByPeriod(@ln_id_period INT, @Ln_id_student INT)
RETURNS INT
AS
BEGIN
DECLARE @Ln_total_suscriptions INT;
Select @Ln_total_suscriptions = ISNULL(count(1),0)
From [periods] P
Right Join periods_courses PC
Right Join periods_courses_suscriptions PCS
On PCS.id_periods_courses = PC.id
And PCS.student_id = @Ln_id_student
On PC.id_periods = P.id
Where P.id = @ln_id_period;
RETURN @Ln_total_suscriptions;
END;
SELECT [dbo].F_TotalSuscriptionByPeriod(1, 13)
CREATE OR ALTER FUNCTION F_TotalSuscriptionByPeriodTeacher(@ln_id_period INT, @Ln_id_teacher INT)
RETURNS INT
AS
BEGIN
DECLARE @Ln_total_of_suscrip_teacher INT;
Select @Ln_total_of_suscrip_teacher = ISNULL(count(1),0)
From periods_courses
Where id_periods = @ln_id_period
And id_teacher = @Ln_id_teacher
Group by id_periods, id_teacher;
RETURN ISNULL(@Ln_total_of_suscrip_teacher,0);
END;
SELECT [dbo].F_TotalSuscriptionByPeriodTeacher(4, 2)
CREATE OR ALTER FUNCTION F_GetNameCourse( @Lv_course_code CHAR(10))
RETURNS VARCHAR(100)
AS
BEGIN
DECLARE @Lv_name_course VARCHAR(100);
Select @Lv_name_course = [name]
From [dbo].[cat_courses]
Where [code] = @Lv_course_code;
RETURN @Lv_name_course;
END;
Select dbo.F_GetNameCourse('C005');
CREATE OR ALTER FUNCTION F_GetNameCourseFromCourseXCareer( @Ln_id_courses_x_career int)
RETURNS VARCHAR(100)
AS
BEGIN
DECLARE @Lv_name_course VARCHAR(100);
Select @Lv_name_course = dbo.F_GetNameCourse(code_course)
From cat_courses_x_career
Where id = @Ln_id_courses_x_career;
RETURN @Lv_name_course;
END;
Select dbo.F_GetNameCourseFromCourseXCareer(4);
CREATE OR ALTER FUNCTION F_GetCompleteNameUser( @Ln_id_user int)
RETURNS VARCHAR(400)
AS
BEGIN
DECLARE @Lv_complete_name VARCHAR(400);
Select @Lv_complete_name = Concat([name], ' ', last_name)
From users
Where id = @Ln_id_user;
RETURN @Lv_complete_name;
END;
Select dbo.F_GetCompleteNameUser(1);