CREATE TABLE vn.ppeLocation (
	code varchar(10) NULL,
	description varchar(100) NULL,
	CONSTRAINT ppeLocation_PK PRIMARY KEY (code)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci;

INSERT INTO vn.ppeLocation (code, description) VALUES('B01', 'VILASSAR');
INSERT INTO vn.ppeLocation (code, description) VALUES('H01', 'MERCA HOL');
INSERT INTO vn.ppeLocation (code, description) VALUES('M01', 'TORREJON DE ARDOZ');
INSERT INTO vn.ppeLocation (code, description) VALUES('M02', 'MADRID SAN FERNANDO DE HENA');
INSERT INTO vn.ppeLocation (code, description) VALUES('V01', 'VALENCIA');
INSERT INTO vn.ppeLocation (code, description) VALUES('V02', 'ALGEMESI P.I. PEPE MIQUEL');
INSERT INTO vn.ppeLocation (code, description) VALUES('V03', 'Terme d''Alginet');
INSERT INTO vn.ppeLocation (code, description) VALUES('V04', 'VALENCIA-ALGEMESI (ZONA PULPIS)');
INSERT INTO vn.ppeLocation (code, description) VALUES('V05', 'MARJAL');
INSERT INTO vn.ppeLocation (code, description) VALUES('V06', 'Finca Rustica Alzira');
INSERT INTO vn.ppeLocation (code, description) VALUES('V07', 'NAVE EN SILLA');
INSERT INTO vn.ppeLocation (code, description) VALUES('V08', 'PICASSENT');
INSERT INTO vn.ppeLocation (code, description) VALUES('V09', 'Mercavalencia');

UPDATE vn.ppe p
	JOIN (SELECT p.id,
				p.location,
				pl.code
			FROM vn.ppe p
			JOIN vn.ppeLocation pl ON pl.description = p.location) sub ON sub.id = p.id
	SET p.location = sub.code;

UPDATE vn.ppe
	SET location='V05'
	WHERE id=530;
UPDATE vn.ppe
	SET location='V05'
	WHERE id=631;
UPDATE vn.ppe
	SET location='V03'
	WHERE id=643;

UPDATE vn.ppeLocation
	SET description='BARCELONA-VILASSAR'
	WHERE code='B01';
UPDATE vn.ppeLocation
	SET description='HOLANDA-MERCA HOL'
	WHERE code='H01';
UPDATE vn.ppeLocation
	SET description='MADRID-TORREJON DE ARDOZ'
	WHERE code='M01';
UPDATE vn.ppeLocation
	SET description='MADRID-SAN FERNANDO DE HENA'
	WHERE code='M02';
UPDATE vn.ppeLocation
	SET description='VALENCIA-ALGEMESI CL FENOLLAR'
	WHERE code='V02';
UPDATE vn.ppeLocation
	SET description='VALENCIA-CARLET (ZONA ARENAL)'
	WHERE code='V03';
UPDATE vn.ppeLocation
	SET description='VALENCIA-ALGEMESI (ZONA MARJA)'
	WHERE code='V05';
UPDATE vn.ppeLocation
	SET description='VALENCIA-ALZIRA (CUADRA)'
	WHERE code='V06';
UPDATE vn.ppeLocation
	SET description='VALENCIA-NAVE EN SILLA'
	WHERE code='V07';
UPDATE vn.ppeLocation
	SET description='VALENCIA-PICASSENT'
	WHERE code='V08';
UPDATE vn.ppeLocation
	SET description='VALENCIA-MERCAVALENCIA'
	WHERE code='V09';

UPDATE vn.ppeLocation
	SET description='HOLANDA-MERCA HOL [BAJA/VENTA]'
	WHERE code='H01';
UPDATE vn.ppeLocation
	SET description='MADRID-SAN FERNANDO DE HENA [BAJA/VENTA]'
	WHERE code='M02';
UPDATE vn.ppeLocation
	SET description='VALENCIA-NAVE EN SILLA [BAJA/VENTA]'
	WHERE code='V07';
UPDATE vn.ppeLocation
	SET description='VALENCIA-PICASSENT [BAJA/VENTA]'
	WHERE code='V08';
UPDATE vn.ppeLocation
	SET description='VALENCIA-MERCAVALENCIA [BAJA/VENTA]'
	WHERE code='V09';

ALTER TABLE vn.ppe MODIFY COLUMN location varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL;
ALTER TABLE vn.ppe ADD CONSTRAINT ppe_FK FOREIGN KEY (location) REFERENCES vn.ppeLocation(code) ON DELETE RESTRICT ON UPDATE CASCADE;