-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
103 lines (92 loc) · 3.58 KB
/
Copy pathschema.sql
File metadata and controls
103 lines (92 loc) · 3.58 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
-- Create and Select the Database
CREATE DATABASE IF NOT EXISTS course_db;
USE course_db;
-- ==========================================
-- SECTION A: CORE TABLES (Used by the App)
-- ==========================================
-- Users Table
-- Stores Admins, Instructors, Students, and Analysts
CREATE TABLE IF NOT EXISTS Users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL, -- Stores Hashed Passwords
role ENUM('admin', 'instructor', 'student', 'analyst') NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Departments Table (From ER Diagram)
-- Currently unused by app, but required for schema completeness
CREATE TABLE IF NOT EXISTS Departments (
dept_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
head_of_dept VARCHAR(100)
);
-- Courses Table
-- Links to an Instructor and optionally a Department
CREATE TABLE IF NOT EXISTS Courses (
course_id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(150) NOT NULL,
description TEXT,
credits INT DEFAULT 3,
instructor_id INT,
dept_id INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (instructor_id) REFERENCES Users(user_id) ON DELETE SET NULL,
FOREIGN KEY (dept_id) REFERENCES Departments(dept_id) ON DELETE SET NULL
);
-- Enrollments Table
-- Links Students to Courses and stores their Grade
CREATE TABLE IF NOT EXISTS Enrollments (
enrollment_id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT NOT NULL,
course_id INT NOT NULL,
grade VARCHAR(5) DEFAULT 'NA', -- Stores grades like 'A', 'B', 'C', 'NA'
enrollment_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
status VARCHAR(20) DEFAULT 'Active',
FOREIGN KEY (student_id) REFERENCES Users(user_id) ON DELETE CASCADE,
FOREIGN KEY (course_id) REFERENCES Courses(course_id) ON DELETE CASCADE,
UNIQUE(student_id, course_id)
);
-- ========================
-- SECTION B: FUTURE SCOPE
-- ========================
-- 6. Assignments Table
-- Future feature: Professors can create specific tasks
CREATE TABLE IF NOT EXISTS Assignments (
assignment_id INT AUTO_INCREMENT PRIMARY KEY,
course_id INT NOT NULL,
title VARCHAR(100) NOT NULL,
max_score INT DEFAULT 100,
deadline DATETIME,
FOREIGN KEY (course_id) REFERENCES Courses(course_id) ON DELETE CASCADE
);
-- 7. Submissions Table
-- Future feature: Students submit work for assignments
CREATE TABLE IF NOT EXISTS Submissions (
submission_id INT AUTO_INCREMENT PRIMARY KEY,
assignment_id INT NOT NULL,
student_id INT NOT NULL,
submission_text TEXT,
marks_obtained INT DEFAULT 0,
submitted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (assignment_id) REFERENCES Assignments(assignment_id) ON DELETE CASCADE,
FOREIGN KEY (student_id) REFERENCES Users(user_id) ON DELETE CASCADE
);
-- 8. Course Prerequisites Table (Self-Referencing)
-- Future feature: Prevent enrollment if prereq not met
CREATE TABLE IF NOT EXISTS CoursePrerequisites (
course_id INT NOT NULL,
prereq_id INT NOT NULL,
PRIMARY KEY (course_id, prereq_id),
FOREIGN KEY (course_id) REFERENCES Courses(course_id) ON DELETE CASCADE,
FOREIGN KEY (prereq_id) REFERENCES Courses(course_id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS AuditLogs (
log_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
action VARCHAR(255),
details VARCHAR(255),
ip_address VARCHAR(50),
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES Users(user_id) ON DELETE SET NULL
);