-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-databases.sql
161 lines (143 loc) · 4.87 KB
/
create-databases.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
DROP DATABASE PILLWATCHER;
CREATE DATABASE IF NOT EXISTS PILLWATCHER
CHARACTER SET UTF8
COLLATE UTF8_GENERAL_CI;
USE PILLWATCHER;
DROP TABLE SUPPLY;
DROP TABLE APPLIED_MEDICATION;
DROP TABLE CUP;
DROP TABLE MEDICATION;
DROP TABLE MEDICINE;
DROP TABLE PRESCRIPTION;
DROP TABLE NURSE_PATIENT;
DROP TABLE PATIENT;
DROP TABLE NURSE;
DROP TABLE ADMIN;
DROP TABLE USER;
CREATE TABLE IF NOT EXISTS USER (
ID_USER BIGINT AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(50) NOT NULL,
DOCUMENT VARCHAR(11) NOT NULL,
IMAGE_URL VARCHAR(200),
ENRYPTED_PASS VARCHAR(200),
CONSTRAINT USER_AK UNIQUE (ID_USER)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS ADMIN (
ID_ADMIN BIGINT AUTO_INCREMENT PRIMARY KEY,
ID_USER BIGINT NOT NULL,
EMAIL VARCHAR(50) NOT NULL,
INCLUSION_DATE DATE NOT NULL,
UPDATE_DATE DATE NOT NULL,
CONSTRAINT ADMIN_FK FOREIGN KEY (ID_USER)
REFERENCES USER (ID_USER),
CONSTRAINT ADMIN_AK UNIQUE (EMAIL)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS NURSE (
ID_NURSE BIGINT AUTO_INCREMENT PRIMARY KEY,
ID_USER BIGINT NOT NULL,
EMAIL VARCHAR(50) NOT NULL,
COREN VARCHAR(15) NOT NULL,
FEDERATIVE_UNIT VARCHAR(2) NOT NULL,
BIOMETRY BOOLEAN NOT NULL,
PHONE VARCHAR(15) NOT NULL,
INCLUSION_DATE DATE NOT NULL,
UPDATE_DATE DATE NOT NULL,
CONSTRAINT NURSE_FK FOREIGN KEY (ID_USER)
REFERENCES USER (ID_USER),
CONSTRAINT NURSE_AK UNIQUE (EMAIL)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS PATIENT (
ID_PATIENT BIGINT AUTO_INCREMENT PRIMARY KEY,
ID_USER BIGINT NOT NULL,
BORN_DATE DATE NOT NULL,
OBSERVATION VARCHAR(250),
INCLUSION_DATE DATE NOT NULL,
UPDATE_DATE DATE NOT NULL,
CONSTRAINT PATIENT_FK FOREIGN KEY (ID_USER)
REFERENCES USER (ID_USER)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS NURSE_PATIENT (
ID_NURSE_PAT BIGINT AUTO_INCREMENT PRIMARY KEY,
ID_NURSE BIGINT NOT NULL,
ID_PATIENT BIGINT NOT NULL,
CONSTRAINT NURSE_PATIENT_FK FOREIGN KEY (ID_PATIENT)
REFERENCES PATIENT (ID_PATIENT),
CONSTRAINT NURSE_PATIENT_FK_2 FOREIGN KEY (ID_NURSE)
REFERENCES NURSE (ID_NURSE)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS PRESCRIPTION (
ID_PRESCRIPTION BIGINT AUTO_INCREMENT PRIMARY KEY,
ID_PATIENT BIGINT NOT NULL,
IMAGE_URL VARCHAR(200),
INCLUSION_DATE DATE NOT NULL,
UPDATE_DATE DATE NOT NULL,
VALIDITY_DATE DATE NOT NULL,
CONSTRAINT PRESCRIPTION_FK FOREIGN KEY (ID_PATIENT)
REFERENCES PATIENT (ID_PATIENT)
) ENGINE=INNODB;
-- Atualizar
CREATE TABLE IF NOT EXISTS MEDICINE (
ID_MEDICINE BIGINT AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(150) NOT NULL,
DOSAGE INT NOT NULL,
DOSAGE_TYPE VARCHAR(50) NOT NULL,
INCLUSION_DATE DATE NOT NULL,
UPDATE_DATE DATE NOT NULL,
CONSTRAINT MEDICINE_AK UNIQUE (DOSAGE_TYPE , DOSAGE, NAME)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS CUP (
ID_CUP BIGINT AUTO_INCREMENT PRIMARY KEY,
COLOR VARCHAR(50) NOT NULL,
TAG VARCHAR(255) NOT NULL,
CONSTRAINT CUP_AK UNIQUE (TAG)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS MEDICATION (
ID_MEDICATION BIGINT AUTO_INCREMENT PRIMARY KEY,
ID_PRESCRIPTION BIGINT NOT NULL,
ID_MEDICINE BIGINT NOT NULL,
QUANTITY INT NOT NULL,
INTERVAL_TIME INT NOT NULL,
BATCH VARCHAR(200) NOT NULL,
OBSERVATION VARCHAR(255) NOT NULL,
AVAILABLE_QUANTITY INT NOT NULL,
START_DATE DATE NOT NULL,
UPDATE_DATE DATE NOT NULL,
INCLUSION_DATE DATE NOT NULL,
EXPIRATION_DATE DATE NOT NULL,
LOCATION INT NOT NULL,
ID_CUP BIGINT NOT NULL,
CONSTRAINT MEDICATION_FK FOREIGN KEY (ID_PRESCRIPTION)
REFERENCES PRESCRIPTION (ID_PRESCRIPTION),
CONSTRAINT MEDICATION_FK2 FOREIGN KEY (ID_MEDICINE)
REFERENCES MEDICINE (ID_MEDICINE),
CONSTRAINT MEDICATION_FK3 FOREIGN KEY (ID_CUP)
REFERENCES CUP (ID_CUP),
CONSTRAINT MEDICATION_AK UNIQUE (ID_PRESCRIPTION , ID_MEDICINE)
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS APPLIED_MEDICATION (
ID_APPLIED_MEDICATION BIGINT AUTO_INCREMENT PRIMARY KEY,
ID_NURSE BIGINT NOT NULL,
ID_MEDICATION BIGINT NOT NULL,
MEDICATION_DATE DATE NOT NULL,
NEXT_MEDICATION_DATE DATE NOT NULL,
CONSTRAINT NURSE_MEDICATION_FK FOREIGN KEY (ID_NURSE)
REFERENCES NURSE (ID_NURSE),
CONSTRAINT APPLIED_MEDICATION_FK2 FOREIGN KEY (ID_MEDICATION)
REFERENCES MEDICATION (ID_MEDICATION)
) ENGINE=INNODB;
-- ATUALIZAR
CREATE TABLE IF NOT EXISTS SUPPLY (
ID_SUPPLY BIGINT AUTO_INCREMENT PRIMARY KEY,
ID_NURSE BIGINT NOT NULL,
ID_MEDICATION BIGINT NOT NULL,
QUANTITY INT NOT NULL,
INCLUSION_DATE DATE NOT NULL,
UPDATE_DATE DATE NOT NULL,
CONFIRMED_DATE DATE NOT NULL,
SUCCESS INT NOT NULL,
CONSTRAINT SUPPLY_FK FOREIGN KEY (ID_NURSE)
REFERENCES NURSE (ID_NURSE),
CONSTRAINT SUPPLY_FK2 FOREIGN KEY (ID_MEDICATION)
REFERENCES MEDICATION (ID_MEDICATION)
) ENGINE=INNODB;
COMMIT;