如何进行所有插入,然后检查外键约束? (SQL事务因外键约束而失败)

时间:2015-02-03 22:43:43

标签: mysql transactions

我有两张桌子:

enter image description here

每张牌组(卡牌)总是属于某个玩家(一个玩家可以拥有1个*牌组)。此外,每个总是的玩家都有一个(也是唯一一个)活动套牌。

我想添加一个新播放器,但如果我只执行INSERT INTO players(...) VALUES (...),我将不会在此查询中插入active_deck_id。同样,如果我从decks开始,我将不会player_id

这就是为什么我决定使用交易(在程序中):

CREATE PROCEDURE `add_player` (IN login VARCHAR(255), IN password VARCHAR(255), 
    OUT player_id INT(11))
BEGIN
START TRANSACTION;
INSERT INTO `players` (id, `login`, `password`, registered, last_logged, active_deck_id) 
    VALUES ('', login, pass, now(), now(), 0);

SET player_id = LAST_INSERT_ID();
INSERT INTO `decks` (id, name, player_id) VALUES ('', `sample deck`, player_id);

COMMIT;
END
END

然后我用它来调用它:

set @login = 'mylogin';
set @pass = 'pass';
call add_player(@login, @pass, @id);

...但我收到了一个错误:

#1452 - Cannot add or update a child row: a foreign key constraint fails (`game`.`players`, CONSTRAINT `active_deck` FOREIGN KEY (`active_deck_id`) REFERENCES `decks` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) 

那么,如何进行所有插入,然后检查外键约束?因为在第一次插入后,约束应该被破坏,但在整个事务之后它们应该没问题。

我桌子的SQL:

CREATE TABLE IF NOT EXISTS `game`.`decks` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `player_id` INT UNSIGNED NOT NULL,
  PRIMARY KEY (`id`, `player_id`),
  INDEX `player` (`player_id` ASC),
  CONSTRAINT `player`
    FOREIGN KEY (`player_id`)
    REFERENCES `game`.`players` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `game`.`players` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `login` VARCHAR(255) NOT NULL,
  `password` VARCHAR(255) NOT NULL,
  `registered` DATETIME NOT NULL,
  `last_logged` DATETIME NOT NULL,
  `active_deck_id` INT UNSIGNED NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `active_deck` (`active_deck_id` ASC),
  CONSTRAINT `active_deck`
    FOREIGN KEY (`active_deck_id`)
    REFERENCES `game`.`decks` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

1 个答案:

答案 0 :(得分:2)

尝试此命令

SET FOREIGN_KEY_CHECKS = 0;

在进行插入之前

,然后通过将值设置为1来打开它。