我有一个触发器将新行插入到另一个表中但是在某处出现错误,因为它告诉我我的值为NULL并且它们不是或者至少它们不是假设的。
TRIGGER的代码
分隔符//
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 ;
我甚至尝试在SET用户名,密码,电子邮件SET语句中使用currentUser的值进行硬编码。但是它不起作用我得到以下错误:
错误
SQLSTATE [23000]:完整性约束违规:1048列“用户名”不能为空
我甚至试图只输入ID(@currentUser)以确保它的设置,其余部分是硬编码的(工作正常!所以我认为错误在SET语句中,但无法弄清楚它告诉我什么总是该值为NULL
这就是我创建表格的方式
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
答案 0 :(得分:0)
这可能是你想要的:
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 ;
它会在users
表中插入pre_reg
中更新的内容。