任务管理的数据库模式

时间:2015-03-03 15:07:21

标签: mysql

我必须管理待办事项列表,并且我正在搜索在MySQL数据库中实现它的最佳方法。

我的待办事项列表如下所示

  • 预运
    • 任务1
    • 任务2
    • 任务3
    • Task4
  • 收据图片和数据
    • 任务1
    • 任务2
  • ...
  • ...
  • 后运
    • 任务1
    • 任务2

我正在考虑像这样构建我的架构:

*************
*** steps ***
*************
id_step 
name_step 

1|Pre-op
2|Receipt images and data
3|...
8|Post-op

*************
*** tasks ***
*************
id_task 
name_task 

1|Task1
2|Task2
3|...
8|Task56

****************
*** todolist ***
****************
id_todolist 
step_id 
task_id
state 

1|1|1|archived
2|1|2|current
3|...
x|8|56|current

我做得对吗?或者有更好的方法可以继续吗?

不同的任务是唯一的。最后,我将至少有两个这样的列表。

我管理外科手术。目前提交了两项专利。因此有两种不同的管理操作方式。将来,我们可以想象其他专利将被提交。

1 个答案:

答案 0 :(得分:1)

也许这有点晚了:D但是......

我假设你想要这样的东西: Todolist er diagram

以下是使用demodata创建此设置以清空数据库的方法:

CREATE TABLE `todolists` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `description` text,
  `order` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `order` (`order`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

INSERT INTO `todolists` (`id`, `name`, `description`, `order`) VALUES
    (1, 'todolist1name', NULL, 1),
    (2, 'todolist2name', NULL, 2);

CREATE TABLE `steps` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `description` text,
  `todolist_id` int(11) NOT NULL,
  `order_in_todolist` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `step_order_in_todolist` (`todolist_id`,`order_in_todolist`),
  CONSTRAINT `FK1: belong to todolist` FOREIGN KEY (`todolist_id`) REFERENCES `todolists` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `steps` (`id`, `name`, `description`, `todolist_id`, `order_in_todolist`) VALUES
    (1, 'step11name', NULL, 1, 1),
    (2, 'step12name', NULL, 1, 2),
    (3, 'step13name', NULL, 1, 3),
    (6, 'step14name', NULL, 1, 4),
    (7, 'step21name', NULL, 2, 1),
    (8, 'step22name', NULL, 2, 2),
    (9, 'step23name', NULL, 2, 3);

CREATE TABLE `states` (
  `name` char(20) NOT NULL,
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `states` (`name`) VALUES
    ('Archived'),
    ('Current');

CREATE TABLE `tasks` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `description` text,
  `state` char(20) NOT NULL,
  `step_id` int(11) NOT NULL,
  `order_in_steplist` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `Unique order in steplist` (`step_id`,`order_in_steplist`),
  KEY `FK1: reference states` (`state`),
  CONSTRAINT `FK1: reference states` FOREIGN KEY (`state`) REFERENCES `states` (`name`),
  CONSTRAINT `FK2: belongs to step` FOREIGN KEY (`step_id`) REFERENCES `steps` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `tasks` (`id`, `name`, `description`, `state`, `step_id`, `order_in_steplist`) VALUES
    (1, 'Task111name', NULL, 'Archived', 1, 1),
    (2, 'Task112name', NULL, 'Current', 1, 2),
    (3, 'Task113name', NULL, 'Current', 1, 3),
    (4, 'Task114name', NULL, 'Current', 1, 4),
    (5, 'Task121name', NULL, 'Current', 2, 1),
    (6, 'Task122name', NULL, 'Current', 2, 2),
    (7, 'Task123name', NULL, 'Current', 2, 3),
    (8, 'Task131name', NULL, 'Current', 3, 1),
    (9, 'Task132name', NULL, 'Current', 3, 2),
    (10, 'Task133name', NULL, 'Current', 3, 3),
    (12, 'Task141name', NULL, 'Current', 6, 1),
    (13, 'Task142name', NULL, 'Current', 6, 2),
    (14, 'Task143name', NULL, 'Current', 6, 3),
    (15, 'Task211name', NULL, 'Archived', 7, 1),
    (16, 'Task212name', NULL, 'Archived', 7, 2),
    (17, 'Task213name', NULL, 'Archived', 7, 3),
    (18, 'Task221name', NULL, 'Archived', 8, 1),
    (19, 'Task222name', NULL, 'Archived', 8, 2),
    (20, 'Task223name', NULL, 'Current', 8, 3),
    (21, 'Task231name', NULL, 'Current', 9, 1),
    (22, 'Task232name', NULL, 'Current', 9, 2),
    (23, 'Task233name', NULL, 'Current', 9, 3);

CREATE VIEW `show_all_list_with_steps_and_tasks` AS SELECT `todolists`.`order` AS `todolist_number`, `todolists`.`name` AS `todolist_name`, `steps`.`order_in_todolist` AS `step_order`, `steps`.`name` AS step_name, `tasks`.`order_in_steplist` AS `task_order`, `tasks`.`name` AS `task_name`, `tasks`.`state`
FROM `todolists` JOIN `steps` ON `todolists`.`id` = `steps`.`todolist_id` JOIN `tasks` ON `steps`.`id` = `tasks`.`step_id` 
ORDER BY `todolist_number`, `step_order`, `task_order` ;