我该如何修理我的桌子以便它们工作?

时间:2019-05-06 20:21:11

标签: sql oracle

我已经将所有数据输入到表中,但是当我在SQL中运行它时,会显示以下消息:

  

第1行出现错误:   ORA-02291:完整性约束(SYSTEM.SYS_C008579)被违反-父键未   找到

create table B_C_MAP (
B_C_ID INTEGER PRIMARY KEY,
B_ID    integer references BUSINESS(B_ID),
C_ID    integer references CATEGORY(C_ID)
);

insert into BUSINESS values (201, 'Gainesville', 'Bonefish', 301, 'BF Management');
insert into BUSINESS values (202, 'Manassas', 'Target', 302, 'Target Management');
insert into BUSINESS values (203, 'Centreville', 'Trader Joes', 302, 'TJ Management');
insert into BUSINESS values (204, 'Gainesville,', 303, 'VA Tire and Auto', 'TA Management');
insert into BUSINESS values (205, 'Manassas', 'Okras', 301, 'Okras Management');
insert into BUSINESS values (206, 'Gainesville', 'Giant', 302, 'Giant Management');
insert into BUSINESS values (207, 'Centreville', 'Kemper Carpet', 303, 'KC Management');
insert into BUSINESS values (208, 'Manassas', 'Imperial Auto', 303, 'IA Management');
insert into BUSINESS values (209, 'Gainesville', 'Glory Days Grille', 301, 'GDG Management');
insert into BUSINESS values (210, 'Manassas', 'Cracker Barrel', 301, 'SW Management');

insert into CATEGORY values (301, 'Food');
insert into CATEGORY values (302, 'Retail');
insert into CATEGORY values (303, 'Service');

insert into B_C_MAP values (401, 301,201);
insert into B_C_MAP values (402, 301, 205);
insert into B_C_MAP values (403, 301,209);
insert into B_C_MAP values (404, 301,210);
insert into B_C_MAP values (405, 302,202);
insert into B_C_MAP values (406, 302,203);
insert into B_C_MAP values (407, 302,206);
insert into B_C_MAP values (408,303,204);
insert into B_C_MAP values (409, 303,207);
insert into B_C_MAP values (410, 303,208);

4 个答案:

答案 0 :(得分:2)

养成在插入内容中列出您具有值的字段的习惯,这样就不会像以前那样在错误的位置输入它们。

model_dir

insert into B_C_MAP(b_c_id, c_id, b_id) values (401, 301,201);

答案 1 :(得分:1)

说明::您似乎在B_C_MAP插入语句中翻转了“业务”和“类别”的值。

参考文献: http://www.dba-oracle.com/t_ora_02291_integrity_constraint_string_string_violated_parent_key_not_found.htm

示例:

B_ID = 201, 202, 203, 204, 205, 206, 207, 208, 209, 210
C_ID = 301, 302, 303

insert into B_C_MAP values (B_C_ID, B_ID, C_ID);
insert into B_C_MAP values (401, 201, 301);

固定的插入语句:

insert into B_C_MAP (B_C_ID, B_ID, C_ID) values (401, 201, 301);
insert into B_C_MAP (B_C_ID, B_ID, C_ID) values (402, 205, 301);
insert into B_C_MAP (B_C_ID, B_ID, C_ID) values (403, 209, 301);
insert into B_C_MAP (B_C_ID, B_ID, C_ID) values (404, 210, 301);
insert into B_C_MAP (B_C_ID, B_ID, C_ID) values (405, 202, 302);
insert into B_C_MAP (B_C_ID, B_ID, C_ID) values (406, 203, 302);
insert into B_C_MAP (B_C_ID, B_ID, C_ID) values (407, 206, 302);
insert into B_C_MAP (B_C_ID, B_ID, C_ID) values (408, 204, 303);
insert into B_C_MAP (B_C_ID, B_ID, C_ID) values (409, 207, 303);
insert into B_C_MAP (B_C_ID, B_ID, C_ID) values (410, 208, 303);

答案 2 :(得分:0)

  

插入B_C_MAP(B_C_ID,C_ID,B_ID)值(401、301,201);

您正在写交换的值。没有BUSINESS的主键值为301。这就是Oracle拒绝插入行的原因。

解决方案1(首选且安全)

insert into B_C_MAP (B_C_ID, C_ID, B_ID) values (401, 301, 201);

解决方案2(懒惰和不安全)

insert into B_C_MAP values (401, 201, 301);

答案 3 :(得分:0)

我看不到类别和业务表的结构,但是似乎必须在业务插入之前执行类别插入