违反-未找到父键错误 [英] violated - parent key not found error
问题描述
我出现以下错误:
INSERT INTO GroupMembers VALUES ('Goldfrat', 'Simon Palm')
*
ERROR at line 1:
ORA-02291: integrity constraint (SHAHA1.IAM_IS_GROUP_FK) violated - parent key
not found
GroupMembers
表中的约束为:
CONSTRAINT iam_is_group_fk FOREIGN KEY(is_group) REFERENCES Members(group_name)
成员表如下:
CREATE TABLE Members (
group_name VARCHAR2(40),
CONSTRAINT g_id_pk PRIMARY KEY(group_name),
CONSTRAINT m_group_name_fk FOREIGN KEY(group_name) REFERENCES Artist(artistic_name));
在创建 GroupMembers
表之前,所有表都可以很好地创建.有人有主意吗?我已经抓挠了一段时间了.
All of the tables are created fine until it comes to creating the GroupMembers
table. Anyone have any ideas? I've been scratching for quite a while.
推荐答案
问题是
CONSTRAINT iam_is_group_fk FOREIGN KEY(is_group)参考成员(group_name);在group_name字段上引用表Member.
这意味着表 GroupMembers
上的字段is_group在表 Members
和 group_name
字段上必须具有相同的值.
This means that the field is_group on the table GroupMembers
must have an identical value on the table Members
and group_name
field.
我认为这是不好的做法.首先,您应该在表GroupMembers上有一个主键字段.您不应将组成员的名称存储在表GroupMembers中,而应将其对应的ID存储在表Member中.
In my opinion this is bad practice. First of all you should have a primary key field on the table GroupMembers. You should not store the names of the group members in the table GroupMembers, but their corresponding id from the table Members.
表成员"也应如下所示:
Also the table Members, should look something like this:
CREATE TABLE Members (
member_id NUMBER PRIMARY KEY
member_name VARCHAR2(40),
CONSTRAINT g_id_pk PRIMARY KEY(member_id),
CONSTRAINT m_group_name_fk FOREIGN KEY(group_name) REFERENCES Artist(artistic_name));
然后在表 GroupMembers
上,我想您想将一些成员关联到其组的组并返回,因此您应该执行以下操作:
Then on the table GroupMembers
, I suppose you want to associate some members to their set of groups and back, so you should do something like this:
CREATE TABLE GroupMembers (
member_id NUMBER,
group_id NUMBER
)
CONSTRAINT iam_is_member_fk FOREIGN KEY(member_id) REFERENCES Members(member_id);
CONSTRAINT iam_is_member_fk FOREIGN KEY(group_id) REFERENCES Groups(group_id);
假设您有一个包含所有组详细信息的表 Groups
,其中主键
存储为 number
,名称为group_id
.
Supposing that you have a table Groups
containing all the group details, with the primary key
stored as number
, and name group_id
.
始终记住每个表必须具有主键
.将此键设为数字是一种很好的做法.
Always remeber that each table must have a primary key
. It is good practice for this key to be a number.
因此,通过在 Members
中具有member_id,在 Groups
中具有 group_id
,您可以在 GroupMembers中建立多对多关系代码>.另外,在此表上放置一个唯一索引,这样就不会有重复项(同一成员多次与同一id关联).
So by having member_id in Members
, group_id
in Groups
, you can create a many to many relationship in GroupMembers
. Also, put a unique index on this table so you don't have duplicates ( the same member associated to the same id several times ).
看这个例子,将用户链接到角色.情况相同:
Look at this example linking users to roles. It is the same case:
这篇关于违反-未找到父键错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!