Py学习  »  DATABASE

MySQL——表中有很多列——需要建议

The Cool Cat • 3 年前 • 1215 次点击  

我想询问关于我的一个数据库表的建议。下面是 create 我的表名为 io_generated 其中包含 30多列 .我的问题是有这样的列好吗?还是我应该在不同的表中分隔一些数据?我在用VB。net在我的数据库中插入数据,我正在使用OOP。这是个好习惯吗?或者,为了创造一张漂亮的桌子,是否有任何建议?

桌子上有 8 logs ,4次输入和4次输出日志。每根圆木都有 branch_id (branch1…branch8)我还有一列来确定 log is edited (in1_编辑…out4_编辑),还有 Late , Undertime total working hours .都在这张桌子上。

提前谢谢!非常感谢你的帮助。

CREATE TABLE `io_generated` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `employee_id` int(11) unsigned DEFAULT NULL,
  `date` date DEFAULT NULL,
  `branch_id` mediumint(8) unsigned DEFAULT NULL,
  `in1` datetime DEFAULT NULL,
  `out1` datetime DEFAULT NULL,
  `in2` datetime DEFAULT NULL,
  `out2` datetime DEFAULT NULL,
  `in3` datetime DEFAULT NULL,
  `out3` datetime DEFAULT NULL,
  `in4` datetime DEFAULT NULL,
  `out4` datetime DEFAULT NULL,
  `branch1` mediumint(8) unsigned DEFAULT NULL,
  `branch2` mediumint(8) unsigned DEFAULT NULL,
  `branch3` mediumint(8) unsigned DEFAULT NULL,
  `branch4` mediumint(8) unsigned DEFAULT NULL,
  `branch5` mediumint(8) unsigned DEFAULT NULL,
  `branch6` mediumint(8) unsigned DEFAULT NULL,
  `branch7` mediumint(8) unsigned DEFAULT NULL,
  `branch8` mediumint(8) unsigned DEFAULT NULL,
  `in1_edited` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `out1_edited` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `in2_edited` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `out2_edited` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `in3_edited` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `out3_edited` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `in4_edited` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `out4_edited` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `late1` decimal(10,2) NOT NULL DEFAULT '0.00',
  `late2` decimal(10,2) NOT NULL DEFAULT '0.00',
  `late3` decimal(10,2) NOT NULL DEFAULT '0.00',
  `late4` decimal(10,2) NOT NULL DEFAULT '0.00',
  `total_late` decimal(10,2) NOT NULL DEFAULT '0.00',
  `hrs1` decimal(10,2) NOT NULL DEFAULT '0.00',
  `hrs2` decimal(10,2) NOT NULL DEFAULT '0.00',
  `total_whrs` decimal(10,2) NOT NULL DEFAULT '0.00',
  `ot_hrs` decimal(10,2) NOT NULL DEFAULT '0.00',
  `ut1` decimal(10,2) NOT NULL DEFAULT '0.00',
  `ut2` decimal(10,2) NOT NULL DEFAULT '0.00',
  `ut3` decimal(10,2) NOT NULL DEFAULT '0.00',
  `ut4` decimal(10,2) NOT NULL DEFAULT '0.00',
  `total_ut` decimal(10,2) NOT NULL DEFAULT '0.00',
  `day_count` double(10,2) NOT NULL DEFAULT '0.00',
  PRIMARY KEY (`id`),
  UNIQUE KEY `employee_id` (`employee_id`,`id`,`date`,`branch_id`),
  KEY `branch_id` (`branch_id`),
  KEY `branch1` (`branch1`),
  KEY `branch2` (`branch2`),
  KEY `branch3` (`branch3`),
  KEY `branch4` (`branch4`),
  KEY `branch5` (`branch5`),
  KEY `branch6` (`branch6`),
  KEY `branch7` (`branch7`),
  KEY `branch8` (`branch8`),
  CONSTRAINT `io_generated_ibfk_1` FOREIGN KEY (`employee_id`) REFERENCES `employee` (`Employee_ID`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `io_generated_ibfk_10` FOREIGN KEY (`branch8`) REFERENCES `branch` (`Branch_ID`),
  CONSTRAINT `io_generated_ibfk_2` FOREIGN KEY (`branch_id`) REFERENCES `branch` (`Branch_ID`) ON UPDATE CASCADE,
  CONSTRAINT `io_generated_ibfk_3` FOREIGN KEY (`branch1`) REFERENCES `branch` (`Branch_ID`),
  CONSTRAINT `io_generated_ibfk_4` FOREIGN KEY (`branch2`) REFERENCES `branch` (`Branch_ID`),
  CONSTRAINT `io_generated_ibfk_5` FOREIGN KEY (`branch3`) REFERENCES `branch` (`Branch_ID`),
  CONSTRAINT `io_generated_ibfk_6` FOREIGN KEY (`branch4`) REFERENCES `branch` (`Branch_ID`),
  CONSTRAINT `io_generated_ibfk_7` FOREIGN KEY (`branch5`) REFERENCES `branch` (`Branch_ID`),
  CONSTRAINT `io_generated_ibfk_8` FOREIGN KEY (`branch6`) REFERENCES `branch` (`Branch_ID`),
  CONSTRAINT `io_generated_ibfk_9` FOREIGN KEY (`branch7`) REFERENCES `branch` (`Branch_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

In1到Out4与Branch1到BRANCH8以及In1_编辑到Out4_编辑相关。每个输入/输出都有相应的分支和编辑标识符。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128403
 
1215 次点击  
文章 [ 2 ]  |  最新文章 3 年前
Muhammad Mujtaba Shafique
Reply   •   1 楼
Muhammad Mujtaba Shafique    3 年前

你的数据库应该规范化。 尽量减少列数,增加表数。 例如: 为员工、分支机构、时间安排制作单独的表格 使用外键添加数据 这比这更容易理解

Thorsten Kettner
Reply   •   2 楼
Thorsten Kettner    3 年前

不应在表中存储计算值。这违反了数据库规范化,因为您会冗余存储值。示例:如果一个表包含x和y列以及x_+y列,并且它们在表行中的值是10、12和13,那么某个人或进程插入了无效数据,因为10+12=22,而不是13。也许一开始这些值是正确的,但后来其中一个值被更新了,更新人员或流程也不知道他们必须更新依赖列。不管怎样,现在一些查询可能使用x_加y,而另一些查询可能使用x和y计算结果,因此它们给出不同的结果。这绝对不是问题。

解决方法是:不要存储这些值,因为你总是可以临时计算它们。但是,您可以编写视图或将生成的列添加到表中。生成的列仅仅是在查询时或基值更改时完成的计算。例如。

create table io_generated
(
  ...
  total_late decimal(10,2) generated always as (late1 + late2 + late3 + late4) virtual;
  ...
);

(规则的例外:在数据仓库中,我们通常接受冗余。我们通常从数据库中获取数据而不使用冗余,并引入冗余以提高访问速度。)

除此之外,你的桌子看起来还不错。然而,我们无法知道它的设计是否合适,因为我们对您的数据知之甚少。更典型的设计是:

CREATE TABLE io_generated 
(
  io_generated_id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  employee_id INT(11) UNSIGNED NOT NULL,
  date DATE NOT NULL,
  branch_id MEDIUMINT(8) UNSIGNED NOT NULL
);

CREATE TABLE io_detail
(
  io_detail_id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  io_generated_id INT(11) NOT NULL,
  in_datetime DATETIME NOT NULL,
  out_datetime DATETIME NOT NULL,
  in_branch_id MEDIUMINT(8) UNSIGNED NOT NULL,
  out_branch_id MEDIUMINT(8) UNSIGNED NOT NULL,
  in_edited TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
  out_edited TINYINT(1) UNSIGNED NOT NULL DEFAULT 0
);

与你的设计相比,这种设计有优点也有缺点。

  • 例如,在晚上10点之后判断是否有IOs是非常容易的,因为这只是我们必须查看的一个数据库列。另一方面,很难判断第三个IO在晚上10点之后是否有IOs,因为我们必须首先确定哪个细节行是第三个。
  • 这很容易扩展,将来有五台IOs,而不是只有四台。只需添加一行;我们根本不需要改变桌子的设计。另一方面,很难或不可能保证有四个IOs。
  • 很容易计算出涉及到多少不同的in分支( COUNT(DISTINCT in_branch_id) ).
  • 不可能保证所有细节行的日期都与父行的日期匹配。然而,这可以通过从代理关键点切换到自然复合关键点来轻松解决。

我希望这能帮助你了解当决定一个设计或另一个设计时该考虑什么。