当前位置: 首页 > 知识库问答 >
问题:

ORA-02291:违反完整性约束(H00300581.FK_类)-未找到父密钥

顾昊穹
2023-03-14

当我做插入它给我这个错误:

ORA-02291:违反完整性约束(H00300581.FK_类)-未找到父密钥

对于注册表中的所有插入,都会发生此错误。我需要帮忙,请帮我修一下。

是否来自表中的外键?可能是因为我什么都试过了,但都没用。

创建表

drop table g3_trainers CASCADE CONSTRAINTS;
drop table g3_classes CASCADE CONSTRAINTS;
drop table g3_members CASCADE CONSTRAINTS;
drop table g3_registration CASCADE CONSTRAINTS;

CREATE TABLE G3_TRAINERS
(
TRAINER_ID VARCHAR(6) NOT NULL,
TRAINER_NAME VARCHAR(50) NOT NULL,
COUNTRY VARCHAR(25) NOT NULL,
DOB DATE NOT NULL,
SPECIALITY VARCHAR(30) NOT NULL,
DESCRIPTION VARCHAR(500) NOT NULL,
PRIMARY KEY  (TRAINER_ID)
);

CREATE TABLE G3_CLASSES
(
CLASS_NUM VARCHAR(6) NOT NULL,
TRAINER_ID VARCHAR(6) NOT NULL,
CLASS_TITLE VARCHAR(50) NOT NULL,
START_DATE DATE NOT NULL,
END_DATE DATE NOT NULL,
COST NUMBER(6) NOT NULL,
PRIMARY KEY  (CLASS_NUM),
CONSTRAINT fk_TRAINERS FOREIGN KEY(TRAINER_ID) REFERENCES G3_TRAINERS(TRAINER_ID)
);

CREATE TABLE G3_MEMBERS
(
MEMBER_ID VARCHAR(6) NOT NULL,
MEMBER_NAME VARCHAR(50) NOT NULL,
MEMBER_AGE NUMBER(2) NOT NULL,
GENDER VARCHAR(10) NOT NULL,
PHONE_NUMBER VARCHAR(10) NOT NULL,
CHECK (regexp_like (PHONE_NUMBER,'^(\d{3}-\d{3}-?\d{4}|\d{10})$')),
CHECK (MEMBER_AGE>=17),
CHECK(MEMBER_AGE NOT LIKE '%[^a-zA-Z]%'),
PRIMARY KEY  (MEMBER_ID)
);

CREATE TABLE G3_REGISTRATION
(
REGISTRATION_ID VARCHAR(6) NOT NULL,
MEMBER_ID VARCHAR(6) NOT NULL,
AMOUNT_TO_PAY NUMBER(6) NOT NULL,
CLASS_NUM VARCHAR(6) NOT NULL,
PRIMARY KEY  (REGISTRATION_ID),
CONSTRAINT fk_MEMBERS FOREIGN KEY(MEMBER_ID) REFERENCES G3_MEMBERS(MEMBER_ID),
CONSTRAINT fk_CLASSES FOREIGN KEY(CLASS_NUM) REFERENCES G3_CLASSES(CLASS_NUM)
);

创建序列

CREATE SEQUENCE G3_TRAINER_Seq
 START WITH     10001
 INCREMENT BY   1
 NOCACHE
 NOCYCLE;

CREATE SEQUENCE G3_MEMBER_Seq
 START WITH     20001
 INCREMENT BY   1
 NOCACHE
 NOCYCLE;

CREATE SEQUENCE G3_CLASSE_Seq
 START WITH     30001
 INCREMENT BY   1
 NOCACHE
 NOCYCLE;

CREATE SEQUENCE G3_REGISTRATIONS_Seq
 START WITH     40001
 INCREMENT BY   1
 NOCACHE
 NOCYCLE;

培训师数据

 INSERT INTO G3_TRAINERS (TRAINER_ID, TRAINER_NAME, COUNTRY, DOB, SPECIALITY, DESCRIPTION)
 VALUES (G3_TRAINER_Seq.nextVal, 'Ahmed', 'Eygpt', '2/3/1988', 'Body Building', 'I describe my self as an active guy i workout everyday and thats why i desided to train people to be good as me');

 INSERT INTO G3_TRAINERS (TRAINER_ID, TRAINER_NAME, COUNTRY, DOB, SPECIALITY, DESCRIPTION)
 VALUES (G3_TRAINER_Seq.nextVal, 'John', 'Canada', '4/6/1987', 'Crossfit', 'I like being healthy and work as much as i can to be fit and to have a great life');

 INSERT INTO G3_TRAINERS (TRAINER_ID, TRAINER_NAME, COUNTRY, DOB, SPECIALITY, DESCRIPTION)
 VALUES (G3_TRAINER_Seq.nextVal, 'Sara', 'Canada', '9/1/1990', 'Yoga', 'I like interacting with poeple, and to teach them how yoga will be a very good source of flexing and relaxing your body');

 INSERT INTO G3_TRAINERS (TRAINER_ID, TRAINER_NAME, COUNTRY, DOB, SPECIALITY, DESCRIPTION)
 VALUES (G3_TRAINER_Seq.nextVal, 'Jun', 'China', '12/4/1981', 'Crossfit', 'I describe my self as an daily working guy i like my job because i get to know more about people and i teach many people how to be fit');

  INSERT INTO G3_TRAINERS (TRAINER_ID, TRAINER_NAME, COUNTRY, DOB, SPECIALITY, DESCRIPTION)
 VALUES (G3_TRAINER_Seq.nextVal, 'Mike', 'Ohio', '2/22/1981', 'Body Building', 'Being strong and big is what i am aiming for, i like keeping my diet healthy and to train poeple to be healthy and big like me');

 INSERT INTO G3_TRAINERS (TRAINER_ID, TRAINER_NAME, COUNTRY, DOB, SPECIALITY, DESCRIPTION)
 VALUES (G3_TRAINER_Seq.nextVal, 'Mia', 'Germany', '12/12/1987', 'Yoga', 'I describe myself as a hard working i love my job and i love my members');

 INSERT INTO G3_TRAINERS (TRAINER_ID, TRAINER_NAME, COUNTRY, DOB, SPECIALITY, DESCRIPTION)
 VALUES (G3_TRAINER_Seq.nextVal, 'Essam', 'UAE', '1/5/1980', 'Body Building', 'I am a hard worker and i like to train my members and i want them to be healthy and strong');

类数据

 INSERT INTO G3_CLASSES (CLASS_NUM, TRAINER_ID, CLASS_TITLE, START_DATE, END_DATE, COST)
 VALUES (G3_CLASSE_Seq.nextVal, 10001, 'Weight Lifting Class', '1/12/2017', '1/1/2018', 1150);

 INSERT INTO G3_CLASSES (CLASS_NUM, TRAINER_ID, CLASS_TITLE, START_DATE, END_DATE, COST)
 VALUES (G3_CLASSE_Seq.nextVal, 10001, 'Weight Lifting Class', '1/2/2018', '1/3/2018', 1150);

 INSERT INTO G3_CLASSES (CLASS_NUM, TRAINER_ID, CLASS_TITLE, START_DATE, END_DATE, COST)
 VALUES (G3_CLASSE_Seq.nextVal, 10002, 'Body Weight Class', '2/1/2018', '2/2/2018', 950);

 INSERT INTO G3_CLASSES (CLASS_NUM, TRAINER_ID, CLASS_TITLE, START_DATE, END_DATE, COST)
 VALUES (G3_CLASSE_Seq.nextVal, 10003, 'Yoga Class', '3/2/2018', '3/3/2018', 750);

 INSERT INTO G3_CLASSES (CLASS_NUM, TRAINER_ID, CLASS_TITLE, START_DATE, END_DATE, COST)
 VALUES (G3_CLASSE_Seq.nextVal, 10003, 'Yoga Class', '5/5/2018', '5/6/2018', 750);

 INSERT INTO G3_CLASSES (CLASS_NUM, TRAINER_ID, CLASS_TITLE, START_DATE, END_DATE, COST)
 VALUES (G3_CLASSE_Seq.nextVal, 10004, 'Body Weight Class', '10/2/2018', '10/3/2018', 950);

 INSERT INTO G3_CLASSES (CLASS_NUM, TRAINER_ID, CLASS_TITLE, START_DATE, END_DATE, COST)
 VALUES (G3_CLASSE_Seq.nextVal, 10004, 'Body Weight Class', '10/4/2018', '10/5/2018', 950);

 INSERT INTO G3_CLASSES (CLASS_NUM, TRAINER_ID, CLASS_TITLE, START_DATE, END_DATE, COST)
 VALUES (G3_CLASSE_Seq.nextVal, 10005, 'Weight Lifting Class', '12/20/2017', '1/20/2018', 1150);

 INSERT INTO G3_CLASSES (CLASS_NUM, TRAINER_ID, CLASS_TITLE, START_DATE, END_DATE, COST)
 VALUES (G3_CLASSE_Seq.nextVal, 10005, 'Weight Lifting Class', '2/20/2018', '3/20/2018', 1150);

 INSERT INTO G3_CLASSES (CLASS_NUM, TRAINER_ID, CLASS_TITLE, START_DATE, END_DATE, COST)
 VALUES (G3_CLASSE_Seq.nextVal, 10005, 'Weight Lifting Class', '4/20/2018', '5/20/2018', 1150);

 INSERT INTO G3_CLASSES (CLASS_NUM, TRAINER_ID, CLASS_TITLE, START_DATE, END_DATE, COST)
 VALUES (G3_CLASSE_Seq.nextVal, 10006, 'Yoga Class', '12/28/2017', '1/28/2018', 750);

 INSERT INTO G3_CLASSES (CLASS_NUM, TRAINER_ID, CLASS_TITLE, START_DATE, END_DATE, COST)
 VALUES (G3_CLASSE_Seq.nextVal, 10007, 'Weight Lifting Class', '10/3/2018', '10/4/2018', 1150);

 INSERT INTO G3_CLASSES (CLASS_NUM, TRAINER_ID, CLASS_TITLE, START_DATE, END_DATE, COST)
 VALUES (G3_CLASSE_Seq.nextVal, 10007, 'Weight Lifting Class', '10/5/2018', '10/6/2018', 1150);

成员数据

 INSERT INTO G3_MEMBERS (MEMBER_ID, MEMBER_NAME, MEMBER_AGE, GENDER, PHONE_NUMBER)
 VALUES (G3_MEMBER_Seq.nextVal, 'Mohammed', '21', 'Male', '0502202212');

 INSERT INTO G3_MEMBERS (MEMBER_ID, MEMBER_NAME, MEMBER_AGE, GENDER, PHONE_NUMBER)
 VALUES (G3_MEMBER_Seq.nextVal, 'Afra', '28', 'Female', '0555544558');

 INSERT INTO G3_MEMBERS (MEMBER_ID, MEMBER_NAME, MEMBER_AGE, GENDER, PHONE_NUMBER)
 VALUES (G3_MEMBER_Seq.nextVal, 'Essa', '30', 'Male', '0555544558');

 INSERT INTO G3_MEMBERS (MEMBER_ID, MEMBER_NAME, MEMBER_AGE, GENDER, PHONE_NUMBER)
 VALUES (G3_MEMBER_Seq.nextVal, 'Ahmed', '19', 'Male', '0563020300');

 INSERT INTO G3_MEMBERS (MEMBER_ID, MEMBER_NAME, MEMBER_AGE, GENDER, PHONE_NUMBER)
 VALUES (G3_MEMBER_Seq.nextVal, 'Alex', '25', 'Female', '0521022932');

 INSERT INTO G3_MEMBERS (MEMBER_ID, MEMBER_NAME, MEMBER_AGE, GENDER, PHONE_NUMBER)
 VALUES (G3_MEMBER_Seq.nextVal, 'Abdulla', '24', 'Male', '0504493393');

 INSERT INTO G3_MEMBERS (MEMBER_ID, MEMBER_NAME, MEMBER_AGE, GENDER, PHONE_NUMBER)
 VALUES (G3_MEMBER_Seq.nextVal, 'Abdulrahman', '20', 'Male', '0502292290');

 INSERT INTO G3_MEMBERS (MEMBER_ID, MEMBER_NAME, MEMBER_AGE, GENDER, PHONE_NUMBER)
 VALUES (G3_MEMBER_Seq.nextVal, 'Olivia', '21', 'Female', '0561020200');

 INSERT INTO G3_MEMBERS (MEMBER_ID, MEMBER_NAME, MEMBER_AGE, GENDER, PHONE_NUMBER)
 VALUES (G3_MEMBER_Seq.nextVal, 'Omar', '30', 'Male', '0502503323');

 INSERT INTO G3_MEMBERS (MEMBER_ID, MEMBER_NAME, MEMBER_AGE, GENDER, PHONE_NUMBER)
 VALUES (G3_MEMBER_Seq.nextVal, 'Nasser', '29', 'Male', '0504334439');

 INSERT INTO G3_MEMBERS (MEMBER_ID, MEMBER_NAME, MEMBER_AGE, GENDER, PHONE_NUMBER)
 VALUES (G3_MEMBER_Seq.nextVal, 'Sophia', '22', 'Female', '0561616316');

 INSERT INTO G3_MEMBERS (MEMBER_ID, MEMBER_NAME, MEMBER_AGE, GENDER, PHONE_NUMBER)
 VALUES (G3_MEMBER_Seq.nextVal, 'Nadia', '21', 'Female', '0507797786');

注册资料

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20001, 1150, 30001);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20001, 750, 30004);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20002, 750, 30004);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20002, 950, 30006);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20003, 1150, 30001);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20003, 1150, 300010);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20004, 950, 30006);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20004, 1150, 30010);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20005, 1150, 30007);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20005, 750, 30011);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20006, 1150, 30013);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20006, 1150, 30012);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20007, 1150, 30009);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20007, 750, 30011);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20008, 1150, 30008);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20008, 750, 30011);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20009, 1150, 30013);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20009, 750, 30005);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20010, 1150, 30013);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20010, 1150, 30012);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20010, 950, 30007);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20011, 1150, 30013);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20011, 750, 30005);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20012, 750, 30004);

 INSERT INTO G3_REGISTRATION (REGISTRATION_ID, MEMBER_ID, AMOUNT_TO_PAY, CLASS_NUM)
 VALUES (G3_REGISTRATIONS_Seq.nextVal, 20012, 1150, 30009);

共有1个答案

罗昕
2023-03-14

大多数语句缺少逗号,例如:

之间'Eygpt'

 INSERT INTO G3_TRAINERS (TRAINER_ID, TRAINER_NAME, COUNTRY, DOB,
 SPECIALITY, DESCRIPTION)  VALUES (G3_TRAINERS_Seq.nextVal, 'Ahmed',
 'Eygpt' '12/3/1988', 'Body Building', 'I describe my self as an active
 guy i workout everyday and thats why i desided to train people to be
 good as me');

在“举重类”之间

 INSERT INTO G3_CLASSES (CLASS_NUM, TRAINER_ID, CLASS_TITLE, START_DATE, END_DATE, COST)
 VALUES (G3_CLASSES_Seq.nextVal, 10001, 'Weight Lifting Class' '1/12/2017', '1/1/2018', 1150);

您有CONSTRAINTfk_CLASSESFOREIGN KEY(CLASS_NUM)REFERENCESG3_CLASSES(CLASS_NUM)作为约束

您有G3_类_Seq序列来填充G3_类表中的class_num列,该表应便于使用fk_类外键,但您有另一个序列G3_类_Seq30000开始。顺便说一下,它似乎应该从30001

i、 e.注意G3类与G3类不同。

顺便说一下,在创建句子中删除列名之间的空格,如下所示:

作为一个例子,转换这个

CREATE TABLE G3_TRAINERS
(

TRAINER_ID VARCHAR(6) NOT NULL,
TRAINER_NAME VARCHAR(50) NOT NULL,
COUNTRY VARCHAR(25) NOT NULL,
DOB DATE NOT NULL,
SPECIALITY VARCHAR(30) NOT NULL,
DESCRIPTION VARCHAR(500) NOT NULL,

PRIMARY KEY  (TRAINER_ID)

);

为此:

CREATE TABLE G3_TRAINERS
(
TRAINER_ID VARCHAR(6) NOT NULL,
TRAINER_NAME VARCHAR(50) NOT NULL,
COUNTRY VARCHAR(25) NOT NULL,
DOB DATE NOT NULL,
SPECIALITY VARCHAR(30) NOT NULL,
DESCRIPTION VARCHAR(500) NOT NULL,
PRIMARY KEY  (TRAINER_ID)
);

下面的语句中还有一个问题(300010

在G3_注册(注册ID、会员ID、支付金额、类别编号)中插入值(G3_注册Seq.nextVal,200031150,300010);

 类似资料:
  • 所以这里是我的数据库: 插入表格: 批次表不断出现的错误是:ORA-02290:违反了检查约束(EJAY.batches\u DATE\u CHK) 学生表出现的错误是:ORA-02291:违反完整性约束-未找到父键 我感到困惑的是,它只显示在表格中的一些行。教职员工和课程都很好,只有这两个。如果答案很明显,我道歉,我是初学者。请帮帮忙

  • 我试图为和创建会话,这两个表都与相同的外键关联到登录表-但是当我为创建种子时,我得到了错误,在我的另一个问题在这个链接中,我没有以最好的方式解释,但得到了一些关于使用(党-角色-关系模型)和这段代码下面他更多的想法有关已解决的问题链接 创建表和主键 创建表和主键 创建表以及主键和外键 使用表为管理员创建会话的过程: 使用表为用户创建会话的过程: 创建管理会话时,我收到一个错误 我做错了什么?

  • 我遇到了一些SQL外键的问题。 下面是我的表和插入SQL。 SQL ORA-02291:违反完整性约束-未找到父键 我不知道为什么我的外键是错的?

  • 当我创建表时,我将作为主键,将作为外键从同一个表中创建,作为dep表中的外键,当我想插入显示的值时,现在我感到困惑。 该表包含以下内容: 我第一次使用以下命令行: 然后我尝试不使用,如下所示:

  • 问题内容: 嗨,我正在Oracle SQL开发人员中开发数据库,​​试图从另一个表访问foriegn键。我目前正在使用以下CREATE语句创建的ItemOrdered表 如您所见,我具有以下前键Ords和BeltID。 现在,当我尝试运行以下语句时 它给了我以下错误 违反-找不到父密钥02291。00000-“违反完整性约束(%s。%s)-找不到父密钥” 如果需要,我已经提供了Ords CREAT

  • 我最近正在了解这一点,我想知道我是否可以帮助自己,这里是我的数据库: 插入值