11条重要的数据库设计规则
欢迎进入Windows社区论坛,与300万技术人员互动交流 >>进入 在你开始阅读这篇文章之前,我(指原文作者)得明确地告诉你,我并不是一个数据库设计领域的大师。以下列出的11点是我从自己在平时项目实践和阅读中学习到的经验总结出来的个人见解。我个人认为它
欢迎进入Windows社区论坛,与300万技术人员互动交流 >>进入
在你开始阅读这篇文章之前,我(指原文作者)得明确地告诉你,我并不是一个数据库设计领域的大师。以下列出的11点是我从自己在平时项目实践和阅读中学习到的经验总结出来的个人见解。我个人认为它们对我的数据库设计提供了很大的帮助。
我之所以写下这篇长文是因为,很多开发者一参与到数据库设计,就会很自然地把“三范式”当作银弹一样来使用。他们往往认为遵循这个规范就是数据库设计的唯一标准。由于这种心态,他们往往尽管一路碰壁也会坚持把项目做下去。
如果你对 “三范式” 不清楚,请点击这里一步一步的了解什么是“三范式”。
大家都说标准规范是重要的指导方针,并且也都这么做,但是死记硬背还是会带来麻烦的。以下11点是我在数据库设计时会优先考虑的规则。
规则1:弄清楚将要开发的应用程序是什么性质的(OLTP 还是 OLAP)?
当你要开始设计一个数据库的时候,你应该首先要分析出你为之设计的应用程序是什么类型的,它是“事务处理型”(Transactional)的还是 “分析型” (Analytical)的?你会发现许多开发人员采用标准化做法去设计数据库,而不考虑目标程序是什么类型的。采用这种做法设计的数据库很快就会陷入性能、客户定制化的问题当中。正如前面所说的,这里有两种应用程序类型,“基于事务处理” 和 “基于分析”,下面让我们来了解一下这两种类型究竟说的是什么意思:
事务处理型:对于这种类型的应用程序,你的用户更关注数据的增查改删(CRUD,Creating/Reading/Updating/Deleting)。这种类型官方称之为 “OLTP”。
分析型:对于这种类型的应用程序,你的用户更关注数据分析、报表、趋势预测等功能。这一类的数据库的“插入” 和“更新”操作相对来说是比较少的。用户的主要目的是更加快速地查询、分析数据。这种类型官方称之为 “OLAP”。
那么换句话说,如果你认为插入、更新、删除数据这些操作在你的程序中更为突出的话,那就设计一个规范化的表,否则的话就去创建一个扁平的、不规范化的数据库结构。
以下这个简单的图表显示了像左边 Names 和 Address这样的简单规范化的表,怎么通过应用不规范化结构来创建一个扁平的表结构。
规则2:将你的数据按照逻辑意义分成不同的块,让事情做起来更简单
这个规则其实就是 “三范式”中的第一范式。违反这条规则的一个标志就是:你的查询使用了很多字符串解析函数,例如 substring、charindex 等等。若真如此,那就需要应用这条规则了。
比如你看到的下面图片中有一个有学生名字的表,如果你想要查询学生名字中包含“Koirala”,但不包含“Harisingh”的记录,你可以想象一下你将会得到什么样的结果。
所以更好的做法是将这个字段拆分为更深层次的逻辑分块,以便我们的表数据写起来更干净,以及优化查询。
规则3:不要过度使用“规则 2”
开发者都是一个很可爱的群体。如果你告诉他们这是一条解决问题的正路,他们就会一直这么做下去,做到过了头导致产生一些不必要的后果。这也可以应用于我们刚刚在前面提到的规则2.当你考虑字段分解时,先暂停一下,并且问问你自己是否真的需要这么做。正如前面所说的,分解应该是要符合逻辑的。
例如,你可以看到电话号码这个字段,你很少会把电话号码的 ISD 代码单独分开来操作(除非你的应用程序要求这么做)。所以一个很明智的决定就是让它保持原样,否则这会带来更多的问题。
规则4:把重复、不统一的数据当成你最大的敌人来对待
集中那些重复的数据然后重构它们。我个人更加担心的是这些重复数据带来的混乱而不是它们占用了多少磁盘空间。
例如下面这个图表,你可以看到 "5th Standard" 和 "Fifth standard" 是一样的意思,它们是重复数据。现在你可能会说是由于那些录入者录入了这些重复的数据或者是差劲的验证程序没有拦住,让这些重复的数据进入到了你的系统。 现在,如果你想导出一份将原本在用户眼里十分困惑的数据显示为不同实体数据的报告,该怎么做呢?
解决方法之一是将这些数据完整地移到另外一个主表,然后通过外键引用过来。在下面这个图表中你可以看到我们是如何创建一个名为Standards 的主表,然后同样地使用简单的外键连接过去。
规则5:当心被分隔符分割的数据,它们违反了“字段不可再分”规则
前面的规则 2 即“第一范式”说的是避免“重复组”。下面这个图表作为其中的一个例子解释了“重复组”是什么样子的。如果你仔细的观察 Syllabus 这个字段,会发现在这一个字段里实在是填充了太多的数据了。像这些字段就被称为“重复组”了。如果我们又得必须使用这些数据,那么这些查询将会十分复杂并且我也怀疑这些查询会有性能问题。
这些被塞满了分隔符的数据列需要特别注意。一个较好的办法是将这些字段移到另外一个表中,使用外键连接过去,以便于更好的管理。
那么,让我们现在就应用规则2(第一范式)“避免重复组” 吧。你可以看到上面这个图表,我创建了一个单独的 Syllabus表,然后使用“多对多” 关系将它与 Subject表关联起来。
通过这个方法,主表(Student 表)的 Syllabus字段就不再有重复数据和分隔符了。
规则6:当心那些仅仅部分依赖主键的列
留心注意那些仅仅部分依赖主键的列。例如上面这个图表,我们可以看到这个表的主键是 Roll No.+Standard.现在请仔细观察 Syllabus 字段,可以看到 Syllabus字段仅仅关联Standard字段而不是直接地关联某个学生Roll No.字段。
Syllabus 字段关联的是学生正在学习的哪个课程级别字段而不是直接关联到学生本身。那如果明天我们要更新教学大纲(课程)的话还要痛苦地为每个同学也修改一下,这明显是不符合逻辑的(也是不正常的做法)。更有意义的做法是将这些字段从这个表移到另外一个表,然后将它们与 Standard表关联起来。
你可以看到我们是如何移动 Syllabus 字段并且同样地附上 Standard 表。
这条规则只不过是 “三范式” 里的 “第二范式”:“所有字段都必须完整地依赖主键而不是部分依赖”。
规则7:仔细地选择派生列
如果你正在开发一个 OLTP型的应用程序,那强制不去使用派生字段会是一个很好的思路,除非有迫切的性能要求,比如经常需要求和、计算的 OLAP 程序,为了性能,这些派生字段就有必要存在了。
通过上面的这个图表,你可以看到 Average 字段是如何依赖 Marks 和 Subjects 字段的。这也是冗余的一种形式。因此对于这样的由其他字段得到的字段,需要思考一下它们是否真的有必要存在。
这个规则也被称为 “三范式” 里的第三条:“不应该有依赖于非主键的列” . 我的个人看法是不要盲目地运用这条规则,应该要看实际情况,冗余数据并不总是坏的。如果冗余数据是计算出来的,看看实际情况再来决定是否应用这第三范式。
规则8:如果性能是关键,不要固执地去避免冗余
不要把“避免冗余”当作是一条绝对的规则去遵循。如果对性能有迫切的需求,考虑一下打破常规。常规情况下你需要做多个表的连接操作,而在非常规的情况下这样的多表连接是会大大地降低性能的。
规则9:多维数据是各种不同数据的聚合
OLAP 项目主要是解决多维数据问题。比如你可以看看下面这个图表,你会想拿到每个国家、每个顾客、每段时期的销售额情况。简单的说你正在看的销售额数据包含了三个维度的交叉。
为这种情况做一个实际的设计是一个更好的办法。简单的说,你可以创建一个简单的主要销售表,它包含了销售额字段,通过外键将其他所有不同维度的表连接起来。
规则10:将那些键/值表统一起来设计
很多次我都遇到过这种键/值表。键/值表意味着它有一些键,这些键被其他数据关联着。比如下面这个图表,你可以看到我们有 Currency和 Country这两张表。如果你仔细观察你会发现实际上这些表都只有键和值。
对于这种表,创建一个主要的表,通过一个 Type字段来区分不同的数据将会更有意义。
规则11:无限分级结构的数据,引用自己的主键作为外键
我们会经常碰到一些无限分级结构的数据。例如考虑一个多级销售方案的情况,一个销售人员之下可以有多个销售人员。注意到都是“销售人员”。也就是说数据本身就是一个层级。但是层级不同。这时候我们可以引用自己的主键作为外键来表达这种层级关系,从而达成目的。
这篇文章的用意不是叫大家不要遵循范式,而是叫大家不要盲目地遵循范式。根据你的项目性质和需要处理的数据类型来做出正确的选择。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

According to news on April 17, HMD teamed up with the well-known beer brand Heineken and the creative company Bodega to launch a unique flip phone - The Boring Phone. This phone is not only full of innovation in design, but also returns to nature in terms of functionality, aiming to lead people back to real interpersonal interactions and enjoy the pure time of drinking with friends. Boring mobile phone adopts a unique transparent flip design, showing a simple yet elegant aesthetic. It is equipped with a 2.8-inch QVGA display inside and a 1.77-inch display outside, providing users with a basic visual interaction experience. In terms of photography, although it is only equipped with a 30-megapixel camera, it is enough to handle simple daily tasks.

According to news on April 26, ZTE’s 5G portable Wi-Fi U50S is now officially on sale, starting at 899 yuan. In terms of appearance design, ZTE U50S Portable Wi-Fi is simple and stylish, easy to hold and pack. Its size is 159/73/18mm and is easy to carry, allowing you to enjoy 5G high-speed network anytime and anywhere, achieving an unimpeded mobile office and entertainment experience. ZTE 5G portable Wi-Fi U50S supports the advanced Wi-Fi 6 protocol with a peak rate of up to 1800Mbps. It relies on the Snapdragon X55 high-performance 5G platform to provide users with an extremely fast network experience. Not only does it support the 5G dual-mode SA+NSA network environment and Sub-6GHz frequency band, the measured network speed can even reach an astonishing 500Mbps, which is easily satisfactory.

According to news on July 12, the Honor Magic V3 series was officially released today, equipped with the new Honor Vision Soothing Oasis eye protection screen. While the screen itself has high specifications and high quality, it also pioneered the introduction of AI active eye protection technology. It is reported that the traditional way to alleviate myopia is "myopia glasses". The power of myopia glasses is evenly distributed to ensure that the central area of sight is imaged on the retina, but the peripheral area is imaged behind the retina. The retina senses that the image is behind, promoting the eye axis direction. grow later, thereby deepening the degree. At present, one of the main ways to alleviate the development of myopia is the "defocus lens". The central area has a normal power, and the peripheral area is adjusted through optical design partitions, so that the image in the peripheral area falls in front of the retina.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

According to news on July 19, Xiaomi MIX Fold 4, the first flagship folding new phone, was officially released tonight and is equipped with a "three-dimensional special-shaped battery" for the first time. According to reports, Xiaomi MIX Fold4 has achieved a major breakthrough in battery technology and designed an innovative "three-dimensional special-shaped battery" specifically for folding screens. Traditional folding screen devices mostly use conventional square batteries, which have low space utilization efficiency. In order to solve this problem, Xiaomi did not use the common winding battery cells, but developed a new lamination process to create a new form of battery, which greatly improved the space utilization. Battery Technology Innovation In order to accurately alternately stack positive and negative electrode sheets and ensure the safe embedding of lithium ions, Xiaomi has developed a new ultrasonic welding machine and lamination machine to improve welding and cutting accuracy.

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

According to news on July 29, the Honor X60i mobile phone is officially on sale today, starting at 1,399 yuan. In terms of design, the Honor X60i mobile phone adopts a straight screen design with a hole in the center and almost unbounded ultra-narrow borders on all four sides, which greatly broadens the field of view. Honor X60i parameters Display: 6.7-inch high-definition display Battery: 5000mAh large-capacity battery Processor: Dimensity 6080 processor (TSMC 6nm, 2x2.4G A76+6×2G A55) System: MagicOS8.0 system Other features: 5G signal enhancement, smart capsule, under-screen fingerprint, dual MIC, noise reduction, knowledge Q&A, photography capabilities: rear dual camera system: 50 million pixels main camera, 2 million pixels auxiliary lens, front selfie lens: 8 million pixels, price: 8GB

According to news on May 8, Apple’s new iPad Pro/Air tablets have been released. According to Apple’s official website, the new iPad Pro and iPad Air no longer support the second-generation Apple Pencil released in 2018, but only support Apple Pencil Pro and Apple Pencil (USB- C). Apple Pencil (USB-C) will be released in November 2023. This stylus maintains the same pixel-level accuracy, low latency, and tilt-angle sensing features as the first and second-generation Apple Pencil, while eliminating pressure sensitivity. function and does not support wireless charging. Its price is 649 yuan. And the newly released ApplePe
