For the entities and attributes you've described so far and assuming I've understood the domain, I can't see a need for anything more than the tables below. If you there are attributes specific to a Fitter or Admin, add distinct tables for those attributes.
CREATE TABLE UserType
(
UserTypeId INT
, UserType NVARCHAR(50)
, CONSTRAINT PK_UserType PRIMARY KEY (UserTypeId)
)
CREATE TABLE Address
(
AddressId INT
, Line1 NVARCHAR(50)
, Line2 NVARCHAR(50)
, Line3 NVARCHAR(50)
, Line4 NVARCHAR(50)
, Line5 NVARCHAR(50)
, Postcode NVARCHAR(20)
, Country NVARCHAR(50)
, CONSTRAINT PK_Address PRIMARY KEY (AddressId)
)
CREATE TABLE [User]
(
UserId INT
, UserTypeId INT
, UserName NVARCHAR(50)
, Password NVARCHAR(50)
, Title NVARCHAR(50)
, Forename NVARCHAR(50)
, Surname NVARCHAR(50)
, CONSTRAINT PK_User PRIMARY KEY (UserId)
, CONSTRAINT FK_User_UserType FOREIGN KEY (UserTypeId) REFERENCES UserType(UserTypeId)
)
CREATE TABLE Client
(
ClientId INT
, Name NVARCHAR(50)
, AddressId INT
, CONSTRAINT PK_Client PRIMARY KEY (ClientId)
, CONSTRAINT FK_Client_Address FOREIGN KEY (AddressId) REFERENCES Address(AddressId)
)
CREATE TABLE ClientUser
(
ClientId INT
, UserId INT
, CONSTRAINT PK_ClientUser PRIMARY KEY (ClientId, UserId)
, CONSTRAINT FK_ClientUser_Client FOREIGN KEY (ClientId) REFERENCES Client(ClientId)
, CONSTRAINT FK_ClientUser_User FOREIGN KEY (UserId) REFERENCES [User](UserId)
)
The simplest structure would be a PERMISSIONS
table like this:
create table PERMISSIONS
( Site_ID int
, User_ID int
, Role char(1)
, primary key (Site_ID, User_ID, Role) -- scenario 1
-- OR:
, primary key (Site_ID, User_ID) -- scenario 2
)
Use values for Role
such as:
- A = Administrator
- P = Publisher
- E = Editor
How many rows you need depends on how your code interprets the data. You could have very simple code which interprets each permission strictly at face value. To do an administrative task, the user would need an 'A' permission. To do a publishing task, they would need a 'P' permission, etc. In this case an administrator would need three records per site (one each of A, P, E). Similarly publishers would need two records per site and editors only one. There is a risk in this approach that data consistency errors could creep in, like someone could be a publisher, but not an editor (by virtue of the 'E' record being missing).
Alternatively, you could make your code more complex, in which case each user only needs one permission record per site. In this scenario, administrative tasks require an 'A' permission, publishing requires an 'A' or a 'P' and editing requires 'A', 'P', or 'E'. The advantage of this approach is that there is less data to maintain and you won't have inconsistencies.
Note too that you do not need to have the Users.Parent
column. If an administrator has an 'A' permission for a site, that automatically gives them control of all of the users for that site.
Best Answer
The first path is certainly the more conventional of the two, absent a compelling reason to break the system down into multiple schemas. Something along these lines: