在插入触发器之前使用MYSQL中的自动增量值?

Kea*_*eia 9 mysql triggers

用户表:

CREATE TABLE `users` (
  `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(45) DEFAULT NULL,
  `username` varchar(16) DEFAULT NULL,
  `salt` varchar(16) DEFAULT NULL,
  `password` varchar(128) DEFAULT NULL,
  `lastlogin` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `joined` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `loggedin` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `sessionkey` varchar(60) DEFAULT NULL,
  `verifycode` varchar(16) DEFAULT NULL,
  `verified` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `banned` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `locked` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `ip_address` varchar(45) DEFAULT NULL,
  `failedattempts` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `unlocktime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
Run Code Online (Sandbox Code Playgroud)

user_records表:

CREATE TABLE `user_records` (
  `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
  `userid` int(8) unsigned DEFAULT NULL,
  `action` varchar(100) DEFAULT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
Run Code Online (Sandbox Code Playgroud)

users表上的before insert触发器:

USE `gknet`;
DELIMITER $$
CREATE DEFINER=`root`@`localhost` TRIGGER `before_create_user` BEFORE INSERT ON `users` FOR EACH ROW BEGIN
INSERT INTO user_records (action, userid, timestamp)
  VALUES ('CREATED', ID, NOW() );
END
Run Code Online (Sandbox Code Playgroud)

基本上,我的问题是,当我尝试输入由MySQL自动分配的用户的id(PK,NN,自动递增)时触发器,它只是在user_records表中为userid放入0.我该怎么做才能选择用户通过SQL分配的id,并将其作为userid放在记录条目上(ID在'CREATED'之后)?

此外,如果您看到可以对表格进行任何其他优化,请随时告诉我:D

Rav*_*ddy 17

OP的评论:
我以前怎么做,你呢?

您可以找到auto_increment要分配给新记录的当前值.
并在before触发器中使用相同的user_records表作为父用户id .
您必须查询information_schema.tables表以查找值.

示例:

use `gknet`;

delimiter $$

drop trigger if exists before_create_user; $$

create definer=`root`@`localhost` trigger `before_create_user` 
       before insert on `users` 
for each row begin
  declare fk_parent_user_id int default 0;

  select auto_increment into fk_parent_user_id
    from information_schema.tables
   where table_name = 'users'
     and table_schema = database();

  insert into user_records ( action, userid, timestamp )
         values ( 'created', fk_parent_user_id, now() );
end;

$$

delimiter ;
Run Code Online (Sandbox Code Playgroud)


Abh*_*rty 8

将触发器更改为after insert而不是before insert并用于NEW获取最后插入的 id

USE `gknet`;
DELIMITER $$
CREATE DEFINER=`root`@`localhost` 
TRIGGER `after_create_user` AFTER INSERT ON `users` 
FOR EACH ROW 
BEGIN
INSERT INTO user_records (action, userid, timestamp)
  VALUES ('CREATED', NEW.ID, NOW() );
END; $$
Run Code Online (Sandbox Code Playgroud)

  • @AbhikChakraborty:不,不需要手动增加值。您可以通过读取下一个要分配的可用 auto_increment 值来实现此目的。检查我的答案。 (2认同)
  • @RichardAQuadling:在插入下一条记录之前,似乎有一些关于使用 **auto_increment** 捕获进行的测试。建议在批处理过程中不要依赖。请阅读我的答案之一下的评论:/sf/answers/1564028581/。 (2认同)