简体   繁体   English

咨询MySql表设计

[英]advice MySql tables design

Hey guys i was wondering if there is a better way to go about some mysql tables. 大家好,我想知道是否有更好的方法来处理一些mysql表。 I have to evaluate (in a php app) how a task was completed based on 9 attributes The problem is that once a year the columns might change (the naming not the number of evaluation fields) 我必须评估(在php应用中)如何基于9个属性完成任务问题是每年一次列可能会更改(命名不是评估字段的数目)

CREATE TABLE IF NOT EXISTS `grades` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `score_id` int(10) NOT NULL,
  `task_number` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
  `assigned_to` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `location` varchar(3) COLLATE utf8mb4_unicode_ci NOT NULL,
  `completed_at` date NOT NULL,
  `priority` tinyint(1) NOT NULL DEFAULT '0',
  `worknotes` tinyint(1) NOT NULL DEFAULT '0',
  `client_com` tinyint(1) NOT NULL DEFAULT '0',
  `closure_info` tinyint(1) NOT NULL DEFAULT '0',
  `status` tinyint(1) NOT NULL DEFAULT '0',
  `category` tinyint(1) NOT NULL DEFAULT '0',
  `ci` tinyint(1) NOT NULL DEFAULT '0',
  `timecard` tinyint(1) NOT NULL DEFAULT '0',
  `resolution_time` tinyint(1) NOT NULL DEFAULT '0',
  `validated` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT
CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
COMMIT;

So my first thought is to create another table with the attributes name and have 2 tables like this 所以我首先想到的是用属性名称创建另一个表,并有2个这样的表

  CREATE TABLE IF NOT EXISTS `grades` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `score_id` int(10) NOT NULL,
  `task_number` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
  `assigned_to` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `location` varchar(3) COLLATE utf8mb4_unicode_ci NOT NULL,
  `completed_at` date NOT NULL,
  `grade1` tinyint(1) NOT NULL DEFAULT '0',
  `grade2` tinyint(1) NOT NULL DEFAULT '0',
  `grade3` tinyint(1) NOT NULL DEFAULT '0',
  `grade4` tinyint(1) NOT NULL DEFAULT '0',
  `grade5` tinyint(1) NOT NULL DEFAULT '0',
  `grade6` tinyint(1) NOT NULL DEFAULT '0',
  `grade7` tinyint(1) NOT NULL DEFAULT '0',
  `grade8` tinyint(1) NOT NULL DEFAULT '0',
  `grade9` tinyint(1) NOT NULL DEFAULT '0',
  `validated` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
COMMIT;

and

   CREATE TABLE IF NOT EXISTS `evaluation_fields` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` varchar(40) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
COMMIT;

INSERT INTO `evaluation_fields` (`id`, `name`) VALUES
(1, 'Priority'),
(2, 'Worknotes'),
(3, 'Client Com'),
(4, 'Closure information'),
(5, 'Status management'),
(6, 'Category management'),
(7, 'CI identification', ),
(8, 'Timecard management'),
(9, 'Resolution Time');
COMMIT;

How would you recommend going about this? 您会如何建议呢? Thx in advance for your time. 在您的时间提前。 Hope i am making some kind of sense 希望我有某种意义

我认为您需要3个表,一个用于任务及其属性,另一个用于评估字段,其中包含此评估的名称和任何其他属性,并且此表与任务之间的关系将会很多,这意味着第三个表基本上应具有任务的ID,评估字段的ID和所需的其他属性

You don't want to change the structure every year or so. 您不想每年左右更改结构。 You need to define a design that is flexible enough to fit your use case. 您需要定义一个足够灵活以适合您的用例的设计。

I suggest that you should be able to manage this with 3 tables : 我建议您应该可以使用3个表来管理它:

attributes contains all the attributes that can be used to evaluate a task. 属性包含可用于评估任务的所有属性。 It is basically a referential table, whose content is modified only when new evaluation attributes need to be created (so about once a year). 它基本上是一个参照表,仅当需要创建新的评估属性时(大约每年一次),才修改其内容。 Like : 喜欢 :

id        - primary key
name  - name of the evaluation attribute 

grades is meant to store the evaluation results. 成绩旨在存储评估结果。 There is one row for each evaluation, with a few fields containing the master data of the evaluation, like : 每次评估都有一行,其中一些字段包含评估的主数据,例如:

id     - primary key
score_id
task_number
assigned_to
location 
completed_at
priority 

evaluations is meant to store the details of each evaluation. 评估旨在存储每个评估的详细信息。 It has one line for each attribute that was used for each evaluation, and refers to both above tables using foreign keys, like : 对于每个评估所使用的每个属性,它只有一行,并且使用外键来引用上述两个表,例如:

id                    - primary key 
grade_id         - foreign key to column id in table grades
evaluation_id  - foreign key to column id in table attributes

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

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