简体   繁体   English

我应该有单独的表格还是整合数据?

[英]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.

相关问题 我应该将实际数据和预测数据分成 2 个表吗? - Should I separate Actual and Forecast data into 2 tables? 我应该将“消息”分成两个表格吗? - Should I separate “message” into 2 tables? 我应该有两个相同的表吗 - should I have 2 identical tables 我应该把一张桌子分成两个单独的桌子吗? - Should I split a table into 2 separate tables? 我应该将数据库图中的哪些字段分成关系表? - Which fields in my database diagram should I separate into relation tables? 我应该为不同的查询类型分别使用SQL帐户吗? - Should I have separate SQL accounts for different query types? 如果您不构建数据仓库,应该如何将维度表与事实表分开? - How should you separate dimension tables from fact tables if you are not building a data warehouse? 我应该在MS Access中使用什么SQL查询来从两个表中获取数据,其中可能没有任何数据 - What SQL query should I use in MS Access to get data from two tables, where either might not have any data in it 两个表之间的关系应该是什么,以便每个学生可以有单独的markid - what should be the relation ship between both the tables so each student can have separate marksid 如果我有主房间和备用房间,它们应该在不同的桌子中吗? - if i have main room and spare room should they be in different tables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM