Design Theory for Relational Databases
Functional Dependencies
There is a design theory for relations that lets use examine a design carefully and make improvements based on a few simple principles.
The so called "functional dependencies" is a statement of a type that generalizes the idea of a key for a relation.
Definition of Functional Dependency
A functional dependency on a relation is a statement of the form "If two tuples of agree on all of the attributes ", then they must agree on all of another list of attributes .
We write this functional dependency as and say that " functionally determines ".
If we can make sure that every record of the relation will be one in which a given functional dependency is true, then we say the relation satisfies an functional dependency .
Keys of Relations
We say a set of one or more attributes is a key for a relation if:
- Those attributes functionally determine all other attributes of the relation.
- No proper subset of functionally determine all other attributes of .
Superkeys
A set of attributes that contains a key is called a superkey, short for "superset of a key". Every superkey satisfies the first condition of a key, however, the second condition is not satisfied for a superkey that contains more than just a key.
Rules About Functional Dependencies
Suppose we are told of a set of functional dependencies that a relation satisfies. Often, we can deduce that the relation must satisfy certain other functional dependencies. This ability of discovering additional functional dependencies is essential when we talk about the design of good relation schemas later.
Reasoning About Functional Dependencies
Functional dependencies often can be presented in several different ways, without changing the set of legal instances of the relation. We say:
The Splitting/Combining Rule
Splitting rule: If , then for .
Combining Rule: If for , then .
Trivial Functional Dependencies
A constraint of any kind on a relation is said to be trivial if it holds for every instance of the relation, regardless of what other constraints are assumed.
Say , if is not a subset of , then the functional dependency is not trivial. If is a subset of , the functional dependency is trivial.
Computing the Closure of Attributes
Starting with the given set of attributes, we repeatedly expand the set by adding the right sides of functional dependencies as soon as we have included their left sides. Eventually, we cannot expand the set any further, and the resulting set is the closure.
We denote the closure of a set of attributes by .
By computing the closure of any set of attributes, we can test whether any given functional dependency follows from a set of functional dependencies . If is in , then the functional dependency does follow from . Else, it does not follow from .
Armstrong Principles System
In 1974, W. W. Armstrong concluded a set of functional dependencies deduction rules, which is called the Armstrong Principles System.
Say a relation , is a set of attributes and is a set of functional dependencies. For the relation , we have such deduction rules:
- Reflexivity: If , then .
- Augmentation: If , then .
- Transitivity: If , then .
- The Combining Rule: If , then .
- The Pseudo Transitivity Rule: If , then .
- The Decomposition Rule: If and , then .
Closing Sets of Functional Dependencies
Sometimes we have a choice of which functional dependencies we use to represent the full set of functional dependencies for a relation. If we were given a set of functional dependencies , then any set of functional dependencies equivalent to is said to be a basis for S.
A minimal basis for a relation is a basis that satisfies three conditions:
- All the functional dependencies in have singleton right sides.
- If any functional dependency is removed from , the result is no longer a basis.
- If for any functional dependency in we remove one or more attributes from the left side of , the result is no longer a basis.
Projecting Functional Dependencies
Suppose we have a relation with set of functional dependencies , and we project by computing , for some list of attributes . When functional dependencies hold in ?
This algorithm is very useful when we need to decompose a relation.
Algorithm:
- Let be the eventual output set of functional dependencies. Initially, is empty.
- For each set of attributes that is a subset of the attributes of , compute . This computation is performed with respect to the set of functional dependencies , and may involve attributes that are in the schema of but not . Add to all nontrivial functional dependencies such that is both in and an attribute of .
- Now, is a basis for the functional dependencies that hold in , but may not be the minimal basis. We can construct a minimal basis by modifying as follows:
- If there is an functional dependency in that follows from the other functional dependencies in , remove from .
- Let be an functional dependency in , with at least two attributes in , and let be with one of its attributes removed. If follows from the functional dependencies in , then replace by .
- Repeat the above steps in all possible ways until no more changes to can be made.
By definition, it may be a little hard to understand. Let's take a example: Suppose has functional dependencies and . Suppose also that we wish to project out the attribute , leaving a relation . In principle, to find the functional dependencies for , we need to take the closure of all eight subsets of , using the full set of functional dependencies, including those involving . However, there are some obvious simplifications we can make.
Thus, we may start with the closures of the singleton sets, and then move on to the doubleton sets if necessary. For each closure of a set , we add the functional dependency for each attribute that is in and in the schema of , but not in .
First, . Thus, and hold in . Note that is true in , but makes no sense in because is not an attribute of .
Next, we consider , from which we get the additional functional dependency for .
Since , we can add no more functional dependencies, and are done with the singletons.
Since already includes all attributes in , there is no point in considering any superset of . Thus, the only doubleton whose closure we need to take is . This observation allows us to add nothing. We are done with the closures, and functional dependencies we have discovered are and . A simpler, equivalent set of functional dependencies for is .
Design of Relational Database Schemas
Careless selection of a relational database schema can lead to redundancy and related anomalies.
Anomalies
Problems such as redundancy occur when we try to cram too much into a single relation are called anomalies.
The principal kinds of anomalies that we encounter are:
- Redundancy
- Update Anomalies. We mat change information in one tuple but leave the same information unchanged in another.
- Deletion Anomalies. If a set of values becomes empty, we may lose other information as a side effect.
Decomposing Relations
The accepted way to eliminate these anomalies is to decompose relations. Decomposition of involves splitting the attributes of to make the schemas of two new relations.
Given a relation , we may decompose into two relations and such that:
- .
Boyce-Codd Normal Form
The goal of decomposition is to replace a relation by several that do not exhibit anomalies. There is a condition called Boyce-Codd normal form, or BCNF, under which the anomalies discussed can be guaranteed not to exist.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2020-02-28 編寫Makefile