-
Notifications
You must be signed in to change notification settings - Fork 0
/
dml.sql
130 lines (88 loc) · 5.9 KB
/
dml.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
/* CREATE */
/* Insert new patient */
INSERT INTO PATIENTS (first_name, last_name, birthdate, email, phone, primary_doctor, primary_location) VALUES (?,?,?,?,?,?,?)
/* Insert new doctor */
INSERT INTO DOCTORS (title, first_name, last_name, degree) VALUES (?,?,?,?)
/* Insert new location */
INSERT INTO LOCATIONS (label, street1, street2, city, state, zip, phone) VALUES (?,?,?,?,?,?,?);
/* Insert new appointment */
INSERT INTO APPOINTMENTS (doctor_id, location_id, time, year, month, day, day_of_week) VALUES (?,?,?,?,?,?,?);
/* READ */
/* Get open appointment slots on a particular date */
SELECT appointment_id AS id, time FROM APPOINTMENTS WHERE patient_id IS NULL AND doctor_id = ? AND location_id = ? AND year = ? AND month = ? AND day = ?;
/* Get dates where there are open appointment slots */
SELECT DOCTORS.doctor_id AS did, DOCTORS.title AS dtitle, DOCTORS.first_name AS dfname, DOCTORS.last_name AS dlname, LOCATIONS.location_id AS lid, LOCATIONS.label AS location FROM PATIENTS
JOIN DOCTORS ON PATIENTS.primary_doctor = DOCTORS.doctor_id AND PATIENTS.patient_id = ?
JOIN LOCATIONS ON PATIENTS.primary_location = LOCATIONS.location_id AND PATIENTS.patient_id = ?;
SELECT year, month, day FROM APPOINTMENTS WHERE patient_id IS NULL AND doctor_id = ? AND location_id = ? GROUP BY month, day;
/* Get patients who have a primary doctor and location assigned */
SELECT patient_id, first_name, last_name FROM PATIENTS WHERE primary_doctor IS NOT NULL AND primary_location IS NOT NULL;
/* Get details of a particular scheduled appointment */
SELECT PATIENTS.patient_id AS pid, PATIENTS.first_name AS pfname, PATIENTS.last_name AS plname, year, month, day, time, day_of_week,
title AS dtitle, DOCTORS.first_name AS dfname, DOCTORS.last_name AS dlname, label AS location FROM APPOINTMENTS
JOIN PATIENTS ON APPOINTMENTS.patient_id = PATIENTS.patient_id
JOIN DOCTORS ON APPOINTMENTS.doctor_id = DOCTORS.doctor_id
JOIN LOCATIONS ON APPOINTMENTS.location_id = LOCATIONS.location_id
WHERE appointment_id = ?;
/* Get list of patients */
SELECT patient_id AS id, CONCAT(first_name, " ", last_name) AS name FROM PATIENTS;
/* Get list of doctors */
SELECT doctor_id, CONCAT(title, " ", first_name, " ", last_name) AS name FROM DOCTORS;
/* Get list of locations */
SELECT location_id, label FROM LOCATIONS;
/* Get list of appointments */
SELECT appointment_id AS id, day_of_week, month, CONCAT(" ", day, ", ", year) AS header, time, APPOINTMENTS.patient_id AS booked,
APPOINTMENTS.doctor_id AS doc, CONCAT(title, " ", first_name, " ", last_name) AS doctor_name, APPOINTMENTS.location_id AS loc, label FROM APPOINTMENTS
JOIN DOCTORS ON APPOINTMENTS.doctor_id = DOCTORS.doctor_id AND APPOINTMENTS.doctor_id = ?
JOIN LOCATIONS ON APPOINTMENTS.location_id = LOCATIONS.location_id AND APPOINTMENTS.location_id = ?
ORDER BY loc, doc, id ASC;
/* Get details of a particular patient */
SELECT patient_id, first_name, last_name, birthdate, email, phone FROM PATIENTS WHERE patient_id = ?;
/* Get details of a particular doctor */
SELECT doctor_id, title, first_name, last_name, degree FROM DOCTORS WHERE doctor_id = ?;
/* Get details of a particular location */
SELECT location_id, label, street1, street2, city, state, zip, phone FROM LOCATIONS WHERE location_id = ?;
/* Get details of a particular appointment */
SELECT appointment_id, time, year, month, day FROM APPOINTMENTS WHERE appointment_id = ?;
/* Get list of appointments scheduled by a particular patient */
SELECT PATIENTS.patient_id AS pid, CONCAT(PATIENTS.first_name, " ", PATIENTS.last_name) AS name, birthdate, email, PATIENTS.phone AS phone,
CONCAT(title, " ", DOCTORS.first_name, " ", DOCTORS.last_name) AS doc, label AS loc,
appointment_id AS aid, day_of_week, month, CONCAT(" ", day, ", ", year) AS header, time, chief_complaint AS reason FROM PATIENTS
JOIN DOCTORS ON PATIENTS.primary_doctor = DOCTORS.doctor_id
JOIN LOCATIONS ON PATIENTS.primary_location = LOCATIONS.location_id
LEFT JOIN APPOINTMENTS ON PATIENTS.patient_id = APPOINTMENTS.patient_id
WHERE PATIENTS.patient_id = ?;
/* Get list of doctors who work at each location */
SELECT DOCTORS.doctor_id AS id, title, first_name, last_name, degree, label FROM DOCTORS
JOIN DOCTORS_LOCATIONS ON DOCTORS.doctor_id = DOCTORS_LOCATIONS.doctor_id
JOIN LOCATIONS ON DOCTORS_LOCATIONS.location_id = LOCATIONS.location_id ORDER BY id ASC;
/* Get list of locations who have each doctor on-site */
SELECT LOCATIONS.location_id AS id, label, street1, street2, city, state, zip, phone, title, first_name, last_name FROM LOCATIONS
JOIN DOCTORS_LOCATIONS ON LOCATIONS.location_id = DOCTORS_LOCATIONS.location_id
JOIN DOCTORS ON DOCTORS_LOCATIONS.doctor_id = DOCTORS.doctor_id ORDER BY id ASC;
/* Get list of locations who have a particular doctor on-site */
SELECT LOCATIONS.location_id AS id, label FROM LOCATIONS
JOIN DOCTORS_LOCATIONS ON LOCATIONS.location_id = DOCTORS_LOCATIONS.location_id
JOIN DOCTORS ON DOCTORS_LOCATIONS.doctor_id = DOCTORS.doctor_id AND DOCTORS.doctor_id = ?
ORDER BY id ASC;
/* Get list of doctors who work at a particular location */
SELECT DOCTORS.doctor_id AS id, CONCAT(title, " ", first_name, " ", last_name) AS name FROM DOCTORS
JOIN DOCTORS_LOCATIONS ON DOCTORS.doctor_id = DOCTORS_LOCATIONS.doctor_id
JOIN LOCATIONS ON DOCTORS_LOCATIONS.location_id = LOCATIONS.location_id AND LOCATIONS.location_id = ?
ORDER BY id ASC;
/* UPDATE */
/* Schedule appointment */
UPDATE APPOINTMENTS SET patient_id = ?, chief_complaint = ? WHERE appointment_id = ?;
/* Cancel ("un-schedule") appointment */
UPDATE APPOINTMENTS SET patient_id = NULL, chief_complaint = NULL WHERE appointment_id = ?;
/* DELETE */
/* Delete patient */
DELETE FROM PATIENTS WHERE patient_id = ?;
/* Delete doctor */
DELETE FROM DOCTORS WHERE doctor_id = ?;
DELETE FROM DOCTORS_LOCATIONS WHERE doctor_id = ?;
/* Delete location */
DELETE FROM LOCATIONS WHERE location_id = ?;
DELETE FROM DOCTORS_LOCATIONS WHERE location_id = ?;
/* Delete appointment */
DELETE FROM APPOINTMENTS WHERE appointment_id = ?;