[英]Insert a value in sql using foreign key
I have two tables employee_table
and salary_table
. 我有两个表
employee_table
和salary_table
。
employee_table
has emp_id
(primary key and identity), emp_name
and designation
columns, while salary_table
has emp_id
(foreign key) and salary
. employee_table
具有emp_id
(主键和同一性), emp_name
和designation
列,同时salary_table
具有emp_id
(外键)和salary
。
In my C# Windows Forms application, I am inserting emp_name
, designation
and salary
. 在我的C#Windows窗体应用程序,我将
emp_name
, designation
和salary
。
While inserting these values, emp_id
will be auto-generated. 插入这些值时,将自动生成
emp_id
。
I want to pass the emp_id
value to salary
table as the foreign key automatically. 我想将
emp_id
值作为外键自动传递给salary
表。
What database and which driver are you using? 您正在使用哪个数据库和哪个驱动程序? Each different database has a different way of getting the identity column back through your insert statement.
每个不同的数据库都有不同的方式来通过您的插入语句获取标识列。 For instance for Microsoft SQL Server you do:
例如,对于Microsoft SQL Server,您可以执行以下操作:
INSERT INTO Table(Columns...)
VALUES (VALUES...);SELECT SCOPE_IDENTITY();
This will return the auto-number column value back for you to be able to use in your child table insert statements as the foreign key values. 这将返回自动编号列值,以供您在子表插入语句中用作外键值。
Oracle has a different way of doing that. Oracle有不同的方法。 Let us know what database you are using so we can help you...
让我们知道您正在使用什么数据库,以便我们为您提供帮助...
Enjoy! 请享用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.