简体   繁体   English

MySQL TRIGGER中的语法错误->插入语句

[英]Syntax ERROR inside MySQL TRIGGER -> Insert Into Statement

I have a trigger that inserts new row into another table however there is an error somewhere as it tells me my values are NULL and they are not or at least they are not suppose to be. 我有一个触发器,它将新行插入到另一个表中,但是在某处出现错误,因为它告诉我我的值是NULL,并且它们不是或至少不应该是。

Code of a TRIGGER 触发代码

Delimiter // 分隔符//

CREATE TRIGGER new_active_user AFTER UPDATE ON `pre_reg`
FOR EACH ROW
BEGIN
    DECLARE currentUser INT;
    DECLARE banned INT;
    DECLARE user_type INT;
    DECLARE username VARCHAR (25);
    DECLARE password VARCHAR (60);
    DECLARE usersince datetime;
    DECLARE email varchar(40);
    SET @currentUser := (select `id` FROM `pre_reg` WHERE `active` = 1);
    SET @banned = 0;
    SET @user_type = 3;
    SET @username := (select `username` FROM `pre_reg` WHERE `id` = currentUser);
    SET @password := (select `password` FROM `pre_reg` WHERE `id` = currentUser);
    SET @usersince := (select `date` FROM `pre_reg` WHERE `id` = currentUser);
    SET @email := (select `email` FROM `pre_reg` WHERE `id` = currentUser);
    INSERT INTO `users` (`id`, `username`, `password`) VALUES (@currentUser, @username, @password);
END//
Delimiter ;

I even tried hard coding in the value of a currentUser in SET username, password, email SET statements. 我什至尝试在SET用户名,密码,电子邮件SET语句中使用currentUser的值进行硬编码。 However It doesn't work I get following error: 但是,它不起作用,我得到以下错误:

Error 错误

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'username' cannot be null SQLSTATE [23000]:违反完整性约束:1048列“用户名”不能为空

I even tried to enter only ID (@currentUser) to make sure its set and the rest was hard coded (Works fine! So I assume that error is in the SET statements but can't figure out what it tells me always that value is NULL 我什至尝试仅输入ID(@currentUser)以确保其设置,其余的都进行了硬编码(工作正常!所以我假设SET语句中存在错误,但无法弄清楚它告诉我的总是值是空值

This is how I have created the tables 这就是我创建表格的方式

CREATE TABLE `pre_reg` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `username` varchar(25) NOT NULL,
 `password` varchar(60) NOT NULL,
 `email` varchar(40) NOT NULL,
 `authentication` varchar(32) NOT NULL,
 `active` smallint(1) NOT NULL DEFAULT '0',
 `date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`id`),
 UNIQUE KEY `username` (`username`,`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
GO
CREATE TABLE `users` (
`id` INT PRIMARY KEY,
`username` VARCHAR (25) NOT NULL,
`password` VARCHAR (60) NOT NULL,
`banned` SMALLINT NOT NULL DEFAULT 0,
`user_since` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
)ENGINE=InnoDB DEFAULT CHARSET=latin1
GO

This is probably what you want: 这可能是您想要的:

Delimiter //

CREATE TRIGGER new_active_user AFTER UPDATE ON `pre_reg`
FOR EACH ROW
BEGIN
    INSERT INTO `users` (`id`, `username`, `password`)
        VALUES (new.id, new.username, new.password);
END//
Delimiter ;

It inserts into the users table whatever what just updated in pre_reg . 它将pre_reg刚刚更新的内容插入到users表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM