[英]Should I have separate tables or integrate the data?
I am designing an SQL database which has 2 tables that require a 'manager_id'. 我正在设计一个SQL数据库,其中有2个需要'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.
我是SQL的新手,不确定此类情况。 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.
在这种情况下,您将添加一个Manager表来处理这些表。
-- 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. 当您走得更远时,您可能会发现将其中一些属性添加到employee表中并具有“ Is_manager”列会更有效。 This is essentially denormalisation and something that you should avoid until you need it.
这本质上是非规范化,在需要之前应避免使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.