MySQL 解决check功能缺陷问题

在 mysql 中,尚未实现check功能,在这里,我搜集了一些资料,发现使用触发器功能可以部分代替check实现所需要的功能。

例如:

create trigger Update_BookTypeNum
	before update on book
	for each row
	begin
		declare msg varchar(32);
		if(new.bookTypeNum<0 or new.bookTypeNum>9999)
			then
				set msg = "Error:Out of range!";
				SIGNAL SQLSTATE 'HY000' SET MESSAGE_TEXT = msg;	
				
		end if;

	end;
这里使用signal 函数实现报错

以下说明signal的用法:
signal sqlstate  |   condition_name

set

condition_information_item_name_1 = value_1,
   
         condition_information_item_name_2 = value_2, et...;

condition_information_item_name: MESSAGE_TEXTMYSQL_ERRORNOCURSOR_NAME

猜你喜欢

转载自blog.csdn.net/Curry7895/article/details/79105458