-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCS6360.002-Team23-DB-Creation.sql
executable file
·183 lines (147 loc) · 5.82 KB
/
CS6360.002-Team23-DB-Creation.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
/*
CS6360: Database Design - Pharmacy Management System
Team 23:
Rahul Nalawade (rsn170330)
Ishan Sharma (ixs171130)
*/
DROP TABLE if exists Customer;
CREATE TABLE Customer (
SSN number(10) NOT NULL,
First Name char(255) NOT NULL,
Last Name char(255) NOT NULL,
Phone number(10) NOT NULL UNIQUE,
Gender char(1) NOT NULL,
Address char(1000) NOT NULL,
Date of Birth date NOT NULL,
Insurance ID number(10) NOT NULL UNIQUE,
PRIMARY KEY (SSN)
);
ALTER TABLE Customer ADD CONSTRAINT insures FOREIGN KEY (Insurance ID)
REFERENCES Insurance (Insurance ID) ON DELETE Set null;
DROP TABLE if exists Prescription;
CREATE TABLE Prescription (
Prescription ID number(10) NOT NULL,
SSN number(10) NOT NULL,
Doctor ID number(10) NOT NULL,
Prescribed Date date NOT NULL,
PRIMARY KEY (Prescription ID)
);
ALTER TABLE Prescription ADD CONSTRAINT holds FOREIGN KEY (SSN)
REFERENCES Customer (SSN);
CREATE TABLE Prescribed Drugs (
Prescription ID number(10) NOT NULL,
Drug Name char(255) NOT NULL,
Prescribed Quantity number(10) NOT NULL,
Refill Limit number(10) NOT NULL,
PRIMARY KEY (Prescription ID, Drug Name)
);
ALTER TABLE Prescribed Drugs ADD CONSTRAINT consists of FOREIGN KEY (Prescription ID)
REFERENCES Prescription (Prescription ID) ON DELETE Cascade;
DROP TABLE if exists Order;
CREATE TABLE Order (
Order ID number(10) NOT NULL,
Prescription ID number(10) NOT NULL,
EmployeeID number(5) NOT NULL,
Order Date date NOT NULL,
PRIMARY KEY (Order ID)
);
ALTER TABLE Order ADD CONSTRAINT prepares FOREIGN KEY (EmployeeID)
REFERENCES Employee (ID);
ALTER TABLE Order ADD CONSTRAINT uses FOREIGN KEY (Prescription ID)
REFERENCES Prescription (Prescription ID);
CREATE TABLE Ordered Drugs (
Order ID number(10) NOT NULL,
Drug Name char(255) NOT NULL,
Batch Number number(10) NOT NULL,
Ordered Quantity number(10) NOT NULL,
Price number(2) NOT NULL,
PRIMARY KEY (Order ID, Drug Name, Batch Number)
);
ALTER TABLE Ordered Drugs ADD CONSTRAINT contains FOREIGN KEY (Order ID)
REFERENCES Order (Order ID) ON DELETE Cascade;
ALTER TABLE Ordered Drugs ADD CONSTRAINT Fulfilled From FOREIGN KEY (Drug Name, Batch Number)
REFERENCES Medicine (Drug Name, Batch Number);
DROP TABLE if exists Insurance;
CREATE TABLE Insurance (
Insurance ID number(10) NOT NULL,
Company Name char(255) NOT NULL,
Start Date date NOT NULL,
End Date date NOT NULL,
Co-Insurance number(4) NOT NULL,
PRIMARY KEY (Insurance ID)
);
CREATE INDEX Insurance_Company Name
ON Insurance (Company Name);
DROP TABLE if exists Employee;
CREATE TABLE Employee (
ID number(5) NOT NULL,
SSN number(10) NOT NULL UNIQUE,
License number(10) UNIQUE,
First Name char(255) NOT NULL,
Last Name char(255) NOT NULL,
Start Date date NOT NULL,
End Date date,
Role char(255) NOT NULL,
Salary number(4) NOT NULL,
Phone Number number(10) NOT NULL,
Date of Birth date NOT NULL,
PRIMARY KEY (ID)
);
CREATE TABLE Medicine (
Drug Name char(255) NOT NULL,
Batch Number number(10) NOT NULL,
MedicineType char(255) NOT NULL,
Manufacturer char(255) NOT NULL,
Stock Quantity number(10) NOT NULL,
Expiry Date date NOT NULL,
Price number(4) NOT NULL,
PRIMARY KEY (Drug Name, Batch Number)
);
CREATE TABLE Bill (
Order ID number(10) NOT NULL,
CustomerSSN number(10) NOT NULL,
Total Amount number(4) NOT NULL,
Customer Payment number(4) NOT NULL,
Insurance Payment number(4) NOT NULL,
PRIMARY KEY (Order ID, CustomerSSN)
);
ALTER TABLE Bill ADD CONSTRAINT makes FOREIGN KEY (Order ID)
REFERENCES Order (Order ID);
ALTER TABLE Bill ADD CONSTRAINT pays FOREIGN KEY (CustomerSSN)
REFERENCES Customer (SSN);
CREATE TABLE Disposed Drugs (
Drug Name char(255) NOT NULL,
Batch Number number(10) NOT NULL,
Quantity number(10) NOT NULL,
Company char(255) NOT NULL,
PRIMARY KEY (Drug Name, Batch Number)
);
ALTER TABLE Disposed Drugs ADD CONSTRAINT disposed FOREIGN KEY (Drug Name, Batch Number)
REFERENCES Medicine (Drug Name, Batch Number);
DROP TABLE if exists Notification;
CREATE TABLE Notification (
ID number(10) NOT NULL,
Message char(255) NOT NULL,
Type char(255) NOT NULL,
PRIMARY KEY (ID)
);
CREATE TABLE Employee_Notification (
EmployeeID number(5) NOT NULL,
NotificationID number(10) NOT NULL,
PRIMARY KEY (EmployeeID, NotificationID)
);
ALTER TABLE Employee_Notification ADD CONSTRAINT FKEmployee_N849182 FOREIGN KEY (EmployeeID)
REFERENCES Employee (ID) ON DELETE Cascade;
ALTER TABLE Employee_Notification ADD CONSTRAINT FKEmployee_N664471 FOREIGN KEY (NotificationID)
REFERENCES Notification (ID) ON DELETE Cascade;
CREATE TABLE Employee_Disposed Drugs (
EmployeeID number(5) NOT NULL,
Drug Name char(255) NOT NULL,
Batch Number number(10) NOT NULL,
Disposal Date date NOT NULL,
PRIMARY KEY (EmployeeID, Drug Name, Batch Number, Disposal Date)
);
ALTER TABLE Employee_Disposed Drugs ADD CONSTRAINT FKEmployee_D470142 FOREIGN KEY (EmployeeID)
REFERENCES Employee (ID);
ALTER TABLE Employee_Disposed Drugs ADD CONSTRAINT FKEmployee_D990025 FOREIGN KEY (Drug Name, Batch Number)
REFERENCES Disposed Drugs (Drug Name, Batch Number);