我试图创建一个MySQL插入之前触发下面的代码这将做我想做的事情,如果我能找到一种方法来执行由触发产生的事先准备好的声明。
是从触发器内执行预处理语句任何其他方式? 谢谢
BEGIN
SET @CrntRcrd = (SELECT AUTO_INCREMENT FROM information_schema.TABLES
WHERE TABLE_SCHEMA=DATABASE()
AND TABLE_NAME='core_Test');
SET @PrevRcrd = @CrntRcrd-1;
IF (NEW.ID IS NULL) THEN
SET NEW.ID = @CrntRcrd;
END IF;
SET @PrevHash = (SELECT Hash FROM core_Test WHERE Record=@PrevRcrd);
SET @ClmNms = (SELECT CONCAT('NEW.',GROUP_CONCAT(column_name
ORDER BY ORDINAL_POSITION SEPARATOR ',NEW.'),'')
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = 'core_Test');
SET @Query = CONCAT("SET @Query2 = CONCAT_WS(',','",@PrevHash,"','", @CrntRcrd, "',", @ClmNms, ");");
PREPARE stmt1 FROM @Query;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
SET NEW.Hash = @Query2;
END
UPDATE /澄清 :的数据将被如下存储在表中。
+------------+-----+------+----------------+
| Record (AI)| ID | Data | HASH |
+------------+-----+------+----------------+
| 1 | 1 | ASDF | =DHFBGKJSDFHBG | (Hash Col 1)
| 2 | 2 | NULL | =UEGFRYJKSDFHB | (Hash Col 1 + Col 2)
| 3 | 1 | VBNM | =VKJSZDFVHBFJH | (Hash Col 2 + Col 3)
| 4 | 4 | TYUI | =KDJFGNJBHMNVB | (Hash Col 3 + Col 4)
| 5 | 5 | ZXCV | =SDKVBCVJHBJHB | (Hash Col 4 + Col 5)
+------------+-----+------+----------------+
在每次插入命令表将被透水行的哈希值appeding到整个新行的CONCAT()生成该行的哈希值,然后重新散列整个字符串。 这将创建哈希值的运行记录以供审核/应用程序的其它部分使用。
我的约束,这有INSERT之前完成为行不能事后更新。
更新 :我目前使用下面的代码,直到我能找到一种方法,通过列名动态CONCAT:
BEGIN
SET @Record = (
SELECT AUTO_INCREMENT FROM information_schema.TABLES
WHERE TABLE_SCHEMA=DATABASE()
AND TABLE_NAME='core_Test' #<--- UPDATE TABLE_NAME HERE
);
SET @PrevRecrd = @Record-1;
IF (new.ID IS NULL) THEN
SET new.ID = @Record;
END IF;
SET @PrevHash = (
SELECT Hash FROM core_Test #<--- UPDATE TABLE_NAME HERE
WHERE Record=@PrevRecrd
);
SET new.Hash = SHA1(CONCAT_WS(',',@PrevHash, @Record,
/* --- UPDATE TABLE COLUMN NAMES HERE (EXCLUDE "new.Record" AND "new.Hash") --- */
new.ID, new.Name, new.Data
));
END