CREATE TABLE `lease` (
  `l_id` INT NOT NULL AUTO_INCREMENT,
  `l_name` VARCHAR(128) NOT NULL,
  `l_expire_type` TINYINT(3) NOT NULL,
  `l_sum` INT NOT NULL,
  `l_expire_date` DATE NULL DEFAULT NULL,
  `l_deleted` INT NULL DEFAULT 0,
  PRIMARY KEY (`l_id`));

CREATE TABLE `lease_training_type` (
  `ltt_id` INT NOT NULL AUTO_INCREMENT,
  `ltt_lease_l_id` INT NOT NULL,
  `ltt_training_type_trt_id` INT NOT NULL,
  PRIMARY KEY (`ltt_id`),
  INDEX `fk_lease_training_type_1_idx` (`ltt_lease_l_id` ASC),
  INDEX `fk_lease_training_type_2_idx` (`ltt_training_type_trt_id` ASC),
  CONSTRAINT `fk_lease_training_type_1`
    FOREIGN KEY (`ltt_lease_l_id`)
    REFERENCES `lease` (`l_id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_lease_training_type_2`
    FOREIGN KEY (`ltt_training_type_trt_id`)
    REFERENCES `training_type` (`trt_id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE);

INSERT INTO `log_category` (`logc_id`, `logc_name`, `logc_title`, `logc_type`, `logc_table`, `logc_field`, `logc_selector`) VALUES (NULL, 'new_lease', 'Új bérlettípus', '1', 'lease', 'l_name', 'l_id');
INSERT INTO `log_category` (`logc_id`, `logc_name`, `logc_title`, `logc_type`, `logc_table`, `logc_field`, `logc_selector`) VALUES (NULL, 'update_lease', 'Bérlettípus módosítása', '1', 'lease', 'l_name', 'l_id');
INSERT INTO `log_category` (`logc_id`, `logc_name`, `logc_title`, `logc_type`, `logc_table`, `logc_field`, `logc_selector`) VALUES (NULL, 'delete_lease', 'Bérlettípus törlése', '1', 'lease', 'l_name', 'l_id');

ALTER TABLE `money_deposit`
ADD COLUMN `mod_lease_l_id` INT NULL DEFAULT NULL,
ADD INDEX `fk_money_deposit_2_idx` (`mod_lease_l_id` ASC);

ALTER TABLE `money_deposit`
ADD CONSTRAINT `fk_money_deposit_2`
  FOREIGN KEY (`mod_lease_l_id`)
  REFERENCES `lease` (`l_id`)
  ON DELETE SET NULL
  ON UPDATE CASCADE;


ALTER TABLE `money_deposit`
ADD COLUMN `mod_expire_date` DATE NULL DEFAULT NULL AFTER `mod_lease_l_id`;
