generated from microverseinc/curriculum-template-databases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema_based_on_diagram.sql
51 lines (44 loc) · 1.45 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
CREATE DATABASE clinic;
CREATE TABLE patients (
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name VARCHAR(50),
date_of_birth DATE
);
CREATE TABLE medical_histories (
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
patient_id INT REFERENCES patients(id),
status VARCHAR(50),
admitted_at TIMESTAMP,
);
CREATE TABLE invoices (
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
total_amount DECIMAL(10, 2),
generate_at TIMESTAMP,
payed_at TIMESTAMP,
medical_history_id INT REFERENCES medical_histories(id)
);
CREATE TABLE treatments (
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name VARCHAR(50),
type VARCHAR(50)
);
CREATE TABLE medical_history_treatments (
medical_history_id INT REFERENCES medical_histories(id),
treatment_id INT REFERENCES treatments(id),
PRIMARY KEY (medical_history_id, treatment_id)
);
CREATE TABLE invoice_items (
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
unit_price DECIMAL(10, 2),
quantity INT,
total_price DECIMAL(10, 2),
invoice_id INT REFERENCES invoices(id),
treatment_id INT REFERENCES treatments(id)
);
/*-- Path: data.sql */
CREATE INDEX ON medical_histories (patient_id);
CREATE INDEX ON invoices (medical_history_id);
CREATE INDEX ON invoice_items (invoice_id);
CREATE INDEX ON invoice_items (treatment_id);
CREATE INDEX ON medical_history_treatments (medical_history_id);
CREATE INDEX ON medical_history_treatments (treatment_id);