I have userid and role id fields in the table. In that table couple userid’s has two roles. But it shouldn’t.
example:
Userid(FK) RoleID(FK)
1 1E36A840-2EBB-44EC-8861-0E3D262AC676
1 0B54F223-E0D4-4CFC-84C3-7C98C1BFC6DA
2 1E36A840-2EBB-44EC-8861-0E3D262AC676
3 0B54F223-E0D4-4CFC-84C3-7C98C1BFC6DA
4 1E36A840-2EBB-44EC-8861-0E3D262AC676
4 0B54F223-E0D4-4CFC-84C3-7C98C1BFC6DA
In this case I have to delete 1 and 4 where ‘1E36A840-2EBB-44EC-8861-0E3D262AC676’ which has ‘0B54F223-E0D4-4CFC-84C3-7C98C1BFC6DA’
You can prevent this from happening in the first place by putting a unique constraint on the
Useridcolumn. However, before doing this you will need to remove existing duplicates. Given the information we have so far (aUseridand aRoleid) there’s really no way to “correctly” do this. As @Joe Stefanelli mentioned in his comment, you’d need to decide which role is the “correct” one for each user. That sounds like a manual process. (Unless you know ahead of time that only the first one should be kept in each case, in which case @Thomas has a good answer.)Out of curiosity, is there a specific reason why a user can’t (or shouldn’t, in this case) have multiple roles? Usually that’s what roles are for, applying multiple permission sets, etc. to various users in a many-to-many fashion. Think of it in terms of object-oriented design. Roles are interfaces (types) and users are classes (implementations). By having multiple roles, each user can be polymorphic into multiple user types throughout the system.