Repository navigation
Expand file tree
/
Copy pathdatabase.sql
More file actions
executable file
·169 lines (146 loc) · 3.98 KB
/
Copy pathdatabase.sql
File metadata and controls
executable file
·169 lines (146 loc) · 3.98 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
CREATE DATABASE buymatch;
USE buymatch;
-- USERS TABLE
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(150) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
role ENUM('buyer','organizer','admin') DEFAULT 'buyer',
status ENUM('active','disabled') DEFAULT 'active',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- MATCHES TABLE
CREATE TABLE matches (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(150) NOT NULL,
description TEXT,
match_date DATETIME NOT NULL,
location VARCHAR(150),
organizer_id INT NOT NULL,
status ENUM('pending','approved','rejected') DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_match_organizer
FOREIGN KEY (organizer_id)
REFERENCES users(id)
ON DELETE CASCADE
);
-- CATEGORIES TABLE
CREATE TABLE categories (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price DECIMAL(8,2) NOT NULL,
match_id INT NOT NULL,
CONSTRAINT fk_category_match
FOREIGN KEY (match_id)
REFERENCES matches(id)
ON DELETE CASCADE
);
-- TICKETS TABLE
CREATE TABLE tickets (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
match_id INT NOT NULL,
category_id INT NOT NULL,
seat_number VARCHAR(20),
purchase_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_ticket_user
FOREIGN KEY (user_id)
REFERENCES users(id)
ON DELETE CASCADE,
CONSTRAINT fk_ticket_match
FOREIGN KEY (match_id)
REFERENCES matches(id)
ON DELETE CASCADE,
CONSTRAINT fk_ticket_category
FOREIGN KEY (category_id)
REFERENCES categories(id)
ON DELETE CASCADE
);
-- COMMENTS TABLE
CREATE TABLE comments (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
match_id INT NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_comment_user
FOREIGN KEY (user_id)
REFERENCES users(id)
ON DELETE CASCADE,
CONSTRAINT fk_comment_match
FOREIGN KEY (match_id)
REFERENCES matches(id)
ON DELETE CASCADE
);
-- SQL VIEW Tickets per match
CREATE VIEW tickets_per_match AS
SELECT
m.id AS match_id,
m.title AS match_title,
COUNT(t.id) AS total_tickets
FROM matches m
LEFT JOIN tickets t ON m.id = t.match_id
GROUP BY m.id;
-- STORED PROCEDURE Total sales per match
DELIMITER //
CREATE PROCEDURE total_sales(IN matchId INT)
BEGIN
SELECT
m.title,
SUM(c.price) AS total_sales
FROM tickets t
JOIN categories c ON t.category_id = c.id
JOIN matches m ON t.match_id = m.id
WHERE t.match_id = matchId;
END //
DELIMITER ;
-- fake data to test with
INSERT INTO matches (title, description, match_date, location, organizer_id, status)
VALUES
(
'Real Madrid vs FC Barcelona',
'El Clasico – Spanish league showdown',
'2026-02-10 20:00:00',
'Santiago Bernabéu Stadium',
2,
'pending'
),
(
'Manchester City vs Arsenal',
'Premier League top clash',
'2026-02-15 18:30:00',
'Etihad Stadium',
2,
'approved'
),
(
'LA Lakers vs Golden State Warriors',
'NBA regular season game',
'2026-03-01 21:00:00',
'Crypto.com Arena',
2,
'approved'
);
INSERT INTO categories (name, price, match_id)
VALUES
-- Match 1
('VIP', 80.00, 1),
('Premium', 50.00, 1),
('Standard', 30.00, 1),
-- Match 2
('VIP', 75.00, 2),
('Standard', 35.00, 2),
-- Match 3
('VIP', 90.00, 3),
('Standard', 40.00, 3);
INSERT INTO tickets (user_id, match_id, category_id, seat_number)
VALUES
(1, 1, 3, 'A12'),
(1, 2, 5, 'B07'),
(1, 3, 6, 'VIP-01');
INSERT INTO comments (user_id, match_id, content)
VALUES
(1, 1, 'Amazing atmosphere and great match!'),
(1, 2, 'Very smooth booking experience.'),
(1, 3, 'Totally worth the price!');