简体   繁体   English

不同表的两个主键作为另一个表的外键

[英]Two Primary keys of different tables as foreign keys to another table

I have a winForms application with DataGridView, 3 tables and some queries. 我有一个带有DataGridView,3个表和一些查询的winForms应用程序。

Lets say I have Tables , A,B,C. 可以说我有表格A,B,C。 In table AI have an auto increment primary Key and also in table B. Tables A and B are both strangers. 在表AI中有一个自动递增主键,在表B中也有。表A和B都是陌生人。

Table C is a Join Table that have 2 columns: 表C是具有两列的联接表:

1) A primary Key 1)主键

2) B primary Key 2)B主键

Now, in the form I have a button and it's just programmatically inserts values for table A and B. What I'm trying to do is to insert the primary keys of tables A and B to the right columns in table C. is it possible to do it programmatically? 现在,在表单中,我有一个按钮,它只是以编程方式插入表A和B的值。我想做的是将表A和B的主键插入表C的右列。以编程方式进行?

if you using sql server you can run in your buttonclick code this: 如果使用sql server,则可以在buttonclick中运行以下代码:

declare @aid int, @bid int 
insert A(col1) values(val1) 
set @aid = scope_identity()

insert B(col1) values(val1) 
set @bid = scope_identity()

insert C(aid, bid) values(@aid, @bid)

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

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