-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_schemas.sql
140 lines (127 loc) · 4.16 KB
/
create_schemas.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
DROP DATABASE IF EXISTS nomad_list;
CREATE DATABASE IF NOT EXISTS nomad_list;
USE nomad_list;
CREATE TABLE IF NOT EXISTS continents (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) UNIQUE,
created_on DATETIME NOT NULL DEFAULT NOW(),
updated_on DATETIME DEFAULT NULL ON UPDATE NOW()
);
CREATE TABLE IF NOT EXISTS currencies (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) UNIQUE,
code VARCHAR(10) UNIQUE,
created_on DATETIME NOT NULL DEFAULT NOW(),
updated_on DATETIME DEFAULT NULL ON UPDATE NOW()
);
CREATE TABLE IF NOT EXISTS countries (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) UNIQUE,
id_continent INT,
iso2 CHAR(2) UNIQUE,
iso3 CHAR(3) UNIQUE,
iso_numeric SMALLINT UNIQUE,
population INT,
id_currency INT,
fips_code VARCHAR(10),
phone_prefix VARCHAR(100),
created_on DATETIME NOT NULL DEFAULT NOW(),
updated_on DATETIME DEFAULT NULL ON UPDATE NOW(),
FOREIGN KEY(id_continent) REFERENCES continents(id),
FOREIGN KEY (id_currency) REFERENCES currencies(id)
);
CREATE TABLE IF NOT EXISTS cities (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) UNIQUE,
city_rank INT,
id_country INT,
iata_code CHAR(3) UNIQUE,
latitude DOUBLE,
longitude DOUBLE,
timezone VARCHAR(100),
gmt SMALLINT,
geoname_id INT,
created_on DATETIME NOT NULL DEFAULT NOW(),
updated_on DATETIME DEFAULT NULL ON UPDATE NOW(),
FOREIGN KEY (id_country) REFERENCES countries(id)
);
CREATE TABLE IF NOT EXISTS cities_relationships (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
id_city INT,
id_related_city INT,
type tinyint(2) CHECK (type in (0, 1, 2)),
created_on DATETIME NOT NULL DEFAULT NOW(),
updated_on DATETIME DEFAULT NULL ON UPDATE NOW(),
FOREIGN KEY (id_related_city) REFERENCES cities(id),
FOREIGN KEY (id_city) REFERENCES cities(id),
UNIQUE (id_city, id_related_city, type)
);
CREATE TABLE IF NOT EXISTS tabs (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) UNIQUE,
created_on DATETIME NOT NULL DEFAULT NOW(),
updated_on DATETIME DEFAULT NULL ON UPDATE NOW()
);
CREATE TABLE IF NOT EXISTS attributes (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
id_tab INT,
created_on DATETIME NOT NULL DEFAULT NOW(),
updated_on DATETIME DEFAULT NULL ON UPDATE NOW(),
FOREIGN KEY (id_tab) REFERENCES tabs(id),
UNIQUE (name, id_tab)
);
CREATE TABLE IF NOT EXISTS city_attributes (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
id_city INT,
id_attribute INT,
attribute_value DOUBLE,
description VARCHAR(255),
url VARCHAR(255),
created_on DATETIME NOT NULL DEFAULT NOW(),
updated_on DATETIME DEFAULT NULL ON UPDATE NOW(),
FOREIGN KEY (id_city) REFERENCES cities(id),
FOREIGN KEY (id_attribute) REFERENCES attributes(id),
UNIQUE (id_city, id_attribute)
);
CREATE TABLE IF NOT EXISTS pros_and_cons (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
description VARCHAR(250),
type CHAR CHECK (type in ('p', 'c')),
id_city INT,
created_on DATETIME NOT NULL DEFAULT NOW(),
updated_on DATETIME DEFAULT NULL ON UPDATE NOW(),
FOREIGN KEY (id_city) REFERENCES cities(id),
UNIQUE (id_city, type, description)
);
CREATE TABLE IF NOT EXISTS monthly_weathers_attributes (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
id_city INT,
id_attribute INT,
month_number INT,
attribute_value VARCHAR(255),
description VARCHAR(255),
created_on DATETIME NOT NULL DEFAULT NOW(),
updated_on DATETIME DEFAULT NULL ON UPDATE NOW(),
FOREIGN KEY (id_city) REFERENCES cities(id),
FOREIGN KEY (id_attribute) REFERENCES attributes(id),
UNIQUE (id_city, id_attribute, month_number)
);
CREATE TABLE IF NOT EXISTS reviews (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
description TEXT,
published_date DATE,
id_city INT,
created_on DATETIME NOT NULL DEFAULT NOW(),
updated_on DATETIME DEFAULT NULL ON UPDATE NOW(),
FOREIGN KEY (id_city) REFERENCES cities(id)
);
CREATE TABLE IF NOT EXISTS photos (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
src VARCHAR(255),
id_city INT,
created_on DATETIME NOT NULL DEFAULT NOW(),
updated_on DATETIME DEFAULT NULL ON UPDATE NOW(),
FOREIGN KEY (id_city) REFERENCES cities(id),
UNIQUE (id_city, src)
);