Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have three tables


USERS:
USERID(1,2), USERNAME(ALI, USER) , PASSWORD(1B23C)


GROUPS:
GROUPID(1,2) , GROUPNAME(ADMINISTRATOR,USER)


ROLES:
USERID(1 ,2), GROUPID(1,2)


So I want to assign a role to a user having username in table user

What will be the query to assign the role to the new user. I am confused. We just insert the username and assign role to that user name. How are the userid and groupid managed in their values ?

Please anyone help me.

Moreover, can I create an alternative of one table having the values username, password and role ? Is there any drawback to this ?

(removed shouting and retarded text speak)
Posted
Updated 15-Aug-11 5:59am
v3
Comments
Smithers-Jones 14-Aug-11 17:34pm    
Why are you shouting?

1 solution

If I understand your question correctly, your table names are confusing, I think this is partially why you haven't had a response. If you can re-name Roles to UserGroups, or better yet rename Roles to UserRoles and Groups to Roles then this will improve the understandibility of your tables, it current looks like you want to be able to assign users to both groups and roles (which is something a lot of people actually want to do).

Before you make the changes, the SQL will be something like:


SQL
INSERT INTO 
  ROLES (UserId,GroupId)
SELECT 
   UserId,
   GroupId
FROM 
  Users,
  Groups
WHERE
  UserName= 'Ali'
AND
  GroupName='Administrator'


You should parameterise this query to avoid SQL injection[^] so 'Ali' and 'Administrators' become parameters.

As for rolling it up into one table: don't. As Christain said in his [deleted] reply it will result in you only being able to assign one role to each user, this is almost certainly not what is needed as users normally have multiple roles.
 
Share this answer
 
Comments
Christian Graus 15-Aug-11 16:53pm    
Yes, it seems that I am not allowed to tell people to stop asking the same thing over and over, despite the fact that I have explained all of this to him at length, every time he has asked the same question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900