我有一个名为tblspmaster
sp 列的表,其中我有唯一索引,因此不会插入重复项,但我想将重复的行插入tblspduplicate
. 所以我决定为此编写触发器。tblspmaster
将使用 Load File 插入作为记录的 IN 主表mysql
create trigger tblspmaster_noduplicate
before insert on tblspmaster
for each row
begin
if ( select count(sp) from tblspmaster where sp=new.sp > 0 )then
insert into tblspduplicate (sp,FileImported,AMZFileName) values (NEW.sp,NEW.FileImported,NEW.AMZFileName)
END
END
我有问题清单
这是停止重复并插入另一个表的正确方法吗?
我的触发器没有执行,因为它显示了一些语法错误
错误响应是Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END END' at line 7
** * ** * ** * ** * ****已编辑* ** * ** * ** * ****
这是主表和重复表的表定义以及触发和加载数据文件 MySQL 语句
CREATE TABLE IF NOT EXISTS `tblspmaster` (
`CSN` bigint(20) NOT NULL AUTO_INCREMENT,
`SP` varchar(10) NOT NULL,
`FileImportedDate` date NOT NULL,
`AMZFileName` varchar(50) NOT NULL,
`CasperBatch` varchar(50) NOT NULL,
`BatchProcessedDate` date NOT NULL,
`ExpiryDate` date NOT NULL,
`Region` varchar(50) NOT NULL,
`FCCity` varchar(50) NOT NULL,
`VendorID` int(11) NOT NULL,
`LocationID` int(11) NOT NULL,
PRIMARY KEY (`CSN`),
UNIQUE KEY `SP` (`SP`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10000000000 ;
CREATE TABLE IF NOT EXISTS `tblspduplicate` (
`SP` varchar(50) NOT NULL,
`FileImportedDate` date NOT NULL,
`AMZFileName` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
use casper;
DELIMITER $$
create trigger tblspmaster_noduplicate
before insert on tblspmaster
for each row
begin
if ( select count(sp) from tblspmaster where sp=new.sp > 0 ) then
insert into tblspduplicate (sp,FileImportedDate,AMZFileName) values (NEW.sp,NEW.FileImportedDate,NEW.AMZFileName);
END IF;
END$$
DELIMITER ;
LOAD DATA local INFILE 'E://31october//SP//sp_files_sample1//400k sp00 6-19 E.csv'
INTO TABLE tblspmaster
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(sp);
这是我从 ac# 控制台应用程序执行这个 MySql 命令的故事的一个转折点,但我认为它不会以任何方式影响我们的 db 相关结构或程序。我还需要删除IGNORE 1 LINES
语句,因为不会有标题行。