phpmyadmin 创建触发器将UNIX_TIMESTAMP()插入日志表

tjjdgumg  于 2022-11-09  发布在  PHP
关注(0)|答案(2)|浏览(153)

嗨,我在使用这个触发器向我的日志表中插入值的语法上卡住了。在phpmyadmin中使用sql标签,在执行sql语句之前出现了一个错误

`#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 '' at line 8 `

下面是我在创建触发器时使用的sql语句

CREATE TRIGGER after_insert_list  
AFTER INSERT ON list FOR EACH ROW  
BEGIN  
    INSERT INTO log (user_id, action, date_log)  
    VALUES (
        NEW.user_id,  
        NEW.action, 
        UNIX_TIMESTAMP()
    );
END
0sgqnhkj

0sgqnhkj1#

按如下所示创建触发器:

delimiter |
CREATE TRIGGER testref 
BEFORE INSERT ON test1FOR EACH ROW BEGIN
INSERT INTO test2 SET a2 = NEW.a1;
DELETE FROM test3 WHERE a3 = NEW.a1;
UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
END;
|
delimiter ;
gupuwyp2

gupuwyp22#

下面是一个100%有效的示例:

表格demo

CREATE TABLE `demo` (
    `id` int NOT NULL AUTO_INCREMENT,
    `text` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
    `updated_at` int NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`)
);

INSERT时触发器

DELIMITER ;;
CREATE TRIGGER `updated_at__on_insert`
BEFORE INSERT
ON `demo`
FOR EACH ROW
BEGIN
   SET new.updated_at = UNIX_TIMESTAMP();
END;;
DELIMITER ;

UPDATE时触发器

DELIMITER ;;
CREATE TRIGGER `updated_at__on_update`
BEFORE UPDATE
ON `demo`
FOR EACH ROW
BEGIN
   SET new.updated_at = UNIX_TIMESTAMP();
END;;
DELIMITER ;

我正在为我的CMS Effcore寻找这样的解决方案。

相关问题