-
Notifications
You must be signed in to change notification settings - Fork 176
/
CreateTables_vn_units.sql
97 lines (76 loc) · 3.12 KB
/
CreateTables_vn_units.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
-- DROP TABLE IF EXISTS wards;
-- DROP TABLE IF EXISTS districts;
-- DROP TABLE IF EXISTS provinces;
-- DROP TABLE IF EXISTS administrative_units;
-- DROP TABLE IF EXISTS administrative_regions;
-- CREATE administrative_regions TABLE
CREATE TABLE administrative_regions (
id integer NOT NULL,
name varchar(255) NOT NULL,
name_en varchar(255) NOT NULL,
code_name varchar(255) NULL,
code_name_en varchar(255) NULL,
CONSTRAINT administrative_regions_pkey PRIMARY KEY (id)
);
-- CREATE administrative_units TABLE
CREATE TABLE administrative_units (
id integer NOT NULL,
full_name varchar(255) NULL,
full_name_en varchar(255) NULL,
short_name varchar(255) NULL,
short_name_en varchar(255) NULL,
code_name varchar(255) NULL,
code_name_en varchar(255) NULL,
CONSTRAINT administrative_units_pkey PRIMARY KEY (id)
);
-- CREATE provinces TABLE
CREATE TABLE provinces (
code varchar(20) NOT NULL,
name varchar(255) NOT NULL,
name_en varchar(255) NULL,
full_name varchar(255) NOT NULL,
full_name_en varchar(255) NULL,
code_name varchar(255) NULL,
administrative_unit_id integer NULL,
administrative_region_id integer NULL,
CONSTRAINT provinces_pkey PRIMARY KEY (code)
);
-- provinces foreign keys
ALTER TABLE provinces ADD CONSTRAINT provinces_administrative_region_id_fkey FOREIGN KEY (administrative_region_id) REFERENCES administrative_regions(id);
ALTER TABLE provinces ADD CONSTRAINT provinces_administrative_unit_id_fkey FOREIGN KEY (administrative_unit_id) REFERENCES administrative_units(id);
CREATE INDEX idx_provinces_region ON provinces(administrative_region_id);
CREATE INDEX idx_provinces_unit ON provinces(administrative_unit_id);
-- CREATE districts TABLE
CREATE TABLE districts (
code varchar(20) NOT NULL,
name varchar(255) NOT NULL,
name_en varchar(255) NULL,
full_name varchar(255) NULL,
full_name_en varchar(255) NULL,
code_name varchar(255) NULL,
province_code varchar(20) NULL,
administrative_unit_id integer NULL,
CONSTRAINT districts_pkey PRIMARY KEY (code)
);
-- districts foreign keys
ALTER TABLE districts ADD CONSTRAINT districts_administrative_unit_id_fkey FOREIGN KEY (administrative_unit_id) REFERENCES administrative_units(id);
ALTER TABLE districts ADD CONSTRAINT districts_province_code_fkey FOREIGN KEY (province_code) REFERENCES provinces(code);
CREATE INDEX idx_districts_province ON districts(province_code);
CREATE INDEX idx_districts_unit ON districts(administrative_unit_id);
-- CREATE wards TABLE
CREATE TABLE wards (
code varchar(20) NOT NULL,
name varchar(255) NOT NULL,
name_en varchar(255) NULL,
full_name varchar(255) NULL,
full_name_en varchar(255) NULL,
code_name varchar(255) NULL,
district_code varchar(20) NULL,
administrative_unit_id integer NULL,
CONSTRAINT wards_pkey PRIMARY KEY (code)
);
-- wards foreign keys
ALTER TABLE wards ADD CONSTRAINT wards_administrative_unit_id_fkey FOREIGN KEY (administrative_unit_id) REFERENCES administrative_units(id);
ALTER TABLE wards ADD CONSTRAINT wards_district_code_fkey FOREIGN KEY (district_code) REFERENCES districts(code);
CREATE INDEX idx_wards_district ON wards(district_code);
CREATE INDEX idx_wards_unit ON wards(administrative_unit_id);