跳线,在插入触发器之前?

时间:2016-11-29 13:50:12

标签: mysql navicat

我有两个表,表(文件)和表(哈希)我想创建一个插入新(文件)的过程,如果不存在则添加(哈希),然后在(文件)中插入(ID) )表,如果存在选择(ID)并插入它,我也必须使用(在插入文件触发器之前),但它或者给我一个CONSTRAINT错误或插入哈希并且没有插入文件。

CREATE TABLE `Hashes` (
  `ID` int(255) unsigned NOT NULL AUTO_INCREMENT,
  `Hash` varchar(255) NOT NULL,
  `Counter` int(255) NOT NULL DEFAULT '1',
  PRIMARY KEY (`ID`),
  UNIQUE KEY `Hash` (`Hash`)
)

CREATE TABLE `Files` (
  `ID` int(255) unsigned NOT NULL AUTO_INCREMENT,
  `Name` varchar(255) NOT NULL,
  `ParentPath` varchar(255) DEFAULT NULL,
  `Size` double NOT NULL,
  `Date` date NOT NULL,
  `ParentID` int(255) unsigned NOT NULL DEFAULT '0',
  `HashID` int(255) unsigned DEFAULT NULL,
  PRIMARY KEY (`ID`),
  KEY `fk_Hash_ID` (`HashID`),
  CONSTRAINT `fk_Hash_ID` FOREIGN KEY (`HashID`) REFERENCES `hashes` (`ID`)
)

////////
CREATE PROCEDURE SP_Files_Insert
(                      
parmName VARCHAR(255),                 
parmParentPath VARCHAR(255),         
parmSize DOUBLE,   
parmDate DATE,
parmParentID INTEGER,
parmHash VARCHAR(255)
)
BEGIN

    INSERT INTO Files( Name, ParentPath, Size, Date, ParentID, HashID) 
    VALUES(parmName, parmParentPath, parmSize, parmDate, parmParentID,    LAST_INSERT_ID());

END;

////////////
CREATE TRIGGER TR_Insert_File BEFORE INSERT ON Files 
FOR EACH ROW
BEGIN

  INSERT INTO Hash( Hash, Counter) 
    VALUE( parmHash, 1) 
    ON DUPLICATE KEY UPDATE ID = LAST_INSERT_ID(ID), Counter = Counter + 1;

END;

1 个答案:

答案 0 :(得分:0)

在你的触发器中使用分隔符,如下所示

delimiter \\
CREATE TRIGGER TR_Insert_File BEFORE INSERT ON Files 
FOR EACH ROW
BEGIN

  INSERT INTO Hash( Hash, Counter) 
    VALUE( parmHash, 1) 
    ON DUPLICATE KEY UPDATE ID = LAST_INSERT_ID(ID), Counter = Counter + 1;

END\\ 
delimiter ;