-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sql
215 lines (185 loc) · 5.66 KB
/
script.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
create table people
(
first_name varchar(20) not null,
surname varchar(20) not null,
id_card_number char(18) not null
constraint people_pkey
primary key,
phone_number varchar(15) not null,
gender char,
student_or_not char
);
alter table people
owner to tao;
create table stations
(
id integer not null
constraint stations_pkey
primary key,
station_name varchar(30) not null,
province_name varchar(30) not null
);
alter table stations
owner to tao;
create table route_detail
(
train_id integer not null,
station_number integer not null,
station_id integer
constraint route_detail_stations_id_fk
references stations,
arrive_time time not null,
depart_time time not null,
id serial not null
constraint route_detail_pk
primary key,
constraint route_detail_pkey
unique (train_id, station_number)
);
alter table route_detail
owner to tao;
create unique index stations_station_name_uindex
on stations (station_name);
create table train_type_seat_type
(
train_type varchar not null,
seat_type integer not null,
seat_type_name varchar not null,
constraint table_name_pk
primary key (train_type, seat_type)
);
alter table train_type_seat_type
owner to tao;
create table ticket_detail
(
id serial not null
constraint ticket_detail_pk
primary key,
start_station_id integer not null
constraint ticket_price_stations_id_fk
references stations,
end_station_id integer not null
constraint ticket_price_stations_id_fk_2
references stations,
train_type varchar not null,
price double precision not null,
seat_type integer not null,
constraint ticket_detail_train_type_seat_type_train_type_seat_type_fk
foreign key (train_type, seat_type) references train_type_seat_type
);
alter table ticket_detail
owner to tao;
create unique index ticket_detail_start_station_id_end_station_id_train_type_seat_t
on ticket_detail (start_station_id, end_station_id, train_type, seat_type);
create table trains
(
train_id serial not null
constraint trains_pk
primary key,
train_code varchar(20) not null,
train_type varchar(20) not null,
start_station_id integer not null
constraint trains_stations_id_fk
references stations,
end_station_id integer
constraint trains_stations_id_fk_2
references stations,
status integer,
constraint train_code_train_type
unique (train_code, train_type)
);
alter table trains
owner to tao;
create table tickets
(
ticket_id serial not null
constraint tickets_pkey
primary key,
train_id integer not null
constraint tickets_trains_train_id_fk
references trains,
id_card_number char(18) not null
constraint fk3
references people,
ticket_entrance varchar(20),
ticket_date date not null,
ticket_type char not null,
ticket_detail_id integer not null
constraint tickets_ticket_detail_id_fk
references ticket_detail
);
alter table tickets
owner to tao;
create unique index tickets_train_id_id_card_number_ticket_date_ticket_detail_id_ui
on tickets (train_id, id_card_number, ticket_date, ticket_detail_id);
create table trains_stations_seats
(
id serial not null
constraint trains_stations_seats_pk
primary key,
seat_type integer not null,
route_detail_id integer not null
constraint trains_stations_seats_route_detail_id_fk
references route_detail
);
alter table trains_stations_seats
owner to tao;
create table rest_tickets
(
trains_stations_seats_id integer not null
constraint rest_tickets_trains_stations_seats_id_fk
references trains_stations_seats,
date date not null,
ticket_num integer default 5,
constraint rest_tickets_pk
primary key (trains_stations_seats_id, date)
);
alter table rest_tickets
owner to tao;
create table users
(
user_name varchar(30) not null
constraint pk
primary key,
password varchar(30) not null,
phone_number varchar(15) not null,
id_card_number char(18) not null
constraint fk
references people,
credit integer,
constraint unique_
unique (user_name, id_card_number)
);
alter table users
owner to tao;
create table orders
(
order_id serial not null
constraint orders_pkey
primary key,
user_name varchar(30) not null
constraint orders_user_fk
references users,
create_time timestamp not null,
order_status integer not null
);
alter table orders
owner to tao;
create table orders_tickets
(
id serial not null
constraint orders_tickets_pk
primary key,
order_id integer not null
constraint orders_tickets_orders_order_id_fk
references orders,
ticket_id integer
constraint orders_tickets_tickets_ticket_id_fk
references tickets
);
alter table orders_tickets
owner to tao;
create unique index orders_tickets_order_id_ticket_id_uindex
on orders_tickets (order_id, ticket_id);
create unique index users_id_card_number_uindex
on users (id_card_number);