--Las sentencias mostradas a continuación pueden almacenarse en un fichero y ejecutarse desde el mismo: 

-- estas sentencias sirven para crear la base de datos de ejemplos del libro.
-- (Las líneas que empiezan con dos guiones son comentarios)
-- Supongamos que estas líneas están guardadas en el disco “x”, en un fichero 
-- llamado “bdejem.txt”.

-- Se pueden ejecutar escribiendo en la Ventana de Mandatos el mandato:  
--                            >  db2 -tvf a:bdejem.txt > a:salida.txt 
-- Según el mandato anterior, el resultado de la ejecución se almacena en un fichero
-- llamado “salida.txt”. 

-- Antes de mandar a ejecutarlo hay que substituir el usuario y la 
-- contraseña adecuados en la sentencia Connect que se ve más abajo.

  --  destruir y crear la base de datos   

 drop database  if exists bdejem;
 create database bdejem;


-- conectarse a la base de datos (UUU = usuario; CCC = contraseña)  

-- connect to bdejem user UUU using CCC;
use bdejem;

-- destruir y crear tablas 

drop table   if exists tcenter;
drop table   if exists tdepto;
drop table   if exists temple;

create table  tcentr (
	numce	integer not null,
	nomce	varchar(25) not null,
	señas	varchar(25) not null,
	primary key (numce),
	unique (nomce)
	)  ;

create table  tdepto (
	numde	integer not null,
	numce	integer,
	direc	integer,
	tidir	char(1) not null,
	presu	decimal (3, 0) not null,
	depde	integer,
	nomde	varchar (20) not null,
	primary key (numde),
	unique (nomde),
	constraint departsm foreign key (depde) references  tdepto (numde) on delete no action,
	constraint centro foreign key (numce) references  tcentr (numce)
	) ;

create table  temple (
	numem	integer not null,
	numde	integer not null,
	extel	smallint not null,
	fecna	date not null,
	fecin	date not null,
	salar	decimal (4, 0) not null,
	comis	decimal (4, 0),
	numhi	smallint not null,
	nomem	varchar (20) not null,
	primary key (numem),
	constraint depart foreign key (numde) references  tdepto (numde) on delete no action
	) ;

alter table  tdepto add
	constraint director foreign key (direc) references  temple (numem) on delete set null ;

-- crear índices sobre las claves ajenas

-- create index indep2 on table tdepto (numce);
-- create index indep3 on table tdepto (depde);
-- create index indep4 on table tdepto (direc);
-- create index inemp2 on table temple (numde);

-- llenar las tablas con filas   

DELETE FROM  TEMPLE;

DELETE FROM  TDEPTO;

DELETE FROM  TCENTR;

insert into  tcentr values
	(10, 'SEDE CENTRAL', 'C. ALCALA, 820, MADRID')
	, (20, 'RELACION CON CLIENTES', 'C. ATOCHA. 405, MADRID')
	, (50, 'ALMACEN', 'C. LAVAPIES, 520, MADRID')
	;

insert into  tdepto values
	(  100, 10, NULL, 'P', 120, NULL, 'DIRECCION GENERAL')
	, (110, 20, NULL, 'P', 150, 100    , 'DIRECCION COMERCIAL')
	, (111, 20, NULL, 'F', 110, 110     , 'SECTOR INDUSTRIAL')
	, (112, 20, NULL, 'P',   90, 110     , 'SECTOR SERVICIOS')
	, (120, 10, NULL, 'F',   30, 100     , 'ORGANIZACION')
	, (121, 10, NULL, 'P',   20, 120     , 'PERSONAL')
	, (122, 10, NULL, 'P',   60, 120     , 'PROCESO DE DATOS')
	, (130, 10, NULL, 'P',   20, 100     , 'FINANZAS')
	, (123, NULL, NULL, 'F', 100, 121, 'PERSONAL CONTRATADO')
	;

insert into  temple values
  (110, 121, 350, '1929-11-10', '1950-02-15', 3100, NULL, 3, 'PONS, CESAR')
, (120, 112, 840, '1935-06-09', '1968-10-01', 3500, 1100, 1, 'LASA, MARIO')
, (130, 112, 810, '1945-09-09', '1969-02-01', 2900, 1100, 2, 'TEROL, LUCIANO')
, (150, 121, 340, '1930-08-10', '1948-01-15', 4400, NULL, 0, 'PEREZ, JULIO')
, (160, 111, 740, '1939-07-09', '1968-11-11', 3100, 1100, 2, 'AGUIRRE, AUREO')
, (180, 110, 508, '1934-10-18', '1956-03-18', 4800, 500, 2, 'PEREZ, MARCOS')
, (190, 121, 350, '1932-05-12', '1962-02-11', 3000, NULL, 4, 'VEIGA, JULIANA')
, (210, 100, 200, '1940-09-28', '1959-01-22', 3800, NULL, 2, 'GALVEZ, PILAR')
, (240, 111, 760, '1942-02-26', '1966-02-24', 2800, 1000, 3, 'SANZ, LAVINIA')
, (250, 100, 250, '1946-10-27', '1967-03-01', 4500, NULL, 0, 'ALBA, ADRIANA')
, (260, 100, 220, '1943-12-03', '1968-07-12', 7200, NULL, 6, 'LOPEZ, ANTONIO')
, (270, 112, 800, '1945-05-21', '1966-09-10', 3800, 800, 3, 'GARCIA, OCTAVIO')
, (280, 130, 410, '1948-01-11', '1971-10-08', 2900, NULL, 5, 'FLOR, DOROTEA')
, (285, 122, 620, '1949-10-25', '1968-02-15', 3800, NULL, 0, 'POLO, OTILIA')
, (290, 120, 910, '1947-11-30', '1968-02-14', 2700, NULL, 3, 'GIL, GLORIA')
, (310, 130, 480, '1946-11-21', '1971-01-15', 4200, NULL, 0, 'GARCIA, AUGUSTO')
, (320, 122, 620, '1957-12-25', '1978-02-05', 4050, NULL, 2, 'SANZ, CORNELIO')
, (330, 112, 850, '1948-08-19', '1972-03-01', 2800, 900, 0, 'DIEZ, AMELIA')
, (350, 122, 610, '1949-04-13', '1984-09-10', 4500, NULL, 1, 'CAMPS, AURELIO')
, (360, 111, 750, '1958-10-28', '1968-10-10', 2500, 1000, 2, 'LARA, DORINDA')
, (370, 121, 360, '1967-06-22', '1987-01-20', 1900, NULL, 1, 'RUIZ, FABIOLA')
, (380, 112, 880, '1968-03-30', '1988-01-01', 1800, NULL, 0, 'MARTIN, MICAELA')
, (390, 110, 500, '1966-02-19', '1986-10-08', 2150, NULL, 1, 'MORAN, CARMEN')
, (400, 111, 780, '1969-08-18', '1987-11-01', 1850, NULL, 0, 'LARA, LUCRECIA')
, (410, 122, 660, '1968-07-14', '1988-10-13', 1750, NULL, 0, 'MUÑOZ, AZUCENA')
, (420, 130, 450, '1966-10-22', '1988-11-19', 4000, NULL, 0, 'FIERRO, CLAUDIA')
, (430, 122, 650, '1967-10-26', '1988-11-19', 2100, NULL, 1, 'MORA, VALERIANA')
, (440, 111, 760, '1966-09-26', '1986-02-28', 2100, 1000, 0, 'DURAN, LIVIA')
, (450, 112, 880, '1966-10-21', '1986-02-28', 2100, 1000, 0, 'PEREZ, SABINA')
, (480, 111, 760, '1965-04-04', '1986-02-28', 2100, 1000, 1, 'PINO, DIANA')
, (490, 112, 880, '1964-06-06', '1988-01-01', 1800, 1000, 0, 'TORRES, HORACIO')
, (500, 111, 750, '1965-10-08', '1987-01-01', 2000, 1000, 0, 'VAZQUEZ, HONORIA')
, (510, 110, 550, '1966-05-04', '1986-11-01', 2000, NULL, 1, 'CAMPOS, ROMULO')
, (550, 111, 780, '1970-01-10', '1988-01-21', 1000, 1200, 0, 'SANTOS, SANCHO')
;

UPDATE  TDEPTO SET DIREC = 260 WHERE NUMDE = 100;
UPDATE  TDEPTO SET DIREC = 180 WHERE NUMDE = 110;
UPDATE  TDEPTO SET DIREC = 180 WHERE NUMDE = 111;
UPDATE  TDEPTO SET DIREC = 270 WHERE NUMDE = 112;
UPDATE  TDEPTO SET DIREC = 150 WHERE NUMDE = 120;
UPDATE  TDEPTO SET DIREC = 150 WHERE NUMDE = 121;
UPDATE  TDEPTO SET DIREC = 350 WHERE NUMDE = 122;
UPDATE  TDEPTO SET DIREC = 310 WHERE NUMDE = 130;
UPDATE  TDEPTO SET DIREC = 150 WHERE NUMDE = 123;

-- comprobar contenido de las tablas   

select * from  tcentr;

select * from  tdepto;

select * from temple;

-- select cast (numem as char(4)) as emp, cast (numde as char(4)) as dep, extel as tel, fecna as nac, 
-- 	fecin as ing, salar as sal, comis as com, numhi as hij, nomem as nom
-- 	from  temple;

-- desconectarse

-- disconnect all;

-- fin