访问:具有 2 个主键字段的可更新连接查询,这两个主键字段也是外键
Access: Updatable join query with 2 primary key fields that are both also foreign keys
在 MS Access 中,我正在尝试实现一个多对多 table,它将存储双向关系,类似于 Association between two entries in SQL table。这个table存储了“A和B是同事”“C和D是朋友”等信息。table是这样的:
构成关系
- LeftId(数字,主键,外键到Constituents.ConstitId)
- RightId(数字,主键,外键到Constituents.ConstitId)
- 描述(文本)
请注意,主键是两个 Id 字段的组合。
table 也有限制:
[LeftId]<>[RightId] AND [LeftId]<[RightId]
table 在我的 Access 项目中工作正常,除了我无法弄清楚如何制作一个我想用作数据表子窗体的 可更新 查询因此用户可以轻松 add/delete 记录和更改描述。我目前有一个非 updatable 查询:
SELECT Constituents.ConstituentId, Constituents.FirstName,
Constituents.MiddleName, Constituents.LastName,
ConstitRelationships.Description, ConstitRelationships.LeftId,
ConstitRelationships.RightId
FROM ConstitRelationships INNER JOIN Constituents ON
(Constituents.ConstituentId =
ConstitRelationships.RightId) OR (Constituents.ConstituentId =
ConstitRelationships.LeftId);
如果我忽略我想要的constituentId在leftId列中的可能性,我可以这样做,是 updatable。所以上面内部连接中的 OR
条件就是搞砸了。
SELECT Constituents.ConstituentId, Constituents.FirstName,
Constituents.MiddleName, Constituents.LastName,
ConstitRelationships.Description, ConstitRelationships.LeftId,
ConstitRelationships.RightId
FROM ConstitRelationships INNER JOIN Constituents ON
(Constituents.ConstituentId =
ConstitRelationships.RightId) ;
我也试过这个古怪的 iif 东西,将两个 LeftId 和 RightId 字段折叠成 FriendId,但它也不可更新。
SELECT Constituents.ConstituentId, Constituents.FirstName,
Constituents.MiddleName,
Constituents.LastName, subQ.Description
FROM Constituents
INNER JOIN (
SELECT Description, Iif([Forms]![Constituents Form]![ConstituentId] <>
ConstitRelationships.LeftId, ConstitRelationships.LeftId,
ConstitRelationships.RightId) AS FriendId
FROM ConstitRelationships
WHERE ([Forms]![Constituents Form]![ConstituentId] =
ConstitRelationships.RightId)
OR ([Forms]![Constituents Form]![ConstituentId] =
ConstitRelationships.LeftId)
) subQ
ON (subQ.FriendId = Constituents.ConstituentId)
;
如何对 ConstitRelationships 进行更新table 查询,包括使用 Constituent.FirstName MiddleName LastName 字段的 JOIN?
恐怕这是不可能的。因为您在超过三个 table 的查询中使用联接,所以它不是 updatable。没有办法解决这个问题。
这里有一些关于该主题的详细信息:http://www.fmsinc.com/Microsoftaccess/query/non-updateable/index.html
如链接文章中所述,一种可能的解决方案也是我认为最适合您的解决方案是临时 table。与简单的 "bind-form-to-a-query" 方法相比,这是一项繁重的工作,但效果最好。
另一种方法是以不需要连接的方式更改数据方案。但是随后非规范化数据和重复数据会横冲直撞,这使得临时 table 成为一个有利的选择。
在 MS Access 中,我正在尝试实现一个多对多 table,它将存储双向关系,类似于 Association between two entries in SQL table。这个table存储了“A和B是同事”“C和D是朋友”等信息。table是这样的:
构成关系
- LeftId(数字,主键,外键到Constituents.ConstitId)
- RightId(数字,主键,外键到Constituents.ConstitId)
- 描述(文本)
请注意,主键是两个 Id 字段的组合。
table 也有限制:
[LeftId]<>[RightId] AND [LeftId]<[RightId]
table 在我的 Access 项目中工作正常,除了我无法弄清楚如何制作一个我想用作数据表子窗体的 可更新 查询因此用户可以轻松 add/delete 记录和更改描述。我目前有一个非 updatable 查询:
SELECT Constituents.ConstituentId, Constituents.FirstName,
Constituents.MiddleName, Constituents.LastName,
ConstitRelationships.Description, ConstitRelationships.LeftId,
ConstitRelationships.RightId
FROM ConstitRelationships INNER JOIN Constituents ON
(Constituents.ConstituentId =
ConstitRelationships.RightId) OR (Constituents.ConstituentId =
ConstitRelationships.LeftId);
如果我忽略我想要的constituentId在leftId列中的可能性,我可以这样做,是 updatable。所以上面内部连接中的 OR
条件就是搞砸了。
SELECT Constituents.ConstituentId, Constituents.FirstName,
Constituents.MiddleName, Constituents.LastName,
ConstitRelationships.Description, ConstitRelationships.LeftId,
ConstitRelationships.RightId
FROM ConstitRelationships INNER JOIN Constituents ON
(Constituents.ConstituentId =
ConstitRelationships.RightId) ;
我也试过这个古怪的 iif 东西,将两个 LeftId 和 RightId 字段折叠成 FriendId,但它也不可更新。
SELECT Constituents.ConstituentId, Constituents.FirstName,
Constituents.MiddleName,
Constituents.LastName, subQ.Description
FROM Constituents
INNER JOIN (
SELECT Description, Iif([Forms]![Constituents Form]![ConstituentId] <>
ConstitRelationships.LeftId, ConstitRelationships.LeftId,
ConstitRelationships.RightId) AS FriendId
FROM ConstitRelationships
WHERE ([Forms]![Constituents Form]![ConstituentId] =
ConstitRelationships.RightId)
OR ([Forms]![Constituents Form]![ConstituentId] =
ConstitRelationships.LeftId)
) subQ
ON (subQ.FriendId = Constituents.ConstituentId)
;
如何对 ConstitRelationships 进行更新table 查询,包括使用 Constituent.FirstName MiddleName LastName 字段的 JOIN?
恐怕这是不可能的。因为您在超过三个 table 的查询中使用联接,所以它不是 updatable。没有办法解决这个问题。
这里有一些关于该主题的详细信息:http://www.fmsinc.com/Microsoftaccess/query/non-updateable/index.html
如链接文章中所述,一种可能的解决方案也是我认为最适合您的解决方案是临时 table。与简单的 "bind-form-to-a-query" 方法相比,这是一项繁重的工作,但效果最好。
另一种方法是以不需要连接的方式更改数据方案。但是随后非规范化数据和重复数据会横冲直撞,这使得临时 table 成为一个有利的选择。