简体   繁体   English

我应该有两个互相引用的主键吗?

[英]Should I have two primary keys referencing each other?

CREATE TABLE1 (
   ID INT NOT NULL PRIMARY KEY
   FOREIGN KEY ID REFERENCES TABLE2 (ID)
)

CREATE TABLE2 (
   ID INT NOT NULL,
   OTHER INT NOT NULL
   PRIMARY KEY (ID, OTHER)
   FOREIGN KEY ID REFERENCES TABLE1 (ID)
)

Table 1 and Table 2 are big tables containing separate sets of information. 表1和表2是包含独立信息集的大表。 They have a one-to-one relationship requiring full participation from both sides. 他们之间存在一对一的关系,需要双方的充分参与。 Where should I put the foreign key statement? 我应该在哪里放置外键声明? In Table1, Table2, or both? 在表1,表2中,还是在两者中? And why? 又为什么呢?

There is nothing particularly wrong with doing it, however you should put the foreign key in both tables. 这样做没有什么特别的错误,但是您应该将外键放在两个表中。 That means when you insert new values you'll have to start a transaction, do both inserts, then commit the transaction. 这意味着,当您插入新值时,您将必须开始事务,进行两次插入,然后提交事务。

I would strongly recommend merging the tables into one table. 我强烈建议将这些表合并为一个表。 It will make everything easier. 这将使一切变得容易。

Id to id relationship is considered . 从id到id的关系被考虑。 This is a great trick in implementation . 这是实现的一个绝妙技巧。

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

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