Mysql 如何模拟;国际贸易组织;我的sql中的触发器
我需要在我的sql中重写insert操作,如果某个条件为真,则写入另一个值。 例如:Mysql 如何模拟;国际贸易组织;我的sql中的触发器,mysql,sql,database-trigger,Mysql,Sql,Database Trigger,我需要在我的sql中重写insert操作,如果某个条件为真,则写入另一个值。 例如: Insert into table_name(age) values(2) 条件-如果年龄=18您将在插入触发器之前使用: create trigger <trigger name> before insert on <table> for each row begin set new.age = least(new.age, 18); end; 在插入之前创建触发器 每行
Insert into table_name(age) values(2)
条件-如果年龄=18您将在插入触发器之前使用
:
create trigger <trigger name> before insert on <table>
for each row
begin
set new.age = least(new.age, 18);
end;
在插入之前创建触发器
每行
开始
设置new.age=least(new.age,18);
结束;
在insert
触发器之前使用。如果条件为真,我必须在当前表中中断insert并将其放入另一个表中?@Darcky使用if
语句插入另一个表,然后引发错误以停止当前insert。@Darcky但如果插入多行,则会导致问题插入到表中,因为它将取消整个操作,从而阻止插入请求。您可能应该在应用程序逻辑中而不是触发器中执行此操作,但它会破坏整个应用程序transaction@Darcky . . . 据我所知,大多数数据库将insert
视为单个事务,即使有多行。所以要么全部插入,要么不插入。可以使用insert ignore
来避免触发器生成的错误。