-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_tables.sql
193 lines (167 loc) · 4.62 KB
/
create_tables.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
CREATE DATABASE DBMS_PROJECT;
USE DBMS_PROJECT;
CREATE TABLE login(
username VARCHAR(20) NOT NULL,
password VARCHAR(20) NOT NULL
);
INSERT INTO login VALUES ('admin','admin');
#table 1
CREATE TABLE User(
User_ID int NOT NULL,
Name varchar(20) NOT NULL,
Date_of_Birth date NOT NULL,
Medical_insurance int,
Medical_history varchar(20),
Street varchar(20),
City varchar(20),
State varchar(20),
PRIMARY KEY(User_ID)
);
#table 2
CREATE TABLE User_phone_no(
User_ID int NOT NULL,
phone_no varchar(15),
FOREIGN KEY(User_ID) REFERENCES User(User_ID) ON DELETE CASCADE
);
#table 3
CREATE TABLE Organization(
Organization_ID int NOT NULL,
Organization_name varchar(20) NOT NULL,
Location varchar(20),
Government_approved int, # 0 or 1
PRIMARY KEY(Organization_ID)
);
#table 4
CREATE TABLE Doctor(
Doctor_ID int NOT NULL,
Doctor_Name varchar(20) NOT NULL,
Department_Name varchar(20) NOT NULL,
organization_ID int NOT NULL,
FOREIGN KEY(organization_ID) REFERENCES Organization(organization_ID) ON DELETE CASCADE,
PRIMARY KEY(Doctor_ID)
);
#table 5
CREATE TABLE Patient(
Patient_ID int NOT NULL,
organ_req varchar(20) NOT NULL,
reason_of_procurement varchar(20),
Doctor_ID int NOT NULL,
User_ID int NOT NULL,
FOREIGN KEY(User_ID) REFERENCES User(User_ID) ON DELETE CASCADE,
FOREIGN KEY(Doctor_ID) REFERENCES Doctor(Doctor_ID) ON DELETE CASCADE,
PRIMARY KEY(Patient_Id, organ_req)
);
#table 6
CREATE TABLE Donor(
Donor_ID int NOT NULL,
organ_donated varchar(20) NOT NULL,
reason_of_donation varchar(20),
Organization_ID int NOT NULL,
User_ID int NOT NULL,
FOREIGN KEY(User_ID) REFERENCES User(User_ID) ON DELETE CASCADE,
FOREIGN KEY(Organization_ID) REFERENCES Organization(Organization_ID) ON DELETE CASCADE,
PRIMARY KEY(Donor_ID, organ_donated)
);
#table 7
CREATE TABLE Organ_available(
Organ_ID int NOT NULL AUTO_INCREMENT,
Organ_name varchar(20) NOT NULL,
Donor_ID int NOT NULL,
FOREIGN KEY(Donor_ID) REFERENCES Donor(Donor_ID) ON DELETE CASCADE,
PRIMARY KEY(Organ_ID)
);
#table 8
CREATE TABLE Transaction(
Patient_ID int NOT NULL,
Organ_ID int NOT NULL,
Donor_ID int NOT NULL,
Date_of_transaction date NOT NULL,
Status int NOT NULL, #0 or 1
FOREIGN KEY(Patient_ID) REFERENCES Patient(Patient_ID) ON DELETE CASCADE,
FOREIGN KEY(Donor_ID) REFERENCES Donor(Donor_ID) ON DELETE CASCADE,
PRIMARY KEY(Patient_ID,Organ_ID)
);
#table 9
CREATE TABLE Organization_phone_no(
Organization_ID int NOT NULL,
Phone_no varchar(15),
FOREIGN KEY(Organization_ID) REFERENCES Organization(Organization_ID) ON DELETE CASCADE
);
#table 10
CREATE TABLE Doctor_phone_no(
Doctor_ID int NOT NULL,
Phone_no varchar(15),
FOREIGN KEY(Doctor_ID) REFERENCES Doctor(Doctor_ID) ON DELETE CASCADE
);
#table 11
CREATE TABLE Organization_head(
Organization_ID int NOT NULL,
Employee_ID int NOT NULL,
Name varchar(20) NOT NULL,
Date_of_joining date NOT NULL,
Term_length int NOT NULL,
FOREIGN KEY(Organization_ID) REFERENCES Organization(Organization_ID) ON DELETE CASCADE,
PRIMARY KEY(Organization_ID,Employee_ID)
);
create table log (
querytime datetime,
comment varchar(255)
);
delimiter //
create trigger ADD_DONOR_LOG
after insert
on Donor
for each row
begin
insert into log values
(now(), concat("Inserted new Donor", cast(new.Donor_Id as char)));
end //
create trigger UPD_DONOR_LOG
after update
on Donor
for each row
begin
insert into log values
(now(), concat("Updated Donor Details", cast(new.Donor_Id as char)));
end //
delimiter //
create trigger DEL_DONOR_LOG
after delete
on Donor
for each row
begin
insert into log values
(now(), concat("Deleted Donor ", cast(old.Donor_Id as char)));
end //
create trigger ADD_PATIENT_LOG
after insert
on Patient
for each row
begin
insert into log values
(now(), concat("Inserted new Patient ", cast(new.Patient_Id as char)));
end //
create trigger UPD_PATIENT_LOG
after update
on Patient
for each row
begin
insert into log values
(now(), concat("Updated Patient Details ", cast(new.Patient_Id as char)));
end //
create trigger DEL_PATIENT_LOG
after delete
on Donor
for each row
begin
insert into log values
(now(), concat("Deleted Patient ", cast(old.Donor_Id as char)));
end //
create trigger ADD_TRASACTION_LOG
after insert
on Transaction
for each row
begin
insert into log values
(now(), concat("Added Transaction :: Patient ID : ", cast(new.Patient_ID as char), "; Donor ID : " ,cast(new.Donor_ID as char)));
end //