Question

CREATE TABLE emp_u (
        id      INT NOT NULL AUTO_INCREMENT,
        emailAddress    VARCHAR(100) NOT NULL,
        username        VARCHAR(30) NOT NULL,
        passwd          VARCHAR(40) NOT NULL,
        title           CHAR(4) NOT NULL,
        firstName       VARCHAR(50) NOT NULL,
        surname         VARCHAR(50) NOT NULL,
        PRIMARY KEY (id),
        UNIQUE KEY(emailAddress),
        UNIQUE KEY(username),
        KEY idx_fullname (firstName, surname)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE emp_u_p (
    employer_id_fk  INT NOT NULL DEFAULT 0,
    s_mgt           TINYINT(1) NOT NULL DEFAULT 0,
    add_j_mgt       TINYINT(1) NOT NULL DEFAULT 0,
    edit_j_mgt      TINYINT(1) NOT NULL DEFAULT 0,
    del_j_mgt       TINYINT(1) NOT NULL DEFAULT 0,
    j_mgt           TINYINT(1) NOT NULL default 0,
    cp_mgt  TINYINT(1) NOT NULL default 0,
    ph_mgt      TINYINT(1) NOT NULL default 0,
    cb_mgt      TINYINT(1) NOT NULL default 0,
    search_mgt      TINYINT(1) NOT NULL default 0,
    PRIMARY KEY (employer_id_fk)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

I have these two tables in my database. Will be better to combine these two tables together or leave then as it is.

I'm new to database and just write some tables for my mysql.

Was it helpful?

Solution

If we are advising toward normalization (always!), then I suggest you leave them as two tables, but change the nature of the permissions table to be a one-to-many instead of 1-1 like it appears to be now.

There should be one row for each employee in the employee table, and then one row per permission in the permissions table.

CREATE TABLE emp_u_p (
 emp_u_id INT NOT NULL DEFAULT 0,
 permission_name varchar
);

This is highly advantageous when you need to add permission types to your system; you will not need to refactor your schema. If you want to really go all the way, make a third table called "permissions", and then have a many-to-many, where your employee_permissions table is just two foreign keys: one for employee_id and one for permission_id

To select employees with a given permission:

SELECT emp_u.* 
FROM emp_u JOIN emp_u_p 
  ON emp_u.id = emp_u_p.emp_u_id 
  AND permission_name = "add_j_mgt";

OTHER TIPS

As your tables represent, first table conatin the basic info of employee and second one contains the permission right of that employee...I think, there should be relationship between two table as foreign relationship...you can use ID of employee as a foreign key in second table...It'll maintain the database property.....So, I would recommend you to use two table with foreign relationship......

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top