-
Notifications
You must be signed in to change notification settings - Fork 3
/
01_db_supermercado_DDL.sql
576 lines (374 loc) · 13.1 KB
/
01_db_supermercado_DDL.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/* ----------------------------
* ------ SUPERMERCADO ---------
* ----------------------------
*
*
* ========= DDL =============
*/
-- TABLAS
drop table if exists proveedores cascade;
drop table if exists productos cascade;
drop table if exists empleados cascade;
drop table if exists ventas cascade;
drop table if exists clientes cascade;
drop table if exists compras_clientes cascade;
drop table if exists ventas_productos cascade;
drop table if exists facturas cascade;
drop table if exists facturas_detalles cascade;
-- ENUMERADOS
-- Enumerados tabla facturas_detalles
drop type if exists tipo_factura_enum cascade;
drop type if exists tipo_pago_enum cascade;
-- Todos lo id PK auto_increment
drop sequence if exists id_sec_provee cascade;
drop sequence if exists id_sec_produc cascade;
drop sequence if exists id_sec_empl cascade;
drop sequence if exists id_sec_vent cascade;
drop sequence if exists id_sec_cli cascade;
drop sequence if exists id_sec_compr_cli cascade;
drop sequence if exists id_sec_vent_prod cascade;
drop sequence if exists id_sec_fact cascade;
drop sequence if exists id_sec_fact_det cascade;
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- =====================================
-- ======= TABLA PROVEEDORES ===========
-- =====================================
create table proveedores(
id int primary key,
empresa varchar(60) not null,-- Food S.A. , Alimentos Refrigerados S.A. , Tapalque Alimentos S.A. , etc
tipo_producto varchar(100) not null,
direccion varchar(60) not null,
nro_tel_princ varchar(40) not null,
nro_tel_sec varchar(40),
email varchar(60) not null
);
-- ======= Restricciones Tabla proveedores ===========
-- UNIQUE ID
alter table proveedores
add constraint UNIQUE_proveedores_id
unique(id);
-- CHECK EMPRESA | TIPO_PRODUCTOS | DIRECCION | NRO_TEL_PRINC | EMAIL
alter table proveedores
add constraint CHECK_proveedores_empresa_tipo_productos_direccion_nro_tel_princ_email
check((empresa <> '') and (tipo_producto <> '') and (direccion <> '') and
(nro_tel_princ <> '') and (nro_tel_sec <> '') and (email <> ''));
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ===================================
-- ======= TABLA EMPLEADOS ===========
-- ==================================
create table empleados(
id int primary key,
nombre varchar(30) not null,
apellido varchar(30) not null,
edad int not null,
fecha_nac date not null,
tipo_doc varchar(50) not null, -- Pasaporte, DNI, Etc
nro_doc varchar(20) not null,
cuil varchar(30) not null, -- Si no tiene DNI, cuil provisorio
direccion varchar(40) not null,
nro_tel_princ varchar(40) not null,
nro_tel_sec varchar(40),
email varchar(40),
cargo varchar(40) not null,
antiguedad varchar(30) not null,
fecha_ingreso date not null,
salario_anual decimal(10,2) not null
);
-- ======= Restricciones Tabla Empleados ===========
-- UNIQUE ID
alter table empleados
add constraint UNIQUE_empleados_id
unique(id);
-- UNIQUE NOMBRE/APELLIDO
alter table empleados
add constraint UNIQUE_empleados_nombre_apellido
unique(nombre,apellido);
-- CHECK EDAD
alter table empleados
add constraint CHECK_empleados_edad
check (edad >= 18);
-- CHECK FECHA_NACIMIENTO
alter table empleados
add constraint CHECK_empleados_fecha_nac
check (fecha_nac < current_date);
--UNIQUE TIPO_NRO_DOC
alter table empleados
add constraint UNIQUE_tipo_nro_doc
unique(tipo_doc, nro_doc);
--- UNIQUE NRO_DOC_CUIL
alter table empleados
add constraint UNIQUE_empleados_nro_doc_cuil
unique(nro_doc, cuil);
-- UNIQUE NRO_TEL
alter table empleados
add constraint UNIQUE_empleados_nro_tel_princ
unique(nro_tel_princ);
-- CHECK FECHA_INGR
alter table empleados
add constraint CHECK_empleados_fecha_ingreso
check (fecha_ingreso <= current_date);
-- CHECK SALARIO_ANUAL
alter table empleados
add constraint CHECK_empleados_salario_anual
check (salario_anual > 0);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ===================================
-- ======= TABLA CLIENTES ===========
-- ===================================
create table clientes(
id int primary key,
nombre varchar(30) not null,
apellido varchar(30) not null,
tipo_doc varchar(20) not null,
nro_doc varchar(20) not null,
nro_tel_princ varchar(40) not null,
nro_tel_sec varchar(40),
email varchar(40)
);
-- ======= Restricciones Tabla clientes ===========
-- UNIQUE ID
alter table clientes
add constraint UNIQUE_clientes_id
unique(id);
-- UNIQUE NOMBRE_APELLIDO
alter table clientes
add constraint UNIQUE_clientes_nombre_apellido
unique(nombre,apellido);
--- UNIQUE TIPO_NRO_DOCUMENTO
alter table clientes
add constraint UNIQUE_clientes_tipo_nro_documento
unique(nro_doc, tipo_doc);
-- UNIQUE TELEFONO
alter table clientes
add constraint UNIQUE_clientes_nro_tel_princ
unique(nro_tel_princ);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ===================================
-- ======= TABLA FACTURAS ===========
-- ===================================
create table facturas(
id int primary key,
numero varchar(200) not null,-- 00008
codigo varchar(200) not null, -- 00059374
fecha date not null,-- ej '2001-10-07'
hora time not null, -- ej '09:00:07'
importe_total decimal(10,2) not null -- + impuestos + costos + etc
);
-- ======= Restricciones Tabla facturas ===========
-- UNIQUE ID
alter table facturas
add constraint UNIQUE_facturas_id
unique(id);
-- CHECK NUMERO Y CODIGO
alter table facturas
add constraint CHECK_facturas_numero_codigo
check ((codigo <> '') and (numero <> ''));
-- CHECK FECHA
alter table facturas
add constraint CHECK_facturas_fecha
check ( (fecha <= current_date) or (fecha >= current_date));
-- CHECK HORA
alter table facturas
add constraint CHECK_facturas_hora
check ( (hora <= current_time) or (hora >= current_time));
--CHECK IMPORTE_TOTAL
alter table facturas
add constraint CHECK_facturas_importe_total
check(importe_total > 0);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ===================================
-- == TABLA FACTURAS_DETALLES ========
-- ===================================
-- Los enumerados deben estar declarados fuera de la creacion de tabla
create type tipo_factura_enum as enum('A','B','C','D');
create type tipo_pago_enum as enum('EFECTIVO','CHEQUE','TARJETA CREDITO', 'TARJETA DEBITO');
create table facturas_detalles(
id int primary key,
id_factura int not null,
tipo tipo_factura_enum not null, -- A,C ETC
descr_factura varchar(100) not null,--
costo_asoc decimal(8,2) not null, -- servicios, comisiones, etc
iva decimal(10,2) not null,
medio_de_pago tipo_pago_enum not null,
descr_pago varchar(100) not null
);
-- ======= Restricciones Tabla facturas_detalles ===========
-- UNIQUE ID
alter table facturas_detalles
add constraint UNIQUE_facturas_detalles_id
unique(id);
-- UNIQUE ID_FACTURA
alter table facturas_detalles
add constraint UNIQUE_facturas_detalles_id_factura
unique(id_factura);
-- FK ID_FACTURA
alter table facturas_detalles
add constraint FK_facturas_detalles_id_factura
foreign key(id_factura)
references facturas(id) on delete cascade;
-- CHECK COSTO_ASOCIADO
alter table facturas_detalles
add constraint CHECK_facturas_detalles_costo_asoc
check( costo_asoc > 0);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ===================================
-- ======= TABLA PRODUCTOS ===========
-- ===================================
create table productos(
id int primary key,
id_proveedor int not null,
codigo varchar(100) not null,
imagen varchar(600) ,
nombre varchar(100) not null,
marca varchar(60) not null,
tipo varchar(60) not null,-- bebidas, almacen, carnes y pescados, frutas y verduras, etc
grupo varchar(60) not null, -- Vinos, Gaseosas, etc
peso decimal(8,3) not null,
precio_unidad decimal(8,3) not null,
stock smallint not null
);
-- ======= Restricciones Tabla productos ===========
-- UNIQUE ID
alter table productos
add constraint UNIQUE_productos_id
unique(id);
--UNIQUE CODIGO_NOMBRE
alter table productos
add constraint UNIQUE_productos_codigo_nombre
unique(codigo, nombre);
-- FK ID_PROVEEDOR
alter table productos
add constraint FK_productos_id_proveedor
foreign key(id_proveedor)
references proveedores(id) on delete cascade;
-- UNIQUE CÓDIGO
alter table productos
add constraint UNIQUE_productos_codigo
unique(codigo);
-- CHECK PESO
alter table productos
add constraint CHECK_productos_peso
check(peso > 0);
-- CHECK PRECIO_UNIDAD
alter table productos
add constraint CHECK_productos_precio_unidad
check(precio_unidad > 0);
-- CHECK STOCK
alter table productos
add constraint CHECK_productos_stock
check(stock > 0);
-- CHECK NOMBRE | MARCA | TIPO | GRUPO
alter table productos
add constraint CHECK_nombre_marca_tipo_grupo
check((nombre <> '') and (marca <> '') and (tipo <> '') and (grupo <> ''));
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ================================
-- ======= TABLA VENTAS ===========
-- ================================
create table ventas(
id int primary key,
id_empleado int not null,
id_factura int not null
);
-- ======= Restricciones Tabla ventas ===========
-- UNIQUE ID
alter table ventas
add constraint UNIQUE_ventas_id
unique(id);
-- FK ID_EMPLEADO
alter table ventas
add constraint FK_ventas_id_empleado
foreign key(id_empleado)
references empleados(id) on delete cascade;
-- FK ID_FACTURA
alter table ventas
add constraint FK_ventas_id_factura
foreign key(id_factura)
references facturas(id) on delete cascade;
-- UNIQUE ID_FACTURA
alter table ventas
add constraint UNIQUE_ventas_id_factura
unique(id_factura);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ===================================
-- ======= TABLA VENTAS_PRODUCTOS ===========
-- ===================================
create table ventas_productos(
id int primary key,
id_venta int not null,
id_producto int not null,
cantidad int not null
);
-- ======= Restricciones Tabla ventas_productos ===========
-- UNIQUE ID
alter table ventas_productos
add constraint UNIQUE_ventas_productos_id
unique(id);
-- FK ID_VENTA
alter table ventas_productos
add constraint FK_ventas_productos_id_venta
foreign key(id_venta)
references ventas(id) on delete cascade;
-- FK ID_PRODUCTO
alter table ventas_productos
add constraint FK_ventas_productos_id_producto
foreign key(id_producto)
references productos(id) on delete cascade;
--CHECK CANTIDAD
alter table ventas_productos
add constraint CHECK_ventas_productos_cantidad
check(cantidad > 0);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ===================================
-- ======= TABLA COMPRAS_CLIENTES ===========
-- ===================================
create table compras_clientes(
id int primary key,
id_venta int not null,
id_cliente int not null
);
-- ======= Restricciones Tabla compras_clientes ===========
-- UNIQUE ID
alter table compras_clientes
add constraint UNIQUE_compras_clientes_id
unique(id);
-- FK ID_VENTA
alter table compras_clientes
add constraint FK_compras_clientes_id_venta
foreign key(id_venta)
references ventas(id) on delete cascade;
-- FK ID_CLIENTE
alter table compras_clientes
add constraint FK_compras_clientes_id_cliente
foreign key(id_cliente)
references clientes(id) on delete cascade;
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======== TODOS LOS ID´S PK DE LAS TABLAS COMO AUTO_INCREMENT =======
create sequence id_sec_provee ;
create sequence id_sec_produc;
create sequence id_sec_empl;
create sequence id_sec_vent ;
create sequence id_sec_cli ;
create sequence id_sec_compr_cli ;
create sequence id_sec_vent_prod;
create sequence id_sec_fact ;
create sequence id_sec_fact_det ;
alter table proveedores alter id set default nextval('id_sec_provee');
alter table productos alter id set default nextval('id_sec_produc');
alter table empleados alter id set default nextval('id_sec_empl');
alter table ventas alter id set default nextval('id_sec_vent');
alter table clientes alter id set default nextval('id_sec_cli');
alter table compras_clientes alter id set default nextval('id_sec_compr_cli');
alter table ventas_productos alter id set default nextval('id_sec_vent_prod');
alter table facturas alter id set default nextval('id_sec_fact');
alter table facturas_detalles alter id set default nextval('id_sec_fact_det');