0

我在empdetails 数据库中有一张emppersonal 表。

我创建了一个触发器

use empdetails;

delimiter $$
create trigger `newtirger` after insert  on `emppersonal` 
for each row begin 
if new.mobile = null
then 
update emppersonal set mobile = '121';
end if;
end $$;

我的情况是在插入查询期间,如果列移动为空,则在移动字段中插入 121..

insert into emppersonal(empname,empadddress,mobile,empdob) values('lop1wde','n1agar',null,null) ;

但触发器没有向我显示任何错误,也没有工作,请帮忙。

4

2 回答 2

0

尝试

use empdetails;

delimiter $$
create trigger `newtirger` after insert  on `emppersonal` 
for each row begin 
if new.mobile IS NULL
then 
--modify this line update emppersonal set mobile = '121';
update emppersonal set mobile = '121';
end if;
end $$;
于 2013-10-07T07:36:43.807 回答
0

A better way to update an empty value would be using a default value for a column. That way it will be automatically set if null.

ALTER TABLE emppersonal
CHANGE mobile mobile  VARCHAR(50) NOT NULL DEFAULT '121';

if that is not possible then try

delimiter $$
create trigger `newtrigger` before insert on emppersonal
for each row begin 
    set NEW.mobile = if(OLD.mobile IS NULL, '121', OLD.mobile);
end
$$
于 2013-10-07T07:33:07.617 回答