0

I have a table called tbl_vehicles and in this table, there are two foreign keys these are:

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

If a vehicle belongs to an agency then the owner field should be null and If a vehicle belongs to an owner then the agency field should be null. But it gives me an error. Which is:

1452 - Cannot add or update a child row: a foreign key constraint fails (db_lamlex_car_rental.tbl_vehicles, CONSTRAINT

tbl_vehicles_ibfk_2 FOREIGN KEY (ownerID) REFERENCES tbl_owner (ownerID) ON DELETE CASCADE ON UPDATE CASCADE)

How can I solve this? Please help.

Community
  • 1
  • 1
Shorif
  • 1
  • 3
  • post your insert query along with table structure – Rahul Jul 31 '17 at 12:36
  • You can't ask a question without relevant code. – Mjh Jul 31 '17 at 12:39
  • (1, 'Toyota Fielder X', 'High', 'octane', 'available', '150km/hr', 'Yellow', 'Lamborghini 2014', 'Lamborghini', '200', 34534534, 2, 'Toyota Fielder X.jpg', 1, 0) i'm inserting in sql first. – Shorif Jul 31 '17 at 12:40
  • Re-Edit your question and provide the information (table structure and insert statement) in the question. – Myonara Jul 31 '17 at 12:43
  • 1) That is not entire *relevant* code 2) please edit the question, you are a member here for multiple days, you should be able to edit simple text 3) Relevant code is the `create table` statement AND insert statement 4) "Blank" means `null` in SQL world, not a zero, not empty string. – Mjh Jul 31 '17 at 12:43
  • ok i'll. Right away. – Shorif Jul 31 '17 at 12:49
  • Please go through the following post that comprehensively discusses on having `NULL` values in `FK`. It will help in re-defining the constraints or redesign the tables. https://stackoverflow.com/questions/7573590/can-a-foreign-key-be-null-and-or-duplicate – Aniket V Aug 01 '17 at 07:01

0 Answers0