generated from microverseinc/curriculum-template-databases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema_based_on_digram.sql
54 lines (47 loc) · 1.31 KB
/
schema_based_on_digram.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
-- Creating tables & establishing relationships
CREATE TABLE patients (
id serial NOT NULL ,
name VARCHAR(250),
date_of_birth DATE,
PRIMARY KEY (id)
);
CREATE TABLE treatments (
id serial NOT NULL ,
type VARCHAR(250),
name VARCHAR(250),
PRIMARY KEY (id)
);
CREATE TABLE medical_histories (
id serial NOT NULL,
admitted_at TIMESTAMP,
patient_id INTEGER REFERENCES patients(id),
STATUS VARCHAR(250),
PRIMARY KEY (id),
FOREIGN KEY (patient_id) REFERENCES patients(id)
);
CREATE TABLE invoices (
id serial NOT NULL,
total_amount DECIMAL,
generated_at TIMESTAMP,
payed_at TIMESTAMP,
medical_history__id INT,
PRIMARY KEY (id),
FOREIGN KEY (medical_history__id) REFERENCES medical_histories(id)
);
CREATE TABLE invoice_items (
id serial NOT NULL,
unit_price DECIMAL,
quantity INT,
total_price DECIMAL,
invoice_id INT,
treatment_id INT,
PRIMARY KEY (id),
FOREIGN KEY (invoice_id) REFERENCES invoices(id),
FOREIGN KEY (treatment_id) REFERENCES treatments(id)
);
-- Join tables that have many to many relationships
CREATE TABLE medical_treatments (
medical_histories_id INT REFERENCES medical_histories(id),
treatments_id INT REFERENCES treatments(id),
PRIMARY KEY(medical_histories_id, treatments_id)
);