简体   繁体   中英

advice MySql tables design

Hey guys i was wondering if there is a better way to go about some mysql tables. 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)

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

  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 :

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

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