douqi1625 2014-12-01 01:51
浏览 319
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • doushai4890 2014-12-01 02:07
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算