Reputation: 37
sorry, i am still new in php... i want to ask... how actually foreign key is functional? i mean, i have create a foreign key and primary key, everything fine. i try look in internet, but still i cant make it work or did mistake that i dont know. i get this error "Cannot add or update a child row: a foreign key constraint fails" when i try to insert information inside my other table that have foreign key
my parent table "user_information" have:
user_id = primary key
user_password
name
user_category
group_id
and my other "table vehicle_registration" have:
plate_number = primary key
user_id = foreign key
roadtax_validation
vehicle_category
user_option
insurance name
i want to make the user that log in can insert their vehicle registration, and it bind to that user, the user_id should be similar, just other information is diferent... how i can make it? is it correct that the foreign key is to work like that? cause when i try to insert value inside the vehicle_registration it will show "Cannot add or update a child row: a foreign key constraint fails" can someone explain how it actually done?
Upvotes: 0
Views: 138
Reputation: 13665
You have to add user/s first. You can't add a record in vehicle_registration table
if you dont have a matching record in the parent table.
That is how foreign key constraint works. For example if you have a record in user_information table
with user_id = 1
, you are only allowed to have records with user_id = 1
in the vehicle_registration table
. And so on...
Upvotes: 1