简体   繁体   English

两个不同的主键使用相同的外键

[英]same foreign key for two different primary keys

my problem is that I don't know and I cannot figure out if it is possible to have two different primary keys refering the same foreighn key. 我的问题是我不知道,也无法弄清楚是否可以有两个不同的主键引用相同的外键。 To be more specific, I have three tables. 更具体地说,我有三个表。 The first table is called staff with attributes: id fname lname password . 第一个表称为staff,具有以下属性: id fname lname password The second table is called student with attributes: id fname lname password role and finally the third table that is called message and it has 4 attributes: id text date user_fk and I want the two primary keys to refer the foreighn key. 第二个表称为具有属性的学生: id fname lname password role ,最后一个表称为message,它具有4个属性: id text date user_fk ,我希望两个主键引用外键。 In phpmyadmin is quite easy and I made it. 在phpmyadmin中非常简单,我做到了。 The problem is that when I want to enter a new row the database, I suppose, cannot figure out if the primary key is from the staff table or from the student. 问题是,当我想输入新行时,我想数据库无法确定主键是来自职员表还是来自学生。 The message that I get is: #1452 - Cannot add or update a child row: a foreign key constraint fails ( diss . message , CONSTRAINT student_fk FOREIGN KEY ( user_fk ) REFERENCES student ( studentid )) . 我得到的是该消息: #1452 - Cannot add or update a child row: a foreign key constraint fails (迪斯.消息, CONSTRAINT student_fk FOREIGN KEY ( user_fk ) REFERENCES学生( studentid )) So my question is: is it completely wrong what I want to do? 所以我的问题是:我想做什么完全错误吗? Maybe it will be better to create for example another table that will store only the foreighn keys? 例如,也许最好创建另一个仅存储上述键的表? Or the error is somewhere else that I cannot find it? 还是错误是我找不到的其他地方? Thank you! 谢谢!

If I understand you correctly, the foreign key user_fk in message refers to student and staff ids. 如果我理解正确, message的外键user_fk指的是studentstaff ID。

In that case, if you try to insert data to message table, it will check both tables student and staff for that id. 在这种情况下,如果您尝试将数据插入到message表中,它将检查表studentstaff的ID。 Hence the error. 因此,错误。

What you can do is add two foreign key columns in message one for each table referred. 您可以做的是在每个引用的表的message添加两个外键列。

Update 更新

The above approach will lead to the possibility of nullable foreign keys. 上述方法将导致可为空的外键的可能性。 We can instead have two mapping tables one for each table. 相反,我们可以有两个映射表,每个表一个。 for eg: student_message_map and staff_message_map . 例如: student_message_mapstaff_message_map Each table will contain 2 foreign keys student_id and message_id / staff_id and message_id . 每个表将包含2个外键student_idmessage_id / staff_idmessage_id

This will make the columns not nullable but querying will need to do two joins. 这将使列不可为空,但查询将需要进行两次连接。 Also, someone looking at this relationship may think it is many to many relationship because of the mapping tables. 另外,由于映射表的原因,查看此关系的人可能会认为这是多对多关系。

IMO having a nullable foreign key is not bad practice, you just have to be aware while querying, about the possibility of null values. 拥有可空外键的IMO并不是不好的做法,您只需要在查询时注意有关空值的可能性即可。

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

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