-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtables.sql
53 lines (43 loc) · 1.4 KB
/
tables.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
DROP TABLE IF EXISTS users, bus, trips, bookings CASCADE;
CREATE TABLE users (
id serial primary key,
first_name varchar(128) not null,
last_name varchar(128) not null,
email varchar(200) unique,
password varchar(80) not null,
is_admin boolean not null default false,
created_at timestamptz not null default CURRENT_TIMESTAMP,
updated_at timestamptz
);
CREATE TABLE bus (
id serial primary key,
number_plate varchar(30) not null unique,
manufacturer varchar(30),
model varchar(30) not null,
year varchar(5),
capacity integer not null,
created_at timestamptz not null default CURRENT_TIMESTAMP,
updated_at timestamptz
);
CREATE TABLE trips (
id serial primary key,
bus_id integer REFERENCES bus (id) not null,
user_id integer REFERENCES users (id) not null,
origin varchar(50) not null,
destination varchar(50) not null,
trip_date timestamptz not null,
fare real not null,
seats json[] not null,
status integer default 1,
created_at timestamptz not null default CURRENT_TIMESTAMP,
updated_at timestamptz
);
CREATE TABLE bookings (
id serial primary key,
trip_id integer not null REFERENCES trips (id) on delete cascade,
user_id integer not null REFERENCES users (id) on delete cascade,
seat_number int check (seat_number > 0),
created_at timestamptz not null default CURRENT_TIMESTAMP,
updated_at timestamptz,
UNIQUE (trip_id, user_id, seat_number)
);