Getting ERROR 1701, ERROR 1452 and ERROR 1305 errors in MySQL - Need some expertise

。_饼干妹妹 提交于 2019-12-06 03:49:34

You have to add this statement as well Add it at the top of your dump file

SET foreign_key_checks = 0;

This will disable the foreign key checks and you can easily execute the dump file

and add another statement at the bottom of your dump file to enable back the foreign key check

SET foreign_key_checks = 1;

The Foreign Key constraints you have between person (the Superclass) and player, parent and coach (the Subclasses) are not defining 1:1 relationships. Here is a possible solution:

-- Create the "person" table.
--
-- This table has one:one relationships
-- with the parent, coach and player 
-- tables.
DROP TABLE IF EXISTS `person` ;                --- no change
CREATE TABLE `person` (
  `personID` INT(5) NOT NULL AUTO_INCREMENT ,
  --- ...
  --- several columns omitted for clarity
  --- ...
  PRIMARY KEY (`personID`))
ENGINE = InnoDB;

The parent table can have the personID both as Primary and Foreign Key to person:

-- Create the "parent" table.
DROP TABLE IF EXISTS `parent` ;
CREATE TABLE `parent` (
  --- `parentID` INT(5) NOT NULL ,             --- removed

  `personID` INT(5) NOT NULL ,

  --- PRIMARY KEY (`parentID`, `personID`),    --- removed

  PRIMARY KEY (`personID`),                    --- this part only as PK
                                               --- which is also a FK
  FOREIGN KEY (`personID`)                     --- as previously (no change here)
    REFERENCES `person` (`personID`)
    ON DELETE CASCADE 
    ON UPDATE CASCADE)
ENGINE = InnoDB;

And similarly for player:

-- Create the "school" table.
-- omitted for clarity
--
--    
-- Create the "player" table.
--
-- Inherits fields from the "person"
-- and "school" tables.
DROP TABLE IF EXISTS `player` ;
CREATE TABLE `player` (
  --- `playerID` INT(5) NOT NULL ,             --- removed
  `personID` INT(5) NOT NULL ,
  `schoolID` INT(5) NOT NULL ,
  PRIMARY KEY (`personID`),                    --- Primary Key
  FOREIGN KEY (`personID`)                     --- that is also Foreign Key
    REFERENCES `person` (`personID`)
    ON DELETE CASCADE 
    ON UPDATE CASCADE, 
  FOREIGN KEY (`schoolID`)
    REFERENCES `school` (`schoolID`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

The same should be done to the coach table.


Note that the above solution allows a person to be both a player and a parent or both a parent and a coach. If you want to restrict a row of the superclass (person) table to be only one the subclass tables, it would have to be a slightly different approach.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!