我有一个名为tbl_vehicles
的表,在这个表中,有两个外键:
ownerID
和
agencyID
tbl_agency:
CREATE TABLE `tbl_agency` (
`agencyID` int(11) NOT NULL,
`ag_agencyName` varchar(50) NOT NULL,
`ag_email` varchar(50) NOT NULL,
`ag_username` varchar(50) NOT NULL,
`ag_password` varchar(15) NOT NULL,
`ag_confirmPassword` int(11) NOT NULL,
`ag_phoneNo` int(11) NOT NULL,
`ag_address` varchar(50) NOT NULL,
`ag_photo` varchar(50) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `tbl_agency`
ADD PRIMARY KEY (`agencyID`);
ALTER TABLE `tbl_agency`
MODIFY `agencyID` int(11) NOT NULL AUTO_INCREMENT;
tbl_owner:
CREATE TABLE `tbl_owner` (
`ownerID` int(11) NOT NULL,
`ow_ownerName` varchar(20) NOT NULL,
`ow_nationalID` varchar(50) NOT NULL,
`ow_email` varchar(50) NOT NULL,
`ow_username` varchar(50) NOT NULL,
`ow_password` varchar(15) NOT NULL,
`ow_confirmPassword` int(11) NOT NULL,
`ow_phoneNo` int(11) NOT NULL,
`ow_address` varchar(50) NOT NULL,
`ow_photo` varchar(50) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1
ALTER TABLE `tbl_owner`
ADD PRIMARY KEY (`ownerID`);
ALTER TABLE `tbl_owner`
MODIFY `ownerID` int(11) NOT NULL AUTO_INCREMENT;
tbl_vehicles:
CREATE TABLE `tbl_vehicles` (
`vehiclesID` int(11) NOT NULL,
`ve_name` varchar(100) NOT NULL,
`ve_capacity` varchar(100) NOT NULL,
`ve_fuelType` varchar(100) NOT NULL,
`ve_availability` varchar(100) NOT NULL,
`ve_mileage` varchar(100) NOT NULL,
`ve_color` varchar(100) NOT NULL,
`ve_modelName` varchar(100) NOT NULL,
`ve_brandName` varchar(100) NOT NULL,
`ve_price` varchar(50) NOT NULL,
`ve_licenseCode` int(11) NOT NULL,
`ve_noOfPassenger` int(11) NOT NULL,
`ve_photo` varchar(50) NOT NULL,
`agencyID` int(11) NOT NULL,
`ownerID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `tbl_vehicles`
ADD PRIMARY KEY (`vehiclesID`),
ADD KEY `agencyID` (`agencyID`),
ADD KEY `ownerID` (`ownerID`);
ALTER TABLE `tbl_vehicles`
MODIFY `vehiclesID` int(11) NOT NULL AUTO_INCREMENT;
--
-- Constraints for table `tbl_vehicles`
--
ALTER TABLE `tbl_vehicles`
ADD CONSTRAINT `fk_tbl_vehicles_tbl_agency` FOREIGN KEY (`agencyID`) REFERENCES `tbl_agency` (`agencyID`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `tbl_vehicles`
ADD CONSTRAINT `fk_tbl_vehicles_tbl_owner` FOREIGN KEY (`ownerID`) REFERENCES `tbl_owner` (`ownerID`) ON DELETE CASCADE ON UPDATE CASCADE;
如果车辆属于代理商,则所有者字段应为空,如果车辆属于所有者,则代理商字段应为空。 但它给了我一个错误。 这是:
1452 - 无法添加或更新子行:外键约束失败(
db_lamlex_car_rental
。tbl_vehicles
,CONSTRAINT
tbl_vehicles_ibfk_2
FOREIGN KEY(ownerID
)参考tbl_owner
(ownerID
)ON UPETE CASCADE ON UPDATE CASCADE)
我该如何解决这个问题? 请帮忙。