-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.sql
136 lines (110 loc) · 3.49 KB
/
schema.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
CREATE SCHEMA erp;
CREATE SCHEMA audit;
CREATE SCHEMA support;
CREATE SCHEMA test;
-- Customers go in this table.
CREATE TABLE erp.customers (
id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
first_name text NOT NULL,
middle_name text,
last_name text,
marketing_consent boolean DEFAULT false NOT NULL
);
-- This is where we hold contact details for customers.
CREATE TABLE erp.customer_contact_details (
id bigint PRIMARY KEY REFERENCES erp.customers(id),
email text DEFAULT '' NOT NULL,
street_address text,
city text,
state text,
country text,
phone_no text
);
CREATE INDEX ON erp.customer_contact_details (email);
-- We represent order status by an enumeration.
CREATE TYPE erp.order_status AS ENUM (
'Placed',
'Fulfilled',
'Cancelled'
);
-- Order groups aggregate large orders for multiple items.
CREATE TABLE erp.order_groups (
id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
status erp.order_status,
placed_at timestamptz,
updated_at timestamptz,
customer bigint REFERENCES erp.customers(id)
);
-- Table to hold orders for individual items or services.
CREATE TABLE erp.orders (
id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
order_group bigint REFERENCES erp.order_groups(id),
status erp.order_status,
placed_at timestamptz,
updated_at timestamptz,
item integer,
service integer
);
-- Each invoice for an order group goes in here.
CREATE TABLE erp.invoices (
id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
amount numeric NOT NULL,
customer bigint REFERENCES erp.customers(id),
paid boolean DEFAULT false NOT NULL,
order_group bigint REFERENCES erp.order_groups(id),
updated_at timestamptz DEFAULT CURRENT_TIMESTAMP,
created_at timestamptz DEFAULT CURRENT_TIMESTAMP
);
-- We hold payments for specific invoices in here.
CREATE TABLE erp.payments (
id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
tstamp timestamptz NOT NULL,
amount numeric NOT NULL,
invoice bigint REFERENCES erp.invoices(id)
);
CREATE INDEX ON erp.payments (tstamp);
-- Our list of suppliers and their details.
CREATE TABLE erp.suppliers (
id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
company_name text,
state text,
country text,
phone_no text,
email text
);
-- We represent the type of each email sent by an enumeration.
CREATE TYPE erp.email_type AS ENUM (
'Invoice reminder',
'Welcome',
'Account closed',
'Happy birthday'
);
-- This table is the history of all emails sent out to customers.
CREATE TABLE erp.sent_emails (
tstamp timestamptz PRIMARY KEY DEFAULT CURRENT_TIMESTAMP,
customer bigint REFERENCES erp.customers(id),
email_type erp.email_type,
invoice bigint REFERENCES erp.invoices(id)
);
-- This table records energy usage readings for each of the branches.
CREATE TABLE erp.energy_usage (
branch_id integer NOT NULL,
reading_time timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
reading numeric NOT NULL,
unit varchar DEFAULT 'kWh' NOT NULL
);
-- This table holds customer service ticket details.
CREATE TABLE support.tickets (
id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
content text,
status smallint,
opened_at timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
closed_at timestamptz
);
-- Logging of user activity for audit purposes.
CREATE TABLE audit.audit_log (
id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
what text,
who text,
tstamp timestamptz
);