2012-04-21 5 views
3

이것은 제 3 정규형으로 데이터베이스를 작성한 첫 번째 실제 균열입니다. DDL 스크립트를 만들었지 만 (스크립트를 작성하기 전에 논리적 모델을 3NF로 확인했습니다.) 오류를 많이 겪었습니다. 나는이 특별한 경우를 위해 씹을 수있는 것보다 더 물릴지도 모르지만 나는 포기하고 싶지 않습니다. 그것은 나에게 커다란 학습 곡선이며, 이것을 지나쳐서 계속 나아갈 수있는 도움이됩니다.MySQL에서 ERROR 1701, ERROR 1452 및 ERROR 1305 오류가 발생합니다 - 전문 지식이 필요합니다

모든

첫째, 여기 내 DDL 스크립트입니다 :

-- This sql script creates the structure. 
-- of the rugby club database. 

DROP DATABASE IF EXISTS database_rugby; 

CREATE DATABASE database_rugby; 

USE database_rugby; 

-- Create the "coach" table. 
DROP TABLE IF EXISTS `database_rugby`.`coach` ; 
CREATE TABLE IF NOT EXISTS `database_rugby`.`coach` (
    `coachID` INT(5) NOT NULL , 
    `dateBeganCoaching` DATE NOT NULL , 
    `personID` INT(5) NOT NULL , 
    PRIMARY KEY (`coachID`)) 
ENGINE = InnoDB; 


-- Create the "grade" table. 
DROP TABLE IF EXISTS `database_rugby`.`grade` ; 
CREATE TABLE IF NOT EXISTS `database_rugby`.`grade` (
    `gradeID` INT(5) NOT NULL AUTO_INCREMENT , 
    `gradeName` VARCHAR(50) NOT NULL , 
    `minWeight` INT(3) NOT NULL , 
    `maxWeight` INT(3) NOT NULL , 
    `minAge` INT(3) NOT NULL , 
    `maxAge` INT(3) NOT NULL , 
    `ballSize` INT(1) NOT NULL , 
    PRIMARY KEY (`gradeID`)) 
ENGINE = InnoDB; 


-- Create the "coachQualification" table. 
DROP TABLE IF EXISTS `database_rugby`.`coachQualification` ; 

CREATE TABLE IF NOT EXISTS `database_rugby`.`coachQualification` (
    `qualID` INT(5) NOT NULL AUTO_INCREMENT , 
    `qualName` CHAR(5) NOT NULL , 
    `gradeID` INT(5) NOT NULL , 
    PRIMARY KEY (`qualID`) , 
    INDEX `gradeID` (`gradeID` ASC) , 
    CONSTRAINT `coachQualification_ibfk_1` 
    FOREIGN KEY (`gradeID`) 
    REFERENCES `database_rugby`.`grade` (`gradeID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE) 
ENGINE = InnoDB; 


-- Create the "parent" table. 
DROP TABLE IF EXISTS `database_rugby`.`parent` ; 
CREATE TABLE IF NOT EXISTS `database_rugby`.`parent` (
    `parentID` INT(5) NOT NULL , 
    `personID` INT(5) NOT NULL , 
    PRIMARY KEY (`parentID`)) 
ENGINE = InnoDB; 


-- Create the "school" table. 
DROP TABLE IF EXISTS `database_rugby`.`school` ; 
CREATE TABLE IF NOT EXISTS `database_rugby`.`school` (
    `schoolID` INT(5) NOT NULL AUTO_INCREMENT , 
    `schoolName` VARCHAR(100) NOT NULL , 
    PRIMARY KEY (`schoolID`)) 
ENGINE = InnoDB; 


-- Create the "player" table. 
-- 
-- Inherits fields from the "person" 
-- and "school" tables. 
DROP TABLE IF EXISTS `database_rugby`.`player` ; 

CREATE TABLE IF NOT EXISTS `database_rugby`.`player` (
    `playerID` INT(5) NOT NULL , 
    `personID` INT(5) NOT NULL , 
    `schoolID` INT(5) NOT NULL , 
    PRIMARY KEY (`playerID`) , 
    INDEX `schoolID` (`schoolID` ASC) , 
    CONSTRAINT `player_ibfk_1` 
    FOREIGN KEY (`schoolID`) 
    REFERENCES `database_rugby`.`school` (`schoolID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE) 
ENGINE = InnoDB; 


-- Create the "person" table. 
-- 
-- This table has one:one relationships 
-- with the parent, coach and player 
-- tables. 
DROP TABLE IF EXISTS `database_rugby`.`person` ; 
CREATE TABLE IF NOT EXISTS `database_rugby`.`person` (
    `personID` INT(5) NOT NULL AUTO_INCREMENT , 
    `firstName` VARCHAR(50) NOT NULL , 
    `lastName` VARCHAR(50) NOT NULL , 
    `dateOfBirth` DATE NOT NULL , 
    `streetAddress` VARCHAR(150) NOT NULL , 
    `suburbAddress` VARCHAR(150) NULL DEFAULT NULL , 
    `cityAddress` VARCHAR(150) NOT NULL , 
    `photo` BLOB NULL DEFAULT NULL , 
    `parent_parentID` INT(5) NOT NULL DEFAULT '0' , 
    `coach_coachID` INT(5) NOT NULL DEFAULT '0' , 
    `player_playerID` INT(5) NOT NULL DEFAULT '0' , 
    `parent_parentID1` INT(5) NOT NULL DEFAULT '0' , 
    `player_playerID1` INT(5) NOT NULL DEFAULT '0' , 
    `coach_coachID1` INT(5) NOT NULL DEFAULT '0' , 
    `coach_coachID2` INT(5) NOT NULL DEFAULT '0' , 
    `parent_parentID2` INT(5) NOT NULL DEFAULT '0' , 
    `player_playerID2` INT(5) NOT NULL DEFAULT '0' , 
    PRIMARY KEY (`personID`) , 
    INDEX `fk_person_coach1` (`coach_coachID2` ASC) , 
    INDEX `fk_person_parent1` (`parent_parentID2` ASC) , 
    INDEX `fk_person_player1` (`player_playerID2` ASC) , 
    CONSTRAINT `fk_person_coach1` 
    FOREIGN KEY (`coach_coachID2`) 
    REFERENCES `database_rugby`.`coach` (`coachID`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    CONSTRAINT `fk_person_parent1` 
    FOREIGN KEY (`parent_parentID2`) 
    REFERENCES `database_rugby`.`parent` (`parentID`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    CONSTRAINT `fk_person_player1` 
    FOREIGN KEY (`player_playerID2`) 
    REFERENCES `database_rugby`.`player` (`playerID`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 


-- Create the "homePhone" table. 
DROP TABLE IF EXISTS `database_rugby`.`homePhone` ; 
CREATE TABLE IF NOT EXISTS `database_rugby`.`homePhone` (
    `homePhoneID` INT(5) NOT NULL AUTO_INCREMENT , 
    `homeNumber` CHAR(9) NOT NULL , 
    PRIMARY KEY (`homePhoneID`)) 
ENGINE = InnoDB; 


-- Create the "mobilePhone" table. 
DROP TABLE IF EXISTS `database_rugby`.`mobilePhone` ; 

CREATE TABLE IF NOT EXISTS `database_rugby`.`mobilePhone` (
    `mobilePhoneID` INT(5) NOT NULL AUTO_INCREMENT , 
    `mobileNumber` CHAR(10) NULL DEFAULT NULL , 
    PRIMARY KEY (`mobilePhoneID`)) 
ENGINE = InnoDB; 


-- Create the "emailAddress" table. 
DROP TABLE IF EXISTS `database_rugby`.`emailAddress` ; 

CREATE TABLE IF NOT EXISTS `database_rugby`.`emailAddress` (
    `emailAddressID` INT(5) NOT NULL AUTO_INCREMENT , 
    `emailAddress` CHAR(10) NULL DEFAULT NULL , 
    PRIMARY KEY (`emailAddressID`)) 
ENGINE = InnoDB; 


-- Create the "Contact" table 
-- 
-- This is a linking table 
-- that describes the many:many 
-- relationships between "person" 
-- and the "homePhone", "mobilePhone", 
-- and "emailAddress" tables. 
DROP TABLE IF EXISTS `database_rugby`.`contact` ; 
CREATE TABLE IF NOT EXISTS `database_rugby`.`contact` (
    `personID` INT(5) NOT NULL , 
    `homePhoneID` INT(5) NOT NULL , 
    `mobilePhoneID` INT(5) NULL DEFAULT NULL , 
    `emailAddressID` INT(5) NULL DEFAULT NULL , 
    INDEX `personID` (`personID` ASC) , 
    INDEX `homePhoneID` (`homePhoneID` ASC) , 
    INDEX `mobilePhoneID` (`mobilePhoneID` ASC) , 
    INDEX `emailAddressID` (`emailAddressID` ASC) , 
    CONSTRAINT `contact_ibfk_1` 
    FOREIGN KEY (`personID`) 
    REFERENCES `database_rugby`.`person` (`personID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE, 
    CONSTRAINT `contact_ibfk_2` 
    FOREIGN KEY (`homePhoneID`) 
    REFERENCES `database_rugby`.`homePhone` (`homePhoneID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE, 
    CONSTRAINT `contact_ibfk_3` 
    FOREIGN KEY (`mobilePhoneID`) 
    REFERENCES `database_rugby`.`mobilePhone` (`mobilePhoneID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE, 
    CONSTRAINT `contact_ibfk_4` 
    FOREIGN KEY (`emailAddressID`) 
    REFERENCES `database_rugby`.`emailAddress` (`emailAddressID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE) 
ENGINE = InnoDB; 


-- Create the "family" table. 
-- 
-- This is a linking table 
-- that describes the many:many 
-- relationship between "parent" 
-- and "player" tables. 
DROP TABLE IF EXISTS `database_rugby`.`family` ; 
CREATE TABLE IF NOT EXISTS `database_rugby`.`family` (
    `parentID` INT(5) NOT NULL , 
    `playerID` INT(5) NOT NULL , 
    `parent_parentID` INT(5) NOT NULL , 
    INDEX `playerID` (`playerID` ASC) , 
    INDEX `fk_family_parent1` (`parent_parentID` ASC) , 
    CONSTRAINT `family_ibfk_2` 
    FOREIGN KEY (`playerID`) 
    REFERENCES `database_rugby`.`player` (`playerID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE, 
    CONSTRAINT `fk_family_parent1` 
    FOREIGN KEY (`parent_parentID`) 
    REFERENCES `database_rugby`.`parent` (`parentID`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 


-- Create the "qualificationSet" table. 
-- 
-- This is a linking table 
-- that describes the many:many 
-- relationship between "coach" 
-- and "coachQualification" tables. 
DROP TABLE IF EXISTS `database_rugby`.`qualificationSet` ; 
CREATE TABLE IF NOT EXISTS `database_rugby`.`qualificationSet` (
    `coachID` INT(5) NOT NULL , 
    `qualID` INT(5) NOT NULL , 
    INDEX `coachID` (`coachID` ASC) , 
    INDEX `qualID` (`qualID` ASC) , 
    CONSTRAINT `qualificationSet_ibfk_1` 
    FOREIGN KEY (`coachID`) 
    REFERENCES `database_rugby`.`coach` (`coachID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE, 
    CONSTRAINT `qualificationSet_ibfk_2` 
    FOREIGN KEY (`qualID`) 
    REFERENCES `database_rugby`.`coachQualification` (`qualID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE) 
ENGINE = InnoDB; 


-- Create the "team" table. 
DROP TABLE IF EXISTS `database_rugby`.`team` ; 
CREATE TABLE IF NOT EXISTS `database_rugby`.`team` (
    `teamID` INT(5) NOT NULL AUTO_INCREMENT , 
    `teamName` VARCHAR(50) NOT NULL , 
    `teamYear` INT(2) NOT NULL , 
    `gradeID` INT(5) NOT NULL , 
    PRIMARY KEY (`teamID`) , 
    INDEX `gradeID` (`gradeID` ASC) , 
    CONSTRAINT `team_ibfk_1` 
    FOREIGN KEY (`gradeID`) 
    REFERENCES `database_rugby`.`grade` (`gradeID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE) 
ENGINE = InnoDB; 


-- Create the "teamAllocation" table 
-- 
-- this is a linking table for a 
-- many:many relationship between 
-- team and player tables. 
DROP TABLE IF EXISTS `database_rugby`.`teamAllocation` ; 

CREATE TABLE IF NOT EXISTS `database_rugby`.`teamAllocation` (
    `teamID` INT(5) NOT NULL , 
    `playerID` INT(5) NOT NULL , 
    INDEX `teamID` (`teamID` ASC) , 
    INDEX `playerID` (`playerID` ASC) , 
    CONSTRAINT `teamallocation_ibfk_1` 
    FOREIGN KEY (`teamID`) 
    REFERENCES `database_rugby`.`team` (`teamID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE, 
    CONSTRAINT `teamAllocation_ibfk_2` 
    FOREIGN KEY (`playerID`) 
    REFERENCES `database_rugby`.`player` (`playerID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE) 
ENGINE = InnoDB; 


-- Create the "teamCoachAllocation" table. 
-- 
-- This is a linking table 
-- that describes the many:many 
-- relationship between "coach" 
-- and "team" tables. 
DROP TABLE IF EXISTS `database_rugby`.`teamCoachAllocation` ; 
CREATE TABLE `database_rugby`.`teamCoachAllocation` (
    `coachID` INT(5) NOT NULL , 
    `teamID` INT(5) NOT NULL , 
    INDEX `coachID` (`coachID` ASC) , 
    INDEX `teamID` (`teamID` ASC) , 
    CONSTRAINT `teamCoachAllocation_ibfk_1` 
    FOREIGN KEY (`coachID`) 
    REFERENCES `database_rugby`.`coach` (`coachID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE, 
    CONSTRAINT `teamCoachAllocation_ibfk_2` 
    FOREIGN KEY (`teamID`) 
    REFERENCES `database_rugby`.`team` (`teamID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE) 
ENGINE = InnoDB; 

그리고 여기 내 DML 스크립트 (주 : 지금까지 스크립트 만 "사람"에 레코드를 삽입하려고, "학교"와 .

-- database_data.sql. 
-- This sql script inserts data into the 
-- rugby club database. 

USE database_rugby; 

TRUNCATE TABLE database_rugby.person; 

-- Insert new persons which will be 
-- players. 
INSERT INTO database_rugby.person (
firstName, 
lastName, 
dateOfBirth, 
streetAddress, 
suburbAddress, 
cityAddress) 
VALUES(
"Michael", 
"Peck", 
'2002-12-10', 
"45 Skibo Street", 
"Caversham", 
"Dunedin"); 

INSERT INTO database_rugby.person (
firstName, 
lastName, 
dateOfBirth, 
streetAddress, 
suburbAddress, 
cityAddress) 
VALUES(
"Matt", 
"Petersen", 
'2001-06-15', 
"192 Bayview Road", 
"South Dunedin", 
"Dunedin"); 

INSERT INTO database_rugby.person (
firstName, 
lastName, 
dateOfBirth, 
streetAddress, 
suburbAddress, 
cityAddress) 
VALUES(
"Christopher", 
"Petersen", 
'2003-02-19', 
"192 Bayview Road", 
"South Dunedin", 
"Dunedin"); 

INSERT INTO database_rugby.person (
firstName, 
lastName, 
dateOfBirth, 
streetAddress, 
suburbAddress, 
cityAddress) 
VALUES(
"Richard", 
"Michaels", 
'2002-04-08', 
"15 Fitzroy Street", 
"Caversham", 
"Dunedin"); 

INSERT INTO database_rugby.person (
firstName, 
lastName, 
dateOfBirth, 
streetAddress, 
suburbAddress, 
cityAddress) 
VALUES(
"Shaun", 
"Michaels", 
'2003-11-11', 
"15 Fitzroy Street", 
"Caversham", 
"Dunedin"); 

INSERT INTO database_rugby.person (
firstName, 
lastName, 
dateOfBirth, 
streetAddress, 
suburbAddress, 
cityAddress) 
VALUES(
"Harry", 
"Dackers", 
'2004-02-11', 
"32 Peter Street", 
"Caversham", 
"Dunedin"); 

INSERT INTO database_rugby.person (
firstName, 
lastName, 
dateOfBirth, 
streetAddress, 
suburbAddress, 
cityAddress) 
VALUES(
"Daniel", 
"Mitchell", 
'2002-05-19', 
"112 South Road", 
"Caversham", 
"Dunedin"); 

-- Insert new persons which will be 
-- parents. 
INSERT INTO database_rugby.person (
firstName, 
lastName, 
dateOfBirth, 
streetAddress, 
suburbAddress, 
cityAddress) 
VALUES(
"Gregory", 
"Peck", 
'1971-07-22', 
"123 Burns Street", 
"South Dunedin", 
"Dunedin"); 


INSERT INTO database_rugby.person (
firstName, 
lastName, 
dateOfBirth, 
streetAddress, 
suburbAddress, 
cityAddress) 
VALUES(
"Laura", 
"Peck", 
'1979-09-08', 
"123 Burns Street", 
"South Dunedin", 
"Dunedin"); 

INSERT INTO database_rugby.person (
firstName, 
lastName, 
dateOfBirth, 
streetAddress, 
suburbAddress, 
cityAddress) 
VALUES(
"Martha", 
"Petersen", 
'1973-12-07', 
"192 Bayview Road", 
"South Dunedin", 
"Dunedin"); 

INSERT INTO database_rugby.person (
firstName, 
lastName, 
dateOfBirth, 
streetAddress, 
suburbAddress, 
cityAddress) 
VALUES(
"Chris", 
"Michaels", 
'1967-08-07', 
"15 Fitzroy Street", 
"Caversham", 
"Dunedin"); 

INSERT INTO database_rugby.person (
firstName, 
lastName, 
dateOfBirth, 
streetAddress, 
suburbAddress, 
cityAddress) 
VALUES(
"Nadine", 
"Michaels", 
'1973-10-19', 
"15 Fitzroy Street", 
"Caversham", 
"Dunedin"); 

INSERT INTO database_rugby.person (
firstName, 
lastName, 
dateOfBirth, 
streetAddress, 
suburbAddress, 
cityAddress) 
VALUES(
"Barry", 
"Dackers", 
'1965-02-11', 
"32 Peter Street", 
"Caversham", 
"Dunedin"); 

INSERT INTO database_rugby.person (
firstName, 
lastName, 
dateOfBirth, 
streetAddress, 
suburbAddress, 
cityAddress) 
VALUES(
"Kevin", 
"Mitchell", 
'1972-05-19', 
"112 South Road", 
"Caversham", 
"Dunedin"); 

INSERT INTO database_rugby.person (
firstName, 
lastName, 
dateOfBirth, 
streetAddress, 
suburbAddress, 
cityAddress) 
VALUES(
"Rebecca", 
"Mitchell", 
'1978-01-23', 
"112 South Road", 
"Caversham", 
"Dunedin"); 

-- Insert schools into school table. 
TRUNCATE TABLE database_rugby.school; 
INSERT INTO database_rugby.school(
schoolName) 
VALUES(
"College Street School"); 

INSERT INTO database_rugby.school(
schoolName) 
VALUES(
"Macandrew Intermediate School"); 

INSERT INTO database_rugby.school(
schoolName) 
VALUES(
"Carlton Hill Primary"); 

INSERT INTO database_rugby.school(
schoolName) 
VALUES(
"Caversham Primary School"); 

INSERT INTO database_rugby.school(
schoolName) 
VALUES(
"Concord School"); 

-- Insert players into player table. 
TRUNCATE TABLE database_rugby.player; 
INSERT INTO database_rugby.player(
personID, 
schoolID) 
VALUES(
    (SELECT personID FROM database_rugby.person 
    WHERE database_rugby.person(firstName) = "Michael" 
    AND database_rugby.person(lastName) = "Peck"), 
    (SELECT schoolID FROM database_rugby.school 
    WHERE database_rugby.school(schoolName) = "College Street School")); 

INSERT INTO database_rugby.player(
personID, 
schoolID) 
VALUES(
    (SELECT personID FROM database_rugby.person 
    WHERE database_rugby.person(firstName) = "Matt" 
    AND database_rugby.person(lastName) = "Petersen"), 
    (SELECT schoolID FROM database_rugby.school 
    WHERE database_rugby.school(schoolName) = "Macandrew Intermediate School")); 

INSERT INTO database_rugby.player(
personID, 
schoolID) 
VALUES(
    (SELECT personID FROM database_rugby.person 
    WHERE database_rugby.person(firstName) = "Christopher" 
    AND database_rugby.person(lastName) = "Petersen"), 
    (SELECT schoolID FROM database_rugby.school 
    WHERE database_rugby.school(schoolName) = "College Street School")); 

INSERT INTO database_rugby.player(
personID, 
schoolID) 
VALUES(
    (SELECT personID FROM database_rugby.person 
    WHERE database_rugby.person(firstName) = "Richard" 
    AND database_rugby.person(lastName) = "Michaels"), 
    (SELECT schoolID FROM database_rugby.school 
    WHERE database_rugby.school(schoolName) = "College Street School")); 

INSERT INTO database_rugby.player(
personID, 
schoolID) 
VALUES(
    (SELECT personID FROM database_rugby.person 
    WHERE database_rugby.person(firstName) = "Shaun" 
    AND database_rugby.person(lastName) = "Michaels"), 
    (SELECT schoolID FROM database_rugby.school 
    WHERE database_rugby.school(schoolName) = "College Street School")); 

INSERT INTO database_rugby.player(
personID, 
schoolID) 
VALUES(
    (SELECT personID FROM database_rugby.person 
    WHERE database_rugby.person(firstName) = "Harry" 
    AND database_rugby.person(lastName) = "Dackers"), 
    (SELECT schoolID FROM database_rugby.school 
    WHERE database_rugby.school(schoolName) = "Caversham Primary School")); 

INSERT INTO database_rugby.player(
personID, 
schoolID) 
VALUES(
    (SELECT personID FROM database_rugby.person 
    WHERE database_rugby.person(firstName) = "Daniel" 
    AND database_rugby.person(lastName) = "Mitchell"), 
    (SELECT schoolID FROM database_rugby.school 
    WHERE database_rugby.school(schoolName) = "Caversham Primary School")); 

을 그리고 여기에 두 개의 스크립트에서 생성되는 MySQL의 명령 줄 인터페이스에 출력이다 - : 나는 발생한 문제가 지금까지 해결하는 경우 "플레이어"자세한 내용은이) .. 나중에 저를 도울 수 오류에 대한 자세한 정보가 표시됩니다 :

Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 82 
Server version: 5.5.23 MySQL Community Server (GPL) 

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. 

Oracle is a registered trademark of Oracle Corporation and/or its 
affiliates. Other names may be trademarks of their respective 
owners. 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 

mysql> source c:\scripts\database_schema.sql 
Query OK, 16 rows affected (0.35 sec) 

Query OK, 1 row affected (0.03 sec) 

Database changed 
Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected (0.06 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected (0.06 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected (0.04 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected (0.07 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected (0.06 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected (0.06 sec) 

Query OK, 0 rows affected, 1 warning (0.01 sec) 

Query OK, 0 rows affected (0.09 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected (0.06 sec) 

Query OK, 0 rows affected, 1 warning (0.01 sec) 

Query OK, 0 rows affected (0.07 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected (0.07 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected (0.10 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected (0.06 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected (0.08 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected (0.06 sec) 

Query OK, 0 rows affected, 1 warning (0.00 sec) 

Query OK, 0 rows affected (0.10 sec) 

Query OK, 0 rows affected, 1 warning (0.01 sec) 

Query OK, 0 rows affected (0.07 sec) 

mysql> source c:\scripts\database_data.sql 
Database changed 
ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constrai 
nt (`database_rugby`.`contact`, CONSTRAINT `contact_ibfk_1` FOREIGN KEY (`person 
ID`) REFERENCES `database_rugby`.`person` (`personID`)) 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f 
ails (`database_rugby`.`person`, CONSTRAINT `fk_person_coach1` FOREIGN KEY (`coa 
ch_coachID2`) REFERENCES `coach` (`coachID`) ON DELETE NO ACTION ON UPDATE NO AC 
TION) 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f 
ails (`database_rugby`.`person`, CONSTRAINT `fk_person_coach1` FOREIGN KEY (`coa 
ch_coachID2`) REFERENCES `coach` (`coachID`) ON DELETE NO ACTION ON UPDATE NO AC 
TION) 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f 
ails (`database_rugby`.`person`, CONSTRAINT `fk_person_coach1` FOREIGN KEY (`coa 
ch_coachID2`) REFERENCES `coach` (`coachID`) ON DELETE NO ACTION ON UPDATE NO AC 
TION) 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f 
ails (`database_rugby`.`person`, CONSTRAINT `fk_person_coach1` FOREIGN KEY (`coa 
ch_coachID2`) REFERENCES `coach` (`coachID`) ON DELETE NO ACTION ON UPDATE NO AC 
TION) 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f 
ails (`database_rugby`.`person`, CONSTRAINT `fk_person_coach1` FOREIGN KEY (`coa 
ch_coachID2`) REFERENCES `coach` (`coachID`) ON DELETE NO ACTION ON UPDATE NO AC 
TION) 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f 
ails (`database_rugby`.`person`, CONSTRAINT `fk_person_coach1` FOREIGN KEY (`coa 
ch_coachID2`) REFERENCES `coach` (`coachID`) ON DELETE NO ACTION ON UPDATE NO AC 
TION) 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f 
ails (`database_rugby`.`person`, CONSTRAINT `fk_person_coach1` FOREIGN KEY (`coa 
ch_coachID2`) REFERENCES `coach` (`coachID`) ON DELETE NO ACTION ON UPDATE NO AC 
TION) 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f 
ails (`database_rugby`.`person`, CONSTRAINT `fk_person_coach1` FOREIGN KEY (`coa 
ch_coachID2`) REFERENCES `coach` (`coachID`) ON DELETE NO ACTION ON UPDATE NO AC 
TION) 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f 
ails (`database_rugby`.`person`, CONSTRAINT `fk_person_coach1` FOREIGN KEY (`coa 
ch_coachID2`) REFERENCES `coach` (`coachID`) ON DELETE NO ACTION ON UPDATE NO AC 
TION) 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f 
ails (`database_rugby`.`person`, CONSTRAINT `fk_person_coach1` FOREIGN KEY (`coa 
ch_coachID2`) REFERENCES `coach` (`coachID`) ON DELETE NO ACTION ON UPDATE NO AC 
TION) 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f 
ails (`database_rugby`.`person`, CONSTRAINT `fk_person_coach1` FOREIGN KEY (`coa 
ch_coachID2`) REFERENCES `coach` (`coachID`) ON DELETE NO ACTION ON UPDATE NO AC 
TION) 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f 
ails (`database_rugby`.`person`, CONSTRAINT `fk_person_coach1` FOREIGN KEY (`coa 
ch_coachID2`) REFERENCES `coach` (`coachID`) ON DELETE NO ACTION ON UPDATE NO AC 
TION) 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f 
ails (`database_rugby`.`person`, CONSTRAINT `fk_person_coach1` FOREIGN KEY (`coa 
ch_coachID2`) REFERENCES `coach` (`coachID`) ON DELETE NO ACTION ON UPDATE NO AC 
TION) 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f 
ails (`database_rugby`.`person`, CONSTRAINT `fk_person_coach1` FOREIGN KEY (`coa 
ch_coachID2`) REFERENCES `coach` (`coachID`) ON DELETE NO ACTION ON UPDATE NO AC 
TION) 
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f 
ails (`database_rugby`.`person`, CONSTRAINT `fk_person_coach1` FOREIGN KEY (`coa 
ch_coachID2`) REFERENCES `coach` (`coachID`) ON DELETE NO ACTION ON UPDATE NO AC 
TION) 
ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constrai 
nt (`database_rugby`.`player`, CONSTRAINT `player_ibfk_1` FOREIGN KEY (`schoolID 
`) REFERENCES `database_rugby`.`school` (`schoolID`)) 
Query OK, 1 row affected (0.01 sec) 

Query OK, 1 row affected (0.01 sec) 

Query OK, 1 row affected (0.01 sec) 

Query OK, 1 row affected (0.00 sec) 

Query OK, 1 row affected (0.00 sec) 

ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constrai 
nt (`database_rugby`.`person`, CONSTRAINT `fk_person_player1` FOREIGN KEY (`play 
er_playerID2`) REFERENCES `database_rugby`.`player` (`playerID`)) 
ERROR 1305 (42000): FUNCTION database_rugby.person does not exist 
ERROR 1305 (42000): FUNCTION database_rugby.person does not exist 
ERROR 1305 (42000): FUNCTION database_rugby.person does not exist 
ERROR 1305 (42000): FUNCTION database_rugby.person does not exist 
ERROR 1305 (42000): FUNCTION database_rugby.person does not exist 
ERROR 1305 (42000): FUNCTION database_rugby.person does not exist 
ERROR 1305 (42000): FUNCTION database_rugby.person does not exist 
mysql> 

다음은 논리적 인 모델입니다 (모든 관계에 문제가있는 경우 - 특히 많은 관계가 많은 경우 알려주십시오). 내가 너무 많은 정보를 준,하지만 난 그게 나를 도와하려는 사람들에게 일을 더 쉽게 만들 것 모든 정보를 주면 내가 생각하는 경우

enter image description here

나는 죄송합니다. 나는 DDL 스크립트를 3 시간 동안 조정 해 보았지만 오랜 시간을 보낸 후에는 더 많은 경험을 가진 사람이 필요하다는 것을 깨달았습니다. 필자는 데이터베이스에서 업계 경험이있는 사람에게 논리적 모델을 보여 주었고 3NF를 준수한다고 생각합니다. 개념적 모델과 논리적 모델을 혼자서하는 것은 나쁘지 않았습니다. 논리적 모델을 작업 스크립트로 변환하고 가장 큰 장애물이 된 데이터를 성공적으로 삽입하려고했습니다. (각 테이블에 적어도 10 개의 레코드를 삽입하고 싶습니다.)

미리 감사드립니다.

+1

** 최소 ** 테스트 케이스를 만들 것을 제안합니다. 즉, 테이블을 훨씬 더 단순하게 배열하는 것과 같은 문제가 여전히 발생합니다. 그렇게하면 SO 사용자가 구문 분석하고 이해할 코드가 줄어 듭니다. –

+0

@Oli - 당신이 바로 거기에 있습니다. 전체 스크립트를 보여주는 것이 문제에 대한 더 넓은 시각을 줄 수 있다고 생각했습니다. 방금 말한 것을 만족시킬 수있는 방법으로이 게시물을 편집 할 수있는 방법을 알려주십시오. 전체 DDL 스크립트가 필요하다고 생각하지만이 예제에서는 DML 스크립트를 다듬을 수 있다고 생각합니다. 논리적 모델의 이미지를 제공하여 불일치가 있는지도 확인했습니다. – Rob

답변

3

당신이이 문을 추가해야 이 외부 키 체크를 해제합니다

SET foreign_key_checks = 0; 

덤프 파일의 상단에 추가하고 당신은 쉽게 덤프 파일

을 실행하고 추가 할 수 있습니다 덤프 파일의 맨 아래에 외래 키 확인을 다시 허용하는 다른 문

SET foreign_key_checks = 1; 
+0

많은 의견을 보내 주셔서 감사합니다. 잠을 자고 아침에 제일 먼저 해보고 어떻게 진행되었는지 알려 드리겠습니다 :). – Rob

+0

Hi Shakti, 코드 라인이 잘 작동하는 것처럼 보였지만 전체 문제를 해결하지 못했습니다 ... "Person"에 대한 고유 식별자가 "Player"의 기본 키가 될 수는 있습니까? "Player"고유 식별자가 있습니까? 그렇다면 나에게 모범을 보여줄 수 있습니까? – Rob

0

당신이 배당 한 외래 키 제약 person (수퍼 클래스) 및 player, parentcoach (서브 클래스)은 1:1 관계를 정의하지 않습니다.

-- 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; 

와 유사 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; 

동일 parent 표는 personID 모두 person에 기본 및 외래 키 등을 할 수 있습니다

-- 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; 

: 여기에 가능한 솔루션입니다 coach 테이블로 이동해야합니다. 상기 용액을 person 될 수 있도록


하는 것으로 모두 playerparent 또는 둘 parentcoach. 수퍼 클래스 (person) 테이블의 행을 하나의 서브 클래스 테이블로 제한하려면 약간 다른 접근 방식이어야합니다.