-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
111 lines (101 loc) · 3.02 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
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username varchar(12) NOT NULL,
first_name varchar(30) NOT NULL,
last_name varchar(30) NOT NULL,
chinese_name varchar(5),
email varchar(50),
hashed_password varchar(60),
created_at timestamp DEFAULT now() NOT NULL,
updated_at timestamp DEFAULT now()
);
CREATE TABLE countries (
id SERIAL PRIMARY KEY,
name_english varchar(30) NOT NULL,
abbreviation varchar(5),
name_chinese varchar(12),
flag_url varchar(120),
created_at timestamp DEFAULT now() NOT NULL,
updated_at timestamp DEFAULT now()
);
CREATE TABLE cities (
id SERIAL PRIMARY KEY,
name_english varchar(30) NOT NULL,
name_chinese varchar(12),
country_id integer REFERENCES countries NOT NULL,
created_at timestamp DEFAULT now() NOT NULL,
updated_at timestamp DEFAULT now()
);
CREATE TABLE developments (
id SERIAL PRIMARY KEY,
name_english varchar(30) NOT NULL,
name_chinese varchar(12),
address varchar(100),
city_id integer REFERENCES cities NOT NULL,
created_at timestamp DEFAULT now() NOT NULL,
updated_at timestamp DEFAULT now()
);
CREATE TABLE properties (
id SERIAL PRIMARY KEY,
bldg varchar(4),
unit varchar(4),
house_num varchar(8) NOT NULL,
num_bedrooms smallint NOT NULL,
dev_id integer REFERENCES developments NOT NULL,
remarks text,
created_at timestamp DEFAULT now() NOT NULL,
updated_at timestamp DEFAULT now()
);
CREATE TABLE guests (
id SERIAL PRIMARY KEY,
country_id integer REFERENCES countries NOT NULL,
prefix varchar(6),
first_name varchar(30) NOT NULL,
middle_name varchar(30),
last_name varchar(30) NOT NULL,
suffix varchar(10),
chinese_name varchar(5),
email1 varchar(50),
email2 varchar(50),
phone1 varchar(20),
phone2 varchar(20),
remarks text,
created_at timestamp DEFAULT now() NOT NULL,
updated_at timestamp DEFAULT now()
);
CREATE TYPE rate_type AS ENUM ('daily', 'weekly', 'monthly');
CREATE TABLE bookings (
id SERIAL PRIMARY KEY,
guest_id integer REFERENCES guests NOT NULL,
date_in date NOT NULL,
date_out date NOT NULL,
may_extend boolean DEFAULT false NOT NULL,
rate decimal NOT NULL,
rate_type rate_type DEFAULT 'daily' NOT NULL,
on_deposit decimal DEFAULT 0.0 NOT NULL,
paid_amt decimal DEFAULT 0.0 NOT NULL,
total_amt decimal NOT NULL,
remarks text,
created_at timestamp DEFAULT now() NOT NULL,
updated_at timestamp DEFAULT now()
);
CREATE TABLE stays (
id SERIAL PRIMARY KEY,
booking_id integer NOT NULL REFERENCES bookings ON DELETE CASCADE,
property_id integer REFERENCES properties NOT NULL,
date_in date NOT NULL,
date_out date NOT NULL,
remarks text,
created_at timestamp DEFAULT now() NOT NULL,
updated_at timestamp DEFAULT now()
);
CREATE TYPE payment_type AS ENUM ('cash', 'bank transfer', 'credit card');
CREATE TABLE payments (
id SERIAL PRIMARY KEY,
booking_id integer NOT NULL REFERENCES bookings ON DELETE CASCADE,
amount decimal NOT NULL,
remarks text,
type payment_type NOT NULL,
created_at timestamp DEFAULT now() NOT NULL,
updated_at timestamp DEFAULT now()
);