-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLQuery1.sql
735 lines (590 loc) · 22.2 KB
/
SQLQuery1.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/*==============================================================*/
/* DBMS name: Microsoft SQL Server 2012 */
/* Created on: 5/5/2018 1:41:35 AM */
/*==============================================================*/
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('APPOINTMENT') and o.name = 'FK_APPOINTM_HAS_DOCTOR')
alter table APPOINTMENT
drop constraint FK_APPOINTM_HAS_DOCTOR
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('APPOINTMENT') and o.name = 'FK_APPOINTM_MAKE_PATIENT')
alter table APPOINTMENT
drop constraint FK_APPOINTM_MAKE_PATIENT
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('BED') and o.name = 'FK_BED_BELONG_TO_DEPARTME')
alter table BED
drop constraint FK_BED_BELONG_TO_DEPARTME
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('BED') and o.name = 'FK_BED_SUPERVISE_NURSE')
alter table BED
drop constraint FK_BED_SUPERVISE_NURSE
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('BILL') and o.name = 'FK_BILL_CHECK_PATIENT')
alter table BILL
drop constraint FK_BILL_CHECK_PATIENT
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('BUY') and o.name = 'FK_BUY_BUY_BILL')
alter table BUY
drop constraint FK_BUY_BUY_BILL
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('BUY') and o.name = 'FK_BUY_BUY2_MEDICINE')
alter table BUY
drop constraint FK_BUY_BUY2_MEDICINE
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('CARRY_OUT') and o.name = 'FK_CARRY_OU_CARRY_OUT_DOCTOR')
alter table CARRY_OUT
drop constraint FK_CARRY_OU_CARRY_OUT_DOCTOR
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('CARRY_OUT') and o.name = 'FK_CARRY_OU_CARRY_OUT_SURGERY')
alter table CARRY_OUT
drop constraint FK_CARRY_OU_CARRY_OUT_SURGERY
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('DOCTOR') and o.name = 'FK_DOCTOR_F_EMPLOYEE')
alter table DOCTOR
drop constraint FK_DOCTOR_F_EMPLOYEE
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('EMPLOYEE') and o.name = 'FK_EMPLOYEE_WORKS_ON_DEPARTME')
alter table EMPLOYEE
drop constraint FK_EMPLOYEE_WORKS_ON_DEPARTME
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('HELP_IN') and o.name = 'FK_HELP_IN_HELP_IN_NURSE')
alter table HELP_IN
drop constraint FK_HELP_IN_HELP_IN_NURSE
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('HELP_IN') and o.name = 'FK_HELP_IN_HELP_IN2_SURGERY')
alter table HELP_IN
drop constraint FK_HELP_IN_HELP_IN2_SURGERY
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('NURSE') and o.name = 'FK_NURSE_D_EMPLOYEE')
alter table NURSE
drop constraint FK_NURSE_D_EMPLOYEE
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('SURGERY') and o.name = 'FK_SURGERY_UNDERGOES_PATIENT')
alter table SURGERY
drop constraint FK_SURGERY_UNDERGOES_PATIENT
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('VISITOR') and o.name = 'FK_VISITOR_VISITS_PATIENT')
alter table VISITOR
drop constraint FK_VISITOR_VISITS_PATIENT
go
if exists (select 1
from sysindexes
where id = object_id('APPOINTMENT')
and name = 'HAS_FK'
and indid > 0
and indid < 255)
drop index APPOINTMENT.HAS_FK
go
if exists (select 1
from sysindexes
where id = object_id('APPOINTMENT')
and name = 'MAKE_FK'
and indid > 0
and indid < 255)
drop index APPOINTMENT.MAKE_FK
go
if exists (select 1
from sysobjects
where id = object_id('APPOINTMENT')
and type = 'U')
drop table APPOINTMENT
go
if exists (select 1
from sysindexes
where id = object_id('BED')
and name = 'SUPERVISE_FK'
and indid > 0
and indid < 255)
drop index BED.SUPERVISE_FK
go
if exists (select 1
from sysindexes
where id = object_id('BED')
and name = 'BELONG_TO_FK'
and indid > 0
and indid < 255)
drop index BED.BELONG_TO_FK
go
if exists (select 1
from sysobjects
where id = object_id('BED')
and type = 'U')
drop table BED
go
if exists (select 1
from sysindexes
where id = object_id('BILL')
and name = 'CHECK_FK'
and indid > 0
and indid < 255)
drop index BILL.CHECK_FK
go
if exists (select 1
from sysobjects
where id = object_id('BILL')
and type = 'U')
drop table BILL
go
if exists (select 1
from sysindexes
where id = object_id('BUY')
and name = 'BUY2_FK'
and indid > 0
and indid < 255)
drop index BUY.BUY2_FK
go
if exists (select 1
from sysindexes
where id = object_id('BUY')
and name = 'BUY_FK'
and indid > 0
and indid < 255)
drop index BUY.BUY_FK
go
if exists (select 1
from sysobjects
where id = object_id('BUY')
and type = 'U')
drop table BUY
go
if exists (select 1
from sysindexes
where id = object_id('CARRY_OUT')
and name = 'CARRY_OUT2_FK'
and indid > 0
and indid < 255)
drop index CARRY_OUT.CARRY_OUT2_FK
go
if exists (select 1
from sysindexes
where id = object_id('CARRY_OUT')
and name = 'CARRY_OUT_FK'
and indid > 0
and indid < 255)
drop index CARRY_OUT.CARRY_OUT_FK
go
if exists (select 1
from sysobjects
where id = object_id('CARRY_OUT')
and type = 'U')
drop table CARRY_OUT
go
if exists (select 1
from sysobjects
where id = object_id('DEPARTMENT')
and type = 'U')
drop table DEPARTMENT
go
if exists (select 1
from sysobjects
where id = object_id('DOCTOR')
and type = 'U')
drop table DOCTOR
go
if exists (select 1
from sysindexes
where id = object_id('EMPLOYEE')
and name = 'WORKS_ON_FK'
and indid > 0
and indid < 255)
drop index EMPLOYEE.WORKS_ON_FK
go
if exists (select 1
from sysobjects
where id = object_id('EMPLOYEE')
and type = 'U')
drop table EMPLOYEE
go
if exists (select 1
from sysindexes
where id = object_id('HELP_IN')
and name = 'HELP_IN2_FK'
and indid > 0
and indid < 255)
drop index HELP_IN.HELP_IN2_FK
go
if exists (select 1
from sysindexes
where id = object_id('HELP_IN')
and name = 'HELP_IN_FK'
and indid > 0
and indid < 255)
drop index HELP_IN.HELP_IN_FK
go
if exists (select 1
from sysobjects
where id = object_id('HELP_IN')
and type = 'U')
drop table HELP_IN
go
if exists (select 1
from sysobjects
where id = object_id('MEDICINE')
and type = 'U')
drop table MEDICINE
go
if exists (select 1
from sysobjects
where id = object_id('NURSE')
and type = 'U')
drop table NURSE
go
if exists (select 1
from sysobjects
where id = object_id('PATIENT')
and type = 'U')
drop table PATIENT
go
if exists (select 1
from sysindexes
where id = object_id('SURGERY')
and name = 'UNDERGOES_FK'
and indid > 0
and indid < 255)
drop index SURGERY.UNDERGOES_FK
go
if exists (select 1
from sysobjects
where id = object_id('SURGERY')
and type = 'U')
drop table SURGERY
go
if exists (select 1
from sysindexes
where id = object_id('VISITOR')
and name = 'VISITS_FK'
and indid > 0
and indid < 255)
drop index VISITOR.VISITS_FK
go
if exists (select 1
from sysobjects
where id = object_id('VISITOR')
and type = 'U')
drop table VISITOR
go
/*==============================================================*/
/* Table: APPOINTMENT */
/*==============================================================*/
create table APPOINTMENT (
APPOINTMENT_NO_ int not null,
PATIENT_ID int null,
EMPLOYEE_ID int null,
APPOINTMENT_DATE datetime null,
DOCTORID int null,
PATIENTID int null,
constraint PK_APPOINTMENT primary key (APPOINTMENT_NO_)
)
go
/*==============================================================*/
/* Index: MAKE_FK */
/*==============================================================*/
create nonclustered index MAKE_FK on APPOINTMENT (PATIENT_ID ASC)
go
/*==============================================================*/
/* Index: HAS_FK */
/*==============================================================*/
create nonclustered index HAS_FK on APPOINTMENT (EMPLOYEE_ID ASC)
go
/*==============================================================*/
/* Table: BED */
/*==============================================================*/
create table BED (
ROOMNO_ int null,
BED_ID int not null,
DEP_CODE int not null,
EMPLOYEE_ID int not null,
PATIENTID int null,
constraint PK_BED primary key (BED_ID)
)
go
/*==============================================================*/
/* Index: BELONG_TO_FK */
/*==============================================================*/
create nonclustered index BELONG_TO_FK on BED (DEP_CODE ASC)
go
/*==============================================================*/
/* Index: SUPERVISE_FK */
/*==============================================================*/
create nonclustered index SUPERVISE_FK on BED (EMPLOYEE_ID ASC)
go
/*==============================================================*/
/* Table: BILL */
/*==============================================================*/
create table BILL (
BILL_CODE int not null,
PATIENT_ID int null,
BILL_PRICE float null,
constraint PK_BILL primary key (BILL_CODE)
)
go
/*==============================================================*/
/* Index: CHECK_FK */
/*==============================================================*/
create nonclustered index CHECK_FK on BILL (PATIENT_ID ASC)
go
/*==============================================================*/
/* Table: BUY */
/*==============================================================*/
create table BUY (
BILL_CODE int not null,
MED_NAME varchar(10) not null,
constraint PK_BUY primary key (BILL_CODE, MED_NAME)
)
go
/*==============================================================*/
/* Index: BUY_FK */
/*==============================================================*/
create nonclustered index BUY_FK on BUY (BILL_CODE ASC)
go
/*==============================================================*/
/* Index: BUY2_FK */
/*==============================================================*/
create nonclustered index BUY2_FK on BUY (MED_NAME ASC)
go
/*==============================================================*/
/* Table: CARRY_OUT */
/*==============================================================*/
create table CARRY_OUT (
EMPLOYEE_ID int not null,
SURGERY_NO_ int not null,
constraint PK_CARRY_OUT primary key (EMPLOYEE_ID, SURGERY_NO_)
)
go
/*==============================================================*/
/* Index: CARRY_OUT_FK */
/*==============================================================*/
create nonclustered index CARRY_OUT_FK on CARRY_OUT (EMPLOYEE_ID ASC)
go
/*==============================================================*/
/* Index: CARRY_OUT2_FK */
/*==============================================================*/
create nonclustered index CARRY_OUT2_FK on CARRY_OUT (SURGERY_NO_ ASC)
go
/*==============================================================*/
/* Table: DEPARTMENT */
/*==============================================================*/
create table DEPARTMENT (
DEP_CODE int not null,
DEP_NAME varchar(10) null,
constraint PK_DEPARTMENT primary key (DEP_CODE)
)
go
/*==============================================================*/
/* Table: DOCTOR */
/*==============================================================*/
create table DOCTOR (
EMPLOYEE_ID int not null,
DEP_CODE int null,
DEP_NO_ int null,
PHONE_NO_ numeric(10) null,
EMPLOYEE_NAME varchar(10) null,
EMPLOYEE_DOB datetime null,
EMPLOYEE_ADDRESS varchar(10) null,
EMPLOYEE_GENDER varchar(10) null,
RANK int null,
constraint PK_DOCTOR primary key (EMPLOYEE_ID)
)
go
/*==============================================================*/
/* Table: EMPLOYEE */
/*==============================================================*/
create table EMPLOYEE (
EMPLOYEE_ID int not null,
DEP_CODE int not null,
DEP_NO_ int null,
PHONE_NO_ numeric(10) null,
EMPLOYEE_NAME varchar(10) null,
EMPLOYEE_DOB datetime null,
EMPLOYEE_ADDRESS varchar(10) null,
EMPLOYEE_GENDER varchar(10) null,
constraint PK_EMPLOYEE primary key (EMPLOYEE_ID)
)
go
/*==============================================================*/
/* Index: WORKS_ON_FK */
/*==============================================================*/
create nonclustered index WORKS_ON_FK on EMPLOYEE (DEP_CODE ASC)
go
/*==============================================================*/
/* Table: HELP_IN */
/*==============================================================*/
create table HELP_IN (
EMPLOYEE_ID int not null,
SURGERY_NO_ int not null,
constraint PK_HELP_IN primary key (EMPLOYEE_ID, SURGERY_NO_)
)
go
/*==============================================================*/
/* Index: HELP_IN_FK */
/*==============================================================*/
create nonclustered index HELP_IN_FK on HELP_IN (EMPLOYEE_ID ASC)
go
/*==============================================================*/
/* Index: HELP_IN2_FK */
/*==============================================================*/
create nonclustered index HELP_IN2_FK on HELP_IN (SURGERY_NO_ ASC)
go
/*==============================================================*/
/* Table: MEDICINE */
/*==============================================================*/
create table MEDICINE (
MED_NAME varchar(10) not null,
QUANTITY int null,
constraint PK_MEDICINE primary key (MED_NAME)
)
go
/*==============================================================*/
/* Table: NURSE */
/*==============================================================*/
create table NURSE (
EMPLOYEE_ID int not null,
DEP_CODE int null,
DEP_NO_ int null,
PHONE_NO_ numeric(10) null,
EMPLOYEE_NAME varchar(10) null,
EMPLOYEE_DOB datetime null,
EMPLOYEE_ADDRESS varchar(10) null,
EMPLOYEE_GENDER varchar(10) null,
constraint PK_NURSE primary key (EMPLOYEE_ID)
)
go
/*==============================================================*/
/* Table: PATIENT */
/*==============================================================*/
create table PATIENT (
PATIENT_ID int not null,
PATIENT_DOB datetime null,
PATIENT_GENDER varchar(6) null,
PATIENT_NAME varchar(10) null,
PATIENT_ADDRESS varchar(20) null,
PATIENT_CONTACT numeric(10) null,
constraint PK_PATIENT primary key (PATIENT_ID)
)
go
/*==============================================================*/
/* Table: SURGERY */
/*==============================================================*/
create table SURGERY (
SURGERY_NO_ int not null,
PATIENT_ID int null,
ROOM_NO_ int null,
SURGERY_DATE datetime null,
constraint PK_SURGERY primary key (SURGERY_NO_)
)
go
/*==============================================================*/
/* Index: UNDERGOES_FK */
/*==============================================================*/
create nonclustered index UNDERGOES_FK on SURGERY (PATIENT_ID ASC)
go
/*==============================================================*/
/* Table: VISITOR */
/*==============================================================*/
create table VISITOR (
SSN int not null,
PATIENT_ID int null,
VISITOR_NAME varchar(10) null,
constraint PK_VISITOR primary key (SSN)
)
go
/*==============================================================*/
/* Index: VISITS_FK */
/*==============================================================*/
create nonclustered index VISITS_FK on VISITOR (PATIENT_ID ASC)
go
alter table APPOINTMENT
add constraint FK_APPOINTM_HAS_DOCTOR foreign key (EMPLOYEE_ID)
references DOCTOR (EMPLOYEE_ID)
go
alter table APPOINTMENT
add constraint FK_APPOINTM_MAKE_PATIENT foreign key (PATIENT_ID)
references PATIENT (PATIENT_ID)
go
alter table BED
add constraint FK_BED_BELONG_TO_DEPARTME foreign key (DEP_CODE)
references DEPARTMENT (DEP_CODE)
go
alter table BED
add constraint FK_BED_SUPERVISE_NURSE foreign key (EMPLOYEE_ID)
references NURSE (EMPLOYEE_ID)
go
alter table BILL
add constraint FK_BILL_CHECK_PATIENT foreign key (PATIENT_ID)
references PATIENT (PATIENT_ID)
go
alter table BUY
add constraint FK_BUY_BUY_BILL foreign key (BILL_CODE)
references BILL (BILL_CODE)
go
alter table BUY
add constraint FK_BUY_BUY2_MEDICINE foreign key (MED_NAME)
references MEDICINE (MED_NAME)
go
alter table CARRY_OUT
add constraint FK_CARRY_OU_CARRY_OUT_DOCTOR foreign key (EMPLOYEE_ID)
references DOCTOR (EMPLOYEE_ID)
go
alter table CARRY_OUT
add constraint FK_CARRY_OU_CARRY_OUT_SURGERY foreign key (SURGERY_NO_)
references SURGERY (SURGERY_NO_)
go
alter table DOCTOR
add constraint FK_DOCTOR_F_EMPLOYEE foreign key (EMPLOYEE_ID)
references EMPLOYEE (EMPLOYEE_ID)
go
alter table EMPLOYEE
add constraint FK_EMPLOYEE_WORKS_ON_DEPARTME foreign key (DEP_CODE)
references DEPARTMENT (DEP_CODE)
go
alter table HELP_IN
add constraint FK_HELP_IN_HELP_IN_NURSE foreign key (EMPLOYEE_ID)
references NURSE (EMPLOYEE_ID)
go
alter table HELP_IN
add constraint FK_HELP_IN_HELP_IN2_SURGERY foreign key (SURGERY_NO_)
references SURGERY (SURGERY_NO_)
go
alter table NURSE
add constraint FK_NURSE_D_EMPLOYEE foreign key (EMPLOYEE_ID)
references EMPLOYEE (EMPLOYEE_ID)
go
alter table SURGERY
add constraint FK_SURGERY_UNDERGOES_PATIENT foreign key (PATIENT_ID)
references PATIENT (PATIENT_ID)
go
alter table VISITOR
add constraint FK_VISITOR_VISITS_PATIENT foreign key (PATIENT_ID)
references PATIENT (PATIENT_ID)
go