MySQL,phpMyAdmin:插入触发器后不触发

时间:2017-06-22 11:35:05

标签: mysql sql triggers phpmyadmin

我在phpMyAdmin中为我的数据库定义了以下触发器: enter image description here

这是一个非常简单的触发器,我只是在 item 表中插入后将记录插入 history 表中:

insert into history values(null, new.Id, new.LocationId, new.LocationId, new.PersonId, new.PersonId, date(now()))

但是,触发器没有触发(当我在MySQL Workbench中测试它时它正常工作)。

修改1:

当我用一些虚拟值定义它时,触发器会触发:

insert into history values(null, 1, 1, 1, 1, 1, date(now()))

编辑2:

当我使用虚拟值定义触发器时,会写入 history 表(参见编辑1)。一些最近添加的条目ID是11(ID是auto_increment),即使之前的最后一个条目ID = 3.我的观点是它们不是连续的,而是好像其间有条目但是被删除了一些原因...

编辑3:

以下是历史表的DDL:

create table Item
(
Id int not null auto_increment primary key,
InventoryNumber int not null unique,
ItemStatus varchar(20) not null,
ItemType varchar(30) not null,
ItemName varchar(100) not null,
PurchaseDate date not null,
PurchaseValue decimal(12,4) not null,
Amortization decimal(12,4) not null,
LocationId int not null,
PersonId int not null,
foreign key(PersonId) references Person(Id),
foreign key(LocationId) references Location(Id)
);

create table History
(
Id int not null auto_increment primary key,
ItemId int not null,
OldLocationId int not null,
NewLocationId int not null,
OldPersonId int not null,
NewPersonId int not null,
DateOfChange date not null,
foreign key(ItemId) references Item(Id),
foreign key(OldLocationId) references Location(Id),
foreign key(NewLocationId) references Location(Id), 
foreign key(OldPersonId) references Person(Id),
foreign key(NewPersonId) references Person(Id)
);

和触发器(我认为这是无关紧要的,因为我没有通过脚本导入触发器,我使用phpMyAdmin的GUI定义它):

DELIMITER //
CREATE TRIGGER historyTriggerInsert AFTER INSERT ON Item
FOR EACH ROW
BEGIN
insert into History values(null, new.Id, new.LocationId, new.LocationId, new.PersonId, new.PersonId, date(now()));
END;//
DELIMITER ;

这是我发现的另一件事:当我使用我的Yii Web应用程序向 item 添加条目时,该条目在phpMA的 item 表中可见,但触发器不会在 history 表中插入任何内容。但是,当我在phpMA中直接向 item 添加条目时,触发器还会在历史记录中创建一个条目,即它可以正常工作。

此外,当我使用这些虚拟值定义触发器但只留下new.Id时,如下所示:

insert into history values(null, new.Id, 1, 1, 1, 1, date(now()))

它也不起作用。这个和递增ID让我得出结论触发器被触发,尝试插入历史,但由于某种原因获得了无效的new.Id值。

0 个答案:

没有答案
相关问题