CREATE TABLE `rehab_qrcode` (
  `QRcode_ID` int(9) NOT NULL AUTO_INCREMENT,
  `User_ID` bigint(20) NOT NULL,
  `Code` varchar(1024),
  `License` varchar(255),
  `Active` tinyint(1) NOT NULL DEFAULT 1,
  `EditedBy` bigint(20),
  `Checksum` varchar(32),
  `Last_Edit` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`QRcode_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `rehab_qrcode_backup` (
  `Backup_ID` int(9) NOT NULL AUTO_INCREMENT,
  `QRcode_ID` int(9),
  `User_ID` bigint(20) NOT NULL,
  `Code` varchar(1024),
  `License` varchar(255),
  `Active` tinyint(1) NOT NULL DEFAULT 1,
  `EditedBy` bigint(20),
  `Checksum` varchar(32),
  `Previous_Last_Edit` timestamp NULL DEFAULT NULL,
  `Last_Edit` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`Backup_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TRIGGER IF EXISTS `QRcode_Backup`;
DELIMITER //
CREATE TRIGGER `QRcode_Backup`  AFTER UPDATE ON `rehab_qrcode`
 FOR EACH ROW
  BEGIN
	INSERT INTO `rehab_qrcode_backup`(`QRcode_ID`,`User_ID`,`Code`,`License`,`Active`,`EditedBy`,`Checksum`,`Previous_Last_Edit`) 
    VALUES (OLD.`QRcode_ID`,OLD.`User_ID`,OLD.`Code`,OLD.`License`,OLD.`Active`,OLD.`EditedBy`,OLD.`Checksum`, OLD.`Last_Edit`) ;
  END
// DELIMITER ;