简体   繁体   English

引用同一个主键的两个外键

[英]Two foreign keys referencing the same primary key

Is this okay to have two foreign keys in one table referencing one primary key of other table?在一个表中有两个外键引用另一个表的一个主键是否可以?

EmployeeID is a primary key in the employee table and appearing as a foreign key twice in the timesheet table. EmployeeID 是雇员表中的主键,在时间表表中作为外键出现两次。

There will be few admin users filling up timsheets on the behalf of other employees.很少有管理员用户代表其他员工填写时间表。

In the timsheet table field 'TimsheetFor' will have employeeID of that person who has worked on projects and field 'EnteredBy' or 'FilledBy' will have employeeid of that person who has filled up this timesheet.在时间表表中,“TimsheetFor”字段将包含参与项目的人员的员工 ID,“EnteredBy”或“FilledBy”字段将包含填写此时间表的人员的员工 ID。

Which of the following option is correct?以下哪个选项是正确的?

NOTE: Tables are showing only those fields which are related to this question.注意:表格仅显示与此问题相关的字段。

在此处输入图片说明

I would go with option 1 .我会选择选项 1 It is perfectly fine to have two foreign key columns referencing the same primary key column in a different table since each foreign key value will reference a different record in the related table.有两个外键列引用不同表中的相同主键列是完全没问题的,因为每个外键值将引用相关表中的不同记录。

I'm sure option 2 would work, but you would essentially have a 1-to-1 relationship between TIMESHEET_TABLE and TIMESHEET_FILLED_BY , making two tables unnecessary and more difficult to maintain.我确信选项 2会起作用,但您基本上会在TIMESHEET_TABLETIMESHEET_FILLED_BY之间TIMESHEET_TABLE TIMESHEET_FILLED_BY关系,从而使两个表变得不必要且更难以维护。

In fact, if both ENTERED_BY and TIMESHEET_FOR are required in pairs, using option 1 makes far more sense because this is automatically enforced by the database and foreign keys.事实上,如果ENTERED_BYTIMESHEET_FOR成对需要,使用选项 1更有意义,因为这是由数据库和外键自动强制执行的。

Option 1 is a perfect solution.选项 1 是一个完美的解决方案。 You may define foreign key constraint as following您可以定义外键约束如下

1st foreign key constraint for Timesheet_For column Timesheet_For 列的第一个外键约束

ALTER TABLE TIMESHEETTABLE 
ADD CONSTRAINT fk_TimesheetTable_EmployeeTable
FOREIGN KEY (TIMESHEET_FOR)
REFERENCES EMPLOYEETABLE(EMPLOYEE_ID)

2nd foreign key constraint for Entered_By column Entered_By 列的第二个外键约束

ALTER TABLE TIMESHEETTABLE 
ADD CONSTRAINT fk_TimesheetTable_EmployeeTable_1
FOREIGN KEY (ENTERED_BY)
REFERENCES EMPLOYEETABLE(EMPLOYEE_ID)

是的,这没有问题……您可以将另一个表中一个表的主键用作外键两次。

Your query like :您的查询如下:

SELECT t.EMPLOYEE_ID, a.NAME as TimeSheetFor, b.NAME as EnteredBy 
FROM timesheet t
JOIN employee a ON t.timesheet_for =a.employee_id
JOIN employee b ON t.entered_by = b.employee_id

Using this query you will get result as you want.使用此查询,您将获得所需的结果。

我有一个相同的问题,因为当我将更新规则设置为(级联)而不是(没有动作)来删除规则时,我收到一条错误消息实际上是在尝试在itemUnit2ID与表的第二个外键之间建立关系时发生的[项目]和来自表格[units]的对应主键[unitId]

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

相关问题 插入到两个表中,其中一个表具有自动增量主键,并且同一键用作另一表中的外键 - Insert into two tables with one table having an auto increment primary key and that same key is used as a foreign key in another table 在同一个表的主键上创建外键约束的不良做法? - Bad practice to create a foreign key constraint on primary key of the same table? 为什么我的模型中的两个外键导致外键约束? - Why two foreign keys in my model cause Foreign Key Constraint? 两个主键类 - Two primary keys class 在表中使用两个外键指向同一个表的惊人命名 - Surprising naming in table with two foreign keys to the same table ASP.NET Linq to sql多个外键到主键的不同表 - ASP.NET Linq to sql multiple foreign keys to primary key different table 实体框架代码优先设置的外键和同一表中的主键 - Entity Framework code-first set foreign key with primary key in same table 如何使用连接查询从两个以上的表中获取数据,而无需外键和主键 - How to use join query to fetch data from more than two tables without foreign key and primary key 主键和外键的插入 - Primary key and foreign key's insertion 复合主键/外键问题 - Composite Primary Key/Foreign Key Headaches
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM