首页 > 代码库 > MYSQL,触发器,实现多个表共用ID不重复

MYSQL,触发器,实现多个表共用ID不重复

 

参考:https://my.oschina.net/jiuxiaoyao/blog/839266

MYSQL 没有sequence 

SET @@auto_increment_offset
SET @@auto_increment_increment

也只能在当前查询有效.所以这也是权宜之计

DROP TRIGGER IF EXISTS `InsertUser`;
create trigger InsertUser    
before insert on userinfo   
for each row    
Begin   

declare id int;
insert into user values (NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);#让自动增长的表插入失败,ID作废,就可以让触发器的表使用
select id  into @id from user order by id desc limit 1;

set new.id = @id;

end;  

 

MYSQL,触发器,实现多个表共用ID不重复