SQL Server Management Studio 更新级联

发布于 2025-01-05 16:35:25 字数 429 浏览 1 评论 0原文

我正在开发一个简单的数据库,我希望在其中建立一对多的关系,根据我对 的理解,我创建了一个外键,该外键位于 ON UPDATE CASCADE 上在更新级联时,如果我的主表增加,引用它的其他表中的外键也将增加/具有其他表中主表的值。但问题是,当我在父表中插入数据时,另一个表中的外键没有获取我父表上主表的值,为什么会这样?我的外键如何具有与其相关的键的值?这是一个屏幕截图,供大家轻松理解 在此处输入图像描述

正如您所看到的 P_ID订单表中的值没有值,即使它链接到人员表中的 P_ID

预期结果将是订单表中 P_ID 的值将相当于people 表的 P_ID,我在其中插入了数据。

I am working on a simple database where I would want to have a one is to many relationship, I've created a foreign key which has on an ON UPDATE CASCADE, based from my understanding of ON UPDATE CASCADE, if my Primary gets incremented the Foreign key in the other table that references it will also get incremented/ have the value of the Primary in the other table . but the problem is as I insert data in my Parent table, the foreign Key in the other table did not get the value of the Primary on my parent table why is it like that? How would my foreign key have the value of the key that is related to it? here's a screenshot for you guys to easiy understand enter image description here

As you can see the P_ID in the orders table doesn't have a value even though it is linked to the P_ID in the People table

The Expected Result will be that in the Orders table the value of P_ID would be equivalent to P_ID of people table, where I inserted my data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的憧憬 2025-01-12 16:35:25

但问题是当我在父表中插入数据时。

您不会在插入时执行任何操作,只会在更新时执行任何操作。

如果您有 1 号人员,并且有一个给 1 号人员的订单,如果您将其更改为 2 号,则订单将更改为 2。

您可能需要一个触发器。告诉我们更多预期结果是什么

编辑:

触发示例:

create trigger InsertRecordOnOrder
on People
after insert
as
begin
    insert into Orders
    select * from INSERTED
end

but the problem is as I insert data in my Parent table.

You wont get any action on insert, only on update.

If you have people number 1 and an order to people number one, if you change it to number 2, the order will be changed to 2.

You may want a trigger. tell us more what's the expected result

EDIT:

trigger example:

create trigger InsertRecordOnOrder
on People
after insert
as
begin
    insert into Orders
    select * from INSERTED
end
丶情人眼里出诗心の 2025-01-12 16:35:25

我认为,您误解了更新级联。请参考以下链接。

http://msdn.microsoft.com/en -us/library/aa933119%28v=sql.80%29.aspx

I think, you misunderstand ON UPDATE CASCADE. Please reference the following link.

http://msdn.microsoft.com/en-us/library/aa933119%28v=sql.80%29.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文