使用MySQL Workbench我正在尝试转发架构,但我不断收到消息:
错误:错误1022:无法写入;表'tbl_banner_ad'中的重复键
我看过类似的问题,但所有的解决方案都说删除这个或删除它,但我不想删除那些会影响我的数据库工作方式的东西。
从我下面的SQL中,任何人都可以告诉我我需要做什么来摆脱这个错误,但不会摆脱任何参照完整性或类似的东西:
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 ibn_db
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `ibn_db` ;
CREATE SCHEMA IF NOT EXISTS `ibn_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `ibn_db` ;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_users` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_users` (
`_id` INT UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`name` VARCHAR(80) NOT NULL,
`email` VARCHAR(255) NOT NULL,
`address_building` VARCHAR(80) NULL,
`address_street` VARCHAR(80) NOT NULL,
`address_city` VARCHAR(80) NOT NULL,
`address_county` VARCHAR(80) NULL,
`address_postcode` VARCHAR(13) NOT NULL,
`address_country` VARCHAR(80) NULL,
`is_admin` TINYINT(1) NOT NULL DEFAULT 0,
`phone` VARCHAR(15) NOT NULL,
`company` VARCHAR(80) NULL,
`banned` TINYINT(1) NOT NULL DEFAULT 0,
`ban_reason` VARCHAR(160) NULL,
`ban_expiry_date` DATETIME NULL,
`password` VARCHAR(128) NOT NULL,
`salt` VARCHAR(128) NOT NULL,
`joined_datetime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_modified_datetime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`_id`),
UNIQUE INDEX `email_UNIQUE` (`email` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_unauthorized_users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_unauthorized_users` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_unauthorized_users` (
`_id` INT UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`email` VARCHAR(255) NOT NULL,
`password` VARCHAR(128) NOT NULL,
`salt` VARCHAR(128) NOT NULL,
`activation_hash` VARCHAR(128) NOT NULL,
`created_datetime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`_id`),
UNIQUE INDEX `email_UNIQUE` (`email` ASC),
UNIQUE INDEX `activation_hash_UNIQUE` (`activation_hash` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_events`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_events` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_events` (
`_id` INT ZEROFILL UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(80) NOT NULL,
`description` MEDIUMTEXT NULL,
`start_datetime` DATETIME NOT NULL,
`end_datetime` DATETIME NOT NULL,
`created_datetime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_modified_datetime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`address_building` VARCHAR(80) NULL,
`address_street` VARCHAR(80) NOT NULL,
`address_city` VARCHAR(80) NOT NULL,
`address_county` VARCHAR(80) NULL,
`address_postcode` VARCHAR(13) NOT NULL,
`address_country` VARCHAR(80) NULL,
`image_url` VARCHAR(255) NULL,
PRIMARY KEY (`_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_payments`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_payments` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_payments` (
`_id` INT ZEROFILL UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_attendee`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_attendee` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_attendee` (
`_id` INT ZEROFILL UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`event_id` INT UNSIGNED NOT NULL,
`payment_id` INT UNSIGNED NOT NULL,
`created_datetime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`_id`),
INDEX `user_id_idx` (`user_id` ASC),
INDEX `event_id_idx` (`event_id` ASC),
INDEX `payment_id_idx` (`payment_id` ASC),
CONSTRAINT `user_id`
FOREIGN KEY (`user_id`)
REFERENCES `ibn_db`.`tbl_users` (`_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `event_id`
FOREIGN KEY (`event_id`)
REFERENCES `ibn_db`.`tbl_events` (`_id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `payment_id`
FOREIGN KEY (`payment_id`)
REFERENCES `ibn_db`.`tbl_payments` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_banner_ad`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_banner_ad` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_banner_ad` (
`_id` INT UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`payment_id` INT UNSIGNED NOT NULL,
`url` VARCHAR(255) NOT NULL,
`width` INT UNSIGNED NOT NULL,
`height` INT UNSIGNED NOT NULL,
`created_datetime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`_id`),
INDEX `user_id_idx` (`user_id` ASC),
INDEX `payment_id_idx` (`payment_id` ASC),
CONSTRAINT `payment_id`
FOREIGN KEY (`payment_id`)
REFERENCES `ibn_db`.`tbl_payments` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `user_id`
FOREIGN KEY (`user_id`)
REFERENCES `ibn_db`.`tbl_users` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_reviews`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_reviews` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_reviews` (
`_id` INT UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`event_id` INT UNSIGNED NOT NULL,
`comment` VARCHAR(255) NULL,
`stars` INT NULL,
PRIMARY KEY (`_id`),
INDEX `user_id_idx` (`user_id` ASC),
INDEX `event_id_idx` (`event_id` ASC),
CONSTRAINT `user_id`
FOREIGN KEY (`user_id`)
REFERENCES `ibn_db`.`tbl_users` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `event_id`
FOREIGN KEY (`event_id`)
REFERENCES `ibn_db`.`tbl_events` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ibn_db`.`tbl_login_attempts`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ibn_db`.`tbl_login_attempts` ;
CREATE TABLE IF NOT EXISTS `ibn_db`.`tbl_login_attempts` (
`_id` INT UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ip` VARCHAR(45) NOT NULL,
`success` TINYINT(1) NOT NULL,
`fail_reason` VARCHAR(160) NULL,
PRIMARY KEY (`_id`),
INDEX `user_id_idx` (`user_id` ASC),
CONSTRAINT `user_id`
FOREIGN KEY (`user_id`)
REFERENCES `ibn_db`.`tbl_users` (`_id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
编辑:我尝试删除tbl_banner_ads中的INDEX语句但我无法删除它,因为索引是外键的一部分。
答案 0 :(得分:3)
尝试并更改tbl_banner_ad的CREATE
。改变这一部分:
CONSTRAINT `payment_id`
FOREIGN KEY (`payment_id`)
REFERENCES `ibn_db`.`tbl_payments` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `user_id`
FOREIGN KEY (`user_id`)
REFERENCES `ibn_db`.`tbl_users` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
为:
CONSTRAINT `payment_id1`
FOREIGN KEY (`payment_id`)
REFERENCES `ibn_db`.`tbl_payments` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `user_id1`
FOREIGN KEY (`user_id`)
REFERENCES `ibn_db`.`tbl_users` (`_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
(或者,您可以命名约束以反映父表和子表名以及columnn-name。即tbl_payment_tbl_banner_ad_payment_id
。