简体   繁体   English

从链接到许多表的另一个表的主键中获取外键字段

[英]Get foreign key field from primary key in another table that linked to many table

My database is designed in SQL Server & I want get output in asp.net, LINQ, C# 我的数据库是在SQL Server中设计的,我想在asp.net,LINQ,C#中获得输出

I have 2 tables linked to 1 table (1:1) 我有2个表链接到1个表(1:1)

My question is can I get a primary key linked to which table? 我的问题是可以获取链接到哪个表的主键吗?

For example: 例如:

  • tbl_Document (ID, Date, ...) tbl_Document(ID,日期等)
  • tbl_Factor (ID, DocID, ...) tbl_Factor(ID,DocID等)
  • tbl_Finance (ID, DocID, ...) tbl_Finance(ID,DocID等)

What is the best way to know ID in tbl_Document linked to which table? 知道链接到哪个表的tbl_Document ID的最佳方法是什么?

I can add record in tbl_Document as 'whichTable' and write the name of table in every column, and every time I want to search set "if" and check 'WhichTable'. 我可以在tbl_Document中将记录添加为“ whichTable”,并在每列中以及每次我要搜索集合“ if”并检查“ WhichTable”时写表的名称。

Is there a better way to do that? 有更好的方法吗?

Thanks, and sorry for my bad English :) 谢谢,对不起我的英语不好:)

By default, you can get only all tables that have foreign key constraints to the parent table: 默认情况下,您只能获取对父表具有外键约束的所有表:

select  object_name(f.referenced_object_id) pk_table, c1.name pk_column_name,
  object_name(f.parent_object_id) fk_table, c2.name fk_column_name
from
sys.foreign_keys f
join sys.columns c1 on c1.object_id = f.referenced_object_id
join sys.columns c2 on c2.object_id = f.parent_object_id 
join sys.foreign_key_columns k 
  on (k.constraint_object_id = f.object_id 
    and c2.column_id = k.parent_column_id
    and c1.column_id = k.referenced_column_id )
where object_name(f.referenced_object_id) ='tbl_Document'

There is no such additional information regarding every particular row in parent table. 没有有关父表中每个特定行的此类附加信息。 It would be a duplicate information (since you can figure out it by searching in every child table). 这将是重复的信息(因为您可以通过在每个子表中进行搜索来找出它)。 Thus, as you mentioned you can store child table name in the additional column and then as an option, construct sql dynamically to query child row. 因此,如前所述,您可以将子表名称存储在附加列中,然后作为一种选择,动态构造sql以查询子行。

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

相关问题 使用ASPNETUSER表电子邮件字段作为另一个表中的主键,即主外键 - Use ASPNETUSER Table Email Field as a primary key in another table, primary foreign key AspNetUsers的主键作为另一个表的外来 - Primary key of AspNetUsers as foreign of another table 从使用外键链接的表中获取数据 - Get data from table that linked using Foreign Key 通过主键从外表获取数据 - Getting Data from foreign table by primary key 如何将一个表中的主(代理)键添加到另一个表的外键中? - How to add a primary (Surrogate) key from one table into a foreign key of another table? 将生成的ID从表1(主键)传递到表2(外键)? - Passing generated id from table 1 (primary key) to table 2 (foreign key)? 是否可以将非主键设置为另一个表中的外键? - Is it possible to set a non primary key as foreign key in another table? 如何用petapoco更新另一个表中的外键主键? - how to update a primary key that is also a foreign key in another table with petapoco? 获取最近添加的记录的主键,并作为外键插入到另一个表中 - get primary key of recently added record and insert into another table as foreign key 将生成的主键作为外键插入控制器的不同表中 - Insert generated primary key as foreign key in different table from controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM