rdbms - RBDMS design for Employees with admin powers in a Company -


we have 2 models, company , employee. 1 company has many employees.

the vast majority of employees not have admin powers, there may 1 or more employees admins powers on company.

what best way of doing in db? can think of following 2 ways, , both have cons , benefits.

1) indicate admin status in employee table though false majority of employees. benefit not having joins determine if employee has admins.

company ------- id  employee -------- id company id admin  

2) create junction table between company , employee, , include employees admin powers. requires join everytime want determine if employee has admin powers.

company ------- id name  employee -------- id company id  adminemployee (is there better name this?) ------------- id company id employee id 

the usual way create roles can associated users. below minimum need:

a company table list companies:

company     id                  unsigned int(p)     name                varchar(255)     ... +----+--------------------+ | id | name               | +----+--------------------+ |  1 | acme brick company | |  2 | abc widgets inc    | | .. | .................. | +----+--------------------+ 

a role table list possible roles:

role     id                  unsigned int(p)     description     varchar(15)  +----+---------------+ | id | description   | +----+---------------+ |  1 | administrator | |  2 | clerk         | | .. | ............. | +----+---------------+ 

a user table hold user information companies:

user     id          unsigned int(p)     company_id  unsigned int(f company.id)     username    varchar(255)     password    varbinary(255)     etc.  +----+------------+----------+----------+-----+ | id | company_id | username | password | ... | +----+------------+----------+----------+-----+ |  1 |          1 | bob      | ******** | ... | |  2 |          2 | mary     | ******** | ... | |  3 |          2 | john     | ******** | ... | | .. | .......... | ........ | ........ | ... | +----+------------+----------+----------+-----+ 

and associative table tie users , roles together. in example data can see user bob administrator @ acme brick company while user mary both administrator , clerk @ abc widgets inc , user john clerk @ abc widgets inc.

user_role     user_id    unsigned int(f user.id)\_(p)     role_id    unsigned int(f role.id)/  +----+---------+---------+ | id | user_id | role_id | +----+---------+---------+ |  1 |       1 |       2 | |  2 |       2 |       1 | |  3 |       2 |       2 | |  4 |       3 |       2 | | .. | ....... | ....... | +----+---------+---------+ 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -