给出下表列.active当且仅当UserId是唯一的时,我如何确保只能设置为true?注意,orgId和UserId是多对多关系的复合键,userId也可以是重复的.
OrgId int
UserId int
Active bit
Run Code Online (Sandbox Code Playgroud)
示例1 - 无效,因为userid出现2次,active被设置为true两次.
orgId userId active
1 2 1
2 2 1
Run Code Online (Sandbox Code Playgroud)
示例:2有效.
orgId userId active
1 2 1
2 2 0
3 3 1
Run Code Online (Sandbox Code Playgroud)
你要确保该值最多一个userId拥有active = 1.您可以使用筛选的唯一索引执行此操作:
create unique index idx_table_userId_active
on table(userId)
where active = 1;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39 次 |
| 最近记录: |