How to design a user/role schema in a SQL Server database?

Not quite sure what you mean, but...

  • User_Roles should have 2 columns only User_id and Role_id
    Both of these form the Primary Key
  • You do not need an extra id column User_Roles
  • User_id is a foreign key to Users.id
  • Role_id is a foreign key to Roles.id

Edit: Now I understand. Yes, always use foreign keys

Also...

  • if password is nvarchar(50), this implies plain text. This is bad.
  • if you have duplicate name values in Users, how do you know which user is which?
    Especially if they have the same password (which will happen because we meatsacks are stupid)

Edit after comment after primary key creation...

CREATE TABLE [dbo].[User_Roles]
(
    [User_id] [int] NOT NULL,
    [Role_id] [int] NOT NULL,

    CONSTRAINT [PK_User_Roles] PRIMARY KEY CLUSTERED ([User_id], [Role_id]),
    CONSTRAINT [UQ_ReversePK] UNIQUE ([Role_id], [User_id])
)

Tags:

Sql Server