-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_create_resources.sql
104 lines (94 loc) · 2.32 KB
/
01_create_resources.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
-- Entities
CREATE TABLE Patient (
ID int PRIMARY KEY IDENTITY (1, 1),
FirstName varchar(255) NOT NULL,
LastName varchar(255),
Email varchar(255),
PhoneNumber varchar(25),
DateOfBirth date,
Sex varchar(25),
SSN varchar(11),
Street varchar(255),
State varchar(5),
ZipCode varchar(10)
);
CREATE TABLE Doctor (
ID int PRIMARY KEY IDENTITY (1, 1),
FirstName varchar(255) NOT NULL,
LastName varchar(255),
Email varchar(255),
PhoneNumber varchar(25)
);
CREATE TABLE MedicalHistory (
ID int PRIMARY KEY IDENTITY (1, 1),
PatientID int,
Medications varchar(255),
Diseases varchar(255),
Allergies varchar(255),
FamilyHistory varchar(255)
);
CREATE TABLE Room (
ID int PRIMARY KEY IDENTITY (1, 1),
Number int NOT NULL,
Floor int NOT NULL,
Type varchar(255),
Occupied bit NOT NULL
);
CREATE TABLE Drug (
ID int PRIMARY KEY IDENTITY (1, 1),
Name varchar(255) NOT NULL,
Company varchar(255),
UnitCost money
);
CREATE TABLE Visit (
ID int PRIMARY KEY IDENTITY (1, 1),
PatientID int NOT NULL,
DoctorID int NOT NULL,
RoomID int,
VisitDate date,
VisitTime time,
PatientWeight float,
PatientSymptoms varchar(255),
Notes varchar(255)
);
CREATE TABLE Prescription (
ID int PRIMARY KEY IDENTITY (1, 1),
DrugID int NOT NULL,
VisitID int NOT NULL,
Quantity float,
Dosage varchar(255)
);
-- Foreign Keys
-- Medical History of a Patient
ALTER TABLE MedicalHistory ADD
CONSTRAINT FK_MedicalHistory_Patient FOREIGN KEY (PatientID)
REFERENCES Patient (ID)
ON DELETE CASCADE
ON UPDATE CASCADE
;
-- A Visit happens between a Patient and a Doctor in a Room
ALTER TABLE Visit ADD
CONSTRAINT FK_Visit_Patient FOREIGN KEY (PatientID)
REFERENCES Patient (ID)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT FK_Visit_Doctor FOREIGN KEY (DoctorID)
REFERENCES Doctor (ID)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT FK_Visit_Room FOREIGN KEY (RoomID)
REFERENCES Room (ID)
ON DELETE CASCADE
ON UPDATE CASCADE
;
-- A Prescription lists a Drug and happens during a Visit
ALTER TABLE Prescription ADD
CONSTRAINT FK_Prescription_Visit FOREIGN KEY (VisitID)
REFERENCES Visit (ID)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT FK_Prescription_Drug FOREIGN KEY (DrugID)
REFERENCES Drug (ID)
ON DELETE CASCADE
ON UPDATE CASCADE
;