错误代码1452,使用外键将数据加载到表中很困难

时间:2019-04-06 18:35:50

标签: mysql

我一直在尝试用外键创建表,但继续得到错误代码1452,其中指出无法添加或更新子行。我正在尝试将具有Account_ID主键的帐户表链接到具有Account_ID外键的支付表。创建两个表时,所有数据都与在父表中找到的值以及数据类型相匹配,但是我对于如何解决此问题感到迷茫。

我删除了付款表,然后再次创建它,以确保我具有正确的数据类型并引用了正确的表,在这种情况下,引用了acount表(我知道它的拼写不正确,但是对于由于某些原因,它不允许我创建account表)。

`CREATE TABLE PAYMENT
    (Payment_ID int(6) not null,
    payment_date date not null,
    payment_amount decimal(10,2) not null,
    Account_ID int(6) not null,

constraint pk_payment primary key (Payment_ID),

constraint fk_payment foreign key (Account_ID) references acount (Account_ID));`

CREATE TABLE ACOUNT 
    (Account_ID int(6) not null,
    Account_Balance decimal(10,2) not null,

constraint pk_acount primary key (Account_ID));`

1 个答案:

答案 0 :(得分:1)

尝试更改顺序

CREATE TABLE ACOUNT 
    (Account_ID int(6) not null,
    Account_Balance decimal(10,2) not null,

constraint pk_acount primary key (Account_ID));`

  CREATE TABLE PAYMENT
  (Payment_ID int(6) not null,
  payment_date date not null,
  payment_amount decimal(10,2) not null,
  Account_ID int(6) not null,

  constraint pk_payment primary key (Payment_ID),

  constraint fk_payment foreign key (Account_ID) references acount (Account_ID));

首先创建PAYMENT,然后尝试访问不存在的表和列。