-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreaciontablas.sql
More file actions
247 lines (227 loc) · 7.38 KB
/
Copy pathcreaciontablas.sql
File metadata and controls
247 lines (227 loc) · 7.38 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
create table zonas(
nome character varying(30),
extension real,
coordenadaX real,
coordenadaY real,
constraint zonaspk primary key(nome)
);
create table atraccions(
nome character varying(30),
aforo integer not null,
alturaMin integer not null,
custoMantemento real,
descricion character varying(500),
zona character varying(30),
constraint atraccionsfk1 foreign key (zona) references public.zonas(nome),
constraint atraccionspk primary key (nome)
);
create table atraccionssoadultos(
nome character varying(30),
idadeMin integer,
constraint atraccionssoadultosfk1 foreign key (nome) references public.atraccions(nome)
on update cascade on delete set null
);
create table atraccionsfamiliares(
nome character varying(30),
idadeRecomendada integer,
constraint atraccionsfamiliaresfk1 foreign key (nome) references public.atraccions(nome)
on update cascade on delete set null
);
create table hostalaria(
nomeEstablecemento character varying(30),
aforo integer not null,
horaInicio time,
horaFin time,
constraint hostalariapk primary key(nomeEstablecemento),
zona character varying(30),
constraint hostalariafk1 foreign key (zona) references public.zonas(nome)
on update cascade on delete set null
);
create table espectaculos(
nome character varying(30),
horaInicio time,
horaFin time,
tematica character varying(15),
descricion character varying(200),
constraint espectaculospk primary key(nome),
zona character varying(30),
constraint espectaculosfk1 foreign key (zona) references public.zonas(nome)
on update cascade on delete set null
);
create table traballadoresParque(
dni character(9),
nome character varying(60) not null,
rua character varying(40),
numero integer,
cp integer,
localidade character varying(30),
salario real not null,
telefono character(9),
dataInicio date not null,
dataNacemento date,
formacion character varying(100) not null,
nomeAtraccion character varying(30) ,
nomeEspectaculo character varying(30) ,
constraint traballadoresParquepk primary key(dni),
constraint traballadoresParquefk1 foreign key (nomeAtraccion)
references public.atraccions(nome)
on update cascade on delete set null,
constraint traballadoresParquefk2 foreign key (nomeEspectaculo)
references public.espectaculos(nome)
on update cascade on delete set null
);
create table hostaleiros(
dni character(9),
nome character varying(60) not null,
rua character varying(40),
numero integer,
cp integer,
localidade character varying(30),
salario real not null,
telefono character(9),
dataInicio date not null,
dataNacemento date,
formacion character varying(100) not null,
nomeEstablecemento character varying(30),
constraint hostaleirospk primary key(dni),
constraint hostaleirosfk1 foreign key (nomeEstablecemento)
references public.hostalaria(nomeEstablecemento)
on update cascade on delete set null
);
create table medios(
nomeMedio character varying(30),
tipo character varying(30) not null,
prezo real not null,
capacidade integer not null,
velocidade real not null,
constraint mediospk primary key (nomeMedio)
);
create table visitantes(
dni character(9) not null,
nome character varying(60) not null,
nacionalidade character varying(30),
telefono character(9),
dataNacemento date,
altura integer not null,
medioTransporte character varying(30),
constraint visitantespk primary key(dni),
constraint mediosfk1 foreign key (medioTransporte)
references public.medios(nomeMedio)
on update cascade on delete set null
);
create table ir(
visitante character(9),
atraccion character varying(30),
horaInicio time,
fecha date,
constraint irpk primary key(visitante, atraccion, horaInicio, fecha),
constraint irfk1 foreign key (visitante)
references public.visitantes(dni)
on update cascade on delete cascade,
constraint irfk2 foreign key (atraccion)
references public.atraccions(nome)
on update cascade on delete cascade
);
create table asistir(
visitante character(9),
espectaculo character varying(30),
horaInicio time,
fecha date,
constraint asistirpk primary key(visitante,espectaculo,horaInicio,fecha),
constraint asistirfk1 foreign key (visitante)
references public.visitantes(dni)
on update cascade on delete cascade,
constraint asistirfk2 foreign key (espectaculo)
references public.espectaculos(nome)
on update cascade on delete cascade
);
create table xantar(
visitante char(9),
hostalaria character varying(30),
horaInicio time,
fecha date,
PRIMARY KEY (visitante, hostalaria, fecha, horaInicio),
constraint xantarfk1 foreign key (hostalaria) references
public.hostalaria(nomeEstablecemento)
on update cascade on delete set null,
constraint xantarfk2 foreign key (visitante) references
public.visitantes(dni)
on update cascade on delete set null
);
create table musica(
codigoCancion character (9),
nome character varying (30) not null,
clasificacion character varying (30) not null,
popularidade integer,
artista character varying(30) not null,
album character varying(30),
constraint musicapk primary key (codigoCancion)
);
create table sistemasDeAudio(
identificador character (5),
funcion character varying (20) not null,
descricion character varying(150),
constraint sistemasDeAudiopk primary key (identificador),
zona character varying(30),
constraint atraccionsfk1 foreign key (zona) references public.zonas(nome)
on update cascade on delete set null
);
create table reproducir(
data date,
musica character(9),
sistema character (5),
constraint reproducirpk primary key(data,musica,sistema),
constraint reproducirfk1 foreign key (musica)
references public.musica(codigoCancion)
on update cascade on delete cascade,
constraint reproducirfk2 foreign key (sistema)
references public.sistemasDeAudio(identificador)
on update cascade on delete cascade
);
create table DJ(
dni character(9),
nome character varying(60) not null,
rua character varying(40),
numero integer,
cp integer,
localidade character varying(30),
salario real not null,
telefono character(9),
fechaInicio date not null,
fechaNacemento date,
formacion character varying(100) not null,
sistema character varying(30) ,
constraint DJpk primary key(dni),
constraint DJfk1 foreign key (sistema)
references public.sistemasDeAudio(identificador)
on update cascade on delete set null
);
create table valoracions(
identificador serial,
data date,
descricion character varying(200),
puntuacion integer not null,
visitante character(9),
constraint valoracionspk primary key (identificador),
constraint valoracionsfk1 foreign key (visitante)
references public.visitantes(dni)
on update cascade on delete set null
);
create or replace function check_person_exists(dni char(9), nome varchar(60))
returns boolean
language sql
as $$
select exists (select 1 from visitantes as v where v.dni = $1 and v.nome = $2)
or exists (select 1 from traballadoresparque as t where t.dni = $1 and t.nome = $2)
or exists (select 1 from hostaleiros as h where h.dni = $1 and h.nome = $2)
or exists (select 1 from dj as d where d.dni = $1 and d.nome = $2)
$$;
create table Users (
id_user serial primary key,
dni char(9),
nome varchar(60),
username varchar(60),
password char(64),
is_admin boolean,
constraint user_exists check (check_person_exists(dni, nome))
);