Not allowing multiple Null values in an Access 2010 multi-column Index

强颜欢笑 提交于 2019-12-12 03:37:35

问题


I'm trying to create a table in Access 2010 which will not allow duplicates in two fields, but will allow nulls in one of those fields providing there is only a single null value (so no duplication of value/null).

My table fields are as below with the ID field set as a Primary Key and the plan is to not allow duplicates in CostCode/TeamID but TeamID can be Null once for each instance of a CostCode.

The picture below shows that I can't add a CostCode and TeamID twice if they both have values, but I can add a CostCode twice with Null values in TeamID.

Is there anyway to achieve this?
I've read I could give TeamID a default value of an empty string (or 0 as that will never be a TeamID) but I'd like to use Null if possible as that is what the empty string or 0 would represent.

EDIT:
After the comment from JJ32 and a weekend to think it through I've gone with putting the TeamID value into a separate table.
I would then have a Many-2-Many join between tbl_BranchDetail and tbl_CostCodes and a Many-2-Many join between tbl_CostCodeM2MJoin and tbl_Teams.

This will remove Null values from occurring in either Many-2-Many table and my query will now read as:

SELECT       M2M.BranchID
            ,M2M.CostCodeID
            ,TM2M.TeamID
            ,CC.CostCode
            ,TM.TeamName
FROM        ((tbl_CostCodes CC INNER JOIN tbl_CostCodeM2MJoin M2M ON CC.ID = M2M.CostCodeID)
                               LEFT JOIN tbl_CostCodeToTeamM2MJoin TM2M ON (M2M.BranchID = TM2M.BranchID AND
                                                                           M2M.CostCodeID = TM2M.CostCodeID))
                               LEFT JOIN tbl_Teams TM ON TM2M.TeamID = TM.ID     

回答1:


I don't believe it is possible to disallow duplicate nulls in a unique composite index since no two Nulls are ever considered equal.

So in your example above you'd have three unique rows, one with a combination of TBC/1 and two with a combination of TBC/null.

The only answer I know of, unfortunately, is to choose some non-null value to represent null in TeamID, and then display the result as empty within the application.



来源:https://stackoverflow.com/questions/39043760/not-allowing-multiple-null-values-in-an-access-2010-multi-column-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!