-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path01_db_inmobiliaria_DDL.sql
More file actions
1208 lines (811 loc) · 30.9 KB
/
01_db_inmobiliaria_DDL.sql
File metadata and controls
1208 lines (811 loc) · 30.9 KB
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ----------------------------
* ------ INMOBILIARIA---------
* ----------------------------
*
*
* ========= DDL =============
*/
-- Tablas
drop table if exists inmuebles cascade;
drop table if exists inmuebles_descripciones cascade;
drop table if exists inmuebles_medidas cascade;
drop table if exists inmuebles_marketing cascade;
drop table if exists inspecciones_inmuebles cascade;
drop table if exists citas_inmuebles cascade;
drop table if exists propietarios_inmuebles cascade;
drop table if exists facturas cascade;
drop table if exists facturas_detalles cascade;
drop table if exists ventas cascade;
drop table if exists administradores cascade; -- planeacion, organizacion, etc
drop table if exists gerentes cascade;-- metas y procedimientos, control administracion
drop table if exists vendedores cascade;-- ventas inmuebles, entrevistas, etc
drop table if exists compradores cascade;
drop table if exists clientes cascade;
drop table if exists empleados cascade;
drop table if exists oficinas cascade;
drop table if exists oficinas_detalles cascade;
drop table if exists servicios_oficinas cascade;
-- Todos lo id PK auto_increment
drop sequence if exists id_sec_of cascade;
drop sequence if exists id_sec_serv_of cascade;
drop sequence if exists id_sec_of_det cascade;
drop sequence if exists id_sec_inm cascade;
drop sequence if exists id_sec_inm_descr cascade;
drop sequence if exists id_sec_inm_med cascade;
drop sequence if exists id_sec_inm_mark cascade;
drop sequence if exists id_sec_insp_inm cascade;
drop sequence if exists id_sec_cit_inm cascade;
drop sequence if exists id_sec_prop_inm cascade;
drop sequence if exists id_sec_empl cascade;
drop sequence if exists id_sec_cli cascade;
drop sequence if exists id_sec_adm cascade;
drop sequence if exists id_sec_ger cascade;
drop sequence if exists id_sec_vend cascade;
drop sequence if exists id_sec_comp cascade;
drop sequence if exists id_sec_vent cascade;
drop sequence if exists id_sec_fact cascade;
drop sequence if exists id_sec_fact_det cascade;
-- Enumerados tabla oficinas_detalles
drop type if exists estado_oficina_enum cascade;
drop type if exists tipo_oficina_enum cascade;
-- Enumerados tabla servicios_oficinas
drop type if exists division_comercial_enum cascade;
drop type if exists division_vivienda_enum cascade;
drop type if exists tasaciones_enum cascade;
drop type if exists administracion_enum cascade;
-- Enumerados tabla inspecciones_inmuebles
drop type if exists tipo_inspeccion_enum cascade;
drop type if exists estado_inspeccion_enum cascade;
-- Enumerados tabla inmuebles
drop type if exists estado_inmueble_enum cascade;
-- Enumerados tabla citas_inmuebles
drop type if exists estado_cita_enum cascade;
-- Enumerados tabla facturas_detalles
drop type if exists tipo_factura_enum cascade;
drop type if exists tipo_pago_enum cascade;
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA OFICINAS ===========
create table oficinas(
id int primary key,
nombre varchar(30) not null,
direccion varchar(60) not null,
nro_telefono varchar(40) not null,
email varchar(60)
);
-- ======= Restricciones Tabla Oficinas ===========
-- UNIQUE ID
alter table oficinas
add constraint UNIQUE_oficinas_id
unique(id);
-- UNIQUE DIRECCION
alter table oficinas
add constraint UNIQUE_oficinas_direccion
unique(direccion);
-- UNIQUE TELEFONO
alter table oficinas
add constraint UNIQUE_oficinas_nro_telefono
unique(nro_telefono);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA OFICINAS_DETALLES ===========
create type estado_oficina_enum as enum('ALQUILADA','PROPIA');
create type tipo_oficina_enum as enum('PEQUEÑA','ESTANDAR','EJECUTIVA');
-- select * from pg_enum;
create table oficinas_detalles(
id int primary key,
id_oficina int not null,
localidad varchar(40) not null,
tipo_oficina tipo_oficina_enum not null,
estado_oficina estado_oficina_enum not null,
superficie_total decimal(8,2) not null,
cantidad_ambientes int not null, -- 1,2,3,etc | smallint-->2bytes, int-->4bytes |
cantidad_sanitarios int not null, -- Reemplazamos baños por caracter especial
antiguedad int, -- 20 años, etc
sitio_web varchar(200)
);
-- ======= Restricciones Tabla oficinas_detalles ===========
-- UNIQUE ID
alter table oficinas_detalles
add constraint UNIQUE_oficinas_detalles_id
unique(id);
-- UNIQUE ID_OFICINA
alter table oficinas_detalles
add constraint UNIQUE_oficinas_detalles_id_oficina
unique(id_oficina);
-- FK ID_OFICINA
alter table oficinas_detalles
add constraint FK_oficinas_detalles_id_oficina
foreign key(id_oficina)
references oficinas(id);
-- CHECK SUPERFICIE_TOTAL
alter table oficinas_detalles
add constraint CHECK_oficinas_detalles_superficie_total
check (superficie_total > 0 );
-- CHECK CANTIDAD AMBIENTES
alter table oficinas_detalles
add constraint CHECK_oficinas_detalles_cantidad_ambientes
check (cantidad_ambientes > 0 );
-- CHECK CANTIDAD SANITARIOS
alter table oficinas_detalles
add constraint CHECK_oficinas_detalles_cantidad_sanitarios
check (cantidad_sanitarios > 0 );
-- CHECK ANTIGUEDAD
alter table oficinas_detalles
add constraint CHECK_oficinas_detalles_antiguedad
check (antiguedad >= 0 or antiguedad = null ); -- Puede ser nulleable
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA SERVICIOS_OFICINAS ===========
-- https://www.mosquerabrokers.com.ar/es/informacion/servicios
create type division_comercial_enum as enum('LOCALES','OFICINAS','TERRENOS'
,'LOCALES_OFICINAS_TERRENOS','NO_APLICA');
create type division_vivienda_enum as enum('DEPARTAMENTOS','CASAS','TERRENOS'
,'DEPARTAMENTOS_CASAS_TERRENOS','NO_APLICA');
create type tasaciones_enum as enum('PROFESIONAL','JUDICIAL','PROFESIONAL_JUDICIAL'
,'NO_APLICA');
create type administracion_enum as enum('ALQUILERES','CUENTAS','ALQUILERES_CUENTAS'
,'NO_APLICA');
create table servicios_oficinas(
id int primary key,
id_oficina int not null,
tipo_comercial division_comercial_enum not null,
tipo_vivienda division_vivienda_enum not null,
tipo_tasaciones tasaciones_enum not null,
tipo_administracion administracion_enum not null,
descripcion_servicios varchar(200)
);
-- ======= Restricciones Tabla servicios_oficinas ===========
-- UNIQUE ID
alter table servicios_oficinas
add constraint UNIQUE_servicios_oficinas_id
unique(id);
-- FK ID_OFICINA
alter table servicios_oficinas
add constraint FK_servicios_oficinas_id_oficina
foreign key(id_oficina)
references oficinas(id);
-- UNIQUE ID_OFICINA
alter table servicios_oficinas
add constraint UNIQUE_servicios_oficinas_id_oficina
unique(id_oficina);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA PROPIETARIOS_INMUEBLES ===========
create table propietarios_inmuebles(
id int primary key,
nombre varchar(40) not null,
apellido varchar(40) not null,
edad int not null,
fecha_nacimiento date not null,
tipo_documento varchar(20) not null,
nro_documento varchar(20) not null,
direccion varchar(40) not null,
nro_telefono_principal varchar(40) not null,
nro_telefono_secundario varchar(40),
email varchar(40)
);
-- ======= Restricciones Tabla propietarios_inmuebles ===========
-- UNIQUE ID
alter table propietarios_inmuebles
add constraint UNIQUE_propietarios_inmuebles_id
unique(id);
-- UNIQUE NOMBRE/APELLIDO
alter table propietarios_inmuebles
add constraint UNIQUE_propietarios_inmuebles_nombre_apellido
unique(nombre,apellido);
-- CHECK EDAD
alter table propietarios_inmuebles
add constraint CHECK_propietarios_inmuebles_edad
check (edad >= 18);
-- UNIQUE TELEFONO
alter table propietarios_inmuebles
add constraint UNIQUE_propietarios_inmuebles_nro_telefono_principal
unique(nro_telefono_principal);
-- CHECK FECHA_NACIMIENTO
alter table propietarios_inmuebles
add constraint CHECK_propietarios_inmuebles_fecha_nacimiento
check (current_date > fecha_nacimiento);
--- UNIQUE NRO_DOCUMENTO
alter table propietarios_inmuebles
add constraint UNIQUE_propietarios_inmuebles_nro_documento
unique(nro_documento);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA EMPLEADOS ===========
create table empleados(
id int primary key,
id_oficina int not null,
nombre varchar(30) not null,
apellido varchar(30) not null,
edad int not null,
fecha_nacimiento date not null,
tipo_documento varchar(50) not null, -- Pasaporte, LE(libreta enrolamiento),LC(libreta civica)
nro_documento varchar(20) not null,
cuil varchar(30) not null, -- Si no tiene DNI, cuil provisorio
direccion varchar(40) not null,
nro_telefono_principal varchar(40) not null,
nro_telefono_secundario varchar(40),
email varchar(40),
cargo varchar(40) not null,
antiguedad int,
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);
-- FK ID_OFICINA
alter table empleados
add constraint FK_empleados_id_oficina
foreign key(id_oficina)
references oficinas(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 ANTIGUEDAD
alter table empleados
add constraint CHECK_empleados_antiguedad
check (antiguedad >= 0 or antiguedad=null);
--- UNIQUE NRO_DOCUMENTO
alter table empleados
add constraint UNIQUE_empleados_nro_documento
unique(nro_documento);
-- UNIQUE TELEFONO
alter table empleados
add constraint UNIQUE_empleados_nro_telefono_principal
unique(nro_telefono_principal);
-- CHECK FECHA_NACIMIENTO Y FECHA_INGRESO
alter table empleados
add constraint CHECK_empleados_fecha_nacimiento_ingreso
check (current_date > fecha_nacimiento and current_date >= fecha_ingreso );
-- CHECK SALARIO_ANUAL
alter table empleados
add constraint CHECK_empleados_salario_anual
check (salario_anual > 300);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA CLIENTES ===========
create table clientes(
id int primary key,
nombre varchar(30) not null,
apellido varchar(30) not null,
edad int not null,
fecha_nacimiento date not null,
tipo_documento varchar(20) not null,
nro_documento varchar(20) not null,
direccion varchar(40) not null,
nro_telefono_principal varchar(40) not null,
nro_telefono_secundario varchar(40),
email varchar(40),
fecha_alta date not null
);
-- ======= 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);
-- CHECK EDAD
alter table clientes
add constraint CHECK_clientes_edad
check (edad >= 18);
-- UNIQUE TELEFONO
alter table clientes
add constraint UNIQUE_clientes_nro_telefono_principal
unique(nro_telefono_principal);
--- UNIQUE NRO_DOCUMENTO
alter table clientes
add constraint UNIQUE_clientes_nro_documento
unique(nro_documento);
-- CHECK FECHA_NACIMIENTO Y FECHA_ALTA
alter table clientes
add constraint CHECK_clientes_fecha_nacimiento_alta
check (current_date > fecha_nacimiento and current_date >= fecha_alta );
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA INMUEBLES DESCRIPCIONES ===========
create table inmuebles_descripciones(
id int primary key,
superficie_total decimal(8,2) not null,-- ej: 92 m^2 Total.
superficie_cubierta decimal(8,2) not null,-- ej: 72 m^2 Total.
cantidad_ambientes smallint not null, -- 1,2,3,etc | smallint-->2bytes, int-->4bytes |
cantidad_dormitorios smallint not null,-- 1,2,3
cantidad_sanitarios smallint not null,-- 1,2,3 reemplazamos baños, por el caracter especial
cantidad_patios_jardines smallint , -- patio casa, jardin piso/semipiso
cantidad_cocheras smallint ,-- si no tiene 0 o null
cantidad_balcones smallint ,-- si no tiene 0 o null
antiguedad smallint -- 20 años, etc
);
-- ======= Restricciones Tabla Inmuebles Descripción ===========
-- UNIQUE ID
alter table inmuebles_descripciones
add constraint UNIQUE_inmuebles_descripciones_id
unique(id);
-- CHECK SUPERFICIE_TOTAL
alter table inmuebles_descripciones
add constraint CHECK_inmuebles_descripciones_superficie_total
check (superficie_total > 0 );
-- CHECK SUPERFICIE_CUBIERTA
alter table inmuebles_descripciones
add constraint CHECK_inmuebles_descripciones_superficie_cubierta
check (superficie_total >= superficie_cubierta );
-- CHECK CANTIDAD AMBIENTES
alter table inmuebles_descripciones
add constraint CHECK_inmuebles_descripciones_cantidad_ambientes
check (cantidad_ambientes > 0 );
-- CHECK CANTIDAD DORMITORIOS
alter table inmuebles_descripciones
add constraint CHECK_inmuebles_descripciones_cantidad_dormitorios
check (cantidad_dormitorios > 0 );
-- CHECK CANTIDAD SANITARIOS
alter table inmuebles_descripciones
add constraint CHECK_inmuebles_descripciones_cantidad_sanitarios
check (cantidad_sanitarios > 0 );
-- CHECK CANTIDAD PATIOS/JARDINES
alter table inmuebles_descripciones
add constraint CHECK_inmuebles_descripciones_cantidad_patios_jardines
check (cantidad_patios_jardines >= 0 or cantidad_patios_jardines = null );
-- CHECK CANTIDAD COCHERAS
alter table inmuebles_descripciones
add constraint CHECK_inmuebles_descripciones_cantidad_cocheras
check (cantidad_cocheras >= 0 or cantidad_cocheras = null ); -- Puede ser nulleable
-- CHECK CANTIDAD BALCONES
alter table inmuebles_descripciones
add constraint CHECK_inmuebles_descripciones_cantidad_balcones
check (cantidad_balcones >= 0 or cantidad_balcones = null ); -- Puede ser nulleable
-- CHECK ANTIGUEDAD
alter table inmuebles_descripciones
add constraint CHECK_inmuebles_descripciones_antiguedad
check (antiguedad >= 0 or antiguedad = null ); -- Puede ser nulleable
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA INMUEBLES MEDIDAS ===========
-- Pueden no estar estipuladas
create table inmuebles_medidas(-- ALTO X ANCHO
id int primary key,
living_comedor varchar(100),-- ej: 8,05 x 3,4
cocina varchar(100), -- Cocina 1: 3,25 x 1,55
dormitorio varchar(100), -- Dormitorio 1: 23 x 2,9 | Dormitorio 2: 43 x 1,9
sanitario varchar(100),
patio_jardin varchar(100),
cochera varchar(50),
balcon varchar(50)
);
-- ======= Restricciones Tabla inmuebles_medidas ===========
-- UNIQUE ID
alter table inmuebles_medidas
add constraint UNIQUE_inmuebles_medidas_id
unique(id);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA INMUEBLES ===========
create type estado_inmueble_enum as enum('VENDIDO','DISPONIBLE','NO_DISPONIBLE'
,'FALTA_INSPECCION');
create table inmuebles(
id int primary key,
id_propietario_inmueble int not null,
id_inmueble_medidas int not null,
id_inmueble_descripcion int not null,
id_oficina int not null,
descripcion varchar(200) not null,-- ej: semipiso de 3 Amb en Palermo
tipo varchar(20) not null, -- depto, casa, etc
estado_inmueble estado_inmueble_enum not null,
precio_inmueble_usd decimal(10,2) not null,
direccion varchar(60) not null,-- San sarasa 123
ubicacion varchar(60) not null, -- zona:palermo, recoleta, etc
sitio_web varchar(60)-- link de la pag de la descripcion
);
-- ======= Restricciones Tabla inmuebles ===========
-- UNIQUE ID
alter table inmuebles
add constraint UNIQUE_inmuebles_id
unique(id);
-- UNIQUE ID_INMUEBLE_MEDIDAS
alter table inmuebles
add constraint UNIQUE_inmuebles_id_inmueble_medidas
unique(id_inmueble_medidas);
-- UNIQUE ID_INMUEBLE_DESCRIPCION
alter table inmuebles
add constraint UNIQUE_inmuebles_id_inmueble_descripcion
unique(id_inmueble_descripcion);
-- FK ID_INMUEBLES_MEDIDAS
alter table inmuebles
add constraint FK_inmuebles_id_inmueble_medidas
foreign key(id_inmueble_medidas)
references inmuebles_medidas(id);
-- FK ID_INMUEBLE_DESCRIPCIONES
alter table inmuebles
add constraint FK_inmuebles_id_inmueble_descripciones
foreign key(id_inmueble_descripcion)
references inmuebles_descripciones(id);
--FK ID_PROPIETARIO_INMUEBLE
alter table inmuebles
add constraint FK_inmuebles_id_propietario_inmueble
foreign key(id_propietario_inmueble)
references propietarios_inmuebles(id);
--FK ID_OFICINA
alter table inmuebles
add constraint FK_inmuebles_id_oficina
foreign key(id_oficina)
references oficinas(id);
-- CHECK PRECIO_INMUEBLE_USD
alter table inmuebles
add constraint CHECK_inmuebles_precio_inmueble_usd
check (precio_inmueble_usd > 0);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA CITAS_INMUEBLES ===========
create type estado_cita_enum as enum('PENDIENTE','COMPLETADA','INCOMPLETA');
create table citas_inmuebles(
id int primary key,
id_inmueble int not null,
id_empleado int NOT NULL,-- Puede ser un gerente o vendedor
id_cliente int NOT null,-- Si hay cita automaticamente pasa a ser un cliente
estado_cita estado_cita_enum not null,
descripcion_cita varchar(200) not null,
fecha_cita date NOT null,-- ej '2001-10-07'
hora_cita time NOT NULL -- ej '09:00:07'
);
-- ======= Restricciones Tabla citas_inmuebles ===========
-- UNIQUE ID
alter table citas_inmuebles
add constraint UNIQUE_citas_inmuebles_id
unique(id);
-- FK ID_INMUEBLE
alter table citas_inmuebles
add constraint FK_citas_inmuebles_id_inmueble
foreign key(id_inmueble)
REFERENCES inmuebles(id);
-- FK ID_EMPLEADO
alter table citas_inmuebles
add constraint FK_citas_inmuebles_id_empleado
foreign key(id_empleado)
REFERENCES empleados(id);
-- FK ID_CLIENTE
alter table citas_inmuebles
add constraint FK_citas_inmuebles_id_cliente
foreign key(id_cliente)
REFERENCES clientes(id);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA INSPECCIONES_INMUEBLES ===========
-- http://checkhouse.com.ar/inspecciones-para-inquilinos/#amenities-content
create type estado_inspeccion_enum as enum('ACEPTADA','NO_ACEPTADA','PENDIENTE_REVISION');
create type tipo_inspeccion_enum as enum('DEPARTAMENTO','CASA','PH');
create table inspecciones_inmuebles(
id int primary key,
id_inmueble int not null,
estado_inspeccion estado_inspeccion_enum not null,
tipo_inspeccion tipo_inspeccion_enum not null,
descripcion_inspeccion varchar(200) not null,
empresa varchar(30) not null,
direccion varchar(30) not null,
nro_telefono varchar(30) not null,
costo decimal(10,2) not null,
fecha date not null,-- ej '2001-10-07'
hora time not null -- ej '09:00:07'
);
-- ======= Restricciones Tabla inspecciones_inmuebles ===========
-- UNIQUE ID
alter table inspecciones_inmuebles
add constraint UNIQUE_inspecciones_inmuebles_id
unique(id);
-- FK ID_INMUEBLE
alter table inspecciones_inmuebles
add constraint FK_inspecciones_inmuebles_id_inmueble
foreign key(id_inmueble)
references inmuebles(id);
-- CHECK COSTO
alter table inspecciones_inmuebles
add constraint CHECK_inspecciones_inmuebles_costo
check(costo > 0);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA INMUEBLES_MARKETING===========
create table inmuebles_marketing(
id int primary key,
id_inmueble int not null,
tipo_anuncio_principal varchar(50) not null,
tipo_anuncio_secundario varchar(50),
descripcion_anuncio varchar(300) not null,
inversion_total decimal(8,2) not null
);
-- ======= Restricciones Tabla inmuebles_marketing ===========
-- UNIQUE ID
alter table inmuebles_marketing
add constraint UNIQUE_inmuebles_marketing_id
unique(id);
-- FK ID_INMUEBLES
alter table inmuebles_marketing
add constraint FK_inmuebles_marketing_id_inmueble
foreign key(id_inmueble)
references inmuebles(id);
-- CHECK INVERSION_TOTAL
alter table inmuebles_marketing
add constraint CHECK_inmuebles_marketing_inversion_total
check(inversion_total > 0);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA ADMINISTRADORES ===========
create table administradores(
id int primary key,
id_empleado int not null,
tipo_inmueble varchar(20),-- casa, depto, etc
certificaciones varchar(200),-- administracion zonas residenciales, rurales, etc
nivel_experiencia varchar(30),-- bueno,alto,excelente
cualidades varchar(100)-- flexibilidad, confianza,etc
);
-- ======= Restricciones Tabla Administradores ===========
-- UNIQUE ID
alter table administradores
add constraint UNIQUE_administradores_id
unique(id);
-- UNIQUE ID_EMPLEADO
alter table administradores
add constraint UNIQUE_administradores_id_empleado
unique(id_empleado);
-- FK ID_EMPLEADO
alter table administradores
add constraint FK_administradores_id_empleado
foreign key(id_empleado)
references empleados(id);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA GERENTES ===========
-- Cargo de Directores, etc
create table gerentes(
id int primary key,
id_empleado int not null,
titulo varchar(30) not null,
aneos_experiencia_laboral decimal(4,2) not null, -- ej 1.2 años, etc
competencias varchar(100),-- planeamiento Estrategico, comunicacion efectiva, liderazgo, trabajo en equipo, orientacion a resultados,etc
beneficios varchar(100),--Viajes, Horario flexible, home office,etc
retribucion_salarial_anual decimal(8,2) not null
);
-- ======= Restricciones Tabla Gerentes ===========
-- UNIQUE ID
alter table gerentes
add constraint UNIQUE_gerentes_id
unique(id);
-- UNIQUE ID_EMPLEADO
alter table gerentes
add constraint UNIQUE_gerentes_id_empleado
unique(id_empleado);
-- FK ID_EMPLEADO
alter table gerentes
add constraint FK_gerentes_id_empleado
foreign key(id_empleado)
references empleados(id);
-- CHECK EXPERIENCIA_LABORAL
alter table gerentes
add constraint CHECK_gerentes_aneos_expericia_laboral
check ( aneos_experiencia_laboral >= 2);-- 2 años
-- CHECK MONTO_ADICIONAL
alter table gerentes
add constraint CHECK_gerentes_retribucion_salarial_anual
check ( retribucion_salarial_anual > 0);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA VENDEDORES ===========
create table vendedores(
id int primary key,
id_empleado int not null,
cantidad_ventas int not null,
bonificacion_ventas decimal(8,2),
puntuacion_ventas varchar(20),-- buena, normal, excelente
orientacion_tipo_inmueble varchar(30),-- casa, depto, etc
cualidades varchar(100)-- flexibilidad, confianza,etc
);
-- ======= Restricciones Tabla vendedores ===========
-- UNIQUE ID
alter table vendedores
add constraint UNIQUE_vendedores_id
unique(id);
-- UNIQUE ID_EMPLEADO
alter table vendedores
add constraint UNIQUE_vendedores_id_empleado
unique(id_empleado);
-- FK ID_EMPLEADO
alter table vendedores
add constraint FK_vendedores_id_empleado
foreign key(id_empleado)
references empleados(id);
-- CHECK CANTIDAD_VENTAS
alter table vendedores
add constraint CHECK_vendedores_cantidad_ventas
check ( cantidad_ventas >= 0);
-- CHECK BONIFICACION_VENTAS
alter table vendedores
add constraint CHECK_vendedores_bonificacion_ventas
check ( bonificacion_ventas >= 0);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA COMPRADORES ===========
create table compradores(
id int primary key,
id_cliente int not null,
id_inmueble int not null,
beneficios_compras varchar(100) not null,
descuento_cliente_usd decimal(8,2) not null
);
-- ======= Restricciones Tabla compradores ===========
-- UNIQUE ID
alter table compradores
add constraint UNIQUE_compradores_id
unique(id);
-- UNIQUE ID_INMUEBLE
alter table compradores
add constraint UNIQUE_compradores_id_inmueble
unique(id_inmueble);
-- FK ID_CLIENTE
alter table compradores
add constraint FK_compradores_id_cliente
foreign key(id_cliente)
references clientes(id);
-- FK ID_INMUEBLE
alter table compradores
add constraint FK_compradores_id_inmueble
foreign key(id_inmueble)
references inmuebles(id);
-- CHECK DESCUENTO_CLIENTE
alter table compradores
add constraint CHECK_compradores_descuento_cliente_usd
check ( descuento_cliente_usd >= 0);
-- ---------------------------------------------------------------------------
-- ---------------------------------------------------------------------------
-- ======= TABLA VENTAS ===========
create table ventas(
id int primary key,