简体   繁体   English

具有自动生成键的外键约束,基本问题(InnoDB)

[英]Foreign key constraints with automatic generated keys, basic question (InnoDB)

I'm new to foreign key constraints.我是外键约束的新手。 I will formulate a simple example to explain my situation.我将制定一个简单的例子来解释我的情况。

I have a table user and a table entry .我有一个表user和一个表entry In user there is a user.firstEntry which is a foreign key to entry.EntryID .user中有一个user.firstEntry ,它是entry.EntryID的外键。 In entry there is a entry.userID which is a foreign key to the user.userID table.entry中有一个entry.userID ,它是user.userID表的外键。 These IDs are all auto increment values.这些 ID 都是自动递增值。

Are cycles like that forbidden?这样的循环是禁止的吗? Then I will have to change the design?那么我将不得不改变设计?

I am not able to insert some valid entry into both tables, because the first insert already says that there's a problem with the constraints.我无法在两个表中插入一些有效的条目,因为第一个插入已经表明约束存在问题。 Auto commit is off.自动提交已关闭。

What shall I do?我该怎么办?

Thanks谢谢

Both user and entry need the other to be already created beforehand.用户和条目都需要预先创建另一个。 and since either cant be created without the other, you will have this problem IF foreign constraints check is on that is.并且由于任何一个都不能在没有另一个的情况下创建,因此如果启用了外部约束检查,您将遇到这个问题。

Whatever I can understand from your question, each user seems to have multiple entries.无论我从您的问题中了解到什么,每个用户似乎都有多个条目。 So your table design could look like Table_User(user_id(pk), user_name etc) and the entry table could be Table_Entry(entry_id(pk), entry_whatever,...,user_id(fk to user table)) As it seems the user is independent but the entries are dependent on users.所以你的表设计可能看起来像 Table_User(user_id(pk), user_name etc) 而条目表可能是 Table_Entry(entry_id(pk), entry_whatever,...,user_id(fk to user table)) 看起来用户是独立,但条目取决于用户。

A foreign key constraint is supposed to prevent your from adding invalid data into the foreign key column.外键约束应该防止您将无效数据添加到外键列中。

In most cases it will check to see if the value actually exists in the specified table.在大多数情况下,它将检查该值是否实际存在于指定的表中。 Because you have a cycle in your user and entry table, when you attempt to create a entry it will check to see if the value of entry.userID exists in the user table.因为您的用户和条目表中有一个循环,所以当您尝试创建一个条目时,它会检查用户表中是否存在 entry.userID 的值。 It will do the same when you attempt to add a new user, it will check the entry table for the value you entered for user.firstEntry.当您尝试添加新用户时,它将执行相同的操作,它将检查条目表中您为 user.firstEntry 输入的值。 If both user and entry are new there is no way to link the two because of your cycle.如果用户和条目都是新的,则由于您的周期,无法将两者联系起来。 A new entry record needs an existing user and a new user record needs an existing entry.新条目记录需要现有用户,新用户记录需要现有条目。 When both tables are empty I don't think you will be able to satisfy the constraint.当两个表都为空时,我认为您将无法满足约束。

I would suggest keeping the foreign key to userID in the entry table (since I'm assuming entries are linked to users) and finding some other way to represent a user's first entry.我建议将 userID 的外键保留在条目表中(因为我假设条目链接到用户)并找到其他方式来表示用户的第一个条目。 Maybe an user_entry_history table or something along those lines.也许是一个 user_entry_history 表或类似的东西。

DISCLAIMER - It's been awhile since I messed with Database design.免责声明 - 自从我搞砸数据库设计以来已经有一段时间了。

Bit strange design, but you can do this:有点奇怪的设计,但你可以这样做:

When creating a User, set firstEntry to NULL.创建用户时,将 firstEntry 设置为 NULL。 Insert an Entry with that user's id.插入具有该用户 ID 的条目。 Update Users and set firstEntry to the id of the inserted entry.更新用户并将 firstEntry 设置为插入条目的 id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM