-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_tables.sql
483 lines (413 loc) · 11 KB
/
create_tables.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use meeni_erp_db;
go
------------
-- IMAGES --
------------
-- (dummy data) --
print '';
print 'Creating images table...';
go
create table
images (
image_id int identity (1, 1) not null,
url varchar(300) not null,
constraint uq_image unique (url),
primary key (image_id)
);
go
---------------
-- COUNTRIES --
---------------
-- (initial data) --
print '';
print 'Creating countries table...';
go
create table
countries (
country_id int identity (1, 1) not null,
name varchar(50) not null,
constraint uq_country unique (name),
primary key (country_id)
);
go
---------------
-- PROVINCES --
---------------
-- (initial data) --
print '';
print 'Creating provinces table...';
go
create table
provinces (
province_id int identity (1, 1) not null,
name varchar(50) not null,
country_id int not null,
constraint uq_province unique (name, country_id),
primary key (province_id),
foreign key (country_id) references countries (country_id)
);
go
------------
-- CITIES --
------------
-- (dummy data) --
print '';
print 'Creating cities table...';
go
create table
cities (
city_id int identity (1, 1) not null,
name varchar(50) not null,
zip_code varchar(10) null,
province_id int not null,
constraint uq_city unique (name, province_id),
primary key (city_id),
foreign key (province_id) references provinces (province_id)
);
go
---------------
-- ADDRESSES --
---------------
-- (dummy data) --
print '';
print 'Creating addresses table...';
go
create table
addresses (
address_id int identity (1, 1) not null,
street_name varchar(50) not null,
street_number varchar(10) not null,
flat varchar(10) null,
details varchar(300) null,
city_id int not null,
constraint uq_address unique (street_name, street_number, flat, city_id),
primary key (address_id),
foreign key (city_id) references cities (city_id)
);
go
--------------------------
-- IDENTIFICATION TYPES --
--------------------------
-- (initial data) --
print '';
print 'Creating identification_types table...';
go
create table
identification_types (
identification_type_id int identity (1, 1) not null,
name varchar(50) not null,
country_id int null,
constraint uq_identification_type unique (name),
primary key (identification_type_id),
foreign key (country_id) references countries (country_id)
);
go
---------------------
-- IDENTIFICATIONS --
---------------------
-- (dummy data) --
print '';
print 'Creating identifications table...';
go
create table
identifications (
identification_id int identity (1, 1) not null,
code varchar(50) not null,
identification_type_id int not null,
primary key (identification_id),
foreign key (identification_type_id) references identification_types (identification_type_id)
);
go
--------------
-- ENTITIES --
--------------
-- (dummy data) --
print '';
print 'Creating entities table...';
go
create table
entities (
entity_id int identity (1, 1) not null,
name varchar(50) not null,
is_organization bit default (0) not null,
email varchar(50) null,
phone varchar(50) null,
birth_date date null,
image_id int null,
address_id int null,
identification_id int null,
primary key (entity_id),
foreign key (image_id) references images (image_id),
foreign key (address_id) references addresses (address_id),
foreign key (identification_id) references identifications (identification_id)
);
go
-------------------
-- PRICING PLANS --
-------------------
-- (initial data) --
print '';
print 'Creating pricing_plans table...';
go
create table
pricing_plans (
pricing_plan_id int identity (1, 1) not null,
name varchar(50) not null,
monthly_fee money not null,
constraint uq_pricing_plan unique (name),
constraint chk_plan_fee check (0 <= monthly_fee),
primary key (pricing_plan_id)
);
go
-------------------
-- ORGANIZATIONS --
-------------------
-- (dummy data) --
print '';
print 'Creating organizations table...';
go
create table
organizations (
organization_id int not null,
activity_status bit default (1) not null,
admission_date date default cast(getdate () as date) not null,
pricing_plan_id int not null,
primary key (organization_id),
foreign key (pricing_plan_id) references pricing_plans (pricing_plan_id),
foreign key (organization_id) references entities (entity_id)
);
go
------------------
-- STAKEHOLDERS --
------------------
-- (dummy data) --
print '';
print 'Creating stakeholders table...';
go
create table
stakeholders (
stakeholder_id int not null,
organization_id int not null,
primary key (stakeholder_id),
foreign key (stakeholder_id) references entities (entity_id),
foreign key (organization_id) references organizations (organization_id)
);
go
--------------
-- PARTNERS --
--------------
-- (dummy data) --
print '';
print 'Creating partners table...';
go
create table
partners (
partner_id int not null,
activity_status bit default (1) not null,
is_client bit not null,
is_supplier bit not null,
constraint chk_is_any check (
is_client is not null
or is_supplier is not null
),
primary key (partner_id),
foreign key (partner_id) references stakeholders (stakeholder_id)
);
go
---------------
-- EMPLOYEES --
---------------
-- (dummy data) --
print '';
print 'Creating employees table...';
go
create table
employees (
employee_id int not null,
activity_status bit default (1) not null,
admission_date date default cast(getdate () as date) not null,
primary key (employee_id),
foreign key (employee_id) references stakeholders (stakeholder_id)
);
go
-----------
-- ROLES --
-----------
-- (initial data) --
print '';
print 'Creating roles table...';
go
create table
roles (
role_id int identity (1, 1) not null,
name varchar(20) not null,
constraint uq_role unique (name),
primary key (role_id)
);
go
-----------
-- USERS --
-----------
-- (dummy data) --
print '';
print 'Creating users table...';
go
create table
users (
user_id int not null,
username varchar(50) not null,
password varchar(50) not null,
constraint uq_user unique (username),
primary key (user_id),
foreign key (user_id) references employees (employee_id)
);
go
-------------------------
-- USER-ROLE RELATIONS --
-------------------------
-- (dummy data) --
print '';
print 'Creating user_role_rel table...';
go
create table
user_role_rel (
user_id int not null,
role_id int not null,
primary key (user_id, role_id),
foreign key (user_id) references users (user_id),
foreign key (role_id) references roles (role_id)
);
go
------------
-- BRANDS --
------------
-- (dummy data) --
print '';
print 'Creating brands table...';
go
create table
brands (
brand_id int identity (1, 1) not null,
activity_status bit default (1) not null,
name varchar(50) not null,
organization_id int not null,
constraint uq_brand unique (name, organization_id),
primary key (brand_id),
foreign key (organization_id) references organizations (organization_id)
);
go
--------------
-- PRODUCTS --
--------------
-- (dummy data) --
print '';
print 'Creating products table...';
go
create table
products (
product_id int identity (1, 1) not null,
activity_status bit default (1) not null,
is_service bit default (0) not null,
name varchar(50) not null,
description varchar(300) null,
sku varchar(50) null,
price money not null,
cost money not null,
brand_id int null,
organization_id int not null,
constraint chk_price check (0 < price),
constraint chk_cost check (0 <= cost),
primary key (product_id),
foreign key (brand_id) references brands (brand_id),
foreign key (organization_id) references organizations (organization_id)
);
go
----------------
-- CATEGORIES --
----------------
-- (dummy data) --
print '';
print 'Creating categories table...';
go
create table
categories (
category_id int identity (1, 1) not null,
activity_status bit default (1) not null,
name varchar(50) not null,
organization_id int not null,
constraint uq_category unique (name, organization_id),
primary key (category_id),
foreign key (organization_id) references organizations (organization_id)
);
go
--------------------------------
-- PRODUCT-CATEGORY RELATIONS --
--------------------------------
-- (dummy data) --
print '';
print 'Creating product_category_rel table...';
go
create table
product_category_rel (
product_id int not null,
category_id int not null,
primary key (product_id, category_id),
foreign key (product_id) references products (product_id),
foreign key (category_id) references categories (category_id)
);
go
-----------------------------
-- PRODUCT-IMAGE RELATIONS --
-----------------------------
-- (dummy data) --
print '';
print 'Creating product_image_rel table...';
go
create table
product_image_rel (
product_id int not null,
image_id int not null,
primary key (product_id, image_id),
foreign key (product_id) references products (product_id),
foreign key (image_id) references images (image_id)
);
go
----------------
-- WAREHOUSES --
----------------
-- (dummy data) --
print '';
print 'Creating warehouses table...';
create table
warehouses (
warehouse_id int identity (1, 1) not null,
activity_status bit default (1) not null,
name varchar(50) not null,
address_id int null,
organization_id int not null,
constraint uq_warehouse unique (name, organization_id),
primary key (warehouse_id),
foreign key (address_id) references addresses (address_id),
foreign key (organization_id) references organizations (organization_id)
);
go
------------------
-- COMPARTMENTS --
------------------
-- (dummy data) --
print '';
print 'Creating compartments table...';
create table
compartments (
compartment_id int identity (1, 1) not null,
activity_status bit default (1) not null,
name varchar(50) not null,
stock int default (0) not null,
product_id int not null,
warehouse_id int not null,
constraint uq_compartment unique (name, warehouse_id),
constraint chk_stock check (0 <= stock),
primary key (compartment_id),
foreign key (warehouse_id) references warehouses (warehouse_id)
);
go