用外键将数据插入表中 [英] Inserting data into a table with a Foreign key

查看:133
本文介绍了用外键将数据插入表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为数据库创建了所有表,但是我正在手动插入20条随机记录,
我正在尝试将数据插入表中:

创建表ACCOUNT(acc_ID int NOT NULL,acc_name varchar(20)NOT NULL,acc_manager varchar(20)NOT NULL,acc_balance DECIMAL(6)NOT NULL,acc_transationLimit int NOT NULL,acc_creditlimit int NOT NULL,out_ID int NOT NULL,主键(acc_ID),FOREIGN KEY(out_ID)参考出口(out_ID));

即时通讯使用安全外壳来实现sql,我正在输入:



(``000'',``account1'',``parker'',``000.000'',``99999'',``50000''),
(``001'',``account2'',``kate'',``200.000'',``99999'',``40000''),
(``002'',``account3'',``fred'',``240.000'',``99999'',``20000''),
(``003'',``account4'',``cat'',``500.000'',``99999'',``60000''),
(``004'',``account5'',``parker'',``350.000'',``99999'',``50000''),
(``005'',``account6'',``kate'',``120.000'',``99999'',``40000''),
(``006'',``account7'',``fred'',``100.000'',``99999'',``20000''),
(``007'',``account8'',``cat'',``090.000'',``99999'',``60000''),
(``008'',``account9'',``parker'',``400.000'',``99999'',``50000''),
(``009'',``account10'',``kate'',``900.000'',``99999'',``40000''),
(``010'',``account11'',``fred'',``340.000'',``99999'',``20000''),
(``011'',``account12'',``cat'',``850.000'',``99999'',``60000''),
(``012'',``account13'',``parker'',``400.000'',``99999'',``50000''),
(``013'',``account14'',``kate'',``250.000'',``99999'',``40000''),
(``014'',``account15'',``fred'',``460.000'',``99999'',``20000''),
(``015'',``account16'',``cat'',``210.000'',``99999'',``60000''),
(``016'',``account17'',``parker'',``660.000'',``99999'',``50000''),
(``017'',``account18'',``kate'',``410.000'',``99999'',``40000''),
(``018'',``account19'',``fred'',``000.000'',``99999'',``20000''),
(``019'',``account20'',``parker'',``100.000'',``99999'',``50000'');


但是我得到了这个错误:

错误1452(23000):无法添加或更新子行:外键约束失败(`JM`.`ACCOUNT`,CONSTRAINT`ACCOUNT_ibfk_1` FOREIGN KEY(`out_ID`)参考`OUTLET`(`out_ID`))

Hi, I''ve created all my tables for a database however I am manually inserting 20 random records,
I am trying to insert the data into my table which is:

create table ACCOUNT (acc_ID int NOT NULL, acc_name varchar(20) NOT NULL, acc_manager varchar(20) NOT NULL, acc_balance DECIMAL(6) NOT NULL, acc_transationLimit int NOT NULL, acc_creditlimit int NOT NULL, out_ID int NOT NULL, PRIMARY KEY (acc_ID), FOREIGN KEY (out_ID) REFERENCES OUTLET (out_ID));

im using secure shell to implement the sql and i am inputting:



('' 000 '', ''account1'', '' parker '', '' 000.000'', '' 99999 '', '' 50000 ''),
('' 001 '', '' account2 '', '' kate '', '' 200.000 '', '' 99999 '', '' 40000 ''),
('' 002 '', '' account3 '', '' fred '', '' 240.000 '', '' 99999 '', '' 20000 ''),
('' 003 '', '' account4 '', '' cat '', '' 500.000 '', '' 99999 '', '' 60000 ''),
('' 004 '', '' account5 '', '' parker '', '' 350.000 '', '' 99999 '', '' 50000 ''),
('' 005 '', '' account6 '', '' kate '', '' 120.000 '', '' 99999 '', '' 40000 ''),
('' 006 '', '' account7 '', '' fred '', '' 100.000 '', '' 99999 '', '' 20000 ''),
('' 007 '', '' account8 '', '' cat '', '' 090.000 '', '' 99999 '', '' 60000 ''),
('' 008 '', '' account9 '', '' parker '', '' 400.000 '', '' 99999 '', '' 50000 ''),
('' 009 '', '' account10 '', '' kate '', '' 900.000 '', '' 99999 '', '' 40000 ''),
('' 010 '', '' account11 '', '' fred '', '' 340.000 '', '' 99999 '', '' 20000 ''),
('' 011 '', '' account12 '', '' cat '', '' 850.000 '', '' 99999 '', '' 60000 ''),
('' 012 '', '' account13 '', '' parker '', '' 400.000 '', '' 99999 '', '' 50000 ''),
('' 013 '', '' account14 '', '' kate '', '' 250.000 '', ''99999 '', '' 40000 ''),
('' 014 '', '' account15 '', '' fred '', '' 460.000 '', '' 99999 '', '' 20000 ''),
('' 015 '', '' account16 '', '' cat '', '' 210.000 '', '' 99999 '', '' 60000 ''),
('' 016 '', '' account17 '', '' parker '', '' 660.000 '', '' 99999 '', '' 50000 ''),
('' 017 '', '' account18 '', '' kate '', '' 410.000 '', '' 99999 '', '' 40000 ''),
('' 018 '', '' account19 '', '' fred '', '' 000.000 '', '' 99999 '', '' 20000 ''),
('' 019 '', '' account20 '', '' parker '', '' 100.000 '', '' 99999 '', '' 50000 '');


However I''ve been given this error:

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`JM`.`ACCOUNT`, CONSTRAINT `ACCOUNT_ibfk_1` FOREIGN KEY (`out_ID`) REFERENCES `OUTLET` (`out_ID`))

推荐答案

首先填充表OUTLET,并确保要在父表中插入的所有出口信息必须已经在出口表中可用.只有这样,我们才能将记录插入到您的主表中.

否则,我们可以在导致问题的查询中删除带有null的出口值.
First populate the table OUTLET and make sure all outlet info you are inserting in parent table must be already available in outlet table. Only then we can insert records into your primary table.

Otherwise, we can remove the outlet value with null in your query which create the issue .


如解决方案1所述,您可能会插入''out_ID''不在父表( OUTLET 表)中.

为此,您可以在SQL Server中暂时禁用外键约束并尝试插入数据.
As mention in the Solution 1 you may going to insert a ''out_ID'' which is not in the parent table (OUTLET table).

To check this you can temporarily disable foreign key constraint in sql server and try to insert data.
-- Disable single constraint

ALTER TABLE Table NOCHECK CONSTRAINT ConstraintName

-- Enable single constraint

ALTER TABLE Table CHECK CONSTRAINT ConstraintName

-- Disable all table constraints

ALTER TABLE Table NOCHECK CONSTRAINT ALL

-- Enable all table constraints

ALTER TABLE Table CHECK CONSTRAINT ALL


如果可行,那么肯定是您要插入不在OUTLET表中的out_id.


If it is worked then definitely the issue is you are going insert out_id which is not in the OUTLET table.


这篇关于用外键将数据插入表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆