尝试更改表约束时出现错误代码1452

时间:2017-09-26 16:53:38

标签: mysql foreign-keys mysql-error-1452

ALTER TABLE `charter`
  ADD CONSTRAINT `charter_ibfk_3` FOREIGN KEY (`CHAR_DESTINATION`) 
  REFERENCES `airport` (`AIRPORT_CODE`);

我正试图建立这种关系:

enter image description here

Charter and Airport ERD Diagram

CREATE TABLE `antonellacammarota` (
  `CUS_CODE` int(6) NOT NULL DEFAULT '0',
  PRIMARY KEY (`CUS_CODE`),
  FOREIGN KEY (`CUS_CODE`) REFERENCES `CUSTOMER` (`CUS_CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

insert into customer values (10076, "Cammarota", "Antonella", "T", "805", "555-1212", 0);

insert into antonellacammarota(select cus_code from customer where cus_lname = 'Cammarota');

--
-- Table structure for table `airport`
--

CREATE TABLE IF NOT EXISTS `airport` (
  `AIRPORT_CODE` varchar(3) NOT NULL DEFAULT '0',
  `AIRPORT_NAME` varchar(40) DEFAULT NULL,
  `AIRPORT_ADDRESS` varchar(45) DEFAULT NULL,
  `AIRPORT_CITY` varchar(25) DEFAULT NULL,
  `AIRPORT_STATE` char(2) DEFAULT NULL,
  `AIRPORT_ZIP` varchar(10) DEFAULT NULL,
  `AIRPORT_COUNTRY` varchar(25) DEFAULT NULL,
  `AIRPORT_AREACODE` varchar(3) DEFAULT NULL,
  `AIRPORT_PHONE` varchar(8) DEFAULT NULL,
  PRIMARY KEY (`AIRPORT_CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Constraints for table `airport`
--
ALTER TABLE `charter`
  ADD CONSTRAINT `charter_ibfk_3` FOREIGN KEY (`CHAR_DESTINATION`) REFERENCES `airport` (`AIRPORT_CODE`);


--
-- Dumping data for table `airport`
--

INSERT INTO `airport` (`AIRPORT_CODE`, `AIRPORT_NAME`, `AIRPORT_ADDRESS`, `AIRPORT_CITY`, `AIRPORT_STATE`, 
`AIRPORT_ZIP`, `AIRPORT_COUNTRY`, `AIRPORT_AREACODE`, `AIRPORT_PHONE`) VALUES
('ATL', 'Hartsfield-Jackson Atlanta Int', '6000 N Terminal Pkwy.', 'Atlanta', 'GA', '30320', 'US', '808', '897-1910'),
('BNA', 'Nashville Int', '1 Terminal Dr.', 'Nashville', 'TN', '37214', 'US', '615', '275-1675'),
('GNV', 'Gainesville Regional', '3880 NE 39th Ave.', 'Gainesville', 'FL', '32609', 'US', '352', '373-0249'),
('MOB', 'Mobile Regional', '8400 Airport Blvd.', 'Mobile', 'AL', '36608', 'US', '800', '357-5373'),
('MOY', 'Monterrey', '', 'Monterrey', '', '', 'Columbia', '', ''),
('STL', 'St. Louis Lambert Int', '10701 Lambert International Blvd.', 'St. Louis', 'MO', '63145', 'US', '314', '426-8000'),
('TYS', 'McGhee Tyson', '2055 Alcoa Hwy', 'Alcoa', 'TN', '37701', 'US', '865', '345-3000');

1 个答案:

答案 0 :(得分:1)

使用它来找出哪些条目搞砸了

SELECT CHAR_TRIP, CHAR_DESTINATION 
FROM charter
WHERE CHAR_DESTINATION NOT IN (
SELECT AIRPORT_CODE FROM airport);