2014-12-01 01:51

无法添加或更新子行:外键约束失败。 这是什么?


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

and my other "table vehicle_registration" have:

plate_number = primary key
user_id = foreign key
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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • doushai4890 doushai4890 7年前

    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...

    点赞 评论 复制链接分享