我的目标是通过函数将值插入到字段中。
下面是我创建的函数。
DELIMITER $$
create function Identifier(num int)
Returns int
Begin
Return (num*10);
end $$
DELIMITER ;
下表
create table simulator
(
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
identifier BIGINT null,
name varchar(20) null,
number varchar(30) null
);
现在我的目标是根据id更新标识符的值,id是自动递增的。如果id为1,则标识符为10,如果2标识符为20,则标识符为10。
我创建了一个触发器,它将在插入之前设置标识符的值(在插入之前/之后,我有点困惑)。
以下是触发器
CREATE TRIGGER before_insert_simulator
BEFORE INSERT ON simulator
FOR EACH ROW
SET new.identifier = Identifier(new.id)
但每次标识符的值为0。
我正在运行mysql 8.0.16
发布于 2020-09-14 08:34:16
触发器无法看到即将分配给自动递增列的值。
由于identifier
包含派生信息,因此不实际存储它可能更简单。计算列不是选项,因为它不能引用自动增量列.您可以使用视图来代替:
create view myview (id, identifier, name, number) as
select id, id * 10, name, number from mytable
https://stackoverflow.com/questions/63880333
复制相似问题