简体   繁体   English

一个表中两列之间的外键关系

[英]Foreign Key relationship between two columns in one table

I'm building a database table representing a 'note' that has a parent-child relationship in two of it's columns, like so: 我正在构建一个数据库表,表示在其中两列中具有父子关系的“注释”,如下所示:

ID           INT(10) PK NOT NULL UNSIGNED ZEROFILL AUTOINCREMENT  -- Primary key
parent_ID    INT(10) UNSIGNED ZEROFILL                 -- References Primary key
username     VARCHAR(30) NOT NULL
information  VARCHAR(256) NOT NULL
comment      VARCHAR(256) NOT NULL

where username is a foreign key to a user table and is the username of the person who wrote the note, information is the information contained in the note and comment is what username added. 其中usernameuser表的外键,是编写注释的人的用户名, informationcomment包含的信息, commentusername添加的。 comment will always have something in it, and is not necessarily the same as the previous user. comment将始终包含一些内容,并且不一定与之前的用户相同。

The concept is that someone can "copy" a note and have their own comments, and say "I got this note from xxx", hence the parent-child relationship. 这个概念是有人可以“复制”一个笔记并有自己的评论,并说“我从xxx获得了这个笔记”,因此是亲子关系。 A bit like sharing in facebook, perhaps. 或许有点像在Facebook上分享。

How do I correctly form the parent_ID column? 如何正确形成parent_ID列? Should I use an identifying relationship; 我应该使用识别关系; which sides are mandatory? 哪一方是强制性的? I figure this has to be a 1-to-Many relationship since five people can copy the same note. 我认为这必须是1对多的关系,因为五个人可以复制相同的音符。

I expect there to be more copying than creating new notes, so there will be a relatively small number of NULL 's in the records, but to get rid of nulls entirely should I apply a NOT NULL constraint and simply make the default parent 0 with an essentially meaningless record at ID 0 and take note of this in the software? 我希望复制比创建新笔记更多,因此记录中会有相对较少的NULL ,但是为了完全摆脱空值,我应该应用NOT NULL约束并简单地使默认父0 ID 0基本上没有意义的记录,并在软件中注意到这一点?

Is this even the correct way? 这甚至是正确的方法吗? Should I employ a two-table system with: 我应该使用双表系统:

ID          INT(10)      PK
information VARCHAR(256)
orig_user   VARHCAR(30)  FK  -- Potentially

and

ID          INT(10)      PK FK
username    VARCHAR(30)  PK FK
comment     VARCHAR(256)

Which eliminates any possible NULL 's by definiton? 哪个通过定义消除了任何可能的NULL

Thanks 谢谢

Should I use an identifying relationship 我应该使用识别关系吗?

No, only ID should be in PK since it alone is unique. 不,只有ID应该在PK中,因为它本身就是唯一的。 Also, a root (parent-less) note will have a NULL parent_ID and NULLs cannot be in PK. 此外,根(无父)注释将具有NULL parent_ID并且NULL不能在PK中。

I figure this has to be a 1-to-Many relationship since five people can copy the same note. 我认为这必须是1对多的关系,因为五个人可以复制相同的音符。

Correct. 正确。 It's also a tree, since there can be multiple levels of copying. 它也是一棵树,因为可以有多个级别的复制。

... should I apply a NOT NULL constraint and simply make the default parent 0 with an essentially meaningless record at ID 0 and take note of this in the software? ...我应该应用NOT NULL约束并简单地使默认父0在ID 0处具有基本无意义的记录并在软件中记下这一点吗?

No need, FK would still be enforced on 0 and you'd need a "dummy" row just to satisfy it, as you already noted. 没有必要,FK仍会在0上执行,你需要一个“虚拟”行来满足它,正如你已经注意到的那样。 FK ignores NULLs, so just put a NULL in the root note's parent_ID . FK忽略NULL,所以只需在根音的parent_ID放置一个NULL。

Should I employ a two-table system... 我应该采用双桌制......

This doesn't model the same thing. 这不会模仿同样的事情。 It just allows you to connect to the original user, but not original note. 它只允许您连接到原始用户,而不是原始注释。

A different two-table design would be viable if a single note could have multiple parents, but that doesn't seem to be the case here. 如果单个音符可以有多个父母,那么不同的双表设计是可行的,但这似乎不是这种情况。

Which eliminates any possible NULL's by definiton? 通过定义消除任何可能的NULL?

You seem very intent on eliminating NULLs. 你似乎非常想消除NULL。 Any reason for that? 有什么理由吗?


All in all, you should store notes in just one table, like this: 总而言之,您应该将注释存储在一个表中,如下所示:

CREATE TABLE note (
    ID           INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    parent_ID    INT(10) UNSIGNED,
    username     VARCHAR(30) NOT NULL,
    information  VARCHAR(256) NOT NULL,
    comment      VARCHAR(256) NOT NULL,
    FOREIGN KEY (parent_ID) REFERENCES note (ID),
    FOREIGN KEY (username) REFERENCES `user` (username)
);

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

相关问题 按条件从一列到不同表的两列的外键 - foreign key from one column to two columns of different table by condition 如何在两个表中建立外键关系 - how to make FOREIGN KEY Relationship in Two table 是否可以在Laravel的String / varchar类型的列的两个表之间创建外键关系? - Is it possible to create a foreign key relationship between two tables on String/varchar type columns in Laravel? 如何命名SQL FOREIGN KEY约束,将两个表列创建为关系表 - How to name SQL FOREIGN KEY Constraint on CREATE two tables columns into a relationship TABLE 两个不同外键列的同一个表的外键关系 - Foreign key relationships with the same table for two different foreign key columns 同一表中的两列和同一外键 - Two columns in same table and same foreign key 如何在 SQL 的一个表中使用两个相同的外键命名连接表中的两列? - How to name two columns in join tables with two same foreign key at one table in SQL? 我们可以在一个表中将一个FOREIGN KEY定义为多于两列吗? - Can we define One FOREIGN KEY as more than Two columns in one Table? 一个表中的两列是否可以具有另一表中同一列的外键? - Can two columns from one table have a foreign key to the same column in another table? 在两个表之间创建关系并创建外键 - Creating a relationship between two tables and creating a foreign key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM