MySQL错误:写入或更新时重复键

时间:2016-01-18 12:15:08

标签: mysql sql mysql-workbench

我一直在使用MySQL来设计数据库,当编译成SQL代码并执行它时会产生两个错误。一个表示Variable 'unique checks' can't be set to the value of 'NULL'而另一个表示消息"Duplicate key on write and update"我觉得这是我的外键导致了这个问题,但仍然无法解决问题。我在下面附上了查询代码。

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `mydb` ;

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User` ;

CREATE TABLE IF NOT EXISTS `mydb`.`User` (
  `idUser` INT NOT NULL,
  `first_name` VARCHAR(45) NULL,
  `last_name` VARCHAR(45) NULL,
  `username` VARCHAR(45) NULL,
  `password` VARCHAR(45) NULL,
  `company_pin` VARCHAR(45) NULL,
  `student_pin` VARCHAR(45) NULL,
  `isAdmin` TINYINT(1) NULL DEFAULT 0,
  `isCandidate` TINYINT(1) NULL,
  `isVoter` TINYINT(1) NULL,
  `votes_left` INT NULL,
  `votes_achieved` INT NULL,
  PRIMARY KEY (`idUser`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`Issue`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Issue` ;

CREATE TABLE IF NOT EXISTS `mydb`.`Issue` (
  `idIssue` INT NOT NULL,
  `issueTitle` VARCHAR(45) NULL,
  `issueDescription` VARCHAR(255) NULL,
  `userID` INT NULL,
  `isResolved` TINYINT(1) NULL DEFAULT 0,
  `upVotes` INT NULL,
  PRIMARY KEY (`idIssue`),
  INDEX `FK_USERID_idx` (`userID` ASC),
  CONSTRAINT `FK_USERID`
    FOREIGN KEY (`userID`)
    REFERENCES `mydb`.`User` (`idUser`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`Forum`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Forum` ;

CREATE TABLE IF NOT EXISTS `mydb`.`Forum` (
  `idForum` INT NOT NULL,
  `userID` INT NULL,
  `postTitle` VARCHAR(45) NULL,
  `postText` VARCHAR(255) NULL,
  `upVotes` INT NULL,
  PRIMARY KEY (`idForum`),
  INDEX `FK_USERID_idx` (`userID` ASC),
  CONSTRAINT `FK_USERID`
    FOREIGN KEY (`userID`)
    REFERENCES `mydb`.`User` (`idUser`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`Vote`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Vote` ;

CREATE TABLE IF NOT EXISTS `mydb`.`Vote` (
  `idVote` INT NOT NULL,
  `voteFor` ENUM('TOPIC', 'USER', 'ISSUE') NULL,
  `topicID` INT NULL,
  `userID` INT NULL,
  `issueID` INT NULL,
  PRIMARY KEY (`idVote`),
  INDEX `FK_USER_VOTED_FOR_idx` (`userID` ASC),
  INDEX `FK_ISSUE_VOTED_FOR_idx` (`issueID` ASC),
  INDEX `FK_FORUM_VOTED_FOR_idx` (`topicID` ASC),
  CONSTRAINT `FK_USER_VOTED_FOR`
    FOREIGN KEY (`userID`)
    REFERENCES `mydb`.`User` (`idUser`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `FK_ISSUE_VOTED_FOR`
    FOREIGN KEY (`issueID`)
    REFERENCES `mydb`.`Issue` (`idIssue`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `FK_FORUM_VOTED_FOR`
    FOREIGN KEY (`topicID`)
    REFERENCES `mydb`.`Forum` (`idForum`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

2 个答案:

答案 0 :(得分:5)

您已在表FK_USERID上拥有名为mydb的约束。issue。给mydbForum另一个名称约束。 e.g。

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `mydb` ;

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User` ;

CREATE TABLE IF NOT EXISTS `mydb`.`User` (
  `idUser` INT NOT NULL,
  `first_name` VARCHAR(45) NULL,
  `last_name` VARCHAR(45) NULL,
  `username` VARCHAR(45) NULL,
  `password` VARCHAR(45) NULL,
  `company_pin` VARCHAR(45) NULL,
  `student_pin` VARCHAR(45) NULL,
  `isAdmin` TINYINT(1) NULL DEFAULT 0,
  `isCandidate` TINYINT(1) NULL,
  `isVoter` TINYINT(1) NULL,
  `votes_left` INT NULL,
  `votes_achieved` INT NULL,
  PRIMARY KEY (`idUser`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`Issue`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Issue` ;

CREATE TABLE IF NOT EXISTS `mydb`.`Issue` (
  `idIssue` INT NOT NULL,
  `issueTitle` VARCHAR(45) NULL,
  `issueDescription` VARCHAR(255) NULL,
  `userID` INT NULL,
  `isResolved` TINYINT(1) NULL DEFAULT 0,
  `upVotes` INT NULL,
  PRIMARY KEY (`idIssue`),
  INDEX `FK_USERID_idx` (`userID` ASC),
  CONSTRAINT `FK_USERID`
    FOREIGN KEY (`userID`)
    REFERENCES `mydb`.`User` (`idUser`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`Forum`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Forum` ;

CREATE TABLE IF NOT EXISTS `mydb`.`Forum` (
  `idForum` INT NOT NULL,
  `userID` INT NULL,
  `postTitle` VARCHAR(45) NULL,
  `postText` VARCHAR(255) NULL,
  `upVotes` INT NULL,
  PRIMARY KEY (`idForum`),
  INDEX `FK_USERID_idx` (`userID` ASC),
  CONSTRAINT `FK_Forum_USERID`
    FOREIGN KEY (`userID`)
    REFERENCES `mydb`.`User` (`idUser`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`Vote`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Vote` ;

CREATE TABLE IF NOT EXISTS `mydb`.`Vote` (
  `idVote` INT NOT NULL,
  `voteFor` ENUM('TOPIC', 'USER', 'ISSUE') NULL,
  `topicID` INT NULL,
  `userID` INT NULL,
  `issueID` INT NULL,
  PRIMARY KEY (`idVote`),
  INDEX `FK_USER_VOTED_FOR_idx` (`userID` ASC),
  INDEX `FK_ISSUE_VOTED_FOR_idx` (`issueID` ASC),
  INDEX `FK_FORUM_VOTED_FOR_idx` (`topicID` ASC),
  CONSTRAINT `FK_USER_VOTED_FOR`
    FOREIGN KEY (`userID`)
    REFERENCES `mydb`.`User` (`idUser`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `FK_ISSUE_VOTED_FOR`
    FOREIGN KEY (`issueID`)
    REFERENCES `mydb`.`Issue` (`idIssue`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `FK_FORUM_VOTED_FOR`
    FOREIGN KEY (`topicID`)
    REFERENCES `mydb`.`Forum` (`idForum`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

答案 1 :(得分:1)

还原数据库时出现此错误,我发现以下详细信息:

当您的表/数据库名称与双方在同一表/数据库名称中使用的小写/大写字母不同时,就会出现此问题(需要在双方保持相同的名称)区分大小写)。