简体   繁体   中英

Should I have separate tables or integrate the data?

I am designing an SQL database which has 2 tables that require a 'manager_id'. The 'Employees' table and the 'Facilities' table. Because Managers are considered to be employees, I'm not sure if I should have a separate 'Managers' table or to just integrate it into the 'employees' table. I'm new to SQL and not sure about cases like this. This is the code I have so far:

CREATE TABLE Employees (
emp_id NUMBER(5) NOT NULL,
emp_name VARCHAR2(20) NOT NULL,
emp_add1 VARCHAR2(30) NOT NULL,
emp_add2 VARCHAR2(30) NOT NULL,
emp_add3 VARCHAR2(30),
emp_town VARCHAR2(30),
emp_county NUMBER(2) NOT NULL,
emp_telno NUMBER(10),
emp_position NUMBER(3) NOT NULL,
emp_manager NUMBER(4),
CONSTRAINT pk_empid PRIMARY KEY (emp_id),
CONSTRAINT fk_empcounty FOREIGN KEY (emp_county) REFERENCES County(county_id),
CONSTRAINT fk_empposition FOREIGN KEY (emp_position) REFERENCES Positions(position_id),
CONSTRAINT fk_empmanager FOREIGN KEY (emp_manager) REFERENCES Manager(manager_id)
);

CREATE TABLE Facilities (
facility_id NUMBER(2) NOT NULL,
facility_name VARCHAR(15) NOT NULL,
facility_manager NUMBER(4) NOT NULL,
CONSTRAINT pk_facilityid PRIMARY KEY (facility_id);
CONSTRAINT fk_facilitymanager FOREIGN KEY (facility_manager) REFERENCES Manager(manager_id)
);

This is question about relational normalisation (the organisation of data in a relational database).

So how to organise:

Well though there are many steps in normalisation with the aim to produce the most efficient structure. In your case you should first try the approach of putting the common bits in the same table and the non common bits should be in another.

So since the manger is an employee (lets say with the attributes employee title, name, department) this should be in the employee table. But lets say managers have clipboards (eg attributes colour and size) that non managers don't. In which case the you'd add a Manager table to handle those.

-- employeee table
id  Name      Title      Dept
1   adohertyd Coder      IT
2   Preet     Ninja      SillyWalks
3   Skeety    Secretary  Cleaning

-- manager table 
manager_id  employee_id  clipboard_size  clipboard_colour
1           2            Big             Black

The you'd find a manager like this

select Name, Dept, clipboard_size  
from employee e 
inner join manager m on e.id = m.employee_id

As you go further you may find it more efficient to bring some of those attributes into the employee table and have a 'Is_manager' column. This is essentially denormalisation and something that you should avoid until you need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM