-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDDL vehiculos.sql
More file actions
180 lines (131 loc) · 4.51 KB
/
DDL vehiculos.sql
File metadata and controls
180 lines (131 loc) · 4.51 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
--CREAMOS EL ESQUEMA PARA GESTIONAR LA BASE DE DATOS
create schema vehiculos authorization phdrtnbo
--CREACIÓN DE LAS TABLAS: GRUPO EMPRESARIAL, MARCA, MODELO
create table vehiculos.grupo_empresarial(
id_grupo varchar(10) not null, --PK
nombre varchar(50) not null,
cif varchar(100) not null,
descripcion varchar(512) null
);
alter table vehiculos.grupo_empresarial
add constraint grupo_empresarial_PK primary key(id_grupo);
create table vehiculos.marca(
id_marca varchar(10) not null, --PK
id_grupo varchar(10) not null, --FK GRUPO EMPRESARIAL
nombre varchar(50) not null,
cif varchar(100) not null,
descripcion varchar(512) null
);
alter table vehiculos.marca
add constraint marca_PK primary key(id_marca);
alter table vehiculos.marca
add constraint marca_grupoEmpresarial_FK
foreign key(id_grupo)
references vehiculos.grupo_empresarial(id_grupo);
create table vehiculos.modelo(
id_modelo varchar(10) not null, --PK
id_marca varchar(10) not null, --FK MARCA
nombre varchar(50) not null,
descripcion varchar(512) null
);
alter table vehiculos.modelo
add constraint modelo_PK primary key(id_modelo);
alter table vehiculos.modelo
add constraint modelo_marca_FK
foreign key(id_marca)
references vehiculos.marca(id_marca);
--CREAMOS LAS TABLAS DEL COLOR Y EL ESTADO DEL COCHE
create table vehiculos.color(
id_color varchar(10) not null, --PK
nombre varchar(20) not null,
descripcion varchar(100) null
);
alter table vehiculos.color
add constraint color_PK primary key (id_color);
create table vehiculos.estado(
id_estado varchar(10) not null, --PK
nombre varchar(20) not null,
descripcion varchar(100) null
);
alter table vehiculos.estado
add constraint estado_PK primary key (id_estado);
--CREAMOS LA TABLA DE COCHE
create table vehiculos.coche(
matricula varchar(15) not null, --PK
id_modelo varchar(10) not null, --FK modelo
fecha_compra date not null,
id_color varchar(10) not null, --FK
km integer not null,
id_estado varchar(10) not null --FK
);
alter table vehiculos.coche
add constraint coche_PK primary key(matricula);
alter table vehiculos.coche
add constraint coche_modeloo_FK
foreign key (id_modelo)
references vehiculos.modelo(id_modelo);
alter table vehiculos.coche
add constraint coche_color_FK
foreign key (id_color)
references vehiculos.color(id_color);
alter table vehiculos.coche
add constraint coche_estado_FK
foreign key (id_estado)
references vehiculos.estado(id_estado);
--CREAMOS LA TABLA TIPO_MONEDA Y ASEGURADORA
create table vehiculos.tipo_moneda(
id_moneda varchar(10) not null, --PK
nombre varchar(20) not null,
pais varchar(20) not null,
descripcion varchar(512) null
);
create table vehiculos.aseguradora(
id_aseguradora varchar(10) not null, --PK
nombre varchar(50) not null,
cif varchar(100) not null,
descripcion varchar(512) null
);
alter table vehiculos.tipo_moneda
add constraint moneda_PK primary key(id_moneda);
alter table vehiculos.aseguradora
add constraint aseguradora_PK primary key(id_aseguradora);
--CREAMOS LAS TABLAS RELATIVAS A LOS SEGUROS Y A LAS REVISIONES DE CADA COCHE
create table vehiculos.revision_coche(
matricula varchar(15) not null, --PK,FK coche
fecha_revision date not null, --PK
km_revision integer not null,
importe numeric(15,2) not null,
id_moneda varchar(10) not null --FK
);
alter table vehiculos.revision_coche
add constraint revision_coche_PK primary key (matricula,fecha_revision);
alter table vehiculos.revision_coche
add constraint revision_coche_coche_FK
foreign key (matricula)
references vehiculos.coche(matricula);
alter table vehiculos.revision_coche
add constraint revision_coche_tipoMoneda_FK
foreign key (id_moneda)
references vehiculos.tipo_moneda(id_moneda);
create table vehiculos.seguros_coche(
matricula varchar(15) not null, --PK,FK coche
poliza varchar(100) not null, --PK
id_aseguradora varchar(10) not null, --FK
fecha_inicio date not null,
fecha_fin date not null,
id_estado varchar(10) not null--FK
);
alter table vehiculos.seguros_coche
add constraint seguros_coche_PK primary key(matricula,poliza);
alter table vehiculos.seguros_coche
add constraint seguros_coche_coche_FK
foreign key(matricula)
references vehiculos.coche(matricula);
alter table vehiculos.seguros_coche
add constraint seguro_coche_asegurador_FK
foreign key(id_aseguradora)
references vehiculos.aseguradora(id_aseguradora);
alter table vehiculos.seguros_coche
add constraint seguro_coche_estado_FK
foreign key (id_estado)
references vehiculos.estado(id_estado);