Require help in creating table

  • I am having a main table - Client (ClientID Primary Key)

    Then 2 tables below:

    1. Employee (EmployeeID Primary Key, with ClientID as Foreign Key)

    2. User (UserID Primary Key)

    Now, it is possible that one employee might be working for more than one client. So, the EmployeeID/EmpCode/Paycode (many other data) will be different for a single person per client. Question is: User should only be able to use ONE user to access both client information. In other words, there should only be ONE user created in the system to access clients for whom employee is working.

    Example: "ABC" is a taxi driver (Employee). He is working for "XYZ" and "PQR" (Clients).

    1. Client Table:

    ClientName: XYZ, ClientID: 1, ...

    ClientName: PQR, ClientID: 2, ...

    2. Employee Table:

    EmpName: ABC, EmpCode: 679, ClientID: 1, Shift: Day, ...

    EmpName: ABC, EmpCode: 579, ClientID: 2, Shift: Night, ...

    3. User Table:

    UserID: 100, UserName: ABC_User

    Somehow, I need to normalize the "User" "Employee" table in a way that with only "ABC_User" user login, "ABC" should be able to access data for Client "XYZ" and "PQR". I thought of creating a table "UserAccess" with ClientID, EmployeeID and UserID.

    Let me know your thoughts.

  • first question is

    do employee for example BBB should have access to "XYZ" and "PQR"?

  • Yes it is possible. Thinking of it in this way:

    "BBB" is actually employee (project manager) of client "XYZ", but he is also working as employee (as consultant) for client "PQR".

  • The schema definition seems odd. You might find that things become easier if you change the definition of the Employee table to be something like

    Employee (user_id not null references User(UserID), ClientID not null references Client(ClientID), ...other attributes..., primary key (UserID,ClientID))

    so that the Employee table (which is a table where each row shows the relationship between a client and a user, not a table describing an entity) has the compound primary key required to indicate the client and user for which the row with that primary key describes the relationship. That will eliminate the dummy entity identifier EmployeeID, which you can't use as a primary key despite the statement in your first post that it is the primary key because it's not unique: the data in your post has two rows with the same value for that column, and which is in any case clearly being used to say which user is involved, not which Employee relationship.

    Tom

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply