自动触发器将数据复制到另一个格式化MySQL的表

时间:2016-04-23 11:32:02

标签: php mysql sql triggers mysql-workbench

所以我有一个问题。我不知道如何编写一个触发器,不仅可以将数据从( Table1 )发送到另一个表( Table2 ),还可以编辑它以及在图像链接中看到 - Full问题结束时的转变

use ptype;
create table IDVU (
`ID` int(8)  unsigned not null auto_increment ,
`VU` varchar(45) not null,
PRIMARY KEY (`id`),
KEY `ix_VU` (`VU`)
)ENGINE = InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

create table sep (
ID1 int(8) unsigned NOT NULL primary key auto_increment,
ID2 int(8) unsigned not null,
V varchar(45) not null,
U varchar(45) not null,
KEY `ix_ID2` (`ID2`),
CONSTRAINT `ID_IDVU_SEP` FOREIGN KEY (`ID2`) REFERENCES `IDVU` (`ID`) ON
DELETE CASCADE ON UPDATE CASCADE
)ENGINE = InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

delimiter $$
CREATE TRIGGER `edit` AFTER INSERT 
ON `idvu` 
FOR EACH ROW BEGIN

    IF new.VU like '% %' THEN
        SET @V = SUBSTRING_INDEX(SUBSTRING_INDEX(new.Vu, ' ', 1), ' ', -1) ,
            @U = SUBSTRING_INDEX(SUBSTRING_INDEX(new.Vu, ' ', 2), ' ', -1);
    else
        SET @V = 'NEW',@U = 'NEW';
    END IF;

    INSERT INTO sep (ID2,V, U) VALUES (new.ID,@V, @U);

END$$

delimiter ;

LOAD  DATA LOCAL INFILE '/Users/MarcisL/Desktop/MySQL faili/CSV/PTCSV.csv' ignore 
INTO TABLE IDVU FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';    

这是我想用作基础的代码,因为触发器在我看来与我现在想要的非常相似。代码工作如下 - 如果table2(IDVU)更新比table3(sep)更新,并且table2(IDVU)中的Player(VU)列被拆分为Name(V),Surname(U) - 在问题结尾处进行全面转换

最后,我的整个脚本应该像这样工作 - Full transformation

1 个答案:

答案 0 :(得分:0)