今天用数据库时出现 Error 1215:Cannot add foreign key constraint 报错,在经历了一番波折后解决了问题的所在,写下这篇博客记录一下。
运行的代码:
CREATE TABLE `admitpatient_room` (
`PatientID` int(10) NOT NULL,
`Disease` varchar(50) DEFAULT NULL,
`AdmitDate` date DEFAULT NULL,
`RoomNo` int(5) DEFAULT NULL,
`DoctorID` int(10) NOT NULL,
`AP_Remarks` varchar(50) DEFAULT NULL,
PRIMARY KEY (`PatientID`),
KEY `RoomNo` (`RoomNo`),
KEY `DoctorID` (`DoctorID`),
CONSTRAINT `admitpatient_room_ibfk_1` FOREIGN KEY (`RoomNo`) REFERENCES `room` (`RoomNo`),
CONSTRAINT `admitpatient_room_ibfk_2` FOREIGN KEY (`DoctorID`) REFERENCES `doctor` (`DoctorID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `room` (
`RoomNo` int(5) NOT NULL,
`RoomType` varchar(10) DEFAULT NULL,
`RoomCharges` int(10) DEFAULT NULL,
`RoomStatus` varchar(10) DEFAULT NULL,
PRIMARY KEY (`RoomNo`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `doctor` (
`DoctorID` int(10) NOT NULL AUTO_INCREMENT,
`DoctorName` varchar(20) DEFAULT NULL,
`FatherName` varchar(20) DEFAULT NULL,
`Email` varchar(50) DEFAULT NULL,
`ContacNo` varchar(11) DEFAULT NULL,
`Qualifications` varchar(50) DEFAULT NULL,
`Gender` varchar(1) DEFAULT NULL,
`BloodGroup` varchar(5) DEFAULT NULL,
`DateOfJoining` date DEFAULT NULL,
`Address` varchar(100</