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

时间:2021-07-14 05:06:27

前后台没有分开,为了区分前后台用户,所以分表,但是ID不能重复,因为关联了权限表.

 

这里实现后台用户表使用奇数ID

前台用户表使用偶数ID

 

 

MYSQL 没有sequence 

SET @@auto_increment_offset
SET @@auto_increment_increment

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

 

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
--
Table structure for acl_user
--
----------------------------
DROP TABLE IF EXISTS `acl_user`;
CREATE TABLE `acl_user` (
`id`
int(11) NOT NULL AUTO_INCREMENT,
`userName`
varchar(50) NOT NULL COMMENT '用户名',
`password`
varchar(50) NOT NULL COMMENT '密码',
`enabled`
tinyint(4) DEFAULT NULL,
`accountNonExpired`
tinyint(4) DEFAULT NULL,
`credentialsNonExpired`
tinyint(4) DEFAULT NULL,
`accountNonLocked`
tinyint(4) DEFAULT NULL,
`
desc` varchar(255) DEFAULT NULL COMMENT '描述',
`insertDate`
timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE
=InnoDB DEFAULT CHARSET=utf8;
DROP TRIGGER IF EXISTS `InsertAclUser`;
DELIMITER ;;
CREATE TRIGGER `InsertAclUser` BEFORE INSERT ON `acl_user` FOR EACH ROW Begin
SET session auto_increment_offset=1;
SET session auto_increment_increment=2;
end
;;
DELIMITER ;

-- ----------------------------
--
Records of acl_user
--
----------------------------
INSERT INTO `acl_user` VALUES ( NULL,'adminstrator', '123456aa', '1', '1', '1', '1', null, '2017-04-14 10:51:45');
















SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
--
Table structure for invoice_userinfo
--
----------------------------
DROP TABLE IF EXISTS `userinfo`;
CREATE TABLE `userinfo` (
`id`
int(11) NOT NULL AUTO_INCREMENT,
`usrname`
varchar(15) COLLATE utf8_general_mysql500_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE
=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_mysql500_ci;

-- ----------------------------
--
Records of invoice_userinfo
--
----------------------------
DROP TRIGGER IF EXISTS `InsertUser`;
DELIMITER ;;
CREATE TRIGGER `InsertUser` BEFORE INSERT ON `userinfo` FOR EACH ROW Begin
SET session auto_increment_offset=2;
SET session auto_increment_increment=2;
end
;;
DELIMITER ;