简体   繁体   中英

Adding Foreign Key Error

I want to add a foreign key from Table Customers , row= "Customer ID" to Table Pet, row= "Customer ID" .

-- Table structure for table `Customers`

CREATE TABLE IF NOT EXISTS `Customers` (
  `CustomerID` varchar(50) NOT NULL,
  `Fname` varchar(50) DEFAULT NULL,
  `LName` varchar(20) DEFAULT NULL,
  `Tel` varchar(20) DEFAULT NULL,
  `Fax` varchar(20) DEFAULT NULL,
  `CustType` varchar(20) DEFAULT NULL,
  `AdState` varchar(50) DEFAULT NULL,
  `City` varchar(20) DEFAULT NULL,
  `Zip` varchar(20) DEFAULT NULL,
  `Street` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`CustomerID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- Dumping data for table `Customers`

INSERT INTO `Customers` (`CustomerID`, `Fname`, `LName`, `Tel`, `Fax`, `CustType`, `AdState`, `City`, `Zip`, `Street`) VALUES
('AC001', 'All', 'Creatures', '206 555-6622', '206 555-7854', '2', 'WA', 'Tall Pines', '98746', '21 Grace St.'),
('AD001', 'Johnathan', 'Adams', '206 555 7623', '206 555 8855', '1', 'WA', 'Mountain View', '984101012', '66 10th St'),
('AD002', 'William', 'Adams', '503 555 7623', '503 555 7319', '1', 'OR', 'Lakewille', '9740110011', '1122 10th_St'),
('AK001', 'Animal', 'Kingdom', '208 555 7108', '', '2', 'ID', 'Borderville', '834835646', '15 Marlin Lane');

CREATE TABLE IF NOT EXISTS `Pet` (
  `ID` varchar(50) NOT NULL,
  `CustomerID` varchar(50) NOT NULL,
  `Gender` varchar(20) DEFAULT NULL,
  `Race` varchar(20) DEFAULT NULL,
  `Name` varchar(20) DEFAULT NULL,
  `Kind` varchar(20) DEFAULT NULL,
  `Birthday` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- Dumping data for table `Pet`

INSERT INTO `Pet` (`ID`, `CustomerID`, `Gender`, `Race`, `Name`, `Kind`, `Birthday`) VALUES
('AC001-01', '0', 'M', 'Long Ear', 'Bobo', 'Rabbit', '4/8/92'),
('AC001-02', '0', 'F', 'Chameleon', 'Presto Chango', 'Lizard', '5/1/92'),
('AC001-03', '0', 'M', '', 'Stinky', 'Skunk', '8/1/91'),
('AC001-04', '0', 'M', 'German Shepherd', 'Fido', 'Dog', '6/1/90'),
('AD001-01', '0', 'F', 'Potbelly', 'Patty', 'Pig', '2/15/91'),
('AD001-02', '0', 'M', 'Palomino', 'Rising Sun', 'Horse', '4/10/90'),
('AD002-01', '0', 'F', 'Mixed', 'Dee Dee', 'Dog', '2/15/91'),
('AK001-03', '0', 'M', '', 'Jerry', 'Rat', '2/1/88'),
('AK001-07', '0', 'M', 'Beagle', 'Luigi', 'Dog', '8/1/92');

This is the code that I have been using to add the foreign key:

ALTER TABLE Pet ADD CONSTRAINT Pet_FK 
FOREIGN KEY (CustomerID) REFERENCES Customers (CustomerID);

And the error message from this is:

#1452 - Cannot add or update a child row: a foreign key constraint fails     
(`hospital`.`#sql-523_76e`, CONSTRAINT `Pet_FK` FOREIGN KEY (`CustomerID`) 
REFERENCES `Customers` (`CustomerID`))

I am quite a beginner with database and I have no idea what I should try next.

I think that's all. Im still new to this stackoverflow so if I missed any necessary information please tell me and I will add it.

UPDATE***

ALTER TABLE Customers ADD CONSTRAINT Customers_FK 
FOREIGN KEY (CustomerID) REFERENCES Pet (CustomerID);

I swapped some positions and the error code I recieve is:

#1215 - Cannot add foreign key constraint

Simple one.

There is an row that contains the CustomerID that can't be matched. So first you need to remove/edit/handle the entry and than add a foreign key.

The CustomerID you're trying to enter in PETS table, does not exist in CUSTOMERS table, and that is why your Foreign Key constraint fails and throws error.

You need to ensure that the CustomerIDs you're entering in your Pets table, exist in Customers table OR simply insert NULL in the PETS.CUSTOMERID field

Enterx is right.

For being able to detect not matching row :

SELECT * FROM Pet p WHERE (SELECT COUNT(*) FROM Customers c WHERE c.CustomerID=p.CustomerID)=0

Just change SELECT * by DELETE for deleting missmatching Pet entry. You can UPDATE Pet.CustomerID to NULL too. But you have to define CustomerID, from Pet table, with NULL option (and not NOT NULL)

try this

CREATE TABLE IF NOT EXISTS `Pet` (
  `ID` varchar(50) NOT NULL,
  FOREIGN KEY (`CustomerID`) REFERENCES Customers(CustomerID) varchar(50) NOT NULL,
  `Gender` varchar(20) DEFAULT NULL,
  `Race` varchar(20) DEFAULT NULL,
  `Name` varchar(20) DEFAULT NULL,
  `Kind` varchar(20) DEFAULT NULL,
  `Birthday` varchar(20) DEFAULT NULL,
   PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

It looks like to me that you inserted values in table Pet's column ID when they should have been inserted in CustomerID and vice-versa.

By the way, it's not really good to have IDs as VARCHARs, specially when they are foreign keys. This makes queries processing slower, although your tables don't look like they'll have a huge number of rows for this to make a difference. Anyway, it's just an observation. I would consider have artificial int primary keys in my tables.

EDIT

I had misread table Pet's values. The other answers here are right. You need to update those 0 values in CustomerID column to match existing CustomerIDs in Customer table or delete them, otherwise you'll get an error when trying to create the FK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM