更新外键值
我有一个数据库应用程序,其中的组建模如下:
TABLE Group
(
group_id integer primary key,
group_owner_id integer
)
TABLE GroupItem
(
item_id integer primary key,
group_id integer,
group_owner_id integer,
Foreign Key (group_id, group_owner_id) references Group(group_id, group_owner_id)
)
我们设置了一个多字段外键,其中包括 group_owner_id
因为我们希望确保 GroupItem
不能具有与它所在的 Group
的所有者不同。由于其他原因(我认为我不需要详细说明这一点),无法从 GroupItem
中删除 group_owner_id表,所以仅仅删除它不是一个选择。
我的大问题是,如果我想更新整个组的group_owner_id
,我正在编写这样的代码(以伪代码):
...
BeginTransaction();
BreakForeignKeys(group_items);
SetOwnerId(group, new_owner_id);
SaveGroup(group);
SetOwnerId(group_items, new_owner_id);
SetForeignKeys(group_items, group);
SaveGroupItems(group_items);
CommitTransaction()
...
有没有办法解决这个问题?看起来有点笨拙。希望我已经发布了足够的细节。
谢谢。
I have a database application in which a group is modeled like this:
TABLE Group
(
group_id integer primary key,
group_owner_id integer
)
TABLE GroupItem
(
item_id integer primary key,
group_id integer,
group_owner_id integer,
Foreign Key (group_id, group_owner_id) references Group(group_id, group_owner_id)
)
We have a multi field foreign key set up including the group_owner_id
because we want to ensure that a GroupItem
cannot have a different owner than the Group
it is in. For other reasons (I don't think I need to go into detail on this) the group_owner_id cannot be removed from the GroupItem
table, so just removing it is not an option.
My big problem is if i want to update the group_owner_id
for the entire group, I'm writing code like this (in pseudo code):
...
BeginTransaction();
BreakForeignKeys(group_items);
SetOwnerId(group, new_owner_id);
SaveGroup(group);
SetOwnerId(group_items, new_owner_id);
SetForeignKeys(group_items, group);
SaveGroupItems(group_items);
CommitTransaction()
...
Is there a way around doing this? It seems a bit clunky. Hopefully, I've posted enough detail.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SQL Server 不支持 UPDATE CASCADE 吗? :-
然后您只需更新 Group 表的 group_owner_id 即可。
Does SQL Server not support UPDATE CASCADE? :-
Then you simply update the Group table's group_owner_id.
托尼·安德鲁的建议有效。例如,假设您想要将组 1 的所有者从 2 更改为 5。启用 ON UPDATE CASCADE 时,此查询:
将自动更新 GroupItem 中的所有行。
如果您不控制数据库中的索引和键,可以通过首先插入新组,然后修改依赖表中的所有行,最后删除原始组来解决此问题:
顺便说一下,GROUP 是一个 SQL关键字且不能是表名。但我假设你的真实表格有真实姓名,所以这不是问题。
Tony Andrew's suggestion works. For example, say you'd like to change the owner of group 1 from 2 to 5. When ON UPDATE CASCADE is enabled, this query:
will automatically update all rows in GroupItem.
If you don't control the indexes and keys in the database, you can work around this by first inserting the new group, then modifying all rows in the dependant table, and lastly deleting the original group:
By the way, GROUP is a SQL keyword and cannot be a table name. But I assume your real tables have real names so this is not an issue.
您可以尝试添加更新规则来级联更改。
微软链接
外键关系对话框(请参阅更新规则)
使用逻辑记录对相关行的更改进行分组
You might try to add an update rule to cascade changes.
Microsoft Links
Foreign Key Relationships Dialog Box (see Update Rule)
Grouping Changes to Related Rows with Logical Records