简体   繁体   English

在MySQL的一个表中存储100多个列的最佳方法

[英]Best approach to store 100+ columns in one table in MySQL

I am working on a data model where I need to store Employee's basic details and his rating of skillsets in MySQL database. 我正在一个数据模型上,我需要在mysql数据库中存储Employee的基本详细信息和他对技能的评价。

The number of skillsets for each employee is more than 100. So the information I need to store is as following: Employee ID, Name , Department , Contact info, Skillset1,Skillset2,Skillset3, ... , Skillset115 每个员工的技能组数量超过100。因此,我需要存储的信息如下:员工ID,姓名,部门,联系信息,Skillset1,Skillset2,Skillset3,...,Skillset115

Is creating one table with approximately 120 columns is good approach? 创建一个约有120列的表是一种好方法吗? If not, what is the best practice to deal with this kind of requirement. 如果没有,处理这种要求的最佳实践是什么。

No. You should have a separate table with one row per employee and per skill: 否。您应该有一个单独的表格,每个员工和每个技能一行:

create table employeeSkills (
    employeeSkillId int auto_increment primary key,
    employeeId int not null,
    skill varchar(255),
    constraint fk_employeeSkills_employeeid foreign key (employeeId) references employees(employeeId)
);

In fact, you should really have two extra tables. 实际上,您实际上应该有两个额外的表。 The skills themselves should be stored in a separate table and the above should really be: 技能本身应存储在单独的表中,以上内容实际上应该是:

create table employeeSkills (
    employeeSkillId int auto_increment primary key,
    employeeId int not null,
    skillId int,
    constraint fk_employeeSkills_employeeid foreign key (employeeId) references employees(employeeId),
    constraint fk_employeeSkills_skillid foreign key (skillId) references skills(skillId)
);

This type of table is called a "junction table", and is common in any properly constructed data model. 这种类型的表称为“连接表”,在任何适当构造的数据模型中都很常见。

You need to create two tables that would handle the skills and the assigned skill for each employee. 您需要创建两个表来处理每个员工的技能和分配的技能。

This would give you a proper order in your database and also will extend your options in the future. 这将为您提供适当的数据库顺序,并在将来扩展您的选择范围。 It'll be better in search, add and assign skills to each employee. 搜索,添加和分配技能给每个员工会更好。 It's even more organized and would be able to be expanded easily such as adding skills category and sub-category. 它甚至更有组织,并且可以轻松扩展,例如添加技能类别和子类别。

The two tables schema should be something like this : 这两个表的架构应该是这样的:

CREATE TABLE Skills (
    Skill_ID            INT NOT NULL AUTO_INCREMENT, 
    Skill_Description   VARCHAR(250), 
    PRIMARY KEY (`Skill_ID`)
); 

CREATE TABLE EmpolyeeSkills (
    ES_ID       INT NOT NULL AUTO_INCREMENT,
    Skill_ID    INT, 
    Employee_ID INT,
    PRIMARY KEY (`ES_ID`),
    CONSTRAINT FK_EMPLOYEEID FOREIGN KEY (Employee_ID) REFERENCES Employees(Employee_ID),
    CONSTRAINT FK_SKILLID FOREIGN KEY (Skill_ID) REFERENCES Skills(Skill_ID)
); 

The Skills table will assign an ID for each skill, and it'll be in a separate table. 技能表将为每个技能分配一个ID,该ID将在单独的表中。 This will make you have a unique skills list, there won't be any redundancy. 这将使您拥有独特的技能列表,不会有任何冗余。 Then, you'll use EmployeeSkills to save the assigned skills on each Employee_ID. 然后,您将使用EmployeeSkills在每个Employee_ID上保存分配的技能。 Which you can use it later on to join it with other records. 以后可以使用它与其他记录联接。

The FOREIGN KEY on Employee_ID and Skill_ID will help you in monitoring the skills between them. Employee_ID和Skill_ID上的FOREIGN KEY将帮助您监视它们之间的技能。 The ES_ID primary key for EmpolyeeSkills will be an additional advantage that can be helpful in the future. EmpolyeeSkills的ES_ID主键将是一个额外的优势,将来可能会有所帮助。 For instance, if you want to know the latest skill that has been assigned, then your faster approach will be getting the last ES_ID as it's an AUTO_INCREMENT. 例如,如果您想知道已分配的最新技能,则更快的方法是获取最后的ES_ID,因为它是AUTO_INCREMENT。 This is just one advantage from tons of others. 这只是其他众多优点中的一种。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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