· 9 years ago · Dec 09, 2016, 12:21 PM
1use padron;
2
3drop table if exists habitante;
4drop table if exists vivienda;
5drop table if exists municipio; -- Borra la informacion y la estructura.
6--truncate table municipio; --Solo borra la informacion.
7
8create table municipio(
9 cp char(5),
10 nombre varchar(50) not null,
11 primary key (cp)
12);
13
14insert into municipio (cp, nombre) values ("29007", "Málaga - Portada Alta");
15insert into municipio (cp, nombre) values ("29006", "Málaga - Cruz del Humilladero");
16insert into municipio (cp, nombre) values ("29010", "Málaga - Teatinos");
17
18create table vivienda(
19 nrc char(20),
20 direccion varchar(50) not null,
21 municipio char (5) not null,
22 primary key (nrc),
23 foreign key (municipio) references municipio (cp) on update cascade on delete restrict
24);
25
26insert into vivienda (nrc, direccion, municipio) values ("001", "Direccion vivienda 0001", "29006");
27insert into vivienda (nrc, direccion, municipio) values ("002", "Direccion vivienda 0002", "29007");
28insert into vivienda (nrc, direccion, municipio) values ("003", "Direccion vivienda 0003", "29010");
29
30create table habitante(
31 id char (10),
32 nombre varchar (30) not null,
33 fNac date not null,
34 dondeVive char(20) not null,
35 cf char(10) not null,
36 primary key (id),
37 foreign key (dondeVive) references vivienda (nrc) on update cascade on delete restrict,
38 foreign key (cf) references habitante (id) on update cascade on delete restrict
39);
40
41insert into habitante (id, nombre, fNac, dondeVive, cf) values ("01", "HAB01", "1980-12-12", "002", "01");
42insert into habitante (id, nombre, fNac, dondeVive, cf) values ("02", "HAB02", "1981-11-10", "002", "01");
43insert into habitante (id, nombre, fNac, dondeVive, cf) values ("03", "HAB03", "2010-12-30", "002", "01");
44insert into habitante (id, nombre, fNac, dondeVive, cf) values ("04", "HAB04", "1970-04-20", "001", "04");
45insert into habitante (id, nombre, fNac, dondeVive, cf) values ("05", "HAB05", "1976-12-31", "003", "05");