MYSQL 中的规范化
MySQL 中的规范化是什么?在什么情况下以及如何使用它?
What is normalization in MySQL and in which case and how we need to use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
MySQL 中的规范化是什么?在什么情况下以及如何使用它?
What is normalization in MySQL and in which case and how we need to use it?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
我尝试在这里用外行术语来解释标准化。 首先,它适用于关系数据库(Oracle、Access、MySQL),因此它不仅仅适用于 MySQL。
规范化是为了确保每个表具有唯一的最小字段并消除依赖关系。 想象一下,您有一个员工记录,每个员工都属于一个部门。 如果将部门与员工的其他数据一起存储为字段,则会遇到问题 - 如果删除部门会发生什么? 您必须更新所有部门字段,并且有可能出错。 如果某些员工没有部门(也许是新分配的?)怎么办? 现在将会有空值。
因此,简而言之,规范化是为了避免字段为空,并确保表中的所有字段仅属于所描述的数据的一个域。 例如,在员工表中,字段可以是 id、姓名、社会安全号码,但这三个字段与部门无关。 只有员工 ID 描述了该员工属于哪个部门。 所以这意味着员工所在的部门应该在另一个表中。
这是一个简单的标准化过程。
正如所解释的,这没有标准化。 规范化形式可能如下所示
,此处,Employee 表仅负责一组数据。 那么我们在哪里存储员工属于哪个部门呢? 在另一个表中
这不是最佳的。 如果部门名称变了怎么办? (这种情况一直在美国政府中发生)。 因此最好这样做。
有第一范式、第二范式和第三范式。 但除非你正在学习数据库课程,否则我通常只会选择我能理解的最规范化的形式。
I try to attempt to explain normalization in layman terms here. First off, it is something that applies to relational database (Oracle, Access, MySQL) so it is not only for MySQL.
Normalisation is about making sure each table has the only minimal fields and to get rid of dependencies. Imagine you have an employee record, and each employee belongs to a department. If you store the department as a field along with the other data of the employee, you have a problem - what happens if a department is removed? You have to update all the department fields, and there's opportunity for error. And what if some employees does not have a department (newly assigned, perhaps?). Now there will be null values.
So the normalisation, in brief, is to avoid having fields that would be null, and making sure that the all the fields in the table only belong to one domain of data being described. For example, in the employee table, the fields could be id, name, social security number, but those three fields have nothing to do with the department. Only employee id describes which department the employee belongs to. So this implies that which department an employee is in should be in another table.
Here's a simple normalization process.
This is not normalized, as explained. A normalized form could look like
Here, the Employee table is only responsible for one set of data. So where do we store which department the employee belongs to? In another table
This is not optimal. What if the department name changes? (it happens in the US government all the time). Hence it is better to do this
There are first normal form, second normal form and third normal form. But unless you are studying a DB course, I usually just go for the most normalized form I could understand.
规范化不仅仅适用于 MYSql。 它是一个通用数据库概念。
SQL 中的范式如下所示。
另请参阅
数据库规范化基础知识
Normalization is not for MYSql only. Its a general database concept.
Normal forms in SQL are given below.
Seel also
Database Normalization Basics
这是一种通过消除重复来确保数据保持一致的技术。 因此,如果数据库中相同的信息存储在多个表中,那么该数据库就不是标准化的。
请参阅有关数据库规范化的维基百科文章。
(这是关系数据库的通用技术,不是 MySQL 特有的。)
It's a technique for ensuring that your data remains consistent, by eliminating duplication. So a database in which the same information is stored in more than one table is not normalized.
See the Wikipedia article on Database normalization.
(It's a general technique for relational databases, not specific to MySQL.)
在为应用程序创建数据库架构时,您需要确保避免任何信息存储在不同表的多个列中。
由于数据库中的每个表都标识应用程序中的重要实体,因此唯一标识符是它们的必备列。
现在,在决定存储模式时,正在识别这些实体(表)之间的各种关系,即一对一、一对多、多对多。
学生在大学中拥有独特的排名
类),同一个表可用于
存储列(来自两个表)。
一个学期可以有多个
课程),外键正在
在父表中创建。
一位教授照顾许多学生并且
反之亦然),第三个表需要
被创建(主键来自
两个表都作为复合键),以及
两个表的相关数据将
被存储。
一旦您处理了所有这些场景,您的数据库模式将标准化为 4NF。
While creating a database schema for your application, you need to make sure that you avoid any information being stored in more than one column across different tables.
As every table in your DB, identifies a significant entity in your application, a unique identifier is a must-have columns for them.
Now, while deciding the storage schema, various kinds of relationships are being identified between these entities (tables), viz-a-viz, one-to-one, one-to-many, many-to-many.
Student has a unique rank in the
class), same table could be used to
store columns (from both tables).
A semester can have multiple
courses), a foreign key is being
created in a parent table.
A Prof. attends to many students and
vice-versa), a third table needs to
be created (with primary key from
both tables as a composite key), and
related data of the both tables will
be stored.
Once you attend to all these scenarios, your db-schema will be normalized to 4NF.
编辑:来源:http://en.wikipedia.org/wiki/Database_normalization
Edit: Source: http://en.wikipedia.org/wiki/Database_normalization