Mysql 触发器加判断语句 插入值?

Mysql 触发器加判断语句 插入值?,第1张

我的表结构如下:

create table students(id int auto_increment,

name varchar(10) not null DEFAULT '',

age int not null DEFAULT 0,

sex varchar(2) not null DEFAULT '',

studId varchar(18) not null DEFAULT '',

PRIMARY KEY (`id`))

触发器:

DELIMITER $$

CREATE TRIGGER `students_insert` BEFORE INSERT ON `students`

FOR EACH ROW

BEGIN

if mod(substring(new.studId,17,1),2)=0 then

set new.sex = '女'

else

set new.sex = '男'

end if

END

$$

DELIMITER

以上程序已测试通过,

另外友情提醒一下,在触发器中处理这个判断,效率不高,最好在程序中处理这部分逻辑.

create trigger [dbo].[delete]

on [dbo].[表A]

instead of delete

as

begin

if not exists (select count(*) from B where 登记号 = 条件)

insert 表B---插入语句

else

delete 表B where 登记号 = 条件

insert 表B ---插入语句

end


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/6423442.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-21
下一篇 2023-03-21

发表评论

登录后才能评论

评论列表(0条)

保存