数据库三大范式 郝伟 2021/04/30 [TOC]

1. 简介

经过多年经验总结,数据库的设计如果满足一定的规则,那么就会具有良好的扩展性和可维护性。这里的规则就是三大范式。只要遵守了三大范式,我们就会发现数据库具有写入良好的SQL语句(选择,更新,插入或删除)将很难,有时实际上不可能使用大量的程序编码。

2. 数据库三大范式简单解释

第一范式原则(1NF):原子性,即无符合属性,属性不可分割,表中的字段是最小单位,无法再拆分成更小的单位了。 举例来说,学生信息组成学生信息表,有姓名、年龄、性别、学号等信息组成。不能将这些内容用一个字段表示,而是根据内容表示成多个字段。不过需要注意,最小单位也往往是由实际需求决定的,比如有的项目中姓名是一体的不需要分别处理,而在其他的一些项目中对姓名需要单独使用,此时姓名就需要拆分成姓和名两个字段。

第二范式(2NF):依赖性,即无部分依赖,所有字段都完全依赖于主键。本质上,所有的属性必需是此表的基本属性。 主键实际上就是一条记录的唯一性的保证,其他字段依赖于主键的具体含意是与主键所表示是相同一个对象。 举例来说,在订单表中,有订单编号、用户编号、用户名和用户手机。那么用户名和用户手机都违背了2NF,因为用户名和用户手机都只依赖于用户编号,而不依赖于订单号。

第三范式(3NF):独占性,即无传递性,不存在A.p => A->B.p 没有传递依赖。 所谓传递依赖,是指在主体中A可以推出B,B可以推出C,此时A与C就是传递依赖关系,C就是冗余的。 举例来说,在学生表中,有编号(A),出生所在市(B),出生所在省(C),那么有了编号可以推出出生所在市,而有了出生所在市即可推出出生所在省,此时编号和出生所在省就是传递依赖关系。

3. 五大范式+两个补充规则

  • 1NF (First Normal Form) 原子性,所有属性都不再拆分。 A relation R is in first normal form (1NF) if and only if all underlying domains contain atomic values only.

  • 2NF (Second Normal Form) 所有属性必需完全依赖于主键。 A relation R is in second normal form (2NF) if and only if it is in 1NF and every nonkey attribute is fully dependent on the primary key.

  • 3NF (Third Normal Form) 没有传递依赖。 A relation R is in third normal form (3NF) if and only if it is in 2NF and every nonkey attribute is non-transitively dependent on the primary key.

  • Boyce/Codd Normal Form A relation R is in Boyce/Codd normal form (BCNF) if and only if every determinant is a candidate key.

  • 4NF (Fourth Normal Form) A relation R is in fourth normal form (4NF) if and only if, wherever there exists an MVD in R, say A -> -> B, then all attributes of R are also functionally dependent on A. In other words, the only dependencies (FDs or MVDs) in R are of the form K -> X (i.e. a functional dependency from a candidate key K to some other attribute X). Equivalently: R is in 4NF if it is in BCNF and all MVDs in R are in fact FDs.

  • 5NF (Fifth Normal Form) A relation R is in fifth normal form (5NF) - also called projection-join normal form (PJ/NF) if and only if every join dependency in R is a consequence of the candidate keys of R.

For every normal form it is assumed that every occurrence of R can be uniquely identified by a primary key using one or more attributes in R.

FD = Functional Dependency MVD = Multi-Valued Dependency

4. 主键

A primary key has the following characteristics:

  • There can only be one primary key for a table.
  • The primary key consists of one or more columns.
  • The primary key enforces the entity integrity of the table.
  • All columns defined must be defined as NOT NULL.
  • The primary key uniquely identifies a row.
  • Primary keys result in CLUSTERED unique indexes by default.
  • The primary key of a row must not be modified once assigned.

5. 参考资料

results matching ""

    No results matching ""