RelationalSchema: ddl (1).sql

File ddl (1).sql, 6.4 KB (added by 221128, 10 days ago)
Line 
1DROP TABLE IF EXISTS payments CASCADE;
2DROP TABLE IF EXISTS online_orders CASCADE;
3DROP TABLE IF EXISTS tab_orders CASCADE;
4DROP TABLE IF EXISTS order_items CASCADE;
5DROP TABLE IF EXISTS orders CASCADE;
6DROP TABLE IF EXISTS inventories CASCADE;
7DROP TABLE IF EXISTS products CASCADE;
8DROP TABLE IF EXISTS categories CASCADE;
9DROP TABLE IF EXISTS frontstaff_managed_reservations CASCADE;
10DROP TABLE IF EXISTS tables CASCADE;
11DROP TABLE IF EXISTS reservations CASCADE;
12DROP TABLE IF EXISTS assignments CASCADE;
13DROP TABLE IF EXISTS shifts CASCADE;
14DROP TABLE IF EXISTS managers CASCADE;
15DROP TABLE IF EXISTS back_staff CASCADE;
16DROP TABLE IF EXISTS front_staff CASCADE;
17DROP TABLE IF EXISTS staff_roles CASCADE;
18DROP TABLE IF EXISTS customers CASCADE;
19DROP TABLE IF EXISTS employees CASCADE;
20DROP TABLE IF EXISTS users CASCADE;
21
22
23CREATE TABLE IF NOT EXISTS users (
24 id BIGSERIAL PRIMARY KEY,
25 email VARCHAR(255) NOT NULL UNIQUE,
26 password VARCHAR(255) NOT NULL,
27 phone_number VARCHAR(50) UNIQUE,
28 street VARCHAR(255),
29 city VARCHAR(255)
30);
31
32CREATE TABLE IF NOT EXISTS employees (
33 user_id BIGSERIAL PRIMARY KEY,
34 net_salary DECIMAL(10,2),
35 gross_salary DECIMAL(10,2),
36 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE
37);
38
39CREATE TABLE IF NOT EXISTS customers (
40 user_id BIGSERIAL PRIMARY KEY,
41 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE
42);
43
44CREATE TABLE IF NOT EXISTS staff_roles (
45 id BIGSERIAL PRIMARY KEY,
46 name VARCHAR(255) NOT NULL
47);
48
49CREATE TABLE IF NOT EXISTS front_staff (
50 employee_id BIGSERIAL PRIMARY KEY,
51 tip_percent DECIMAL(10,2),
52 staff_role_id BIGINT NOT NULL,
53 FOREIGN KEY (employee_id) REFERENCES employees (user_id) ON DELETE CASCADE ON UPDATE CASCADE,
54 FOREIGN KEY (staff_role_id) REFERENCES staff_roles (id) ON DELETE CASCADE ON UPDATE CASCADE
55);
56
57CREATE TABLE IF NOT EXISTS back_staff (
58 employee_id BIGSERIAL PRIMARY KEY,
59 staff_role_id BIGINT NOT NULL,
60 FOREIGN KEY (employee_id) REFERENCES employees (user_id) ON DELETE CASCADE ON UPDATE CASCADE,
61 FOREIGN KEY (staff_role_id) REFERENCES staff_roles (id) ON DELETE CASCADE ON UPDATE CASCADE
62);
63
64CREATE TABLE IF NOT EXISTS managers (
65 employee_id BIGSERIAL PRIMARY KEY,
66 FOREIGN KEY (employee_id) REFERENCES employees (user_id) ON DELETE CASCADE ON UPDATE CASCADE
67);
68
69CREATE TABLE IF NOT EXISTS shifts (
70 id BIGSERIAL PRIMARY KEY,
71 date DATE NOT NULL,
72 start_time TIME NOT NULL,
73 end_time TIME NOT NULL,
74 manager_id BIGINT NOT NULL,
75 FOREIGN KEY (manager_id) REFERENCES managers (employee_id) ON DELETE CASCADE ON UPDATE CASCADE
76);
77
78CREATE TABLE IF NOT EXISTS assignments (
79 id BIGSERIAL PRIMARY KEY,
80 clock_in_time TIME,
81 clock_out_time TIME,
82 manager_id BIGINT NOT NULL,
83 employee_id BIGINT NOT NULL,
84 shift_id BIGINT NOT NULL,
85 FOREIGN KEY (manager_id) REFERENCES managers (employee_id) ON DELETE CASCADE ON UPDATE CASCADE,
86 FOREIGN KEY (employee_id) REFERENCES employees (user_id) ON DELETE CASCADE ON UPDATE CASCADE,
87 FOREIGN KEY (shift_id) REFERENCES shifts (id) ON DELETE CASCADE ON UPDATE CASCADE
88);
89
90CREATE TABLE IF NOT EXISTS tables (
91 table_number BIGSERIAL PRIMARY KEY,
92 capacity INT NOT NULL
93);
94
95CREATE TABLE IF NOT EXISTS reservations (
96 id BIGSERIAL PRIMARY KEY,
97 customer_id BIGINT NOT NULL,
98 datetime TIMESTAMP NOT NULL,
99 stay_length DECIMAL(10,2) NULL, --hours
100 FOREIGN KEY (customer_id) REFERENCES customers (user_id) ON DELETE CASCADE ON UPDATE CASCADE
101);
102
103CREATE TABLE IF NOT EXISTS frontstaff_managed_reservations (
104 id BIGSERIAL PRIMARY KEY,
105 reservation_id BIGINT NOT NULL,
106 front_staff_id BIGINT NOT NULL,
107 table_number BIGINT NOT NULL,
108 FOREIGN KEY (reservation_id) REFERENCES reservations (id) ON DELETE CASCADE ON UPDATE CASCADE,
109 FOREIGN KEY (front_staff_id) REFERENCES front_staff (employee_id) ON DELETE CASCADE ON UPDATE CASCADE,
110 FOREIGN KEY (table_number) REFERENCES tables (table_number) ON DELETE CASCADE ON UPDATE CASCADE
111);
112
113CREATE TABLE IF NOT EXISTS categories (
114 id BIGSERIAL PRIMARY KEY,
115 name VARCHAR(255) NOT NULL
116);
117
118CREATE TABLE IF NOT EXISTS products (
119 id BIGSERIAL PRIMARY KEY,
120 name VARCHAR(255) NOT NULL,
121 price DECIMAL(10,2) NOT NULL,
122 category_id BIGINT NOT NULL,
123 manage_inventory BOOLEAN NOT NULL DEFAULT FALSE,
124 FOREIGN KEY (category_id) REFERENCES categories (id) ON DELETE CASCADE ON UPDATE CASCADE
125);
126
127CREATE TABLE IF NOT EXISTS inventories (
128 product_id BIGSERIAL PRIMARY KEY,
129 quantity INT NOT NULL,
130 FOREIGN KEY (product_id) REFERENCES products (id) ON DELETE CASCADE ON UPDATE CASCADE
131);
132
133CREATE TABLE IF NOT EXISTS orders (
134 id BIGSERIAL PRIMARY KEY,
135 status VARCHAR(255) NOT NULL DEFAULT 'PENDING',
136 datetime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
137);
138
139CREATE TABLE IF NOT EXISTS order_items (
140 id BIGSERIAL PRIMARY KEY,
141 order_id BIGINT NOT NULL,
142 product_id BIGINT NOT NULL,
143 is_processed BOOLEAN NOT NULL DEFAULT FALSE,
144 quantity INT NOT NULL,
145 FOREIGN KEY (order_id) REFERENCES orders (id) ON DELETE CASCADE ON UPDATE CASCADE,
146 FOREIGN KEY (product_id) REFERENCES products (id) ON DELETE CASCADE ON UPDATE CASCADE
147);
148
149CREATE TABLE IF NOT EXISTS tab_orders (
150 order_id BIGSERIAL PRIMARY KEY,
151 front_staff_id BIGINT NOT NULL,
152 table_number BIGINT NOT NULL,
153 FOREIGN KEY (order_id) REFERENCES orders (id) ON DELETE CASCADE ON UPDATE CASCADE,
154 FOREIGN KEY (table_number) REFERENCES tables (table_number) ON DELETE CASCADE ON UPDATE CASCADE,
155 FOREIGN KEY (front_staff_id) REFERENCES front_staff (employee_id) ON DELETE CASCADE ON UPDATE CASCADE
156);
157
158
159
160CREATE TABLE IF NOT EXISTS online_orders (
161 order_id BIGSERIAL PRIMARY KEY,
162 delivery_address VARCHAR(255) NOT NULL,
163 customer_id BIGINT NOT NULL,
164 FOREIGN KEY (customer_id) REFERENCES customers (user_id) ON DELETE CASCADE ON UPDATE CASCADE,
165 FOREIGN KEY (order_id) REFERENCES orders (id) ON DELETE CASCADE ON UPDATE CASCADE
166);
167
168CREATE TABLE IF NOT EXISTS payments (
169 id BIGSERIAL PRIMARY KEY,
170 order_id BIGINT NOT NULL,
171 amount DECIMAL(10,2) NOT NULL,
172 timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
173 FOREIGN KEY (order_id) REFERENCES orders (id) ON DELETE CASCADE ON UPDATE CASCADE
174);