触发器
若存在以下表
Create table tblUser
(
UID char(4) primary key,
UName char(3) not null,
UBirthday date not null
)
请为此表设计触发器,tri_check, 检查写入数据的正确性
要求:1、当UName小于2个字符时,消息报错‘必须2个字符以上’
2、当UBirthday 日期大于当前日期,消息报错‘不能大于当前日期’
3、若以上都存在,消息叠加报错
CREATE TRIGGER tri_check #创建触发器
before INSERT #和下面一起
on tbluser #和下面一起
for each ROW #在数据写入tbluser表之前,对于每一行语句都检查一遍
BEGIN
if CHAR_LENGTH(new.uname)<2 and new.uBirthday>CURDATE() then
set @msg='必须2个字符以上
不能大于当前日期';
SIGNAL SQLSTATE '45000' set message_text=@msg; #如果符合以上两个条件,报错输出设定好的语句
end if;
if CHAR_LENGTH(new.uname)<2 then
set @msg='必须2个字符以上';
SIGNAL SQLSTATE '45000' set message_text=@msg; #如果符合以上一个条件,报错输出设定好的语句
end if;
if new.ubirthday>CURDATE() then
set @ts='不能大于当前日期';
SIGNAL SQLSTATE '45000' set message_text=@ts; #如果符合以上一个条件,报错输出设定好的语句
end if;
end