mysql利用函数和触发器实现非主键自增

2017-10-04 15:18:00来源:http://12173069.blog.51cto.com/12163069/1969929作者:人点击

分享

函数

set global log_bin_trust_function_creators=1;

DELIMITER //

DROP function if exists incNum//

CREATE FUNCTION incNum() RETURNS int(11)

BEGIN

set @tem=1;

select card_number into @tem from `vip_card` order by card_number desc limit 1;

set @tem=@tem+1;

return @tem;

END

//

DELIMITER ;

select incNum();

触发器

CREATE TRIGGER incNum BEFORE INSERT ON vip_card

FOR EACH ROW BEGIN

set @tem=1;

select card_number into @tem from `vip_card` order by card_number desc limit 1;

set new.card_number=@tem+1;

end


微信扫一扫

第七城市微信公众平台