我有其中一组是仿照这样的数据库应用程序:更新外键值
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
不能有与其所有者不同。由于其他原因(我认为我不需要详细说明),无法从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()
...
有没有办法解决这样做呢?看起来有点笨重。希望我已经发布了足够的细节。
谢谢。
+1虽然Group是保留关键字,但不能成为表名 – Andomar 2009-11-03 23:49:34