forked from Russell-A/database-course-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrebas1.sql
269 lines (237 loc) · 9.89 KB
/
crebas1.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*==============================================================*/
/* DBMS name: Microsoft SQL Server 2008 */
/* Created on: 2020/07/02 18:44:21 */
/*==============================================================*/
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('航班') and o.name = 'FK_航班_FK_飞机编号_飞机登记')
alter table 航班
drop constraint FK_航班_FK_飞机编号_飞机登记
go
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('航班') and o.name = 'FK_航班_机场代码_出发_机场')
alter table 航班
drop constraint FK_航班_机场代码_出发_机场
go
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('航班') and o.name = 'FK_航班_机场代码_到达_机场')
alter table 航班
drop constraint FK_航班_机场代码_到达_机场
go
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('航班') and o.name = 'FK_航班_机场代码_经停_机场')
alter table 航班
drop constraint FK_航班_机场代码_经停_机场
go
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('订票') and o.name = 'FK_订票_用户名_用户表')
alter table 订票
drop constraint FK_订票_用户名_用户表
go
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('订票') and o.name = 'FK_订票_航程号_飞行计划安排')
alter table 订票
drop constraint FK_订票_航程号_飞行计划安排
go
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('飞机登记') and o.name = 'FK_飞机登记_机型_飞机表')
alter table 飞机登记
drop constraint FK_飞机登记_机型_飞机表
go
if exists (select 1
from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
where r.fkeyid = object_id('飞行计划安排') and o.name = 'FK_飞行计划安排_航班编号_航程_航班')
alter table 飞行计划安排
drop constraint FK_飞行计划安排_航班编号_航程_航班
go
if exists (select 1
from sysobjects
where id = object_id('机场')
and type = 'U')
drop table 机场
go
if exists (select 1
from sysobjects
where id = object_id('用户表')
and type = 'U')
drop table 用户表
go
if exists (select 1
from sysobjects
where id = object_id('航班')
and type = 'U')
drop table 航班
go
if exists (select 1
from sysobjects
where id = object_id('订票')
and type = 'U')
drop table 订票
go
if exists (select 1
from sysobjects
where id = object_id('飞机登记')
and type = 'U')
drop table 飞机登记
go
if exists (select 1
from sysobjects
where id = object_id('飞机表')
and type = 'U')
drop table 飞机表
go
if exists (select 1
from sysobjects
where id = object_id('飞行计划安排')
and type = 'U')
drop table 飞行计划安排
go
/*==============================================================*/
/* Table: 机场 */
/*==============================================================*/
create table 机场 (
机场代码 varchar(20) not null,
机场名 varchar(50) not null,
所在城市 varchar(20) not null,
constraint PK_机场 primary key (机场代码)
)
go
/*==============================================================*/
/* Table: 用户表 */
/*==============================================================*/
create table 用户表 (
用户名 varchar(20) not null,
用户密码 varchar(20) not null,
用户权限 int not null,
用户联系方式 varchar(20) not null,
constraint PK_用户表 primary key (用户名)
)
go
/*==============================================================*/
/* Table: 航班 */
/*==============================================================*/
create table 航班 (
航班编号 varchar(10) not null,
出发机场代码 varchar(20) not null,
经停机场代码 varchar(20) null,
到达机场代码 varchar(20) not null,
飞机编号 varchar(10) null,
constraint PK_航班 primary key (航班编号),
constraint AK_UQ_飞机编号_航班 unique (飞机编号)
)
go
/*==============================================================*/
/* Table: 订票 */
/*==============================================================*/
create table 订票 (
机票编号 int identity,
航程号 int not null,
出发机场 varchar(20) not null,
目的机场 varchar(20) not null,
座位编号 varchar(6) not null,
舱位 varchar(6) not null,
用户名 varchar(20) not null,
乘客姓名 varchar(50) not null,
乘客性别 varchar(2) not null
constraint CKC_乘客性别_订票 check (乘客性别 in ('男','女')),
乘客类别 varchar(10) not null
constraint CKC_乘客类别_订票 check (乘客类别 in ('老人','小孩','孕妇','成人','残疾','病人','<Val8>')),
"乘客身份证号/护照号" varchar(20) not null,
乘客联系方式 varchar(20) not null,
constraint PK_订票 primary key (机票编号)
)
go
/*==============================================================*/
/* Table: 飞机登记 */
/*==============================================================*/
create table 飞机登记 (
飞机编号 varchar(10) not null,
机型 varchar(50) not null,
注册日期 smalldatetime not null,
使用年限 tinyint not null,
其他信息 text null,
constraint PK_飞机登记 primary key (飞机编号)
)
go
/*==============================================================*/
/* Table: 飞机表 */
/*==============================================================*/
create table 飞机表 (
机型 varchar(50) not null,
商务舱数量 smallint null,
头等舱数量 smallint null,
经济舱数量 smallint null,
载重量 real not null,
最大里程 real not null,
constraint PK_飞机表 primary key (机型)
)
go
/*==============================================================*/
/* Table: 飞行计划安排 */
/*==============================================================*/
create table 飞行计划安排 (
航程号 int identity,
航班编号 varchar(10) not null,
计划出发时间 datetime not null,
计划到达经停机场时间 datetime null,
计划从经停机场出发时间 datetime null,
计划到达时间 datetime not null,
"经济舱(开始-到达)剩余座位" smallint null,
"商务舱(开始-到达)剩余座位" smallint null,
"头等舱(开始-到达)剩余座位" smallint null,
"经济舱(经停-到达)剩余座位" smallint null,
"商务舱(经停-到达)剩余座位" smallint null,
"头等舱(经停-到达)剩余座位" smallint null,
"经济舱(开始-经停)剩余座位" smallint null,
"商务舱(开始-经停)剩余座位" smallint null,
"头等舱(开始-经停)剩余座位" smallint null,
"票价(开始-到达,经济舱)" money null,
"票价(开始-经停,经济舱)" money null,
"票价(经停-到达,经济舱)" money null,
"票价(开始-到达,商务舱)" money null,
"票价(开始-经停,商务舱)" money null,
"票价(经停-到达,商务舱)" money null,
"票价(开始-到达,头等舱)" money null,
"票价(开始-经停,头等舱)" money null,
"票价(经停-到达,头等舱)" money null,
constraint PK_飞行计划安排 primary key (航程号)
)
go
alter table 航班
add constraint FK_航班_FK_飞机编号_飞机登记 foreign key (飞机编号)
references 飞机登记 (飞机编号)
go
alter table 航班
add constraint FK_航班_机场代码_出发_机场 foreign key (出发机场代码)
references 机场 (机场代码)
go
alter table 航班
add constraint FK_航班_机场代码_到达_机场 foreign key (到达机场代码)
references 机场 (机场代码)
go
alter table 航班
add constraint FK_航班_机场代码_经停_机场 foreign key (经停机场代码)
references 机场 (机场代码)
go
alter table 订票
add constraint FK_订票_用户名_用户表 foreign key (用户名)
references 用户表 (用户名)
go
alter table 订票
add constraint FK_订票_航程号_飞行计划安排 foreign key (航程号)
references 飞行计划安排 (航程号)
go
alter table 飞机登记
add constraint FK_飞机登记_机型_飞机表 foreign key (机型)
references 飞机表 (机型)
go
alter table 飞行计划安排
add constraint FK_飞行计划安排_航班编号_航程_航班 foreign key (航班编号)
references 航班 (航班编号)
go