-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInitialDatabase.sql
More file actions
76 lines (68 loc) · 2.05 KB
/
Copy pathInitialDatabase.sql
File metadata and controls
76 lines (68 loc) · 2.05 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
-- Create database
CREATE DATABASE TypeRush;
USE TypeRush;
-- Players table
CREATE TABLE Players (
player_id INT AUTO_INCREMENT PRIMARY KEY,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Enemies table
CREATE TABLE Enemies (
enemy_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
base_hp INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Words table
CREATE TABLE Words (
word_id INT AUTO_INCREMENT PRIMARY KEY,
value VARCHAR(50) NOT NULL UNIQUE,
difficulty_level INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Game sessions table
CREATE TABLE GameSessions (
session_id INT AUTO_INCREMENT PRIMARY KEY,
player_id INT,
start_time TIMESTAMP,
end_time TIMESTAMP,
final_score INT,
FOREIGN KEY (player_id) REFERENCES Players(player_id)
);
-- Score history table
CREATE TABLE Scores (
score_id INT AUTO_INCREMENT PRIMARY KEY,
session_id INT,
enemy_id INT,
score_value INT NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (session_id) REFERENCES GameSessions(session_id),
FOREIGN KEY (enemy_id) REFERENCES Enemies(enemy_id)
);
-- Enemy encounters during game sessions
CREATE TABLE SessionEnemies (
session_enemy_id INT AUTO_INCREMENT PRIMARY KEY,
session_id INT,
enemy_id INT,
remaining_hp INT NOT NULL,
FOREIGN KEY (session_id) REFERENCES GameSessions(session_id),
FOREIGN KEY (enemy_id) REFERENCES Enemies(enemy_id)
);
-- Words encountered during game sessions
CREATE TABLE SessionWords (
session_word_id INT AUTO_INCREMENT PRIMARY KEY,
session_id INT,
word_id INT,
was_correct BOOLEAN,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (session_id) REFERENCES GameSessions(session_id),
FOREIGN KEY (word_id) REFERENCES Words(word_id)
);
-- Top scores view
CREATE VIEW TopScores AS
SELECT s.score_value, p.player_id, g.start_time
FROM Scores s
JOIN GameSessions g ON s.session_id = g.session_id
JOIN Players p ON g.player_id = p.player_id
ORDER BY s.score_value DESC
LIMIT 10;