generated from microverseinc/curriculum-template-databases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema_based_on_diagram.sql
57 lines (45 loc) · 1.93 KB
/
schema_based_on_diagram.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
CREATE TABLE patients (
id int GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL PRIMARY KEY,
name varchar(50),
date_of_birth date
);
CREATE TABLE invoices (
id int GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL PRIMARY KEY,
total_amount decimal,
generated_at timestamp,
payed_at timestamp
);
CREATE TABLE invoice_items (
id int GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL PRIMARY KEY,
unit_price decimal,
quantity int,
total_price decimal
);
CREATE TABLE medical_histories (
id int GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL PRIMARY KEY,
admitted_at timestamp,
status varchar(50)
);
CREATE TABLE treatments (
id int GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL PRIMARY KEY,
type varchar(50),
name varchar(50)
);
ALTER TABLE invoices
ADD medical_history_id int REFERENCES medical_histories (id) ON UPDATE CASCADE;
ALTER TABLE invoice_items
ADD invoice_id int REFERENCES invoices (id) ON UPDATE CASCADE,
ADD treatment_id int REFERENCES treatments (id) ON UPDATE CASCADE;
ALTER TABLE medical_histories
ADD patient_id int REFERENCES patients (id) ON UPDATE CASCADE;
CREATE TABLE treatment_histories (
medical_histories_id int REFERENCES medical_histories (id) ON UPDATE CASCADE,
treatments_id int REFERENCES treatments (id) ON UPDATE CASCADE,
PRIMARY KEY (medical_histories_id, treatments_id)
);
CREATE INDEX idx_medical_histories_treatment_histories ON treatment_histories (medical_histories_id);
CREATE INDEX idx_treatments_treatment_histories ON treatment_histories (treatments_id);
CREATE INDEX idx_medical_histories_patients ON medical_histories (patient_id);
CREATE INDEX idx_invoices_medical_histories ON invoices (medical_history_id);
CREATE INDEX idx_invoice_items_invoices ON invoice_items (invoice_id);
CREATE INDEX idx_invoice_items_treaments ON invoice_items (treatment_id);