免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3319 | 回复: 6
打印 上一主题 下一主题

[数据库 外键问题]外键约束错误,不能插入数据,痛苦中! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-07-31 17:45 |只看该作者 |倒序浏览
SQL 查询:

INSERT INTO phone_maker( makerName, zoneid )
VALUES (
'海尔', 0
)

MySQL 返回:

#1216 - Cannot add or update a child row: a foreign key constraint fails

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
2 [报告]
发表于 2005-07-31 18:43 |只看该作者

[数据库 外键问题]外键约束错误,不能插入数据,痛苦中!

因為 fk 對不到的關係,所以資料寫不進去。

你这里的字段里面有外键关系,必须和外键对应

论坛徽章:
0
3 [报告]
发表于 2005-07-31 18:53 |只看该作者

[数据库 外键问题]外键约束错误,不能插入数据,痛苦中!

我没有定义外键啊!

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
4 [报告]
发表于 2005-07-31 21:09 |只看该作者

[数据库 外键问题]外键约束错误,不能插入数据,痛苦中!

但是他确实提示的是因为外键不匹配

论坛徽章:
0
5 [报告]
发表于 2005-07-31 23:22 |只看该作者

[数据库 外键问题]外键约束错误,不能插入数据,痛苦中!

贴出 create table 的代码看看

论坛徽章:
0
6 [报告]
发表于 2005-08-01 09:13 |只看该作者

[数据库 外键问题]外键约束错误,不能插入数据,痛苦中!

OK

  1. /*==============================================================*/
  2. /* DBMS name:      MySQL 4.0                                    */
  3. /* Created on:     2005-7-27 16:02:05                           */
  4. /*==============================================================*/


  5. drop table if exists Phone_otherInfo;

  6. drop table if exists phone_Phone;

  7. drop table if exists phone_Zone;

  8. drop table if exists phone_maker;

  9. /*==============================================================*/
  10. /* Table: Phone_otherInfo                                       */
  11. /*==============================================================*/
  12. create table Phone_otherInfo
  13. (
  14.    id                             int                  auto_increment  not null,
  15.    PhoneID                        int,
  16.    ParaName                       varchar(50),
  17.    ParaVaule                      text,
  18.    classify                       varchar(50),
  19.    Porder                         int,
  20.    primary key (id)
  21. )
  22. comment = "the optional parameters of phone"
  23. type = InnoDB;

  24. /*==============================================================*/
  25. /* Table: phone_Phone                                           */
  26. /*==============================================================*/
  27. create table phone_Phone
  28. (
  29.    phoneID                        int              auto_increment              not null,
  30.    makerID                        int,
  31.    phoneType                      varchar(50),
  32.    phoneImage                     varchar(100),
  33.    forumID                        int,
  34.    fileName                       varchar(100),
  35.    commentURL                     varchar(100),
  36.    primary key (phoneID)
  37. )
  38. comment = "table of phone"
  39. type = InnoDB;

  40. /*==============================================================*/
  41. /* Table: phone_Zone                                            */
  42. /*==============================================================*/
  43. create table phone_Zone
  44. (
  45.    ZoneID                         int               auto_increment             not null,
  46.    zoneValue                      varchar(10),
  47.    primary key (ZoneID)
  48. )
  49. comment = "zone of the manufacture of the mobile"
  50. type = InnoDB;

  51. /*==============================================================*/
  52. /* Table: phone_maker                                           */
  53. /*==============================================================*/
  54. create table phone_maker
  55. (
  56.    makerID                        int               auto_increment             not null,
  57.    makerName                      varchar(50),
  58.    zoneID                         int,
  59.    primary key (makerID)
  60. )
  61. comment = "the manufacture of the phone"
  62. type = InnoDB;

复制代码

论坛徽章:
0
7 [报告]
发表于 2005-08-01 10:25 |只看该作者

[数据库 外键问题]外键约束错误,不能插入数据,痛苦中!

问题解决.

很奇怪.让我重建数据库之后,然后运行程序,竟然没有遇到一点的错误!

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP