什么是数据库范式,你能举例吗?

发布于 2024-07-17 05:07:57 字数 248 浏览 4 评论 0原文

在关系数据库设计中,有一个数据库规范化或者简称规范化的概念,它是组织列(属性)和表(关系)以减少数据冗余并提高数据完整性的过程。 维基百科

基于 1NF、2NF、3NF 和 3.5NF 的示例进行解释(博伊斯-Codd BCNF)是什么意思?

In relational database design, there is a concept of database normalization or simply normalization, which is a process of organizing columns (attributes) and tables (relations) to reduce data redundancy and improve data integrity. Wikipedia

What is an explanation based on examples about what 1NF, 2NF, 3NF, and 3.5NF (Boyce-Codd BCNF) mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萝莉病 2024-07-24 05:07:57

1NF 是最基本的范式 - 表中的每个单元格必须仅包含一条信息,并且不能有重复的行。

2NF 和 3NF 都是关于主键的依赖。 回想一下,主键可以由多个列组成。 正如克里斯在他的回复中所说:

数据取决于密钥 [1NF]、整个密钥 [2NF],除了密钥 [3NF] 之外什么都没有(所以帮助我 Codd)。

2NF

假设您有一个表,其中包含某个学期所修课程,并且您有以下数据:

|-----Primary Key----|               uh oh |
                                           V
CourseID | SemesterID | #Places  | Course Name  |
------------------------------------------------|
IT101    |   2009-1   | 100      | Programming  |
IT101    |   2009-2   | 100      | Programming  |
IT102    |   2009-1   | 200      | Databases    |
IT102    |   2010-1   | 150      | Databases    |
IT103    |   2009-2   | 120      | Web Design   |

不在 2NF 中,因为第四列不依赖于整个< /em> 键 - 但只是其中的一部分。 课程名称取决于课程 ID,但与参加哪个学期无关。因此,如您所见,我们有重复的信息 - 有几行告诉我们 IT101 是编程,IT102 是数据库。 因此,我们通过将课程名称移动到另一个表中来解决这个问题,其中 CourseID 是 ENTIRE 键。

Primary Key |

CourseID    |  Course Name |
---------------------------|
IT101       | Programming  |
IT102       | Databases    |
IT103       | Web Design   |

没有冗余!

3NF

好的,假设我们还将课程教师的姓名以及有关他们的一些详细信息添加到 RDBMS 中:

|-----Primary Key----|                           uh oh |
                                                       V
Course  |  Semester  |  #Places   |  TeacherID  | TeacherName  |
---------------------------------------------------------------|
IT101   |   2009-1   |  100       |  332        |  Mr Jones    |
IT101   |   2009-2   |  100       |  332        |  Mr Jones    |
IT102   |   2009-1   |  200       |  495        |  Mr Bentley  |
IT102   |   2010-1   |  150       |  332        |  Mr Jones    |
IT103   |   2009-2   |  120       |  242        |  Mrs Smith   |

现在希望可以明显看出 TeacherName 依赖于 TeacherID,而 TeacherID 不是主要的一部分key - 所以这不在 3NF 中。 为了解决这个问题,我们所做的与 2NF 中的操作大致相同 - 将 TeacherName 字段从该表中取出,并将其放入自己的表中,其中以 TeacherID 作为键。

 Primary Key |

 TeacherID   | TeacherName  |
 ---------------------------|
 332         |  Mr Jones    |
 495         |  Mr Bentley  |
 242         |  Mrs Smith   |

没有冗余!!

要记住的一件重要的事情是,如果某些东西不属于 1NF,那么它也不属于 2NF 或 3NF。 因此,每个附加的范式都需要较低范式所具有的一切,以及一些必须全部满足的额外条件。

1NF is the most basic of normal forms - each cell in a table must contain only one piece of information, and there can be no duplicate rows.

2NF and 3NF are all about being dependent on the primary key. Recall that a primary key can be made up of multiple columns. As Chris said in his response:

The data depends on the key [1NF], the whole key [2NF] and nothing but the key [3NF] (so help me Codd).

2NF

Say you have a table containing courses that are taken in a certain semester, and you have the following data:

|-----Primary Key----|               uh oh |
                                           V
CourseID | SemesterID | #Places  | Course Name  |
------------------------------------------------|
IT101    |   2009-1   | 100      | Programming  |
IT101    |   2009-2   | 100      | Programming  |
IT102    |   2009-1   | 200      | Databases    |
IT102    |   2010-1   | 150      | Databases    |
IT103    |   2009-2   | 120      | Web Design   |

This is not in 2NF, because the fourth column does not rely upon the entire key - but only a part of it. The course name is dependent on the Course's ID, but has nothing to do with which semester it's taken in. Thus, as you can see, we have duplicate information - several rows telling us that IT101 is programming, and IT102 is Databases. So we fix that by moving the course name into another table, where CourseID is the ENTIRE key.

Primary Key |

CourseID    |  Course Name |
---------------------------|
IT101       | Programming  |
IT102       | Databases    |
IT103       | Web Design   |

No redundancy!

3NF

Okay, so let's say we also add the name of the teacher of the course, and some details about them, into the RDBMS:

|-----Primary Key----|                           uh oh |
                                                       V
Course  |  Semester  |  #Places   |  TeacherID  | TeacherName  |
---------------------------------------------------------------|
IT101   |   2009-1   |  100       |  332        |  Mr Jones    |
IT101   |   2009-2   |  100       |  332        |  Mr Jones    |
IT102   |   2009-1   |  200       |  495        |  Mr Bentley  |
IT102   |   2010-1   |  150       |  332        |  Mr Jones    |
IT103   |   2009-2   |  120       |  242        |  Mrs Smith   |

Now hopefully it should be obvious that TeacherName is dependent on TeacherID which is not part of the primary key - so this is not in 3NF. To fix this, we do much the same as we did in 2NF - take the TeacherName field out of this table, and put it in its own, which has TeacherID as the key.

 Primary Key |

 TeacherID   | TeacherName  |
 ---------------------------|
 332         |  Mr Jones    |
 495         |  Mr Bentley  |
 242         |  Mrs Smith   |

No redundancy!!

One important thing to remember is that if something is not in 1NF, it is not in 2NF or 3NF either. So each additional Normal Form requires everything that the lower normal forms had, plus some extra conditions, which must all be fulfilled.

与之呼应 2024-07-24 05:07:57

我对精确措辞的记忆力一直不好,但在我的数据库课上,我认为教授总是这样说:

数据取决于密钥 [1NF]、整个密钥 [2NF],仅取决于密钥 [3NF]。

I've never had a good memory for exact wording, but in my database class I think the professor always said something like:

The data depends on the key [1NF], the whole key [2NF] and nothing but the key [3NF].

思慕 2024-07-24 05:07:57

这是一个快速的、诚然屠宰的回答,但用一句话来说:

1NF:您的表被组织为无序数据集,并且没有重复的列。

2NF:您不会因为另一列而在表的某一列中重复数据。

3NF:表中的每一列仅与表的键相关——表中不会有一列描述表中不是键的另一列。

有关更多详细信息,请参阅维基百科...

Here's a quick, admittedly butchered response, but in a sentence:

1NF : Your table is organized as an unordered set of data, and there are no repeating columns.

2NF: You don't repeat data in one column of your table because of another column.

3NF: Every column in your table relates only to your table's key -- you wouldn't have a column in a table that describes another column in your table which isn't the key.

For more detail, see wikipedia...

猫弦 2024-07-24 05:07:57

1NF:每列只有一个值

2NF:表中的所有非主键列都应依赖于整个主键。

3NF:表中的所有非主键列应直接依赖于整个主键。

我在此处写了一篇更详细的文章

1NF: Only one value per column

2NF: All the non primary key columns in the table should depend on the entire primary key.

3NF: All the non primary key columns in the table should depend DIRECTLY on the entire primary key.

I have written an article in more detail over here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文