-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.sql
230 lines (187 loc) · 5.17 KB
/
schema.sql
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
--@block
CREATE DATABASE IF NOT EXISTS quoteslection;
-- @block
CREATE TABLE user (
username VARCHAR(30) PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
password_hash VARCHAR(255) NOT NULL,
is_admin BOOLEAN NOT NULL
);
--@block
CREATE TABLE favourite (
phone_id INT UNSIGNED,
username VARCHAR(30),
FOREIGN KEY (phone_id) REFERENCES phone(id),
FOREIGN KEY (username) REFERENCES user(username)
);
-- @block
CREATE TABLE review (
review_text VARCHAR(255) ,
rating INT UNSIGNED NOT NULL,
submission_date_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
phone_id INT UNSIGNED,
username VARCHAR(30),
FOREIGN KEY (phone_id) REFERENCES phone(id),
FOREIGN KEY (username) REFERENCES user(username)
);
-- @BLOCK
CREATE TABLE brand (
name VARCHAR(30) PRIMARY KEY,
description VARCHAR(1000) NOT NULL,
logo_url VARCHAR(500)
);
--@block
CREATE TABLE marketshare (
brand_name VARCHAR(30),
share_percentage INT UNSIGNED,
year INT UNSIGNED NOT NULL,
quarter INT UNSIGNED NOT NULL,
FOREIGN KEY (brand_name) REFERENCES brand(name)
);
--@block
CREATE TABLE phone (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
brand_name VARCHAR(30),
name VARCHAR(100) NOT NULL,
image_url VARCHAR(500),
price_rupees INT UNSIGNED,
FOREIGN KEY (brand_name) REFERENCES brand(name)
);
--@block
CREATE TABLE specification (
phone_id INT UNSIGNED,
os VARCHAR(30),
weight_grams INT UNSIGNED,
cpu VARCHAR(255),
chipset VARCHAR(255),
display_technology VARCHAR(255),
screen_size_inches INT UNSIGNED,
display_resoluition VARCHAR(10),
extra_display_features VARCHAR(255),
built_in_memory VARCHAR(10),
ram VARCHAR(10),
battery_capacity_mah INT UNSIGNED,
battery_features VARCHAR(255),
FOREIGN KEY (phone_id) REFERENCES phone(id)
);
-- battery features column dropped later
--@block
CREATE TABLE color (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
color_name VARCHAR(30)
);
--@block
CREATE TABLE phone_color (
phone_id INT UNSIGNED,
color_id INT UNSIGNED,
FOREIGN KEY (phone_id) REFERENCES phone(id),
FOREIGN KEY (color_id) REFERENCES color(id)
);
--@block
CREATE TABLE sensor (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
sensor_name VARCHAR(30)
);
--@block
CREATE TABLE phone_sensor(
sensor_id INT UNSIGNED,
phone_id INT UNSIGNED,
FOREIGN KEY (phone_id) REFERENCES phone(id),
FOREIGN KEY (sensor_id) REFERENCES sensor(id)
);
--@block
CREATE TABLE camera (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
mega_pixels INT UNSIGNED NOT NULL,
type VARCHAR(10), -- eg: ultrawide, macro
location VARCHAR(5) NOT NULL -- "front or rear"
)
--@block
CREATE TABLE phone_camera (
phone_id INT UNSIGNED,
camera_id INT UNSIGNED,
FOREIGN KEY (phone_id) REFERENCES phone(id),
FOREIGN KEY (camera_id) REFERENCES camera(id)
);
--@block
CREATE TABLE news (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
heading VARCHAR(255) NOT NULL,
image_url VARCHAR(500),
news_text VARCHAR(1024) NOT NULL
);
--@block
ALTER TABLE specification
DROP COLUMN battery_features;
--@BLOCK
ALTER TABLE specification
DROP COLUMN display_resoluition;
ALTER TABLE specification
ADD COLUMN display_resolution VARCHAR(50)
AFTER screen_size_inches;
--@block
ALTER TABLE specification
MODIFY COLUMN screen_size_inches FLOAT;
--@block;
ALTER TABLE camera RENAME COLUMN mega_pixels TO megapixels;
--- test selects
--@block
SELECT brand_name, name, sensor_name
FROM
phone JOIN phone_sensor ON phone.id = phone_sensor.phone_id
JOIN sensor ON phone_sensor.sensor_id = sensor.id;
--@block
SELECT phone.brand_name, phone.name, camera.megapixels, camera.type
FROM
phone JOIN phone_camera ON phone.id = phone_camera.phone_id
JOIN camera ON phone_camera.camera_id = camera.id
WHERE
location = "rear";
--@block
ALTER TABLE camera
DROP COLUMN type;
--@block
ALTER TABLE phone
DROP COLUMN price_rupees;
ALTER TABLE specification
ADD COLUMN price_rupees INT UNSIGNED;
--@block
ALTER TABLE specification
MODIFY COLUMN built_in_memory INT UNSIGNED;
ALTER TABLE specification
MODIFY COLUMN ram INT UNSIGNED;
ALTER TABLE specification
RENAME COLUMN built_in_memory TO built_in_memory_GB;
ALTER TABLE specification
RENAME COLUMN ram TO ram_GB;
--@block
ALTER TABLE specification
MODIFY COLUMN os VARCHAR(100);
-- @block
-- moving specs to phone table and deleting specification table
ALTER TABLE phone
ADD COLUMN os VARCHAR(100),
ADD COLUMN weight_grams INT UNSIGNED,
ADD COLUMN cpu VARCHAR(255),
ADD COLUMN chipset VARCHAR(255),
ADD COLUMN display_technology VARCHAR(255),
ADD COLUMN screen_size_inches FLOAT,
ADD COLUMN display_resolution VARCHAR(50),
ADD COLUMN extra_display_features VARCHAR(255),
ADD COLUMN built_in_memory_GB INT UNSIGNED,
ADD COLUMN ram_GB INT UNSIGNED,
ADD COLUMN battery_capacity_mah INT UNSIGNED;
DROP TABLE specification;
--@block
ALTER TABLE phone
ADD COLUMN price_rupees INT UNSIGNED;
--@block
ALTER TABLE brand
MODIFY COLUMN description TEXT;
--@block
ALTER TABLE news
ADD COLUMN news_date_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
--@block
ALTER TABLE news
MODIFY COLUMN news_text TEXT;