如何在通过外键链接的两个表中插入数据?
How do I insert data in two tables which are linked by foreign key?
我有两个 table 由外键链接如下。
userdetails
-----------------------------
uid|username|password|address
-----------------------------
phonedetails
-----------------------------
pid|uid|phone1|phone2
-----------------------------
uid
是 phonedetails
table 中的外键。我想在 userdetails
table 中插入 userdetails
并在 phonedetails
table.
中插入相应的 phone 数字
我该怎么做?
您可以制作程序在两个 table 中插入数据。
过程是这样的...
插入用户详细信息数据
SELECT @uid=SCOPE_IDENTITY() ; -- 第二步捕捉
procedure.
userdetail table 新给定的uid
使用@uid 变量插入电话详细信息数据。
此处有更多详细信息SQL - How to INSERT a foreign key as a value for a column
程序:
DECLARE @uid INT
INSERT INTO dbo.userdetail
(Col1, Col2)
VALUES (col1 value, col2 value)
SELECT @uid =
SCOPE_IDENTITY();
INSERT INTO dbo.phonedetails
(Col1,uid,col3) Values(col1 value, @uid,col3 value)
我有两个 table 由外键链接如下。
userdetails
-----------------------------
uid|username|password|address
-----------------------------
phonedetails
-----------------------------
pid|uid|phone1|phone2
-----------------------------
uid
是 phonedetails
table 中的外键。我想在 userdetails
table 中插入 userdetails
并在 phonedetails
table.
我该怎么做?
您可以制作程序在两个 table 中插入数据。
过程是这样的...
插入用户详细信息数据
SELECT @uid=SCOPE_IDENTITY() ; -- 第二步捕捉 procedure.
userdetail table 新给定的uid
使用@uid 变量插入电话详细信息数据。
此处有更多详细信息SQL - How to INSERT a foreign key as a value for a column
程序:
DECLARE @uid INT
INSERT INTO dbo.userdetail
(Col1, Col2)
VALUES (col1 value, col2 value)
SELECT @uid =
SCOPE_IDENTITY();
INSERT INTO dbo.phonedetails
(Col1,uid,col3) Values(col1 value, @uid,col3 value)