-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
258 lines (194 loc) · 6.92 KB
/
database.py
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
import pymysql
db = pymysql.connect(host='localhost', user='root', passwd='akif')
cursor = db.cursor()
def create_database():
# Create a new Database 'test_3' if it dosen't already exists.
sql = "CREATE DATABASE IF NOT EXISTS Edunix DEFAULT CHARACTER SET utf8;"
cursor.execute(sql)
sql = "USE Edunix;"
cursor.execute(sql)
# Creating Course_Category Table.
sql = \
'''
CREATE TABLE IF NOT EXISTS Course_Category (
category_id INTEGER NOT NULL AUTO_INCREMENT,
category_name VARCHAR(50) NOT NULL,
PRIMARY KEY (category_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
'''
cursor.execute(sql)
# Creating Course Table
sql = \
'''
CREATE TABLE IF NOT EXISTS Course (
course_id INTEGER NOT NULL AUTO_INCREMENT,
course_name VARCHAR(80) NOT NULL,
credits INTEGER NOT NULL,
duration TIME NOT NULL,
platform VARCHAR(20),
category_id INTEGER NOT NULL,
PRIMARY KEY (course_id),
INDEX (course_name) USING BTREE,
CONSTRAINT FOREIGN KEY (category_id) REFERENCES Course_Category (category_id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
'''
cursor.execute(sql)
# Creating CTeacher Table
sql = \
'''
CREATE TABLE IF NOT EXISTS CTeacher(
teacher_id INTEGER NOT NULL AUTO_INCREMENT,
teacher_name VARCHAR(50) NOT NULL,
phno VARCHAR(15) NOT NULL,
PRIMARY KEY (teacher_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
'''
cursor.execute(sql)
sql = \
'''
CREATE TABLE IF NOT EXISTS Course_Teacher (
teacher_id INTEGER NOT NULL,
course_id INTEGER NOT NULL,
PRIMARY KEY(teacher_id, course_id),
CONSTRAINT FOREIGN KEY (teacher_id) REFERENCES CTeacher (teacher_id)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY (course_id) REFERENCES Course (course_id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
'''
cursor.execute(sql)
sql = \
'''
CREATE TABLE IF NOT EXISTS Branch (
branch_id INTEGER NOT NULL AUTO_INCREMENT,
branch_name VARCHAR(60) NOT NULL,
PRIMARY KEY (branch_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
'''
cursor.execute(sql)
sql = \
'''
CREATE TABLE IF NOT EXISTS Student (
student_id INTEGER NOT NULL AUTO_INCREMENT,
student_name VARCHAR(50) NOT NULL,
university_id VARCHAR(10) NOT NULL,
username VARCHAR(10) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(40) NOT NULL,
phno VARCHAR(15) NOT NULL,
courses_enrolled INTEGER DEFAULT 0,
semester INTEGER NOT NULL,
dob DATE NOT NULL,
branch_id INTEGER NOT NULL,
INDEX (username),
PRIMARY KEY (student_id),
CONSTRAINT FOREIGN KEY (branch_id) REFERENCES Branch (branch_id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
'''
cursor.execute(sql)
sql = \
'''
CREATE TABLE IF NOT EXISTS Enrollment(
student_id INTEGER NOT NULL,
course_id INTEGER NOT NULL,
enroll_date DATE DEFAULT NOW(),
PRIMARY KEY(student_id, course_id),
CONSTRAINT FOREIGN KEY (student_id) REFERENCES Student (student_id)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY (course_id) REFERENCES Course (course_id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
'''
cursor.execute(sql)
sql = \
'''
CREATE TABLE IF NOT EXISTS Department (
dept_id INTEGER NOT NULL AUTO_INCREMENT,
dept_name VARCHAR(80) NOT NULL,
PRIMARY KEY(dept_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
'''
cursor.execute(sql)
sql = \
'''
CREATE TABLE IF NOT EXISTS Faculty (
faculty_id INTEGER NOT NULL AUTO_INCREMENT,
faculty_name VARCHAR(40) NOT NULL,
email VARCHAR(35) NOT NULL,
university_id VARCHAR(15) NOT NULL,
username VARCHAR(10) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
phno VARCHAR(15) NOT NULL,
dept_id INTEGER NOT NULL,
INDEX (faculty_name),
PRIMARY KEY(faculty_id),
CONSTRAINT FOREIGN KEY (dept_id) REFERENCES Department (dept_id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
'''
cursor.execute(sql)
sql = \
'''
CREATE TABLE IF NOT EXISTS Admin (
admin_id INTEGER NOT NULL AUTO_INCREMENT,
admin_name VARCHAR(20) NOT NULL,
username VARCHAR(10) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(35) NOT NULL,
PRIMARY KEY (admin_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
'''
cursor.execute(sql)
# Table for storing password reset tokens
sql = \
'''
CREATE TABLE Password_Reset (
token VARCHAR(32),
email VARCHAR(25),
acc_type INT,
PRIMARY KEY (token, email, acc_type)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;
'''
cursor.execute(sql)
# -- Trigger For When a Student Enrolls in a Course
sql = \
'''
CREATE OR REPLACE TRIGGER add_CourseCount
AFTER INSERT ON Enrollment
FOR EACH ROW
UPDATE Student
SET Student.courses_enrolled = Student.courses_enrolled + 1
WHERE Student.student_id = NEW.student_id;
'''
cursor.execute(sql)
# -- Trigger for when a student unenrolls from a Course
sql = \
'''
CREATE OR REPLACE TRIGGER sub_CourseCount
AFTER DELETE ON Enrollment
FOR EACH ROW
UPDATE Student
SET Student.courses_enrolled = Student.courses_enrolled - 1
WHERE Student.student_id = OLD.student_id;
'''
cursor.execute(sql)
# -- View To Display Age
sql = "CREATE VIEW age_calc AS SELECT student_id, floor(DATEDIFF((now()),dob)/365) AS Age FROM Student;"
cursor.execute(sql)
sql = \
'''
SELECT student_name, username, university_id, email, courses_enrolled, semester, dob, age_calc.Age, Branch.branch_name
FROM Student JOIN Branch JOIN age_calc ON Student.branch_id = Branch.branch_id AND Student.student_id = age_calc.student_id;
'''
# cursor.execute(sql)
script="SHOW TABLES;"
cursor.execute(script)
print(cursor.fetchall())
print('\nDatabase Created Successfully')
if __name__ == '__main__':
try:
create_database()
except:
print('Error in creating the database.')