내 클라이언트 및/또는 사이트 테이블에 레코드를 추가하려고하면 다음 오류가 발생합니다.MySQL 외래 키 오류 1452
스키마 생성 실패 : 추가 또는 자식 행을 업데이트 할 수 없습니다 : 외래 키 제약 조건이 실패합니다. (
db_2_6ceaf
client
은 CONSTRAINTclient2offer
FOREIGN KEY (clientID
는) 참고 문헌 UPDATE ON NO ACTION NO ACTION 삭제하지 ONoffer_to_client
(clientID
)) :
SQL :
SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @[email protected]@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Table `offer_to_category`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `offer_to_category` (
`offerID` INT UNSIGNED NOT NULL ,
`categoryID` INT UNSIGNED NOT NULL ,
INDEX `offer_to_category` (`offerID` ASC, `categoryID` ASC) ,
INDEX `o2c_categoryID` (`categoryID` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `offer_to_client`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `offer_to_client` (
`offerID` INT UNSIGNED NOT NULL ,
`clientID` INT UNSIGNED NOT NULL ,
INDEX `offer_to_client` (`offerID` ASC, `clientID` ASC) ,
INDEX `o2cl_clientID` (`clientID` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `offer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `offer` (
`offerID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`offerName` VARCHAR(255) NULL ,
`offerDescription` LONGTEXT NULL ,
`offerAction` TEXT NULL ,
`offerStart` BIGINT NULL ,
`offerEnd` BIGINT NULL ,
`offerStatus` TINYINT(1) NULL ,
PRIMARY KEY (`offerID`) ,
CONSTRAINT `offer2cat`
FOREIGN KEY (`offerID`)
REFERENCES `offer_to_category` (`offerID`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `offer2client`
FOREIGN KEY (`offerID`)
REFERENCES `offer_to_client` (`offerID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `category`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `category` (
`categoryID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`categoryName` VARCHAR(255) NULL ,
`categoryDescription` LONGTEXT NULL ,
`categoryStatus` TINYINT(1) NULL ,
PRIMARY KEY (`categoryID`) ,
CONSTRAINT `cat2offer`
FOREIGN KEY (`categoryID`)
REFERENCES `offer_to_category` (`categoryID`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `user` (
`userID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`clientID` INT UNSIGNED NOT NULL ,
`userEmail` VARCHAR(255) NULL ,
`userFirstName` VARCHAR(255) NULL ,
`userLastName` VARCHAR(255) NULL ,
`userRegistered` BIGINT NULL ,
`userStatus` TINYINT(1) NULL ,
PRIMARY KEY (`userID`) ,
INDEX `client` (`clientID` ASC, `userEmail` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `client`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `client` (
`clientID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`siteID` INT UNSIGNED NOT NULL ,
`clientName` VARCHAR(255) NULL ,
`clientDescription` LONGTEXT NULL ,
`clientUrl` LONGTEXT NULL ,
`clientStatus` TINYINT(1) NULL ,
PRIMARY KEY (`clientID`) ,
INDEX `clientsiteid` (`siteID` ASC) ,
CONSTRAINT `client2offer`
FOREIGN KEY (`clientID`)
REFERENCES `offer_to_client` (`clientID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `client2user`
FOREIGN KEY (`clientID`)
REFERENCES `user` (`clientID`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `admins`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `admins` (
`adminID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`adminName` VARCHAR(255) NULL ,
`adminEmail` LONGTEXT NULL ,
`adminUsername` VARCHAR(255) NULL ,
`adminStatus` TINYINT(1) NULL ,
PRIMARY KEY (`adminID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `site`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `site` (
`siteID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`siteName` VARCHAR(255) NULL ,
`siteURL` LONGTEXT NULL ,
`siteStyles` LONGTEXT NULL ,
`siteImages` LONGTEXT NULL ,
`sitesStatus` TINYINT(1) NULL ,
PRIMARY KEY (`siteID`) ,
CONSTRAINT `site2client`
FOREIGN KEY (`siteID`)
REFERENCES `client` (`siteID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET [email protected]_SQL_MODE;
SET [email protected]_FOREIGN_KEY_CHECKS;
SET [email protected]_UNIQUE_CHECKS;
-- -----------------------------------------------------
-- Data for table `client`
-- -----------------------------------------------------
START TRANSACTION;
INSERT INTO `client` (`clientID`, `siteID`, `clientName`, `clientDescription`, `clientUrl`, `clientStatus`) VALUES (NULL, 1, 'Weight Watchers', 'Weight Watchers microsite', 'weightwachers', 1);
INSERT INTO `client` (`clientID`, `siteID`, `clientName`, `clientDescription`, `clientUrl`, `clientStatus`) VALUES (NULL, 1, 'A Uni', NULL, NULL, 1);
INSERT INTO `client` (`clientID`, `siteID`, `clientName`, `clientDescription`, `clientUrl`, `clientStatus`) VALUES (NULL, 2, 'Ollie Biz', '', NULL, 1);
INSERT INTO `client` (`clientID`, `siteID`, `clientName`, `clientDescription`, `clientUrl`, `clientStatus`) VALUES (NULL, 2, 'Ollie Uni', NULL, 'ollieuni', 1);
COMMIT;
-- -----------------------------------------------------
-- Data for table `site`
-- -----------------------------------------------------
START TRANSACTION;
INSERT INTO `site` (`siteID`, `siteName`, `siteURL`, `siteStyles`, `siteImages`, `sitesStatus`) VALUES (1, 'We Love Rewards', 'http://www.weloverewards.co.uk', NULL, NULL, 1);
INSERT INTO `site` (`siteID`, `siteName`, `siteURL`, `siteStyles`, `siteImages`, `sitesStatus`) VALUES (2, 'Ollie Rewards', 'http://www.olliesrewards.co.uk', NULL, NULL, 1);
COMMIT;
클라이언트 <-> 제공 업체 제약 조건이 클라이언트 테이블이 아닌 client2offer 테이블에 적용되지 않습니까? (client_to_offer에 항목을 추가하여 새 클라이언트와 일치시킨 다음 Client 레코드를 삽입하면 클라이언트에 제약 조건을 적용 할 수 있습니다. – ModulusJoe
모든 외래 키 제약 조건이 잘못된 테이블에 선언 된 것 같습니다. 그들은 다른 방향으로 있어야합니다. –