2.数据库中范式

设计关系数据库时,遵从不同的规范要求,设计出合理的关系型数据库,这些不同的规范要求被称为不同的范式,各种范式呈递次规范,越高的范式数据库冗余越小。

固然范式可以避免数据冗余,减少数据库的空间,减轻维护数据完整性的麻烦,但是操作困难,因为需要联系多个表才能得到所需要数据,而且范式越高性能就会越差。要权衡是否使用更高范式是比较麻烦的,一般在项目中,用得最多的也就是第三范式,性能好而且方便管理数据。

First normal form

    First normal form (1NF) means that a table has no multivalued attributes or composite attributes. (A composite attribute contains other attributes and can therefore be divided

into smaller parts.) All relational tables are by definition in 1NF, because the value of any column in a row must beatomic—that is, single valued.

Second Normal Form 

A table is in second normal form (2NF) if it is in 1NF and there is no nonkey column dependent on a partial primary key of that table. This means if (A,B) is a combination of two table columns building the key of the new table, then there is no column of the table depending either on only A or only B.

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|  Semester  |  #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 theentire 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 putting 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!

Third Normal Form

A table is in third normal form (3NF) if it is in 2NF and there are no functional dependencies between nonkey columns.

第三范式(3NF)是第二范式(2NF)的一个子集,即满足第三范式(3NF)必须满足第二范式(2NF)。简而言之,第三范式(3NF)要求一个关系中不包含已在其它关系已包含的非主关键字信息。例如,存在一个部门信息表,其中每个部门有部门编号(dept_id)、部门名称、部门简介等信息。那么在的员工信息表中列出部门编号后就不能再将部门名称、部门简介等与部门有关的信息再加入员工信息表中。如果不存在部门信息表,则根据第三范式(3NF)也应该构建它,否则就会有大量的数据冗余。简而言之,第三范式就是属性不依赖于其它非主属性,也就是在满足2NF的基础上,任何非主属性不得传递依赖于主属性。

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 it should be obvious that TeacherName is dependent on TeacherID - so this isnot in 3NF. To fix this, we do much the same as we did in 2NF - take TeacherName 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.

 

 简单的说,
第一范式就是原子性,字段不可再分割也即表内每个记录中每个列的值只有有唯一值;
第二范式就是完全依赖,没有部分依赖也即非主属性只允许完全依赖于主属性;
第三范式就是没有传递依赖,也即除了要满足第二范式外,非主属性间不允许存在依赖关系。

 

发布了54 篇原创文章 · 获赞 6 · 访问量 3万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章