forked from ShreyashTailor/quiz-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_setup.sql
More file actions
187 lines (167 loc) · 5.48 KB
/
database_setup.sql
File metadata and controls
187 lines (167 loc) · 5.48 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
-- Create leaderboard table for storing quiz results
CREATE TABLE IF NOT EXISTS leaderboard (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
username VARCHAR(100) NOT NULL,
quiz_id INT NOT NULL,
quiz_title VARCHAR(255) NOT NULL,
score INT NOT NULL,
total_questions INT NOT NULL,
percentage DECIMAL(5,2) NOT NULL,
completion_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_user_id (user_id),
INDEX idx_quiz_id (quiz_id),
INDEX idx_percentage (percentage),
INDEX idx_completion_time (completion_time)
);
-- Create users table if it doesn't exist
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100) UNIQUE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
role ENUM('user', 'admin') DEFAULT 'user',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Create quizzes table if it doesn't exist
CREATE TABLE IF NOT EXISTS quizzes (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
created_by INT DEFAULT NULL,
creator_name VARCHAR(100) DEFAULT NULL,
status ENUM('pending', 'approved', 'rejected') DEFAULT 'approved',
admin_notes TEXT DEFAULT NULL,
approved_by INT DEFAULT NULL,
approved_at TIMESTAMP NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_status (status),
INDEX idx_created_by (created_by)
);
-- Create questions table if it doesn't exist
CREATE TABLE IF NOT EXISTS questions (
id INT AUTO_INCREMENT PRIMARY KEY,
quiz_id INT NOT NULL,
question TEXT NOT NULL,
option_a VARCHAR(255) NOT NULL,
option_b VARCHAR(255) NOT NULL,
option_c VARCHAR(255) NOT NULL,
option_d VARCHAR(255) NOT NULL,
correct_option ENUM('A', 'B', 'C', 'D') NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (quiz_id) REFERENCES quizzes(id) ON DELETE CASCADE
);
-- Update existing quizzes table structure
-- Check and add columns one by one to avoid errors
-- Add created_by column
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = 'quizzes'
AND column_name = 'created_by';
SET @sql = IF(@col_exists = 0,
'ALTER TABLE quizzes ADD COLUMN created_by INT DEFAULT NULL',
'SELECT "Column created_by already exists" as message'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Add creator_name column
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = 'quizzes'
AND column_name = 'creator_name';
SET @sql = IF(@col_exists = 0,
'ALTER TABLE quizzes ADD COLUMN creator_name VARCHAR(100) DEFAULT NULL',
'SELECT "Column creator_name already exists" as message'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Add status column
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = 'quizzes'
AND column_name = 'status';
SET @sql = IF(@col_exists = 0,
'ALTER TABLE quizzes ADD COLUMN status ENUM(''pending'', ''approved'', ''rejected'') DEFAULT ''approved''',
'SELECT "Column status already exists" as message'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Add admin_notes column
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = 'quizzes'
AND column_name = 'admin_notes';
SET @sql = IF(@col_exists = 0,
'ALTER TABLE quizzes ADD COLUMN admin_notes TEXT DEFAULT NULL',
'SELECT "Column admin_notes already exists" as message'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Add approved_by column
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = 'quizzes'
AND column_name = 'approved_by';
SET @sql = IF(@col_exists = 0,
'ALTER TABLE quizzes ADD COLUMN approved_by INT DEFAULT NULL',
'SELECT "Column approved_by already exists" as message'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Add approved_at column
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = 'quizzes'
AND column_name = 'approved_at';
SET @sql = IF(@col_exists = 0,
'ALTER TABLE quizzes ADD COLUMN approved_at TIMESTAMP NULL',
'SELECT "Column approved_at already exists" as message'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- Add indexes if they don't exist
SET @index_exists = 0;
SELECT COUNT(*) INTO @index_exists
FROM information_schema.statistics
WHERE table_schema = DATABASE()
AND table_name = 'quizzes'
AND index_name = 'idx_status';
SET @sql = IF(@index_exists = 0,
'ALTER TABLE quizzes ADD INDEX idx_status (status)',
'SELECT "Index idx_status already exists" as message'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @index_exists = 0;
SELECT COUNT(*) INTO @index_exists
FROM information_schema.statistics
WHERE table_schema = DATABASE()
AND table_name = 'quizzes'
AND index_name = 'idx_created_by';
SET @sql = IF(@index_exists = 0,
'ALTER TABLE quizzes ADD INDEX idx_created_by (created_by)',
'SELECT "Index idx_created_by already exists" as message'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;