0

I have a database table with columns "User","Role"and"Application". My requirement is that a user can only have one role but he can belong to multiple applications. I will want add this restriction in form of a constraint but i am not able to achieve it.

Setting unique constraint on "user" and "role" will not help. Also setting unique constraint on all 3 columns will also not help.

Can anyone suggest on how to set constraint here.

Edit1:

Following is the expected out:

User   Role   Application
U1     R1     A1
U1     R1     A2
U2     R2     A3

and so on... what i want to restrict is that now that U1 has been mapped to R1, U1 should not be mapped to any other Role.

Lokesh
  • 7,810
  • 6
  • 48
  • 78

1 Answers1

0

Then you need to change your table design and normalize it further. If the relationship between USER and ROLE is 1-to-1 then have a lookup table for user_role_mapping and create another table for user_application_mapping.

Ideally your two tables would look like :

Create table user_role_map
   (
    user <datatype>,
    role <datatype>
    constraint user_pk primary key (user, role)
   );

Create table user_application_map
   (
    user         <datatype> NOT NULL,
    applications <datatype> NOT NULL
   );
Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124